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

Vite: Fix wrong import paths when configDir is not in project root #30206

Merged
merged 1 commit into from
Jan 7, 2025

Conversation

JReinhold
Copy link
Contributor

@JReinhold JReinhold commented Jan 7, 2025

Closes #30147

What I did

This PR fixes a regression in #28941, that caused import paths to be relative to the wrong directory if the config directory (aka .storybook) was not a direct child of the project root. The problem was originally discussed in #28941 (comment) but was unfortunately missed in the end.

The idea of a custom projectRoot was good, but right now there are just a lot of implicit logic that depends on the path being relative to CWD, so the easiest fix was just to more or less revert c9107a1 .

cc @tobiasdiez

I applied these changes to the reproduction provided in the original issue and confirmed they fix it.

Checklist for Contributors

Testing

The changes in this PR are covered in the following automated tests:

  • stories
  • unit tests
  • integration tests
  • end-to-end tests

Manual testing

  1. Create a new Vite-based sandbox
  2. Move the .storybook directory to a nested one, eg. to deeply/nested/.storybook.
  3. Fix the stories glob in main.ts to be correctly relative to the new location
  4. Change the storybook script in package.json to point to the new directory with the flag -c deeply/nested/.storybook
  5. Run Storybook and ensure that stories correctly render.

This section is mandatory for all contributions. If you believe no manual test is necessary, please state so explicitly. Thanks!

Documentation

  • Add or update documentation reflecting your changes
  • If you are deprecating/removing a feature, make sure to update
    MIGRATION.MD

Checklist for Maintainers

  • When this PR is ready for testing, make sure to add ci:normal, ci:merged or ci:daily GH label to it to run a specific set of sandboxes. The particular set of sandboxes can be found in code/lib/cli-storybook/src/sandbox-templates.ts

  • Make sure this PR contains one of the labels below:

    Available labels
    • bug: Internal changes that fixes incorrect behavior.
    • maintenance: User-facing maintenance tasks.
    • dependencies: Upgrading (sometimes downgrading) dependencies.
    • build: Internal-facing build tooling & test updates. Will not show up in release changelog.
    • cleanup: Minor cleanup style change. Will not show up in release changelog.
    • documentation: Documentation only changes. Will not show up in release changelog.
    • feature request: Introducing a new feature.
    • BREAKING CHANGE: Changes that break compatibility in some way with current major version.
    • other: Changes that don't fit in the above categories.

🦋 Canary release

This PR does not have a canary release associated. You can request a canary release of this pull request by mentioning the @storybookjs/core team here.

core team members can create a canary release here or locally with gh workflow run --repo storybookjs/storybook canary-release-pr.yml --field pr=<PR_NUMBER>

name before after diff z %
createSize 0 B 0 B 0 B - -
generateSize 77.8 MB 77.8 MB 590 B 0.47 0%
initSize 131 MB 131 MB 406 B -1.36 0%
diffSize 53 MB 53 MB -184 B -1.36 0%
buildSize 7.19 MB 7.19 MB 0 B -0.33 0%
buildSbAddonsSize 1.85 MB 1.85 MB 0 B -0.31 0%
buildSbCommonSize 195 kB 195 kB 0 B - 0%
buildSbManagerSize 1.87 MB 1.87 MB 0 B 0.14 0%
buildSbPreviewSize 0 B 0 B 0 B - -
buildStaticSize 0 B 0 B 0 B - -
buildPrebuildSize 3.91 MB 3.91 MB 0 B -0.31 0%
buildPreviewSize 3.28 MB 3.28 MB 0 B -2.38 0%
testBuildSize 0 B 0 B 0 B - -
testBuildSbAddonsSize 0 B 0 B 0 B - -
testBuildSbCommonSize 0 B 0 B 0 B - -
testBuildSbManagerSize 0 B 0 B 0 B - -
testBuildSbPreviewSize 0 B 0 B 0 B - -
testBuildStaticSize 0 B 0 B 0 B - -
testBuildPrebuildSize 0 B 0 B 0 B - -
testBuildPreviewSize 0 B 0 B 0 B - -
name before after diff z %
createTime 23.6s 16.3s -7s -314ms 0.49 -44.8%
generateTime 21.9s 20.6s -1s -289ms 0.4 -6.2%
initTime 15.5s 14.7s -815ms 0.84 -5.5%
buildTime 8.7s 7.6s -1s -23ms -1.64 🔰-13.3%
testBuildTime 0ms 0ms 0ms - -
devPreviewResponsive 5.1s 4.5s -635ms -1.27 🔰-14.1%
devManagerResponsive 3.7s 3.4s -272ms -1.08 -7.9%
devManagerHeaderVisible 605ms 541ms -64ms -1.51 🔰-11.8%
devManagerIndexVisible 635ms 567ms -68ms -1.52 🔰-12%
devStoryVisibleUncached 2s 1.9s -105ms -0.06 -5.3%
devStoryVisible 636ms 568ms -68ms -1.73 🔰-12%
devAutodocsVisible 491ms 447ms -44ms -1.81 🔰-9.8%
devMDXVisible 525ms 487ms -38ms -0.95 -7.8%
buildManagerHeaderVisible 519ms 548ms 29ms -0.87 5.3%
buildManagerIndexVisible 610ms 636ms 26ms -0.97 4.1%
buildStoryVisible 510ms 525ms 15ms -1.03 2.9%
buildAutodocsVisible 427ms 450ms 23ms -0.87 5.1%
buildMDXVisible 485ms 463ms -22ms -0.39 -4.8%

Greptile Summary

This PR fixes import path resolution in the Vite builder when the Storybook config directory is not in the project root.

  • Removed projectRoot parameter from toImportFn in code/builders/builder-vite/src/codegen-importfn-script.ts to use process.cwd() consistently
  • Updated tests in codegen-importfn-script.test.ts to mock process.cwd() instead of passing root parameter
  • Reverted changes from PR Vite: Don't prefix story import with @fs #28941 in vite-config.ts to restore correct path resolution behavior
  • Removed projectRoot property from BuilderOptions interface in core-common.ts

Copy link

nx-cloud bot commented Jan 7, 2025

View your CI Pipeline Execution ↗ for commit c4542a2.

Command Status Duration Result
nx affected -t check -c production --parallel=7 ✅ Succeeded 1m 1s View ↗
nx run-many -t build -c production --parallel=3 ✅ Succeeded 3m 13s View ↗

☁️ Nx Cloud last updated this comment at 2025-01-07 12:59:08 UTC

@JReinhold JReinhold marked this pull request as ready for review January 7, 2025 13:02
Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

4 file(s) reviewed, 1 comment(s)
Edit PR Review Bot Settings | Greptile

Comment on lines +32 to +34
export async function toImportFn(stories: string[]) {
const objectEntries = stories.map((file) => {
const relativePath = relative(root, file);
const relativePath = relative(process.cwd(), file);
Copy link
Contributor

Choose a reason for hiding this comment

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

style: Removing the root parameter and hardcoding process.cwd() could make this function less flexible for future use cases where a different root directory is needed. Consider keeping the parameter but defaulting to process.cwd().

Copy link
Contributor

@tobiasdiez tobiasdiez left a comment

Choose a reason for hiding this comment

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

One small suggestion, otherwise lgtm. Thanks for the fix!

@JReinhold JReinhold merged commit b59cfc1 into next Jan 7, 2025
69 of 71 checks passed
@JReinhold JReinhold deleted the jeppe/fix-projectroot branch January 7, 2025 14:27
@github-actions github-actions bot mentioned this pull request Jan 7, 2025
13 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: importers[path] is not a function
3 participants