Skip to content

CLI: Improve yarn 1 vs Berry detection and log package manager during init#34427

Merged
Sidnioulz merged 5 commits into
project/sb-agentic-setupfrom
improve-yarn-version-detection
Apr 2, 2026
Merged

CLI: Improve yarn 1 vs Berry detection and log package manager during init#34427
Sidnioulz merged 5 commits into
project/sb-agentic-setupfrom
improve-yarn-version-detection

Conversation

@Sidnioulz
Copy link
Copy Markdown
Contributor

@Sidnioulz Sidnioulz commented Apr 1, 2026

  • Fix yarn 1 version detection regex (/^1\./)
  • Add Berry detection via packageManager field in package.json
  • Add Berry detection via .yarnrc.yml presence
  • Log package manager during sb init
  • Add unit tests for new detection logic
  • Apply comment suggestion from @Sidnioulz (improved inline comment wording)
  • Compute getProjectRoot() once in getYarnVersion() and pass result to helper functions to avoid repeated filesystem traversals

Summary by CodeRabbit

  • Bug Fixes

    • Improved Yarn detection: now considers package.json's packageManager and presence of Yarn Berry config in ancestor folders in addition to yarn --version, yielding more accurate Yarn 1/2/Berry identification.
  • New Features

    • The selected package manager is now logged during initialization with a friendly label.
  • Tests

    • Added extensive tests covering upward package.json walking, Yarn Berry config scenarios, and various Yarn version/reporting edge cases.

@nx-cloud
Copy link
Copy Markdown

nx-cloud Bot commented Apr 1, 2026

View your CI Pipeline Execution ↗ for commit 06315a0

Command Status Duration Result
nx run-many -t compile,check,knip,test,lint,fmt... ✅ Succeeded 8m 1s View ↗

☁️ Nx Cloud last updated this comment at 2026-04-02 17:45:37 UTC

@Sidnioulz Sidnioulz changed the base branch from next to project/sb-agentic-setup April 1, 2026 09:05
@Sidnioulz Sidnioulz requested a review from yannbf April 1, 2026 09:06
Comment thread code/core/src/common/js-package-manager/JsPackageManagerFactory.ts Outdated
@storybook-app-bot
Copy link
Copy Markdown

storybook-app-bot Bot commented Apr 1, 2026

Package Benchmarks

Commit: 06315a0, ran on 2 April 2026 at 17:48:21 UTC

The following packages have significant changes to their size or dependencies:

storybook

Before After Difference
Dependency count 50 50 0
Self size 20.47 MB 20.51 MB 🚨 +34 KB 🚨
Dependency size 16.55 MB 16.56 MB 🚨 +4 KB 🚨
Bundle Size Analyzer Link Link

@storybook/cli

Before After Difference
Dependency count 184 184 0
Self size 782 KB 806 KB 🚨 +24 KB 🚨
Dependency size 67.70 MB 67.98 MB 🚨 +279 KB 🚨
Bundle Size Analyzer Link Link

@storybook/codemod

Before After Difference
Dependency count 177 177 0
Self size 32 KB 32 KB 0 B
Dependency size 66.22 MB 66.26 MB 🚨 +39 KB 🚨
Bundle Size Analyzer Link Link

create-storybook

Before After Difference
Dependency count 51 51 0
Self size 1.04 MB 1.28 MB 🚨 +240 KB 🚨
Dependency size 37.03 MB 37.07 MB 🚨 +38 KB 🚨
Bundle Size Analyzer node node

eslint-plugin-storybook

Before After Difference
Dependency count 20 20 0
Self size 131 KB 131 KB 🎉 -6 B 🎉
Dependency size 3.41 MB 3.45 MB 🚨 +31 KB 🚨
Bundle Size Analyzer Link Link

@Sidnioulz Sidnioulz marked this pull request as ready for review April 1, 2026 09:07
Copilot AI review requested due to automatic review settings April 1, 2026 09:07
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Improves Storybook CLI package-manager handling by making Yarn Classic vs Yarn Berry detection more reliable and surfacing the detected package manager during sb init.

Changes:

  • Add human-friendly package manager logging during PreflightCheckCommand.execute().
  • Improve Yarn version detection by prioritizing packageManager in package.json, using .yarnrc.yml as a Berry signal, and fixing the Yarn 1 regex.
  • Add unit tests covering the new logging and Yarn Berry detection heuristics.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
code/lib/create-storybook/src/commands/PreflightCheckCommand.ts Logs the detected package manager with a friendly label during init.
code/lib/create-storybook/src/commands/PreflightCheckCommand.test.ts Adds a unit test asserting the package manager is logged.
code/core/src/common/js-package-manager/JsPackageManagerFactory.ts Enhances Yarn Classic vs Berry detection using packageManager and .yarnrc.yml, and fixes the Yarn 1 version regex.
code/core/src/common/js-package-manager/JsPackageManagerFactory.test.ts Adds coverage for Berry detection via .yarnrc.yml, packageManager, and version outputs.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread code/core/src/common/js-package-manager/JsPackageManagerFactory.ts Outdated
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 1, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: d57b7d70-1f34-4523-acbe-3685673b9b46

📥 Commits

Reviewing files that changed from the base of the PR and between bc20482 and 06315a0.

📒 Files selected for processing (2)
  • code/core/src/common/js-package-manager/JsPackageManagerFactory.test.ts
  • code/core/src/common/js-package-manager/JsPackageManagerFactory.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • code/core/src/common/js-package-manager/JsPackageManagerFactory.test.ts

📝 Walkthrough

Walkthrough

Adds upward-walking package.json detection and .yarnrc.yml probing to Yarn detection, reorders resolution priority (package.json → .yarnrc.yml → yarn --version), adjusts fallback behavior when yarn fails, expands Yarn/Berry tests, and logs the resolved package manager label in PreflightCheckCommand.

Changes

Cohort / File(s) Summary
Yarn Version Detection
code/core/src/common/js-package-manager/JsPackageManagerFactory.ts
Added getYarnVersionFromPackageJson() and hasYarnBerryConfig(); refactored getYarnVersion() to prefer ancestor package.json packageManager, then .yarnrc.yml, then yarn --version; updated fallback behavior when yarn --version throws.
Yarn Version Detection Tests
code/core/src/common/js-package-manager/JsPackageManagerFactory.test.ts
Expanded tests: mocked empathic/walk/empathic/find, ../utils/paths.getProjectRoot, and node:fs functions; added many Yarn2/Berry scenarios (including .yarnrc.yml, package.json packageManager variants, yarn version outputs, yarn failures) and upward-walking package.json parsing/error cases.
Preflight Check Logging
code/lib/create-storybook/src/commands/PreflightCheckCommand.ts, code/lib/create-storybook/src/commands/PreflightCheckCommand.test.ts
Added PACKAGE_MANAGER_LABEL mapping and a logger.info call to print the selected package manager label; added test asserting the logged label for Yarn Berry.

Sequence Diagram

sequenceDiagram
    participant Caller
    participant JsPM as "JsPackageManagerFactory"
    participant FS as "filesystem (read/find)"
    participant YarnCmd as "yarn --version"

    Caller->>JsPM: getYarnVersion(cwd)

    rect rgba(100,150,200,0.5)
    Note over JsPM,FS: Step 1 — walk upward for package.json with packageManager
    JsPM->>FS: findUp("package.json") up to project root
    FS-->>JsPM: path or undefined
    alt package.json found
        JsPM->>FS: readFileSync(package.json)
        FS-->>JsPM: contents / parse error
        alt packageManager declares yarn@<major>
            JsPM-->>Caller: return parsed major (1 or 2)
        end
    end
    end

    rect rgba(150,150,100,0.5)
    Note over JsPM,FS: Step 2 — check for .yarnrc.yml (Yarn Berry config)
    JsPM->>FS: findUp(".yarnrc.yml") up to project root
    FS-->>JsPM: path or undefined
    alt .yarnrc.yml found
        JsPM-->>Caller: return Yarn2
    end
    end

    rect rgba(150,100,150,0.5)
    Note over JsPM,YarnCmd: Step 3 — run `yarn --version`
    JsPM->>YarnCmd: execute --version
    YarnCmd-->>JsPM: output ("1.x"/"2.x"/"3.x"/error)
    alt output starts with "1."
        JsPM->>FS: findUp(".yarnrc.yml")
        FS-->>JsPM: path or undefined
        alt .yarnrc.yml exists
            JsPM-->>Caller: return Yarn2
        else
            JsPM-->>Caller: return Yarn1
        end
    else output >= 2
        JsPM-->>Caller: return parsed major
    else command failed
        JsPM->>FS: findUp(".yarnrc.yml")
        FS-->>JsPM: path or undefined
        alt .yarnrc.yml exists
            JsPM-->>Caller: return Yarn2
        else
            JsPM-->>Caller: return undefined
        end
    end
    end
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related PRs

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
code/core/src/common/js-package-manager/JsPackageManagerFactory.test.ts (1)

121-130: Prefer beforeEach helpers for these new mock setups.

These scenarios reconfigure findMock.up, executeCommandSyncMock, and readFileSyncMock inline inside individual it(...) blocks. Moving that setup into nested describe / beforeEach helpers would keep the new Berry cases isolated and make the suite less stateful.

As per coding guidelines, "Implement mock behaviors in beforeEach blocks in Vitest tests" and "Avoid inline mock implementations within test cases in Vitest tests".

Also applies to: 223-232, 324-484

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@code/core/src/common/js-package-manager/JsPackageManagerFactory.test.ts`
around lines 121 - 130, The test in JsPackageManagerFactory.test.ts is setting
mocks inline in individual it(...) blocks (e.g., findMock.up,
executeCommandSyncMock, readFileSyncMock); refactor by creating nested describe
blocks for the "Berry"/Yarn-Berry scenarios and move those mock implementations
into beforeEach hooks so each case gets isolated setup, and use afterEach or
beforeEach to reset or restore mocks (vi.resetAllMocks()/mockReset()) between
tests; if you need the real implementation (vi.importActual) do that inside the
beforeEach and assign the conditional mock behavior there so tests at lines
~121, ~223, ~324 are simplified and stateful inline mock code is removed.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@code/core/src/common/js-package-manager/JsPackageManagerFactory.ts`:
- Around line 257-277: getYarnVersionFromPackageJson currently stops at the
first package.json found (using find.up) even if that file lacks a
packageManager field; change it to walk upward from the provided cwd (or project
root fallback from getProjectRoot()) to the repository root and check every
package.json until one declares packageManager. Concretely, in
getYarnVersionFromPackageJson: start at cwd (or getProjectRoot()), loop up
directories until reaching the project root, at each directory check for
package.json (e.g., using find.up or fs.existsSync/readFileSync), parse and
inspect content.packageManager, and return 1 or 2 when a matching "yarn@X." is
found; only return undefined after the loop completes. Keep the same error
handling (try/catch around JSON.parse) and continue up on missing or unparsable
package.json files.

---

Nitpick comments:
In `@code/core/src/common/js-package-manager/JsPackageManagerFactory.test.ts`:
- Around line 121-130: The test in JsPackageManagerFactory.test.ts is setting
mocks inline in individual it(...) blocks (e.g., findMock.up,
executeCommandSyncMock, readFileSyncMock); refactor by creating nested describe
blocks for the "Berry"/Yarn-Berry scenarios and move those mock implementations
into beforeEach hooks so each case gets isolated setup, and use afterEach or
beforeEach to reset or restore mocks (vi.resetAllMocks()/mockReset()) between
tests; if you need the real implementation (vi.importActual) do that inside the
beforeEach and assign the conditional mock behavior there so tests at lines
~121, ~223, ~324 are simplified and stateful inline mock code is removed.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: c94cb81b-054e-4cc5-a08c-83dbec981396

📥 Commits

Reviewing files that changed from the base of the PR and between 15180fa and 6a7bd8d.

📒 Files selected for processing (4)
  • code/core/src/common/js-package-manager/JsPackageManagerFactory.test.ts
  • code/core/src/common/js-package-manager/JsPackageManagerFactory.ts
  • code/lib/create-storybook/src/commands/PreflightCheckCommand.test.ts
  • code/lib/create-storybook/src/commands/PreflightCheckCommand.ts

Comment thread code/core/src/common/js-package-manager/JsPackageManagerFactory.ts Outdated
Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
code/core/src/common/js-package-manager/JsPackageManagerFactory.test.ts (1)

25-33: Consider adding spy: true to empathic/walk mock for consistency with other mocks.

The new mocks for ../utils/paths and node:fs correctly use { spy: true }, but empathic/walk follows the existing pattern of empathic/find without it. While this is consistent within the file, it deviates from the coding guideline to use spy: true for all package mocks.

♻️ Optional: Add spy: true for consistency
-vi.mock('empathic/walk');
+vi.mock('empathic/walk', { spy: true });
 const walkMock = vi.mocked(walk);

Note: This would also apply to the existing empathic/find mock at line 22 for full consistency.

As per coding guidelines: "Use vi.mock() with the spy: true option for all package and file mocks in Vitest tests"

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@code/core/src/common/js-package-manager/JsPackageManagerFactory.test.ts`
around lines 25 - 33, The mock for 'empathic/walk' is missing the { spy: true }
option; update the vi.mock call for 'empathic/walk' to include { spy: true } so
walkMock (vi.mocked(walk)) is a spy like the other mocks, and also add { spy:
true } to the existing 'empathic/find' vi.mock to keep both package mocks
consistent with the project's Vitest guideline.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@code/core/src/common/js-package-manager/JsPackageManagerFactory.test.ts`:
- Around line 25-33: The mock for 'empathic/walk' is missing the { spy: true }
option; update the vi.mock call for 'empathic/walk' to include { spy: true } so
walkMock (vi.mocked(walk)) is a spy like the other mocks, and also add { spy:
true } to the existing 'empathic/find' vi.mock to keep both package mocks
consistent with the project's Vitest guideline.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 860fd4a8-e9ad-470b-b944-4f2ee7d3a632

📥 Commits

Reviewing files that changed from the base of the PR and between 6a7bd8d and bc20482.

📒 Files selected for processing (2)
  • code/core/src/common/js-package-manager/JsPackageManagerFactory.test.ts
  • code/core/src/common/js-package-manager/JsPackageManagerFactory.ts

@Sidnioulz
Copy link
Copy Markdown
Contributor Author

@yannbf should be good to re-review now, I've addressed the bunny feedback

Copy link
Copy Markdown
Member

@yannbf yannbf left a comment

Choose a reason for hiding this comment

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

LGTM! As long as sandbox generation works well I'm good with it!

@Sidnioulz Sidnioulz merged commit 11bc8f0 into project/sb-agentic-setup Apr 2, 2026
124 of 125 checks passed
@Sidnioulz Sidnioulz deleted the improve-yarn-version-detection branch April 2, 2026 18:00
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.

4 participants