Skip to content

Commit 2b9aca7

Browse files
Backport "Consolidate jvm subprocess and inprocess functions (#4456)" to 0.12 (#4504)
Backport of #4456 --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent 0ee85fa commit 2b9aca7

File tree

31 files changed

+623
-255
lines changed

31 files changed

+623
-255
lines changed

bsp/worker/src/mill/bsp/worker/MillScalaBuildServer.scala

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -119,21 +119,18 @@ private trait MillScalaBuildServer extends ScalaBuildServer { this: MillBuildSer
119119
) {
120120
case (ev, state, id, m: TestModule, Some((classpath, testFramework, testClasspath))) =>
121121
val (frameworkName, classFingerprint): (String, Agg[(Class[_], Fingerprint)]) =
122-
Jvm.inprocess(
123-
classpath.map(_.path),
124-
classLoaderOverrideSbtTesting = true,
125-
isolated = true,
126-
closeContextClassLoaderWhenDone = false,
127-
cl => {
128-
val framework = Framework.framework(testFramework)(cl)
129-
val discoveredTests = TestRunnerUtils.discoverTests(
130-
cl,
131-
framework,
132-
Agg.from(testClasspath.map(_.path))
133-
)
134-
(framework.name(), discoveredTests)
135-
}
136-
)(new mill.api.Ctx.Home { def home = os.home })
122+
Jvm.withClassLoader(
123+
classPath = classpath.map(_.path).toVector,
124+
sharedPrefixes = Seq("sbt.testing.")
125+
) { classLoader =>
126+
val framework = Framework.framework(testFramework)(classLoader)
127+
val discoveredTests = TestRunnerUtils.discoverTests(
128+
classLoader,
129+
framework,
130+
Agg.from(testClasspath.map(_.path))
131+
)
132+
(framework.name(), discoveredTests)
133+
}
137134
val classes = Seq.from(classFingerprint.map(classF => classF._1.getName.stripSuffix("$")))
138135
new ScalaTestClassesItem(id, classes.asJava).tap { it =>
139136
it.setFramework(frameworkName)

contrib/jmh/src/mill/contrib/jmh/JmhModule.scala

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,17 @@ trait JmhModule extends JavaModule {
3838
def runJmh(args: String*) =
3939
Task.Command {
4040
val (_, resources) = generateBenchmarkSources()
41-
Jvm.runSubprocess(
42-
"org.openjdk.jmh.Main",
41+
Jvm.callProcess(
42+
mainClass = "org.openjdk.jmh.Main",
4343
classPath = (runClasspath() ++ generatorDeps()).map(_.path) ++
4444
Seq(compileGeneratedSources().path, resources),
4545
mainArgs = args,
46-
workingDir = Task.ctx().dest,
47-
javaHome = zincWorker().javaHome().map(_.path)
46+
cwd = Task.ctx().dest,
47+
javaHome = zincWorker().javaHome().map(_.path),
48+
stdin = os.Inherit,
49+
stdout = os.Inherit
4850
)
51+
()
4952
}
5053

5154
def listJmhBenchmarks(args: String*) = runJmh(("-l" +: args): _*)
@@ -82,17 +85,19 @@ trait JmhModule extends JavaModule {
8285
os.remove.all(resourcesDir)
8386
os.makeDir.all(resourcesDir)
8487

85-
Jvm.runSubprocess(
86-
"org.openjdk.jmh.generators.bytecode.JmhBytecodeGenerator",
87-
(runClasspath() ++ generatorDeps()).map(_.path),
88+
Jvm.callProcess(
89+
mainClass = "org.openjdk.jmh.generators.bytecode.JmhBytecodeGenerator",
90+
classPath = (runClasspath() ++ generatorDeps()).map(_.path),
8891
mainArgs = Seq(
8992
compile().classes.path.toString,
9093
sourcesDir.toString,
9194
resourcesDir.toString,
9295
"default"
9396
),
9497
javaHome = zincWorker().javaHome().map(_.path),
95-
jvmArgs = forkedArgs
98+
jvmArgs = forkedArgs,
99+
stdin = os.Inherit,
100+
stdout = os.Inherit
96101
)
97102

98103
(sourcesDir, resourcesDir)

contrib/proguard/src/mill/contrib/proguard/Proguard.scala

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,13 @@ trait Proguard extends ScalaModule {
111111
// val result = os.proc(cmd).call(stdout = Task.dest / "stdout.txt", stderr = Task.dest / "stderr.txt")
112112
// Task.log.debug(s"result: ${result}")
113113

114-
Jvm.runSubprocess(
114+
Jvm.callProcess(
115115
mainClass = "proguard.ProGuard",
116-
classPath = proguardClasspath().map(_.path),
116+
classPath = proguardClasspath().map(_.path).toVector,
117117
mainArgs = args,
118-
workingDir = Task.dest
118+
cwd = Task.dest,
119+
stdin = os.Inherit,
120+
stdout = os.Inherit
119121
)
120122

121123
// the call above already throws an exception on a non-zero exit code,

kotlinlib/src/mill/kotlinlib/KotlinModule.scala

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,11 +179,13 @@ trait KotlinModule extends JavaModule { outer =>
179179

180180
Task.log.info("dokka options: " + options)
181181

182-
Jvm.runSubprocess(
182+
Jvm.callProcess(
183183
mainClass = "",
184-
classPath = Agg.empty,
184+
classPath = Seq.empty,
185185
jvmArgs = Seq("-jar", dokkaCliClasspath().head.path.toString()),
186-
mainArgs = options
186+
mainArgs = options,
187+
stdin = os.Inherit,
188+
stdout = os.Inherit
187189
)
188190
}
189191

kotlinlib/src/mill/kotlinlib/android/AndroidAppKotlinModule.scala

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,14 +243,18 @@ trait AndroidAppKotlinModule extends AndroidAppModule with KotlinModule { outer
243243
* @return
244244
*/
245245
def generatePreviews: T[Agg[PathRef]] = Task {
246-
val previewGenOut = mill.util.Jvm.callSubprocess(
246+
val previewGenOut = mill.util.Jvm.callProcess(
247247
mainClass = "com.android.tools.render.compose.MainKt",
248-
classPath = composePreviewRenderer().map(_.path) ++ layoutLibRenderer().map(_.path),
248+
classPath =
249+
composePreviewRenderer().map(_.path).toVector ++ layoutLibRenderer().map(_.path).toVector,
249250
jvmArgs = Seq(
250251
"-Dlayoutlib.thread.profile.timeoutms=10000",
251252
"-Djava.security.manager=allow"
252253
),
253-
mainArgs = Seq(composePreviewArgs().path.toString())
254+
mainArgs = Seq(composePreviewArgs().path.toString()),
255+
cwd = Task.dest,
256+
stdin = os.Inherit,
257+
stdout = os.Inherit
254258
).out.lines()
255259

256260
Task.log.info(previewGenOut.mkString("\n"))

kotlinlib/src/mill/kotlinlib/detekt/DetektModule.scala

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,13 @@ trait DetektModule extends KotlinModule {
2727
Task.log.info("running detekt ...")
2828
Task.log.debug(s"with $args")
2929

30-
Jvm.callSubprocess(
30+
Jvm.callProcess(
3131
mainClass = "io.gitlab.arturbosch.detekt.cli.Main",
32-
classPath = detektClasspath().map(_.path),
32+
classPath = detektClasspath().map(_.path).toVector,
3333
mainArgs = args,
34-
workingDir = millSourcePath, // allow passing relative paths for sources like src/a/b
35-
streamOut = true,
34+
cwd = millSourcePath, // allow passing relative paths for sources like src/a/b
35+
stdin = os.Inherit,
36+
stdout = os.Inherit,
3637
check = false
3738
).exitCode
3839
}

kotlinlib/src/mill/kotlinlib/js/KotlinJsModule.scala

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -161,12 +161,18 @@ trait KotlinJsModule extends KotlinModule { outer =>
161161
case Some(RunTarget.Node) =>
162162
val binaryPath = (binaryDir / s"$artifactId.${moduleKind.extension}")
163163
.toIO.getAbsolutePath
164-
Jvm.runSubprocessWithResult(
165-
commandArgs = Seq(
166-
"node"
167-
) ++ args.value ++ Seq(binaryPath),
168-
envArgs = envArgs,
169-
workingDir = workingDir
164+
val processResult = os.call(
165+
cmd = Seq("node") ++ args.value ++ Seq(binaryPath),
166+
env = envArgs,
167+
cwd = workingDir,
168+
stdin = os.Inherit,
169+
stdout = os.Inherit,
170+
check = false
171+
)
172+
if (processResult.exitCode == 0) Result.Success(processResult.exitCode)
173+
else Result.Failure(
174+
"Interactive Subprocess Failed (exit code " + processResult.exitCode + ")",
175+
Some(processResult.exitCode)
170176
)
171177
case Some(x) =>
172178
Result.Failure(s"Run target $x is not supported")
@@ -474,10 +480,12 @@ trait KotlinJsModule extends KotlinModule { outer =>
474480
// TODO may be optimized if there is a single folder for all modules
475481
// but may be problematic if modules use different NPM packages versions
476482
private def nodeModulesDir = Task(persistent = true) {
477-
Jvm.runSubprocess(
478-
commandArgs = Seq("npm", "install", "mocha@10.2.0", "source-map-support@0.5.21"),
479-
envArgs = Task.env,
480-
workingDir = Task.dest
483+
os.call(
484+
cmd = Seq("npm", "install", "mocha@10.2.0", "source-map-support@0.5.21"),
485+
env = Task.env,
486+
cwd = Task.dest,
487+
stdin = os.Inherit,
488+
stdout = os.Inherit
481489
)
482490
PathRef(Task.dest)
483491
}

kotlinlib/src/mill/kotlinlib/kover/KoverModule.scala

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,12 +212,14 @@ object Kover extends ExternalModule with KoverReportBaseModule {
212212
s"${reportPath.toString()}.xml"
213213
} else reportPath.toString()
214214
args ++= Seq(s"--${reportType.toString.toLowerCase(Locale.US)}", output)
215-
Jvm.runSubprocess(
215+
Jvm.callProcess(
216216
mainClass = "kotlinx.kover.cli.MainKt",
217-
classPath = classpath,
217+
classPath = classpath.toVector,
218218
jvmArgs = Seq.empty[String],
219219
mainArgs = args.result(),
220-
workingDir = workingDir
220+
cwd = workingDir,
221+
stdin = os.Inherit,
222+
stdout = os.Inherit
221223
)
222224
PathRef(os.Path(output))
223225
}

kotlinlib/src/mill/kotlinlib/ktfmt/KtfmtModule.scala

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,13 @@ object KtfmtModule extends ExternalModule with KtfmtBaseModule with TaskModule {
120120
if (!format) args += "--set-exit-if-changed"
121121
args ++= sources.iterator.map(_.path.toString())
122122

123-
val exitCode = Jvm.callSubprocess(
123+
val exitCode = Jvm.callProcess(
124124
mainClass = "com.facebook.ktfmt.cli.Main",
125-
classPath = classPath.map(_.path),
125+
classPath = classPath.map(_.path).toVector,
126126
mainArgs = args.result(),
127-
workingDir = millSourcePath, // allow passing relative paths for sources like src/a/b
128-
streamOut = true,
127+
cwd = millSourcePath, // allow passing relative paths for sources like src/a/b
128+
stdin = os.Inherit,
129+
stdout = os.Inherit,
129130
check = false
130131
).exitCode
131132

kotlinlib/src/mill/kotlinlib/ktlint/KtlintModule.scala

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,12 +122,13 @@ object KtlintModule extends ExternalModule with KtlintModule with TaskModule {
122122
.filter(f => os.exists(f) && (f.ext == "kt" || f.ext == "kts"))
123123
.map(_.toString())
124124

125-
val exitCode = Jvm.callSubprocess(
125+
val exitCode = Jvm.callProcess(
126126
mainClass = "com.pinterest.ktlint.Main",
127-
classPath = classPath.map(_.path),
127+
classPath = classPath.map(_.path).toVector,
128128
mainArgs = args.result(),
129-
workingDir = millSourcePath,
130-
streamOut = true,
129+
cwd = millSourcePath,
130+
stdin = os.Inherit,
131+
stdout = os.Inherit,
131132
check = false
132133
).exitCode
133134

main/api/src/mill/api/ClassLoader.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ object ClassLoader {
1818
}
1919
def java9OrAbove: Boolean = !System.getProperty("java.specification.version").startsWith("1.")
2020

21+
@deprecated("Use callClassLoader", "Mill 0.12.7")
2122
def create(
2223
urls: Seq[URL],
2324
parent: java.lang.ClassLoader,

main/init/src/mill/init/BuildGenModule.scala

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,24 @@ trait BuildGenModule extends TaskModule {
2727

2828
val mainClass = buildGenMainClass()
2929
val classPath = buildGenClasspath().map(_.path)
30-
val exit = Jvm.callSubprocess(
30+
val exitCode = Jvm.callProcess(
3131
mainClass = mainClass,
32-
classPath = classPath,
32+
classPath = classPath.toVector,
3333
mainArgs = args,
34-
workingDir = root
34+
cwd = root,
35+
stdin = os.Inherit,
36+
stdout = os.Inherit
3537
).exitCode
3638

37-
if (exit == 0) {
39+
if (exitCode == 0) {
3840
val files = BuildGenUtil.buildFiles(root).map(PathRef(_)).toSeq
3941
val config = buildGenScalafmtConfig()
4042
Task.log.info("formatting Mill build files")
4143
ScalafmtWorkerModule.worker().reformat(files, config)
4244

4345
Task.log.info("init completed, run \"mill resolve _\" to list available tasks")
4446
} else {
45-
throw BuildGenException(s"$mainClass exit($exit)")
47+
throw BuildGenException(s"$mainClass exit($exitCode)")
4648
}
4749
}
4850
}

main/src/mill/main/VisualizeModule.scala

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,12 @@ trait VisualizeModule extends mill.define.TaskModule {
102102

103103
g = g.graphAttr().`with`(Rank.dir(RankDir.LEFT_TO_RIGHT))
104104

105-
mill.util.Jvm.runSubprocess(
106-
"mill.main.graphviz.GraphvizTools",
107-
classpath().map(_.path),
108-
mainArgs = Seq(s"${os.temp(g.toString)};$dest;txt,dot,json,png,svg")
105+
mill.util.Jvm.callProcess(
106+
mainClass = "mill.main.graphviz.GraphvizTools",
107+
classPath = classpath().map(_.path).toVector,
108+
mainArgs = Seq(s"${os.temp(g.toString)};$dest;txt,dot,json,png,svg"),
109+
stdin = os.Inherit,
110+
stdout = os.Inherit
109111
)
110112

111113
os.list(dest).sorted.map(PathRef(_))

0 commit comments

Comments
 (0)