Skip to content

Commit

Permalink
Whitelist some hardcoded targets, which allow run Mill without a buil…
Browse files Browse the repository at this point in the history
…d.sc
  • Loading branch information
lefou committed Jul 26, 2023
1 parent 718c614 commit 2b82b8e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
3 changes: 2 additions & 1 deletion bsp/worker/src/mill/bsp/worker/State.scala
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ private class State(projectRoot: os.Path, baseLogger: ColorLogger, debug: String
threadCount = None,
targetsAndParams = Seq("resolve", "_"),
prevRunnerState = mill.runner.RunnerState.empty,
logger = baseLogger
logger = baseLogger,
needBuildSc = true
).evaluate()

val rootModules0 = evaluated.result.frames
Expand Down
22 changes: 14 additions & 8 deletions runner/src/mill/runner/MillBuildBootstrap.scala
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ class MillBuildBootstrap(
threadCount: Option[Int],
targetsAndParams: Seq[String],
prevRunnerState: RunnerState,
logger: ColorLogger
logger: ColorLogger,
needBuildSc: Boolean
) {
import MillBuildBootstrap._

Expand All @@ -60,7 +61,7 @@ class MillBuildBootstrap(
}

def evaluateRec(depth: Int): RunnerState = {
println(s"+evaluateRec($depth) " + recRoot(projectRoot, depth))
// println(s"+evaluateRec($depth) " + recRoot(projectRoot, depth))
val prevFrameOpt = prevRunnerState.frames.lift(depth)
val prevOuterFrameOpt = prevRunnerState.frames.lift(depth - 1)

Expand All @@ -69,14 +70,19 @@ class MillBuildBootstrap(
// On this level we typically want assume a Mill project, which means we want to require an existing `build.sc`.
// Unfortunately, some targets also make sense without a `build.sc`, e.g. the `init` command.
// Hence we only report a missing `build.sc` as an problem if the command itself does not succeed.
val state = evaluateRec(depth + 1)
lazy val state = evaluateRec(depth + 1)
if (os.exists(recRoot(projectRoot, depth) / "build.sc")) state
else {
state match {
case RunnerState(bootstrapModuleOpt, frames, Some(error)) =>
val msg = "build.sc file not found. Are you in a Mill project folder?\n"
RunnerState(bootstrapModuleOpt, frames, Some(msg + error))
case state => state
val msg = "build.sc file not found. Are you in a Mill project folder?"
if (needBuildSc) {
RunnerState(None, Nil, Some(msg))
} else {
state match {
case RunnerState(bootstrapModuleOpt, frames, Some(error)) =>
// Add a potential clue (missing build.sc) to the underlying error message
RunnerState(bootstrapModuleOpt, frames, Some(msg + "\n" + error))
case state => state
}
}
}
} else {
Expand Down
16 changes: 15 additions & 1 deletion runner/src/mill/runner/MillMain.scala
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,8 @@ object MillMain {
threadCount = threadCount,
targetsAndParams = targetsAndParams,
prevRunnerState = prevState.getOrElse(stateCache),
logger = logger
logger = logger,
needBuildSc = needBuildSc(targetsAndParams)
).evaluate()
}
)
Expand Down Expand Up @@ -262,6 +263,19 @@ object MillMain {
logger
}

private def needBuildSc(targetsAndParams: Seq[String]): Boolean = {
// Tasks, for which running Mill without an existing buildfile is allowed.
val noBuildFileTaskWhitelist = Seq(
"init",
"version",
"mill.scalalib.giter8.Giter8Module/init"
)
val whitelistMatch =
targetsAndParams.nonEmpty && noBuildFileTaskWhitelist.exists(targetsAndParams.head == _)

!whitelistMatch
}

def checkMillVersionFromFile(projectDir: os.Path, stderr: PrintStream) = {
Seq(
projectDir / ".config" / "mill-version",
Expand Down

0 comments on commit 2b82b8e

Please sign in to comment.