Skip to content

TypeScript renames modules when imported incorrectly #4719

Closed

Description

While working on a larger project I encountered a strange behaviour of the TypeScript compiler that broke my project and was quite difficult to figure out. I've fiddled around and was able to break down the problem to the following simple example.

Assume a simple TypeScript project with two modules moduleA.ts and moduleB.ts with the first one importing something from the second one but with an incorrect cased filename within the import statement like this:

moduleA.ts

import {myFunction} from "./ModuleB"
myFunction();

moduleB.ts

export function myFunction() {}

When compiling this project with the beta version of TypeScript 1.6 on NPM I receive these output files:

  • moduleA.js
  • ModuleB.js

The compiler does not complain about the misspelled "./ModuleB" but simply renames the output file of moduleB.ts to ModuleB.js. If you reverse the example (put contents of moduleA into moduleB.ts and vice versa, misstype ./ModuleA) the compiler will not change the case of the output file names.

As there is no error for importing unknown modules (as reported in #4561) this error becomes realy hard to solve, imagine somewhere else in the project importing moduleB but this time using the correct spelling. This will break the code.

moduleC.ts

import {myFunction} from "./moduleB"
myFunction();

Activity

  1. mhegazy commented on Sep 10, 2015

    @mhegazy
    Contributor

    Similar to the issue reported in #3626 and #2011. we need a compiler option to make these casing mismatches an error.

  2. sebastian-lenz commented on Sep 10, 2015

    @sebastian-lenz
    Author

    Thank you for your fast response. I searched through the issues but these two didn't show up. Your description of how TypeScript resolves the output file names in the second issue was very interesting and perfectly explains my initial problem.

    So the real problem arrises cause TypeScript aligns its case sensitiveness to the file system it is currently running on while the underlying require call of an import statement is case sensitive on all platforms.

  3. mhegazy commented on Sep 10, 2015

    @mhegazy
    Contributor

    yes. it has been a pain point, and usually will take a few hours to figure out the root cause. As mentioned in #3626, we should have a flag to make it an error.

  4. vladima commented on Oct 27, 2015

    @vladima
    Contributor

    #5275 introduces a flag forceConsistentCasingInFileNames - when it is set compiler will report an error if some file has multiple references with inconsistent casing inside the program

  5. added
    Fixed
    on Oct 27, 2015
  6. vladima commented on Oct 27, 2015

    @vladima
    Contributor

    fixed in e811fec

  7. sebastian-lenz commented on Oct 28, 2015

    @sebastian-lenz
    Author

    🎉

  8. locked and limited conversation to collaborators on Jun 19, 2018
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

    BugFixed

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions