Closed Bug 1656248 Opened 5 years ago Closed 5 years ago

Dynamic import() with relative path fetches wrong path after navigation

Categories

(Core :: JavaScript Engine, defect, P1)

78 Branch
defect

Tracking

()

RESOLVED FIXED
82 Branch
Tracking Status
firefox82 --- fixed

People

(Reporter: jversteeg, Assigned: jonco)

Details

Attachments

(3 files)

User Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Firefox/78.0

Steps to reproduce:

index.html contains
<script defer src="js/util.js">

js/util.js contains
import('./modules/mod.js');

Let browser load index.html.

Please see the attachment or the GitHub repo: https://github.com/drjayvee/firefox-import-bug

Actual results:

The first several loads cause the browser to correctly load
js/modules/mod.js. (Unfortunately, the MDN docs are rather vague on how module-names are translated to paths).

After several refreshes (using F5 or other navigation; cache is enabled and effective) however, the browser instead loads modules/mod.js.

Continuing refreshing does not fix this. Disabling cache in dev tools does fix! After a few refreshes, the same behavior repeats.

Expected results:

The import should keep using the same relative path.

Forgot to mention that the attachment zip also contains a HAR log file.

Hi Jeroen,

Thanks for your report and for attaching detailed files.

I'll add this ticket to the Core: Javascript Engine component in the hope someone from their team can take a look and comment.

Regards,
Virginia

Component: Untriaged → JavaScript Engine
Product: Firefox → Core

Jon, would you be the best person to answer these questions, or investigate this issue?

Flags: needinfo?(jcoppeard)

I agree with Jeroen's assessment, and I can reproduce this behavior using the github repo (with a server on localhost—I used python3 -m http.server).

The JS spec for import() says to call a hook named HostImportModuleDynamically that is defined in the HTML spec. Step 4.3 says "Set base URL to referencing script's base URL."

It would be surprising if the spec actually says that navigation affects script base URLs (admittedly navigation is full of unpleasant surprises). A bug, then...?

I'll take a look. I can reproduce this pretty easily.

Flags: needinfo?(jcoppeard)

We're not associating the loader script object with the JSScript for scripts loaded from the bytecode cache, and as a result we're getting the base URL wrong when resolving imports.

Assignee: nobody → jcoppeard
Severity: -- → S4
Priority: -- → P1

The problem is that ResolveModuleSpecifier is being passed a null script and is falling back to using the document base URL to resolve the module import (which is the correct thing to do in some cases). This is happening because the referring JS script was not assoicated with a loader script when it was loaded since it came from the bytecode cache and the logic to do that is missing on this path.

I'm trying to write a test case (see attached file) but I'm having trouble getting the bytecode cache to kick in. I'm repeatedly reloading an iframe which has an external script. If I set strategy to -1 it successfully encodes the script, but then it never uses the cached bytecode. Any ideas how to make this work? I just want to make sure a script is loaded from the bytecode cache. (Apparently you're on PTO so needinfoing you here).

Flags: needinfo?(nicolas.b.pierron)

I will look more in details of the test case that you wrote, but here is a suggested read for inspiration:
https://searchfox.org/mozilla-central/source/dom/base/test/test_script_loader_js_cache.html

(In reply to Nicolas B. Pierron [:nbp] from comment #10)
Thanks, I've got this working now using some of those script loader test events.

Flags: needinfo?(nicolas.b.pierron)
Attachment #9169566 - Attachment description: Bug 1656248 - Add testcase (WIP) → Bug 1656248 - Add testcase that scripts loaded from the bytecode cache are correctly associated with a base URL r?smaug
Pushed by jcoppeard@mozilla.com: https://hg.mozilla.org/integration/autoland/rev/243a8dc843c4 Associate the JS script with the loader script for scripts loaded from the bytecode cache like we do for compiled scripts r=smaug https://hg.mozilla.org/integration/autoland/rev/12c08680c172 Add testcase that scripts loaded from the bytecode cache are correctly associated with a base URL r=smaug
Status: UNCONFIRMED → RESOLVED
Closed: 5 years ago
Resolution: --- → FIXED
Target Milestone: --- → 82 Branch
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: