Skip to content

Referencing module name using index when resolving module name can be incorrect #48229

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

Closed
sheetalkamat opened this issue Mar 11, 2022 · 5 comments · Fixed by #49738
Closed

Referencing module name using index when resolving module name can be incorrect #48229

sheetalkamat opened this issue Mar 11, 2022 · 5 comments · Fixed by #49738
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue

Comments

@sheetalkamat
Copy link
Member

We cannot assume that module name is referenced at index i in containing source file because we could be calling you with only few module names from the source file. https://github.com/microsoft/TypeScript/blob/main/src/compiler/program.ts#L1571 shows that case

Originally posted by @sheetalkamat in #45884 (comment)

@sheetalkamat
Copy link
Member Author

@weswigham

@weswigham
Copy link
Member

weswigham commented Mar 11, 2022

Hm, the called should just need to get updated to pass in ReferencedFiles then, rather than a raw name list, since the ReferencedFiles include the index in the original name list with which we can lookup the mode of the import. Similar to how getModuleNameStringLiteralAt's callers already use FileReferences.

In any case, do you know how this'd manifest in an issue so we can have a test?

@sheetalkamat
Copy link
Member Author

import {a} from "a"
import { b} from "./b"

I think in this case if b.ts was not present in first pass and then b.ts is created this could be true. Some resolutions can be reused but others need to recalculated in the file.

@weswigham
Copy link
Member

So I've spent far, far too long on this - and not on fixing it, no, that's easy. Writing a test that actually proves there's an issue that needs fixing is what's been burning away the hours.

So, your example. It's close to what you'd think would trigger an issue (you'd need to ensure the modes of both imports are different, too, to actually cause a problem, which means using type-only imports and assertions), given the way resolveModulesReusingOldState is written, but no - it's actually impossible for the resolveModuleNames callsite within to get a partial resolution list, at least with our built-in hosts and caches. This is because we only reuse old resolutions at all if the new file is the same as the old file, in which case we'd reuse every resolution - the only missing resolutions would be those which didn't originally resolve, as you suspect. However, when there is a missing resolution, we invalidate all resolutions in the file when any of those missing resolutions appear! (And thus recalculate all resolutions in the file from scratch.) Thus, there's no circumstance where we ever attempt to resolve a partial list of resolutions, as much as the code is evidently written to try to handle such as case.

So it's actually impossible to test my fix for this (which is fairly straightforward), because it's actually impossible to trigger a problem, at least without custom resolution caches and LS hosts that behave very differently from our builtin ones. This is a test you would expect to fail, but it's already passing because of the aforementioned over-invalidation done by our hosts.

So not sure what to do here, tbh.

sheetalkamat added a commit that referenced this issue Jun 29, 2022
@sheetalkamat sheetalkamat self-assigned this Jun 29, 2022
@sheetalkamat
Copy link
Member Author

Found the test case as part of my module resolution caching work

sheetalkamat added a commit that referenced this issue Jul 20, 2022
sheetalkamat added a commit that referenced this issue Jul 22, 2022
sheetalkamat added a commit that referenced this issue Jul 26, 2022
sheetalkamat added a commit that referenced this issue Jul 27, 2022
sheetalkamat added a commit that referenced this issue Jul 28, 2022
sheetalkamat added a commit that referenced this issue Aug 1, 2022
sheetalkamat added a commit that referenced this issue Aug 4, 2022
@DanielRosenwasser DanielRosenwasser added the Bug A bug in TypeScript label Sep 14, 2022
@DanielRosenwasser DanielRosenwasser added this to the TypeScript 4.9.0 milestone Sep 14, 2022
@typescript-bot typescript-bot added the Fix Available A PR has been opened for this issue label Sep 22, 2022
sheetalkamat added a commit that referenced this issue Nov 7, 2022
…rom file are partially used (#49738)

* Test showing wrong resolution is returned because of incorrect mode calculation
Test for #48229

* Pass in information for the module name resolution when resolutions from file are partially used
Fixes #48229

* Make the resolution info complete
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue
Projects
None yet
4 participants