diff --git a/.github/workflows/presubmit.yaml b/.github/workflows/presubmit.yaml index dc152e2bec88..c5de30eba54e 100644 --- a/.github/workflows/presubmit.yaml +++ b/.github/workflows/presubmit.yaml @@ -34,12 +34,14 @@ jobs: steps: - uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6.1.0 with: - fetch-depth: 300 + fetch-depth: 2 persist-credentials: false - name: Use Node.js 24 uses: actions/setup-node@249970729cb0ef3589644e2896645e5dc5ba9c38 # v6.5.0 with: node-version: 24 - run: npm install - - run: npm run lint + - run: node ./bin/linter.mjs --strict name: Run monorepo linter + env: + GIT_DIFF_ARG: "HEAD^1" diff --git a/bin/linter.mjs b/bin/linter.mjs index bd9a976ca944..2eb8645c0881 100755 --- a/bin/linter.mjs +++ b/bin/linter.mjs @@ -26,7 +26,13 @@ const tsconfigCache = new Map(); // --- Main Runner (Entry Point) --- async function run() { try { - const changedTsFiles = getChangedFiles(); + const isStrict = Boolean(process.argv.includes('--strict')); + let changedTsFiles; + if (isStrict) { + changedTsFiles = getChangedFilesStrict(); + } else { + changedTsFiles = getChangedFiles(); + } if (changedTsFiles.length === 0) { console.log('No TypeScript files changed. Skipping checks.'); @@ -63,16 +69,56 @@ function runGit(args, options = {}) { }); } +function getChangedFilesStrict() { + const gitDiffArg = process.env.GIT_DIFF_ARG; + + if (!gitDiffArg) { + throw new Error( + 'Strict mode is enabled, but GIT_DIFF_ARG environment variable or --git-diff-arg flag was not provided. ' + + 'Please set the GIT_DIFF_ARG environment variable or provide --git-diff-arg .' + ); + } + + console.log(`Strict mode enabled. Comparing using GIT_DIFF_ARG: ${gitDiffArg}`); + + const args = gitDiffArg.trim().split(/\s+/); + + try { + const output = runGit([ + 'diff', + '--name-only', + '--diff-filter=ACMRT', + ...args, + '--', + '*.ts', + ]); + return output + .split('\n') + .map(f => f.trim()) + .filter(f => f.length > 0 && existsSync(f)); + } catch (err) { + if (err.status !== 1) { + throw new Error( + `Strict mode error: git diff --quiet ${gitDiffArg} failed with exit code ${err.status}.\n` + + `Ensure that the git reference '${gitDiffArg}' exists locally and that you have fetched the required commits/branches.\n` + + `Details: ${String(err.stderr || err.message || '').trim()}` + ); + } + } +} + /** * Returns a list of changed TypeScript files comparing against target branches/references. */ function getChangedFiles() { const base = process.env.GITHUB_BASE_REF || 'main'; const refsToTry = [ + `origin/${base}...HEAD`, + `${base}...HEAD`, + `upstream/${base}...HEAD`, + `origin/${base}`, base, `upstream/${base}`, - `origin/${base}`, - 'FETCH_HEAD', 'HEAD~1', 'HEAD^', ];