@@ -1064,52 +1064,49 @@ final class BloopBspServices(
1064
1064
projects : Seq [ProjectMapping ],
1065
1065
state : State
1066
1066
): BspResult [bsp.SourcesResult ] = {
1067
- val sourcesItems = projects.iterator.map {
1068
- case (target, project) =>
1069
- def sourceItem (s : AbsolutePath , isGenerated : Boolean ): bsp.SourceItem = {
1070
- import bsp .SourceItemKind ._
1071
- val uri = s.underlying.toUri()
1072
- val (bspUri, kind) = if (s.exists) {
1073
- (uri, if (s.isFile) File else Directory )
1074
- } else {
1075
- val fileMatcher = FileSystems .getDefault.getPathMatcher(" glob:*.{scala, java}" )
1076
- if (fileMatcher.matches(s.underlying.getFileName)) (uri, File )
1077
- // If path doesn't exist and its name doesn't look like a file, assume it's a dir
1078
- else (new URI (uri.toString + " /" ), Directory )
1079
- }
1080
- bsp.SourceItem (bsp.Uri (bspUri), kind, isGenerated)
1081
- }
1067
+ def sourceItem (s : AbsolutePath , isGenerated : Boolean ): bsp.SourceItem = {
1068
+ import bsp .SourceItemKind ._
1069
+ val uri = s.underlying.toUri()
1070
+ val (bspUri, kind) = if (s.exists) {
1071
+ (uri, if (s.isFile) File else Directory )
1072
+ } else {
1073
+ val fileMatcher = FileSystems .getDefault.getPathMatcher(" glob:*.{scala, java}" )
1074
+ if (fileMatcher.matches(s.underlying.getFileName)) (uri, File )
1075
+ // If path doesn't exist and its name doesn't look like a file, assume it's a dir
1076
+ else (new URI (uri.toString + " /" ), Directory )
1077
+ }
1078
+ bsp.SourceItem (bsp.Uri (bspUri), kind, isGenerated)
1079
+ }
1082
1080
1083
- val unmanagedSources = project.allUnmanagedSourceFilesAndDirectories.map { sources =>
1084
- sources.map(sourceItem(_, isGenerated = false ))
1085
- }
1081
+ val dag = Aggregate (projects.map(p => state.build.getDagFor(p._2)).toList)
1086
1082
1087
- val parentGenerators = Dag .dfs(state.build.getDagFor(project))
1088
- val managedSources = Task
1089
- .sequence {
1090
- parentGenerators.reverse.map { project =>
1091
- val generators = project.sourceGenerators
1092
- val tasks = generators.map(
1093
- state.sourceGeneratorCache.update(_, state.logger, state.commonOptions)
1094
- )
1095
- Task .gatherUnordered(tasks)
1096
- }
1097
- }
1098
- .map {
1099
- // Take only the result of the last set of generators, since these are the generated
1100
- // sources of the project we're interested in.
1101
- _.lastOption.map(_.flatten.map(sourceItem(_, isGenerated = true ))).getOrElse(Nil )
1102
- }
1103
- .executeOn(ioScheduler)
1083
+ // Collect the projects' sources following the projects topological sorting, so that
1084
+ // source generators that depend on other source generators' outputs can run correctly.
1085
+ val collectSourcesTasks = Dag .topologicalSort(dag).map { project =>
1086
+ val unmanagedSources = project.allUnmanagedSourceFilesAndDirectories.map { sources =>
1087
+ sources.map(sourceItem(_, isGenerated = false ))
1088
+ }
1089
+
1090
+ val managedSources = {
1091
+ val tasks = project.sourceGenerators
1092
+ .map(state.sourceGeneratorCache.update(_, state.logger, state.commonOptions))
1093
+ Task .gatherUnordered(tasks).map(_.flatten.map(sourceItem(_, isGenerated = true )))
1094
+ }
1104
1095
1105
- val roots = project.sourceRoots.map(_.map(p => bsp.Uri (p.toBspUri)))
1106
- for {
1107
- unmanaged <- unmanagedSources
1108
- managed <- managedSources
1109
- } yield bsp.SourcesItem (target, unmanaged ++ managed, roots)
1110
- }.toList
1096
+ for {
1097
+ unmanaged <- unmanagedSources
1098
+ managed <- managedSources
1099
+ } yield (project, unmanaged ++ managed)
1100
+ }
1101
+
1102
+ val projectToTarget = projects.map { case (target, project) => project -> target }.toMap
1103
+ Task .sequence(collectSourcesTasks).map { results =>
1104
+ val items = results.flatMap {
1105
+ case (project, items) =>
1106
+ val roots = project.sourceRoots.map(_.map(p => bsp.Uri (p.toBspUri)))
1107
+ projectToTarget.get(project).map(bsp.SourcesItem (_, items, roots))
1108
+ }
1111
1109
1112
- Task .sequence(sourcesItems).map { items =>
1113
1110
(state, Right (bsp.SourcesResult (items)))
1114
1111
}
1115
1112
}
0 commit comments