Skip to content

chore(tooling): report dead code with knip (#1356) - #1367

Merged
devsuitup merged 2 commits into
mainfrom
chore/1356-knip-report
Nov 29, 2026
Merged

devsuitup merged 2 commits into
mainfrom
chore/1356-knip-report

Conversation

@devsuitup

Copy link
Copy Markdown
Owner

Summary

First step of #1356: dead-code detection with knip, report only. Nothing is
removed and nothing blocks; removals and the blocking switch come in later PRs.

  • knip 6.45.1 pinned exactly in devDependencies; package-lock.json
    regenerated with npx npm@10 install --package-lock-only.
  • knip.json at the root, scoped to the main process.
  • task deadcode runs the locked knip (npx --no-install knip, arguments after
    -- pass through).
  • New deadcode job in .github/workflows/test.yml: Node 22 on
    ubuntu-latest, npm ci --ignore-scripts, knip's markdown report to the log
    and the job summary. The step has continue-on-error: true, so the job stays
    green. Nothing needs it, and it must not be added to the required checks.

knip version

6.45.1 (published 2026-11-11) is the newest release at least 14 days old on
2026-11-29. 6.46.0 and later are under 14 days old. Engines: ^20.19.0 || >=22.12.0,
so it supports Node 20 and 22; the job runs on Node 22 only. The lock adds 58
packages, all from knip's tree (oxc-parser / oxc-resolver and their
per-platform bindings, @emnapi/*, formatly, smol-toml, unbash, ...). It
removes none. Two shared packages move up a minor version to meet knip's ranges,
and both stay inside their other dependents' ranges: jiti 2.6.1 -> 2.7.0
(app-builder-lib wants ^2.4.2) and tinyglobby 0.2.15 -> 0.2.17 (node-gyp
wants ^0.2.12). npm also sorted devDependencies alphabetically, which moves
mermaid above png2icons.

Config choices

  • Production entries (!): main.js (the package main, which knip detects
    itself), preload.js and workers/*.js. Workers are started with
    new Worker(path.join(__dirname, 'workers', ...)), so they are listed by
    pattern.
  • Other entries: scripts/*.{js,cjs}. Every script is run by package.json,
    Taskfile.yaml, a workflow, a test or by hand from a doc, or is required by
    one that is. Also test/**/*.test.js and e2e/**/*.spec.js.
  • Project: root *.js, src/**, harnesses/** (shipped by electron-builder),
    workers/**, scripts/**. public/** is ignored.
  • Tests are entries, so an export used only by a test counts as used: a module
    kept alive by its unit test is not dead code. task deadcode -- --production
    shows what only tests keep alive (380 exports). It is not run in CI.
  • ignoreDependencies: the packages only the renderer loads (the five xterm
    addons and dompurify from public/index.html, codemirror bundled from
    public/codemirror-setup.js). Also three packages tests reach on purpose
    without listing them: css-tree, resolved through jsdom's own path, and
    app-builder-lib / builder-util, for electron-builder's macro helpers in
    test/build-workflow.test.js.
  • Dynamic loading: the worker files (built with path.join) are entries.
    require(path.join(worktree, 'node_modules', 'electron')) in
    scripts/test-pr.js loads a package, not a local file. Tests that read
    modules as source (for example test/atomic-write-callers.test.js) only read
    files that are also required normally, so no extra entry or ignore was needed.

Report triage (default mode, the CI job's report)

Category Count Notes
Unused files 0 also 0 with --production
Unused dependencies 0 7 false positives fixed by config (renderer-only)
Unlisted dependencies 0 4 false positives fixed by config (css-tree x2, app-builder-lib, builder-util)
Unused exports, truly unneeded 55 the export has no consumer outside its file; every one is still used inside its own module, so this is export-surface cleanup, not dead code. Follow-up candidates.
Unused exports, false positives left 45 knip 6.45.1 does not follow CommonJS namespace access: const ns = require('./x'); ns.name, const { name } = ns, or module.exports = { ...ns } in test/fixtures/trigger-watcher.cjs. One more (rewriteJsonlAtomic) is reached through a test that loads the module as source. Also measured on 6.49.0 with a three-file probe. Not fixable by config.

The 55 include names that collide with unrelated locals elsewhere (for example
DEFAULT_DEBOUNCE_MS, SESSION_ID_RE, asEditorText, which
public/viewer-panel.js defines again). Two ambiguous .name matches turned
out unrelated: actions.refusal is a factory result, and w.PERMISSION_MODES
is a renderer stub. Before any removal PR, check each reported export for the
namespace pattern; docs/development.md says so.

Docs

  • User: docs/development.md, which adds task deadcode to the command table
    and a "Dead-code report" section (entries, project, ignores, production mode,
    the known false positive), and knip.json to its covers.
  • Agent: .ai/contexts/build-release.md, which describes the deadcode job
    (one cell, install without scripts, continue-on-error, not required) and
    adds it to the job and timeout lists.

Changelog

no-changelog: tooling only, users see nothing.

Tests

  • Red first: test/deadcode-report.test.js failed before the Taskfile and
    workflow changes (not ok 3 ... 'task deadcode exists',
    not ok 4 ... 'test.yml has a deadcode job'), then passed 4/4.
  • Mutations, each restored, each turned its test red: knip pinned as
    ^6.45.1; public/**/*.js added to project; check depending on
    deadcode; continue-on-error removed; npm ci without --ignore-scripts.
  • Node 22.23.5, --test --test-concurrency=1 --test-timeout=120000, one at a
    time, all exit 0: deadcode-report 4/4, build-workflow 17/17,
    check-changelog 21 (2 skipped), check-doc-refs 16/16, check-docs 99/99,
    check-fast 17/17, test-pr 298/298, viewer-mermaid 83/83.
  • node scripts/check-docs.js --validate, node scripts/check-doc-refs.js
    (0 failures), npx --no-install eslint test/deadcode-report.test.js,
    git diff --cached --check, SKIP_TESTS=1 task check:fast: all exit 0.
  • knip report: npx --yes knip@6.45.1 from the npx cache, about 11 s.

Not verified:

  • The deadcode job itself has not run yet; this PR's CI is its first run.
  • task deadcode locally: knip is not installed in this checkout's
    node_modules yet, so npx --no-install knip refuses (by design) until
    npm install.
  • Locally, knip also reports knip as an unlisted binary (in Taskfile.yaml
    and test.yml) because it is not installed here. After npm ci that finding
    should go away; this has not been measured.
  • The full task check suite was not run.

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.
@devsuitup devsuitup added the no-changelog The PR changes nothing a user sees; the CHANGELOG.md check is waived label Nov 29, 2026
@devsuitup
devsuitup merged commit ca80786 into main Nov 29, 2026
15 checks passed
@devsuitup
devsuitup deleted the chore/1356-knip-report branch November 29, 2026 03:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

no-changelog The PR changes nothing a user sees; the CHANGELOG.md check is waived

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant