bugfix-705: prevent alias resolution failure in monorepo setups.#706
bugfix-705: prevent alias resolution failure in monorepo setups.#706Lievesley wants to merge 1 commit intovitest-dev:mainfrom
Conversation
…les from workspaces that belong to packages with their own configs
| return fileList.filter(([file]) => { | ||
| const normalizedFile = normalize(file) | ||
| // Keep file if it's not in any package directory | ||
| const isInPackageDir = this.packageCwds.some(cwd => normalizedFile.startsWith(cwd)) |
There was a problem hiding this comment.
We should never rely on a file path to distinguish between projects. Projects can share the same files.
There was a problem hiding this comment.
Please describe the test case that should fail in this case. Do you mean that two packages in a workspace should be able to share the same files?
There was a problem hiding this comment.
getFiles() should already return correct files, it uses Vitest to get them, not some obscure external logic. The files could be in a parent directory or in a different drive, it doesn't matter as long as the user config decided it. The Vitest is the source of truth here and if it returns the wrong information, then there is a bug in initialization logic, not in the post processing.
Also I am not sure you tested this logic, the first argument in fileList is not a file, it's a name of the project, so the whole check here is meaningless. The AI just hallucinated it.
Fix: Filter workspace API files to exclude packages with their own configs
Problem
When using a Vitest workspace configuration (
vitest.workspace.ts) without a rootvitest.config.tsfile, the workspace API'sgetFiles()method was returning test files from all packages in the workspace, including packages that have their ownvitest.config.tsfiles. This caused test files to be incorrectly associated with the workspace API instead of their respective package APIs.This led to alias resolution failures - when test files use path aliases (e.g.,
import { something } from '#/somewhere'), they failed to resolve because the files were being processed by the workspace API context instead of their package-specific API context, which has the correct path alias configuration.Root Cause
The issue occurred because:
vitest.config.ts, Vitest doesn't report any configs invitest.configsfor the workspace APIusedConfigsset remains empty after workspace APIs are createdusedConfigs), but the workspace API still returns files from those packagesSolution
The fix filters the workspace API's
getFiles()results to exclude files that belong to packages with their own configs. This ensures that files from packages with their own configs are only returned by their package APIs, not by the workspace API.Implementation Details
Added
packageCwdstracking: Added apackageCwdsproperty toVitestFolderAPIto track package working directories that have their own configs.Added
setPackageCwds()method: Allows setting package directories on workspace APIs after all APIs are created.Modified
getFiles()filtering: Updated thegetFiles()method inVitestFolderAPIto filter out files that belong to packages with their own configs when it's a workspace API (identified by having aworkspaceFilebut noconfigFile).Post-creation filtering setup: After all APIs are created in
resolveVitestAPI(), the code now:cwddirectories from APIs that have their own config filesChanges
packages/extension/src/api.ts:packageCwds: string[]property toVitestFolderAPIclasssetPackageCwds(cwds: string[])methodgetFiles()to filter files based onpackageCwdsfor workspace APIsresolveVitestAPI()to collect and set packagecwddirectories on workspace APIsTesting
Reproduction Steps
Create a monorepo workspace with the following structure:
Configure
vitest.workspace.tsto include the package configs:Do NOT create a root
vitest.config.tsfileOpen the workspace in VS Code with the Vitest extension installed
Expected Behavior
getFiles()should only return files from the root project (if any)Verification
Related Issues
Fixes the issue described in
BUG_REPORT.mdwhere workspace APIs incorrectly return files from packages with their own configs, causing alias resolution failures.Notes
vitest.config.tsthat excludes everything (exclude: ['**'])