fix: tighten workspace discovery and index Go sibling bindings

Skip leftover test/ workspace roots and extra Vite configs. Publish
same-package Go names from per-package indexes instead of pairing every file.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Gergo Magyar 2026-09-10 06:57:58 +00:00 • committed by Gergő Magyar
parent 4fd790d776
commit 7fd4f0efde
6 changed files with 431 additions and 244 deletions

View file

@ -215,6 +215,8 @@ interface WorkspaceScope {
readonly include: readonly string[];
/** `!`-prefixed patterns, with the `!` stripped. */
readonly exclude: readonly string[];
readonly includeRe: readonly RegExp[];
readonly excludeRe: readonly RegExp[];
}
/** Whether `dir` (repo-relative, `''` for the root) is an admitted package. */
@ -225,9 +227,8 @@ function admits(scope: WorkspaceScope | null, dir: string): boolean {
// An exclusion covers the directory AND everything under it: `!packages/legacy`
// must keep `packages/legacy/foo` out even when a nested workspace root inside
// the excluded subtree re-declares `packages/*`.
if (scope.exclude.some((pattern) => matchesDirOrAncestor(globToRegExp(pattern), dir)))
return false;
return scope.include.some((pattern) => globToRegExp(pattern).test(dir));
if (scope.excludeRe.some((re) => matchesDirOrAncestor(re, dir))) return false;
return scope.includeRe.some((re) => re.test(dir));
}
function matchesDirOrAncestor(re: RegExp, dir: string): boolean {
@ -300,37 +301,56 @@ async function loadWorkspaceScope(repoRoot: string): Promise<WorkspaceScope | nu
// `workspaces`) admit its example packages — and, being shallow, outrank the
// real package of the same name — and let a root inside an excluded subtree
// re-admit what the outer `!exclusion` had removed.
const rootPatterns = await readWorkspacePatternsAt(repoRoot);
const [rootPatterns, workspaceRoots] = await Promise.all([
readWorkspacePatternsAt(repoRoot),
findWorkspaceRoots(repoRoot),
]);
const rootScope = rootPatterns.length === 0 ? null : toScope(rootPatterns);
const patterns: string[] = [...rootPatterns];
for (const root of await findWorkspaceRoots(repoRoot)) {
const admitted: { root: string; prefix: string }[] = [];
for (const root of workspaceRoots) {
if (root === repoRoot) continue;
const prefix = repoRelativeDir(repoRoot, root);
if (rootScope !== null && !admits(rootScope, prefix)) continue;
const rebase = (p: string): string => {
const negated = p.startsWith('!');
const body = negated ? p.slice(1) : p;
const joined = prefix === '' ? body : `${prefix}/${body.replace(/^\.\//, '')}`;
return negated ? `!${joined}` : joined;
};
for (const pattern of await readWorkspacePatternsAt(root)) patterns.push(rebase(pattern));
admitted.push({ root, prefix });
}
const nested = await Promise.all(
admitted.map(async ({ root, prefix }) => {
const rebase = (p: string): string => {
const negated = p.startsWith('!');
const body = negated ? p.slice(1) : p;
const joined = prefix === '' ? body : `${prefix}/${body.replace(/^\.\//, '')}`;
return negated ? `!${joined}` : joined;
};
return (await readWorkspacePatternsAt(root)).map(rebase);
}),
);
for (const batch of nested) patterns.push(...batch);
if (patterns.length === 0) return null;
return toScope(patterns);
}
function toScope(patterns: readonly string[]): WorkspaceScope {
const include = patterns.filter((p) => !p.startsWith('!'));
const exclude = patterns.filter((p) => p.startsWith('!')).map((p) => p.slice(1));
return {
include: patterns.filter((p) => !p.startsWith('!')),
exclude: patterns.filter((p) => p.startsWith('!')).map((p) => p.slice(1)),
include,
exclude,
includeRe: include.map(globToRegExp),
excludeRe: exclude.map(globToRegExp),
};
}
/** The workspace patterns declared at ONE directory, all three spellings merged. */
async function readWorkspacePatternsAt(root: string): Promise<string[]> {
const [rootManifest, yamlPkgs, ymlPkgs, lerna] = await Promise.all([
readJsonFile(path.join(root, 'package.json')),
readYamlPackages(path.join(root, 'pnpm-workspace.yaml')),
readYamlPackages(path.join(root, 'pnpm-workspace.yml')),
readJsonFile(path.join(root, 'lerna.json')),
]);
const patterns: string[] = [];
const rootManifest = await readJsonFile(path.join(root, 'package.json'));
const workspaces = rootManifest?.workspaces;
if (Array.isArray(workspaces)) {
patterns.push(...workspaces.filter((w): w is string => typeof w === 'string'));
@ -341,9 +361,7 @@ async function readWorkspacePatternsAt(root: string): Promise<string[]> {
patterns.push(...nested.filter((w): w is string => typeof w === 'string'));
}
}
patterns.push(...(await readYamlPackages(path.join(root, 'pnpm-workspace.yaml'))));
patterns.push(...(await readYamlPackages(path.join(root, 'pnpm-workspace.yml'))));
const lerna = await readJsonFile(path.join(root, 'lerna.json'));
patterns.push(...yamlPkgs, ...ymlPkgs);
if (Array.isArray(lerna?.packages)) {
patterns.push(...lerna.packages.filter((w): w is string => typeof w === 'string'));
}
@ -363,7 +381,14 @@ const WORKSPACE_ROOT_MAX_DEPTH = 4;
* it now warns, so a truncated scan is a logged fact rather than a quiet one.
*/
const WORKSPACE_ROOT_SCAN_MAX_DIRS = 50_000;
/** Directory names whose nested `workspaces` are starters/fixtures, never members. */
/**
* Directory names whose nested `workspaces` are starters, fixtures, or test
* trees — never members. A leftover `test/pnpm-workspace.yaml` must not become
* a workspace root when the repo root declares none (the #2953 first-wins
* class). A root `workspaces` glob that lists `test/*` still admits those
* packages through the package.json walk; this set only stops nested-root
* discovery from descending into those names.
*/
const NON_MEMBER_ROOT_DIRS = new Set([
'example',
'examples',
@ -373,7 +398,41 @@ const NON_MEMBER_ROOT_DIRS = new Set([
'templates',
'sample',
'samples',
'test',
'tests',
'e2e',
'__tests__',
'spec',
'specs',
]);
/**
* Vite's `DEFAULT_CONFIG_FILES` order. The first filename that exists is the
* config Vite loads; leftover siblings are not a second live entry.
*/
const VITE_CONFIG_FILES = [
'vite.config.js',
'vite.config.mjs',
'vite.config.ts',
'vite.config.cjs',
'vite.config.mts',
'vite.config.cts',
] as const;
function isMissingPath(err: unknown): boolean {
return (err as NodeJS.ErrnoException).code === 'ENOENT';
}
async function readDirSorted(dir: string): Promise<import('fs').Dirent[] | null> {
try {
return (await fs.readdir(dir, { withFileTypes: true })).sort((a, b) =>
a.name < b.name ? -1 : a.name > b.name ? 1 : 0,
);
} catch {
return null;
}
}
async function findWorkspaceRoots(repoRoot: string): Promise<string[]> {
const roots: string[] = [repoRoot];
const queue: { dir: string; depth: number }[] = [{ dir: repoRoot, depth: 0 }];
@ -386,14 +445,8 @@ async function findWorkspaceRoots(repoRoot: string): Promise<string[]> {
break;
}
const { dir, depth } = queue[scanned++]!;
let entries: import('fs').Dirent[];
try {
entries = (await fs.readdir(dir, { withFileTypes: true })).sort((a, b) =>
a.name < b.name ? -1 : a.name > b.name ? 1 : 0,
);
} catch {
continue;
}
const entries = await readDirSorted(dir);
if (entries === null) continue;
if (dir !== repoRoot) {
const names = new Set(entries.filter((e) => e.isFile()).map((e) => e.name));
let declares =
@ -444,13 +497,6 @@ async function readYamlPackages(filePath: string): Promise<string[]> {
}
}
/**
* Collect the `package.json` of every ADMITTED workspace package.
*
* Directory-only BFS: the sole files opened are manifests and the workspace
* declaration, so this is far cheaper than the C# namespace scan next door,
* which reads every `.cs` file.
*/
/**
* Per-repo memo. The TS/JS/Vue scope resolvers and the unresolved-call ledger
* classifier each ask for the same map during one analyze; without this the
@ -460,9 +506,8 @@ async function readYamlPackages(filePath: string): Promise<string[]> {
*/
const workspacePackagesMemo = new Map<string, Promise<NodeWorkspacePackages | null>>();
export function invalidateNodeWorkspacePackages(repoRoot?: string): void {
if (repoRoot === undefined) workspacePackagesMemo.clear();
else workspacePackagesMemo.delete(path.resolve(repoRoot));
export function invalidateNodeWorkspacePackages(repoRoot: string): void {
workspacePackagesMemo.delete(path.resolve(repoRoot));
}
export async function loadNodeWorkspacePackages(
@ -485,6 +530,13 @@ export async function loadNodeWorkspacePackages(
return pending;
}
/**
* Collect the `package.json` of every ADMITTED workspace package.
*
* Directory-only BFS: the sole files opened are manifests and the workspace
* declaration, so this is far cheaper than the C# namespace scan next door,
* which reads every `.cs` file.
*/
async function loadNodeWorkspacePackagesUncached(
repoRoot: string,
): Promise<NodeWorkspacePackages | null> {
@ -504,16 +556,10 @@ async function loadNodeWorkspacePackagesUncached(
const { dir, depth } = queue[queueHead++]!;
dirsScanned++;
let entries: import('fs').Dirent[];
try {
// Sorted like findWorkspaceRoots: same-depth name collisions resolve first-wins,
// and readdir order is filesystem-dependent.
entries = (await fs.readdir(dir, { withFileTypes: true })).sort((a, b) =>
a.name < b.name ? -1 : a.name > b.name ? 1 : 0,
);
} catch {
continue;
}
// Sorted: same-depth name collisions resolve first-wins, and readdir order
// is filesystem-dependent.
const entries = await readDirSorted(dir);
if (entries === null) continue;
for (const entry of entries) {
if (entry.isDirectory()) {
@ -592,7 +638,7 @@ async function readManifest(
const rootlessExports = declaresExports && rootExports.length === 0;
const discovered = rootlessExports
? { entries: [], ambiguous: [], allowConventional: false }
: await discoverSourceEntries(parsed, dir, rebase, packageDir, repoRoot);
: await discoverSourceEntries(parsed, dir, repoRoot);
for (const entry of discovered.entries) push(entries, entry);
if (!declaresExports && discovered.allowConventional) {
for (const conventional of ['src/index', 'index', 'lib/index']) {
@ -698,24 +744,32 @@ function looksLikeBuildOutput(entry: string): boolean {
return /(^|\/)(dist|build|lib|out|esm|cjs|umd)\//.test(entry) || /\.min\.[cm]?js$/.test(entry);
}
function declaredRootExportStrings(exportsRoot: unknown): string[] {
if (typeof exportsRoot === 'string') return [exportsRoot];
if (exportsRoot === null || typeof exportsRoot !== 'object') return [];
const dot = (exportsRoot as Record<string, unknown>)['.'];
if (typeof dot === 'string') return [dot];
if (dot === null || typeof dot !== 'object') return [];
return Object.values(dot).filter((v): v is string => typeof v === 'string');
}
async function existingStems(repoRoot: string, stems: readonly string[]): Promise<string[]> {
const present = await Promise.all(stems.map((stem) => stemExists(repoRoot, stem)));
const existing: string[] = [];
for (let i = 0; i < stems.length; i++) {
if (present[i]) push(existing, stems[i]!);
}
return existing;
}
async function discoverSourceEntries(
parsed: Record<string, unknown>,
dir: string,
rebase: (raw: string) => string,
packageDir: string,
repoRoot: string,
): Promise<{ entries: string[]; ambiguous: string[]; allowConventional: boolean }> {
const declared: string[] = [];
const exportsRoot = parsed.exports;
if (typeof exportsRoot === 'string') declared.push(exportsRoot);
else if (exportsRoot !== null && typeof exportsRoot === 'object') {
const dot = (exportsRoot as Record<string, unknown>)['.'];
if (typeof dot === 'string') declared.push(dot);
else if (dot !== null && typeof dot === 'object') {
for (const v of Object.values(dot as Record<string, unknown>))
if (typeof v === 'string') declared.push(v);
}
}
const packageDir = repoRelativeDir(repoRoot, dir);
const rebase = (raw: string): string => joinRepoPath(packageDir, stripEntryPrefixes(raw));
const declared: string[] = declaredRootExportStrings(parsed.exports);
for (const field of ['module', 'main']) {
const value = parsed[field];
if (typeof value === 'string') declared.push(value);
@ -734,26 +788,32 @@ async function discoverSourceEntries(
if (typeof ps === 'string') candidates.push(rebase(ps));
}
let hasViteConfig = false;
for (const cfg of ['vite.config.ts', 'vite.config.mts', 'vite.config.js', 'vite.config.mjs']) {
for (const cfg of VITE_CONFIG_FILES) {
let text: string;
try {
const text = await fs.readFile(path.join(dir, cfg), 'utf-8');
text = await fs.readFile(path.join(dir, cfg), 'utf-8');
} catch (err) {
if (isMissingPath(err)) continue;
// Present but unreadable: Vite still selected this filename.
hasViteConfig = true;
break;
}
hasViteConfig = true;
try {
const entry = await staticViteEntry(text);
if (entry !== null) push(candidates, rebase(entry));
} catch {
/* no such config */
// Parse/load failure is not a missing file — do not fall through to a leftover sibling.
}
break;
}
const existing: string[] = [];
for (const candidate of candidates) {
if (await stemExists(repoRoot, candidate)) push(existing, candidate);
}
let existing = await existingStems(repoRoot, candidates);
// A config we cannot establish is not evidence for a conventional entry.
if (existing.length === 0 && !hasViteConfig) {
for (const conventional of ['src/main', 'src/index']) {
const stem = joinRepoPath(packageDir, conventional);
if (await stemExists(repoRoot, stem)) push(existing, stem);
}
existing = await existingStems(
repoRoot,
['src/main', 'src/index'].map((conventional) => joinRepoPath(packageDir, conventional)),
);
}
const allowConventional = !hasViteConfig && candidates.length === 0;
if (existing.length > 1) return { entries: [], ambiguous: existing, allowConventional };
@ -776,39 +836,48 @@ async function staticViteEntry(text: string): Promise<string | null> {
(node) => node.type === 'export_statement' && node.children.some((c) => c.type === 'default'),
);
if (exports.length !== 1) return null;
let config = exports[0]!.childForFieldName('value');
if (config?.type === 'call_expression') {
if (config.childForFieldName('function')?.text !== 'defineConfig') return null;
// A locally defined helper need not preserve its argument like Vite does.
if (
tree.rootNode
.descendantsOfType(['function_declaration', 'variable_declarator'])
.some((node) => node.childForFieldName('name')?.text === 'defineConfig')
)
return null;
for (const statement of tree.rootNode.namedChildren) {
if (statement.type !== 'import_statement') continue;
if (!statement.descendantsOfType('identifier').some((n) => n.text === 'defineConfig'))
continue;
const source = statement.childForFieldName('source')?.text;
if (source !== "'vite'" && source !== '"vite"') return null;
const identityImport = statement.descendantsOfType('import_specifier').some((specifier) => {
const imported = specifier.childForFieldName('name')?.text;
const local = specifier.childForFieldName('alias')?.text ?? imported;
return imported === 'defineConfig' && local === 'defineConfig';
});
if (!identityImport) return null;
}
const args = config
.childForFieldName('arguments')
?.namedChildren.filter((n) => n.type !== 'comment');
if (args?.length !== 1) return null;
config = args[0]!;
}
let config = unwrapViteDefineConfig(tree, exports[0]!.childForFieldName('value'));
if (config === null) return null;
for (const key of ['build', 'lib', 'entry']) config = staticObjectProperty(config, key);
if (config?.type !== 'string' || config.namedChildren.some((n) => n.type === 'escape_sequence'))
return staticStringValue(config);
}
/** Unwrap `defineConfig({...})` from Vite; refuse local or re-exported helpers. */
function unwrapViteDefineConfig(
tree: { rootNode: Parser.SyntaxNode },
config: Parser.SyntaxNode | null,
): Parser.SyntaxNode | null {
if (config?.type !== 'call_expression') return config;
if (config.childForFieldName('function')?.text !== 'defineConfig') return null;
// A locally defined helper need not preserve its argument like Vite does.
if (
tree.rootNode
.descendantsOfType(['function_declaration', 'variable_declarator'])
.some((node) => node.childForFieldName('name')?.text === 'defineConfig')
)
return null;
return config.text.slice(1, -1);
for (const statement of tree.rootNode.namedChildren) {
if (statement.type !== 'import_statement') continue;
if (!statement.descendantsOfType('identifier').some((n) => n.text === 'defineConfig')) continue;
if (staticStringValue(statement.childForFieldName('source')) !== 'vite') return null;
const identityImport = statement.descendantsOfType('import_specifier').some((specifier) => {
const imported = specifier.childForFieldName('name')?.text;
const local = specifier.childForFieldName('alias')?.text ?? imported;
return imported === 'defineConfig' && local === 'defineConfig';
});
if (!identityImport) return null;
}
const args = config
.childForFieldName('arguments')
?.namedChildren.filter((n) => n.type !== 'comment');
if (args?.length !== 1) return null;
return args[0]!;
}
function staticStringValue(node: Parser.SyntaxNode | null): string | null {
if (node?.type !== 'string' || node.namedChildren.some((n) => n.type === 'escape_sequence'))
return null;
return node.text.slice(1, -1);
}
/** Reject computed keys, spreads and duplicate properties that could override a value. */
@ -823,51 +892,19 @@ function staticObjectProperty(
if (member.type !== 'pair') return null;
const name = member.childForFieldName('key');
if (name?.type !== 'property_identifier' && name?.type !== 'string') return null;
if (name.type === 'string' && name.namedChildren.some((n) => n.type === 'escape_sequence'))
return null;
const property = name.type === 'string' ? name.text.slice(1, -1) : name.text;
if (property !== key) continue;
if (name.type === 'string') {
const property = staticStringValue(name);
if (property === null) return null;
if (property !== key) continue;
} else if (name.text !== key) {
continue;
}
if (value !== null) return null;
value = member.childForFieldName('value');
}
return value;
}
/**
* Remove line (`//`) and block comments from JS/TS config text before a regex
* reads it. String contents are preserved (a `//` inside quotes is not a
* comment), so `entry: 'src/index.ts'` survives, as does a comment opener
* written inside a string.
*/
export function stripJsComments(text: string): string {
let out = '';
let i = 0;
while (i < text.length) {
const ch = text[i]!;
const next = text[i + 1];
if (ch === '"' || ch === "'" || ch === '`') {
const quote = ch;
let j = i + 1;
while (j < text.length && text[j] !== quote) {
if (text[j] === '\\') j++;
j++;
}
out += text.slice(i, j + 1);
i = j + 1;
} else if (ch === '/' && next === '/') {
const end = text.indexOf('\n', i);
i = end === -1 ? text.length : end;
} else if (ch === '/' && next === '*') {
const end = text.indexOf('*/', i + 2);
i = end === -1 ? text.length : end + 2;
} else {
out += ch;
i++;
}
}
return out;
}
/** A repo-relative stem exists as a source file (with any TS/JS extension). */
// The root is threaded explicitly: a module-level "current root" clobbered
// under two concurrent scans and turned an ambiguity refusal into a confident

View file

@ -4,27 +4,6 @@ import type { ScopeResolutionIndexes } from '../../model/scope-resolution-indexe
import { expandGoDotImports } from './expand-wildcards.js';
import { goPackageDir, inferGoPackageName } from './package-clause.js';
/**
* O(n²×d) where n = files per package, d = defs per file.
* Acceptable for V1 since Go packages are typically small (< 20 files).
* Future optimization: build a name→def inverted index per package to reduce
* to O(n×d).
*/
/**
* Go test files. `_test.go` files are compiled into the package's test binary:
* an INTERNAL test (`package foo`) sees every name its non-test siblings and
* the other `_test.go` files of the package declare; an EXTERNAL test
* (`package foo_test`) is a separate package. This table publishes no
* bare-name sibling bindings across that partition — qualified `foo.X` and
* `import .` stay on the import resolver. Non-test files never see
* test-only helpers — `go build` does not compile them.
*
* Before this, `_test.go` files were dropped from sibling augmentation
* entirely, so every same-package free call from a test fell through to the
* global unique-name fallback: 4,857 labeled guesses on grafana@871af0720,
* 25/25 sampled being test → same-directory helper — the right target reached
* through the wrong path, at guess confidence.
*/
function isGoTestFile(filePath: string): boolean {
return filePath.endsWith('_test.go');
}
@ -60,6 +39,73 @@ function testFilePackageOf(
return { pkg: declared, external: false };
}
interface IndexedDef {
readonly filePath: string;
readonly ref: BindingRef;
}
/** name → defs, in package file order. */
type NameIndex = Map<string, IndexedDef[]>;
function defBareName(def: SymbolDefinition): string {
return def.qualifiedName?.split('.').pop() ?? def.qualifiedName ?? '';
}
function appendToIndex(
index: NameIndex,
filePath: string,
defs: readonly SymbolDefinition[],
): void {
for (const def of defs) {
const name = defBareName(def);
if (name === '') continue;
const list = index.get(name) ?? [];
list.push({ filePath, ref: { def, origin: 'namespace' } });
index.set(name, list);
}
}
function publishIndex(
augmentations: Map<ScopeId, Map<string, BindingRef[]>>,
index: NameIndex,
receiverPath: string,
receiverModule: ScopeId,
): void {
if (index.size === 0) return;
let scopeBindings = augmentations.get(receiverModule);
if (scopeBindings === undefined) {
scopeBindings = new Map<string, BindingRef[]>();
augmentations.set(receiverModule, scopeBindings);
}
for (const [name, entries] of index) {
let bucket = scopeBindings.get(name);
const seen =
bucket === undefined ? new Set<string>() : new Set(bucket.map((b) => b.def.nodeId));
for (const entry of entries) {
if (entry.filePath === receiverPath) continue;
if (seen.has(entry.ref.def.nodeId)) continue;
if (bucket === undefined) {
bucket = [];
scopeBindings.set(name, bucket);
}
bucket.push(entry.ref);
seen.add(entry.ref.def.nodeId);
}
}
}
/**
* Publish same-package sibling bindings, including `_test.go` files.
*
* Internal tests (`package foo`) see production and other internal-test names.
* External tests (`package foo_test`) get no bare-name bindings across that
* partition — qualified `foo.X` and `import .` stay on the import resolver.
* Non-test files never see test-only helpers (`go build` does not compile them).
*
* Per-package name→def indexes are built in O(n×d). Each receiver walks only
* the partitions it can see, so defs are not re-scanned against every sibling
* file. Binding refs are allocated once and reused across receivers.
*/
export function populateGoPackageSiblings(
parsedFiles: readonly ParsedFile[],
indexes: ScopeResolutionIndexes,
@ -111,64 +157,36 @@ export function populateGoPackageSiblings(
: { pkg: declared, external: false };
const key = `${dir}\0${pkg}`;
const list = filesByPackage.get(key) ?? [];
list.push({ filePath: parsed.filePath, defs: [...parsed.localDefs], isTest, external });
list.push({ filePath: parsed.filePath, defs: parsed.localDefs, isTest, external });
filesByPackage.set(key, list);
}
// 3. Use bindingAugmentations channel per I8
// 3. Use bindingAugmentations channel per I8. Same-package files see ALL
// sibling names (exported and unexported). Cross-package visibility is
// the import resolver's job.
const augmentations = indexes.bindingAugmentations as Map<ScopeId, Map<string, BindingRef[]>>;
for (const [, siblings] of filesByPackage) {
for (const target of siblings) {
const targetModule = indexes.moduleScopes.byFilePath.get(target.filePath);
if (targetModule === undefined) continue;
for (const receiver of siblings) {
if (receiver.filePath === target.filePath) continue; // no self-reference
// Non-test files never see test-only declarations.
if (target.isTest && !receiver.isTest) continue;
// A `foo` internal test does not see a `foo_test` file's declarations
// at all (different packages) — and an external test package sees `foo`
// ONLY qualified (`foo.NewThing`), never as a bare name: that binding
// comes from its explicit import of the package path, through the
// ordinary import resolver. Publishing bare exported names here bound
// `NewThing()` in `foo_test` to a call Go itself would reject.
if (target.external && !receiver.external) continue;
if (receiver.external && !target.external) continue;
const receiverModule = indexes.moduleScopes.byFilePath.get(receiver.filePath);
if (receiverModule === undefined) continue;
for (const def of target.defs) {
// Go: same-package sibling files can see ALL names (both
// exported/uppercase and unexported/lowercase). Only cross-
// package visibility requires uppercase first letter.
const name = def.qualifiedName?.split('.').pop() ?? def.qualifiedName ?? '';
if (name === '') continue;
const bucket = getAugmentationBucket(augmentations, receiverModule, name);
if (bucket.some((b) => b.def.nodeId === def.nodeId)) continue;
bucket.push({ def, origin: 'namespace' });
}
for (const siblings of filesByPackage.values()) {
if (siblings.length < 2) continue;
const production: NameIndex = new Map();
const internalTest: NameIndex = new Map();
const external: NameIndex = new Map();
const receivers: { file: SiblingFile; module: ScopeId }[] = [];
for (const file of siblings) {
const module = indexes.moduleScopes.byFilePath.get(file.filePath);
if (module === undefined) continue;
receivers.push({ file, module });
if (file.external) appendToIndex(external, file.filePath, file.defs);
else if (file.isTest) appendToIndex(internalTest, file.filePath, file.defs);
else appendToIndex(production, file.filePath, file.defs);
}
for (const { file, module } of receivers) {
if (file.external) {
publishIndex(augmentations, external, file.filePath, module);
continue;
}
publishIndex(augmentations, production, file.filePath, module);
if (file.isTest) publishIndex(augmentations, internalTest, file.filePath, module);
}
}
}
function getAugmentationBucket(
augmentations: Map<ScopeId, Map<string, BindingRef[]>>,
scopeId: ScopeId,
name: string,
): BindingRef[] {
let scopeBindings = augmentations.get(scopeId);
if (scopeBindings === undefined) {
scopeBindings = new Map<string, BindingRef[]>();
augmentations.set(scopeId, scopeBindings);
}
let bucketArr = scopeBindings.get(name);
if (bucketArr === undefined) {
bucketArr = [];
scopeBindings.set(name, bucketArr);
}
return bucketArr;
}

View file

@ -1,12 +1,9 @@
/**
* Final-review fixes on workspace-package discovery:
* - B2: an `exports` map with no `"."` (rootless) refuses the bare specifier —
* discovery must not manufacture a `src/index` root entry for it.
* - M6: nested workspace roots are gated by the outer scope; an outer
* `!exclusion` keeps binding under a nested root; starter/fixture roots
* (`examples/`, `fixtures/`, `templates/`, `samples/`) are never roots.
* - M9: the package map is memoised per repo root within a process and can be
* invalidated explicitly.
* Workspace-package discovery contracts:
* - an `exports` map with no `"."` refuses the bare specifier
* - nested workspace roots are gated by the outer scope; starter, fixture,
* and test trees are never roots
* - the package map is memoised per repo root and can be invalidated
*/
import { describe, it, expect, beforeAll, afterAll } from 'vitest';
import fs from 'fs';
@ -205,6 +202,51 @@ describe('M6 — nested workspace roots are gated by the outer scope', () => {
});
});
describe('test/ is never a nested workspace root', () => {
it('a leftover test/pnpm-workspace.yaml does not admit fixture package names', async () => {
const { dir, w } = mkRepo('gn-m6-test-root-');
try {
w('README.md', '# polyglot\n');
w('js/pnpm-workspace.yaml', 'packages:\n - "libs/*"\n');
w('js/libs/lodash/package.json', JSON.stringify({ name: 'lodash', main: 'src/index.ts' }));
w('js/libs/lodash/src/index.ts', 'export const real = 1;\n');
w('test/pnpm-workspace.yaml', 'packages:\n - "fixtures/*"\n');
w(
'test/fixtures/lodash/package.json',
JSON.stringify({ name: 'lodash', main: 'src/index.ts' }),
);
w('test/fixtures/lodash/src/index.ts', 'export const fake = 1;\n');
const pkgs = await loadNodeWorkspacePackages(dir);
expect(pkgs!.byName.get('lodash')?.dir).toBe('js/libs/lodash');
for (const pkg of pkgs!.byName.values()) expect(pkg.dir.startsWith('test/')).toBe(false);
} finally {
rm(dir);
}
});
it('a root workspaces glob that lists test/* still admits those packages', async () => {
const { dir, w } = mkRepo('gn-m6-test-listed-');
try {
w(
'package.json',
JSON.stringify({ name: 'root', private: true, workspaces: ['packages/*', 'test/*'] }),
);
w('packages/real/package.json', JSON.stringify({ name: '@repo/real', main: 'src/index.ts' }));
w('packages/real/src/index.ts', 'export const real = 1;\n');
w(
'test/helpers/package.json',
JSON.stringify({ name: '@repo/test-helpers', main: 'src/index.ts' }),
);
w('test/helpers/src/index.ts', 'export const help = 1;\n');
const pkgs = await loadNodeWorkspacePackages(dir);
expect(pkgs!.byName.get('@repo/test-helpers')?.dir).toBe('test/helpers');
expect(pkgs!.byName.get('@repo/real')?.dir).toBe('packages/real');
} finally {
rm(dir);
}
});
});
describe('M9 — per-repo memo', () => {
let dir: string;
beforeAll(() => {

View file

@ -1,28 +1,12 @@
/**
* Review finding on #3182 (magyargergo, node-workspace-packages.ts:730): the
* vite `lib.entry` regex took its FIRST match, which could sit inside a comment
* (`// old lib: { entry: 'src/wrong.ts' }`) ahead of the live config. Comments
* are stripped first, and every live `lib.entry` is a candidate so two
* disagreeing ones are refused as ambiguous rather than first-wins.
* AST Vite discovery ignores commented-out `lib.entry` text and refuses a
* default export that is not a single static config object.
*/
import { describe, it, expect, beforeAll, afterAll } from 'vitest';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import {
loadNodeWorkspacePackages,
stripJsComments,
} from '../../src/core/ingestion/import-resolvers/node-workspace-packages.js';
describe('stripJsComments', () => {
it('drops line and block comments and keeps string contents intact', () => {
expect(stripJsComments("a; // lib: { entry: 'x' }\nb /* lib: {\n entry: 'y' } */ c")).toBe(
'a; \nb c',
);
expect(stripJsComments("const u = 'http://x/*y'; // c")).toBe("const u = 'http://x/*y'; ");
expect(stripJsComments('const s = "a\\"//b"; x')).toBe('const s = "a\\"//b"; x');
});
});
import { loadNodeWorkspacePackages } from '../../src/core/ingestion/import-resolvers/node-workspace-packages.js';
describe('vite lib.entry discovery ignores comments and refuses disagreeing entries', () => {
let dir: string;

View file

@ -73,3 +73,41 @@ describe('Vite discovery follows only the exported static build.lib.entry', () =
).toContain('src/right');
});
});
describe('Vite discovery uses the first existing config filename', () => {
async function entriesForConfigs(files: Record<string, string>): Promise<readonly string[]> {
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'gn-vite-leftover-'));
roots.push(root);
fs.mkdirSync(path.join(root, 'src'));
fs.writeFileSync(
path.join(root, 'package.json'),
JSON.stringify({ name: '@test/config', main: './dist/bundle.js' }),
);
for (const [name, text] of Object.entries(files)) {
fs.writeFileSync(path.join(root, name), text);
}
for (const name of ['from-js', 'from-ts', 'from-mjs']) {
fs.writeFileSync(path.join(root, 'src', `${name}.ts`), `export const ${name} = 1;`);
}
return (await loadNodeWorkspacePackages(root))!.byName.get('@test/config')!.entries;
}
it("adopts Vite's first existing file instead of refusing leftover siblings", async () => {
const entries = await entriesForConfigs({
'vite.config.js': `export default { build: { lib: { entry: 'src/from-js.ts' } } };\n`,
'vite.config.ts': `export default { build: { lib: { entry: 'src/from-ts.ts' } } };\n`,
});
expect(entries).toContain('src/from-js');
expect(entries).not.toContain('src/from-ts');
expect(entries).toContain('dist/bundle');
});
it('does not fall through to a later filename when the first config exists', async () => {
const entries = await entriesForConfigs({
'vite.config.mjs': `export default { build: { lib: { entry: 'src/from-mjs.ts' } } };\n`,
'vite.config.ts': `export default { build: { lib: { entry: 'src/from-ts.ts' } } };\n`,
});
expect(entries).toContain('src/from-mjs');
expect(entries).not.toContain('src/from-ts');
});
});

View file

@ -41,19 +41,87 @@ describe('Go package siblings', () => {
expect(augmentations.get('module:foo-a')?.get('OnlyBar')).toBeUndefined();
expect(augmentations.get('module:bar-a')?.get('OnlyFoo')).toBeUndefined();
});
it("publishes same-name sibling defs in file order and never includes a file's own defs", () => {
const aFoo = def('a-foo', 'pkg/a/a.go', 'Foo');
const bFoo = def('b-foo', 'pkg/a/b.go', 'Foo');
const bBar = def('b-bar', 'pkg/a/b.go', 'Bar');
const cBaz = def('c-baz', 'pkg/a/c.go', 'Baz');
const parsedFiles: ParsedFile[] = [
parsed('pkg/a/a.go', 'module:a', aFoo),
parsed('pkg/a/b.go', 'module:b', bFoo, bBar),
parsed('pkg/a/c.go', 'module:c', cBaz),
];
const indexes = {
moduleScopes: {
byFilePath: new Map([
['pkg/a/a.go', 'module:a'],
['pkg/a/b.go', 'module:b'],
['pkg/a/c.go', 'module:c'],
]),
},
imports: new Map(),
bindings: new Map(),
bindingAugmentations: new Map(),
} as unknown as ScopeResolutionIndexes;
const fileContents = new Map([
['pkg/a/a.go', 'package a\n'],
['pkg/a/b.go', 'package a\n'],
['pkg/a/c.go', 'package a\n'],
]);
populateGoPackageSiblings(parsedFiles, indexes, { fileContents });
const augmentations = indexes.bindingAugmentations;
expect(
augmentations
.get('module:c')
?.get('Foo')
?.map((b) => b.def.nodeId),
).toEqual(['a-foo', 'b-foo']);
expect(
augmentations
.get('module:a')
?.get('Foo')
?.map((b) => b.def.nodeId),
).toEqual(['b-foo']);
expect(
augmentations
.get('module:a')
?.get('Bar')
?.map((b) => b.def.nodeId),
).toEqual(['b-bar']);
expect(
augmentations
.get('module:a')
?.get('Baz')
?.map((b) => b.def.nodeId),
).toEqual(['c-baz']);
expect(
augmentations
.get('module:b')
?.get('Foo')
?.map((b) => b.def.nodeId),
).toEqual(['a-foo']);
});
});
function def(nodeId: string, filePath: string, name: string): SymbolDefinition {
return { nodeId, filePath, type: 'Function', qualifiedName: name };
}
function parsed(filePath: string, moduleScope: string, localDef: SymbolDefinition): ParsedFile {
function parsed(
filePath: string,
moduleScope: string,
...localDefs: SymbolDefinition[]
): ParsedFile {
return {
filePath,
moduleScope,
scopes: [],
parsedImports: [],
localDefs: [localDef],
localDefs,
referenceSites: [],
};
}