Skip to content

v10: in watch mode every recompiled file is read from disk a second time #1744

Done

Description

@johnnyreilly

Follow-up from the review of #1741 (the comment on layerFileContents, raised at 89b1b78 and still present at 5d4f461). Found by reading the code; the cost has not been measured on its own.

What happens

layerFileContents in src/typeScriptApi.ts decides whether a compiled file's text differs from what the TypeScript process reads from disk. Since #1741 it compares with the real disk (readDiskFile, a synchronous fs.readFileSync), because webpack's input file system can hold virtual modules the TypeScript process can't see. On next it read through webpack's cached input file system, which had just read the same file.

#1741 added a shortcut to avoid the extra read:

const diskContents =
  snapshotIsCurrent && !typeScriptInstance.everLayeredFiles.has(layerKey)
    ? contents
    : readDiskFile(fileName);
typeScriptInstance.everLayeredFiles.add(layerKey);

Every compiled file is added to everLayeredFiles, so the shortcut only applies the first time a file is compiled. From the second compile on, each recompiled file costs one uncached whole-file read on the loader thread.

Scenario

A branch switch in watch mode rebuilds 2,000 TypeScript files: 2,000 extra fs.readFileSync calls, which the comment above the code says it means to avoid ("reading every compiled file again here would only repeat that").

#1741's benchmark at 1,200 files shows +4.5% on "Incremental rebuild (hub touch)" against next, and its description flags that as the number to look at again. This read may be the cause; that has not been confirmed.

Possible fixes

  • Only add a file to everLayeredFiles when its layer actually differs from disk, so a file that always matches the disk keeps the shortcut.
  • Cache the disk text by mtime and size.
  • Read the disk only when webpack's input file system is not the real one, or the file is not a real file there.

Any of these needs to keep #1736's behaviour: a virtual module, or a file another loader transformed, must still be sent again to a restarted TypeScript process.

Done when

  • A rebuild that recompiles N unchanged-on-disk files does not do N extra synchronous disk reads.
  • virtualModuleWatch and the compilerProcessRestart* comparison tests still pass.
  • The hub-touch rebuild benchmark is re-run against next and the result recorded.

Activity

  1. johnnyreilly commented on Dec 1, 2026

    @johnnyreilly
    MemberAuthor

    Fixed on next by #1756 (6e7734f), listed under 10.0.0-beta.2 in the changelog.

    #1756 records which files were on disk, under the name webpack watches them by, when they were last compiled (filesSeenOnDisk). A file is looked at on disk when it is first compiled, when the text handed to the loader has changed, or when webpack's watcher has reported it; otherwise ts-loader keeps what it found the last time.

    Against "done when":


    Generated by Claude Code

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions