Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't leak runtime cli imports into generated build scripts #2978

Merged
merged 1 commit into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh I never knew extends DynamicVariable was a thing, but I guess it works

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I first had it as a val in the companion object of MillBuildBootstrap, but acyclic wasn't happy with that. So I moved it and tried to avoid all that nesting, without using a val in a new package object and ended up with this compact thing.

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