From 962e177df77d1d1ee30813825062af6d0f30dce3 Mon Sep 17 00:00:00 2001 From: Samanyu Das Date: Sat, 21 Nov 2026 22:06:44 +1030 Subject: [PATCH 1/2] test(repo-guards): find possible fixture importers through the guard's own resolver The prefilter re-derived resolveLocalImport's candidate paths by hand, so a later resolver change (a new extension, for example) could narrow it silently. It now passes every quoted text that opens with `.` in an unescaped file through resolveLocalImport itself. Without escapes, a request's text is its value, so this stays a superset of each file's relative requests. Files with a backslash stay candidates for every member. The text scan includes overlapping quote pairs, so a stray quote earlier on the line cannot consume a request's opening quote. A new case covers that. Co-Authored-By: Claude Opus 5.5 --- .../daemon-fixture-launch-guard.test.ts | 16 ++++++ scripts/lib/daemon-fixture-launch-guard.ts | 56 ++++++++++--------- 2 files changed, 47 insertions(+), 25 deletions(-) diff --git a/scripts/__tests__/daemon-fixture-launch-guard.test.ts b/scripts/__tests__/daemon-fixture-launch-guard.test.ts index b0761fe368..7bcf4ceeb0 100644 --- a/scripts/__tests__/daemon-fixture-launch-guard.test.ts +++ b/scripts/__tests__/daemon-fixture-launch-guard.test.ts @@ -125,6 +125,22 @@ for (const [fixture, consumer, specifier] of [ }); } +test('a stray quote before a request on the same line cannot hide the request', () => { + const bad = `import { runCmdDetachedMonitored } from '@agent-device/host-kit/command'; const spawn = vi.mocked(runCmdDetachedMonitored); spawn.mockImplementation(() => { throw new Error('raw'); });`; + const violations = scanDaemonFixtureLaunches([ + { + file: 'src/support.ts', + source: `import { createRegisteredDaemonFixtures } from '@agent-device/test-kit'; export const fixtures = createRegisteredDaemonFixtures({});`, + }, + { + file: 'src/consumer.test.ts', + source: `const note = "it'."; import { fixtures } from './support.ts'; ${bad}`, + }, + ]); + assert.equal(violations.length, 1); + assert.ok(violations[0]!.includes('src/consumer.test.ts')); +}); + for (const expression of [ '(spawn as any)', 'spawn!', diff --git a/scripts/lib/daemon-fixture-launch-guard.ts b/scripts/lib/daemon-fixture-launch-guard.ts index 68e31e81ec..9ba342920d 100644 --- a/scripts/lib/daemon-fixture-launch-guard.ts +++ b/scripts/lib/daemon-fixture-launch-guard.ts @@ -39,16 +39,13 @@ export function scanDaemonFixtureLaunches(files: readonly FixtureSource[]): stri .filter((entry) => mayNameSeed(entry.source) && namesSeed(parse(entry).program())) .map(({ file }) => file), ); - // Parsing every file to build the whole import graph costs seconds, so only a file that could - // import a family member is parsed. An escape can spell any request, so every file with one is a - // candidate. - const escaped = files.filter(({ source }) => source.includes('\\')); - const unescaped = files.filter(({ source }) => !source.includes('\\')); + // Parsing every file to build the whole import graph costs seconds, so only a possible importer + // of a family member is parsed. + const { escaped, possibleImporters } = indexPossibleImporters(files, paths); const pending = [...family]; while (pending.length > 0) { const member = pending.pop()!; - const candidates = [...escaped, ...unescaped.filter(mayImport(member))]; - for (const candidate of candidates) { + for (const candidate of [...escaped, ...(possibleImporters.get(member) ?? [])]) { if (family.has(candidate.file) || !importsOf(candidate).has(member)) continue; family.add(candidate.file); pending.push(candidate.file); @@ -60,27 +57,36 @@ export function scanDaemonFixtureLaunches(files: readonly FixtureSource[]): stri } /** - * A cheap superset of the unescaped files whose relative request resolves to `member`. - * {@link resolveLocalImport} reaches `member` from the member path, its `.js` alias, or its - * extensionless path. Each segment of a resolved path is spelled by the request, unless `.` and `..` - * steps take it from the importer's own path, which puts the importer under that path. A request to - * an index passes through its directory, so the directory stands for every form of an index. + * Indexes each unescaped file under every path its relative requests could resolve to. An escape + * can spell any request, so a file with one is a possible importer of every member. */ -function mayImport(member: string): (entry: FixtureSource) => boolean { - const stem = member.replace(/\.ts$/, ''); - const targets = - lastSegment(stem) === 'index' ? [path.posix.dirname(member)] : [member, `${stem}.js`, stem]; - // Every target's last segment contains this name. A module named `.ts` has an empty one, which - // admits every file. - const name = lastSegment(targets.at(-1)!); - const ancestors = targets.map((target) => `${target}/`); - return ({ file, source }) => - source.includes(name) || ancestors.some((ancestor) => file.startsWith(ancestor)); +function indexPossibleImporters(files: readonly FixtureSource[], paths: ReadonlySet) { + const escaped: FixtureSource[] = []; + const possibleImporters = new Map(); + for (const entry of files) { + if (entry.source.includes('\\')) { + escaped.push(entry); + continue; + } + const targets = quotedRelativeText(entry.source).flatMap( + (text) => resolveLocalImport(entry.file, text, paths) ?? [], + ); + for (const target of new Set(targets)) { + const importers = possibleImporters.get(target); + if (importers) importers.push(entry); + else possibleImporters.set(target, [entry]); + } + } + return { escaped, possibleImporters }; } -/** Unlike `path.basename`, keeps an empty segment empty (`src/fixtures/` gives ``). */ -function lastSegment(target: string): string { - return target.slice(target.lastIndexOf('/') + 1); +/** + * The text of every quote pair that opens with `.` on one line, overlaps included. In a source + * without escapes, a module request's text is its value, so this is a superset of its relative + * requests. + */ +function quotedRelativeText(source: string): string[] { + return [...source.matchAll(/(?=(['"])(\.(?:(?!\1)[^\n\r])*)\1)/g)].map((match) => match[2]!); } type ParsedFixtureSource = Readonly<{ program: () => AstNode; dependencies: readonly string[] }>; From 92ac51fc515011b3c1ce5ca09678264ea3fd7d29 Mon Sep 17 00:00:00 2001 From: Samanyu Das Date: Sat, 21 Nov 2026 22:06:44 +1030 Subject: [PATCH 2/2] test(repo-guards): cover the producer scan's escape and constant-only branches The skip regex kept modules with a backslash or a `PRODUCER` name, but only the live repo exercised those branches. Two example modules now need them: an escaped `produc\u0065r` key, and a lone `*_PRODUCER` constant. Each case fails when its branch is removed. Co-Authored-By: Claude Opus 5.5 --- test/repo-guards/snapshot-producer-emitters.test.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test/repo-guards/snapshot-producer-emitters.test.ts b/test/repo-guards/snapshot-producer-emitters.test.ts index 3991a329e0..fd50297818 100644 --- a/test/repo-guards/snapshot-producer-emitters.test.ts +++ b/test/repo-guards/snapshot-producer-emitters.test.ts @@ -261,6 +261,17 @@ test('an unlisted producer in a residue is reported even though the residue publ ]); }); +for (const declaration of [ + String.raw`export const residue = { 'produc\u0065r': 'unlisted-tree' };`, + "export const EXAMPLE_PRODUCER = 'unlisted-tree';", +]) { + test(`a module that never spells producer in plain lowercase is still scanned: ${declaration}`, () => { + assert.deepEqual(producerEmitterViolations([], example([declaration, ''])), [ + `${EXAMPLE_CAPTURE}:1 emits 'unlisted-tree', which SNAPSHOT_PRODUCERS does not list`, + ]); + }); +} + test('a computed identifier key is not a producer property, but a computed string key is', () => { const keyed = (key: string) => example([