Skip to content

Commit 999b4cf

Browse files
authored
Consolidate build-global APIs in mill.define.BuildCtx (#5142)
This PR standardizes the location of APIs that are global to the build on `mill.define.BuildCtx`, meant to be analogous to `EvalCtx` and `ModuleCtx`. Previously they were scattered on `mill.define.WorkspaceApi`,`build.interp`, and some direct calls to `os.checker.withValue`. Now, they all are consolidated under `BuildCtx` where they can be discovered and documented. Since the `watch` lists aren't conveniently bundled together with `RootModule` anymore, we cannot rely on `RootModuleApi` when passing them around `MillBuildBootstrap`, so I wrapped them in a `BuildFileApi` object to pass them around conveniently. This subsumes the previous `wrapper_object_getter` that we code-gened
1 parent d92beda commit 999b4cf

File tree

41 files changed

+235
-200
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+235
-200
lines changed

ci/mill-bootstrap.patch

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
diff --git a/build.mill b/build.mill
2+
index 4465e163a55..6b8da38aae7 100644
3+
--- a/build.mill
4+
+++ b/build.mill
5+
@@ -124,7 +124,7 @@ val bridgeScalaVersions = Seq(
6+
// on the fly anyway. For publishing, we publish everything or a specific version
7+
// if given.
8+
val compilerBridgeScalaVersions =
9+
- interp.watchValue(sys.env.get("MILL_COMPILER_BRIDGE_VERSIONS")) match {
10+
+ mill.define.BuildCtx.watchValue(sys.env.get("MILL_COMPILER_BRIDGE_VERSIONS")) match {
11+
case None | Some("") | Some("none") => Seq.empty[String]
12+
case Some("all") => (essentialBridgeScalaVersions ++ bridgeScalaVersions).distinct
13+
case Some("essential") => essentialBridgeScalaVersions
14+
@@ -181,7 +181,7 @@ def formatDep(dep: Dep) = {
15+
s"${d.module.organization.value}:${d.module.name.value}:${d.versionConstraint.asString}"
16+
}
17+
18+
-def listIn(path: os.Path) = interp.watchValue(os.list(path).map(_.last))
19+
+def listIn(path: os.Path) = mill.define.BuildCtx.watchValue(os.list(path).map(_.last))
20+
21+
val dummyDeps: Seq[Dep] = Seq(
22+
Deps.DocDeps.millScip,
23+
diff --git a/runner/codesig/package.mill b/runner/codesig/package.mill
24+
index 3950422474a..1ed233d24f7 100644
25+
--- a/runner/codesig/package.mill
26+
+++ b/runner/codesig/package.mill
27+
@@ -18,7 +18,7 @@ object `package` extends MillPublishScalaModule {
28+
29+
override lazy val test: CodeSigTests = new CodeSigTests {}
30+
trait CodeSigTests extends MillScalaTests {
31+
- val caseKeys = build.interp.watchValue(
32+
+ val caseKeys = mill.define.BuildCtx.watchValue(
33+
os.walk(moduleDir / "cases", maxDepth = 3)
34+
.map(_.subRelativeTo(moduleDir / "cases").segments)
35+
.collect { case Seq(a, b, c) => s"$a-$b-$c" }
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package mill.api.internal
2+
3+
import mill.api.Watchable
4+
5+
trait BuildFileApi {
6+
def rootModule: RootModuleApi
7+
def moduleWatchedValues: Seq[Watchable]
8+
def evalWatchedValues: collection.mutable.Buffer[Watchable]
9+
}
10+
object BuildFileApi {
11+
class Bootstrap(val rootModule: RootModuleApi) extends BuildFileApi {
12+
def moduleWatchedValues = Nil
13+
def evalWatchedValues = collection.mutable.Buffer[Watchable]()
14+
}
15+
}
Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
11
package mill.api.internal
22

3-
import mill.api.Watchable
4-
5-
import scala.collection.mutable
6-
trait RootModuleApi {
7-
protected[mill] def watchedValues: mutable.Buffer[Watchable]
8-
protected[mill] def evalWatchedValues: mutable.Buffer[Watchable]
9-
}
3+
trait RootModuleApi {}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package mill.define
2+
import collection.mutable
3+
import mill.api.Watchable
4+
import mill.constants.EnvVars
5+
object BuildCtx {
6+
val workspaceRoot: os.Path =
7+
sys.env.get(EnvVars.MILL_WORKSPACE_ROOT).fold(os.pwd)(os.Path(_, os.pwd))
8+
9+
def withFilesystemCheckerDisabled[T](block: => T): T =
10+
os.checker.withValue(os.Checker.Nop) { block }
11+
12+
protected[mill] val watchedValues: mutable.Buffer[Watchable] = mutable.Buffer.empty[Watchable]
13+
protected[mill] val evalWatchedValues: mutable.Buffer[Watchable] = mutable.Buffer.empty[Watchable]
14+
def watchValue[T](v0: => T)(implicit fn: sourcecode.FileName, ln: sourcecode.Line): T = {
15+
withFilesystemCheckerDisabled {
16+
val v = v0
17+
val watchable = Watchable.Value(
18+
() => v0.hashCode,
19+
v.hashCode(),
20+
fn.value + ":" + ln.value
21+
)
22+
watchedValues.append(watchable)
23+
v
24+
}
25+
}
26+
27+
def watch(p: os.Path): os.Path = {
28+
val watchable = Watchable.Path(p.toNIO, false, PathRef(p).sig)
29+
watchedValues.append(watchable)
30+
p
31+
}
32+
33+
def watch0(w: Watchable): Unit = watchedValues.append(w)
34+
35+
def evalWatch0(w: Watchable): Unit = evalWatchedValues.append(w)
36+
}

core/define/src/mill/define/Evaluator.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ trait Evaluator extends AutoCloseable with EvaluatorApi {
9191
serialCommandExec: Boolean = false,
9292
selectiveExecution: Boolean = false
9393
): EvaluatorApi.Result[T] = {
94-
os.checker.withValue(os.Checker.Nop) {
94+
mill.define.BuildCtx.withFilesystemCheckerDisabled {
9595
execute(
9696
targets.map(_.asInstanceOf[Task[T]]),
9797
reporter,

core/define/src/mill/define/ExternalModule.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ abstract class ExternalModule(implicit
1313
millModuleEnclosing0: sourcecode.Enclosing,
1414
millModuleLine0: sourcecode.Line,
1515
millFile0: sourcecode.File
16-
) extends BaseModule(WorkspaceRoot.workspaceRoot, external0 = true)(
16+
) extends BaseModule(BuildCtx.workspaceRoot, external0 = true)(
1717
millModuleEnclosing0,
1818
millModuleLine0,
1919
millFile0

core/define/src/mill/define/JsonFormatters.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,6 @@ object JsonFormatters extends JsonFormatters {
7979
private object PathTokensReader0 extends mainargs.TokensReader.Simple[os.Path] {
8080
def shortName = "path"
8181
def read(strs: Seq[String]): Either[String, Path] =
82-
Right(os.Path(strs.last, WorkspaceRoot.workspaceRoot))
82+
Right(os.Path(strs.last, BuildCtx.workspaceRoot))
8383
}
8484
}

core/define/src/mill/define/WorkspaceRoot.scala

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package mill.define.internal
2+
3+
class BuildFileCls(rootModule0: => mill.define.RootModule0) extends mill.api.internal.BuildFileApi {
4+
def value = this
5+
def checker = mill.define.internal.ResolveChecker(mill.define.BuildCtx.workspaceRoot)
6+
val rootModule = os.checker.withValue(checker) { rootModule0 }
7+
8+
def moduleWatchedValues = mill.define.BuildCtx.watchedValues.toSeq
9+
def evalWatchedValues = mill.define.BuildCtx.evalWatchedValues
10+
}

core/internal/src/mill/internal/FileLogger.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ private[mill] class FileLogger(
2424
var folderCreated = false
2525
// Lazily create the folder and file that we're logging to, so as to avoid spamming the out/
2626
// folder with empty folders/files for the vast majority of tasks that do not have any logs
27-
lazy val inner = os.checker.withValue(os.Checker.Nop) {
27+
lazy val inner = mill.define.BuildCtx.withFilesystemCheckerDisabled {
2828
if (!os.exists(file / os.up)) os.makeDir.all(file / os.up)
2929
folderCreated = true
3030
Files.newOutputStream(file.toNIO, options*)

0 commit comments

Comments
 (0)