Skip to content

fix: clean dist/ before build:ts in TypeScript template#883

Open
winnrz wants to merge 1 commit intofastify:mainfrom
winnrz:fix/ts-template-clean-dist
Open

fix: clean dist/ before build:ts in TypeScript template#883
winnrz wants to merge 1 commit intofastify:mainfrom
winnrz:fix/ts-template-clean-dist

Conversation

@winnrz
Copy link
Copy Markdown

@winnrz winnrz commented Apr 23, 2026

What

Adds a clean script to the generated TypeScript template and runs it before tsc in build:ts, so stale compiled files are removed on every build.

Why

Closes #878. Without this, deleting a file from routes/ or plugins/ leaves a stale .js in dist/, which @fastify/autoload continues to serve — giving the misleading impression that the deletion had no effect. This is a non-obvious debugging problem for developers new to Fastify.

Changes

  • Added clean script using inline node -e (no new dependency, cross-platform)
  • Updated build:ts to run clean first
  • Added a logInstructions entry for clean so it's discoverable
  • Fixed a pre-existing typo in logInstructions: npm build:tsnpm run build:ts

The ESM TypeScript template inherits from typescriptTemplate via spread in cli(), so both --lang=ts and --lang=ts --esm pick up the change.

Verification

Stale file removal on a generated project:

$ mkdir -p dist && echo "stale" > dist/stale.js
$ ls dist/
stale.js
$ npm run clean
$ ls dist/ 2>/dev/null || echo "dist/ removed"
dist/ removed

Tests pass locally:

  • npm test passes end-to-end
  • npm run unit:ts-cjs
  • npm run unit:ts-esm

On Node 20 the template test suites are auto-skipped by the repo's should-skip-test-suites.js, but I ran unit:ts-cjs and unit:ts-esm directly and both pass.

Note

npm run build:ts currently fails on a freshly generated project due to an unrelated TS5011 rootDir error, which reproduces on main without this change. This PR does not affect that behavior — the clean step completes successfully before tsc is invoked.

Alternatives considered

  • rimraf: adds a dependency for what's essentially a one-liner
  • tsc --build --clean: requires incremental/project-reference builds, which the current template doesn't use

Checklist

Note on "tests and/or benchmarks are included": no new tests added because the change is to the generated scripts object, which isn't asserted on in the existing test suite. The existing TS template tests (unit:ts-cjs and unit:ts-esm) pass. Happy to add coverage if reviewers prefer.

Prevents stale compiled files from being served by @fastify/autoload
after a source file is deleted from routes/ or plugins/. The dist/
directory is now removed before tsc runs.

Also fixes a minor typo in logInstructions where 'npm build:ts'
should have been 'npm run build:ts', and adds a log line for the
new clean script.

Closes fastify#878
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add clean step to generated build:ts script to remove stale compiled routes

1 participant