Skip to content

Add chromatic doctor to check a project's setup without a build - #1729

Draft
codykaup wants to merge 2 commits into
mainfrom
codykaup/setup-doctor
Draft

codykaup wants to merge 2 commits into
mainfrom
codykaup/setup-doctor

Conversation

@codykaup

@codykaup codykaup commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Adds chromatic doctor, which checks a project's Chromatic setup without starting a build and fixes what it can be sure of. It's modeled on storybook doctor and storybook automigrate. This is the bottom of a stack: the rebuilt chromatic init sits on top of it and ends by running these checks.

🎬 Demo

chromatic doctor, then chromatic doctor --fix in packages/ui of an npm monorepo with a misconfigured chromatic.config.json (valid JSON, wrong values).

chromatic doctor demo

The GIF was recorded on an earlier commit that still read CI files, so it shows CI checks and fixes that no longer exist. The run today is in the verification table.

📋 What changed

Area Change
bin-src/doctor.ts New doctor subcommand: checks the setup without starting a build; --fix applies fixes one at a time after showing a diff
bin-src/setup/checks.ts, valueChecks.ts The checks, in three sections: Project, Configuration, and Git and token
bin-src/setup/doctorInput.ts Options the way a build sees them: the main command's getPartialOptions, config file and the project's .env, and which file owns each option
bin-src/setup/report.ts The boxed output and the fix loop. A fix can carry a question that replaces "Apply this fix?"
bin-src/setup/detect.ts, fixes.ts, shared.ts Detection of Storybook and E2E projects from git's file list, and file and git helpers
node-src/lib/getOptions.ts, getStorybookMetadata.ts Export findBuildScriptName and findConfigFlags so doctor finds the build script and -c folder the same way a build does
bin-src/register.js Routes doctor

💡 Why

  • Setup is hard to get right, especially TurboSnap. People lean on a separate helper script and on docs pages to work out path options, and many problems only show up as confusing results on later builds.
  • Storybook's CLI already solved this shape of problem. We own Storybook and want Chromatic's CLI to feel the same.

🛠️ How it works

  • Options and paths like a build's: reads the main command's flags (--build-command, --vitest, --project-token, --config-file, …), config file, environment and the project's .env through the main command's own option handling. The Storybook config directory comes from the build's findConfigDirectory, and --storybook-build-dir goes through the same validateFiles as an upload.
  • Only the project's own files. doctor never reads or edits CI files.
  • Which project: from a project it checks that project; from the repo root, every project with a Chromatic config file or a script that runs chromatic, grouped by project, and lists the rest as not set up.
  • Exit code: 1 when a check fails, 0 on warnings.
  • Fixes (each shown as a diff, then confirmed):
Problem Fix
"onlyChanged": "true" (a string, read as a branch name) true
TurboSnap not turned on in the config file, flags or a package script "onlyChanged": true, creating the config file if needed, after asking: "Does CI let Chromatic build Storybook, rather than pass --storybook-build-dir?" Not offered when the config or a script uploads a prebuilt Storybook, which may have no stats file
onlyChanged on Playwright or Cypress Remove it
storybookConfigDir written from the repo root, storybookBaseDir that repeats the directory Chromatic runs from Rewrite the path, or remove storybookBaseDir, after asking: "Does CI run Chromatic from packages/ui?" A CI that runs from the root with --config-file reads the paths as written
untraced / externals globs that match nothing Rewrite from the repo root
projectToken in the config file Move it to a .env that git confirms is ignored, including any nested .gitignore, after asking: "Does CI already pass the token as CHROMATIC_PROJECT_TOKEN?"
No build-storybook script Point buildScriptName at a script that runs storybook build, or add "build-storybook": "storybook build"
chromatic not a dependency Add it to devDependencies
E2E project with nothing saying so "playwright": true (or Cypress, Vitest) when the folder has exactly one of those config files; with two, a failure that names both and no fix
  • Questions and --yes: --yes applies every fix except those with a question, and names each one it skipped with the question to answer in a terminal.
  • Warnings with no automatic fix, on purpose: a missing token, a shallow local clone, and a storybookBuildDir folder that hasn't been built yet (CI may build it first). A folder that exists but isn't a Storybook build fails, still with no fix.
  • In CI or without a terminal, --fix applies nothing and says to rerun with --fix --yes.
  • Fixes only edit what owns the option: config fixes only when the option comes from a plain JSON config file and no flag overrides it. A config file that doesn't load, JSONC and JSON5 get no fix.
  • Large repositories: project detection streams git ls-files -z and keeps only package.json, Storybook main configs and E2E configs, so there's no output buffer to overflow and no scan per package. Glob counts for untraced and externals are taken in the same kind of single streamed pass.

⚖️ Trade-offs & decisions

  • No CI reading. An earlier version parsed GitHub and GitLab pipelines. Against 15 common ways a monorepo runs Chromatic for one package, it read 3 correctly. It missed pnpm --filter, turbo run --filter, nx run, yarn workspace, npm run --workspace, root scripts, shell scripts, composite actions, matrices and reusable workflows, because the package is chosen by name or at runtime, not written as a path. Getting those right would mean reimplementing each workspace tool and GitHub's expressions, so doctor doesn't try.
  • Ask, don't guess. The three fixes that are only right for some CI setups ask one question instead. With --yes or in CI they stay warnings with instructions, so nothing gets applied that could break a pipeline doctor can't see.
  • A known gap: when only CI passes --only-changed, doctor says "TurboSnap isn't on here", and the detail says it's on in CI if CI passes the flag.
  • Run from the project's own package. This layout needs no storybookBaseDir or storybookConfigDir. Making a config file in another directory act as the project root is deferred, because it would break setups that already compensate with root-relative paths.

🧪 Verification

Ran against 4cd84719, with a fake token. Rows marked † ran on earlier commits of #1728 and cover code this PR didn't change since.

Check Command Result
✅ doctor on a monorepo with a misconfigured config CI=1 node dist/bin.cjs doctor in packages/ui 3 passed, 6 warnings, 2 failed; exit 1
✅ --fix --yes skips the questions CI=1 node dist/bin.cjs doctor --fix --yes on the same project Applies 4 fixes (dependency, onlyChanged string, both globs); skips the storybookConfigDir, storybookBaseDir and token fixes, each with its question; ends at 7 passed, 3 warnings, 1 failed, exit 1. The token stays in the config
✅ Answering the questions doctor --fix in a real terminal (tmux), Enter at each prompt Each question replaces "Apply this fix?"; all 7 fixes apply, the token moves to an ignored .env, ends at 8 passed, 1 warning
✅ Large repository doctor in packages/ui of a repo with 300,000 tracked files and 1,505 package.json files 0.9 s, 126 MB peak. Before: 1 min 58 s, 397 MB. From the root: 1.1 s
✅ The question can't be skipped by --yes Removed the skip in applyFixes, reran report.test.ts The test fails
✅ † doctor resolves paths like a build --build-command with no build script; --build-command with -c missing in the build script; --storybook-build-dir missing, then a prebuilt folder with no .storybook; an empty storybookConfigDir; --project packages/ui --config-file configs/selected.json No build-script failure; finds .storybook as the build does; fails, then passes; fails; reads selected.json
✅ † Storybook found through -c doctor with storybook build -c storybook-config "Found Storybook at storybook-config/main.ts"; before: not found
✅ † Tracked main.ts deleted from disk doctor from the root No crash; that Storybook is skipped
✅ † E2E projects doctor --vitest next to playwright.config.ts; doctor --fix --yes in a Playwright app with no config Vitest only, onlyChanged kept; fails like a build, then sets "playwright": true
✅ † Token never shown or committed doctor --fix with { "projectToken" : "…" }; with .env then !.env in .gitignore; with !.env in packages/ui/.gitignore Token never printed; with the negation, no move-token fix; without it, moved to an ignored .env
✅ † Tests catch old .env bugs In tokenEnvironmentEdits, put back: the tracked, root and nested negation checks, dropping the first ignore check, a duplicated token line, no ignore rule after ./.env, a second # Chromatic section A test fails for each
⏭️ Publish to Chromatic — Not run: setup only, no build

👀 Reviewer notes

  • Start here: bin-src/setup/doctorInput.ts, which decides what doctor believes, then valueChecks.ts for the fixes that ask a question.
  • Untested in places. The .env token helper and the fix loop's question have tests; the doctor checks and doctorInput don't yet. Several functions still turn off the complexity lint rule, mostly the check lists and main.
  • Aikido's readFile warnings are about reading files in the user's own project, which the user can already read.
  • Split out of Prototype step-by-step init and doctor subcommands #1728, where rounds 1–5 of review happened. The CI reading those rounds reviewed is gone.
📦 Published PR as canary version: 18.15.2--canary.1729.44930361334.0

✨ Test out this PR locally via:

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

…t a build

doctor finds each Storybook project in the repository and checks its
config file, package.json script, git history, project token and CI
jobs, the way a build would read them. Each problem says why it matters,
and --fix applies the fixes that are certain: plain JSON config edits,
the build script, .gitignore and CI checkout depth when a CI job is
confirmed. It exits 1 when a check fails and 0 on warnings.

Amp-Thread-ID: https://ampcode.com/threads/T-01a1d7b7-69d0-75bd-a184-7440f6a67cef
@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

Comment thread bin-src/setup/shared.ts
*/
export function readJson(filePath: string): Record<string, any> | undefined {
try {
return JSON.parse(readFileSync(filePath, 'utf8'));

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Potential file inclusion attack via reading file - high severity
If an attacker can control the input leading into the ReadFile function, they might be able to read sensitive files and launch further attacks with that information.

Show fix

Remediation: Ignore this issue only after you've verified or sanitized the input going into this function. This issue is only relevant in the backend, not in the frontend!

Reply @AikidoSec ignore: [REASON] to ignore this issue.
More info

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

These paths come from the user's own project or command line, and the CLI runs with the user's permissions, so it can only read files the user can already read. Nothing to fix here.

Comment thread bin-src/setup/fixes.ts
* @returns The edit for a fix.
*/
export function packageJsonEdit(file: string, change: (json: Record<string, any>) => void) {
const previous = readFileSync(file, 'utf8');

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Potential file inclusion attack via reading file - high severity
If an attacker can control the input leading into the ReadFile function, they might be able to read sensitive files and launch further attacks with that information.

Show fix

Remediation: Ignore this issue only after you've verified or sanitized the input going into this function. This issue is only relevant in the backend, not in the frontend!

Reply @AikidoSec ignore: [REASON] to ignore this issue.
More info

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

These paths come from the user's own project or command line, and the CLI runs with the user's permissions, so it can only read files the user can already read. Nothing to fix here.

Comment thread bin-src/setup/ci.ts Outdated
const runs = detectCi(files).flatMap(({ provider, files: found }) =>
found.flatMap((file) => {
try {
const raw = readFileSync(path.join(gitRoot, file), 'utf8');

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Potential file inclusion attack via reading file - medium severity
If an attacker can control the input leading into the ReadFile function, they might be able to read sensitive files and launch further attacks with that information.

Show fix

Remediation: Ignore this issue only after you've verified or sanitized the input going into this function. This issue is only relevant in the backend, not in the frontend!

Reply @AikidoSec ignore: [REASON] to ignore this issue.
More info

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

These paths come from the user's own project or command line, and the CLI runs with the user's permissions, so it can only read files the user can already read. Nothing to fix here.

Comment thread bin-src/setup/detect.ts

function storybookProject(gitRoot: string, relative: string, file: string): Project {
const directory = path.join(gitRoot, relative);
const main = readFileSync(path.join(gitRoot, file), 'utf8');

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Potential file inclusion attack via reading file - medium severity
If an attacker can control the input leading into the ReadFile function, they might be able to read sensitive files and launch further attacks with that information.

Show fix

Remediation: Ignore this issue only after you've verified or sanitized the input going into this function. This issue is only relevant in the backend, not in the frontend!

Reply @AikidoSec ignore: [REASON] to ignore this issue.
More info

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

These paths come from the user's own project or command line, and the CLI runs with the user's permissions, so it can only read files the user can already read. Nothing to fix here.

}
if (!configuration.configFile) return { configuration, configEditable: true };
const configFile = path.resolve(cwd, configuration.configFile);
const configText = readFileSync(configFile, 'utf8');

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Potential file inclusion attack via reading file - medium severity
If an attacker can control the input leading into the ReadFile function, they might be able to read sensitive files and launch further attacks with that information.

Show fix

Remediation: Ignore this issue only after you've verified or sanitized the input going into this function. This issue is only relevant in the backend, not in the frontend!

Reply @AikidoSec ignore: [REASON] to ignore this issue.
More info

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

These paths come from the user's own project or command line, and the CLI runs with the user's permissions, so it can only read files the user can already read. Nothing to fix here.

);
change(json);
const file = input.configFile ?? path.join(input.cwd, 'chromatic.config.json');
const previous = existsSync(file) ? readFileSync(file, 'utf8') : undefined;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Potential file inclusion attack via reading file - medium severity
If an attacker can control the input leading into the ReadFile function, they might be able to read sensitive files and launch further attacks with that information.

Show fix

Remediation: Ignore this issue only after you've verified or sanitized the input going into this function. This issue is only relevant in the backend, not in the frontend!

Reply @AikidoSec ignore: [REASON] to ignore this issue.
More info

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

These paths come from the user's own project or command line, and the CLI runs with the user's permissions, so it can only read files the user can already read. Nothing to fix here.

Comment thread bin-src/setup/report.ts
const { fix } = next;
const preview = fix.edits
.map(({ file, content }) => {
const before = existsSync(file) ? readFileSync(file, 'utf8') : '';

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Potential file inclusion attack via reading file - medium severity
If an attacker can control the input leading into the ReadFile function, they might be able to read sensitive files and launch further attacks with that information.

Show fix

Remediation: Ignore this issue only after you've verified or sanitized the input going into this function. This issue is only relevant in the backend, not in the frontend!

Reply @AikidoSec ignore: [REASON] to ignore this issue.
More info

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

These paths come from the user's own project or command line, and the CLI runs with the user's permissions, so it can only read files the user can already read. Nothing to fix here.

Comment thread bin-src/setup/shared.ts
reason: `git tracks ${relativeOrDot(gitRoot, file)}, so the token would be committed`,
};
}
const text = existsSync(file) ? readFileSync(file, 'utf8') : '';

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Potential file inclusion attack via reading file - medium severity
If an attacker can control the input leading into the ReadFile function, they might be able to read sensitive files and launch further attacks with that information.

Show fix

Remediation: Ignore this issue only after you've verified or sanitized the input going into this function. This issue is only relevant in the backend, not in the frontend!

Reply @AikidoSec ignore: [REASON] to ignore this issue.
More info

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

These paths come from the user's own project or command line, and the CLI runs with the user's permissions, so it can only read files the user can already read. Nothing to fix here.

@github-actions

github-actions Bot commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

📦 Package Size: 8168 KB

Bundled dependencies compared with PR base 82312b5.

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

CI files can't say reliably which project a job builds: monorepos pick
the package through pnpm, Turbo, Nx or Yarn workspaces, a script, a
composite action, or a matrix value, so doctor missed most real setups
and read the rest as the wrong project. doctor now reads only the
project's own files. The three fixes that are only right for some CI
setups (moving the token out of the config, rewriting
storybookConfigDir or removing storybookBaseDir, and turning on
onlyChanged) ask how CI runs Chromatic instead, and --yes skips them.

Project detection now streams git's file list and keeps only the files
that can make a project, so a large repository needs no output buffer
and no scan per package.json.

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