mirror of
https://github.com/apple/pkl.git
synced 2026-05-25 16:19:20 +02:00
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.
This commit is contained in:
@@ -763,6 +763,43 @@ class CliProjectPackagerTest {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `import path verification with glob imports`(@TempDir tempDir: Path) {
|
||||||
|
tempDir.writeFile(
|
||||||
|
"main.pkl",
|
||||||
|
"""
|
||||||
|
import* "**.pkl" as foo
|
||||||
|
|
||||||
|
res = foo
|
||||||
|
"""
|
||||||
|
.trimIndent(),
|
||||||
|
)
|
||||||
|
tempDir.writeFile(
|
||||||
|
"PklProject",
|
||||||
|
"""
|
||||||
|
amends "pkl:Project"
|
||||||
|
|
||||||
|
package {
|
||||||
|
name = "mypackage"
|
||||||
|
version = "1.0.0"
|
||||||
|
baseUri = "package://example.com/mypackage"
|
||||||
|
packageZipUrl = "https://foo.com"
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
.trimIndent(),
|
||||||
|
)
|
||||||
|
|
||||||
|
CliProjectPackager(
|
||||||
|
CliBaseOptions(workingDir = tempDir),
|
||||||
|
listOf(tempDir),
|
||||||
|
CliTestOptions(),
|
||||||
|
".out/%{name}@%{version}",
|
||||||
|
skipPublishCheck = true,
|
||||||
|
consoleWriter = StringWriter(),
|
||||||
|
)
|
||||||
|
.run()
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisabledOnOs(OS.WINDOWS)
|
@DisabledOnOs(OS.WINDOWS)
|
||||||
fun `import path verification -- absolute read from root dir`(@TempDir tempDir: Path) {
|
fun `import path verification -- absolute read from root dir`(@TempDir tempDir: Path) {
|
||||||
|
|||||||
@@ -393,9 +393,6 @@ public final class ProjectPackager {
|
|||||||
*/
|
*/
|
||||||
public void validateImportsAndReads(Project project, Path pklModulePath) {
|
public void validateImportsAndReads(Project project, Path pklModulePath) {
|
||||||
var imports = getImportsAndReads(pklModulePath);
|
var imports = getImportsAndReads(pklModulePath);
|
||||||
if (imports == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
for (var importContext : imports) {
|
for (var importContext : imports) {
|
||||||
var importStr = importContext.stringValue();
|
var importStr = importContext.stringValue();
|
||||||
var sourceSection = importContext.sourceSection();
|
var sourceSection = importContext.sourceSection();
|
||||||
@@ -420,14 +417,14 @@ public final class ProjectPackager {
|
|||||||
.toPklException(stackFrameTransformer, color);
|
.toPklException(stackFrameTransformer, color);
|
||||||
}
|
}
|
||||||
var currentPath = pklModulePath.getParent();
|
var currentPath = pklModulePath.getParent();
|
||||||
var importPath = Path.of(importUri.getPath());
|
var importPath = importUri.getPath().split("/");
|
||||||
// It's not good enough to just check the normalized path to see whether it exists within the
|
// It's not good enough to just check the normalized path to see whether it exists within the
|
||||||
// root dir.
|
// root dir.
|
||||||
// It's possible that the import path resolves to a path outside the project dir,
|
// It's possible that the import path resolves to a path outside the project dir,
|
||||||
// and then back inside the project dir.
|
// and then back inside the project dir.
|
||||||
for (var i = 0; i < importPath.getNameCount(); i++) {
|
for (var segment : importPath) {
|
||||||
var segment = importPath.getName(i);
|
// replace any possibly reserved filename characters with underscore.
|
||||||
currentPath = currentPath.resolve(segment);
|
currentPath = currentPath.resolve(sanitizePathSegment(segment));
|
||||||
var normalized = currentPath.normalize();
|
var normalized = currentPath.normalize();
|
||||||
if (!normalized.startsWith(project.getProjectDir())) {
|
if (!normalized.startsWith(project.getProjectDir())) {
|
||||||
throw new VmExceptionBuilder()
|
throw new VmExceptionBuilder()
|
||||||
@@ -440,6 +437,10 @@ public final class ProjectPackager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private String sanitizePathSegment(String segment) {
|
||||||
|
return segment.replaceAll("[\\\\<>:\"|?*]", "_");
|
||||||
|
}
|
||||||
|
|
||||||
private List<ImportsAndReadsParser.Entry> getImportsAndReads(Path pklModulePath) {
|
private List<ImportsAndReadsParser.Entry> getImportsAndReads(Path pklModulePath) {
|
||||||
try {
|
try {
|
||||||
var moduleKey = ModuleKeys.file(pklModulePath);
|
var moduleKey = ModuleKeys.file(pklModulePath);
|
||||||
|
|||||||
Reference in New Issue
Block a user