Skip to content

API. CreateWatchProgram emits all files even when they have not changed #36223

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Yegorich555 opened this issue Jan 16, 2020 · 4 comments
Closed
Labels
Question An issue which isn't directly actionable in code

Comments

@Yegorich555
Copy link

TypeScript Version: 3.7.4

Search Terms:
ts.createWatchProgram is writing to disk too often; ts.createWatchProgram writes even no changes
Code

const ts = require("typescript");

// creating hooks
const sysConfig = { ...ts.sys };

sysConfig.writeFile = function writeFileWrapper(path) {
  console.warn("write", path);
  return ts.sys.writeFile(...arguments);
};

sysConfig.readFile = function readFile(path) {
  console.warn("read", path);
  return ts.sys.readFile(...arguments);
};

const host = ts.createWatchCompilerHost(
  ["./src/mockServer.ts"],
  {
    esModuleInterop: true,
    allowJs: true,
    skipLibCheck: true,
    outDir: "./dist",
    module: ts.ModuleKind.CommonJS
    // todo wait for transpileOnly option: https://github.com/microsoft/TypeScript/issues/29651
  },
  sysConfig,
  ts.createSemanticDiagnosticsBuilderProgram
);
ts.createWatchProgram(host);

Expected behavior:
One file changed - one writing on disk
One file re-saved but not changed - no writing on disk

Actual behavior:
One file changed - every file is written on disk

Playground Link:

  1. clone the repo: https://github.com/Yegorich555/webpack-mock-server/tree/tsError
  2. npm i
  3. npm run serve
  4. try to re-save any /src/*.ts file
  5. take a look at console under write tags

Related Issues:
#29927

Notes:

  1. tsc --watch works as expected, but ts.createWatchProgram doesn't work as expected
  2. screenshot with explanation:
    image

Additional question

  1. How can I detect that compilation is done? Does compilationCallback exist? diagnostic callback looks like has a different meaning.
@sheetalkamat
Copy link
Member

You are using createSemanticDiagnosticsBuilderProgram which is meant only to get errors efficiently for changed files. Emit of files is not tracked.. To support emit you need to use createEmitAndSemanticDiagnosticsBuilderProgram

@DanielRosenwasser DanielRosenwasser added the Question An issue which isn't directly actionable in code label Jan 16, 2020
@Yegorich555
Copy link
Author

Yegorich555 commented Jan 17, 2020

Oh, thanks a lot. I spent a some days on investigation and missed this. createEmitAndSemanticDiagnosticsBuilderProgram really works fine now.

One question I have, how can I detect in a perfect way that was changes and compilation done: it should be a callback for restarting compiled files.
Right now I detect ts.sys.writeFile and in diagnosticReport restart child_process, but it looks not pretty.
Could you suggest a smarter way?

@sheetalkamat
Copy link
Member

https://github.com/microsoft/TypeScript/wiki/Using-the-Compiler-API#writing-an-incremental-program-watcher shows how to use the API. reportWatchStatusChanged is called with diagnostics for starting compilation and watch complete diagnostics.

@Yegorich555
Copy link
Author

Thanks a lot. I use this as I mentioned upper

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

3 participants