Module cache directory propagation (#1900)
Currently the module cache directory in CLI commands (and thus in Gradle tasks, since they use CLI commands under the hood) is only used for the "primary" task. For example, for an eval command, the module cache directory will be used for the evaluation of the target module. However, most commands also work with projects, and for projects the `PklProject` file must be evaluated. This is done via `Project.loadFromPath`, which internally builds an evaluator with the default module cache path. Therefore, all evaluations of project manifests always use default cache path instead of whatever is configured through the CLI option/Gradle task property. This, in turn, may result in unexpected behavior: in general, the user would expect that if they specify the cache directory, it will be used for all evaluations (e.g. when they pre-populate the cache and expect evaluations to work offline). The same is true for settings evaluation, although for settings it is significantly less important since settings scripts can't have remote dependencies. This PR propagates cache path into the project evaluator (as well as the settings evaluator, for consistency). This will result in the user-specified cache directory being used for `PklProject` evaluations as well.
This commit is contained in:
1 parent
1a3d7a1a76
commit
66fb5c133b
6 files changed
+149
-5
No files matched your search
@@ -69,9 +69,9 @@ abstract class CliCommand(protected val cliOptions: CliBaseOptions) {
|
||||
protected val settings: PklSettings by lazy {
|
||||
try {
|
||||
if (cliOptions.normalizedSettingsModule != null) {
|
||||
PklSettings.load(ModuleSource.uri(cliOptions.normalizedSettingsModule))
|
||||
PklSettings.load(ModuleSource.uri(cliOptions.normalizedSettingsModule), cliModuleCacheDir)
|
||||
} else {
|
||||
PklSettings.loadFromSystem()
|
||||
PklSettings.loadFromSystem(cliModuleCacheDir)
|
||||
}
|
||||
} catch (e: PklException) {
|
||||
// do not use `errorRenderer` because it depends on `settings`
|
||||
@@ -107,9 +107,19 @@ abstract class CliCommand(protected val cliOptions: CliBaseOptions) {
|
||||
stackFrameTransformer,
|
||||
envVars,
|
||||
cliOptions.powerAssertionsEnabled,
|
||||
cliModuleCacheDir,
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* The module cache dir as determined by CLI options alone. Used to evaluate the settings and
|
||||
* project files, which are loaded before project evaluator settings are known.
|
||||
*/
|
||||
private val cliModuleCacheDir: Path? by lazy {
|
||||
if (cliOptions.noCache) null
|
||||
else cliOptions.normalizedModuleCacheDir ?: IoUtils.getSystemModuleCacheDir()
|
||||
}
|
||||
|
||||
private val evaluatorSettings: PklEvaluatorSettings? by lazy {
|
||||
@Suppress("PklCliDirectProjectEvaluatorSettingsAccess")
|
||||
if (cliOptions.omitProjectSettings) null else project?.resolvedEvaluatorSettings
|
||||
|
||||
@@ -26,6 +26,7 @@ import org.junit.jupiter.api.condition.JRE
|
||||
import org.junit.jupiter.api.io.TempDir
|
||||
import org.pkl.commons.cli.commands.BaseCommand
|
||||
import org.pkl.commons.cli.commands.ProjectOptions
|
||||
import org.pkl.commons.test.PackageServer
|
||||
import org.pkl.commons.writeString
|
||||
import org.pkl.core.SecurityManagers
|
||||
import org.pkl.core.evaluatorSettings.PklEvaluatorSettings
|
||||
@@ -134,7 +135,9 @@ class CliCommandTest {
|
||||
@DisabledOnJre(JRE.JAVA_22, JRE.JAVA_23, JRE.JAVA_24)
|
||||
fun `test that --omit-project-settings actually omits project settings`(@TempDir tempDir: Path) {
|
||||
val project = tempDir.resolve("PklProject").writeString(projectWithAllEvaluatorSettings)
|
||||
cmd.parse(arrayOf("--working-dir=$tempDir", "--omit-project-settings"))
|
||||
cmd.parse(
|
||||
arrayOf("--working-dir=$tempDir", "--omit-project-settings", "--settings=pkl:settings")
|
||||
)
|
||||
val opts =
|
||||
cmd.baseOptions.baseOptions(listOf(project.toUri()), cmd.projectOptions, testMode = true)
|
||||
val cliTest = CliTest(opts)
|
||||
@@ -200,4 +203,31 @@ class CliCommandTest {
|
||||
assertThat(cliTest.myExternalModuleReaders)
|
||||
.isEqualTo(mapOf("bar" to PklEvaluatorSettings.ExternalReader("bar", listOf(), null)))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `--cache-dir is used when loading the project`(@TempDir tempDir: Path) {
|
||||
val cacheDir = tempDir.resolve("cache").also(PackageServer::populateCacheDir)
|
||||
tempDir
|
||||
.resolve("PklProject")
|
||||
.writeString(
|
||||
// language=pkl
|
||||
"""
|
||||
amends "pkl:Project"
|
||||
|
||||
import "package://localhost:0/fruit@1.0.5#/catalog/apple.pkl"
|
||||
|
||||
evaluatorSettings {
|
||||
externalProperties {
|
||||
["fruit"] = apple.name
|
||||
}
|
||||
}
|
||||
"""
|
||||
.trimIndent()
|
||||
)
|
||||
cmd.parse(arrayOf("--working-dir=$tempDir", "--cache-dir=$cacheDir"))
|
||||
val opts = cmd.baseOptions.baseOptions(emptyList(), cmd.projectOptions, testMode = true)
|
||||
val cliTest = CliTest(opts)
|
||||
assertThat(cliTest.myProjectEvaluatorSettings!!.externalProperties)
|
||||
.containsEntry("fruit", "Apple")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user