Skip to content
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

Fix auto import file extensions with package.json imports wildcards #59564

Merged
merged 3 commits into from
Aug 9, 2024

Conversation

andrewbranch
Copy link
Member

Fixes #59553

Also discovered and fixed a related bug where auto-imports could give you a .d.ts extension when resolving through "imports".

@typescript-bot typescript-bot added Author: Team For Milestone Bug PRs that fix a bug with a specific milestone labels Aug 8, 2024
//// external/*exports*/;

verify.importFixModuleSpecifiers("imports", ["#add.ts"]);
verify.importFixModuleSpecifiers("exports", ["pkg/external.js"]);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using .ts extensions where possible is limited to "imports", and doesn’t take effect for "exports". We may have to revisit this in the coming years if npm fills up with TS-only packages. We do not currently offer auto-imports for self-name imports (which are resolved through your own package.json "exports"), which is a place where it might make sense to respect allowImportingTsExtensions, so this may need to be revisited in the future as well.

//// add/*imports*/;
//// external/*exports*/;

verify.importFixModuleSpecifiers("imports", ["#add.js"]);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was "#add.d.ts" before this PR 😱

@@ -980,19 1007,23 @@ function tryGetModuleNameFromExportsOrImports(options: CompilerOptions, host: Mo
return { moduleFileToTry: combinePaths(packageName, fragment) };
}
if (declarationFile && containsPath(pathOrPattern, declarationFile, ignoreCase)) {
const fragment = getRelativePathFromDirectory(pathOrPattern, declarationFile, /*ignoreCase*/ false);
const fragment = changeFullExtension(getRelativePathFromDirectory(pathOrPattern, declarationFile, /*ignoreCase*/ false), getJSExtensionForFile(declarationFile, options));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldnt this be dependent of file case sensitvity?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure looks like it!

Copy link
Member Author

@andrewbranch andrewbranch Aug 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or the containsPath call should be hard-coded to false, or it might be more complicated than that... pathOrPattern is a concatenation of a path and an "exports" string key, and Node’s resolution algorithm is case sensitive according to my reading. We may be doing this wrong all around.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

paths and case-sensitivity .. ugh..

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh wait, it’s an "exports" value, not a key. Yeah, I think the whole thing should use the FS case sensitivity.

Copy link
Member Author

@andrewbranch andrewbranch Aug 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, this possible bug only occurs under MatchingMode.Directory, which was deprecated in Node.js 14 and EOL in 17, so I’m not going to touch this. I added a test for case sensitivity for * patterns and it passed without changes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

vfs is case insensitive by default. Dont you need to test it on "case sensitive" that its not offered or doesnt do some wierd case replacement.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test I added shows that a difference in case between the VFS and the paths written in package.json doesn’t prevent auto-imports from working, which is what’s most important IMO. Also, this PR doesn’t touch/change anything about case sensitivity. This ignoreCase argument was already there.

@andrewbranch andrewbranch merged commit 4b12d82 into microsoft:main Aug 9, 2024
32 checks passed
@andrewbranch andrewbranch deleted the bug/59553 branch August 9, 2024 20:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Author: Team For Milestone Bug PRs that fix a bug with a specific milestone
Projects
Status: Done
4 participants