Skip to content
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

lib: fix internalBinding type definitions #47373

Closed
wants to merge 1 commit into from

Conversation

anonrig
Copy link
Member

@anonrig anonrig commented Apr 2, 2023

Work in progress.

Current type definitions does not work for me. Investigating the issue. Full conversation is available on Twitter: https://twitter.com/andhaveaniceday/status/1642583763862552576?s=61&t=MpgxJm-yQa5OwwXgT9yE6A

@nodejs-github-bot
Copy link
Collaborator

Review requested:

  • @nodejs/loaders
  • @nodejs/modules
  • @nodejs/startup

@nodejs-github-bot nodejs-github-bot added lib / src Issues and PRs related to general changes in the lib or src directory. needs-ci PRs that need a full CI run. labels Apr 2, 2023
@jakebailey
Copy link

Ah, I can sorta see why things aren't working, or working strangely. Because Node's internals are not really modules, I think TS is detecting them as scripts all loaded into the same scope. That's probably hidden by not actually having checking enabled. I don't know how much work it'd be, but adding a tsconfig and adding allowJs/checkJs/noEmit might be interesting to try.

In the meantime, another pattern that you can use instead and keep the let is to instead use interface merging to declare the type of this. What you'd do is restore the /** @type {InternalBinding} */, then for each previous declaration of declare function InternalBinding, instead convert it to an interface with a signature.

For example, config.d.ts:

declare interface InternalBinding {
  (binding: 'config'): {
    isDebugBuild: boolean;
    hasOpenSSL: boolean;
    fipsMode: boolean;
    hasIntl: boolean;
    hasTracing: boolean;
    hasNodeOptions: boolean;
    hasInspector: boolean;
    noBrowserGlobals: boolean;
    bits: number;
    hasDtrace: boolean;
  }
}

This is the same approach used by the lib.d.ts files to add more overloads depending on the different lib target set in tsconfig.

Here's a patch you can apply if you revert the _internalBinding commit: change.patch

@jakebailey
Copy link

jakebailey commented Apr 2, 2023

Adding a tsconfig file even if it doesn't have checkJs enabled may be desirable just so you can explicitly ensure that the d.ts files are loaded. Right now, I think they might only show up if you have opened the files in the editor.

Then afterward, you may be able to set moduleDetection=force to disable the scope sharing.

EDIT: I missed that you had a tsconfig already. Oops.

@jakebailey
Copy link

It seems to be working?

image

Some of them don't work because there are things missing from internalBinding but the ones that do exist seem to work fine.

It may be better to keep your renaming change, though, and then use the simpler option of overloads instead. It doesn't seem like the name matters in this loader file?

@anonrig
Copy link
Member Author

anonrig commented Apr 2, 2023

Then afterward, you may be able to set moduleDetection=force to disable the scope sharing.

Current changes + moduleDetection=force did not make any difference for me.

It seems to be working?

Did you get it working with this PR changes + the git diff you've shared? I couldn't get it to working...

@jakebailey
Copy link

With this PR, reverting the _internalBinding change, and applying my diff, yes.

Another thing that worked without scope sharing was to instead declare var internalBinding, then var internalBinding: InternalBinding in a new d.ts file added to tsconfig, but that's not strictly required I don't think, maybe in moduleDetection=force if anything.

@anonrig
Copy link
Member Author

anonrig commented Apr 2, 2023

With this PR, reverting the _internalBinding change, and applying my diff, yes.

It didn't work for me (both VSCode and Webstorm), it still detects isMainThread from internalBinding('worker') as any instead of boolean

@jakebailey
Copy link

Very very strange. Most of these work, but worker does not. I'm not sure what's going on here.

You probably don't want to load DOM to get URL; you'll pull in a load of stuff that won't work. You may just want to re-declare it locally, or copy from URL in @types/node.

@anonrig anonrig force-pushed the fix-autocompletion branch from de5b278 to 152d00a Compare April 2, 2023 19:28
@anonrig
Copy link
Member Author

anonrig commented Apr 2, 2023

You probably don't want to load DOM to get URL; you'll pull in a load of stuff that won't work. You may just want to re-declare it locally, or copy from URL in @types/node.

Agreed. Removed it for the time being.

@gengjiawen
Copy link
Member

gengjiawen commented Apr 3, 2023

With this PR, reverting the _internalBinding change, and applying my diff, yes.

It didn't work for me (both VSCode and Webstorm), it still detects isMainThread from internalBinding('worker') as any instead of boolean

Within this PR, vscode works (including worker), Clion not.

Maybe it's a cache problem in vscode (The ts server is buggy if it runs very long, have to restart it). Try restart ts service on your vscode.

Also online vsode works: https://gitpod.io/#https://github.com/nodejs/node/pull/47373/

CleanShot.2023-04-03.at.11.36.39-converted.mp4

@anonrig
Copy link
Member Author

anonrig commented Apr 3, 2023

Within this PR, vscode works (including worker), Clion not.

@gengjiawen My main goal was to fix the Clion types... I'll leave this as a draft until I find a fix for Clion too...

@anonrig anonrig marked this pull request as ready for review April 3, 2023 19:00
@anonrig anonrig added the commit-queue-squash Add this label to instruct the Commit Queue to squash all the PR commits into the first one. label Apr 3, 2023
@anonrig
Copy link
Member Author

anonrig commented Apr 3, 2023

I finally got it working on Clion too...

Screenshot 2023-04-03 at 3 02 00 PM

@anonrig anonrig added the request-ci Add this label to start a Jenkins CI on a PR. label Apr 3, 2023
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Apr 3, 2023
@nodejs-github-bot
Copy link
Collaborator

@anonrig anonrig force-pushed the fix-autocompletion branch from 5d6d494 to 8e65c90 Compare April 4, 2023 21:11
@anonrig anonrig added request-ci Add this label to start a Jenkins CI on a PR. author ready PRs that have at least one approval, no pending requests for changes, and a CI started. review wanted PRs that need reviews. labels Apr 4, 2023
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Apr 4, 2023
@nodejs-github-bot
Copy link
Collaborator

@nodejs-github-bot
Copy link
Collaborator

@nodejs-github-bot
Copy link
Collaborator

@nodejs-github-bot
Copy link
Collaborator

@nodejs-github-bot
Copy link
Collaborator

@nodejs-github-bot
Copy link
Collaborator

@nodejs-github-bot
Copy link
Collaborator

@nodejs-github-bot
Copy link
Collaborator

@nodejs-github-bot
Copy link
Collaborator

@gengjiawen
Copy link
Member

From what I test, latest code not working for vscode.

@anonrig anonrig removed the author ready PRs that have at least one approval, no pending requests for changes, and a CI started. label Apr 14, 2023
@anonrig
Copy link
Member Author

anonrig commented Apr 14, 2023

From what I test, latest code not working for vscode.

Thanks for testing @gengjiawen. I'll take a look at it

@anonrig
Copy link
Member Author

anonrig commented Apr 15, 2023

@gengjiawen can you try it again?

@anonrig anonrig force-pushed the fix-autocompletion branch from 2ebdf07 to 1d3edf1 Compare April 15, 2023 21:05
@anonrig anonrig added the request-ci Add this label to start a Jenkins CI on a PR. label Apr 16, 2023
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Apr 16, 2023
@nodejs-github-bot
Copy link
Collaborator

@gengjiawen
Copy link
Member

@gengjiawen can you try it again?

still not working.

@targos
Copy link
Member

targos commented Apr 16, 2023

I confirm:
CleanShot 2023-04-16 at 16 18 09@2x

@anonrig
Copy link
Member Author

anonrig commented Aug 16, 2023

OK, I'm officially giving up on this. Feel free to continue, if anyone is interested.

@anonrig anonrig closed this Aug 16, 2023
@anonrig anonrig deleted the fix-autocompletion branch August 16, 2023 15:38
@gengjiawen
Copy link
Member

@anonrig make it work on vscode enough for most people, we can take fix clion maybe later, reopen it ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
commit-queue-squash Add this label to instruct the Commit Queue to squash all the PR commits into the first one. lib / src Issues and PRs related to general changes in the lib or src directory. needs-ci PRs that need a full CI run. review wanted PRs that need reviews.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants