Skip to content

Commit

Permalink
Don't leak runtime cli imports into generated build scripts (#2978)
Browse files Browse the repository at this point in the history
Since the cli option `--import` is supposed to affect only the runtime
classpath, there is no need to persist it's value in the generated
scripts.

Instead, we hold it in a `DynamicVariable` at script evaluation time.

Pull request: #2978
  • Loading branch information
lefou committed Jan 18, 2024
1 parent 47c37f5 commit efcaf99
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 20 deletions.
8 changes: 8 additions & 0 deletions runner/src/mill/runner/CliImports.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package mill.runner

import scala.util.DynamicVariable

/**
* Hold additional runtime dependencies given via the `--import` cli option.
*/
private[runner] object CliImports extends DynamicVariable[Seq[String]](Seq.empty)
7 changes: 4 additions & 3 deletions runner/src/mill/runner/MillBuildBootstrap.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package mill.runner

import mill.util.{ColorLogger, PrefixLogger, Watchable}
import mill.main.BuildInfo
import mill.api.{PathRef, Val, internal}
Expand Down Expand Up @@ -44,7 +45,7 @@ class MillBuildBootstrap(
val millBootClasspath: Seq[os.Path] = prepareMillBootClasspath(projectRoot / "out")
val millBootClasspathPathRefs: Seq[PathRef] = millBootClasspath.map(PathRef(_, quick = true))

def evaluate(): Watching.Result[RunnerState] = {
def evaluate(): Watching.Result[RunnerState] = CliImports.withValue(imports) {
val runnerState = evaluateRec(0)

for ((frame, depth) <- runnerState.frames.zipWithIndex) {
Expand Down Expand Up @@ -101,8 +102,7 @@ class MillBuildBootstrap(
new MillBuildRootModule.BootstrapModule(
projectRoot,
recRoot(projectRoot, depth),
millBootClasspath,
imports.collect { case s"ivy:$rest" => rest }
millBootClasspath
)(
mill.main.RootModule.Info(
recRoot(projectRoot, depth),
Expand Down Expand Up @@ -466,4 +466,5 @@ object MillBuildBootstrap {
def recOut(projectRoot: os.Path, depth: Int): os.Path = {
projectRoot / "out" / Seq.fill(depth)("mill-build")
}

}
34 changes: 17 additions & 17 deletions runner/src/mill/runner/MillBuildRootModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,13 @@ class MillBuildRootModule()(implicit
}
}

def cliImports: Target[Seq[String]] = T.input { millBuildRootModuleInfo.cliImports }
def cliImports: Target[Seq[String]] = T.input {
val imports = CliImports.value
if (imports.nonEmpty) {
T.log.debug(s"Using cli-provided runtime imports: ${imports.mkString(", ")}")
}
imports
}

override def ivyDeps = T {
Agg.from(
Expand All @@ -94,8 +100,10 @@ class MillBuildRootModule()(implicit
}

override def runIvyDeps = T {
val imports = cliImports()
val ivyImports = imports.collect { case s"ivy:$rest" => rest }
Agg.from(
MillIvy.processMillIvyDepSignature(cliImports().toSet)
MillIvy.processMillIvyDepSignature(ivyImports.toSet)
.map(mill.scalalib.Dep.parse)
)
}
Expand All @@ -116,8 +124,7 @@ class MillBuildRootModule()(implicit
parsed.seenScripts,
T.dest,
millBuildRootModuleInfo.enclosingClasspath,
millBuildRootModuleInfo.topLevelProjectRoot,
cliImports()
millBuildRootModuleInfo.topLevelProjectRoot
)
Result.Success(Seq(PathRef(T.dest)))
}
Expand Down Expand Up @@ -253,15 +260,13 @@ object MillBuildRootModule {
class BootstrapModule(
topLevelProjectRoot0: os.Path,
projectRoot: os.Path,
enclosingClasspath: Seq[os.Path],
cliImports: Seq[String]
enclosingClasspath: Seq[os.Path]
)(implicit baseModuleInfo: RootModule.Info) extends RootModule {

implicit private def millBuildRootModuleInfo: Info = MillBuildRootModule.Info(
enclosingClasspath,
projectRoot,
topLevelProjectRoot0,
cliImports
topLevelProjectRoot0
)
object build extends MillBuildRootModule

Expand All @@ -272,8 +277,7 @@ object MillBuildRootModule {
case class Info(
enclosingClasspath: Seq[os.Path],
projectRoot: os.Path,
topLevelProjectRoot: os.Path,
cliImports: Seq[String]
topLevelProjectRoot: os.Path
)

def parseBuildFiles(millBuildRootModuleInfo: MillBuildRootModule.Info): FileImportGraph = {
Expand All @@ -289,8 +293,7 @@ object MillBuildRootModule {
scriptCode: Map[os.Path, String],
targetDest: os.Path,
enclosingClasspath: Seq[os.Path],
millTopLevelProjectRoot: os.Path,
cliImports: Seq[String]
millTopLevelProjectRoot: os.Path
): Unit = {
for (scriptSource <- scriptSources) {
val relative = scriptSource.path.relativeTo(base)
Expand All @@ -303,8 +306,7 @@ object MillBuildRootModule {
scriptSource.path.baseName,
enclosingClasspath,
millTopLevelProjectRoot,
scriptSource.path,
cliImports
scriptSource.path
) +
scriptCode(scriptSource.path) +
MillBuildRootModule.bottom
Expand All @@ -320,8 +322,7 @@ object MillBuildRootModule {
name: String,
enclosingClasspath: Seq[os.Path],
millTopLevelProjectRoot: os.Path,
originalFilePath: os.Path,
cliImports: Seq[String]
originalFilePath: os.Path
): String = {
val superClass =
if (pkg.size <= 1 && name == "build") "_root_.mill.main.RootModule"
Expand Down Expand Up @@ -352,7 +353,6 @@ object MillBuildRootModule {
| ${enclosingClasspath.map(p => literalize(p.toString))}.map(_root_.os.Path(_)),
| _root_.os.Path(${literalize(base.toString)}),
| _root_.os.Path(${literalize(millTopLevelProjectRoot.toString)}),
| _root_.scala.Seq(${cliImports.map(literalize(_)).mkString(", ")})
| )
| implicit lazy val millBaseModuleInfo: _root_.mill.main.RootModule.Info = _root_.mill.main.RootModule.Info(
| millBuildRootModuleInfo.projectRoot,
Expand Down

0 comments on commit efcaf99

Please sign in to comment.