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

vi.mock's importOriginal stuck on circular imports #4373

Closed
6 tasks done
AriPerkkio opened this issue Oct 25, 2023 · 7 comments · Fixed by #4811
Closed
6 tasks done

vi.mock's importOriginal stuck on circular imports #4373

AriPerkkio opened this issue Oct 25, 2023 · 7 comments · Fixed by #4811

Comments

@AriPerkkio
Copy link
Member

AriPerkkio commented Oct 25, 2023

Describe the bug

When a test imports file that contains circular imports and also mocks some of these files, vi.mock's importOriginal does not resolve. Whats worse is that vite-node silently hangs and doesn't log any timeout errors. Even birpc doesn't timeout here for some reason.

import '../src/module-1';

vi.mock('../src/module-2', async (importOriginal) => {
  console.log('Start importOriginal');
  await importOriginal();
  //    ^^^ Never resolves

  console.log('Done importOriginal');

  return { default: () => {} };
});
$ DEBUG=vite-node:* pnpm run test tests/cyclic-imports.test.ts 

> example-project@1.0.0 test /<path>/vitest-example-project
> vitest run "tests/cyclic-imports.test.ts"


 RUN  v1.0.0-beta.2 /<path>/vitest-example-project

  vite-node:server:request /@vite/env +0ms
2023-10-25T18:22:43.172Z vite-node:client:execute /@vite/env
2023-10-25T18:22:43.174Z vite-node:client:native /<path>/vitest/packages/vitest/dist/spy.js
2023-10-25T18:22:43.200Z vite-node:client:native /<path>/vitest/packages/vitest/dist/runners.js
  vite-node:server:request /<path>/vitest-example-project/tests/cyclic-imports.test.ts +42ms
2023-10-25T18:22:43.210Z vite-node:client:execute /<path>/vitest-example-project/tests/cyclic-imports.test.ts
2023-10-25T18:22:43.211Z vite-node:client:native /<path>/vitest/packages/vitest/dist/index.js
  vite-node:server:request /src/module-1.ts +11ms
2023-10-25T18:22:43.215Z vite-node:client:execute /<path>/vitest-example-project/src/module-1.ts
  vite-node:server:request /<path>/vitest-example-project/src/module-2.ts +28ms
stdout | tests/cyclic-imports.test.ts:3:11
Start importOriginal

2023-10-25T18:22:43.244Z vite-node:client:execute /<path>/vitest-example-project/src/module-2.ts
  vite-node:server:request /src/module-3.ts +3ms
2023-10-25T18:22:43.246Z vite-node:client:execute /<path>/vitest-example-project/src/module-3.ts
  vite-node:server:request /src/module-4.ts +3ms
2023-10-25T18:22:43.248Z vite-node:client:execute /<path>/vitest-example-project/src/module-4.ts

# Stuck here, CTRL+C terminates process correctly

Reproduction

https://stackblitz.com/edit/vitest-dev-vitest-8gzjhq?file=test%2Fcyclic-import.test.mjs

System Info

Stackblitz

- Vitest v1 beta and 0.3x.x releases

Used Package Manager

pnpm

Validations

@stackblitz
Copy link

stackblitz bot commented Oct 25, 2023

Fix this issue in StackBlitz Codeflow Start a new pull request in StackBlitz Codeflow.

@transfer1992
Copy link

Very frustrating, especially because it is completely silent. Spent few hours on this.

@mcclayton
Copy link

mcclayton commented Dec 15, 2023

This issue appears to be related to:
#4026

I ultimately fixed this for myself by addressing the circular dependencies (using the eslint rule: https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/no-cycle.md#when-not-to-use-it)

However, it was incredibly difficult to determine that it was ultimately the circular dependencies since vitest just hung without any error output or timeout. Hope these issues can get visibility for others stuck with this.

@AriPerkkio
Copy link
Member Author

I ran into this issue again - this time caused by circular dependency coming from a barrel file. By using the VITE_NODE_DEBUG_RUNNER=true it's possible to see the modules that are stuck loading. From those logs I could figure out the root cause.

### Debug Execution
If the process gets stuck, it might be because there are unresolvable circular dependencies. You can set `VITE_NODE_DEBUG_RUNNER=true` for vite-node to warn about this.
```bash
VITE_NODE_DEBUG_RUNNER=true vite-node example.ts
```

@sheremet-va
Copy link
Member

I found the culprit, PR incoming.

@dlvhdr
Copy link

dlvhdr commented Dec 31, 2023

How can I test this fix?
I have a yarn v1 monorepo and can't figure out how to test this change.
I tried using the "vitest": "file:/Users/dlvhdr/code/misc/vitest/packages/vitest" syntax, as well as running vitest from the build artifacts.
For the first - yarn doesn't know what I mean
For the latter - I get a weird error:0909006C:PEM routines:get_name:no start line error.

Alternatively, can a new release be made with the fix?

@dlvhdr
Copy link

dlvhdr commented Dec 31, 2023

Ok I managed to make it work and this does fix the issue. I just had to remove the packageManager entry from the package.json.

@github-actions github-actions bot locked and limited conversation to collaborators Jan 15, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants