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

Fix #2881 Propagate environment from ctx to subprocesses #2882

Closed
wants to merge 2 commits into from
Closed
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
6 changes: 6 additions & 0 deletions build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,12 @@ trait MillBaseTestsModule extends MillJavaModule with TestModule {
)
}

def forkEnv = T {
super.forkEnv() ++ Seq(
"TEST_ENV_VARIABLE" -> "value"
)
}

def testFramework = "mill.UTestFramework"
}

Expand Down
53 changes: 47 additions & 6 deletions main/util/src/mill/util/Jvm.scala
Original file line number Diff line number Diff line change
Expand Up @@ -146,21 +146,46 @@ object Jvm extends CoursierSupport {

ctx.log.debug(s"Run subprocess with args: ${args.map(a => s"'${a}'").mkString(" ")}")

val envWithPropagated = ctx.env ++ envArgs

if (backgroundOutputs.nonEmpty)
spawnSubprocessWithBackgroundOutputs(args, envArgs, workingDir, backgroundOutputs)
spawnSubprocessWithBackgroundOutputs(
args,
envWithPropagated,
workingDir,
backgroundOutputs,
propagateEnv = false
)
else
runSubprocess(args, envArgs, workingDir)
runSubprocess(args, envWithPropagated, workingDir, propagateEnv = false)
}

/**
* Runs a generic subprocess and waits for it to terminate.
*/
def runSubprocess(commandArgs: Seq[String], envArgs: Map[String, String], workingDir: os.Path) = {
def runSubprocess(
commandArgs: Seq[String],
envArgs: Map[String, String],
workingDir: os.Path
): Unit = runSubprocess(
commandArgs = commandArgs,
envArgs = envArgs,
workingDir = workingDir,
propagateEnv = true
)

private def runSubprocess(
commandArgs: Seq[String],
envArgs: Map[String, String],
workingDir: os.Path,
propagateEnv: Boolean
): Unit = {
val process = spawnSubprocessWithBackgroundOutputs(
commandArgs,
envArgs,
workingDir,
backgroundOutputs = None
backgroundOutputs = None,
propagateEnv = propagateEnv
)
val shutdownHook = new Thread("subprocess-shutdown") {
override def run(): Unit = {
Expand Down Expand Up @@ -217,6 +242,20 @@ object Jvm extends CoursierSupport {
envArgs: Map[String, String],
workingDir: os.Path,
backgroundOutputs: Option[Tuple2[ProcessOutput, ProcessOutput]] = None
): SubProcess = spawnSubprocessWithBackgroundOutputs(
commandArgs = commandArgs,
envArgs = envArgs,
workingDir = workingDir,
backgroundOutputs = backgroundOutputs,
propagateEnv = true
)

private def spawnSubprocessWithBackgroundOutputs(
commandArgs: Seq[String],
envArgs: Map[String, String],
workingDir: os.Path,
backgroundOutputs: Option[Tuple2[ProcessOutput, ProcessOutput]],
propagateEnv: Boolean
): SubProcess = {
// If System.in is fake, then we pump output manually rather than relying
// on `os.Inherit`. That is because `os.Inherit` does not follow changes
Expand All @@ -230,7 +269,8 @@ object Jvm extends CoursierSupport {
env = envArgs,
stdin = if (backgroundOutputs.isEmpty) os.Pipe else "",
stdout = backgroundOutputs.map(_._1).getOrElse(os.Pipe),
stderr = backgroundOutputs.map(_._2).getOrElse(os.Pipe)
stderr = backgroundOutputs.map(_._2).getOrElse(os.Pipe),
propagateEnv = propagateEnv
)

val sources = Seq(
Expand All @@ -255,7 +295,8 @@ object Jvm extends CoursierSupport {
env = envArgs,
stdin = if (backgroundOutputs.isEmpty) os.Inherit else "",
stdout = backgroundOutputs.map(_._1).getOrElse(os.Inherit),
stderr = backgroundOutputs.map(_._2).getOrElse(os.Inherit)
stderr = backgroundOutputs.map(_._2).getOrElse(os.Inherit),
propagateEnv = propagateEnv
)
}
}
Expand Down
12 changes: 12 additions & 0 deletions scalalib/test/resources/hello-world/env/src/Main.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import java.nio.file.{Files, Paths}

object Main {
def main(args: Array[String]): Unit = {
val resultPath = Paths.get(args(0))
Files.createDirectories(resultPath.getParent)
Files.write(
resultPath,
s"TEST_ENV_VARIABLE=${sys.env.getOrElse("TEST_ENV_VARIABLE", "")}".getBytes
)
}
}
18 changes: 18 additions & 0 deletions scalalib/test/src/mill/scalalib/HelloWorldTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,12 @@ object HelloWorldTests extends TestSuite {
}
}

object HelloWorldWithoutEnv extends HelloBase {
object env extends HelloWorldModule {
def forkEnv = T { Map.empty[String, String] }
}
}

val resourcePath = os.pwd / "scalalib" / "test" / "resources" / "hello-world"

def jarMainClass(jar: JarFile): Option[String] = {
Expand Down Expand Up @@ -810,6 +816,18 @@ object HelloWorldTests extends TestSuite {
val Left(Result.Failure(_, None)) = eval.apply(HelloWorldWithoutMain.core.runLocal())

}
"envIsPropagated" - workspaceTest(HelloWorldWithoutEnv) { eval =>
val runResult = eval.outPath / "env" / "run.dest" / "hello-mill"
val Right((_, evalCount)) =
eval.apply(HelloWorldWithoutEnv.env.run(T.task(Args(runResult.toString))))

assert(sys.env.get("TEST_ENV_VARIABLE") == Some("value"))
assert(evalCount > 0)
assert(os.exists(runResult))

val output = os.read(runResult)
assert(output == "TEST_ENV_VARIABLE=value")
}
}

"jar" - {
Expand Down
Loading