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

[Bug?] Vitest fails to load [yarn pnp, monorepo, v1.6.0] #506

Closed
4 tasks done
JunyeongChoi0 opened this issue Oct 19, 2024 · 1 comment
Closed
4 tasks done

[Bug?] Vitest fails to load [yarn pnp, monorepo, v1.6.0] #506

JunyeongChoi0 opened this issue Oct 19, 2024 · 1 comment

Comments

@JunyeongChoi0
Copy link

Describe the bug

TL:DR;

This part of the extension source code seems to make Vitest fail to load on Yarn PnP. It works if I remove L19-L25


Hello,

I’m using Vitest in a Yarn PnP and monorepo environment, and I’ve encountered an intermittent issue when using the Vitest VSCode extension.

The issue occurs when the extension load the Vitest process: it usually loads successfully right after I open the project in VSCode, but if I hit the ‘Refresh’ button to reload Vitest, the process often fails. I have also noticed occasional failures even on the initial load. I’ve attached the error log at the bottom of this report.

After debugging the error logs and the extension’s source code, I found that during the first successful load, Vitest is executed with the parameters --require /Users/choejun-yeong/3o3/3o3_service/.pnp.cjs --experimental-loader file:///Users/choejun-yeong/3o3/3o3_service/.pnp.loader.mjs, as you can see in below error log. However, on the second attempt, these parameters are missing.

I traced this behavior back to this part of the source code:

export function resolveVitestPackage(cwd: string, folder: vscode.WorkspaceFolder | undefined): VitestResolution | null {
const vitestPackageJsonPath = resolveVitestPackagePath(cwd, folder)
if (vitestPackageJsonPath) {
return {
vitestNodePath: resolveVitestNodePath(vitestPackageJsonPath),
vitestPackageJsonPath,
}
}
const pnp = resolveVitestPnpPackagePath(folder?.uri.fsPath || cwd)
if (!pnp)
return null
return {
vitestNodePath: pnp.vitestNodePath,
vitestPackageJsonPath: 'vitest/package.json',
pnp: {
loaderPath: pnp.pnpLoader,
pnpPath: pnp.pnpPath,
},
}
}

Where the function resolveVitestPackagePath tries to require.resolve('vitest/package.json'). If that fails, it falls back to a Yarn PnP-specific resolution.

The problem occurs specifically with the require.resolve('vitest/package.json'). When the Vitest load succeeds, this code throws an error(Cannot find module ...), but when the load fails, the require.resolve call succeeds and returns a path like /Users/choejun-yeong/3o3/3o3_service/.yarn/__virtual__/vitest-virtual-d765642456/0/cache/vitest-npm-2.1.2-6430425a96-3a331a1049.zip/node_modules/vitest/package.json.

Although I haven’t been able to consistently reproduce the issue, I’ve observed that require.resolve throws when the project is freshly opened in VSCode (which leads to Vitest loading successfully). However, after some time has passed, require.resolve succeeds (leading to the Vitest load failure).

When I removed the require.resolve call from above code, the Vitest process always loaded successfully.

Based on this, could you help me understand what might be causing this issue? My suspicion is that determining whether Yarn PnP is being used based on require.resolve alone may not be reliable.

Thank you!

Reproduction

I attempted to create a reproduction repository but was unsuccessful. The repository where this issue occurs is private, so unfortunately, I can’t share it at this time.

Output

[INFO 오후 5:12:25] [v1.6.0] Vitest extension is activated because Vitest is installed or there is a Vite/Vitest config file in the workspace.

[INFO 오후 5:12:26] [API] Running Vitest vpnp (3o3_service/vitest.workspace.ts) with [email protected]: /Users/choejun-yeong/.nvm/versions/node/v20.16.0/bin/node --require /Users/choejun-yeong/3o3/3o3_service/.pnp.cjs --experimental-loader file:///Users/choejun-yeong/3o3/3o3_service/.pnp.loader.mjs

[INFO 오후 5:12:26] [API] Vitest vpnp (3o3_service/vitest.workspace.ts) child process 47661 created

[INFO 오후 5:12:26] [VSCODE] Watching 3o3_service with pattern **/*

[INFO 오후 5:12:36] [API] Vitest process 47661 closed successfully
[INFO 오후 5:12:36] [API] Running Vitest v2.1.2 (3o3_service/vitest.workspace.ts) with [email protected]: /Users/choejun-yeong/.nvm/versions/node/v20.16.0/bin/node

[Error 오후 5:12:36] [API] Failed to refresh Vitest [Error Error] Vitest failed to start:

Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/Users/choejun-yeong/3o3/3o3_service/.yarn/__virtual__/vitest-virtual-d765642456/0/cache/vitest-npm-2.1.2-6430425a96-3a331a1049.zip/node_modules/vitest/dist/node.js' imported from /Users/choejun-yeong/.vscode/extensions/vitest.explorer-1.6.0/dist/worker.js

at finalizeResolution (node:internal/modules/esm/resolve:265:11)

at moduleResolve (node:internal/modules/esm/resolve:933:10)

at defaultResolve (node:internal/modules/esm/resolve:1169:11)

at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:383:12)

at ModuleLoader.resolve (node:internal/modules/esm/loader:352:25)

at ModuleLoader.getModuleJob (node:internal/modules/esm/loader:227:38)

at ModuleLoader.import (node:internal/modules/esm/loader:315:34)

at defaultImportModuleDynamically (node:internal/modules/esm/utils:194:31)

at importModuleDynamicallyCallback (node:internal/modules/esm/utils:216:12)

at To (/Users/choejun-yeong/.vscode/extensions/vitest.explorer-1.6.0/dist/worker.js:20:7180)

Error: Vitest failed to start:

Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/Users/choejun-yeong/3o3/3o3_service/.yarn/__virtual__/vitest-virtual-d765642456/0/cache/vitest-npm-2.1.2-6430425a96-3a331a1049.zip/node_modules/vitest/dist/node.js' imported from /Users/choejun-yeong/.vscode/extensions/vitest.explorer-1.6.0/dist/worker.js

at finalizeResolution (node:internal/modules/esm/resolve:265:11)

at moduleResolve (node:internal/modules/esm/resolve:933:10)

at defaultResolve (node:internal/modules/esm/resolve:1169:11)

at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:383:12)

at ModuleLoader.resolve (node:internal/modules/esm/loader:352:25)

at ModuleLoader.getModuleJob (node:internal/modules/esm/loader:227:38)

at ModuleLoader.import (node:internal/modules/esm/loader:315:34)

at defaultImportModuleDynamically (node:internal/modules/esm/utils:194:31)

at importModuleDynamicallyCallback (node:internal/modules/esm/utils:216:12)

at To (/Users/choejun-yeong/.vscode/extensions/vitest.explorer-1.6.0/dist/worker.js:20:7180)

at ChildProcess.p (/Users/choejun-yeong/.vscode/extensions/vitest.explorer-1.6.0/dist/extension.js:19:3538)

at ChildProcess.emit (node:events:519:28)

at ChildProcess.emit (node:domain:488:12)

at emit (node:internal/child_process:951:14)

at processTicksAndRejections (node:internal/process/task_queues:83:21)

Version

1.6.0

Validations

@JunyeongChoi0
Copy link
Author

Closing this issue as it was uploaded using the wrong account

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

No branches or pull requests

1 participant