test: trim import-heavy startup paths

This commit is contained in:
Peter Steinberger
2026-03-22 00:52:03 +00:00
parent 8b7f40580d
commit d0d82ea67b
9 changed files with 367 additions and 268 deletions

View File

@@ -35,7 +35,7 @@ const baselinePathByMode = {
),
};
const inventoryPromiseByMode = new Map();
let allInventoryByModePromise;
let parsedExtensionSourceFilesPromise;
const ruleTextByMode = {
@@ -193,30 +193,42 @@ function shouldReport(mode, resolvedPath) {
return !resolvedPath.startsWith("src/plugin-sdk/");
}
function collectFromSourceFile(mode, sourceFile, filePath) {
const entries = [];
function collectEntriesByModeFromSourceFile(sourceFile, filePath) {
const entriesByMode = {
"src-outside-plugin-sdk": [],
"plugin-sdk-internal": [],
"relative-outside-package": [],
};
const extensionRoot = resolveExtensionRoot(filePath);
function push(kind, specifierNode, specifier) {
const resolvedPath = resolveSpecifier(specifier, filePath);
if (mode === "relative-outside-package") {
if (!specifier.startsWith(".") || !resolvedPath || !extensionRoot) {
return;
}
if (resolvedPath === extensionRoot || resolvedPath.startsWith(`${extensionRoot}/`)) {
return;
}
} else if (!shouldReport(mode, resolvedPath)) {
return;
}
entries.push({
const baseEntry = {
file: normalizePath(filePath),
line: toLine(sourceFile, specifierNode),
kind,
specifier,
resolvedPath,
reason: classifyReason(mode, kind, resolvedPath, specifier),
});
};
if (specifier.startsWith(".") && resolvedPath && extensionRoot) {
if (!(resolvedPath === extensionRoot || resolvedPath.startsWith(`${extensionRoot}/`))) {
entriesByMode["relative-outside-package"].push({
...baseEntry,
reason: classifyReason("relative-outside-package", kind, resolvedPath, specifier),
});
}
}
for (const mode of ["src-outside-plugin-sdk", "plugin-sdk-internal"]) {
if (!shouldReport(mode, resolvedPath)) {
continue;
}
entriesByMode[mode].push({
...baseEntry,
reason: classifyReason(mode, kind, resolvedPath, specifier),
});
}
}
function visit(node) {
@@ -240,26 +252,35 @@ function collectFromSourceFile(mode, sourceFile, filePath) {
}
visit(sourceFile);
return entries;
return entriesByMode;
}
export async function collectExtensionPluginSdkBoundaryInventory(mode) {
if (!MODES.has(mode)) {
throw new Error(`Unknown mode: ${mode}`);
}
let pending = inventoryPromiseByMode.get(mode);
if (!pending) {
pending = (async () => {
if (!allInventoryByModePromise) {
allInventoryByModePromise = (async () => {
const files = await collectParsedExtensionSourceFiles();
const inventory = [];
const inventoryByMode = {
"src-outside-plugin-sdk": [],
"plugin-sdk-internal": [],
"relative-outside-package": [],
};
for (const { filePath, sourceFile } of files) {
inventory.push(...collectFromSourceFile(mode, sourceFile, filePath));
const entriesByMode = collectEntriesByModeFromSourceFile(sourceFile, filePath);
for (const inventoryMode of MODES) {
inventoryByMode[inventoryMode].push(...entriesByMode[inventoryMode]);
}
}
return inventory.toSorted(compareEntries);
for (const inventoryMode of MODES) {
inventoryByMode[inventoryMode] = inventoryByMode[inventoryMode].toSorted(compareEntries);
}
return inventoryByMode;
})();
inventoryPromiseByMode.set(mode, pending);
}
return await pending;
const inventoryByMode = await allInventoryByModePromise;
return inventoryByMode[mode];
}
export async function readExpectedInventory(mode) {

View File

@@ -60,6 +60,8 @@ const ignoredFiles = new Set([
"src/secrets/runtime-web-tools.test.ts",
]);
let webSearchProviderInventoryPromise;
function normalizeRelativePath(filePath) {
return path.relative(repoRoot, filePath).split(path.sep).join("/");
}
@@ -185,32 +187,37 @@ function scanGenericCoreImports(lines, relativeFile, inventory) {
}
export async function collectWebSearchProviderBoundaryInventory() {
const inventory = [];
const files = (
await Promise.all(scanRoots.map(async (root) => await walkFiles(path.join(repoRoot, root))))
)
.flat()
.toSorted((left, right) =>
normalizeRelativePath(left).localeCompare(normalizeRelativePath(right)),
);
if (!webSearchProviderInventoryPromise) {
webSearchProviderInventoryPromise = (async () => {
const inventory = [];
const files = (
await Promise.all(scanRoots.map(async (root) => await walkFiles(path.join(repoRoot, root))))
)
.flat()
.toSorted((left, right) =>
normalizeRelativePath(left).localeCompare(normalizeRelativePath(right)),
);
for (const filePath of files) {
const relativeFile = normalizeRelativePath(filePath);
if (ignoredFiles.has(relativeFile) || relativeFile.includes(".test.")) {
continue;
}
const content = await fs.readFile(filePath, "utf8");
const lines = content.split(/\r?\n/);
for (const filePath of files) {
const relativeFile = normalizeRelativePath(filePath);
if (ignoredFiles.has(relativeFile) || relativeFile.includes(".test.")) {
continue;
}
const content = await fs.readFile(filePath, "utf8");
const lines = content.split(/\r?\n/);
if (relativeFile === "src/plugins/web-search-providers.ts") {
scanWebSearchProviderRegistry(lines, relativeFile, inventory);
continue;
}
if (relativeFile === "src/plugins/web-search-providers.ts") {
scanWebSearchProviderRegistry(lines, relativeFile, inventory);
continue;
}
scanGenericCoreImports(lines, relativeFile, inventory);
scanGenericCoreImports(lines, relativeFile, inventory);
}
return inventory.toSorted(compareInventoryEntries);
})();
}
return inventory.toSorted(compareInventoryEntries);
return await webSearchProviderInventoryPromise;
}
export async function readExpectedInventory() {