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:
Daniel Chao
2026-04-29 20:20:49 -07:00
committed by GitHub
parent df063f17f3
commit 4a25320995
2 changed files with 45 additions and 7 deletions
@@ -393,9 +393,6 @@ public final class ProjectPackager {
*/
public void validateImportsAndReads(Project project, Path pklModulePath) {
var imports = getImportsAndReads(pklModulePath);
if (imports == null) {
return;
}
for (var importContext : imports) {
var importStr = importContext.stringValue();
var sourceSection = importContext.sourceSection();
@@ -420,14 +417,14 @@ public final class ProjectPackager {
.toPklException(stackFrameTransformer, color);
}
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
// root dir.
// It's possible that the import path resolves to a path outside the project dir,
// and then back inside the project dir.
for (var i = 0; i < importPath.getNameCount(); i++) {
var segment = importPath.getName(i);
currentPath = currentPath.resolve(segment);
for (var segment : importPath) {
// replace any possibly reserved filename characters with underscore.
currentPath = currentPath.resolve(sanitizePathSegment(segment));
var normalized = currentPath.normalize();
if (!normalized.startsWith(project.getProjectDir())) {
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) {
try {
var moduleKey = ModuleKeys.file(pklModulePath);