bugfix-705: prevent alias resolution failure in monorepo setups. #706
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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: ['**'])