Skip to content

Show in doctor what the Storybook preview reaches - #1731

Draft
codykaup wants to merge 2 commits into
codykaup/setup-initfrom
codykaup/doctor-preview-reach
Draft

codykaup wants to merge 2 commits into
codykaup/setup-initfrom
codykaup/doctor-preview-reach

Conversation

@codykaup

@codykaup codykaup commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Shows in chromatic doctor what the Storybook preview reaches and how often those files change. With TurboSnap, a change to any file the preview reaches recaptures every story, so this tells people how big that is and which import causes it. When TurboSnap is on but there's no stats file yet, doctor can build Storybook with stats first and then run the check. It sits on the init PR below it in the stack.

📸 Screenshots

No stats file, --yes: builds, then checks No stats file, CI without --yes
doctor --yes building Storybook with stats, then showing the preview check doctor in CI without --yes, saying how to build

Both are a React Vite project with TurboSnap on, whose .storybook/preview.tsx imports a barrel src/index.ts, with 8 commits spread over 200 days. In a terminal without --yes, doctor asks "Build Storybook with stats to check what preview imports?" before building.

📋 What changed

Area Change
bin-src/setup/previewReach.ts New check: reads the stats file, builds the TurboSnap manifest with the build's own buildManifest, and reports what preview reaches, by import, and how often it changed in git
bin-src/setup/checks.ts, report.ts New info status with a ● mark that summarize doesn't count; wrapped - list items are indented
bin-src/doctor.ts Runs the check after the others, unless the config or the options failed to load. Offers to build Storybook with stats when the check needs them, then prints the preview check
bin-src/setup/checks.ts A check can carry a build: the command and the stats file it writes
bin-src/setup/init.ts init's closing check lists only warnings and failures, so the info line doesn't show there

💡 Why

  • No safe rule can ignore preview's files automatically. Anything preview reaches can change every story, so TurboSnap has to recapture them all.
  • What's left is to show the size of the problem and its cause, so people can import the one file they need instead of a barrel.
  • doctor runs on the user's machine, not in CI, so it has the full git history to say how often those files actually change.

🛠️ How it works

What it reports Where it comes from
How many project and package files preview reaches attribution.previewSubtree from buildManifest, for the preview files in the config directory
Commits in the last 90 days that changed a project file behind preview, and a list of the 5 files changed most often, one per line with its commit count git log --since=90.days over those files. Skipped in a shallow clone. Packages change through the lockfile, so they aren't counted
The 5 imports that reach the most, with what only they reach Reach from preview with and without each import, over the manifest's dependencies
A barrel hint Any project file preview imports that imports 5 or more files itself
How to trace one file chromatic trace -s <stats file> <file>, which already prints the chain
  • Status: a warning when a commit changed a file behind preview, otherwise a pass. It never fails doctor and never offers a fix.

  • No stats file: an info line. It reads <storybookBuildDir or storybook-static>/preview-stats.json.

  • Building the stats file: when TurboSnap is on (onlyChanged) and Chromatic builds Storybook itself (no storybookBuildDir), doctor can run the same command a TurboSnap build runs, from the main command's setBuildCommand, into storybook-static. Then it prints the preview check.

    How doctor runs What it does
    In a terminal Asks first, because a build can take a while. No keeps the info line
    --yes, in a terminal or CI Builds without asking
    No terminal (CI or piped) and no --yes Never builds. Keeps the info line and says to run npx chromatic doctor --yes
    Build fails Writes its output to build-storybook.log, says so, and keeps the info line. doctor still exits 0

    Without TurboSnap, or with a prebuilt Storybook, the info line says how to build one, as before.

  • Skipped for Playwright, Cypress and Vitest projects, which have no preview of their own.

⚖️ Trade-offs & decisions

  • Read-only. It changes no hash and no capture decision.
  • Reach, not use. It says what preview reaches, which is what TurboSnap acts on. Telling reach apart from what preview actually uses would need data from the builder, which we decided not to pursue.
  • A build only on a yes. doctor otherwise never starts one: a Storybook build can take minutes, and CI shouldn't start one unasked. --yes now means "answer yes to everything", so it also builds when --fix isn't set.
  • Builds into storybook-static, where doctor looks for the stats file, so the next run is instant and the chromatic trace -s hint points at a file that exists. A Chromatic build uses a temp folder instead.
  • init doesn't build. Its closing check only shows warnings and failures, so the info line never shows there.

🧪 Verification

Ran against 197c315c on the original branch. After the rebase onto the CI change, the barrel row was rerun at 6b2c7f86 with the same numbers.

Check Command Result
✅ Barrel in preview node dist/bin.cjs doctor in the project in the first screenshot "reaches 10 project files and 4 package files"; "6 commits in the last 90 days"; src/index.ts is the top import, with all 10 project files only through it, flagged "like a barrel"
✅ This repository's own Storybook doctor after build-storybook --stats-json Preview reaches 0 project files and 10 package files
✅ No stats file doctor in a monorepo package with no build The info line, which the summary doesn't count
✅ File list doctor at b99f796e in the barrel project "Changed most often:" then src/theme/tokens.ts: 3 commits, src/utils/date.ts: 2 commits, src/utils/currency.ts: 1 commit
✅ Build with --yes in CI CI=1 doctor --yes after deleting the stats file Built in about 5 s, wrote storybook-static/preview-stats.json, then printed the preview check. No spinner frames in the log
✅ Asks in a terminal doctor in tmux, answered Yes, then again answering No Yes builds and shows the check; No leaves the info line and builds nothing
✅ No build in CI CI=1 doctor without the stats file Info line plus "Run npx chromatic doctor --yes to build it and check."; no stats file written; exit 0
✅ Failed build CI=1 doctor --yes with a build script given an unknown flag "Storybook build failed. See build-storybook.log"; the log has Storybook's error; exit 0
✅ Test catches building without TurboSnap Removed the onlyChanged guard The new test fails
✅ Tests catch a broken check Broke, one at a time: reach without the import, no time window, preview counted as its own file, packages counted as project files, never warning, unsorted counts, ignoring storybookBuildDir A test fails for each of the 7

👀 Reviewer notes

  • Start here: previewReachChecks at the bottom of bin-src/setup/previewReach.ts, then buildStats in bin-src/doctor.ts.
  • Not measured on a large project yet. The per-import breakdown has only run on small projects.
  • Built from a suggestion in the TurboSnap barrel research. Split out of Prototype step-by-step init and doctor subcommands #1728.
📦 Published PR as canary version: 18.15.2--canary.1731.45055248418.0

✨ Test out this PR locally via:

npm install chromatic@18.15.2--canary.1731.45055248418.0
# or 
yarn add chromatic@18.15.2--canary.1731.45055248418.0

@codykaup codykaup added minor Auto: Increment the minor version when merged skip-release Auto: Preserve the current version when merged labels Nov 21, 2026
@coderabbitai

coderabbitai Bot commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

@github-actions

github-actions Bot commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

📦 Package Size: 8060 KB

Bundled dependencies compared with PR base ae4f6f7.

Bundled dependency changes

Versions from emitted source maps; external dependencies are not included.

CLI / Node API

Package Base PR
storybook 10.6.6 10.6.7
zen-observable 0.8.15 0.10.0

GitHub Action

Package Base PR
storybook 10.6.6 10.6.7
zen-observable 0.8.15 0.10.0

…hanges

With TurboSnap, a change to any file the preview reaches recaptures
every story. When a stats file exists, doctor now builds the TurboSnap
manifest and reports how many project and package files preview
reaches, the imports that bring in the most (and what only they bring
in), and how many recent commits changed a project file behind it.
Without a stats file it says how to make one. The check is read-only:
it warns at most and offers no fix.

Amp-Thread-ID: https://ampcode.com/threads/T-01a1d7b7-69d0-75bd-a184-7440f6a67cef
@codykaup
codykaup force-pushed the codykaup/doctor-preview-reach branch from 3112d78 to 6b2c7f8 Compare November 22, 2026 16:24
…ith stats when doctor needs them

The preview check now lists the most-changed files one per line with their
commit counts, instead of packing them into one sentence.

When TurboSnap is on and Chromatic builds Storybook itself but there is no
stats file, doctor offers to run the same build command a TurboSnap build
would, then shows the preview check. In a terminal it asks first; --yes
builds without asking; without a terminal and without --yes it doesn't
build, and says to run doctor --yes.

Amp-Thread-ID: https://ampcode.com/threads/T-01a1d7b7-69d0-75bd-a184-7440f6a67cef

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

minor Auto: Increment the minor version when merged skip-release Auto: Preserve the current version when merged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant