Skip to content

Conversation

@Lievesley
Copy link

Fix: Filter workspace API files to exclude packages with their own configs

Problem

When using a Vitest workspace configuration (vitest.workspace.ts) without a root vitest.config.ts file, the workspace API's getFiles() method was returning test files from all packages in the workspace, including packages that have their own vitest.config.ts files. 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:

  1. When there's no root vitest.config.ts, Vitest doesn't report any configs in vitest.configs for the workspace API
  2. The usedConfigs set remains empty after workspace APIs are created
  3. Package configs are correctly filtered out (they're in usedConfigs), but the workspace API still returns files from those packages
  4. This leads to duplicate file associations and incorrect API routing

Solution

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

  1. Added packageCwds tracking: Added a packageCwds property to VitestFolderAPI to track package working directories that have their own configs.

  2. Added setPackageCwds() method: Allows setting package directories on workspace APIs after all APIs are created.

  3. Modified getFiles() filtering: Updated the getFiles() method in VitestFolderAPI to filter out files that belong to packages with their own configs when it's a workspace API (identified by having a workspaceFile but no configFile).

  4. Post-creation filtering setup: After all APIs are created in resolveVitestAPI(), the code now:

    • Collects package cwd directories from APIs that have their own config files
    • Sets these directories on workspace APIs (APIs with workspace files but no config files)
    • This enables workspace APIs to filter out files from those packages

Changes

  • packages/extension/src/api.ts:
    • Added packageCwds: string[] property to VitestFolderAPI class
    • Added setPackageCwds(cwds: string[]) method
    • Modified getFiles() to filter files based on packageCwds for workspace APIs
    • Updated resolveVitestAPI() to collect and set package cwd directories on workspace APIs

Testing

Reproduction Steps

  1. Create a monorepo workspace with the following structure:

    workspace-root/
    ├── vitest.workspace.ts
    └── packages/
        ├── package-a/
        │   ├── vitest.config.ts
        │   └── test/
        │       └── test.spec.ts
        └── package-b/
            ├── vitest.config.ts
            └── test/
                └── test.spec.ts
    
  2. Configure vitest.workspace.ts to include the package configs:

    export default [
      './packages/package-a/vitest.config.ts',
      './packages/package-b/vitest.config.ts',
    ]
  3. Do NOT create a root vitest.config.ts file

  4. Open the workspace in VS Code with the Vitest extension installed

Expected Behavior

  • The workspace API's getFiles() should only return files from the root project (if any)
  • Package APIs should handle files from their respective packages
  • Path aliases should resolve correctly because files are processed by their package-specific API with the correct configuration
  • No duplicate file associations

Verification

  • Test files from packages with their own configs should not appear in the workspace API's file list
  • Path aliases in test files should resolve correctly
  • Test discovery and execution should work as expected for both workspace and package APIs

Related Issues

Fixes the issue described in BUG_REPORT.md where workspace APIs incorrectly return files from packages with their own configs, causing alias resolution failures.

Notes

  • This fix eliminates the need for the workaround of adding a root vitest.config.ts that excludes everything (exclude: ['**'])
  • The filtering is done at the API level, ensuring correct file-to-API associations throughout the extension

…les from workspaces that belong to packages with their own configs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant