From 12d468aa2cac38191d4fcbfe68cac405812db9e7 Mon Sep 17 00:00:00 2001 From: Vlad Lukiantsev Date: Mon, 16 Nov 2026 10:45:43 +0000 Subject: [PATCH 1/5] chore(lint): enforce eslint on non-ui js scripts - Adds `tools/lint/eslint.nonui.config.mjs` and enables non-UI JS linting across `.github/scripts`, `tools/nightly_tests`, and `cmd/experiment_state_server/static` in CI and `tools/lint/precommit_runner.py`. - Uses recursive `:(glob).../**/*.js` pathspecs so top-level and nested non-UI JS files are covered, with unit tests in `tools/lint/precommit_runner_test.py`. TAG=agy CONV=ba8411ef-c093-4f45-950a-b3bc1899268c --- .github/scripts/git-range-diff.js | 16 ++- .github/scripts/git-range-diff.test.js | 8 -- .github/scripts/range-diff-parser.js | 1 + .github/workflows/lint.yml | 17 +++- tools/lint/eslint.nonui.config.mjs | 136 +++++++++++++++++++++++++ tools/lint/precommit_runner.py | 37 +++++++ tools/lint/precommit_runner_test.py | 41 ++++++++ 7 files changed, 242 insertions(+), 14 deletions(-) create mode 100644 tools/lint/eslint.nonui.config.mjs diff --git a/.github/scripts/git-range-diff.js b/.github/scripts/git-range-diff.js index d97769395..e03d30eaa 100644 --- a/.github/scripts/git-range-diff.js +++ b/.github/scripts/git-range-diff.js @@ -263,7 +263,9 @@ function computeBulkCommitPatchIds(shas, opts) { patchIdMap.set(sha, patchId); } } - } catch (_) {} + } catch (_) { + /* Best-effort cache population; caller tolerates partial map. */ + } return patchIdMap; } @@ -310,7 +312,9 @@ function resolveParentBaseFromMatch(matchedSha, commitIndex, opts) { for (const target of [`${matchedSha}~${baseOffset}`, `${matchedSha}~1`]) { try { return execGit(['rev-parse', '--verify', target], { ...opts, ignoreExitCode: false }).trim(); - } catch (_) {} + } catch (_) { + /* Probe next parent candidate when this offset does not exist. */ + } } return null; } @@ -369,7 +373,9 @@ function resolveStackedApprovedRange(currentRange, approvedRange, approvedSha, p const correlatedBase = correlateStackedParentBase(currCommits, approvedMaps, opts); if (correlatedBase) return `${correlatedBase}..${approvedSha}`; } - } catch (_) {} + } catch (_) { + /* Fall back to commit-count range resolution below. */ + } try { execGit(['rev-parse', '--verify', `${approvedSha}~${prCommitCount}`], { ...opts, ignoreExitCode: false }); @@ -550,7 +556,9 @@ function getPrChangedFiles(baseRefOrApprovedSha, approvedShaOrCurrentSha, curren files.add(file); } } - } catch (_) {} + } catch (_) { + /* Skip unreachable commit SHA gracefully when aggregating file paths. */ + } } return Array.from(files); } diff --git a/.github/scripts/git-range-diff.test.js b/.github/scripts/git-range-diff.test.js index bccd73cf1..248c4d4da 100644 --- a/.github/scripts/git-range-diff.test.js +++ b/.github/scripts/git-range-diff.test.js @@ -18,14 +18,6 @@ const childProcess = require('child_process'); const { validateGitRefs, buildGitEnv, - execGit, - ensureGitRefs, - isFastForwardApproval, - getCommitCountInRange, - getCommitPatchId, - indexApprovedCandidateCommits, - correlateStackedParentBase, - resolveStackedApprovedRange, resolveCommitRanges, runGitRangeDiff, parseNumstatDelta, diff --git a/.github/scripts/range-diff-parser.js b/.github/scripts/range-diff-parser.js index 9e05d1fac..e0887df76 100644 --- a/.github/scripts/range-diff-parser.js +++ b/.github/scripts/range-diff-parser.js @@ -126,6 +126,7 @@ function parseRangeDiff(output) { const addedCommitShas = []; for (const line of lines) { + // eslint-disable-next-line no-control-regex -- intentionally strips ANSI escape codes const cleanLine = line.replace(/\x1b\[[0-9;]*m/g, ''); const commitMatch = cleanLine.match(/^\s*(?:\d+|-):\s+([a-f0-9-]+)\s+([=!<>])\s+(?:\d+|-):\s+([a-f0-9-]+)/); if (commitMatch) { diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 9d1612106..b8821bdf4 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -72,6 +72,13 @@ jobs: - 'tools/common/tool_utils.py' - '.github/workflows/nightly-examples-e2e.yml' - '.github/workflows/lint.yml' + nonui_js: + - '.github/scripts/*.js' + - 'tools/nightly_tests/*.js' + - 'cmd/experiment_state_server/static/*.js' + - 'tools/lint/eslint.nonui.config.mjs' + - 'cmd/ui/package-lock.json' + - '.github/workflows/lint.yml' # 1. Python Linting (Ruff & Mypy) - name: 'Set up Python 3.13' @@ -242,7 +249,7 @@ jobs: # 3. UI Linting & Formatting (Prettier & ESLint) - name: 'Set up Node.js' - if: steps.changes.outputs.ui == 'true' + if: steps.changes.outputs.ui == 'true' || steps.changes.outputs.nonui_js == 'true' uses: 'actions/setup-node@1d0ff469b7ec7b3cb9d8673fde0c81c44821de2a' # v4.2.0 with: node-version: '22' @@ -250,7 +257,7 @@ jobs: cache-dependency-path: 'cmd/ui/package-lock.json' - name: 'Install UI Dependencies' - if: steps.changes.outputs.ui == 'true' + if: steps.changes.outputs.ui == 'true' || steps.changes.outputs.nonui_js == 'true' run: npm ci --prefer-offline --no-audit working-directory: 'cmd/ui' @@ -264,6 +271,12 @@ jobs: run: npm run lint working-directory: 'cmd/ui' + - name: 'Run ESLint on Non-UI JS Scripts' + if: steps.changes.outputs.nonui_js == 'true' + run: | + ./cmd/ui/node_modules/.bin/eslint --no-config-lookup -c tools/lint/eslint.nonui.config.mjs \ + .github/scripts/*.js tools/nightly_tests/*.js cmd/experiment_state_server/static/*.js + # 3a. GitHub Actions & Nightly JS Script Tests (node:test, no dependencies) - name: 'Set up Node.js for JS Script Tests' if: github.event_name == 'workflow_dispatch' || steps.changes.outputs.github_scripts == 'true' || steps.changes.outputs.examples_coverage == 'true' diff --git a/tools/lint/eslint.nonui.config.mjs b/tools/lint/eslint.nonui.config.mjs new file mode 100644 index 000000000..2be55b770 --- /dev/null +++ b/tools/lint/eslint.nonui.config.mjs @@ -0,0 +1,136 @@ +/** + * Copyright 2026 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +const BASE_RULES = { + 'constructor-super': 'error', + 'for-direction': 'error', + 'getter-return': 'error', + 'no-async-promise-executor': 'error', + 'no-case-declarations': 'error', + 'no-class-assign': 'error', + 'no-compare-neg-zero': 'error', + 'no-cond-assign': 'error', + 'no-const-assign': 'error', + 'no-constant-binary-expression': 'error', + 'no-constant-condition': 'error', + 'no-control-regex': 'error', + 'no-debugger': 'error', + 'no-delete-var': 'error', + 'no-dupe-args': 'error', + 'no-dupe-class-members': 'error', + 'no-dupe-else-if': 'error', + 'no-dupe-keys': 'error', + 'no-duplicate-case': 'error', + 'no-empty': ['error', { allowEmptyCatch: false }], + 'no-empty-pattern': 'error', + 'no-ex-assign': 'error', + 'no-extra-boolean-cast': 'error', + 'no-fallthrough': 'error', + 'no-func-assign': 'error', + 'no-global-assign': 'error', + 'no-import-assign': 'error', + 'no-invalid-regexp': 'error', + 'no-irregular-whitespace': 'error', + 'no-loss-of-precision': 'error', + 'no-new-native-nonconstructor': 'error', + 'no-obj-calls': 'error', + 'no-prototype-builtins': 'error', + 'no-redeclare': 'error', + 'no-self-assign': 'error', + 'no-setter-return': 'error', + 'no-shadow-restricted-names': 'error', + 'no-sparse-arrays': 'error', + 'no-this-before-super': 'error', + 'no-undef': 'error', + 'no-unexpected-multiline': 'error', + 'no-unreachable': 'error', + 'no-unsafe-finally': 'error', + 'no-unsafe-negation': 'error', + 'no-unused-labels': 'error', + 'no-unused-vars': [ + 'error', + { + argsIgnorePattern: '^_', + varsIgnorePattern: '^_', + caughtErrorsIgnorePattern: '^_', + }, + ], + 'no-useless-catch': 'error', + 'no-useless-escape': 'error', + 'no-var': 'error', + 'prefer-const': 'error', + 'use-isnan': 'error', + 'valid-typeof': 'error', + eqeqeq: ['error', 'always'], + complexity: ['error', 15], +}; + +export default [ + { + files: ['.github/scripts/**/*.js', 'tools/nightly_tests/**/*.js'], + languageOptions: { + ecmaVersion: 2022, + sourceType: 'commonjs', + globals: { + Buffer: 'readonly', + console: 'readonly', + module: 'readonly', + process: 'readonly', + require: 'readonly', + URL: 'readonly', + URLSearchParams: 'readonly', + fetch: 'readonly', + setTimeout: 'readonly', + clearTimeout: 'readonly', + __dirname: 'readonly', + __filename: 'readonly', + }, + }, + rules: { + ...BASE_RULES, + 'max-lines-per-function': [ + 'error', + { max: 80, skipBlankLines: true, skipComments: true }, + ], + }, + }, + { + files: ['**/*.test.js', '**/*_test.js'], + rules: { + 'max-lines-per-function': 'off', + complexity: 'off', + }, + }, + { + files: ['cmd/experiment_state_server/static/**/*.js'], + languageOptions: { + ecmaVersion: 2022, + sourceType: 'script', + globals: { + window: 'readonly', + document: 'readonly', + fetch: 'readonly', + console: 'readonly', + location: 'readonly', + URL: 'readonly', + setTimeout: 'readonly', + }, + }, + rules: { + ...BASE_RULES, + }, + }, +]; diff --git a/tools/lint/precommit_runner.py b/tools/lint/precommit_runner.py index 4ffda8b7c..005340632 100755 --- a/tools/lint/precommit_runner.py +++ b/tools/lint/precommit_runner.py @@ -286,6 +286,42 @@ def check_shell(ctx: gates.RunContext) -> None: ctx.fail("shellcheck failed.") +_NONUI_JS_GLOBS = ( + ":(glob).github/scripts/**/*.js", + ":(glob)tools/nightly_tests/**/*.js", + ":(glob)cmd/experiment_state_server/static/**/*.js", +) +_NONUI_JS_PATTERN = ( + r"^(\.github/scripts|tools/nightly_tests|cmd/experiment_state_server/static)/.*\.js$" +) + + +def check_nonui_js(ctx: gates.RunContext) -> None: + """Runs ESLint on non-UI JavaScript files using `tools/lint/eslint.nonui.config.mjs`.""" + if ctx.mode == "check" or gates.staged_paths_match( + "ACMR", r"^tools/lint/eslint\.nonui\.config\.mjs$" + ): + files = gates.git_ls_files(_NONUI_JS_GLOBS) + else: + regex = re.compile(_NONUI_JS_PATTERN) + files = [p for p in gates.git_staged_paths("ACMR") if regex.search(p)] + if not files: + return + eslint = gates.resolve_ui_binary(ctx, "eslint") + if not eslint: + return + print("--> Running Non-UI JS Linter (ESLint)...") + for batch in gates.chunked(files, 500): + if ( + gates.exec_cmd( + [eslint, "--no-config-lookup", "-c", "tools/lint/eslint.nonui.config.mjs", *batch], + cwd=gates.REPO_ROOT, + ).returncode + != 0 + ): + ctx.fail("Non-UI JS ESLint check failed.") + + def _autostage(subcommand: str, state_path: str) -> bool: cmd = [sys.executable, "tools/lint/precommit_autostage.py", subcommand, "--state", state_path] return gates.exec_cmd(cmd, quiet_out=True).returncode == 0 @@ -311,6 +347,7 @@ def _run_linter_phase(mode: str) -> bool: deploy_checks.check_deploy, check_workflows, check_shell, + check_nonui_js, ): fn(ctx) if mode == "fix": diff --git a/tools/lint/precommit_runner_test.py b/tools/lint/precommit_runner_test.py index 29eec1ad2..02f6345e8 100644 --- a/tools/lint/precommit_runner_test.py +++ b/tools/lint/precommit_runner_test.py @@ -397,6 +397,45 @@ def test_check_shell_chunking_failures_and_missing_binary(self): self.assertTrue(ctx_sh_missing.failed) self.assertIn(runner._SHELLCHECK_HINT, buf.getvalue()) + def test_check_nonui_js(self): + ctx_cfg = gates.RunContext(mode="fix") + with ( + mock.patch.object(gates, "staged_paths_match", return_value=True), + mock.patch.object(gates, "git_ls_files", return_value=[".github/scripts/a.js"]), + mock.patch.object(gates, "resolve_ui_binary", return_value="eslint"), + mock.patch.object( + gates, "exec_cmd", return_value=subprocess.CompletedProcess([], 0, "", "") + ) as exec_mock, + mock.patch("sys.stdout", new_callable=io.StringIO), + ): + runner.check_nonui_js(ctx_cfg) + self.assertFalse(ctx_cfg.failed) + self.assertEqual( + exec_mock.call_args.args[0], + [ + "eslint", + "--no-config-lookup", + "-c", + "tools/lint/eslint.nonui.config.mjs", + ".github/scripts/a.js", + ], + ) + + ctx_fail = gates.RunContext(mode="fix") + with ( + mock.patch.object(gates, "staged_paths_match", return_value=False), + mock.patch.object( + gates, "git_staged_paths", return_value=[".github/scripts/a.js", "cmd/ui/b.js"] + ), + mock.patch.object(gates, "resolve_ui_binary", return_value="eslint"), + mock.patch.object( + gates, "exec_cmd", return_value=subprocess.CompletedProcess([], 1, "", "") + ), + mock.patch("sys.stdout", new_callable=io.StringIO), + ): + runner.check_nonui_js(ctx_fail) + self.assertTrue(ctx_fail.failed) + class OrchestrationAndMainTest(unittest.TestCase): def test_linter_phase_blocks_on_snapshot_fail_no_autostage_and_restage_fail(self): @@ -420,6 +459,7 @@ def test_linter_phase_blocks_on_snapshot_fail_no_autostage_and_restage_fail(self mock.patch.object(deploy_checks, "check_deploy"), mock.patch.object(runner, "check_workflows"), mock.patch.object(runner, "check_shell"), + mock.patch.object(runner, "check_nonui_js"), mock.patch.object( gates, "exec_cmd", return_value=subprocess.CompletedProcess([], 1, "", "") ), @@ -440,6 +480,7 @@ def test_linter_phase_blocks_on_snapshot_fail_no_autostage_and_restage_fail(self mock.patch.object(deploy_checks, "check_deploy"), mock.patch.object(runner, "check_workflows"), mock.patch.object(runner, "check_shell"), + mock.patch.object(runner, "check_nonui_js"), mock.patch.object(runner, "_autostage", side_effect=[True, False]) as auto_mock, mock.patch("sys.stdout", new_callable=io.StringIO) as buf, ): From 9d9602886b57a74f2f754adfc45acfea74278413 Mon Sep 17 00:00:00 2001 From: Vlad Lukiantsev Date: Mon, 16 Nov 2026 19:54:04 +0000 Subject: [PATCH 2/5] fix(lint): restore import re required by check_nonui_js Restore `import re` in `tools/lint/precommit_runner.py` after PR #1666 migrated `check_shell` to `str.endswith`, resolving `NameError` in `check_nonui_js`. TAG=agy CONV=ba8411ef-c093-4f45-950a-b3bc1899268c --- tools/lint/precommit_runner.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tools/lint/precommit_runner.py b/tools/lint/precommit_runner.py index 005340632..0210c7c9c 100755 --- a/tools/lint/precommit_runner.py +++ b/tools/lint/precommit_runner.py @@ -19,6 +19,7 @@ import argparse import os +import re import shutil import sys import tempfile From 47a14ea24fce75bb135536795111e8c955ee3cac Mon Sep 17 00:00:00 2001 From: Vlad Lukiantsev Date: Mon, 16 Nov 2026 20:06:12 +0000 Subject: [PATCH 3/5] fix(lint): pass --fix in fix mode for check_nonui_js via run_mode_command Address PR #1668 review feedback (`4590812023`, `4590812041`) by delegating `check_nonui_js` execution and 500-file batching to `gates.run_mode_command` with `fix_args=["--fix"]` and asserting both `fix` and `check` CLI flags in `precommit_runner_test.py`. TAG=agy CONV=ba8411ef-c093-4f45-950a-b3bc1899268c --- tools/lint/precommit_runner.py | 17 ++++++++--------- tools/lint/precommit_runner_test.py | 23 +++++++++++++++++++++++ 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/tools/lint/precommit_runner.py b/tools/lint/precommit_runner.py index 0210c7c9c..0125c2b7d 100755 --- a/tools/lint/precommit_runner.py +++ b/tools/lint/precommit_runner.py @@ -312,15 +312,14 @@ def check_nonui_js(ctx: gates.RunContext) -> None: if not eslint: return print("--> Running Non-UI JS Linter (ESLint)...") - for batch in gates.chunked(files, 500): - if ( - gates.exec_cmd( - [eslint, "--no-config-lookup", "-c", "tools/lint/eslint.nonui.config.mjs", *batch], - cwd=gates.REPO_ROOT, - ).returncode - != 0 - ): - ctx.fail("Non-UI JS ESLint check failed.") + gates.run_mode_command( + ctx, + [eslint, "--no-config-lookup", "-c", "tools/lint/eslint.nonui.config.mjs"], + check_args=[], + fix_args=["--fix"], + failure_msg="Non-UI JS ESLint check failed.", + files=files, + ) def _autostage(subcommand: str, state_path: str) -> bool: diff --git a/tools/lint/precommit_runner_test.py b/tools/lint/precommit_runner_test.py index 02f6345e8..21ce2bce4 100644 --- a/tools/lint/precommit_runner_test.py +++ b/tools/lint/precommit_runner_test.py @@ -412,6 +412,29 @@ def test_check_nonui_js(self): self.assertFalse(ctx_cfg.failed) self.assertEqual( exec_mock.call_args.args[0], + [ + "eslint", + "--no-config-lookup", + "-c", + "tools/lint/eslint.nonui.config.mjs", + "--fix", + ".github/scripts/a.js", + ], + ) + + ctx_chk = gates.RunContext(mode="check") + with ( + mock.patch.object(gates, "git_ls_files", return_value=[".github/scripts/a.js"]), + mock.patch.object(gates, "resolve_ui_binary", return_value="eslint"), + mock.patch.object( + gates, "exec_cmd", return_value=subprocess.CompletedProcess([], 0, "", "") + ) as chk_exec, + mock.patch("sys.stdout", new_callable=io.StringIO), + ): + runner.check_nonui_js(ctx_chk) + self.assertFalse(ctx_chk.failed) + self.assertEqual( + chk_exec.call_args.args[0], [ "eslint", "--no-config-lookup", From bcf68b404104e518115029ec785da71346cf5fe4 Mon Sep 17 00:00:00 2001 From: Vlad Lukiantsev Date: Tue, 17 Nov 2026 11:33:43 +0000 Subject: [PATCH 4/5] fix(lint): address PR #1668 review feedback on CI parity, parser safety, tests, and docs - Align `.github/workflows/lint.yml` `nonui_js` filter and `Run ESLint on Non-UI JS Scripts` step with recursive `**/*.js` discovery via `git ls-files -z` and `workflow_dispatch` guards. - Remove the misleading `try / catch (_)` in `getPrChangedFiles` (`.github/scripts/git-range-diff.js`), normalize `ignoreExitCode: false` for `git diff` calls, and add unit tests in `git-range-diff.test.js`. - Bound `commitMatch` leading indentation to `^ {0,3}` in `.github/scripts/range-diff-parser.js` so code context and commit message lines cannot forge commit-pair headers, with regression tests in `range-diff-parser.test.js`. - Hoist `max-lines-per-function` into `BASE_RULES` and move `**/*.test.js` / `**/*_test.js` overrides to the end of `tools/lint/eslint.nonui.config.mjs`. - Strengthen `tools/lint/precommit_runner_test.py` (`test_check_nonui_js` and `test_check_nonui_js_skips_and_missing_binary`) to exercise `not files`, missing `eslint` in `check` vs `fix` modes, `staged_paths_match` config trigger, and filtered `exec_cmd` args/`cwd`. - Synchronize non-UI JS ESLint documentation in `docs/developer.md`, `tools/README.md`, and `.agents/rules/rule_authoring_standards.md`. Comment links: - https://github.com/google/xmc/pull/1668#discussion_r4598399909 - https://github.com/google/xmc/pull/1668#discussion_r4598400279 - https://github.com/google/xmc/pull/1668#discussion_r4598400667 - https://github.com/google/xmc/pull/1668#discussion_r4598400973 - https://github.com/google/xmc/pull/1668#discussion_r4598401259 - https://github.com/google/xmc/pull/1668#discussion_r4598401603 - https://github.com/google/xmc/pull/1668#discussion_r4598401991 TAG=agy CONV=ba8411ef-c093-4f45-950a-b3bc1899268c --- .agents/rules/rule_authoring_standards.md | 1 + .github/scripts/git-range-diff.js | 19 +++--- .github/scripts/git-range-diff.test.js | 77 ++++++++++++++++++++++ .github/scripts/range-diff-parser.js | 2 +- .github/scripts/range-diff-parser.test.js | 33 ++++++++++ .github/workflows/lint.yml | 21 +++--- docs/developer.md | 9 +-- tools/README.md | 1 + tools/lint/eslint.nonui.config.mjs | 22 +++---- tools/lint/precommit_runner_test.py | 79 ++++++++++++++++++++--- 10 files changed, 220 insertions(+), 44 deletions(-) diff --git a/.agents/rules/rule_authoring_standards.md b/.agents/rules/rule_authoring_standards.md index 5be68a81b..c30486c04 100644 --- a/.agents/rules/rule_authoring_standards.md +++ b/.agents/rules/rule_authoring_standards.md @@ -119,6 +119,7 @@ Destinations by language, all reachable through `.githooks/pre-commit` and its ` | Python | [`pyproject.toml`](../../pyproject.toml) ruff `select` and banned-api config, mypy | | Go | [`.golangci.yml`](../../.golangci.yml), `nogo_config.json`, custom checks in `tools/lint/` | | TypeScript / UI | `cmd/ui/eslint.config.js`, prettier, `tools/lint/check_file_naming.py` (UI template/test and `.github/scripts/` test naming) | +| Non-UI JavaScript | [`tools/lint/eslint.nonui.config.mjs`](../../tools/lint/eslint.nonui.config.mjs) (`.github/scripts/`, `tools/nightly_tests/`, `cmd/experiment_state_server/static/`) | | Bazel | buildifier | | Terraform | [`.tflint.hcl`](../../.tflint.hcl) | | Agent docs (`.agents/**/*.md`, `AGENTS.md`) | `tools/lint/check_agent_doc_paths.py` (path references must exist) | diff --git a/.github/scripts/git-range-diff.js b/.github/scripts/git-range-diff.js index e03d30eaa..3772741b4 100644 --- a/.github/scripts/git-range-diff.js +++ b/.github/scripts/git-range-diff.js @@ -544,26 +544,23 @@ function getPrChangedFiles(baseRefOrApprovedSha, approvedShaOrCurrentSha, curren } if (baseRef) { - const prDiffOutput = execGit(['diff', '--name-only', `origin/${baseRef}...${headSha}`], opts); + const diffOpts = { ...opts, ignoreExitCode: false }; + const prDiffOutput = execGit(['diff', '--name-only', `origin/${baseRef}...${headSha}`], diffOpts); const files = new Set(prDiffOutput.split(/\r?\n/).map(f => f.trim()).filter(Boolean)); if (approvedSha && approvedSha !== headSha) { - try { - const isAncestor = execGit(['merge-base', '--is-ancestor', approvedSha, headSha], { ...opts, ignoreExitCode: true }); - if (isAncestor.status === 0) { - const followUpDiff = execGit(['diff', '--name-only', approvedSha, headSha], opts); - for (const file of followUpDiff.split(/\r?\n/).map(f => f.trim()).filter(Boolean)) { - files.add(file); - } + const isAncestor = execGit(['merge-base', '--is-ancestor', approvedSha, headSha], { ...opts, ignoreExitCode: true }); + if (isAncestor.status === 0) { + const followUpDiff = execGit(['diff', '--name-only', approvedSha, headSha], diffOpts); + for (const file of followUpDiff.split(/\r?\n/).map(f => f.trim()).filter(Boolean)) { + files.add(file); } - } catch (_) { - /* Skip unreachable commit SHA gracefully when aggregating file paths. */ } } return Array.from(files); } - const diffOutput = execGit(['diff', '--name-only', approvedSha, headSha], opts); + const diffOutput = execGit(['diff', '--name-only', approvedSha, headSha], { ...opts, ignoreExitCode: false }); return diffOutput.split(/\r?\n/).map(f => f.trim()).filter(Boolean); } diff --git a/.github/scripts/git-range-diff.test.js b/.github/scripts/git-range-diff.test.js index 248c4d4da..408cb5569 100644 --- a/.github/scripts/git-range-diff.test.js +++ b/.github/scripts/git-range-diff.test.js @@ -575,6 +575,83 @@ test('getPrChangedFiles returns files from 3-dot diff and includes follow-up com } }); +test('getPrChangedFiles skips follow-up diff when approvedSha is unreachable or non-ancestor', () => { + const originalExec = childProcess.execFileSync; + let followUpCalled = false; + childProcess.execFileSync = (cmd, args) => { + if (cmd === 'git' && args[0] === 'diff' && args[2] === 'origin/main...headSha') { + return 'file1.go\nfile2.go\n'; + } + if (cmd === 'git' && args[0] === 'merge-base' && args[1] === '--is-ancestor') { + const err = new Error('fatal: Not a valid object name approvedSha'); + err.status = 128; + throw err; + } + if (cmd === 'git' && args[0] === 'diff' && args[2] === 'approvedSha' && args[3] === 'headSha') { + followUpCalled = true; + return 'file3.go\n'; + } + throw new Error(`Unexpected command: ${cmd} ${args.join(' ')}`); + }; + + try { + const files = getPrChangedFiles('main', 'approvedSha', 'headSha'); + assert.strictEqual(followUpCalled, false); + assert.deepStrictEqual(files.sort(), ['file1.go', 'file2.go']); + } finally { + childProcess.execFileSync = originalExec; + } +}); + +test('getPrChangedFiles propagates follow-up git diff failures instead of failing open', () => { + const originalExec = childProcess.execFileSync; + childProcess.execFileSync = (cmd, args) => { + if (cmd === 'git' && args[0] === 'diff' && args[2] === 'origin/main...headSha') { + return 'file1.go\n'; + } + if (cmd === 'git' && args[0] === 'merge-base' && args[1] === '--is-ancestor') { + return ''; + } + if (cmd === 'git' && args[0] === 'diff' && args[2] === 'approvedSha' && args[3] === 'headSha') { + const err = new Error('git diff failed: ETIMEDOUT'); + err.status = 1; + throw err; + } + throw new Error(`Unexpected command: ${cmd} ${args.join(' ')}`); + }; + + try { + assert.throws(() => getPrChangedFiles('main', 'approvedSha', 'headSha'), /ETIMEDOUT/); + } finally { + childProcess.execFileSync = originalExec; + } +}); + +test('getPrChangedFiles normalizes ignoreExitCode to false for git diff calls', () => { + const originalExec = childProcess.execFileSync; + childProcess.execFileSync = (cmd, args) => { + if (cmd === 'git' && args[0] === 'diff' && args[2] === 'origin/main...headSha') { + return 'file1.go\n'; + } + if (cmd === 'git' && args[0] === 'merge-base' && args[1] === '--is-ancestor') { + return ''; + } + if (cmd === 'git' && args[0] === 'diff' && args[2] === 'approvedSha' && args[3] === 'headSha') { + return 'file2.go\n'; + } + throw new Error(`Unexpected command: ${cmd} ${args.join(' ')}`); + }; + + try { + const threeDotFiles = getPrChangedFiles('main', 'approvedSha', 'headSha', { ignoreExitCode: true }); + assert.deepStrictEqual(threeDotFiles.sort(), ['file1.go', 'file2.go']); + const twoDotFiles = getPrChangedFiles('approvedSha', 'headSha', { ignoreExitCode: true }); + assert.deepStrictEqual(twoDotFiles, ['file2.go']); + } finally { + childProcess.execFileSync = originalExec; + } +}); + test('getCommitModifiedFiles returns file list for commit from diff-tree', () => { const originalExec = childProcess.execFileSync; childProcess.execFileSync = (cmd, args) => { diff --git a/.github/scripts/range-diff-parser.js b/.github/scripts/range-diff-parser.js index e0887df76..e864e6169 100644 --- a/.github/scripts/range-diff-parser.js +++ b/.github/scripts/range-diff-parser.js @@ -128,7 +128,7 @@ function parseRangeDiff(output) { for (const line of lines) { // eslint-disable-next-line no-control-regex -- intentionally strips ANSI escape codes const cleanLine = line.replace(/\x1b\[[0-9;]*m/g, ''); - const commitMatch = cleanLine.match(/^\s*(?:\d+|-):\s+([a-f0-9-]+)\s+([=!<>])\s+(?:\d+|-):\s+([a-f0-9-]+)/); + const commitMatch = cleanLine.match(/^ {0,3}(?:\d+|-):\s+([a-f0-9-]+)\s+([=!<>])\s+(?:\d+|-):\s+([a-f0-9-]+)/); if (commitMatch) { const status = commitMatch[2]; if (status !== '=') { diff --git a/.github/scripts/range-diff-parser.test.js b/.github/scripts/range-diff-parser.test.js index c6f92c448..ddc769de2 100644 --- a/.github/scripts/range-diff-parser.test.js +++ b/.github/scripts/range-diff-parser.test.js @@ -280,6 +280,39 @@ test('parseRangeDiff identifies added commits and captures addedCommitShas', () assert.deepStrictEqual(resultRemoved.addedCommitShas, []); }); +test('parseRangeDiff ignores commit-pair patterns in code context and commit message lines', () => { + const sampleOutput = [ + '1: a1b2c3d ! 1: e5f6g7h Commit title', + ' ## Commit message ##', + ' 1: a1b2c3d < -: -------', + ' -: ------- > 2: 89abcdef', + ' @@ pkg/a.go:10,5 @@', + ' 1: 0000000 = 1: 0000000', + ' 1: a1b2c3d < -: -------', + ' -: ------- > 2: 89abcdef', + ' ++realDeltaLine()' + ].join('\n'); + + const result = parseRangeDiff(sampleOutput); + assert.strictEqual(result.hasRemovedCommits, false); + assert.deepStrictEqual(result.addedCommitShas, []); + assert.strictEqual(result.additions, 1); + assert.strictEqual(result.totalDelta, 1); + assert.strictEqual(result.isCleanRebase, false); +}); + +test('parseRangeDiff matches genuine commit headers padded with up to 3 leading spaces', () => { + const sampleOutput = [ + ' 10: a1b2c3d = 10: e5f6g7h Tenth commit', + ' -: ------- > 1000: 89abcdef Added thousandth commit' + ].join('\n'); + + const result = parseRangeDiff(sampleOutput); + assert.strictEqual(result.hasRemovedCommits, false); + assert.deepStrictEqual(result.addedCommitShas, ['89abcdef']); + assert.strictEqual(result.isCleanRebase, false); +}); + test('isIgnoredRangeDiffHeader detects metadata and header lines', () => { assert.strictEqual(isIgnoredRangeDiffHeader('@@ Metadata'), true); assert.strictEqual(isIgnoredRangeDiffHeader('Author: Alice '), true); diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index b8821bdf4..701e2051e 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -73,9 +73,9 @@ jobs: - '.github/workflows/nightly-examples-e2e.yml' - '.github/workflows/lint.yml' nonui_js: - - '.github/scripts/*.js' - - 'tools/nightly_tests/*.js' - - 'cmd/experiment_state_server/static/*.js' + - '.github/scripts/**/*.js' + - 'tools/nightly_tests/**/*.js' + - 'cmd/experiment_state_server/static/**/*.js' - 'tools/lint/eslint.nonui.config.mjs' - 'cmd/ui/package-lock.json' - '.github/workflows/lint.yml' @@ -249,7 +249,7 @@ jobs: # 3. UI Linting & Formatting (Prettier & ESLint) - name: 'Set up Node.js' - if: steps.changes.outputs.ui == 'true' || steps.changes.outputs.nonui_js == 'true' + if: github.event_name == 'workflow_dispatch' || steps.changes.outputs.ui == 'true' || steps.changes.outputs.nonui_js == 'true' uses: 'actions/setup-node@1d0ff469b7ec7b3cb9d8673fde0c81c44821de2a' # v4.2.0 with: node-version: '22' @@ -257,7 +257,7 @@ jobs: cache-dependency-path: 'cmd/ui/package-lock.json' - name: 'Install UI Dependencies' - if: steps.changes.outputs.ui == 'true' || steps.changes.outputs.nonui_js == 'true' + if: github.event_name == 'workflow_dispatch' || steps.changes.outputs.ui == 'true' || steps.changes.outputs.nonui_js == 'true' run: npm ci --prefer-offline --no-audit working-directory: 'cmd/ui' @@ -272,10 +272,15 @@ jobs: working-directory: 'cmd/ui' - name: 'Run ESLint on Non-UI JS Scripts' - if: steps.changes.outputs.nonui_js == 'true' + if: github.event_name == 'workflow_dispatch' || steps.changes.outputs.nonui_js == 'true' run: | - ./cmd/ui/node_modules/.bin/eslint --no-config-lookup -c tools/lint/eslint.nonui.config.mjs \ - .github/scripts/*.js tools/nightly_tests/*.js cmd/experiment_state_server/static/*.js + mapfile -d '' -t NONUI_JS_FILES < <(git -c core.quotePath=false ls-files -z -- \ + ':(glob).github/scripts/**/*.js' \ + ':(glob)tools/nightly_tests/**/*.js' \ + ':(glob)cmd/experiment_state_server/static/**/*.js') + if [ "${#NONUI_JS_FILES[@]}" -gt 0 ]; then + ./cmd/ui/node_modules/.bin/eslint --no-config-lookup -c tools/lint/eslint.nonui.config.mjs -- "${NONUI_JS_FILES[@]}" + fi # 3a. GitHub Actions & Nightly JS Script Tests (node:test, no dependencies) - name: 'Set up Node.js for JS Script Tests' diff --git a/docs/developer.md b/docs/developer.md index 50047a48b..90ae899c3 100644 --- a/docs/developer.md +++ b/docs/developer.md @@ -255,7 +255,7 @@ Go source files (`.go`) are formatted using standard **`gofmt`**: ### 3. UI Code (`prettier` & `eslint`) Angular and TypeScript frontend code under `cmd/ui` is formatted and linted using **Prettier** and **ESLint**: * **`prettier`**: Enforces consistent styling on `.ts`, `.html`, and `.scss` files (`npm run format:check`). -* **`eslint`**: Enforces Angular best practices, import sorting, and TypeScript strictness (`npm run lint`). +* **`eslint`**: Enforces Angular best practices, import sorting, and TypeScript strictness under `cmd/ui` (`npm run lint`). Non-UI `.js` scripts use the separate config documented in section 8. ### 4. Bazel Build Files (`buildifier`) Bazel build graphs (`BUILD`, `BUILD.bazel`, `WORKSPACE`, `MODULE.bazel`, and `.bzl` files) are formatted and analyzed using **`buildifier`**: @@ -277,10 +277,11 @@ Tracked UI templates, UI test specs, and GitHub Actions script tests are checked * Enforces `*.ng.html` for HTML files under `cmd/ui/` (requiring templates under `cmd/ui/app/` and allowing root `cmd/ui/index.html`), `*_test.ts` Karma unit test specs under `cmd/ui/app/` (rejecting `*.spec.ts`, `*_spec.ts`, and `*.test.ts` repo-wide outside `cmd/ui/screenshot_tests/`), `*.screenshot.spec.ts` Playwright specs under `cmd/ui/screenshot_tests/`, and top-level `*.test.js` Node workflow script tests under `.github/scripts/`. * Prevents misnamed test specs (such as `foo.spec.ts` or `.github/scripts/foo_test.js`) from silently compiling without executing in Karma or CI; runs in both the pre-commit hook and CI. -### 8. GitHub Actions Workflows & Shell Scripts (`actionlint` & `shellcheck`) -Workflows and tracked shell scripts are validated in CI ([`.github/workflows/lint.yml`](../.github/workflows/lint.yml)) and locally in `.githooks/pre-commit`: +### 8. GitHub Actions Workflows, Shell Scripts & Non-UI JS (`actionlint`, `shellcheck`, `eslint`) +Workflows, tracked shell scripts, and non-UI JavaScript scripts are validated in CI ([`.github/workflows/lint.yml`](../.github/workflows/lint.yml)) and locally in `.githooks/pre-commit`: * **`actionlint`**: Validates `.github/workflows/*.{yml,yaml}` and integrates `shellcheck` across workflow `run:` blocks. * **`shellcheck`**: Validates tracked `*.sh` and `*.bash` scripts with `shellcheck -S style -e SC1091`. +* **Non-UI JS ESLint**: Runs `./cmd/ui/node_modules/.bin/eslint --no-config-lookup -c tools/lint/eslint.nonui.config.mjs` recursively across `.github/scripts/**/*.js`, `tools/nightly_tests/**/*.js`, and `cmd/experiment_state_server/static/**/*.js` via [`tools/lint/eslint.nonui.config.mjs`](../tools/lint/eslint.nonui.config.mjs) (reusing the ESLint installation in `cmd/ui`). * **Node JS Script Tests**: Runs `node --test .github/scripts/*.test.js` and `node --test tools/nightly_tests/*_test.js` in CI when workflow scripts or nightly test utilities change. --- @@ -295,7 +296,7 @@ Developers can run the pre-commit hook locally in two modes: ``` Automatically formats files and adds missing headers. Formatter fixes to files that were fully staged before the hook ran are restaged automatically ([`tools/lint/precommit_autostage.py`](../tools/lint/precommit_autostage.py)); fixes to partially staged, unstaged, or unrelated files fail the hook so you can review and stage them. Set `XMC_PRECOMMIT_NO_AUTOSTAGE=1` to disable auto-restaging. Affected Bazel unit tests are skipped when every staged path is Markdown or under `docs/`. Which checks run depends on what is staged: * **Per-file linters and formatters** (Ruff, Prettier, ESLint, `terraform fmt`, `addlicense`) only process staged files. - * **Cross-file checks** (mypy, TFLint, `helm lint`, Trivy) also run when a staged delete or rename removes a file they cover. mypy also runs when `pyproject.toml` or a `.gitignore` changes, and TFLint when `.tflint.hcl` changes. The agent doc path check runs on any staged delete or rename. + * **Cross-file checks** (mypy, TFLint, `helm lint`, Trivy) also run when a staged delete or rename removes a file they cover. mypy also runs when `pyproject.toml` or a `.gitignore` changes, and TFLint when `.tflint.hcl` changes. The agent doc path check runs on any staged delete or rename. Staging `tools/lint/eslint.nonui.config.mjs` triggers a full non-UI JS ESLint run across tracked `.github/scripts/**/*.js`, `tools/nightly_tests/**/*.js`, and `cmd/experiment_state_server/static/**/*.js` files (in `fix` mode, a missing `cmd/ui/node_modules` warns and skips, while `--check` fails closed). * **mypy** runs bare `mypy`, so `[tool.mypy]` in `pyproject.toml` is the single source of truth for the checked files in CI and in both hook modes. It checks every Python file in the repository, so new directories are covered automatically and edits that break unchanged importers fail locally. Two lists deny files: `.gitignore` (`exclude_gitignore`, mypy 1.16 or later) skips local-only directories such as virtualenvs, Bazel output and the `cluster-toolkit` clone, and `exclude` skips tracked files. Untracked `.py` files that `.gitignore` does not cover are checked too, so remove or fix stray scratch files before committing. * **Check-only mode (mirrors CI validation)**: diff --git a/tools/README.md b/tools/README.md index 6c4435ed8..fb9230a49 100644 --- a/tools/README.md +++ b/tools/README.md @@ -91,6 +91,7 @@ Offline syntax walkers and static validation tooling: - [`precommit_gates.py`](lint/precommit_gates.py): Declarative runner (`--mode fix|check`) and shared git/execution primitives for pre-commit Python lint gates (`check_legacy_imports`, `check_examples_coverage`, `check_matrix_contract`, `check_agent_doc_paths`, `check_file_naming`, `check_ui_selector_contract`). - [`precommit_deploy_checks.py`](lint/precommit_deploy_checks.py): Pre-commit infrastructure, Helm, and Trivy security gate runner (`--mode fix|check`) covering `deploy/` and `test/helm/`. - [`precommit_runner.py`](lint/precommit_runner.py): Top-level pure-stdlib Python orchestrator invoked by `.githooks/pre-commit` for language, UI, deploy, and affected-test gates. +- [`eslint.nonui.config.mjs`](lint/eslint.nonui.config.mjs): ESLint 10 flat configuration enforcing baseline JS correctness (`no-empty`, `no-unused-vars`, `eqeqeq`, `complexity: 15`, `max-lines-per-function: 80`) recursively across `.github/scripts/**/*.js`, `tools/nightly_tests/**/*.js`, and `cmd/experiment_state_server/static/**/*.js`; invoked by `precommit_runner.py` (`check_nonui_js`) and `.github/workflows/lint.yml`. ### 8. [`tools/migrations/components/`](migrations/components/README.md) (One-Time Component Cutover) Temporary migration side script (`migrate.py`) for transitioning existing umbrella-managed metrics deployments to the `stable-2026a` Component profile (`prepare`, `handoff [--undo]`, `finalize`). See [`docs/migrations/components.md`](../docs/migrations/components.md). diff --git a/tools/lint/eslint.nonui.config.mjs b/tools/lint/eslint.nonui.config.mjs index 2be55b770..fc47b3928 100644 --- a/tools/lint/eslint.nonui.config.mjs +++ b/tools/lint/eslint.nonui.config.mjs @@ -76,6 +76,10 @@ const BASE_RULES = { 'valid-typeof': 'error', eqeqeq: ['error', 'always'], complexity: ['error', 15], + 'max-lines-per-function': [ + 'error', + { max: 80, skipBlankLines: true, skipComments: true }, + ], }; export default [ @@ -101,17 +105,6 @@ export default [ }, rules: { ...BASE_RULES, - 'max-lines-per-function': [ - 'error', - { max: 80, skipBlankLines: true, skipComments: true }, - ], - }, - }, - { - files: ['**/*.test.js', '**/*_test.js'], - rules: { - 'max-lines-per-function': 'off', - complexity: 'off', }, }, { @@ -133,4 +126,11 @@ export default [ ...BASE_RULES, }, }, + { + files: ['**/*.test.js', '**/*_test.js'], + rules: { + 'max-lines-per-function': 'off', + complexity: 'off', + }, + }, ]; diff --git a/tools/lint/precommit_runner_test.py b/tools/lint/precommit_runner_test.py index 21ce2bce4..814d5c9a5 100644 --- a/tools/lint/precommit_runner_test.py +++ b/tools/lint/precommit_runner_test.py @@ -400,8 +400,12 @@ def test_check_shell_chunking_failures_and_missing_binary(self): def test_check_nonui_js(self): ctx_cfg = gates.RunContext(mode="fix") with ( - mock.patch.object(gates, "staged_paths_match", return_value=True), - mock.patch.object(gates, "git_ls_files", return_value=[".github/scripts/a.js"]), + mock.patch.object( + gates, "git_staged_paths", return_value=["tools/lint/eslint.nonui.config.mjs"] + ), + mock.patch.object( + gates, "git_ls_files", return_value=[".github/scripts/a.js"] + ) as cfg_ls, mock.patch.object(gates, "resolve_ui_binary", return_value="eslint"), mock.patch.object( gates, "exec_cmd", return_value=subprocess.CompletedProcess([], 0, "", "") @@ -410,6 +414,8 @@ def test_check_nonui_js(self): ): runner.check_nonui_js(ctx_cfg) self.assertFalse(ctx_cfg.failed) + cfg_ls.assert_called_once_with(runner._NONUI_JS_GLOBS) + self.assertEqual(exec_mock.call_args.kwargs["cwd"], gates.REPO_ROOT) self.assertEqual( exec_mock.call_args.args[0], [ @@ -424,7 +430,9 @@ def test_check_nonui_js(self): ctx_chk = gates.RunContext(mode="check") with ( - mock.patch.object(gates, "git_ls_files", return_value=[".github/scripts/a.js"]), + mock.patch.object( + gates, "git_ls_files", return_value=[".github/scripts/a.js"] + ) as chk_ls, mock.patch.object(gates, "resolve_ui_binary", return_value="eslint"), mock.patch.object( gates, "exec_cmd", return_value=subprocess.CompletedProcess([], 0, "", "") @@ -433,6 +441,8 @@ def test_check_nonui_js(self): ): runner.check_nonui_js(ctx_chk) self.assertFalse(ctx_chk.failed) + chk_ls.assert_called_once_with(runner._NONUI_JS_GLOBS) + self.assertEqual(chk_exec.call_args.kwargs["cwd"], gates.REPO_ROOT) self.assertEqual( chk_exec.call_args.args[0], [ @@ -445,19 +455,70 @@ def test_check_nonui_js(self): ) ctx_fail = gates.RunContext(mode="fix") - with ( - mock.patch.object(gates, "staged_paths_match", return_value=False), - mock.patch.object( - gates, "git_staged_paths", return_value=[".github/scripts/a.js", "cmd/ui/b.js"] - ), + staged_paths = [ + ".github/scripts/a.js", + "tools/nightly_tests/sub/b.js", + "cmd/experiment_state_server/static/c.js", + "cmd/ui/b.js", + ] + with ( + mock.patch.object(gates, "git_staged_paths", return_value=staged_paths), mock.patch.object(gates, "resolve_ui_binary", return_value="eslint"), mock.patch.object( gates, "exec_cmd", return_value=subprocess.CompletedProcess([], 1, "", "") - ), + ) as fail_exec, mock.patch("sys.stdout", new_callable=io.StringIO), ): runner.check_nonui_js(ctx_fail) self.assertTrue(ctx_fail.failed) + self.assertEqual(fail_exec.call_args.kwargs["cwd"], gates.REPO_ROOT) + self.assertEqual( + fail_exec.call_args.args[0], + [ + "eslint", + "--no-config-lookup", + "-c", + "tools/lint/eslint.nonui.config.mjs", + "--fix", + ".github/scripts/a.js", + "tools/nightly_tests/sub/b.js", + "cmd/experiment_state_server/static/c.js", + ], + ) + + def test_check_nonui_js_skips_and_missing_binary(self): + ctx_skip = gates.RunContext(mode="fix") + with ( + mock.patch.object(gates, "git_staged_paths", return_value=["cmd/ui/b.js", "README.md"]), + mock.patch.object(gates, "resolve_ui_binary") as skip_resolve, + mock.patch.object(gates, "exec_cmd") as skip_exec, + ): + runner.check_nonui_js(ctx_skip) + self.assertFalse(ctx_skip.failed) + skip_resolve.assert_not_called() + skip_exec.assert_not_called() + + ctx_missing_chk = gates.RunContext(mode="check") + with ( + mock.patch.object(gates, "git_ls_files", return_value=[".github/scripts/a.js"]), + mock.patch.object(gates.shutil, "which", return_value=None), + mock.patch.object(gates, "exec_cmd") as missing_chk_exec, + mock.patch("sys.stdout", new_callable=io.StringIO), + ): + runner.check_nonui_js(ctx_missing_chk) + self.assertTrue(ctx_missing_chk.failed) + missing_chk_exec.assert_not_called() + + ctx_missing_fix = gates.RunContext(mode="fix") + with ( + mock.patch.object(gates, "git_staged_paths", return_value=[".github/scripts/a.js"]), + mock.patch.object(gates.shutil, "which", return_value=None), + mock.patch.object(gates, "exec_cmd") as missing_fix_exec, + mock.patch("sys.stdout", new_callable=io.StringIO), + ): + runner.check_nonui_js(ctx_missing_fix) + self.assertFalse(ctx_missing_fix.failed) + missing_fix_exec.assert_not_called() class OrchestrationAndMainTest(unittest.TestCase): From 3912786f45baef905944fff348df4ae4c00f8619 Mon Sep 17 00:00:00 2001 From: Vlad Lukiantsev Date: Tue, 17 Nov 2026 14:08:41 +0000 Subject: [PATCH 5/5] fix(ci): pass --no-renames in getPrChangedFiles and wait on ls-files in lint - Pass `--no-renames` to all three `git diff --name-only` calls (3-dot PR diff, ancestor follow-up diff, and 2-dot fallback diff) in `.github/scripts/git-range-diff.js` so pure `git mv` renames out of protected paths still emit the protected source path and trigger `PROTECTED_FILES_MODIFIED` lockout. - Update positional `git diff` mock assertions in `.github/scripts/git-range-diff.test.js` and `.github/scripts/approval-policy.test.js`, add regression tests for `--no-renames` across all three call forms and moved protected files, and align `.github/scripts/README.md`. - Add `wait "$!"` after `mapfile -d '' -t NONUI_JS_FILES < <(...)` in `.github/workflows/lint.yml` so a non-zero `git ls-files` exit code fails the CI step closed. Comment link: https://github.com/google/xmc/pull/1668#discussion_r4600112617 Comment link: https://github.com/google/xmc/pull/1668#discussion_r4600112964 TAG=agy CONV=ba8411ef-c093-4f45-950a-b3bc1899268c --- .github/scripts/README.md | 2 +- .github/scripts/approval-policy.test.js | 31 +++++++++++++---- .github/scripts/git-range-diff.js | 6 ++-- .github/scripts/git-range-diff.test.js | 45 ++++++++++++++++++++----- .github/workflows/lint.yml | 1 + 5 files changed, 66 insertions(+), 19 deletions(-) diff --git a/.github/scripts/README.md b/.github/scripts/README.md index 6d42daf2a..d8711afad 100644 --- a/.github/scripts/README.md +++ b/.github/scripts/README.md @@ -124,7 +124,7 @@ Any modification touching protected files immediately aborts auto-reapproval: - `api/buf.yaml` and `api/BUILD.bazel` (the breaking-change rules and test definition, which could otherwise weaken or redirect the check) - Target base branch configuration files -Scoping uses 3-dot diff against the PR's target base branch (`git diff --name-only origin/${baseRef}...${currentSha}`) to prevent false positives when intermediate commits are merged into `origin/main`. If Git commands fail, the check defaults to `hasProtectedFileChanges = true` (fail-safe). +Scoping uses 3-dot diff against the PR's target base branch (`git diff --name-only --no-renames origin/${baseRef}...${currentSha}`) to prevent false positives when intermediate commits are merged into `origin/main` and to detect renames out of protected paths. If Git commands fail, the check defaults to `hasProtectedFileChanges = true` (fail-safe). **Clean rebase exception**: A PR touching protected files is still auto-reapproved, for any author, when the push is a pure clean rebase: every approved commit is unchanged in `git range-diff`, with zero changed patch lines and no removed or added commits. Any other change (including grace delta or test-only edits) keeps the lockout. Restoring a dismissed approval on the same commit also stays locked out. diff --git a/.github/scripts/approval-policy.test.js b/.github/scripts/approval-policy.test.js index 383914309..3c7720189 100644 --- a/.github/scripts/approval-policy.test.js +++ b/.github/scripts/approval-policy.test.js @@ -76,7 +76,7 @@ test('checkProtectedFileChanges detects sensitive modifications and handles git // Sensitive files modified childProcess.execFileSync = (cmd, args) => { - if (cmd === 'git' && args[0] === 'diff') { + if (cmd === 'git' && args[0] === 'diff' && args[1] === '--name-only' && args[2] === '--no-renames') { return '.github/scripts/incremental-approval.js\ncmd/server/main.go\n'; } throw new Error(`Unexpected command: ${cmd}`); @@ -91,7 +91,7 @@ test('checkProtectedFileChanges detects sensitive modifications and handles git // Non-sensitive files modified childProcess.execFileSync = (cmd, args) => { - if (cmd === 'git' && args[0] === 'diff') { + if (cmd === 'git' && args[0] === 'diff' && args[1] === '--name-only' && args[2] === '--no-renames') { return 'pkg/main.go\nREADME.md\n'; } throw new Error(`Unexpected command: ${cmd}`); @@ -143,11 +143,11 @@ test('checkProtectedFileChanges uses 3-dot diff against baseRef and avoids false const originalExec = childProcess.execFileSync; childProcess.execFileSync = (cmd, args) => { - if (cmd === 'git' && args[0] === 'diff') { - if (args[2] === 'origin/feat/tools-dev-env-helm-deployer...c40cf0f') { + if (cmd === 'git' && args[0] === 'diff' && args[1] === '--name-only' && args[2] === '--no-renames') { + if (args[3] === 'origin/feat/tools-dev-env-helm-deployer...c40cf0f') { return 'tools/dev_env/local_runner.py\ntools/dev_env/local_runner_test.py\n'; } - if (args[2] === '8fa2009' && args[3] === 'c40cf0f') { + if (args[3] === '8fa2009' && args[4] === 'c40cf0f') { return '.github/scripts/incremental-approval.js\ntools/dev_env/local_runner.py\n'; } } @@ -171,7 +171,7 @@ test('checkProtectedFileChanges detects sensitive files in 3-dot diff', () => { const originalExec = childProcess.execFileSync; childProcess.execFileSync = (cmd, args) => { - if (cmd === 'git' && args[0] === 'diff' && args[2] === 'origin/main...c40cf0f') { + if (cmd === 'git' && args[0] === 'diff' && args[1] === '--name-only' && args[2] === '--no-renames' && args[3] === 'origin/main...c40cf0f') { return '.github/workflows/incremental-pr-approval.yml\n'; } throw new Error(`Unexpected command: ${cmd} ${args.join(' ')}`); @@ -189,7 +189,7 @@ test('checkProtectedFileChanges respects 3-argument signature even when execOpti const originalExec = childProcess.execFileSync; childProcess.execFileSync = (cmd, args) => { - if (cmd === 'git' && args[0] === 'diff' && args[2] === 'origin/main...c40cf0f') { + if (cmd === 'git' && args[0] === 'diff' && args[1] === '--name-only' && args[2] === '--no-renames' && args[3] === 'origin/main...c40cf0f') { return 'pkg/foo.go\n'; } throw new Error(`Unexpected command: ${cmd} ${args.join(' ')}`); @@ -203,6 +203,23 @@ test('checkProtectedFileChanges respects 3-argument signature even when execOpti } }); +test('checkProtectedFileChanges detects moved protected file when --no-renames reports both paths', () => { + const originalExec = childProcess.execFileSync; + + childProcess.execFileSync = (cmd, args) => { + if (cmd === 'git' && args[0] === 'diff' && args[1] === '--name-only' && args[2] === '--no-renames' && args[3] === 'origin/main...c40cf0f') { + return '.github/workflows/lint.yml\ndocs/lint.yml.bak\n'; + } + throw new Error(`Unexpected command: ${cmd} ${args.join(' ')}`); + }; + + try { + assert.strictEqual(checkProtectedFileChanges('main', 'c40cf0f', 'c40cf0f'), true); + } finally { + childProcess.execFileSync = originalExec; + } +}); + test('formatReapprovalReviewBody formats markdown body accurately', () => { const body = formatReapprovalReviewBody('Clean rebase detected.', 'alice', 'abcdef1234567890'); assert.match(body, /🤖 \*\*Incremental PR Approval\*\*/); diff --git a/.github/scripts/git-range-diff.js b/.github/scripts/git-range-diff.js index 3772741b4..09446b13d 100644 --- a/.github/scripts/git-range-diff.js +++ b/.github/scripts/git-range-diff.js @@ -545,13 +545,13 @@ function getPrChangedFiles(baseRefOrApprovedSha, approvedShaOrCurrentSha, curren if (baseRef) { const diffOpts = { ...opts, ignoreExitCode: false }; - const prDiffOutput = execGit(['diff', '--name-only', `origin/${baseRef}...${headSha}`], diffOpts); + const prDiffOutput = execGit(['diff', '--name-only', '--no-renames', `origin/${baseRef}...${headSha}`], diffOpts); const files = new Set(prDiffOutput.split(/\r?\n/).map(f => f.trim()).filter(Boolean)); if (approvedSha && approvedSha !== headSha) { const isAncestor = execGit(['merge-base', '--is-ancestor', approvedSha, headSha], { ...opts, ignoreExitCode: true }); if (isAncestor.status === 0) { - const followUpDiff = execGit(['diff', '--name-only', approvedSha, headSha], diffOpts); + const followUpDiff = execGit(['diff', '--name-only', '--no-renames', approvedSha, headSha], diffOpts); for (const file of followUpDiff.split(/\r?\n/).map(f => f.trim()).filter(Boolean)) { files.add(file); } @@ -560,7 +560,7 @@ function getPrChangedFiles(baseRefOrApprovedSha, approvedShaOrCurrentSha, curren return Array.from(files); } - const diffOutput = execGit(['diff', '--name-only', approvedSha, headSha], { ...opts, ignoreExitCode: false }); + const diffOutput = execGit(['diff', '--name-only', '--no-renames', approvedSha, headSha], { ...opts, ignoreExitCode: false }); return diffOutput.split(/\r?\n/).map(f => f.trim()).filter(Boolean); } diff --git a/.github/scripts/git-range-diff.test.js b/.github/scripts/git-range-diff.test.js index 408cb5569..8252fa226 100644 --- a/.github/scripts/git-range-diff.test.js +++ b/.github/scripts/git-range-diff.test.js @@ -555,13 +555,13 @@ test('buildGitEnv constructs non-interactive environment and basic auth header', test('getPrChangedFiles returns files from 3-dot diff and includes follow-up commits if ancestor', () => { const originalExec = childProcess.execFileSync; childProcess.execFileSync = (cmd, args) => { - if (cmd === 'git' && args[0] === 'diff' && args[2] === 'origin/main...headSha') { + if (cmd === 'git' && args[0] === 'diff' && args[1] === '--name-only' && args[2] === '--no-renames' && args[3] === 'origin/main...headSha') { return 'file1.go\nfile2.go\n'; } if (cmd === 'git' && args[0] === 'merge-base' && args[1] === '--is-ancestor') { return ''; } - if (cmd === 'git' && args[0] === 'diff' && args[2] === 'approvedSha' && args[3] === 'headSha') { + if (cmd === 'git' && args[0] === 'diff' && args[1] === '--name-only' && args[2] === '--no-renames' && args[3] === 'approvedSha' && args[4] === 'headSha') { return 'file2.go\nfile3.go\n'; } throw new Error(`Unexpected command: ${cmd} ${args.join(' ')}`); @@ -579,7 +579,7 @@ test('getPrChangedFiles skips follow-up diff when approvedSha is unreachable or const originalExec = childProcess.execFileSync; let followUpCalled = false; childProcess.execFileSync = (cmd, args) => { - if (cmd === 'git' && args[0] === 'diff' && args[2] === 'origin/main...headSha') { + if (cmd === 'git' && args[0] === 'diff' && args[1] === '--name-only' && args[2] === '--no-renames' && args[3] === 'origin/main...headSha') { return 'file1.go\nfile2.go\n'; } if (cmd === 'git' && args[0] === 'merge-base' && args[1] === '--is-ancestor') { @@ -587,7 +587,7 @@ test('getPrChangedFiles skips follow-up diff when approvedSha is unreachable or err.status = 128; throw err; } - if (cmd === 'git' && args[0] === 'diff' && args[2] === 'approvedSha' && args[3] === 'headSha') { + if (cmd === 'git' && args[0] === 'diff' && args[1] === '--name-only' && args[2] === '--no-renames' && args[3] === 'approvedSha' && args[4] === 'headSha') { followUpCalled = true; return 'file3.go\n'; } @@ -606,13 +606,13 @@ test('getPrChangedFiles skips follow-up diff when approvedSha is unreachable or test('getPrChangedFiles propagates follow-up git diff failures instead of failing open', () => { const originalExec = childProcess.execFileSync; childProcess.execFileSync = (cmd, args) => { - if (cmd === 'git' && args[0] === 'diff' && args[2] === 'origin/main...headSha') { + if (cmd === 'git' && args[0] === 'diff' && args[1] === '--name-only' && args[2] === '--no-renames' && args[3] === 'origin/main...headSha') { return 'file1.go\n'; } if (cmd === 'git' && args[0] === 'merge-base' && args[1] === '--is-ancestor') { return ''; } - if (cmd === 'git' && args[0] === 'diff' && args[2] === 'approvedSha' && args[3] === 'headSha') { + if (cmd === 'git' && args[0] === 'diff' && args[1] === '--name-only' && args[2] === '--no-renames' && args[3] === 'approvedSha' && args[4] === 'headSha') { const err = new Error('git diff failed: ETIMEDOUT'); err.status = 1; throw err; @@ -630,13 +630,13 @@ test('getPrChangedFiles propagates follow-up git diff failures instead of failin test('getPrChangedFiles normalizes ignoreExitCode to false for git diff calls', () => { const originalExec = childProcess.execFileSync; childProcess.execFileSync = (cmd, args) => { - if (cmd === 'git' && args[0] === 'diff' && args[2] === 'origin/main...headSha') { + if (cmd === 'git' && args[0] === 'diff' && args[1] === '--name-only' && args[2] === '--no-renames' && args[3] === 'origin/main...headSha') { return 'file1.go\n'; } if (cmd === 'git' && args[0] === 'merge-base' && args[1] === '--is-ancestor') { return ''; } - if (cmd === 'git' && args[0] === 'diff' && args[2] === 'approvedSha' && args[3] === 'headSha') { + if (cmd === 'git' && args[0] === 'diff' && args[1] === '--name-only' && args[2] === '--no-renames' && args[3] === 'approvedSha' && args[4] === 'headSha') { return 'file2.go\n'; } throw new Error(`Unexpected command: ${cmd} ${args.join(' ')}`); @@ -652,6 +652,35 @@ test('getPrChangedFiles normalizes ignoreExitCode to false for git diff calls', } }); +test('getPrChangedFiles passes --no-renames across 3-dot, follow-up, and 2-dot git diff calls', () => { + const originalExec = childProcess.execFileSync; + const diffArgs = []; + childProcess.execFileSync = (cmd, args) => { + if (cmd === 'git' && args[0] === 'diff') { + diffArgs.push(args); + return '.github/workflows/lint.yml\ndocs/lint.yml.bak\n'; + } + if (cmd === 'git' && args[0] === 'merge-base' && args[1] === '--is-ancestor') { + return ''; + } + throw new Error(`Unexpected command: ${cmd} ${args.join(' ')}`); + }; + + try { + const threeDot = getPrChangedFiles('main', 'approvedSha', 'headSha'); + const twoDot = getPrChangedFiles('approvedSha', 'headSha'); + assert.deepStrictEqual(threeDot, ['.github/workflows/lint.yml', 'docs/lint.yml.bak']); + assert.deepStrictEqual(twoDot, ['.github/workflows/lint.yml', 'docs/lint.yml.bak']); + assert.deepStrictEqual(diffArgs, [ + ['diff', '--name-only', '--no-renames', 'origin/main...headSha'], + ['diff', '--name-only', '--no-renames', 'approvedSha', 'headSha'], + ['diff', '--name-only', '--no-renames', 'approvedSha', 'headSha'] + ]); + } finally { + childProcess.execFileSync = originalExec; + } +}); + test('getCommitModifiedFiles returns file list for commit from diff-tree', () => { const originalExec = childProcess.execFileSync; childProcess.execFileSync = (cmd, args) => { diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 701e2051e..a1c9981a7 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -278,6 +278,7 @@ jobs: ':(glob).github/scripts/**/*.js' \ ':(glob)tools/nightly_tests/**/*.js' \ ':(glob)cmd/experiment_state_server/static/**/*.js') + wait "$!" if [ "${#NONUI_JS_FILES[@]}" -gt 0 ]; then ./cmd/ui/node_modules/.bin/eslint --no-config-lookup -c tools/lint/eslint.nonui.config.mjs -- "${NONUI_JS_FILES[@]}" fi