Skip to content

Enable path completions for node12/nodenext #47836

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

Merged
merged 3 commits into from
Feb 11, 2022

Conversation

weswigham
Copy link
Member

Fixes #47011

Just a matter of swapping some checks in stringCompletions to look for any node-like mode rather than just node. Did a quick check, and there don't seem to be any other resolution-mode dependent codepaths in services.

@weswigham
Copy link
Member Author

Oh, and @andrewbranch - it looks like path completions don't use any caching when looking up package.json files (and this isn't exclusive to new resolution modes); I know in #47388 you made the package json cache more accessible in services - maybe we want to, separately from this fix, also use it in the path completions codepath?

Copy link
Member

@andrewbranch andrewbranch left a comment

Choose a reason for hiding this comment

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

I think what’s going to happen here is that while the package name will be completed correctly, if you type a / afterwards, you’re going to get a listing of directories, which may be blocked due to the export map. There’s some logic in this file that specifically deals with completing directories that needs to be swapped for completing export map subpaths when relevant.

@weswigham weswigham force-pushed the path-completions-for-nodenext branch from 0c14ad8 to f608488 Compare February 10, 2022 22:31
@weswigham
Copy link
Member Author

@andrewbranch done - in nodenext and node12, completions are explicitly pulled from an export map, if available, rather than disk. This is maybe awkward for pattern/directory exports, since we won't complete anything in the pattern (but we will yield up the pattern for you to substitute as you please). We could hit disk and try to resolve the value of that export map key as a glob-ish-thing and generate a list of possible options, but it gets kinda hairy once conditions come into play (since every condition could have differing results and search in different locations).

}
const packageFile = combinePaths(ancestor, "node_modules", packagePath, "package.json");
if (tryFileExists(host, packageFile)) {
const packageJson = readJson(packageFile, host as { readFile: (filename: string) => string | undefined });
Copy link
Member

Choose a reason for hiding this comment

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

Did you know this function creates a SourceFile with parse diagnostics?? 😵

Copy link
Member Author

Choose a reason for hiding this comment

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

Hm, I buy it - our json document parsing is more lenient than the JSON.parse one because we allow comments and trailing commas, so I can understand preferring to use it.

Copy link
Member

Choose a reason for hiding this comment

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

npm crashes on comments and trailing commas. My strong suspicion is that readJson leaked into package.json-reading code only because it nicely avoids the need for a try/catch and it’s totally non-obvious that it’s doing so much extra work. I’ve been meaning to rename it and use a wrapped JSON.parse on package.jsons. It might make a little bit of a perf difference in module resolution when you have a lot of node_modules.

Copy link
Member Author

Choose a reason for hiding this comment

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

That's probably true; but I mean, it's probably most important to just use the cached package.json results where possible first 😅

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, definitely.

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
None yet
Development

Successfully merging this pull request may close these issues.

No path completions in --module nodenext
3 participants