Skip to content

fix(37165): Cannot read property 'flags' of undefined #37228

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
wants to merge 1 commit into from

Conversation

a-tarasyuk
Copy link
Contributor

@a-tarasyuk a-tarasyuk commented Mar 5, 2020

Fixes #37165

@sandersn sandersn added the For Milestone Bug PRs that fix a bug with a specific milestone label Mar 13, 2020
@sandersn sandersn requested a review from sheetalkamat March 13, 2020 21:23
Copy link
Member

@sandersn sandersn left a comment

Choose a reason for hiding this comment

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

globalObjectType is the type for Object and should not be undefined under normal circumstances. We should probably fix that instead of covering over the problem.

It's possible that it's undefined in some situations, but in that case, other uses of getGlobalType need to consider this possibility, and the situation needs to be documented, probably on the declaration of getGlobalType.

@a-tarasyuk
Copy link
Contributor Author

@sandersn Thanks for the feedback. 👍

The globalObjectType initialized in the line #35978. However, three is code above (Line #35963) which can refer to globalObjectType.

if (augmentations) {
    // merge _global_ module augmentations.
    // this needs to be done after global symbol table is initialized to make sure that all ambient modules are indexed
    for (const list of augmentations) {
        for (const augmentation of list) {
            if (!isGlobalScopeAugmentation(augmentation.parent as ModuleDeclaration)) continue;
            mergeModuleAugmentation(augmentation);
        }
    }
}

....

// Initialize special types
 globalArrayType = getGlobalType("Array" as __String, /*arity*/ 1, /*reportErrors*/ true);
 globalObjectType = getGlobalType("Object" as __String, /*arity*/ 0, /*reportErrors*/ true);

As I  remember correctly the chain which causes the crash looks like so
 mergeModuleAugmentation
-> resolveExternalModuleSymbol
-> resolveSymbol
-> resolveAlias
-> getTargetOfAliasDeclaration
-> getTargetOfExportAssignment
-> getTargetOfAliasLikeExpression
-> resolveEntityName
-> resolveName
-> resolveNameHelper
-> getSymbol
-> resolveAlias
-> getTargetOfAliasDeclaration
-> getTargetOfImportSpecifier
-> getExternalModuleMember
-> getPropertyOfType

I'll check maybe need to throw error earlier without checking to existence globalObjectType.

@sheetalkamat
Copy link
Member

Looked into this and it turns out this happens because of export as namespace and module agumentations.In the repro when trying to create new merged global of Bar we need to resolve what it is (which results in finding Bar from foo) but this happens before global object type and others are calculated resulting in accessing globalObjectType (because Foo doesn't have Bar so we are trying to figure out if its from global object type).

This raises question of all these globalTypes that we calculate being accessed before and needs to be thought through?

Note that we have similar situation which does get guarded unintentionall... but using undefined instead of getting correct globalObjectType would mean that we would report error which is questionable? In repro scenario we would get this error, but if we comment our Bar declaration from the global we wouldn't.

bar.d.ts:1:10 - error TS2305: Module '"./foo"' has no exported member 'toString'.

1 import { toString as Bar } from './foo';
           ~~~~~~~~


Found 1 error.

@a-tarasyuk
Copy link
Contributor Author

@sheetalkamat Thanks for the detailed feedback 👍. What solution do you think is acceptable for this issue?

@sheetalkamat
Copy link
Member

@ahejlsberg @weswigham @RyanCavanaugh @DanielRosenwasser to comment on what to do when hitting globalObject type before its populated (during module augmentation where the global*types are not yet calculated but module augmentation might use them)

@DanielRosenwasser
Copy link
Member

I think the right thing is to report an error and see what breaks in test suites, and ask partner teams to try it out.

@sandersn
Copy link
Member

@a-tarasyuk can you try reporting an error, and then we'll pass this build on to some partners if the results on our tests are good?

@sheetalkamat
Copy link
Member

@sandersn #37964 is preferred solution and needs more validation of user and rwc tests which i havent gotten to yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
For Milestone Bug PRs that fix a bug with a specific milestone
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Cannot read property 'flags' of undefined
4 participants