Skip to content

Commit 48b0d01

Browse files
committed
wip
1 parent 53c1da5 commit 48b0d01

File tree

4 files changed

+20
-26
lines changed

4 files changed

+20
-26
lines changed

runner/daemon/src/mill/daemon/MillScalaParserImpl.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ object MillScalaParserImpl extends MillScalaParser {
3535
rawCode: String,
3636
fileName: String
3737
): Either[String, (String, Seq[String], Seq[String])] = {
38+
mill.constants.DebugLog.println("rawCode " + pprint.apply(rawCode))
3839
val source = SourceFile.virtual(fileName, rawCode)
3940
def mergeErrors(errors: List[String]): String =
4041
s"$fileName failed to parse:" + System.lineSeparator + errors.mkString(System.lineSeparator)
@@ -56,7 +57,7 @@ object MillScalaParserImpl extends MillScalaParser {
5657
if (!trees.head.startPos.exists) ""
5758
else new String(source.file.toByteArray).take(trees.head.startPos.start)
5859
}
59-
60+
mill.constants.DebugLog.println("pkgs " + pprint.apply(pkgs))
6061
(prefix, pkgs, stmts)
6162
}
6263
}

runner/meta/src/mill/meta/CodeGen.scala

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ object CodeGen {
1818
def generateWrappedAndSupportSources(
1919
projectRoot: os.Path,
2020
allScriptCode: Map[os.Path, String],
21+
allPackageStatements: Map[os.Path, String],
2122
wrappedDest: os.Path,
2223
supportDest: os.Path,
2324
millTopLevelProjectRoot: os.Path,
@@ -66,18 +67,23 @@ object CodeGen {
6667
}
6768
.mkString("\n ")
6869

69-
val pkg = pkgSelector0(Some(CGConst.globalPackagePrefix), None)
70-
7170
val aliasImports = Seq(
7271
// Provide `build` as an alias to the root `build_.package_`, since from the user's
7372
// perspective it looks like they're writing things that live in `package build`,
7473
// but at compile-time we rename things, we so provide an alias to preserve the fiction
7574
"import build_.{package_ => build}"
7675
).mkString("\n")
7776

78-
val scriptCode = allScriptCode(scriptPath)
77+
val strippedPackageStatementComment = allPackageStatements.get(scriptPath) match {
78+
// Add another comment after the marker comment to substitute any package statement
79+
// that was stripped during codegen and ensure the offsets line up properly
80+
case Some(s) => "\n//" + s.drop(2)
81+
case None => ""
82+
}
83+
84+
val markerComment = s"///SOURCE_CODE_START:$scriptPath" + strippedPackageStatementComment
85+
7986

80-
val markerComment = s"///SOURCE_CODE_START:$scriptPath"
8187

8288
val siblingScripts = scriptSources
8389
.filter(_ != scriptPath)
@@ -196,19 +202,6 @@ object CodeGen {
196202

197203
val scriptCode = allScriptCode(scriptPath)
198204

199-
val markerComment =
200-
s"""//SOURCECODE_ORIGINAL_FILE_PATH=$scriptPath
201-
|//SOURCECODE_ORIGINAL_CODE_START_MARKER""".stripMargin
202-
203-
val siblingScripts = scriptSources
204-
.filter(_ != scriptPath)
205-
.filter(p => (p / os.up) == (scriptPath / os.up))
206-
.map(_.last.split('.').head + "_")
207-
208-
val importSiblingScripts = siblingScripts
209-
.filter(s => s != "build_" && s != "package_")
210-
.map(s => s"import $pkg.${backtickWrap(s)}.*").mkString("\n")
211-
212205
if (isBuildScript) {
213206
os.write.over(supportDestDir / "MillMiscInfo.scala", miscInfo, createFolders = true)
214207
}

runner/meta/src/mill/meta/FileImportGraph.scala

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ import mill.internal.Util.backtickWrap
1717
* @param metaBuild If `true`, a meta-build is enabled
1818
*/
1919
@internal
20-
case class FileImportGraph(seenScripts: Map[os.Path, String], errors: Seq[String])
20+
case class FileImportGraph(seenScripts: Map[os.Path, String],
21+
errors: Seq[String],
22+
seenPkgStatements: Map[os.Path, String])
2123

2224
/**
2325
* Logic around traversing the `import $file` graph, extracting necessary info
@@ -42,11 +44,11 @@ object FileImportGraph {
4244
walked: Seq[os.Path]
4345
): FileImportGraph = {
4446
val seenScripts = mutable.Map.empty[os.Path, String]
47+
val seenPkgStatements = mutable.Map.empty[os.Path, String]
4548
val errors = mutable.Buffer.empty[String]
4649

4750
def processScript(s: os.Path, useDummy: Boolean = false): Unit =
4851
try {
49-
5052
val content = if (useDummy) "" else os.read(s)
5153
val fileName = s.relativeTo(topLevelProjectRoot).toString
5254
val buildHeaderError =
@@ -79,6 +81,7 @@ object FileImportGraph {
7981
)
8082
}
8183
seenScripts(s) = prefix + stmts.mkString
84+
seenPkgStatements(s) = "package " + importSegments
8285
case Left(error) =>
8386
seenScripts(s) = ""
8487
errors.append(error)
@@ -95,7 +98,7 @@ object FileImportGraph {
9598

9699
walked.foreach(processScript(_))
97100

98-
new FileImportGraph(seenScripts.toMap, errors.toSeq)
101+
new FileImportGraph(seenScripts.toMap, errors.toSeq, seenPkgStatements.toMap)
99102
}
100103

101104
def findRootBuildFiles(projectRoot: os.Path) = {

runner/meta/src/mill/meta/MillBuildRootModule.scala

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ trait MillBuildRootModule()(using
120120
CodeGen.generateWrappedAndSupportSources(
121121
rootModuleInfo.projectRoot / os.up,
122122
parsed.seenScripts,
123+
parsed.seenPkgStatements,
123124
wrapped,
124125
support,
125126
rootModuleInfo.topLevelProjectRoot,
@@ -131,11 +132,7 @@ trait MillBuildRootModule()(using
131132
}
132133

133134
def millBuildRootModuleResult = Task {
134-
Tuple3(
135-
runClasspath(),
136-
compile().classes,
137-
codeSignatures()
138-
)
135+
Tuple3(runClasspath(), compile().classes, codeSignatures())
139136
}
140137

141138
def codeSignatures: T[Map[String, Int]] = Task(persistent = true) {

0 commit comments

Comments
 (0)