Skip to content

Commit 812bce1

Browse files
committed
Add ComposeBuild, correctly combine classpaths of modules that have dependencies, WIP - test still needs fixing
1 parent f754c15 commit 812bce1

File tree

17 files changed

+431
-364
lines changed

17 files changed

+431
-364
lines changed

Diff for: modules/build/src/main/scala/scala/build/bsp/BloopSession.scala

+11-11
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@ package scala.build.bsp
33
import com.swoval.files.PathWatchers
44

55
import java.util.concurrent.atomic.AtomicReference
6-
import scala.build.Build
6+
77
import scala.build.compiler.BloopCompiler
8-
import scala.build.compose.{Inputs, input as compose}
98
import scala.build.input.{Module, OnDisk, SingleFile, Virtual}
9+
import scala.build.{Build, compose}
1010

1111
final class BloopSession(
12-
val inputs: Inputs,
13-
// val inputsHash: String, TODO Fix inputs hash comparing
14-
val remoteServer: BloopCompiler,
15-
val bspServer: BspServer,
16-
val watcher: Build.Watcher
12+
val inputs: compose.Inputs,
13+
// val inputsHash: String, TODO Fix inputs hash comparing
14+
val remoteServer: BloopCompiler,
15+
val bspServer: BspServer,
16+
val watcher: Build.Watcher
1717
) {
1818
def resetDiagnostics(localClient: BspClient): Unit = for {
1919
module <- inputs.modules
@@ -65,10 +65,10 @@ final class BloopSession(
6565
object BloopSession {
6666

6767
def apply(
68-
inputs: Inputs,
69-
remoteServer: BloopCompiler,
70-
bspServer: BspServer,
71-
watcher: Build.Watcher
68+
inputs: compose.Inputs,
69+
remoteServer: BloopCompiler,
70+
bspServer: BspServer,
71+
watcher: Build.Watcher
7272
): BloopSession = new BloopSession(inputs, remoteServer, bspServer, watcher)
7373

7474
final class Reference {

Diff for: modules/build/src/main/scala/scala/build/bsp/Bsp.scala

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ package scala.build.bsp
22

33
import java.io.{InputStream, OutputStream}
44

5+
import scala.build.compose
56
import scala.build.errors.BuildException
6-
import scala.build.input.{Module, ScalaCliInvokeData, compose}
7+
import scala.build.input.{Module, ScalaCliInvokeData}
78
import scala.concurrent.Future
89

910
trait Bsp {

Diff for: modules/build/src/main/scala/scala/build/bsp/BspImpl.scala

+42-175
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,19 @@ import org.eclipse.lsp4j.jsonrpc.messages.ResponseError
1010
import java.io.{InputStream, OutputStream}
1111
import java.util.UUID
1212
import java.util.concurrent.{CompletableFuture, Executor}
13+
1314
import scala.build.EitherCps.{either, value}
1415
import scala.build.*
1516
import scala.build.bsp.buildtargets.{ManagesBuildTargets, ProjectName}
1617
import scala.build.compiler.BloopCompiler
17-
import scala.build.compose.Inputs
18-
import scala.build.errors.{BuildException, CompositeBuildException, Diagnostic, ParsingInputsException}
18+
import scala.build.compose.{ComposeBuild, Inputs}
19+
import scala.build.errors.{
20+
BuildException,
21+
CompositeBuildException,
22+
Diagnostic,
23+
ParsingInputsException
24+
}
25+
import scala.build.input.{Module, ScalaCliInvokeData}
1926
import scala.build.internal.Constants
2027
import scala.build.options.{BuildOptions, Scope}
2128
import scala.collection.mutable.ListBuffer
@@ -38,12 +45,12 @@ import scala.util.{Failure, Success}
3845
* the output stream of bytes
3946
*/
4047
final class BspImpl(
41-
argsToInputs: Seq[String] => Either[BuildException, Inputs],
42-
bspReloadableOptionsReference: BspReloadableOptions.Reference,
43-
threads: BspThreads,
44-
in: InputStream,
45-
out: OutputStream,
46-
actionableDiagnostics: Option[Boolean]
48+
argsToInputs: Seq[String] => Either[BuildException, Inputs],
49+
bspReloadableOptionsReference: BspReloadableOptions.Reference,
50+
threads: BspThreads,
51+
in: InputStream,
52+
out: OutputStream,
53+
actionableDiagnostics: Option[Boolean]
4754
)(using ScalaCliInvokeData) extends Bsp {
4855

4956
import BspImpl.{
@@ -92,142 +99,18 @@ final class BspImpl(
9299
currentBloopSession: BloopSession,
93100
reloadableOptions: BspReloadableOptions,
94101
maybeRecoverOnError: ProjectName => BuildException => Option[BuildException] = _ => e => Some(e)
95-
): Either[(BuildException, ProjectName), PreBuildProject] =
96-
either[(BuildException, ProjectName)] {
97-
val logger = reloadableOptions.logger
98-
val buildOptions = reloadableOptions.buildOptions
99-
val verbosity = reloadableOptions.verbosity
100-
logger.log("Preparing build")
101-
102-
val persistentLogger = new PersistentDiagnosticLogger(logger)
103-
val bspServer = currentBloopSession.bspServer
104-
105-
val prebuildModules = for (module <- currentBloopSession.inputs.modulesBuildOrder) yield {
106-
val mainProjectName = module.projectName
107-
val testProjectName = module.scopeProjectName(Scope.Test)
108-
109-
// allInputs contains elements from using directives
110-
val (crossSources, allInputs) = value {
111-
CrossSources.forModuleInputs(
112-
inputs = module,
113-
preprocessors = Sources.defaultPreprocessors(
114-
buildOptions.archiveCache,
115-
buildOptions.internal.javaClassNameVersionOpt,
116-
() => buildOptions.javaHome().value.javaCommand
117-
),
118-
logger = persistentLogger,
119-
suppressWarningOptions = buildOptions.suppressWarningOptions,
120-
exclude = buildOptions.internal.exclude,
121-
maybeRecoverOnError = maybeRecoverOnError(mainProjectName)
122-
).left.map(_ -> mainProjectName)
123-
}
124-
125-
val sharedOptions = crossSources.sharedOptions(buildOptions)
126-
127-
if (verbosity >= 3)
128-
pprint.err.log(crossSources)
129-
130-
val scopedSources =
131-
value(crossSources.scopedSources(buildOptions).left.map(_ -> mainProjectName))
132-
133-
if (verbosity >= 3)
134-
pprint.err.log(scopedSources)
135-
136-
val sourcesMain = value {
137-
scopedSources.sources(Scope.Main, sharedOptions, allInputs.workspace, persistentLogger)
138-
.left.map(_ -> mainProjectName)
139-
}
140-
141-
val sourcesTest = value {
142-
scopedSources.sources(Scope.Test, sharedOptions, allInputs.workspace, persistentLogger)
143-
.left.map(_ -> testProjectName)
144-
}
145-
146-
if (verbosity >= 3)
147-
pprint.err.log(sourcesMain)
148-
149-
val options0Main = sourcesMain.buildOptions
150-
val options0Test = sourcesTest.buildOptions.orElse(options0Main)
151-
152-
val generatedSourcesMain =
153-
sourcesMain.generateSources(allInputs.generatedSrcRoot(Scope.Main))
154-
val generatedSourcesTest =
155-
sourcesTest.generateSources(allInputs.generatedSrcRoot(Scope.Test))
156-
157-
bspServer.setExtraDependencySources(options0Main.classPathOptions.extraSourceJars)
158-
bspServer.setExtraTestDependencySources(options0Test.classPathOptions.extraSourceJars)
159-
bspServer.setGeneratedSources(mainProjectName, generatedSourcesMain)
160-
bspServer.setGeneratedSources(testProjectName, generatedSourcesTest)
161-
162-
val (classesDir0Main, scalaParamsMain, artifactsMain, projectMain, buildChangedMain) =
163-
value {
164-
val res = Build.prepareBuild(
165-
allInputs,
166-
sourcesMain,
167-
generatedSourcesMain,
168-
options0Main,
169-
None,
170-
Scope.Main,
171-
currentBloopSession.remoteServer,
172-
persistentLogger,
173-
localClient,
174-
maybeRecoverOnError(mainProjectName)
175-
)
176-
res.left.map(_ -> mainProjectName)
177-
}
178-
179-
val (classesDir0Test, scalaParamsTest, artifactsTest, projectTest, buildChangedTest) =
180-
value {
181-
val res = Build.prepareBuild(
182-
allInputs,
183-
sourcesTest,
184-
generatedSourcesTest,
185-
options0Test,
186-
None,
187-
Scope.Test,
188-
currentBloopSession.remoteServer,
189-
persistentLogger,
190-
localClient,
191-
maybeRecoverOnError(testProjectName)
192-
)
193-
res.left.map(_ -> testProjectName)
194-
}
195-
196-
localClient.setGeneratedSources(mainProjectName, generatedSourcesMain)
197-
localClient.setGeneratedSources(testProjectName, generatedSourcesTest)
198-
199-
val mainScope = PreBuildData(
200-
sourcesMain,
201-
options0Main,
202-
classesDir0Main,
203-
scalaParamsMain,
204-
artifactsMain,
205-
projectMain,
206-
generatedSourcesMain,
207-
buildChangedMain
208-
)
209-
210-
val testScope = PreBuildData(
211-
sourcesTest,
212-
options0Test,
213-
classesDir0Test,
214-
scalaParamsTest,
215-
artifactsTest,
216-
projectTest,
217-
generatedSourcesTest,
218-
buildChangedTest
219-
)
220-
221-
if (actionableDiagnostics.getOrElse(true)) {
222-
val projectOptions = options0Test.orElse(options0Main)
223-
projectOptions.logActionableDiagnostics(persistentLogger)
224-
}
225-
226-
PreBuildModule(module, mainScope, testScope, persistentLogger.diagnostics)
227-
}
228-
229-
PreBuildProject(prebuildModules)
230-
}
102+
): Either[(BuildException, ProjectName), ComposeBuild.PreBuildProject] =
103+
ComposeBuild(
104+
buildOptions = reloadableOptions.buildOptions,
105+
inputs = currentBloopSession.inputs,
106+
logger = reloadableOptions.logger,
107+
compiler = currentBloopSession.remoteServer,
108+
buildClient = localClient,
109+
bspServer = Some(currentBloopSession.bspServer),
110+
actionableDiagnostics = actionableDiagnostics,
111+
verbosity = reloadableOptions.verbosity,
112+
maybeRecoverOnError = maybeRecoverOnError
113+
).prepareBuild()
231114

232115
private def buildE(
233116
currentBloopSession: BloopSession,
@@ -236,7 +119,7 @@ final class BspImpl(
236119
): Either[(BuildException, ProjectName), Unit] = {
237120
def doBuildOnce(
238121
moduleInputs: Module,
239-
data: PreBuildData,
122+
data: ComposeBuild.PreBuildData,
240123
scope: Scope
241124
): Either[(BuildException, ProjectName), Build] =
242125
Build.buildOnce(
@@ -254,7 +137,7 @@ final class BspImpl(
254137
either[(BuildException, ProjectName)] {
255138
val preBuild = value(prepareBuild(currentBloopSession, reloadableOptions))
256139
for (preBuildModule <- preBuild.prebuildModules) do {
257-
val moduleInputs = preBuildModule.inputs
140+
val moduleInputs = preBuildModule.module
258141
// TODO notify only specific build target
259142
if (
260143
notifyChanges && (preBuildModule.mainScope.buildChanged || preBuildModule.testScope.buildChanged)
@@ -327,22 +210,7 @@ final class BspImpl(
327210
executor
328211
)
329212

330-
preBuild.thenCompose { x =>
331-
Thread.sleep(1000)
332-
x.foreach{ r =>
333-
r.prebuildModules.foreach{ m =>
334-
pprint.err.log(m.mainScope.project.projectName)
335-
m.mainScope.sources.paths.foreach(path => pprint.err.log(os.read(path._1)))
336-
m.mainScope.project.classPath.foreach(p => pprint.err.log(p.toString))
337-
}
338-
r.prebuildModules.foreach { m =>
339-
pprint.err.log(m.testScope.project.projectName)
340-
m.testScope.sources.paths.foreach(path => pprint.err.log(os.read(path._1)))
341-
m.testScope.project.classPath.foreach(p => pprint.err.log(p.toString))
342-
}
343-
}
344-
CompletableFuture.supplyAsync(() => x, executor)
345-
}.thenCompose {
213+
preBuild.thenCompose {
346214
case Left((ex, projectName)) =>
347215
val taskId = new b.TaskId(UUID.randomUUID().toString)
348216

@@ -379,14 +247,13 @@ final class BspImpl(
379247
new b.CompileResult(b.StatusCode.ERROR)
380248
)
381249
case Right(params) =>
382-
println(params)
383250
for (targetId <- currentBloopSession.bspServer.targetIds)
384251
actualLocalClient.resetBuildExceptionDiagnostics(targetId)
385252

386253
for {
387254
preBuildModule <- params.prebuildModules
388255
targetId <- currentBloopSession.bspServer
389-
.targetProjectIdOpt(preBuildModule.inputs.projectName)
256+
.targetProjectIdOpt(preBuildModule.module.projectName)
390257
.toSeq
391258
} do
392259
actualLocalClient.reportDiagnosticsForFiles(
@@ -396,7 +263,7 @@ final class BspImpl(
396263
)
397264

398265
doCompile().thenCompose { res =>
399-
def doPostProcess(inputs: Module, data: PreBuildData, scope: Scope): Unit =
266+
def doPostProcess(inputs: Module, data: ComposeBuild.PreBuildData, scope: Scope): Unit =
400267
for (sv <- data.project.scalaCompiler.map(_.scalaVersion))
401268
Build.postProcess(
402269
data.generatedSources,
@@ -413,7 +280,7 @@ final class BspImpl(
413280
CompletableFuture.supplyAsync(
414281
() => {
415282
for (preBuildModule <- params.prebuildModules) do {
416-
val moduleInputs = preBuildModule.inputs
283+
val moduleInputs = preBuildModule.module
417284
doPostProcess(moduleInputs, preBuildModule.mainScope, Scope.Main)
418285
doPostProcess(moduleInputs, preBuildModule.testScope, Scope.Test)
419286
}
@@ -451,9 +318,9 @@ final class BspImpl(
451318
* a new [[BloopSession]]
452319
*/
453320
private def newBloopSession(
454-
inputs: Inputs,
455-
reloadableOptions: BspReloadableOptions,
456-
presetIntelliJ: Boolean = false
321+
inputs: Inputs,
322+
reloadableOptions: BspReloadableOptions,
323+
presetIntelliJ: Boolean = false
457324
): BloopSession = {
458325
val logger = reloadableOptions.logger
459326
val buildOptions = reloadableOptions.buildOptions
@@ -504,8 +371,8 @@ final class BspImpl(
504371
* change on subsequent workspace/reload requests)
505372
*/
506373
override def run(
507-
initialInputs: Inputs,
508-
initialBspOptions: BspReloadableOptions
374+
initialInputs: Inputs,
375+
initialBspOptions: BspReloadableOptions
509376
): Future[Unit] = {
510377
val logger = initialBspOptions.logger
511378
val verbosity = initialBspOptions.verbosity
@@ -607,10 +474,10 @@ final class BspImpl(
607474
* a future containing a valid workspace/reload response
608475
*/
609476
private def reloadBsp(
610-
currentBloopSession: BloopSession,
611-
previousInputs: Inputs,
612-
newInputs: Inputs,
613-
reloadableOptions: BspReloadableOptions
477+
currentBloopSession: BloopSession,
478+
previousInputs: Inputs,
479+
newInputs: Inputs,
480+
reloadableOptions: BspReloadableOptions
614481
): CompletableFuture[AnyRef] = {
615482
val previousTargetIds = currentBloopSession.bspServer.targetIds
616483
val wasIntelliJ = currentBloopSession.bspServer.isIntelliJ

Diff for: modules/build/src/main/scala/scala/build/bsp/BuildServerProxy.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import ch.epfl.scala.bsp4j as b
44

55
import java.util.concurrent.CompletableFuture
66

7-
import scala.build.GeneratedSource
87
import scala.build.bsp.buildtargets.{ManagesBuildTargets, ProjectName}
9-
import scala.build.input.{Module, compose}
8+
import scala.build.input.Module
109
import scala.build.options.Scope
10+
import scala.build.{GeneratedSource, compose}
1111

1212
/** A wrapper for [[BspServer]], allowing to reload the workspace on the fly.
1313
* @param bspServer

Diff for: modules/build/src/main/scala/scala/build/bsp/buildtargets/ManagesBuildTargets.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ package scala.build.bsp.buildtargets
33
import ch.epfl.scala.bsp4j.BuildTargetIdentifier
44
import ch.epfl.scala.bsp4j as b
55

6-
import scala.build.GeneratedSource
7-
import scala.build.input.{Module, compose}
6+
import scala.build.input.Module
87
import scala.build.internal.Constants
98
import scala.build.options.Scope
9+
import scala.build.{GeneratedSource, compose}
1010

1111
trait ManagesBuildTargets {
1212
def targetIds: List[b.BuildTargetIdentifier]

0 commit comments

Comments
 (0)