mirror of
https://github.com/garrytan/gstack.git
synced 2026-11-18 22:24:17 +01:00
Pre-push test selection finds tests in monorepo apps that use their own @/ alias (#4464)
/ship's pre-push test selection now finds the tests of a monorepo app that imports through its own `@/` alias (for example `apps/mobile` mapping `@/*` to `./src/*`). Why: only the root tsconfig was read, so every touched file in such an app failed with `PREGATE_NO_LANE` even with a test beside it. Rebuilt from #4462 by @dtoelke, who found and fixed it; thank you. Nothing to do: a repo whose only tsconfig is at the root (gstack itself) resolves exactly as before. Co-authored-by: stackpt-agent <348614618+stackpt-agent@users.noreply.github.com> Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com> Review-Finding: p1/specialist-batched#1 reader=testing Review-Finding: p2v/outside-voice#1 reader=outside-voice
This commit is contained in:
4 files changed
+143
-11
No files matched your search
@@ -0,0 +1 @@
|
||||
- **/ship's pre-gate finds the tests of a monorepo app that imports through its own `@/` alias.** The import graph read `paths` and `baseUrl` from the repository root's tsconfig only, so in an app like `apps/mobile` whose `tsconfig.json` maps `@/*` to `./src/*`, every touched source file failed with `PREGATE_NO_LANE` even with a test right beside it. Each file now takes its aliases from the nearest tsconfig.json or jsconfig.json above it that declares `paths` or `baseUrl`; a nested config that declares neither defers to the next one up, the root config governs the rest, and an `extends` outside the repository is still never followed. Thanks to @dtoelke for the report and the original fix (#4462).
|
||||
@@ -353,6 +353,15 @@ Two stages with separate clocks, both printed:
|
||||
modified file whose only test the branch deleted still has no lane, and a
|
||||
remaining test that reads a deleted file or directory is selected. /review
|
||||
lists a deleted test as a gate edit (RH-1) either way.
|
||||
A JS/TS file's `paths` aliases and `baseUrl` come from the nearest
|
||||
tsconfig.json (else jsconfig.json) above it that declares `paths` or
|
||||
`baseUrl`, itself or through a relative `extends` chain inside the
|
||||
repository (up to four hops), joined relative to the config that declares
|
||||
them. A nested config that declares neither defers to the next one up, and
|
||||
the repository root's config governs the rest, so `@/x` in
|
||||
`apps/mobile/src/a.test.ts` resolves through `apps/mobile/tsconfig.json`.
|
||||
A package-name `extends` or one that leaves the repository is not followed
|
||||
and is named in the no-lane explanation.
|
||||
An extensionless JS/TS import (relative, a tsconfig/jsconfig `paths` alias or
|
||||
`baseUrl`) tries the exact path, then `.ts`, `.tsx`, `.js`, `.jsx`, `.mjs`,
|
||||
`.cjs`, `.mts`, `.cts`, then `index.ts`, `index.tsx`, `index.js`, `index.jsx`
|
||||
|
||||
+49
-11
@@ -177,10 +177,10 @@ function normalize(p: string): string { return path.posix.normalize(p.replace(/\
|
||||
|
||||
/** One tsconfig/jsconfig `compilerOptions.paths` entry: `@/*` → [`./*`], already joined to its base. */
|
||||
export interface PathAlias { prefix: string; suffix: string; wildcard: boolean; targets: string[] }
|
||||
/** The effective `paths` of the root config, its `baseUrl` (repo-relative, or null) and what could not be read. */
|
||||
/** The effective `paths` of one config, its `baseUrl` (repo-relative, or null) and what could not be read. */
|
||||
export interface PathAliases { aliases: PathAlias[]; baseUrl: string | null; notes: string[] }
|
||||
|
||||
/** How many relative `extends` hops are followed from the root tsconfig/jsconfig. */
|
||||
/** How many relative `extends` hops are followed from a tsconfig/jsconfig. */
|
||||
export const MAX_TSCONFIG_EXTENDS = 4;
|
||||
|
||||
/** JSONC → JSON: drop comments, and a comma whose next token closes an object or array, outside strings. */
|
||||
@@ -213,7 +213,9 @@ interface ConfigOptions { baseUrl?: Located<string>; paths?: Located<Record<stri
|
||||
/** `baseUrl` and `paths` of one config after its relative `extends` chain (later entries and the config itself override). */
|
||||
function effectiveOptions(repoRoot: string, rel: string, depth: number, seen: Set<string>, notes: string[]): ConfigOptions | null {
|
||||
let json: { extends?: unknown; compilerOptions?: { baseUrl?: unknown; paths?: unknown } };
|
||||
try { json = JSON.parse(stripJsonc(fs.readFileSync(path.join(repoRoot, rel), 'utf8'))) ?? {}; } catch { return null; }
|
||||
const text = readRepoFile(repoRoot, rel);
|
||||
if (text === null) return null;
|
||||
try { json = JSON.parse(stripJsonc(text)) ?? {}; } catch { return null; }
|
||||
const dir = path.posix.dirname(rel);
|
||||
let options: ConfigOptions = {};
|
||||
const parents = typeof json.extends === 'string' ? [json.extends] : Array.isArray(json.extends) ? json.extends.filter((e): e is string => typeof e === 'string') : [];
|
||||
@@ -234,16 +236,18 @@ function effectiveOptions(repoRoot: string, rel: string, depth: number, seen: Se
|
||||
}
|
||||
|
||||
/**
|
||||
* The root tsconfig.json (else jsconfig.json) `paths`, relative to the repo,
|
||||
* following relative `extends` up to MAX_TSCONFIG_EXTENDS hops. Targets join
|
||||
* The tsconfig.json (else jsconfig.json) `paths` in repo directory `dir` (the
|
||||
* root by default), relative to the repo, following relative `extends` inside
|
||||
* the repo up to MAX_TSCONFIG_EXTENDS hops. Targets join
|
||||
* `baseUrl` when one is set, else the directory of the config declaring
|
||||
* `paths`. What is not followed is recorded in `notes` for the no-lane
|
||||
* explanation; a missing alias leaves no edge (so `no lane`), never a wrong one.
|
||||
*/
|
||||
export function readPathAliases(repoRoot: string): PathAliases {
|
||||
export function readPathAliases(repoRoot: string, dir = '.'): PathAliases {
|
||||
for (const name of ['tsconfig.json', 'jsconfig.json']) {
|
||||
const notes: string[] = [];
|
||||
const options = effectiveOptions(repoRoot, name, 0, new Set([name]), notes);
|
||||
const rel = normalize(path.posix.join(dir, name));
|
||||
const options = effectiveOptions(repoRoot, rel, 0, new Set([rel]), notes);
|
||||
if (!options) continue;
|
||||
const baseUrl = options.baseUrl ? normalize(path.posix.join(options.baseUrl.dir, options.baseUrl.value)) : null;
|
||||
const base = baseUrl ?? options.paths?.dir ?? '.';
|
||||
@@ -261,6 +265,40 @@ export function readPathAliases(repoRoot: string): PathAliases {
|
||||
|
||||
const NO_ALIASES: PathAliases = { aliases: [], baseUrl: null, notes: [] };
|
||||
|
||||
/**
|
||||
* The aliases governing each file: those of the nearest nested tsconfig.json
|
||||
* or jsconfig.json above it (its own directory first) that declares `paths`
|
||||
* or `baseUrl`, itself or through `extends`, else the root config's. A nested
|
||||
* config that declares neither defers upward, so a file never loses an alias
|
||||
* the root gives it. With no nested config every file gets the root's.
|
||||
*/
|
||||
function pathAliasResolver(repoRoot: string, files: readonly string[]): { aliasesFor: (rel: string) => PathAliases; notes: string[] } {
|
||||
const root = readPathAliases(repoRoot);
|
||||
const notes = [...root.notes];
|
||||
const read = new Set<string>();
|
||||
const declaring = new Map<string, PathAliases>();
|
||||
for (const f of files) {
|
||||
const dir = path.posix.dirname(f);
|
||||
if (dir === '.' || read.has(dir) || !/^[tj]sconfig\.json$/.test(path.posix.basename(f))) continue;
|
||||
read.add(dir);
|
||||
const config = readPathAliases(repoRoot, dir);
|
||||
for (const n of config.notes) if (!notes.includes(n)) notes.push(n);
|
||||
if (config.aliases.length || config.baseUrl !== null) declaring.set(dir, config);
|
||||
}
|
||||
const byDir = new Map<string, PathAliases>();
|
||||
const aliasesFor = (rel: string): PathAliases => {
|
||||
const start = path.posix.dirname(rel);
|
||||
let hit = byDir.get(start);
|
||||
if (hit) return hit;
|
||||
let d = start;
|
||||
while (d !== '.' && !declaring.has(d)) d = path.posix.dirname(d);
|
||||
hit = declaring.get(d) ?? root;
|
||||
byDir.set(start, hit);
|
||||
return hit;
|
||||
};
|
||||
return { aliasesFor, notes };
|
||||
}
|
||||
|
||||
/**
|
||||
* TypeScript's pattern choice: an exact pattern, else the matching wildcard
|
||||
* with the longest prefix. `chosen` is that pattern's expanded targets in
|
||||
@@ -483,8 +521,8 @@ export function buildGraph(repoRoot: string, opts: BuildOptions = {}): Graph {
|
||||
const files = new Set([...all, ...gone]);
|
||||
const dirs = new Set<string>();
|
||||
for (const f of files) { let d = path.posix.dirname(f); while (d && d !== '.') { dirs.add(d); d = path.posix.dirname(d); } }
|
||||
const config = readPathAliases(repoRoot);
|
||||
const graph: Graph = { files, tests: new Set(all.filter(isTestFile)), reverse: new Map(), unresolved: new Map(), aliasNotes: config.notes };
|
||||
const aliases = pathAliasResolver(repoRoot, all);
|
||||
const graph: Graph = { files, tests: new Set(all.filter(isTestFile)), reverse: new Map(), unresolved: new Map(), aliasNotes: aliases.notes };
|
||||
const maxBytes = opts.maxBytes ?? 2 * 1024 * 1024;
|
||||
const packages = dartPackages(repoRoot, all);
|
||||
const pySources = new Map<string, string>();
|
||||
@@ -494,7 +532,7 @@ export function buildGraph(repoRoot: string, opts: BuildOptions = {}): Graph {
|
||||
try { const st = fs.statSync(path.join(repoRoot, rel)); if (!st.isFile() || st.size > maxBytes) continue; source = fs.readFileSync(path.join(repoRoot, rel), 'utf8'); } catch { continue; }
|
||||
if (rel.endsWith('.py')) pySources.set(rel, source);
|
||||
const unresolved: string[] = [];
|
||||
for (const { target, via } of referencesIn(files, dirs, rel, source, config, graph.tests.has(rel) ? unresolved : undefined, packages)) addEdge(graph, target, rel, via);
|
||||
for (const { target, via } of referencesIn(files, dirs, rel, source, aliases.aliasesFor(rel), graph.tests.has(rel) ? unresolved : undefined, packages)) addEdge(graph, target, rel, via);
|
||||
if (unresolved.length) graph.unresolved!.set(rel, unresolved);
|
||||
}
|
||||
const rust = rustEdges(all, rel => readRepoFile(repoRoot, rel, maxBytes));
|
||||
@@ -507,7 +545,7 @@ export function buildGraph(repoRoot: string, opts: BuildOptions = {}): Graph {
|
||||
const present = new Set(all);
|
||||
graph.goneSubjects = new Map();
|
||||
for (const [rel, source] of opts.goneSources) {
|
||||
const targets = [...referencesIn(files, dirs, rel, source, config, undefined, packages).map(r => r.target), ...conventionBins(rel, files)];
|
||||
const targets = [...referencesIn(files, dirs, rel, source, aliases.aliasesFor(rel), undefined, packages).map(r => r.target), ...conventionBins(rel, files)];
|
||||
graph.goneSubjects.set(rel, [...new Set(targets)].filter(t => present.has(t) && !isTestFile(t) && !TEST_TREE.test(t)).sort());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,8 @@
|
||||
* — `@/…` was resolved as a repo path, so it never matched a file.
|
||||
*/
|
||||
import { afterEach, describe, expect, test } from 'bun:test';
|
||||
import * as fs from 'node:fs';
|
||||
import * as path from 'node:path';
|
||||
import { MAX_TSCONFIG_EXTENDS, buildGraph, testsFor } from '../lib/pregate/import-graph';
|
||||
import { cleanup, commit, makeFixtureRepo, type FixtureRepo } from './helpers/pregate-fixture';
|
||||
|
||||
@@ -122,6 +124,88 @@ describe('import graph: route-group / dynamic-segment paths and tsconfig path al
|
||||
});
|
||||
});
|
||||
|
||||
describe('import graph: a nested tsconfig governs the files under it', () => {
|
||||
// Value: protects=an `@/` import inside a monorepo app resolves through that app's own tsconfig `paths`, so its source maps to
|
||||
// its tests; fails_when=only the root config is read again (every touched app file PREGATE_NO_LANE) or a nested extends leaves
|
||||
// the repo; why_new=only root configs were tested (gstack#4462); seam=none; no_claim=TS project references, symlinked configs
|
||||
test('an alias in a nested tsconfig maps a source file under it to the test that imports it', () => {
|
||||
const repo = fixture();
|
||||
commit(repo, {
|
||||
'apps/mobile/tsconfig.json': '{ "extends": "expo/tsconfig.base", "compilerOptions": { "strict": true, "paths": { "@/*": ["./src/*"] } } }\n',
|
||||
'apps/mobile/src/media/close-bar.tsx': 'export const CloseBar = () => null;\n',
|
||||
'apps/mobile/src/media/close-bar.test.tsx': "import { CloseBar } from '@/media/close-bar';\nCloseBar;\n",
|
||||
});
|
||||
const g = buildGraph(repo.work);
|
||||
expect(testsFor(g, 'apps/mobile/src/media/close-bar.tsx').tests).toEqual(['apps/mobile/src/media/close-bar.test.tsx']);
|
||||
expect(g.aliasNotes).toContain('apps/mobile/tsconfig.json extends expo/tsconfig.base: not a relative path, not followed');
|
||||
});
|
||||
|
||||
test('a nested config without paths or baseUrl defers to the root; a sibling app keeps its own alias', () => {
|
||||
const repo = fixture();
|
||||
commit(repo, {
|
||||
'tsconfig.json': '{ "compilerOptions": { "paths": { "@/*": ["src/*"] } } }\n',
|
||||
'src/x.ts': 'export const x = "root";\n',
|
||||
'test/x.test.ts': "import { x } from '@/x';\nx;\n",
|
||||
'packages/core/tsconfig.json': '{ "compilerOptions": { "strict": true } }\n',
|
||||
'packages/core/test/x.test.ts': "import { x } from '@/x';\nx;\n",
|
||||
'apps/web/tsconfig.json': '{ "compilerOptions": { "baseUrl": ".", "paths": { "@/*": ["lib/*"] } } }\n',
|
||||
'apps/web/lib/x.ts': 'export const x = "web";\n',
|
||||
'apps/web/test/x.test.ts': "import { x } from '@/x';\nx;\n",
|
||||
});
|
||||
const g = buildGraph(repo.work);
|
||||
expect(testsFor(g, 'src/x.ts').tests).toEqual(['packages/core/test/x.test.ts', 'test/x.test.ts']);
|
||||
expect(testsFor(g, 'apps/web/lib/x.ts').tests).toEqual(['apps/web/test/x.test.ts']);
|
||||
});
|
||||
|
||||
test('a nested config takes paths through a relative extends, joined relative to the config that declares them', () => {
|
||||
const repo = fixture();
|
||||
commit(repo, {
|
||||
'tsconfig.base.json': '{ "compilerOptions": { "paths": { "#core/*": ["packages/core/src/*"] } } }\n',
|
||||
'apps/api/tsconfig.json': '{ "extends": "../../tsconfig.base.json" }\n',
|
||||
'packages/core/src/plan.ts': 'export const plan = 1;\n',
|
||||
'apps/api/test/plan.test.ts': "import { plan } from '#core/plan';\nplan;\n",
|
||||
});
|
||||
expect(testsFor(buildGraph(repo.work), 'packages/core/src/plan.ts').tests).toEqual(['apps/api/test/plan.test.ts']);
|
||||
});
|
||||
|
||||
test('a repo whose only config is at the root resolves every file through the root config, as before', () => {
|
||||
const repo = fixture();
|
||||
commit(repo, {
|
||||
'tsconfig.json': '{ "extends": "@tsconfig/node22", "compilerOptions": { "baseUrl": ".", "paths": { "~lib/*": ["lib/*"] } } }\n',
|
||||
'lib/plan.ts': 'export const plan = 1;\n',
|
||||
'apps/api/test/plan.test.ts': "import { plan } from '~lib/plan';\nplan;\n",
|
||||
});
|
||||
const g = buildGraph(repo.work);
|
||||
expect(g.aliasNotes).toEqual(['tsconfig.json extends @tsconfig/node22: not a relative path, not followed']);
|
||||
expect(testsFor(g, 'lib/plan.ts').tests).toEqual(['apps/api/test/plan.test.ts']);
|
||||
});
|
||||
|
||||
test('a nested extends that climbs out of the repository is not followed, even when the file exists there', () => {
|
||||
const repo = fixture();
|
||||
fs.writeFileSync(path.join(repo.root, 'outside.json'), '{ "compilerOptions": { "paths": { "#core/*": ["packages/core/src/*"] } } }\n');
|
||||
commit(repo, {
|
||||
'apps/api/tsconfig.json': '{ "extends": "../../../outside.json" }\n',
|
||||
'packages/core/src/plan.ts': 'export const plan = 1;\n',
|
||||
'apps/api/test/plan.test.ts': "import { plan } from '#core/plan';\nplan;\n",
|
||||
});
|
||||
const g = buildGraph(repo.work);
|
||||
expect(testsFor(g, 'packages/core/src/plan.ts').tests).toEqual([]);
|
||||
expect(g.aliasNotes).toEqual(['apps/api/tsconfig.json extends ../../../outside.json: outside the repo or a cycle, not followed']);
|
||||
});
|
||||
|
||||
test.skipIf(process.platform === 'win32')('a nested tsconfig that is a symlink to a file outside the repository is not read', () => {
|
||||
const repo = fixture();
|
||||
fs.writeFileSync(path.join(repo.root, 'outside.json'), '{ "compilerOptions": { "paths": { "#core/*": ["../../packages/core/src/*"] } } }\n');
|
||||
fs.mkdirSync(path.join(repo.work, 'apps', 'api'), { recursive: true });
|
||||
fs.symlinkSync(path.join(repo.root, 'outside.json'), path.join(repo.work, 'apps', 'api', 'tsconfig.json'));
|
||||
commit(repo, {
|
||||
'packages/core/src/plan.ts': 'export const plan = 1;\n',
|
||||
'apps/api/test/plan.test.ts': "import { plan } from '#core/plan';\nplan;\n",
|
||||
});
|
||||
expect(testsFor(buildGraph(repo.work), 'packages/core/src/plan.ts').tests).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('import graph: alias precedence, extends and the no-lane explanation', () => {
|
||||
const twoAliases = (specificFirst: boolean) => {
|
||||
const specific = '"@/lib/*": ["src/lib/*"]';
|
||||
|
||||
Reference in new issue
Block a user