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

test: update ESM loader hooks to allow ambiguous format #52990

Closed
wants to merge 1 commit into from

Conversation

avivkeller
Copy link
Member

@avivkeller avivkeller commented May 14, 2024

Pull request #50314 added support for a null format value in addition to the existing commonjs or module. However, the test for ESM hooks relied on the default value of commonjs, causing it to fail when run with the --experimental-detect-module flag. This PR enables the test to detect null formats, ensuring it runs successfully with experimental detection enabled.

Sidenote: While @GeoffreyBooth and I were debugging this behavior, the single-character function arguments made it very difficult to understand, so this PR also expands them into the full names.

@nodejs-github-bot nodejs-github-bot added esm Issues and PRs related to the ECMAScript Modules implementation. needs-ci PRs that need a full CI run. test Issues and PRs related to the tests. labels May 14, 2024
@avivkeller avivkeller added the loaders Issues and PRs related to ES module loaders label May 14, 2024
Copy link
Member

@GeoffreyBooth GeoffreyBooth left a comment

Choose a reason for hiding this comment

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

But is there a purpose behind Node’s internal resolve hook sometimes returning format: null as opposed to the key being missing? If not, maybe our internal hooks should always either set format if it has a truthy value, or don’t define it at all (or set format: undefined) and leave null out of it.

@GeoffreyBooth
Copy link
Member

@nodejs/loaders

@avivkeller
Copy link
Member Author

avivkeller commented May 14, 2024

But is there a purpose behind Node’s internal resolve hook sometimes returning format: null as opposed to the key being missing? If not, maybe our internal hooks should always either set format if it has a truthy value, or don’t define it at all (or set format: undefined) and leave null out of it.

I don't think there is a purpose of setting it to null rather than undefined, but IIRC @aduh95 added the change, so maybe he has a reason?

That being said, in this case, the null vs undefined could be seen asa "was the format even checked?":
Yes, the resolver tried (null)
No, the resolver didn't even check (undefined)

@GeoffreyBooth
Copy link
Member

GeoffreyBooth commented May 14, 2024

Unless there’s a good reason we need null, I think we should first land this and then reopen #52988 and update it so that format is either a string or undefined (at least as set by Node internals).

That being said, in this case, the null vs undefined could be seen asa “was the format even checked?”:

I’m not sure that this is a good reason. If this is data that we want to preserve, it could be its own property on context, like formatWasChecked: true, and therefore it would be explicit and not mysterious.

@JakobJingleheimer
Copy link
Member

JakobJingleheimer commented May 14, 2024

IIR, the historical reason (advocated by Antoine) was that null vs undefined was immaterial/unimportant and the historical precedent was nil.

Splitting hairs: undefined means "idk" and null means "empty" (where empty is "i explicitly acknowledged this exists and there is nothing here" whereas undefined means 🤷‍♂️ or "ignore me")

@GeoffreyBooth
Copy link
Member

In this case, the different is very material—some tests fail with null but not with undefined. This is a footgun for hooks authors, to need to handle an unexpected extra potential return value. Unless we need null for some reason, I think we should toss it and stick to just strings and undefined.

@avivkeller
Copy link
Member Author

avivkeller commented May 14, 2024

Just for sake of discussion, what value would be put in place for an unknown format module?

(But maybe this is a discussion for another time)

@GeoffreyBooth
Copy link
Member

Just for sake of discussion, what value would be put in place for an unknown format module?

Without custom hooks registered, Node throws on unknown formats.

Copy link
Contributor

@aduh95 aduh95 left a comment

Choose a reason for hiding this comment

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

Changing the test should happen in the same commit that changes the code

@aduh95
Copy link
Contributor

aduh95 commented May 15, 2024

You can use this PR to land the variable renaming if you want

@avivkeller
Copy link
Member Author

avivkeller commented May 15, 2024

Changing the test should happen in the same commit that changes the code

Unlike the other test changes for --experimental-detect-module, I think this one deserves its own PR, as IMO it should have been updated in #50314 to account for the new return value (but there was really no way to tell that this would happen).

WDYT @GeoffreyBooth

@GeoffreyBooth
Copy link
Member

@redyetidev I think reopen #52988 and have it undo the part of #50314 that returned null instead of undefined; and update the tests accordingly. Unless @aduh95 can point to a reason that sometimes returning null is desirable, it seems to me that the returning of null was an unintended change in #50314 that had no documentation or explanation, and we should just undo that part.

@avivkeller
Copy link
Member Author

@redyetidev I think reopen #52988 and have it undo the part of #50314 that returned null instead of undefined; and update the tests accordingly. Unless @aduh95 can point to a reason that sometimes returning null is desirable, it seems to me that the returning of null was an unintended change in #50314 that had no documentation or explanation, and we should just undo that part.

Sure, it'll take some work because it seems that certain infrastructure rely on that behavior, but I'll work on it

@avivkeller avivkeller closed this May 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
esm Issues and PRs related to the ECMAScript Modules implementation. loaders Issues and PRs related to ES module loaders needs-ci PRs that need a full CI run. test Issues and PRs related to the tests.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants