Skip to content

Harden node builder APIs to no longer return undefined for a node when NodeBuilderFlags.IgnoreErrors is provided #38273

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 1 commit into from
May 4, 2020

Conversation

weswigham
Copy link
Member

Fixes #32864

The root cause of the crash in #32864 was a very trivial error - at a callsite in signatureHelp.ts, we neglected to pass in our normal set of NodeBuilderFlags that's supposed to guarantee we always get a node back. Upon seeing this, I've done two things:

  • Changed the API so passing the flags parameter is required (it can still explicitly be undefined, and is such in many codefixes, which hopefully can handle the resulting possibly undefined result)
    • In doing so, I found a few more places in the symbol builder where we were neglecting to provide flags, where we now provide flags.
  • Audited all usages of context.encounteredError in the node builder to ensure all places where we set it are guarded by a check of a flag included in NodeBuilderFlags.IgnoreErrors, this way we know that if we provide that flag, even if the results might not be valid JS, they will, at least, exist as a node tree

I've confirmed this fixes the repro provided in #32864, and have an appropriately similar fourslash test. Interestingly, it points out that our signature help in the presence of overloads is a little odd - the signature that is the currently "chosen" overload has its type parameters erased, while other overloads have their type parameters left in-place (a requirement for triggering the crash was that the signature must have had type parameters, which, because of this quirk, can only happen on overloaded signatures).

…hen `NodeBuilderFlags.IgnoreErrors` is provided
@@ -2040,23 +2040,23 @@ declare namespace ts {
getNonNullableType(type: Type): Type;
getTypeArguments(type: TypeReference): readonly Type[];
/** Note that the resulting nodes cannot be checked. */
typeToTypeNode(type: Type, enclosingDeclaration?: Node, flags?: NodeBuilderFlags): TypeNode | undefined;
typeToTypeNode(type: Type, enclosingDeclaration: Node | undefined, flags: NodeBuilderFlags | undefined): TypeNode | undefined;
Copy link
Member

Choose a reason for hiding this comment

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

This would be a break if we want it for 3.9. Any way we can avoid it?

Copy link
Member Author

@weswigham weswigham Apr 30, 2020

Choose a reason for hiding this comment

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

Easily - now that I've used it to audit our internal usages, swapping it back is pretty simple; changing it is just for safety at callsites (so we may still want it for the 4.0 branch, where we may eventually add more).

@weswigham weswigham added this to the TypeScript 3.9.2 milestone Apr 30, 2020
@@ -4211,8 +4211,12 @@ namespace ts {
context.flags &= ~NodeBuilderFlags.InTypeAlias;

if (!type) {
context.encounteredError = true;
return undefined!; // TODO: GH#18217
if (!(context.flags & NodeBuilderFlags.AllowEmptyUnionOrIntersection)) {
Copy link
Member

Choose a reason for hiding this comment

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

As we discussed offline, I think this (and below) would be clearer as IgnoreErrors, but I can live with this version if it's more idiomatic.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah, I struggled to enumerate it earlier, but if we check for NodeBuilderFlags.IgnoreErrors, then if any of the AllowX flags are set, the error would be suppressed (as that's how binary & works) - it's much neater to tie it to a specific flag (I'd have been tempted to add new ones, but we're actually entirely our of flag space in this enum).

@weswigham weswigham merged commit d9c9c9d into microsoft:master May 4, 2020
@weswigham
Copy link
Member Author

@typescript-bot cherry-pick this to release-3.9 (once the PR is up, I'll edit it to undo the API changes)

@typescript-bot
Copy link
Collaborator

typescript-bot commented May 4, 2020

Heya @weswigham, I've started to run the task to cherry-pick this into release-3.9 on this PR at c80c177. You can monitor the build here.

typescript-bot pushed a commit to typescript-bot/TypeScript that referenced this pull request May 4, 2020
Component commits:
c80c177 Harden node builder APIs to no longer return `undefined` for a node when `NodeBuilderFlags.IgnoreErrors` is provided
@typescript-bot
Copy link
Collaborator

Hey @weswigham, I've opened #38333 for you.

weswigham added a commit that referenced this pull request May 4, 2020
…e-3.9 (#38333)

* Cherry-pick PR #38273 into release-3.9

Component commits:
c80c177 Harden node builder APIs to no longer return `undefined` for a node when `NodeBuilderFlags.IgnoreErrors` is provided

* Undo API changes for release branch

Co-authored-by: Wesley Wigham <t-weswig@microsoft.com>
Co-authored-by: Wesley Wigham <wwigham@gmail.com>
@weswigham weswigham deleted the nodebuilder-hardening branch May 5, 2020 00:02
@weswigham weswigham mentioned this pull request May 5, 2020
3 tasks
@rbuckton
Copy link
Contributor

rbuckton commented May 5, 2020

This may also fix #32851

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

TypeError: Cannot read property 'emitNode' of undefined in 'Object.getStartsOnNewLine' during 'signatureHelp'
5 participants