Skip to content

fix(vitest)!: resolve reportsDirectory against workspace root#34720

Merged
llwt merged 9 commits intomasterfrom
fix/vitest-reports-directory-workspace-root
Mar 9, 2026
Merged

fix(vitest)!: resolve reportsDirectory against workspace root#34720
llwt merged 9 commits intomasterfrom
fix/vitest-reports-directory-workspace-root

Conversation

@llwt
Copy link
Member

@llwt llwt commented Mar 5, 2026

Current Behavior

When reportsDirectory is configured with {workspaceRoot} token in nx.json targetDefaults:

"@nx/vitest:test": {
  "options": {
    "reportsDirectory": "{workspaceRoot}/coverage/{projectRoot}"
  }
}

Coverage output lands in the wrong location. For example, with a project at apps/my-app, coverage goes to apps/my-app/coverage/apps/my-app/ instead of the intended coverage/apps/my-app/.

Expected Behavior

Coverage output should be written to <workspaceRoot>/coverage/apps/my-app/.

Root Cause

Nx's resolveNxTokensInOptions strips {workspaceRoot}/ from option values and replaces {projectRoot}, producing a workspace-root-relative path (e.g. coverage/apps/my-app). The vitest executor then passed this directly to vitest, which resolved it relative to the project root — not the workspace root.

Fix

Resolve non-absolute reportsDirectory paths against the workspace root before passing them to vitest, so vitest writes coverage to the correct location.

Test Plan

  • Added unit tests for the new resolveReportsDirectory helper
  • Verified with a reproduction workspace that coverage now lands at the correct path

When reportsDirectory is set with {workspaceRoot} token in nx.json
targetDefaults, Nx's resolveNxTokensInOptions strips the token and
produces a workspace-root-relative path. The vitest executor then
passed this path directly to vitest, which resolved it relative to
the project root, causing coverage output to land in the wrong
location (e.g. apps/my-app/coverage/apps/my-app/ instead of
coverage/apps/my-app/).

This fix resolves non-absolute reportsDirectory paths against the
workspace root before passing them to vitest, ensuring coverage
output goes to the intended location.
@netlify
Copy link

netlify bot commented Mar 5, 2026

Deploy Preview for nx-dev ready!

Name Link
🔨 Latest commit 1b73565
🔍 Latest deploy log https://app.netlify.com/projects/nx-dev/deploys/69aee2663fbafd0008d68bcf
😎 Deploy Preview https://deploy-preview-34720--nx-dev.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@netlify
Copy link

netlify bot commented Mar 5, 2026

Deploy Preview for nx-docs ready!

Name Link
🔨 Latest commit 1b73565
🔍 Latest deploy log https://app.netlify.com/projects/nx-docs/deploys/69aee2669a7cb10007e18a6a
😎 Deploy Preview https://deploy-preview-34720--nx-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@nx-cloud
Copy link
Contributor

nx-cloud bot commented Mar 5, 2026

View your CI Pipeline Execution ↗ for commit 1b73565

Command Status Duration Result
nx affected --targets=lint,test,build,e2e,e2e-c... ✅ Succeeded 2m 24s View ↗
nx run-many -t check-imports check-lock-files c... ✅ Succeeded 2m 18s View ↗
nx-cloud record -- nx-cloud conformance:check ✅ Succeeded 7s View ↗
nx-cloud record -- nx format:check ✅ Succeeded 1s View ↗
nx-cloud record -- nx sync:check ✅ Succeeded <1s View ↗

☁️ Nx Cloud last updated this comment at 2026-03-09 15:18:25 UTC

@llwt llwt marked this pull request as ready for review March 5, 2026 14:32
@llwt llwt requested a review from a team as a code owner March 5, 2026 14:32
@llwt llwt requested a review from leosvelperez March 5, 2026 14:32
nx-cloud[bot]

This comment was marked as outdated.

The reportsDirectory option is now resolved relative to the workspace root
instead of the project root. This migration prepends {projectRoot}/ to
existing reportsDirectory values so the resolved path remains the same.
@llwt llwt force-pushed the fix/vitest-reports-directory-workspace-root branch from 1d60b83 to a820b13 Compare March 5, 2026 22:35
@@ -17,6 +17,11 @@
},
"description": "Create AI Instructions to help migrate users workspaces past breaking changes for Vitest 4.",
"implementation": "./src/migrations/update-22-1-0/create-ai-instructions-for-vitest-4"
},
"update-22-6-0-prefix-reports-directory": {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a huge deal, but targeting the next beta (22.6.0-beta.10) would help anyone who already updated to beta.0. Mostly it's just us.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

nx-cloud[bot]

This comment was marked as outdated.

llwt added 2 commits March 6, 2026 19:22
…rojectRoot}

Previously the migration checked if {projectRoot} appeared anywhere in
the path, which incorrectly skipped paths like coverage/{projectRoot}.
…paceRoot}

Paths starting with {workspaceRoot} already indicate the user intended
workspace-root-relative resolution, so they should not be prefixed with
{projectRoot}.
nx-cloud[bot]

This comment was marked as outdated.

@llwt llwt changed the title fix(vitest): resolve reportsDirectory against workspace root fix(vitest)!: resolve reportsDirectory against workspace root Mar 6, 2026
@llwt llwt enabled auto-merge (squash) March 6, 2026 19:28
nx-cloud[bot]

This comment was marked as outdated.

@llwt llwt disabled auto-merge March 6, 2026 20:20
@llwt llwt enabled auto-merge (squash) March 6, 2026 20:23
nx-cloud[bot]

This comment was marked as outdated.

The generator was producing project-root-relative reportsDirectory
paths (e.g., ../../coverage/libs/my-lib) but the executor now resolves
these against the workspace root, causing coverage output to land
outside the workspace. Remove the offsetFromRoot prefix so the
generated path is workspace-root-relative (e.g., coverage/libs/my-lib).

Also update the e2e test to use workspace-root-relative path.
nx-cloud[bot]

This comment was marked as outdated.

nx-cloud[bot]

This comment was marked as outdated.

Copy link
Contributor

@nx-cloud nx-cloud bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nx Cloud is proposing a fix for your failed CI:

These changes fix the failing vue:test by updating the test snapshot to reflect the new workspace-root-relative behavior for reportsDirectory. The PR changed the vitest generator to produce paths like "coverage/my-lib" instead of "../coverage/my-lib", and this test needs to expect the new format.

Warning

We could not verify this fix.

diff --git a/packages/vue/src/generators/library/library.spec.ts b/packages/vue/src/generators/library/library.spec.ts
index 9df5db693e..40d53d5450 100644
--- a/packages/vue/src/generators/library/library.spec.ts
+++ b/packages/vue/src/generators/library/library.spec.ts
@@ -586,7 +586,7 @@ module.exports = [
                   "{options.reportsDirectory}"
                 ],
                 "options": {
-                  "reportsDirectory": "../coverage/my-lib"
+                  "reportsDirectory": "coverage/my-lib"
                 }
               }
             }

🔔 Heads up, your workspace has pending recommendations ↗ to auto-apply fixes for similar failures.

Apply fix via Nx Cloud  Reject fix via Nx Cloud


Or Apply changes locally with:

npx nx-cloud apply-locally juld-AVbR

Apply fix locally with your editor ↗   View interactive diff ↗



🎓 Learn more about Self-Healing CI on nx.dev

@llwt llwt requested a review from a team as a code owner March 9, 2026 15:08
@llwt llwt requested a review from Coly010 March 9, 2026 15:08
@llwt llwt merged commit 654118a into master Mar 9, 2026
24 checks passed
@llwt llwt deleted the fix/vitest-reports-directory-workspace-root branch March 9, 2026 15:18
FrozenPandaz added a commit that referenced this pull request Mar 9, 2026
…34766)

## Current Behavior

The webpack legacy e2e test
(`e2e-webpack:e2e-ci--src/webpack.legacy.test.ts`) fails with a snapshot
mismatch because the `reportsDirectory` value changed from
`../coverage/app3224373` to `coverage/app3224373`.

## Expected Behavior

The snapshot should match the new `reportsDirectory` path format
introduced by #34720.

## Related Issue(s)

Fixes the e2e test breakage introduced by #34720 (`fix(vitest)!: resolve
reportsDirectory against workspace root`).
@github-actions
Copy link
Contributor

This pull request has already been merged/closed. If you experience issues related to these changes, please open a new issue referencing this pull request.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 15, 2026
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 this pull request may close these issues.

2 participants