Skip to content

Commit c623f4a

Browse files
committed
fix(lint): reject invalid shard selectors
Punchcard-Session: clear-harbor-river-2k
1 parent e2712c1 commit c623f4a

2 files changed

Lines changed: 74 additions & 21 deletions

File tree

‎scripts/run-oxlint-shards.mjs‎

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -250,15 +250,15 @@ export async function main(extraArgs = process.argv.slice(2), runtimeEnv = proce
250250
})
251251
: () => {};
252252

253-
const shards = createOxlintShards({
254-
cwd: process.cwd(),
255-
env,
256-
platform: process.platform,
257-
splitCore: shardArgs.splitCore,
258-
});
259-
const selectedShards = filterOxlintShards(shards, shardArgs.only);
260-
261253
try {
254+
const shards = createOxlintShards({
255+
cwd: process.cwd(),
256+
env,
257+
platform: process.platform,
258+
splitCore: shardArgs.splitCore,
259+
});
260+
const selectedShards = filterOxlintShards(shards, shardArgs.only);
261+
262262
ensureRepoToolNodeModulesLink(resolveRepoToolBinPath("oxlint"));
263263
const prepareResult = spawnSync(
264264
process.execPath,
@@ -331,20 +331,12 @@ export function parseShardRunnerArgs(args) {
331331
continue;
332332
}
333333
if (arg === "--only") {
334-
const value = args[index + 1];
335-
if (!value || value.startsWith("-")) {
336-
throw new Error("--only requires a shard name");
337-
}
338-
only.add(value);
334+
only.add(requireShardSelector(args[index + 1]));
339335
index += 1;
340336
continue;
341337
}
342338
if (arg.startsWith("--only=")) {
343-
const value = arg.slice("--only=".length);
344-
if (!value) {
345-
throw new Error("--only requires a shard name");
346-
}
347-
only.add(value);
339+
only.add(requireShardSelector(arg.slice("--only=".length)));
348340
continue;
349341
}
350342
oxlintArgs.push(arg);
@@ -354,14 +346,37 @@ export function parseShardRunnerArgs(args) {
354346
}
355347

356348
/**
357-
* Filters shards by an optional comma-separated shard name list.
349+
* Filters shards by optional shard names and rejects unknown selectors.
358350
*/
359351
export function filterOxlintShards(shards, only) {
360352
if (only.size === 0) {
361353
return shards;
362354
}
363355

364-
return shards.filter((shard) => only.has(shard.name) || only.has(shard.name.split(":")[0]));
356+
const selectors = [...only];
357+
const unknownSelectors = selectors.filter(
358+
(selector) => !shards.some((shard) => matchesShardSelector(shard, selector)),
359+
);
360+
if (unknownSelectors.length > 0) {
361+
throw new Error(
362+
`Unknown oxlint shard selector${unknownSelectors.length === 1 ? "" : "s"}: ${unknownSelectors.join(", ")}`,
363+
);
364+
}
365+
366+
return shards.filter((shard) =>
367+
selectors.some((selector) => matchesShardSelector(shard, selector)),
368+
);
369+
}
370+
371+
function requireShardSelector(value) {
372+
if (!value || value.startsWith("-")) {
373+
throw new Error("--only requires a shard name");
374+
}
375+
return value;
376+
}
377+
378+
function matchesShardSelector(shard, selector) {
379+
return selector === shard.name || selector === shard.name.split(":")[0];
365380
}
366381

367382
/**

‎test/scripts/run-oxlint.test.ts‎

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ describe("run-oxlint", () => {
495495
expect(parsed.oxlintArgs).toEqual(["--max-warnings", "0"]);
496496
});
497497

498-
it.each([["--only"], ["--only", "--split-core"], ["--only="]])(
498+
it.each([["--only"], ["--only", "--split-core"], ["--only="], ["--only=-h"]])(
499499
"rejects shard selectors without a name: %s",
500500
(...args) => {
501501
expect(() => parseShardRunnerArgs(args)).toThrow("--only requires a shard name");
@@ -519,6 +519,44 @@ describe("run-oxlint", () => {
519519
]);
520520
});
521521

522+
it.each([
523+
{ selectors: ["wat"], message: "Unknown oxlint shard selector: wat" },
524+
{
525+
selectors: ["core", "wat"],
526+
message: "Unknown oxlint shard selector: wat",
527+
},
528+
])("rejects unmatched shard selectors: $selectors", ({ selectors, message }) => {
529+
expect(() =>
530+
filterOxlintShards(createOxlintShards({ cwd: "/repo" }), new Set(selectors)),
531+
).toThrow(message);
532+
});
533+
534+
it.each([
535+
["--only"],
536+
["--only", "--split-core"],
537+
["--only="],
538+
["--only=-h"],
539+
["--only=wat"],
540+
["--only=core", "--only=wat"],
541+
])("rejects invalid shard CLI input before starting work: %s", (...args) => {
542+
const tempDir = createTempDir("openclaw-oxlint-selector-");
543+
const result = spawnSync(process.execPath, [RUN_OXLINT_SHARDS_URL, ...args], {
544+
cwd: tempDir,
545+
encoding: "utf8",
546+
env: {
547+
...process.env,
548+
OPENCLAW_HEAVY_CHECK_LOCK_SCOPE: "worktree",
549+
OPENCLAW_LOCAL_CHECK: "1",
550+
},
551+
});
552+
553+
expect(result.status).toBe(1);
554+
expect(result.stderr).not.toContain("[oxlint:");
555+
expect(existsSync(join(tempDir, ".artifacts/openclaw-local-checks/heavy-check.lock"))).toBe(
556+
false,
557+
);
558+
});
559+
522560
it("falls back to the full extension shard when Windows extension dirs are unavailable", () => {
523561
const shards = createWindowsExtensionShards({
524562
cwd: "/repo",

0 commit comments

Comments
 (0)