Skip to content

v10: false "Cannot find module" for an import of a file TypeScript only learns about later in the build (virtual modules) #1735

Done

Description

@johnnyreilly

Found while working on #1734. It is on next (e241f23) without that PR, and #1734 does not change it.

Expected Behaviour

A module that exists only in webpack's input file system (for example one supplied by a virtual-modules plugin) can be imported from a TypeScript file without a type error, as long as webpack can build it.

Actual Behaviour

The importer gets a false error on the first build:

TS2307: Cannot find module './generated' or its corresponding type declarations.

webpack builds both modules and the bundle is fine; only the type check is wrong.

Why, from reading the code: app.ts is compiled before generated.ts. At that point TypeScript has never been told about generated.ts, because it isn't on disk and its contents only reach TypeScript when ts-loader compiles it. The end-of-build recheck then doesn't revisit app.ts: an import that failed to resolve isn't recorded as one of its dependencies, so generated.ts arriving doesn't make app.ts a dependant of anything that changed.

The same should apply to any file whose contents TypeScript can only get from ts-loader, not just virtual modules. I have only reproduced the virtual-module case.

Steps to Reproduce the Problem

  1. app.ts:
    import { generated } from './generated';
    console.log(generated);
  2. No generated.ts on disk. A plugin makes webpack's inputFileSystem answer stat, readFile and so on for <context>/generated.ts with export const generated: number = 1;.
  3. tsconfig.json with "files": ["app.ts"], ts-loader with type checking on (not transpileOnly).
  4. Build once.

Result on next and on the #1734 branch: TS2307 on app.ts.

Location of a Minimal Repository that Demonstrates the Issue.

None yet. The plugin used to reproduce it is about 30 lines (a Proxy over compiler.inputFileSystem); it was written as a comparison test for #1734 and removed again, because it would have committed a snapshot of this false error.

Notes

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