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

Suggestion: case-sensitive imports #21736

Open
ccorcos opened this Issue Feb 7, 2018 · 9 comments

Comments

Projects
None yet
6 participants
@ccorcos
Copy link

ccorcos commented Feb 7, 2018

TypeScript Version: 2.7.0-dev.201xxxxx

My mac does not have case sensitive imports. That is you can import the file x.js as ./X.js and everything will work. However, our servers run on linux (like most) and I got a runtime exception that took down the whole server because linux imports are case sensitive.

I think this would be an awesome addition to Typescript to prevent fatal mistakes that are hard to catch like this one.

Related Issues:

#14460

@mhegazy

This comment has been minimized.

Copy link
Contributor

mhegazy commented Feb 7, 2018

--forceConsistentCasingInFileNames should catch some of the scenarios involved here. it does not catch all of them though.

@ccorcos

This comment has been minimized.

Copy link
Author

ccorcos commented Feb 7, 2018

Oh interesting. I misunderstood what that meant. It doesn't stop you from creating a new file and importing it in a single place with the wrong case though.

@Jessidhia

This comment has been minimized.

Copy link
Contributor

Jessidhia commented Feb 8, 2018

There actually is an option that can be given to the tsservice API (useCaseSensitiveFileNames: () => false) to make it be strict with those -- the problem is that it seems that, somewhere, internally, imports are converted to all lower case before they are resolved, which makes things impossible to compile.

@mhegazy

This comment has been minimized.

Copy link
Contributor

mhegazy commented Feb 8, 2018

@Kovensky there are two parts involved, the comparisons which is managed by useCaseSensitiveFileNames and the file system operations. ideally u want the lookup for a module with the wrong case to fail all the time, and not only on a case-sensitive file system. Ideally you want --forceConsistentCasingInFileNames to get fs.realPath and verify it does match the module name used to locate it.

@mhegazy

This comment has been minimized.

Copy link
Contributor

mhegazy commented Feb 8, 2018

I should add the reason why we did not do that is realPath has negative perf implications.

@ccorcos

This comment has been minimized.

Copy link
Author

ccorcos commented Feb 8, 2018

I see. Well it sounds like you guys know what you're doing 👍

It looks like a decent performance hit everywhere I've checked. There's a webpack plugin but it makes several calls to the filesystem:

https://github.com/Urthen/case-sensitive-paths-webpack-plugin/blob/master/index.js

I'm not sure how the TypeScript internals work, but it seems like the performance hit wouldn't be too bad if it were a compile-time check on the import statements rather than actually forcing an error on importing the file. At the very least, we can document it and improve it over time.

@tomgruner

This comment has been minimized.

Copy link

tomgruner commented Sep 4, 2018

Can I upvote this issue?

I spent hours trying to figure out why my TeamCity build was failing only to realize that the project had some imports that were the wrong case!

Quite an unexpected gotcha as I thought OSX would not import the file if it was the wrong case.

@brandonaaskov

This comment has been minimized.

Copy link

brandonaaskov commented Nov 8, 2018

Just to echo the others here, we were able to run our codebase locally on our macs but it fell over on Heroku because of the wrong casing for the filename, which wasn't obvious and continues to be something we have to keep a keen eye out for (aka old school linting).

@briandesousa

This comment has been minimized.

Copy link

briandesousa commented Feb 8, 2019

Similar experience here with a build succeeding on Windows 10 and failing on RHEL 7.6 with a "Cannot find module" error. Took a while to figure out it was a case error on the import. I also tried using the forceConsistentCasingInFileNames setting in my tsconfig.json but it did not catch this.

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