-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Vitest loads same module twice from dependencies #6494
Comments
Not sure what's tripping up Vitest's resolution, but it looks like $ DEBUG=vite-node:* npm test
...
vite-node:client:native /home/hiroshi/Downloads/vitest-dev-vitest-dkrj52/node_modules/@udecode/slate/dist/index.mjs +16ms
vite-node:server:request /home/hiroshi/Downloads/vitest-dev-vitest-dkrj52/node_modules/slate/dist/index.js +26ms
vite-node:client:execute /home/hiroshi/Downloads/vitest-dev-vitest-dkrj52/node_modules/slate/dist/index.js +164ms
.... Potential workaround is to use export default defineConfig({
test: {
server: {
deps: {
external: ['slate']
}
}
},
}) |
@hi-ogawa Thanks for the this! Do you know if there's a way we can fix this problem in the |
I think this could be a bug of Vitest. There's some heuristics to decide for vite-node to whether process an external package or not and probably vitest/packages/vite-node/src/externalize.ts Lines 56 to 83 in 16aa76c
|
Could it be because Slate using the extension https://github.com/ianstormtaylor/slate/blob/main/packages/slate/package.json#L7-L11 |
It looks like $ node
> const ESM_SYNTAX_RE = /(?:[\s;]|^)(?:import[\s\w*,{}]*from|import\s*["'*{]|export\b\s*(?:[*{]|default|class|type|function|const|var|let|async function)|import\.meta\b)/m;
undefined
> code = `// import { thisIsComment } from "test"`
'// import { thisIsComment } from "test"'
> ESM_SYNTAX_RE.test(code)
true This is already fixed on mlly unjs/mlly#196, so we can probably copy the same thing to vite-node. |
It looks like mlly's regex Not sure whether we should go with regex or maybe we can use legitimate tokenizer like https://github.com/guybedford/es-module-lexer or https://github.com/antfu/strip-literal |
I think |
Oh, but it fails completely on this case |
Yeah, dotall flag Using es-module-lexer would be fool-proof, but perf impact is the concern. |
I think regex is probably good enough for the purpose of detecting whether or not the code contains If the goal was to get a definite list of imports, using a lexer would be a much better solution, but I'm not sure it's necessary here. |
Thanks @hi-ogawa, this has been very helpful |
Describe the bug
I found this problem to be reproducible only with Vitest, running Jest or plain node, doesn't seem to cause this issue.
I have two packages.
slate
and@udecode/slate
.@udecode/slate
is a small wrapper around slate, and@udecode/slate
dependent (through peer dependencies), onslate
.The package
slate
has some global state (a WeakMap). If you run test cases which mix the use of@udecode/slate
andslate
, you will get two loads of the same modules fromslate
, making the state of WeakMap incorrect.Some other notes.
console.log
s inside the node_modules dist files and saw thatslate/dist/index.js
was being loaded twice, only in Vitest. Running the same with just node works.slate/dist/index.js
instead ofslate/dist/index.es.js
which I would expect to be loaded under these conditions, I have forced vitest to load theesm
version, but this causes the same issue, where you simply load theesm
version twice, and state is duplicated again.resolves.dedup
option, and that didn't seem to work either.Thank you for your help, Vitest has been a great addition to our codebase, and hopefully this issue can make it a little bit better.
Reproduction
This demonstrates that two different versions of the same module are correct, shown inside the
test
folder.I have also provided another file called
test.node.mjs
which has the same test cases, but just outputs them to stdout. In this case the results are different from vitest, and what we expect.https://stackblitz.com/edit/vitest-dev-vitest-dkrj52?file=test%2Fbasic.test.ts&initialPath=__vitest__/
System Info
Used Package Manager
yarn
Validations
The text was updated successfully, but these errors were encountered: