Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 52 additions & 11 deletions src/planner/python-checks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
* a recipe that does not rewrite files (`--fix`, `ruff format` or `black`/`isort` without a check flag);
* - a direct `mypy`, `pyright`, `basedpyright`, `pyrefly check`, `ruff check` or `ruff format --check`
* call, optionally behind `uv run`, `poetry run`, `pdm run`, `hatch run`, `pipenv run` or `python -m`,
* whose every argument is a plain word and none rewrites files or writes output.
* whose every argument is a plain word and none rewrites files or writes output. Before `uv run`,
* --directory and --project take repository-relative paths; --offline and --no-progress take no value.
*
* FAILING CLOSED. Here that means adding nothing: anything not read with certainty is left out, which
* is the behaviour before this module. Steps in disabled jobs, steps with expressions in their working
Expand All @@ -40,6 +41,8 @@ export interface PythonCheck extends GateCheck {
pythonOnly: boolean;
/** Repository paths besides Python files and configuration whose change runs the check. */
triggers: string[];
/** Additional configuration filenames that trigger the check in any project directory. */
configNames?: string[];
}

export interface WorkflowFile {
Expand Down Expand Up @@ -89,11 +92,34 @@ function rewrites(line: string): boolean {
return false;
}

/** Strip only recognized pre-run globals for matching. Keep the original command for execution so
* uv applies --directory and --project itself, in the order specified by the workflow. */
function withoutUvGlobals(command: string): string | undefined {
if (!command.startsWith("uv --")) return command;
const words = command.split(" ");
const seen = new Set<string>();
let i = 1;
while (i < words.length) {
const word = words[i++]!;
if (word === "run") return ["uv", "run", ...words.slice(i)].join(" ");
const equals = word.indexOf("=");
const option = equals < 0 ? word : word.slice(0, equals);
if (seen.has(option)) return undefined;
seen.add(option);
if (word === "--offline" || word === "--no-progress") continue;
if (option !== "--directory" && option !== "--project") return undefined;
const path = equals < 0 ? words[i++] : word.slice(equals + 1);
if (!path || !/^[\w.][\w./-]*$/.test(path) || normalizeDir(path) === undefined) return undefined;
}
return undefined;
}

/** A direct Python tool call, normalized, with its kind; undefined when the line is anything else. */
export function directToolCall(line: string): { command: string; kind: GateCheck["kind"] } | undefined {
const command = line.trim().replace(/\s+/g, " ");
const prefix = RUNNER_PREFIX.exec(command)![0];
const rest = command.slice(prefix.length);
const invocation = withoutUvGlobals(command);
if (!invocation) return undefined;
const rest = invocation.replace(RUNNER_PREFIX, "");
const tool = TOOL.exec(rest);
if (!tool) return undefined;
const args = rest.slice(tool[0].length).trim().split(" ").filter(Boolean);
Expand Down Expand Up @@ -131,17 +157,27 @@ function makeTarget(dir: string, target: string, read: (path: string) => string

/** True when every recipe line is an echo, a Python tool call, or a repository script that calls a Python
* tool and no JavaScript tool; also returns those scripts. Undefined when any line is something else. */
function pythonOnlyRecipe(target: MakeTarget, dir: string, read: (path: string) => string | undefined): string[] | undefined {
function pythonOnlyRecipe(target: MakeTarget, dir: string, read: (path: string) => string | undefined): { scripts: string[]; uvGlobals: boolean } | undefined {
if (target.prerequisites.includes(";")) return undefined;
const scripts: string[] = [];
let usesPython = false;
let uvGlobals = false;
for (const raw of target.recipe) {
const line = raw.replace(/^[@+-]+/, "").trim();
if (/\$\(MAKE\)|\$\{MAKE\}|(?:^|\s)make\s/.test(line)) return undefined;
if (/^(?:echo\b|printf\b|true$|:$)/.test(line)) continue;
if (JS_TOOL.test(line)) return undefined;
const script = /^(?:(?:bash|sh)\s+)?(\.?\/?[\w./-]+)(?:\s|$)/.exec(line)?.[1];
if (directToolCall(line.replace(/\s+\$\([\w]+\)/g, ""))) { usesPython = true; continue; }
const call = directToolCall(line.replace(/\s+\$\([\w]+\)/g, ""));
if (call) {
if (call.command.startsWith("uv --")) {
// A Make expansion could supply an additional uv option or the command after run.
if (line.includes("$(")) return undefined;
uvGlobals = true;
}
usesPython = true;
continue;
}
if (script && script.includes("/")) {
const path = posix.normalize(posix.join(dir, script.replace(/^\.\//, "")));
const text = path.startsWith("..") ? undefined : read(path);
Expand All @@ -152,7 +188,7 @@ function pythonOnlyRecipe(target: MakeTarget, dir: string, read: (path: string)
}
return undefined;
}
return usesPython ? scripts : undefined;
return usesPython ? { scripts, uvGlobals } : undefined;
}

function makeKind(target: string, recipe: readonly string[]): GateCheck["kind"] {
Expand Down Expand Up @@ -207,7 +243,7 @@ function makeCheck(line: string, stepDir: string, workflowPath: string, read: (p
if (dir === undefined) return undefined;
const found = makeTarget(dir, target!, read);
if (!found || found.recipe.some(rewrites)) return undefined;
const scripts = found.prerequisites ? undefined : pythonOnlyRecipe(found, dir, read);
const recipe = found.prerequisites ? undefined : pythonOnlyRecipe(found, dir, read);
const command = dir ? `make -C ${dir} ${target}` : `make ${target}`;
return {
id: command,
Expand All @@ -217,15 +253,20 @@ function makeCheck(line: string, stepDir: string, workflowPath: string, read: (p
kind: makeKind(target!, found.recipe),
decision: "RUN",
reason: `run by CI workflow ${workflowPath}`,
pythonOnly: scripts !== undefined,
triggers: [found.path, ...(scripts ?? [])],
pythonOnly: recipe !== undefined,
triggers: [found.path, ...(recipe?.scripts ?? [])],
...(recipe?.uvGlobals && { configNames: ["uv.toml"] }),
};
}

function toolCheck(line: string, workflowPath: string): PythonCheck | undefined {
const call = directToolCall(line);
if (!call) return undefined;
return { id: call.command, script: workflowPath, runsScript: false, command: call.command, kind: call.kind, decision: "RUN", reason: `run by CI workflow ${workflowPath}`, pythonOnly: true, triggers: [] };
return {
id: call.command, script: workflowPath, runsScript: false, command: call.command, kind: call.kind,
decision: "RUN", reason: `run by CI workflow ${workflowPath}`, pythonOnly: true, triggers: [],
...(call.command.startsWith("uv --") && { configNames: ["uv.toml"] }),
};
}

function pythonRelevant(path: string): boolean {
Expand All @@ -236,7 +277,7 @@ function pythonRelevant(path: string): boolean {
export function narrowPythonChecks(checks: readonly PythonCheck[], changes: readonly ChangedPath[]): PythonCheck[] {
return checks.map((check) => {
if (!check.pythonOnly) return { ...check, reason: `${check.reason}; not read as Python-only, runs as written` };
const hit = changes.find((change) => pythonRelevant(change.path) || check.triggers.includes(change.path));
const hit = changes.find((change) => pythonRelevant(change.path) || check.triggers.includes(change.path) || check.configNames?.includes(posix.basename(change.path)));
return hit
? { ...check, reason: `${check.reason}; ${hit.path} changed` }
: { ...check, decision: "SKIP", reason: `${check.reason}; no changed Python file or Python configuration` };
Expand Down
33 changes: 33 additions & 0 deletions tests/planner/python-checks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,39 @@ describe("discoverPythonChecks", () => {
]);
});

it("reads the uv directory option in Dify's agent check, directly and in its Make recipe", () => {
const command = "uv --directory dify-agent run --project . pyrefly check";
const text = `jobs:\n agent:\n steps:\n - run: ${command}\n - run: make check\n`;
const checks = discoverPythonChecks(workflow(text), reader({ Makefile: `check:\n\t${command}\n` }));
assert.deepEqual(checks.map((c) => [c.command, c.kind, c.pythonOnly]), [
[command, "typecheck", true], ["make check", "typecheck", true],
]);
for (const [path, decision] of [["dify-agent/main.py", "RUN"], ["dify-agent/uv.toml", "RUN"], ["web/main.ts", "SKIP"]] as const) {
assert.deepEqual(narrowPythonChecks(checks, [{ path, status: "modified" }]).map((c) => c.decision), [decision, decision]);
}
const nested = text.replace("- run:", "- working-directory: api\n run:");
assert.deepEqual(discoverPythonChecks(workflow(nested), reader({})), []);
});

it("keeps a Make check unconditional when a Make variable supplies part of the uv invocation", () => {
const files = { Makefile: "check:\n\tuv --directory api run $(CHECK) pyrefly check\n" };
const checks = discoverPythonChecks(workflow("jobs:\n a:\n steps:\n - run: make check\n"), reader(files));
assert.equal(checks[0]?.pythonOnly, false);
assert.equal(narrowPythonChecks(checks, [{ path: "web/main.ts", status: "modified" }])[0]?.decision, "RUN");
});

it("reads only supported uv globals with their operands before run", () => {
const command = "uv --offline --project=./api --no-progress run --frozen ruff check .";
assert.deepEqual(directToolCall(command), { command, kind: "lint" });
for (const line of [
"uv --directory run pyrefly check", "uv --project= run mypy", "uv --directory --offline run mypy",
"uv --directory ../api run mypy", "uv --directory /api run mypy", "uv --unknown run mypy",
"uv --offline --offline run mypy", "uv --project=a --project=b run mypy",
"uv --group dev run mypy", "uv --offline sync", "uv --offline run unknown-check .",
"uv --offline run ruff format .", "uv --offline run ruff check --fix .",
]) assert.equal(directToolCall(line), undefined, line);
});

it("leaves out calls that rewrite files, write output or need the shell", () => {
for (const line of ["ruff check --fix .", "ruff format .", "mypy --install-types src", "mypy src --junit-xml out.xml", "mypy src | tee out", "mypy $SRC", "mypy \"src\""]) {
assert.equal(directToolCall(line), undefined, line);
Expand Down
Loading