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
+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());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user