From c3d555d49c56a9a7f7b39244083972acac262ca0 Mon Sep 17 00:00:00 2001 From: Islon Scherer Date: Tue, 17 Nov 2026 12:24:58 +0100 Subject: [PATCH] Fix `pkl project package` with multiple projects (#1981) --- pkl-cli/pkl-cli.gradle.kts | 14 ++-- .../kotlin/org/pkl/cli/CliCommandRunner.kt | 5 -- .../main/kotlin/org/pkl/cli/CliEvaluator.kt | 4 - .../main/kotlin/org/pkl/cli/CliTestRunner.kt | 5 -- .../pkl/cli/SubprocessProfilerOutputTest.kt | 18 +--- .../pkl/cli/SubprocessProjectPackagerTest.kt | 84 +++++++++++++++++++ .../test/kotlin/org/pkl/cli/testExtensions.kt | 18 +++- .../java/org/pkl/core/runtime/VmUtils.java | 4 - 8 files changed, 110 insertions(+), 42 deletions(-) create mode 100644 pkl-cli/src/test/kotlin/org/pkl/cli/SubprocessProjectPackagerTest.kt diff --git a/pkl-cli/pkl-cli.gradle.kts b/pkl-cli/pkl-cli.gradle.kts index 47dbe6430..6ef3d6a59 100644 --- a/pkl-cli/pkl-cli.gradle.kts +++ b/pkl-cli/pkl-cli.gradle.kts @@ -87,8 +87,9 @@ tasks.jar { tasks.javadoc { enabled = false } -// has its own dedicated task; see `testProfilerOutput` and `testNativeProfilerOutput` below -tasks.test { exclude("**/SubprocessProfilerOutputTest.class") } +// subprocess tests have their own dedicated tasks; see `testProfilerOutput` and +// `testNativeProfilerOutput` below +tasks.test { exclude("**/Subprocess*Test.class") } tasks.shadowJar { archiveFileName.set("jpkl") @@ -112,8 +113,9 @@ val testJavaExecutable = // executable; // to verify that, we don't want to include them here) (configurations.testRuntimeClasspath.get() - configurations.runtimeClasspath.get()) - // has its own dedicated task; see `testProfilerOutput` and `testNativeProfilerOutput` below - exclude("**/SubprocessProfilerOutputTest.class") + // subprocess tests have their own dedicated tasks; see `testProfilerOutput` and + // `testNativeProfilerOutput` below + exclude("**/Subprocess*Test.class") } // Setup `testJavaExecutable` tasks for multi-JDK testing. @@ -223,7 +225,7 @@ val testProfilerOutput = dependsOn(tasks.javaExecutable) testClassesDirs = sourceSets.test.get().output.classesDirs classpath = sourceSets.test.get().runtimeClasspath - include("**/SubprocessProfilerOutputTest.class") + include("**/Subprocess*Test.class") systemProperty( "org.pkl.cli.testJar", tasks.javaExecutable.get().outputs.files.singleFile.absolutePath, @@ -236,7 +238,7 @@ val testNativeProfilerOutput = dependsOn(tasks.assembleNative) testClassesDirs = sourceSets.test.get().output.classesDirs classpath = sourceSets.test.get().runtimeClasspath - include("**/SubprocessProfilerOutputTest.class") + include("**/Subprocess*Test.class") systemProperty( "org.pkl.cli.testExecutable", tasks.assembleNative.get().outputs.files.singleFile.absolutePath, diff --git a/pkl-cli/src/main/kotlin/org/pkl/cli/CliCommandRunner.kt b/pkl-cli/src/main/kotlin/org/pkl/cli/CliCommandRunner.kt index db2a39916..4a9b2b6de 100644 --- a/pkl-cli/src/main/kotlin/org/pkl/cli/CliCommandRunner.kt +++ b/pkl-cli/src/main/kotlin/org/pkl/cli/CliCommandRunner.kt @@ -21,7 +21,6 @@ import com.github.ajalt.clikt.core.* import com.github.ajalt.clikt.parameters.arguments.* import com.github.ajalt.clikt.parameters.options.* import com.github.ajalt.clikt.parameters.types.int -import com.oracle.truffle.api.TruffleOptions import java.io.OutputStream import java.net.URI import java.nio.file.Path @@ -42,7 +41,6 @@ import org.pkl.core.ModuleSource.uri import org.pkl.core.PklBugException import org.pkl.core.PklException import org.pkl.core.ProfilerOptions -import org.pkl.core.runtime.VmUtils import org.pkl.core.util.IoUtils class CliCommandRunner @@ -63,9 +61,6 @@ constructor( } finally { Closeables.closeQuietly(builder.moduleKeyFactories) Closeables.closeQuietly(builder.resourceReaders) - if (TruffleOptions.AOT) { - VmUtils.closeEngine() - } } } diff --git a/pkl-cli/src/main/kotlin/org/pkl/cli/CliEvaluator.kt b/pkl-cli/src/main/kotlin/org/pkl/cli/CliEvaluator.kt index 6349aea22..712c0468a 100644 --- a/pkl-cli/src/main/kotlin/org/pkl/cli/CliEvaluator.kt +++ b/pkl-cli/src/main/kotlin/org/pkl/cli/CliEvaluator.kt @@ -15,7 +15,6 @@ */ package org.pkl.cli -import com.oracle.truffle.api.TruffleOptions import java.io.File import java.io.InputStream import java.io.OutputStream @@ -110,9 +109,6 @@ constructor( } finally { Closeables.closeQuietly(builder.moduleKeyFactories) Closeables.closeQuietly(builder.resourceReaders) - if (TruffleOptions.AOT) { - VmUtils.closeEngine() - } } } diff --git a/pkl-cli/src/main/kotlin/org/pkl/cli/CliTestRunner.kt b/pkl-cli/src/main/kotlin/org/pkl/cli/CliTestRunner.kt index 07e912d8d..cbd9796a7 100644 --- a/pkl-cli/src/main/kotlin/org/pkl/cli/CliTestRunner.kt +++ b/pkl-cli/src/main/kotlin/org/pkl/cli/CliTestRunner.kt @@ -15,7 +15,6 @@ */ package org.pkl.cli -import com.oracle.truffle.api.TruffleOptions import java.io.StringWriter import java.io.Writer import org.pkl.commons.cli.* @@ -25,7 +24,6 @@ import org.pkl.core.ModuleSource.uri import org.pkl.core.PklException import org.pkl.core.ProfilerOptions import org.pkl.core.TestResults -import org.pkl.core.runtime.VmUtils import org.pkl.core.stdlib.test.report.JUnitReporter import org.pkl.core.stdlib.test.report.MinimalReporter import org.pkl.core.stdlib.test.report.SpecReporter @@ -47,9 +45,6 @@ constructor( } finally { Closeables.closeQuietly(builder.moduleKeyFactories) Closeables.closeQuietly(builder.resourceReaders) - if (TruffleOptions.AOT) { - VmUtils.closeEngine() - } } } diff --git a/pkl-cli/src/test/kotlin/org/pkl/cli/SubprocessProfilerOutputTest.kt b/pkl-cli/src/test/kotlin/org/pkl/cli/SubprocessProfilerOutputTest.kt index cfa0831e7..742bb1449 100644 --- a/pkl-cli/src/test/kotlin/org/pkl/cli/SubprocessProfilerOutputTest.kt +++ b/pkl-cli/src/test/kotlin/org/pkl/cli/SubprocessProfilerOutputTest.kt @@ -36,29 +36,13 @@ import org.junit.jupiter.api.io.TempDir * `-Dorg.pkl.cli.testJar=` (or `-Dorg.pkl.cli.testExecutable=`). */ class SubprocessProfilerOutputTest { - /** - * The command that launches Pkl: the native executable if `org.pkl.cli.testExecutable` is set, - * otherwise the executable jar given by `org.pkl.cli.testJar`. - */ - private fun launcher(): Array { - System.getProperty("org.pkl.cli.testExecutable")?.let { - return arrayOf(it) - } - val jar = - checkNotNull(System.getProperty("org.pkl.cli.testJar")) { - "neither system property `org.pkl.cli.testExecutable` nor `org.pkl.cli.testJar` is set" - } - val javaBin = ProcessHandle.current().info().command().orElseThrow() - return arrayOf(javaBin, "-jar", jar) - } - private fun runTest(tempDir: Path, pklCode: String, command: Array) { val sourceFile = tempDir.resolve("profiled.pkl").also { it.writeText(pklCode) } val outputFile = tempDir.resolve("profile.pb.gz") val process = ProcessBuilder( - *launcher(), + *subprocessLauncher(), *command, "--profile-cpu-output", outputFile.absolutePathString(), diff --git a/pkl-cli/src/test/kotlin/org/pkl/cli/SubprocessProjectPackagerTest.kt b/pkl-cli/src/test/kotlin/org/pkl/cli/SubprocessProjectPackagerTest.kt new file mode 100644 index 000000000..7751b584c --- /dev/null +++ b/pkl-cli/src/test/kotlin/org/pkl/cli/SubprocessProjectPackagerTest.kt @@ -0,0 +1,84 @@ +/* + * Copyright © 2026 Apple Inc. and the Pkl project authors. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.pkl.cli + +import java.nio.file.Path +import kotlin.io.path.exists +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir + +/** + * Runs `pkl project package` using the packaged executable jar (or native executable) as a + * subprocess. + */ +class SubprocessProjectPackagerTest { + private fun Path.writeProject(name: String) { + writeFile( + "$name/PklProject", + """ + amends "pkl:Project" + + package { + name = "$name" + baseUri = "package://example.com/$name" + version = "1.0.0" + packageZipUrl = "https://example.com/$name@\(version).zip" + apiTests { "test.pkl" } + } + """ + .trimIndent(), + ) + writeFile( + "$name/test.pkl", + """ + amends "pkl:test" + + facts { + ["ok"] { true } + } + """ + .trimIndent(), + ) + } + + @Test + fun `multiple projects with apiTests`(@TempDir tempDir: Path) { + tempDir.writeProject("project1") + tempDir.writeProject("project2") + + val process = + ProcessBuilder( + *subprocessLauncher(), + "project", + "package", + "--skip-publish-check", + "--output-path", + ".out", + "project1", + "project2", + ) + .directory(tempDir.toFile()) + .redirectErrorStream(true) + .start() + + val output = process.inputStream.bufferedReader().readText() + val exitCode = process.waitFor() + assertThat(exitCode).withFailMessage { "process failed with output:\n$output" }.isZero() + assertThat(tempDir.resolve(".out/project1@1.0.0.zip").exists()).isTrue + assertThat(tempDir.resolve(".out/project2@1.0.0.zip").exists()).isTrue + } +} diff --git a/pkl-cli/src/test/kotlin/org/pkl/cli/testExtensions.kt b/pkl-cli/src/test/kotlin/org/pkl/cli/testExtensions.kt index 1d63de41d..c5b268a31 100644 --- a/pkl-cli/src/test/kotlin/org/pkl/cli/testExtensions.kt +++ b/pkl-cli/src/test/kotlin/org/pkl/cli/testExtensions.kt @@ -1,5 +1,5 @@ /* - * Copyright © 2024-2025 Apple Inc. and the Pkl project authors. All rights reserved. + * Copyright © 2024-2026 Apple Inc. and the Pkl project authors. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -28,3 +28,19 @@ fun Path.writeFile(fileName: String, contents: String): Path { } fun Path.writeEmptyFile(fileName: String): Path = writeFile(fileName, "") + +/** + * The command that launches Pkl in subprocess tests: the native executable if + * `org.pkl.cli.testExecutable` is set, otherwise the executable jar given by `org.pkl.cli.testJar`. + */ +fun subprocessLauncher(): Array { + System.getProperty("org.pkl.cli.testExecutable")?.let { + return arrayOf(it) + } + val jar = + checkNotNull(System.getProperty("org.pkl.cli.testJar")) { + "neither system property `org.pkl.cli.testExecutable` nor `org.pkl.cli.testJar` is set" + } + val javaBin = ProcessHandle.current().info().command().orElseThrow() + return arrayOf(javaBin, "-jar", jar) +} diff --git a/pkl-core/src/main/java/org/pkl/core/runtime/VmUtils.java b/pkl-core/src/main/java/org/pkl/core/runtime/VmUtils.java index 6101997df..157ee8d4c 100644 --- a/pkl-core/src/main/java/org/pkl/core/runtime/VmUtils.java +++ b/pkl-core/src/main/java/org/pkl/core/runtime/VmUtils.java @@ -122,10 +122,6 @@ public final class VmUtils { return Engine.newBuilder("pkl").option("engine.WarnInterpreterOnly", "false").build(); } - public static void closeEngine() { - PKL_ENGINE.close(true); - } - static VmTyped createEmptyModule() { return new VmTyped(createEmptyMaterializedFrame(), null, EconomicMaps.create()); }