Skip to content

Commit 61488f7

Browse files
fix(scripts): keep --limit bound to its value in depgraph affected
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
1 parent ad048b2 commit 61488f7

2 files changed

Lines changed: 51 additions & 10 deletions

File tree

‎scripts/depgraph/affected-run.ts‎

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ import {
2424
type InteractionPathId,
2525
} from '../../src/contracts/interaction-guarantees.ts';
2626
import { DAEMON_COMMAND_DESCRIPTORS } from '../../src/daemon/daemon-command-registry.ts';
27+
import { parseArgs } from 'node:util';
2728
import { selectChecks, type CheckPlan } from '../check-affected/model.ts';
28-
import { parseScriptArgs } from '../lib/cli-args.ts';
2929
import {
3030
bound,
3131
collectDependents,
@@ -80,18 +80,33 @@ export type BlastRadius = {
8080
guaranteeRows: GuaranteeRow[];
8181
};
8282

83-
function parseInvocation(argv: readonly string[]): { file: string; json: boolean; limit: number } {
84-
const positionals = argv.filter((arg) => !arg.startsWith('-'));
85-
const flags = argv.filter((arg) => arg.startsWith('-'));
86-
const values = parseScriptArgs(flags, USAGE, {
87-
json: { type: 'boolean', default: false },
88-
limit: { type: 'string', default: '10' },
83+
export type Invocation = { file: string; json: boolean; limit: number } | { help: true };
84+
85+
/**
86+
* `parseArgs` with positionals enabled rather than the shared `parseScriptArgs` helper, which
87+
* rejects them: partitioning argv by a leading dash would detach `--limit` from its value.
88+
*/
89+
export function parseInvocation(argv: readonly string[]): Invocation {
90+
const { values, positionals } = parseArgs({
91+
args: [...argv],
92+
options: {
93+
json: { type: 'boolean', default: false },
94+
limit: { type: 'string', default: '10' },
95+
help: { type: 'boolean', short: 'h', default: false },
96+
},
97+
allowPositionals: true,
8998
});
99+
if (values.help) return { help: true };
100+
90101
const target = positionals[0];
91102
if (target === undefined) throw new Error(`missing <path>.\n${USAGE}`);
92-
if (positionals.length > 1) throw new Error(`expected one <path>, got ${positionals.length}.`);
103+
if (positionals.length > 1) {
104+
throw new Error(`expected one <path>, got ${positionals.length}: ${positionals.join(' ')}`);
105+
}
93106
const limit = Number(values.limit);
94-
if (!Number.isInteger(limit) || limit < 1) throw new Error(`--limit must be a positive integer`);
107+
if (!Number.isInteger(limit) || limit < 1) {
108+
throw new Error(`--limit must be a positive integer, got "${values.limit}"`);
109+
}
95110
return { file: target, json: Boolean(values.json), limit };
96111
}
97112

@@ -266,7 +281,12 @@ export function formatBlastRadius(radius: BlastRadius, limit: number): string {
266281
}
267282

268283
export async function runAffected(argv: readonly string[], repoRoot: string): Promise<number> {
269-
const { file, json, limit } = parseInvocation(argv);
284+
const invocation = parseInvocation(argv);
285+
if ('help' in invocation) {
286+
process.stdout.write(USAGE);
287+
return 0;
288+
}
289+
const { file, json, limit } = invocation;
270290
const radius = await computeBlastRadius(repoRoot, normalizeTarget(file, repoRoot));
271291
process.stdout.write(
272292
json ? `${JSON.stringify(radius, null, 2)}\n` : formatBlastRadius(radius, limit),

‎scripts/depgraph/affected.test.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
computeBlastRadius,
2020
formatBlastRadius,
2121
guaranteeRows,
22+
parseInvocation,
2223
parseRouteEntries,
2324
} from './affected-run.ts';
2425
import { collapseEdges, type GraphNode } from './model.ts';
@@ -166,6 +167,26 @@ test('the real guarantee matrix contributes module-qualified rows', () => {
166167
);
167168
});
168169

170+
test('a flag keeps its value whichever side of the path it is on', () => {
171+
const expected = { file: 'src/utils/exec.ts', json: true, limit: 25 };
172+
assert.deepEqual(parseInvocation(['src/utils/exec.ts', '--json', '--limit', '25']), expected);
173+
assert.deepEqual(parseInvocation(['--json', '--limit', '25', 'src/utils/exec.ts']), expected);
174+
assert.deepEqual(parseInvocation(['src/utils/exec.ts', '--json', '--limit=25']), expected);
175+
assert.deepEqual(parseInvocation(['src/utils/exec.ts']), {
176+
file: 'src/utils/exec.ts',
177+
json: false,
178+
limit: 10,
179+
});
180+
assert.deepEqual(parseInvocation(['--help']), { help: true });
181+
});
182+
183+
test('parseInvocation rejects a missing path, a second path, and a non-positive limit', () => {
184+
assert.throws(() => parseInvocation(['--json']), /missing <path>/);
185+
assert.throws(() => parseInvocation(['a.ts', 'b.ts']), /expected one <path>, got 2: a\.ts b\.ts/);
186+
assert.throws(() => parseInvocation(['a.ts', '--limit', '0']), /positive integer/);
187+
assert.throws(() => parseInvocation(['a.ts', '--limit', 'lots']), /positive integer/);
188+
});
189+
169190
test('text output bounds every list and says what it hid', () => {
170191
const text = formatBlastRadius(
171192
{

0 commit comments

Comments
 (0)