From 307cadab1eca8a5edb66a75c2c24f1831bf4a6d9 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Date: Sun, 29 Nov 2026 04:03:08 +0100 Subject: [PATCH 1/2] chore(tooling): report dead code with knip (#1356) Add knip 6.45.1 as a pinned development dependency, a knip.json scoped to the main process, src/, harnesses/, workers/ and scripts/ (public/ is excluded: renderer files share globals through script tags), a `task deadcode` command and a non-blocking `deadcode` job in the test workflow. The report comes first; removals and making the job blocking follow in separate PRs. --- .ai/contexts/build-release.md | 18 +- .github/workflows/test.yml | 23 + Taskfile.yaml | 5 + docs/development.md | 47 +- knip.json | 32 + package-lock.json | 1162 +++++++++++++++++++++++++++++---- package.json | 5 +- test/deadcode-report.test.js | 63 ++ 8 files changed, 1224 insertions(+), 131 deletions(-) create mode 100644 knip.json create mode 100644 test/deadcode-report.test.js diff --git a/.ai/contexts/build-release.md b/.ai/contexts/build-release.md index fae2b4b7c..b059039cc 100644 --- a/.ai/contexts/build-release.md +++ b/.ai/contexts/build-release.md @@ -320,9 +320,9 @@ pushes to `main` and `release/*` run Node 22/Linux+Windows. Matrix keys remain ` `os`, with no job-name override, retaining contexts such as `test (22, ubuntu-latest)`. No step depends on a Node 20 or Windows/Node 20 cell. Linux real-bwrap enforcement stays on every Linux test cell; patch -coverage stays on PR Node 22/Linux only. Lint, docs-refs and e2e still run for every PR +coverage stays on PR Node 22/Linux only. Lint, deadcode, docs-refs and e2e still run for every PR and push to these branches; changelog and docs stay PR-only. Test jobs have 25 minutes; lint, -changelog, docs, docs-refs and e2e have 10 each. `docs-refs` runs +deadcode, changelog, docs, docs-refs and e2e have 10 each. `docs-refs` runs `node scripts/check-doc-refs.js` on Node 20 with no install step (the script has no dependency) and is blocking; its rules are in [changelog.md](../../docs/changelog.md#doc-references). @@ -349,6 +349,20 @@ The rule is in [changelog.md](../../docs/changelog.md#documentation-in-the-same- Review still assesses the prose. Adding the label or editing the reason needs a rerun. Required-check settings and label creation remain separate actions. +The `deadcode` job reports and never blocks (#1356, first step). It runs one +cell, Node 22 on `ubuntu-latest`, installs locked dependencies with +`npm ci --ignore-scripts` (knip needs no native rebuild and no Electron +download), then runs the locked knip with `npx --no-install`, so the version +is the one pinned in `package.json`. The markdown report goes to the log and to +the job summary through `tee`. The step sets `shell: bash` for pipefail, so +knip's non-zero exit still marks the step, and `continue-on-error: true`, so +the job and the workflow stay green whatever knip finds. Nothing `needs` it and +it is not a required check. Making it blocking is a later step of #1356, once +the report is clean. `test/deadcode-report.test.js` checks the pin, the job's +shape and that no gate task or job depends on it. What knip analyses and its +known false positives are in +[development.md](../../docs/development.md#dead-code-report). + Linux runs the full Node suite with coverage. Every non-Linux runner runs `node scripts/run-tests.js --platform-only` without c8; its matrix and job name stay unchanged, including `test (22, windows-2022)`. The coverage step diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 549525ce9..2beb62b71 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -35,6 +35,29 @@ jobs: - name: Run ESLint run: npm run lint + deadcode: + # see docs/development.md ("Dead-code report") + runs-on: ubuntu-latest + timeout-minutes: 10 + + steps: + - uses: actions/checkout@v4 + with: + persist-credentials: false + + - uses: actions/setup-node@v4 + with: + node-version: 22 + cache: npm + + - name: Install dependencies without lifecycle scripts + run: npm ci --ignore-scripts + + - name: Report unused files, exports and dependencies + continue-on-error: true + shell: bash + run: npx --no-install knip --no-progress --reporter markdown | tee -a "$GITHUB_STEP_SUMMARY" + changelog: # see docs/changelog.md ("The CI check") if: github.event_name == 'pull_request' diff --git a/Taskfile.yaml b/Taskfile.yaml index 40b7eec69..c66f82b73 100644 --- a/Taskfile.yaml +++ b/Taskfile.yaml @@ -100,6 +100,11 @@ tasks: - node scripts/check-doc-refs.js - node scripts/check-docs.js --validate + deadcode: + desc: "Report unused files, exports and dependencies with knip (report only; see docs/development.md)" + cmds: + - npx --no-install knip {{.CLI_ARGS}} + ci: desc: CI gate — runs test then lint, verbose, exits on first failure cmds: diff --git a/docs/development.md b/docs/development.md index 279d50b95..c27e1ae77 100644 --- a/docs/development.md +++ b/docs/development.md @@ -1,6 +1,7 @@ --- covers: - "Taskfile.yaml" + - "knip.json" - "package.json" - "public/codemirror-setup.js" - "public/mermaid-setup.js" @@ -50,6 +51,7 @@ run `npm run bundle:mermaid` again. | `task check` | Full `test` and `lint` gate before opening a PR, then the doc reference check (`scripts/check-doc-refs.js`, see [Doc references](changelog.md#doc-references)) | | `task ci` | `npm test`, then `npx eslint .`, in sequence | | `task coverage` | the suite under c8; text and lcov in `./coverage` | +| `task deadcode` | knip's report of unused files, exports and dependencies — see [Dead-code report](#dead-code-report) | | `task install:lint` | installs `eslint` and `jsdom` if missing | | `task build` | `npm run build:linux` — see [Building](#building) | | `task test-pr PR=` | a PR's code, isolated — see [Testing a PR live](testing-a-pr.md) | @@ -115,7 +117,50 @@ modifies must be 80 % covered (`diff-cover`), `main.js`, `preload.js`, the MCP bridge, `claude-auth.js`, `public/app.js`, `public/terminal-manager.js`, the CodeMirror and Mermaid files, workers, scripts, tests and `e2e/` excepted. The same workflow runs the [changelog check](changelog.md#the-ci-check) on pull -requests and the [end-to-end journeys](e2e.md). +requests and the [end-to-end journeys](e2e.md). Its `deadcode` job prints the +[dead-code report](#dead-code-report) and never fails. + +### Dead-code report + +`task deadcode` runs [knip](https://knip.dev), pinned to an exact version in +`devDependencies`, with `npx --no-install`: it uses the locked copy in +`node_modules` and never downloads another. Run `task install` first after +pulling the change that adds it. Arguments after `--` go to knip, for example +`task deadcode -- --production`. knip exits non-zero when it reports anything; +the report is information, not a gate, and no other task depends on it. + +`knip.json` declares what is analysed: + +- **Production entries**, marked `!`: `main.js` (the package `main`, found by + knip itself), `preload.js` and every `workers/*.js`. Workers are started with + `new Worker(path.join(__dirname, 'workers', …))`, a path knip cannot follow, + so they are listed by pattern. +- **Other entries**: every `scripts/*.js` and `scripts/*.cjs` (each one is run + by `package.json`, `Taskfile.yaml`, a workflow, a test or by hand from a doc, + or is required by one that is), + `test/**/*.test.js` and `e2e/**/*.spec.js`. +- **Project**: the root modules, `src/**`, `harnesses/**`, `workers/**` and + `scripts/**`. `public/**` is ignored: renderer files share globals through + `