Skip to content

Conversation

@andrewbranch
Copy link
Member

Fixes #45874, which was a regression of #45354. The hard work of this PR was reverse engineering a repro. The fix here is a little dirty and maybe narrower than we want—mostly posting this to get the failing test up and start deciding collectively how we want to address it.

const namespaceName = getFullyQualifiedName(namespace);
const declarationName = declarationNameToString(right);
const suggestionForNonexistentModule = getSuggestedSymbolForNonexistentModule(right, namespace);
const exportedTypeSymbol = getMergedSymbol(getSymbol(getExportsOfSymbol(namespace), right.escapedText, SymbolFlags.Type));
Copy link
Member

Choose a reason for hiding this comment

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

So the original change also added this. This means that while we're doing symbol merging, we're asking for the merged symbol. Is this also a problem?

Copy link
Member Author

Choose a reason for hiding this comment

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

Nope.

const containingQualifiedName = isQualifiedName(name) && getContainingQualifiedNameNode(name);
const canSuggestTypeof = containingQualifiedName && !isTypeOfExpression(containingQualifiedName.parent) && tryGetQualifiedNameAsValue(containingQualifiedName);
// Can't pull on types if global types aren't initialized yet
const canSuggestTypeof = !!globalObjectType && containingQualifiedName && !isTypeOfExpression(containingQualifiedName.parent) && tryGetQualifiedNameAsValue(containingQualifiedName);
Copy link
Member

@DanielRosenwasser DanielRosenwasser Oct 21, 2021

Choose a reason for hiding this comment

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

It's also weird that we never check for the appropriate SymbolFlags being passed in, right? There's no way you would've done this work if we strictly were looking for SymbolFlags.Type. I think this was overlooked in the follow-up code since there is a check for meaning & SymbolFlags.Namespace below.

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. In order to trigger the bug, the symbol has to not exist by the meaning of the original lookup, but exist in the value meaning, and it has to be part of a qualified name on the right-hand of an import equals, at least in the stack trace we're exclusively seeing in reports. I think the implicit assumption in this code was that if the symbol wasn't found in the lookup but exists by value, we must have been looking for a type, which of course is not true when we’re looking for namespaces exclusively.

There is definitely some logical cleanup that should ideally happen around that, but I’m a little concerned because it would fix this particular repro, but is pretty far removed from the root cause, so I’m not confident it would fix all instances of this crash. There could be some other entrypoint to it that doesn’t come from resolving an import = entity name.

Copy link
Member

@DanielRosenwasser DanielRosenwasser 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 this is an okay band-aid, but I would really like to see a follow-up PR that cleans up this section of the code and does "the right" stuff depending on the symbol, avoids doing work until TS hits the branch, etc.

@andrewbranch
Copy link
Member Author

cleans up this section of the code and does "the right" stuff depending on the symbol, avoids doing work until TS hits the branch, etc.

Did this in the latest commit, though I’m still not convinced checking for globalObjectType presence is the “right” fix, but it’s better than the crash.

@andrewbranch andrewbranch merged commit 3519af0 into microsoft:main Oct 25, 2021
@andrewbranch andrewbranch deleted the bug/45874 branch October 25, 2021 17:53
mprobst pushed a commit to mprobst/TypeScript that referenced this pull request Jan 10, 2022
…ft#46471)

* Add failing test

* Dumb fix

* Compute error message info more lazily

* One more laziness
@microsoft microsoft locked as resolved and limited conversation to collaborators Oct 21, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TS Server fatal error: Cannot read property 'flags' of undefined

2 participants