refactor: dedupe plugin discovery boundary opens

This commit is contained in:
Peter Steinberger
2026-04-06 17:42:55 +01:00
parent a4223f836d
commit 0eb6cec32b

View File

@@ -504,19 +504,9 @@ function resolvePackageEntrySource(params: {
const source = path.resolve(params.packageDir, params.entryPath);
const rejectHardlinks = params.rejectHardlinks ?? true;
const candidates = [source];
if (!rejectHardlinks) {
const builtCandidate = source.replace(/\.[^.]+$/u, ".js");
if (builtCandidate !== source) {
candidates.push(builtCandidate);
}
}
for (const candidate of new Set(candidates)) {
if (!fs.existsSync(candidate)) {
continue;
}
const openCandidate = (absolutePath: string): string | null => {
const opened = openBoundaryFileSync({
absolutePath: candidate,
absolutePath,
rootPath: params.packageDir,
boundaryLabel: "plugin package directory",
rejectHardlinks,
@@ -545,38 +535,22 @@ function resolvePackageEntrySource(params: {
const safeSource = opened.path;
fs.closeSync(opened.fd);
return safeSource;
};
if (!rejectHardlinks) {
const builtCandidate = source.replace(/\.[^.]+$/u, ".js");
if (builtCandidate !== source) {
candidates.push(builtCandidate);
}
}
const opened = openBoundaryFileSync({
absolutePath: source,
rootPath: params.packageDir,
boundaryLabel: "plugin package directory",
rejectHardlinks,
});
if (!opened.ok) {
return matchBoundaryFileOpenFailure(opened, {
path: () => null,
io: () => {
params.diagnostics.push({
level: "warn",
message: `extension entry unreadable (I/O error): ${params.entryPath}`,
source: params.sourceLabel,
});
return null;
},
fallback: () => {
params.diagnostics.push({
level: "error",
message: `extension entry escapes package directory: ${params.entryPath}`,
source: params.sourceLabel,
});
return null;
},
});
for (const candidate of new Set(candidates)) {
if (!fs.existsSync(candidate)) {
continue;
}
return openCandidate(candidate);
}
const safeSource = opened.path;
fs.closeSync(opened.fd);
return safeSource;
return openCandidate(source);
}
function discoverInDirectory(params: {