diff --git a/src/planner/pipeline-steps.ts b/src/planner/pipeline-steps.ts index cf297a8e..6bc1d71d 100644 --- a/src/planner/pipeline-steps.ts +++ b/src/planner/pipeline-steps.ts @@ -100,10 +100,21 @@ function splitParts(body: string): string[] | undefined { parts.push(current.trim()); const unquoted = parts.map((part) => part.replace(/"[^"]*"|'[^']*'/g, "\"\"")); // A double-quoted `$` or backtick still expands in the shell. - if (parts.some((part) => !part || /"[^"]*[$`][^"]*"/.test(part)) || unquoted.some((part) => UNSPLITTABLE.test(part))) return undefined; + if (parts.some((part) => !part || expandsBetweenDoubleQuotes(part)) || unquoted.some((part) => UNSPLITTABLE.test(part))) return undefined; return parts; } +/** + * True when a `$` or backtick sits between two double quotes, the answer `/"[^"]*[$`][^"]*"/` gives. + * That regex is quadratic on a `"` followed by a long run of backticks with no closing quote (#300); + * this is one linear pass: the text strictly between two consecutive `"` holds a `$` or backtick. + */ +export function expandsBetweenDoubleQuotes(part: string): boolean { + const segments = part.split("\""); + for (let i = 1; i < segments.length - 1; i++) if (/[$`]/.test(segments[i]!)) return true; + return false; +} + function globToRegExp(pattern: string): RegExp { return new RegExp(`^${pattern.replace(/[.+^${}()|[\]\\]/g, "\\$&").replace(/\*\*/g, ".*").replace(/\*/g, "[^:]*")}$`); } diff --git a/tests/fuzz/repository-text.fuzz.test.ts b/tests/fuzz/repository-text.fuzz.test.ts index 7346134e..769cfa2f 100644 --- a/tests/fuzz/repository-text.fuzz.test.ts +++ b/tests/fuzz/repository-text.fuzz.test.ts @@ -11,7 +11,7 @@ import fc from "fast-check"; import { evaluateCondition, renderCommand, resolveExpression } from "../../src/ci-inference/expression.js"; import { firstCommandTokens } from "../../src/ci-inference/purpose.js"; import { expressionReferences } from "../../src/ci-inference/resolve.js"; -import { discoverGateChecks } from "../../src/planner/pipeline-steps.js"; +import { discoverGateChecks, expandsBetweenDoubleQuotes } from "../../src/planner/pipeline-steps.js"; import { nodeTestGlobForPath } from "../../src/planner/test-command.js"; import { contentNamesPath, referencePattern, referenceTokens, widenCharacterClasses } from "../../src/repo/file-references.js"; import { workflowMayRunTests } from "../../src/repo/workflow-test-steps.js"; @@ -113,6 +113,12 @@ describe("fuzz: linear rewrites agree with the regexes they replaced", () => { }), runs(1000)); }); + it("double-quoted expansion in a script part (#300)", () => { + fc.assert(fc.property(pieces("\"", "$", "`", "a", " ", "'"), (part) => { + assert.equal(expandsBetweenDoubleQuotes(part), /"[^"]*[$`][^"]*"/.test(part)); + }), runs(1000)); + }); + it("first command of a step", () => { fc.assert(fc.property(pieces(" ", "\t", "|", "&", ";", "a", "b"), (line) => { const [first = ""] = line.trim().split(/\s*(?:\|\||&&|\||;)\s*/); diff --git a/tests/planner/pipeline-steps.test.ts b/tests/planner/pipeline-steps.test.ts index 97876b55..2ffc60e0 100644 --- a/tests/planner/pipeline-steps.test.ts +++ b/tests/planner/pipeline-steps.test.ts @@ -41,6 +41,13 @@ describe("discoverGateChecks", () => { } }); + it("reads a quote followed by a long run of backticks in linear time (#300)", () => { + const body = `tsc && ""${"`".repeat(100_000)}`; + const started = performance.now(); + assert.deepEqual(discoverGateChecks({ check: body }).checks.map((c) => [c.id, c.runsScript]), [["check", true]]); + assert.ok(performance.now() - started < 1000, `took ${Math.round(performance.now() - started)} ms`); + }); + it("leaves test runs to test selection but keeps mixed bodies", () => { const gate = discoverGateChecks({ check: "npm run lint && npm test", lint: "eslint .", test: "vitest run" }); assert.deepEqual(gate.checks.map((c) => c.id), ["lint"]);