You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is running in WSL:
Linux Gryphon 5.15.167.4-microsoft-standard-WSL2 #1 SMP Tue Nov 5 00:21:55 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux
In the Windows environment:
Microsoft Windows NT 10.0.19045.0 x64
Subsystem
No response
What steps will reproduce the bug?
To reproduce, create a module hook which generates a synthetic file using a specific import attribute for a non-existent file path. When a file is imported with that attribute, but imports an non-existent symbol, Node re-resolve the file without the import condition and errors because the file doesn't exist.
We can reproduce this with the following minimal program:
// index.js// `dep.js` does not exist on the file system.// File content is generated by module hook.// Importing `foo` works.// import { foo as data } from './dep.js' with { type: 'hook' };// Importing `bar` fails. It causes a re-resolve which fails to find the file.import{barasdata}from'./dep.js'with{type: 'hook'};console.log(data);
What's failing here is that resolving deps.js without type: 'hook' causes the custom resolve function to ignore the module altogether.
It's possible I'm misunderstanding the expected behavior here, but there is exactly one import of deps.js and it contains type: 'hook', therefore I think the program should never resolve deps.js without that import attribute.
Additional information
I suspect the problem is that importing an invalid symbol causes Node to generate an error message which itself requires resolving the module, and doesn't propagate import attributes correctly.
import * as mod from './deps.js' with { type: 'hook' } and import('./deps.js', { with: { type: 'hook' } }) both seem to work as expected and don't trigger the double resolution. This is maybe because they don't emit the error which cause Node to re-resolve the specifier.
You can update the statement to import { foo as data } from './deps.js' with { type: 'hook' }; and the module loader works as expected, with only one resolution which includes the import attribute. So this definitely seems to be limited to the "importing a wrong symbol" case.
I can maybe see an argument that at least as a best practice, resolve should return consistent behavior regardless of the given import attribute, or at least be consistent about whether the module exists, even if the location changes based on import attributes. I'm not sure if what I'm doing necessarily aligns with the intended use of module hooks.
The text was updated successfully, but these errors were encountered:
Version
v23.6.0
Platform
Subsystem
No response
What steps will reproduce the bug?
To reproduce, create a module hook which generates a synthetic file using a specific import attribute for a non-existent file path. When a file is imported with that attribute, but imports an non-existent symbol, Node re-resolve the file without the import condition and errors because the file doesn't exist.
We can reproduce this with the following minimal program:
Then execute with
module-hook.js
imported first to set up the hooks correctly:How often does it reproduce? Is there a required condition?
100% consistent reproduction.
What is the expected behavior? Why is that the expected behavior?
I expected a typical "module does not provide export" error, given that the dynamically generated
deps.js
exports onlyfoo
, notbar
.What do you see instead?
I actually get a module resolution error, claiming the file wasn't found at all.
Looking at the logs from the program, we can see that
resolve
was called ondep.js
twice. Once withtype: 'hook'
and once without.What's failing here is that resolving
deps.js
withouttype: 'hook'
causes the customresolve
function to ignore the module altogether.It's possible I'm misunderstanding the expected behavior here, but there is exactly one import of
deps.js
and it containstype: 'hook'
, therefore I think the program should never resolvedeps.js
without that import attribute.Additional information
I suspect the problem is that importing an invalid symbol causes Node to generate an error message which itself requires resolving the module, and doesn't propagate import attributes correctly.
import * as mod from './deps.js' with { type: 'hook' }
andimport('./deps.js', { with: { type: 'hook' } })
both seem to work as expected and don't trigger the double resolution. This is maybe because they don't emit the error which cause Node to re-resolve the specifier.You can update the statement to
import { foo as data } from './deps.js' with { type: 'hook' };
and the module loader works as expected, with only one resolution which includes the import attribute. So this definitely seems to be limited to the "importing a wrong symbol" case.I can maybe see an argument that at least as a best practice,
resolve
should return consistent behavior regardless of the given import attribute, or at least be consistent about whether the module exists, even if the location changes based on import attributes. I'm not sure if what I'm doing necessarily aligns with the intended use of module hooks.The text was updated successfully, but these errors were encountered: