microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
99.29k stars 12.31k forks source link

Incremental compilation does not work with relative paths for outDir and tsBuildInfoFile #41690

Open perbergland opened 3 years ago

perbergland commented 3 years ago

TypeScript Version: 4.2.0-dev.20201124

Search Terms: incremental relative absolute outdir

Code

https://github.com/perbergland/typescript-incremental-watch-bug

let triggerBug = false;

const myResolvePath = (s: string) => (triggerBug ? s : system.resolvePath(s));

const compilerOptions = (): ts.CompilerOptions => ({
  tsBuildInfoFile: myResolvePath(`${cachePath}/buildfile.tsbuildinfo`),
  incremental: true,
  noEmit: false,
  outDir: myResolvePath(`${cachePath}/out`),
  sourceMap: true,
});
...
const config = ts.getParsedCommandLineOfConfigFile(
    configPath,
    /*optionsToExtend*/ compilerOptions(),
    /*host*/ {
      ...ts.sys,
      onUnRecoverableConfigFileDiagnostic: (d) => writeDiagnosticMessage(d),
    }
  );

  const host = ts.createIncrementalCompilerHost(config.options, system);
  const program = ts.createIncrementalProgram({
...

Expected behavior:

Incremental compilation should work when outDir and tsBuildInfoFile are relative paths

Actual behavior:

Incremental compilation does not work when outDir and tsBuildInfoFile are relative paths - source files are emitted every time.

I have boiled it down to the lines above so that if I pass outDir and tsBuildInfoFile as relative paths, I can see that program.emit(undefined, ... ) will re-emit all source files on every call. If I instead pass these paths as resolved absolute paths, it works as expected.

./node_modules/.bin/ts-node-script compileTypescript.ts triggerbug
Typescript 4.2.0-dev.20201124
Triggering the bug by not resolving paths
reading buildinfo from output/buildfile.tsbuildinfo
emitAllAffectedFiles invoked
emitting output/out/Hello.js.map for /Users/per.bergland/Projects/typescript-incremental-watch-bug/src/Hello.tsx
emitting output/out/Hello.js for /Users/per.bergland/Projects/typescript-incremental-watch-bug/src/Hello.tsx
emitting output/buildfile.tsbuildinfo for ??
Emitting complete

contrast with

./node_modules/.bin/ts-node-script compileTypescript.ts 
Typescript 4.2.0-dev.20201124
reading buildinfo from /Users/per.bergland/Projects/typescript-incremental-watch-bug/output/buildfile.tsbuildinfo
emitAllAffectedFiles invoked
Emitting complete
sheetalkamat commented 3 years ago

I cant repro this with typescript@next

c:\temp\typescript-incremental-watch-bug>node compileTypescript.js
Typescript 4.2.0-dev.20210129
reading buildinfo from output/buildfile.tsbuildinfo
emitAllAffectedFiles invoked
emitting output/out/Hello.js.map for c:/temp/typescript-incremental-watch-bug/src/Hello.tsx
emitting output/out/Hello.js for c:/temp/typescript-incremental-watch-bug/src/Hello.tsx
emitting output/buildfile.tsbuildinfo for ??
Emitting complete
Incremental compilation succeeded

c:\temp\typescript-incremental-watch-bug>node compileTypescript.js
Typescript 4.2.0-dev.20210129
reading buildinfo from c:\temp\typescript-incremental-watch-bug\output\buildfile.tsbuildinfo
emitAllAffectedFiles invoked
emitting c:/temp/typescript-incremental-watch-bug/output/out/Hello.js.map for c:/temp/typescript-incremental-watch-bug/src/Hello.tsx
emitting c:/temp/typescript-incremental-watch-bug/output/out/Hello.js for c:/temp/typescript-incremental-watch-bug/src/Hello.tsx
emitting c:\temp\typescript-incremental-watch-bug\output\buildfile.tsbuildinfo for ??
Emitting complete
Incremental compilation succeeded