Skip to content

Conversation

@GeoffreyBooth
Copy link
Member

As part of #44710@targos discovered that our test fixture loader for “assertionless” JSON imports—import data from './file.json' where there’s no assert{type: 'json' }—was only working because it mutated the context.importAssertions object, which doesn’t work on the “loaders off-thread” branch. But it arguably shouldn’t work on main either, as mutating function input is a poor way for a hook to return information.

This PR adds an optional importAssertions property to the object returned by the resolve hook, to enable the use case where a module is cached with different import assertions than were present in the original source. I think this is the “correct” way that user hooks should inform the internal module loader what assertion type to use as part of the module cache key, rather than relying on input mutation.

cc @nodejs/loaders

@GeoffreyBoothGeoffreyBooth added esm Issues and PRs related to the ECMAScript Modules implementation. loaders Issues and PRs related to ES module loaders labels Jan 10, 2023
@nodejs-github-bot
Copy link
Collaborator

Review requested:

  • @nodejs/loaders
  • @nodejs/modules

@nodejs-github-botnodejs-github-bot added the needs-ci PRs that need a full CI run. label Jan 10, 2023
@GeoffreyBoothGeoffreyBoothforce-pushed the resolve-hook-returns-import-assertions branch from 52f571c to c797d07CompareJanuary 10, 2023 01:32
@GeoffreyBoothGeoffreyBoothforce-pushed the resolve-hook-returns-import-assertions branch 2 times, most recently from d34bf01 to e80eaa6CompareJanuary 10, 2023 01:55
@nodejs-github-bot
Copy link
Collaborator

@GeoffreyBoothGeoffreyBooth removed the needs-ci PRs that need a full CI run. label Jan 10, 2023
@GeoffreyBoothGeoffreyBoothforce-pushed the resolve-hook-returns-import-assertions branch from e80eaa6 to 1a9e7d6CompareJanuary 10, 2023 06:27
@nodejs-github-bot
Copy link
Collaborator

doc/api/esm.md Outdated
Comment on lines 784 to 788
Import assertions are part of the cache key for saving loaded modules into the
Node.js internal module cache. The `resolve` hook is responsible for returning
an `importAssertions` object if the module should be cached with different
assertions than were present in the source code (for example, if no assertions
were present but the module should be cached with assertions
`{type: 'json'}`).
Copy link
Member

Choose a reason for hiding this comment

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

I feel like the key thing is an implementation detail. The reason a loader needs to customize import assertions is to support modules that import JSON without the assertion (bypass the validation).

Copy link
MemberAuthor

Choose a reason for hiding this comment

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

I’m not sure what you want changed, if anything?

Copy link
Member

Choose a reason for hiding this comment

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

I don't know how to write it differently. If you think users can understand the current wording that's fine.

@aduh95aduh95 changed the title esm: resolve optionally returns import assertionsesm: allow resolve to optionally return import assertionsJan 10, 2023
@aduh95
Copy link
Contributor

The first word of the commit message should be an imperative verb:

* be prefixed with the name of the changed [subsystem](#appendix-subsystems)
and start with an imperative verb. Check the output of `git log --oneline

resolve is an imperative verb, but is not used as one here and it's quite confusing.

@GeoffreyBoothGeoffreyBoothforce-pushed the resolve-hook-returns-import-assertions branch from 1a9e7d6 to 0a01441CompareJanuary 10, 2023 18:28
@GeoffreyBoothGeoffreyBoothforce-pushed the resolve-hook-returns-import-assertions branch from 0a01441 to 246694fCompareJanuary 10, 2023 19:32
@GeoffreyBoothGeoffreyBoothforce-pushed the resolve-hook-returns-import-assertions branch from 246694f to c3a2e8bCompareJanuary 10, 2023 20:04
@GeoffreyBoothGeoffreyBoothforce-pushed the resolve-hook-returns-import-assertions branch 2 times, most recently from e9822be to 349d054CompareJanuary 10, 2023 20:18
@bmeck
Copy link
Member

bmeck commented Jan 11, 2023 via email

@GeoffreyBooth
Copy link
MemberAuthor

Because resolution is explicitly banned from using import assertions to affect it in spec: import assertions (tc39.es)

Yes, but that wouldn’t apply to user loaders, which are allowed to violate spec. The reference @aduh95 found is in a user loader test fixture.

@ljharb

This comment was marked as off-topic.

@nodejs-github-botnodejs-github-bot removed the commit-queue Add this label to land a pull request using GitHub Actions. label Jan 12, 2023
@nodejs-github-botnodejs-github-bot merged commit 91ca2d4 into nodejs:mainJan 12, 2023
@nodejs-github-bot
Copy link
Collaborator

Landed in 91ca2d4

@RafaelGSS
Copy link
Member

Hi @GeoffreyBooth! This didn't land cleanly on v19.x-staging, can you create a manual backport? Thanks

@GeoffreyBooth
Copy link
MemberAuthor

GeoffreyBooth commented Jan 17, 2023

@RafaelGSS This needs to wait on #45869 (comment).

@juanarbol
Copy link
Member

Hey, this is not landing cleanly on v18.x line :(

@GeoffreyBooth
Copy link
MemberAuthor

Hey, this is not landing cleanly on v18.x line :(

What if you land #43772 and then #45869? Hopefully this can land cleanly after those two go first?

@targos
Copy link
Member

Lands cleanly indeed.

targos pushed a commit that referenced this pull request Mar 14, 2023
PR-URL: #46153 Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Jacob Smith <[email protected]>
@targostargos mentioned this pull request Mar 14, 2023
@ljharb
Copy link
Member

Given the tenuous status of import assertions, should this change be backported?

targos pushed a commit that referenced this pull request Nov 10, 2023
PR-URL: #46153 Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Jacob Smith <[email protected]>
@targostargos mentioned this pull request Nov 28, 2023
sercher added a commit to sercher/graaljs that referenced this pull request Apr 25, 2024
PR-URL: nodejs/node#46153 Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Jacob Smith <[email protected]>
sercher added a commit to sercher/graaljs that referenced this pull request Apr 25, 2024
PR-URL: nodejs/node#46153 Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Jacob Smith <[email protected]>
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

author readyPRs that have at least one approval, no pending requests for changes, and a CI started.esmIssues and PRs related to the ECMAScript Modules implementation.loadersIssues and PRs related to ES module loaders

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants

@GeoffreyBooth@nodejs-github-bot@aduh95@bmeck@ljharb@RafaelGSS@juanarbol@targos@JakobJingleheimer