Skip to content

Commit 4a25320

Browse files
authored
Fix import/read verification when encountering glob wildcards (#1559)
Fixes an issue where the import verifier can possibly throw when packaging on Windows due to `*` being an invalid filename.
1 parent df063f1 commit 4a25320

2 files changed

Lines changed: 45 additions & 7 deletions

File tree

pkl-cli/src/test/kotlin/org/pkl/cli/CliProjectPackagerTest.kt

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,43 @@ class CliProjectPackagerTest {
763763
)
764764
}
765765

766+
@Test
767+
fun `import path verification with glob imports`(@TempDir tempDir: Path) {
768+
tempDir.writeFile(
769+
"main.pkl",
770+
"""
771+
import* "**.pkl" as foo
772+
773+
res = foo
774+
"""
775+
.trimIndent(),
776+
)
777+
tempDir.writeFile(
778+
"PklProject",
779+
"""
780+
amends "pkl:Project"
781+
782+
package {
783+
name = "mypackage"
784+
version = "1.0.0"
785+
baseUri = "package://example.com/mypackage"
786+
packageZipUrl = "https://foo.com"
787+
}
788+
"""
789+
.trimIndent(),
790+
)
791+
792+
CliProjectPackager(
793+
CliBaseOptions(workingDir = tempDir),
794+
listOf(tempDir),
795+
CliTestOptions(),
796+
".out/%{name}@%{version}",
797+
skipPublishCheck = true,
798+
consoleWriter = StringWriter(),
799+
)
800+
.run()
801+
}
802+
766803
@Test
767804
@DisabledOnOs(OS.WINDOWS)
768805
fun `import path verification -- absolute read from root dir`(@TempDir tempDir: Path) {

pkl-core/src/main/java/org/pkl/core/project/ProjectPackager.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -393,9 +393,6 @@ private boolean isAbsoluteImport(String importStr) {
393393
*/
394394
public void validateImportsAndReads(Project project, Path pklModulePath) {
395395
var imports = getImportsAndReads(pklModulePath);
396-
if (imports == null) {
397-
return;
398-
}
399396
for (var importContext : imports) {
400397
var importStr = importContext.stringValue();
401398
var sourceSection = importContext.sourceSection();
@@ -420,14 +417,14 @@ public void validateImportsAndReads(Project project, Path pklModulePath) {
420417
.toPklException(stackFrameTransformer, color);
421418
}
422419
var currentPath = pklModulePath.getParent();
423-
var importPath = Path.of(importUri.getPath());
420+
var importPath = importUri.getPath().split("/");
424421
// It's not good enough to just check the normalized path to see whether it exists within the
425422
// root dir.
426423
// It's possible that the import path resolves to a path outside the project dir,
427424
// and then back inside the project dir.
428-
for (var i = 0; i < importPath.getNameCount(); i++) {
429-
var segment = importPath.getName(i);
430-
currentPath = currentPath.resolve(segment);
425+
for (var segment : importPath) {
426+
// replace any possibly reserved filename characters with underscore.
427+
currentPath = currentPath.resolve(sanitizePathSegment(segment));
431428
var normalized = currentPath.normalize();
432429
if (!normalized.startsWith(project.getProjectDir())) {
433430
throw new VmExceptionBuilder()
@@ -440,6 +437,10 @@ public void validateImportsAndReads(Project project, Path pklModulePath) {
440437
}
441438
}
442439

440+
private String sanitizePathSegment(String segment) {
441+
return segment.replaceAll("[\\\\<>:\"|?*]", "_");
442+
}
443+
443444
private List<ImportsAndReadsParser.Entry> getImportsAndReads(Path pklModulePath) {
444445
try {
445446
var moduleKey = ModuleKeys.file(pklModulePath);

0 commit comments

Comments
 (0)