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

Replace invalid import attributes assertion #9599

Merged
merged 4 commits into from
Aug 23, 2023

Conversation

nicolo-ribaudo
Copy link
Contributor

@nicolo-ribaudo nicolo-ribaudo commented Aug 11, 2023

When creating a module script, there is no guarantee yet that the module's dependencies only have valid import attribute keys. Instead of asserting that only type is used, this commit introduces proper validation throwing a SyntaxError.

I was on the fence between SyntaxError and TypeError. ECMA-262 throws a SyntaxError for invalid attributes because it throws a SyntaxError for all the errors that happen before evaluation, but HTML throws a TypeError for invalid values of the "type" attribute. I ended up picking SyntaxError just because it's what ECMA-262 already does.

I found this bug while updating the import attributes implementation in Chromium. I'm not sure if this need explicit implementer interest given that the previous spec was just wrong.

Closes tc39/proposal-import-attributes#144.


  • At least two implementers are interested (and none opposed):
    • Assumed yes as part of the consensus in TC39 on the import attributes proposal
  • Tests are written and can be reviewed and commented upon at:
  • Implementation bugs are filed:
    • Chromium: …
    • Gecko: …
    • WebKit: …
    • Deno (only for timers, structured clone, base64 utils, channel messaging, module resolution, web workers, and web storage): …
    • Node.js (only for timers, structured clone, base64 utils, channel messaging, and module resolution): …
  • MDN issue is filed: Not necessary for this level of detail

(See WHATWG Working Mode: Changes for more details.)


/infrastructure.html ( diff )
/webappapis.html ( diff )

When creating a module script, there is no guarantee yet that the
module's dependencies only have valid import attribute keys. Instead
of asserting that only `type` is used, this commit introduces proper
validation throwing a SyntaxError.
@domenic
Copy link
Member

domenic commented Aug 14, 2023

It seems like this implements (1) from tc39/proposal-import-attributes#144, right? Can you give a bit more detail on the reasoning process there, and in particular how this impacts static vs. dynamic imports?

Hmm no, I guess this is more like (1), but then adding an extra check to skip unnecessary work?

Are there tests you've written that show the choice of (1) vs. (2)?

Copy link
Member

@domenic domenic left a comment

Choose a reason for hiding this comment

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

Editorial touchups in the note, and still trying to make sure I understand the relationship to tc39/proposal-import-attributes#144 and test coverage, but this seems pretty solid.

source Show resolved Hide resolved
source Outdated Show resolved Hide resolved
@nicolo-ribaudo
Copy link
Contributor Author

Right, this PR does (1).

Consider these three examples:

import "./valid.js";
import "INVALID-URL";
import "./valid.js";
import "./valid2.js" with { type: "invalid-type" };
import "./valid.js";
import "./valid2.js" with { invalid: "invalid-attribute" };

For the first two examples, HTML currently first loads the imported specifiers and the type attribute, and then if it succeeds it passes the module to ECMA-262. This means that the first two example will throw an error before passing the module to ECMA-262.

On the other hand, for the third example HTML does not currently do any pre-validation of import attribute keys before passing the Module Record to ECMA-262, and instead it relies on ECMA-262 to validate them. This means that:

  • HTML will pass that module to ECMA-262
  • ECMA-262 will start the loading process for ./valid.js
  • ECMA-262 will start the loading process for ./valid.js, and it will immediately throw an error because of the invalid attribute

This PR aligns the third case with the other twos, by also validating the attribute keys when validating the type and the specifier. I am not sure if this change can be tested, given that the loading process fails anyway and the only difference is whether or not HostLoadImportedModule("./valid.js") will be called before throwing about the invalid attribute.

This PR does not just introduce that check, but it specifically introduces it as the first check. This aligns the errors priority of static imports with the one of dynamic imports, as mentioned in tc39/proposal-import-attributes#144 (comment). This can currently be tested (web-platform-tests/wpt#41581) because, following what ECMA-262 does, validation of unknown attribute keys throws a SyntaxError. If it was a TypeError, there would be no way to test it (other than somehow checking the error messages).

@nicolo-ribaudo
Copy link
Contributor Author

I pushed a new test to web-platform-tests/wpt#41581. The first two tests in each file verify the order in which the import declaration checks happen, while the third one verifies that the "unknown attributes check" is present. Before this PR, this code:

import "./valid" with { unknown: "foo" };
import "INVALID";

would throw about the "INVALID" specifier before validating the unknown attribute.

Copy link
Member

@domenic domenic left a comment

Choose a reason for hiding this comment

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

Thanks for the additional explanation, and tests!

@domenic domenic merged commit 88fe80b into whatwg:main Aug 23, 2023
1 check passed
@nicolo-ribaudo nicolo-ribaudo deleted the import-attribtues-wrong-assertion branch August 23, 2023 16:57
@domenic
Copy link
Member

domenic commented Aug 23, 2023

For any implementation which already has import attributes or assertions, but fails to implement this edge case correctly (per your tests), please file a bug and link to it in the OP. Otherwise, we can assume this will get done correctly as part of the general import attributes work.

domenic pushed a commit to web-platform-tests/wpt that referenced this pull request Aug 23, 2023
@nicolo-ribaudo
Copy link
Contributor Author

I am implementing this in Chromium. I'll check what's the status in Webkit and Gecko. Both Deno and Node.js do not support the new semantics yet.

aarongable pushed a commit to chromium/chromium that referenced this pull request Sep 5, 2023
In the past six months, the old import assertions proposal has been
renamed to "import attributes" with the follwing major changes:
1. the keyword is now `with` instead of `assert`
2. unknown assertions cause an error rather than being ignored

(1) has been implemented in V8 at [v8-patch], under the
`--harmony-import-attributes` flag. This patch implements (2), that due
to how module loading is layered must be implemented in Chromium.

Implementing this change uncovered a bogous assertion in the HTML spec:
this patch implements the fix proposed at [html-assertion-fix], that
adds a check for invalid attributes in the "create a JavaScript module
script" algorithm.

WPT have been merged at [wpt], and tests for the additional HTML PR are
being reviewed at [wpt2]. This patch passes all the tests in the
import-assertions and import-attributes folders.

[v8-patch]: https://chromium.googlesource.com/v8/v8/+/a0fd3209dda8527d7da810abe231df27fffe789e
[html-assertion-fix]: whatwg/html#9599
[wpt]: web-platform-tests/wpt#41156
[wpt2]: web-platform-tests/wpt#41581

Bug: v8:13856
Change-Id: I702985feb17af1c2fefee231be0dac66b3e6cc98
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4738316
Reviewed-by: Hiroshige Hayashizaki <hiroshige@chromium.org>
Commit-Queue: Kouhei Ueno <kouhei@chromium.org>
Reviewed-by: Kouhei Ueno <kouhei@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1192427}
moz-v2v-gh pushed a commit to mozilla/gecko-dev that referenced this pull request Sep 13, 2023
…rrors order, a=testonly

Automatic update from web-platform-tests
Test import declaration errors order

Follows whatwg/html#9599.
--

wpt-commits: bcae71ef7480ab3ac284b22f7c3768ff366f9136
wpt-pr: 41581
vinnydiehl pushed a commit to vinnydiehl/mozilla-unified that referenced this pull request Sep 14, 2023
…rrors order, a=testonly

Automatic update from web-platform-tests
Test import declaration errors order

Follows whatwg/html#9599.
--

wpt-commits: bcae71ef7480ab3ac284b22f7c3768ff366f9136
wpt-pr: 41581
Lightning00Blade pushed a commit to Lightning00Blade/wpt that referenced this pull request Dec 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

Invalid attributes keys in static imports should be a resolution/loading error and not a parsing error
2 participants