Skip to content

Option to clean output only when input file deleted #70

Description

@aomarks

Problem

tsc, even in incremental mode, does not delete the output files corresponding to input files that have been deleted since its last build (see microsoft/TypeScript#30602 (comment)). For example:

  • Write foo.ts
  • Run tsc --build
    • Generates foo.js
  • Rename foo.ts to bar.ts
  • Run tsc --build
    • Generates bar.js, but foo.js still exists

Many build tools behave the same way.

Currently, if you have specified output, by default we delete all output files before running the script. This helps with the above problem, but makes it impossible to use efficient incremental modes, like tsc --build.

You can currently set clean: false to disable deleting before execution, however that means that stale outputs can still exist.

Proposal

Add a new on-delete option to clean which deletes output only if the unique set of matched input files for the script has changed since the last run.

This seems like it would provide a good balance between the two options we currently have; giving you incremental build every time a file is modified, or a new file is added -- but doing a clean build if a file is removed.

So the 3 options would now be:

  • true: Always delete before execution
  • false: Never delete before execution (but do still delete when restoring from cache)
  • "on-delete": Delete only if an input file changed

We could consider making "on-delete" the default, but true still seems like the safer default, because it's also very possible for a stale file to be left around due to a change in a config file (e.g. changing a rollup config to rename the bundle output file).

Example

{
  "build": {
    "command": "tsc --build",
    "files": [
      "src/**/*.ts"
    ],
    "output": [
      "lib/**",
      ".tsbuildinfo"
    ],
    "clean": "on-delete"
  }
}

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

Status
✅ Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions