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

Let AutoImportProvider find non-declaration files in wildcard exports... #56848

Merged
merged 5 commits into from
Jan 3, 2024

Conversation

andrewbranch
Copy link
Member

@andrewbranch andrewbranch commented Dec 21, 2023

...which is important for internal packages consumed by bundlers in monorepos, which typically point their package.json entries to TS implementation files.

Fixes #53116 (comment)

Comment on lines -186 to +190
Info seq [hh:mm:ss:mss] Files (5)
Info seq [hh:mm:ss:mss] Files (6)
/node_modules/pkg/a/a1.d.ts Text-1 "export const a1: number;"
/node_modules/pkg/b/b1.d.ts Text-1 "export const b1: number;"
/node_modules/pkg/b/b2.d.mts Text-1 "export const NOT_REACHABLE: number;"
Copy link
Member Author

Choose a reason for hiding this comment

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

I guess I could have kept the same strict extension filtering while mixing in the allowed TS implementation file extensions, keeping this unreachable file out of the AutoImportProviderProject compilation. But it doesn’t seem realistic for packages to ship declaration files that are literally unreachable, so doing the simpler thing and allowing all supported extensions seems fine. Updated the test to show that this file doesn’t result in a new auto-import.

@@ -2292,8 +2291,8 @@ function loadEntrypointsFromExportMap(
/*excludes*/ undefined,
[
isDeclarationFileName(target)
? replaceFirstStar(target, "**/*")
: changeAnyExtension(replaceFirstStar(target, "**/*"), getDeclarationEmitExtensionForPath(target)),
? changeAnyExtension(replaceFirstStar(target, "**/*"), ".*", supportedDeclarationExtensions, /*ignoreCase*/ true)
Copy link
Member

Choose a reason for hiding this comment

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

It seems like we are only filtering out replacing to *.* only if extension is of type ".d.someext.ts"
Was that intentional. I wonder if we can just always do changeAnyExtension(replaceFirstStar(target, "**/*"), ".*") irrespective of extension .

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 problem is changeAnyExtension(path, ".*") only replaces from the last ., so that would result in foo.d.ts becoming foo.d.*, when we need it to become foo.* instead.

Copy link
Member

Choose a reason for hiding this comment

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

but if i locally change this to do changeAnyExtension(replaceFirstStar(target, "**/*"), ".*"), none of the tests fail ?

Copy link
Member Author

Choose a reason for hiding this comment

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

You're right. There's currently no test where the exports target has a declaration file extension but we find a .ts on disk instead. While that should arguably work, it's not very realistic.

Copy link
Member

Choose a reason for hiding this comment

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

Presumably we don't have many tests with export maps pointing explicitly at declaration files with js or implementation files alongside them that we'd prefer to lookup - it's usually the other way around, so only finding matches for name.d.* gets us what we want almost all the time.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, exactly. Still, I running with an include of name.d.* feels too much like a mistake to me. I made a new helper changeFullExtension that recognizes and swaps all declaration file extensions (including ones for arbitrary file extensions, which the current code doesn't handle correctly), and am just trying to figure out how to push the commit I made on my host machine from my dev box 😅

export function changeFullExtension(path: string, newExtension: string) {
const declarationExtension = getDeclarationFileExtension(path);
if (declarationExtension) {
return path.slice(0, path.length - declarationExtension.length) + newExtension;
Copy link
Member

Choose a reason for hiding this comment

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

Does this need to handle presence or absence of "." in new extension like in changeAnyExtension : (startsWith(ext, ".") ? ext : "." + ext)

@andrewbranch andrewbranch merged commit 0ea57f6 into main Jan 3, 2024
19 checks passed
@andrewbranch andrewbranch deleted the bug/wildcard-exports-monorepo branch January 3, 2024 23:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Offer auto-imports for all files in package.json exports wildcard directories
3 participants