Skip to content

Commit 18f05bd

Browse files
committed
rebase - import JsonFormatters in CodeGen
1 parent 1563089 commit 18f05bd

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

runner/src/mill/runner/CodeGen.scala

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ object CodeGen {
1515
allScriptCode: Map[os.Path, String],
1616
targetDest: os.Path,
1717
enclosingClasspath: Seq[os.Path],
18-
millTopLevelProjectRoot: os.Path
18+
millTopLevelProjectRoot: os.Path,
19+
isScala3: Boolean
1920
): Unit = {
2021
for (scriptSource <- scriptSources) {
2122
val scriptPath = scriptSource.path
@@ -100,7 +101,8 @@ object CodeGen {
100101
pkgLine,
101102
aliasImports,
102103
scriptCode,
103-
markerComment
104+
markerComment,
105+
isScala3
104106
)
105107
}
106108

@@ -118,15 +120,17 @@ object CodeGen {
118120
pkgLine: String,
119121
aliasImports: String,
120122
scriptCode: String,
121-
markerComment: String
123+
markerComment: String,
124+
isScala3: Boolean
122125
) = {
123126
val segments = scriptFolderPath.relativeTo(projectRoot).segments
124127

125128
val prelude = topBuildPrelude(
126129
segments,
127130
scriptFolderPath,
128131
enclosingClasspath,
129-
millTopLevelProjectRoot
132+
millTopLevelProjectRoot,
133+
isScala3
130134
)
131135

132136
val instrument = new ObjectDataInstrument(scriptCode)
@@ -182,10 +186,17 @@ object CodeGen {
182186
segments: Seq[String],
183187
scriptFolderPath: os.Path,
184188
enclosingClasspath: Seq[os.Path],
185-
millTopLevelProjectRoot: os.Path
189+
millTopLevelProjectRoot: os.Path,
190+
isScala3: Boolean
186191
): String = {
192+
val scala3imports = if isScala3 then {
193+
// (Scala 3) package is not part of implicit scope
194+
s"""import _root_.mill.main.TokenReaders.given, _root_.mill.api.JsonFormatters.given"""
195+
} else {
196+
""
197+
}
187198
s"""import _root_.mill.runner.MillBuildRootModule
188-
|import _root_.mill.main.TokenReaders.given // (Scala 3) package is not part of implicit scope
199+
|$scala3imports
189200
|@_root_.scala.annotation.nowarn
190201
|object MillMiscInfo extends MillBuildRootModule.MillMiscInfo(
191202
| ${enclosingClasspath.map(p => literalize(p.toString))},
@@ -216,11 +227,13 @@ object CodeGen {
216227

217228
// User code needs to be put in a separate class for proper submodule
218229
// object initialization due to https://github.com/scala/scala3/issues/21444
219-
s"""object $wrapperObjectName extends $wrapperObjectName{
230+
s"""object $wrapperObjectName extends $wrapperObjectName {
220231
| $childAliases
221-
| override lazy val millDiscover: _root_.mill.define.Discover = _root_.mill.define.Discover[this.type]
222232
|}
223-
|abstract class $wrapperObjectName $extendsClause {""".stripMargin
233+
|abstract class $wrapperObjectName $extendsClause {
234+
|override lazy val millDiscover: _root_.mill.define.Discover = {
235+
| _root_.mill.define.Discover[this.type]
236+
|}""".stripMargin
224237

225238
}
226239

runner/src/mill/runner/MillBuildRootModule.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,15 @@ abstract class MillBuildRootModule()(implicit
114114
val parsed = parseBuildFiles()
115115
if (parsed.errors.nonEmpty) Result.Failure(parsed.errors.mkString("\n"))
116116
else {
117+
val isScala3 = scalaVersion().startsWith("3.")
117118
CodeGen.generateWrappedSources(
118119
millBuildRootModuleInfo.projectRoot / os.up,
119120
scriptSources(),
120121
parsed.seenScripts,
121122
T.dest,
122123
millBuildRootModuleInfo.enclosingClasspath,
123-
millBuildRootModuleInfo.topLevelProjectRoot
124+
millBuildRootModuleInfo.topLevelProjectRoot,
125+
isScala3
124126
)
125127
Result.Success(Seq(PathRef(T.dest)))
126128
}

0 commit comments

Comments
 (0)