Skip to content

Prototype step-by-step init and doctor subcommands - #1728

Closed
codykaup wants to merge 14 commits into
mainfrom
codykaup/init-doctor-prototype
Closed

codykaup wants to merge 14 commits into
mainfrom
codykaup/init-doctor-prototype

Conversation

@codykaup

@codykaup codykaup commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Prototype of a rebuilt chromatic init and a new chromatic doctor, modeled on storybook init, storybook doctor and storybook automigrate. It's a draft while it goes through review.

🎬 Demo

Recorded from 7ce1cc08 with a fake token and a local stand-in for the token check.

chromatic init in a pnpm monorepo with two Storybooks, where packages/ui has no build-storybook script: picks packages/ui, uses the token from the environment, picks GitHub Actions, then accepts "Set buildScriptName".

chromatic init demo

chromatic doctor, then chromatic doctor --fix in packages/ui of an npm monorepo with a misconfigured chromatic.config.json (valid JSON, wrong values), a GitHub workflow without fetch-depth, and a CircleCI job that also runs Chromatic: reports 7 warnings and 3 failures, then accepts all 9 fixes. The duplicate CI and pull_request warnings stay, since they have no automatic fix.

chromatic doctor demo

📋 What changed

Area Change
bin-src/setup/init.ts Step-by-step init: preflight, detect the project, check the token, write the config, install once, set up CI, check the result and offer fixes, then print next steps
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, token and CI
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/ci.ts CI pipelines parsed as YAML: one entry per job and directory that runs Chromatic, with its clone depth and --only-changed. GitHub and GitLab are read job by job; other providers are scanned for commands and treated as advisory
bin-src/setup/detect.ts, project.ts, ciTemplates.ts, report.ts, shared.ts Detection of projects and package manager, picking a project and its secret name, CI templates, and the boxed output
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, bin-src/init.ts Routes doctor, and routes init to the new code. Removes the old init with its test, and the boxen and prompts dependencies only it used

💡 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.
  • Today's init does very little. On Storybook projects it only adds a script. On Storybook 9+ E2E projects it fails, because it tries to install @storybook/addon-essentials at a version that was never published.
  • Storybook's CLI already solved this shape of problem. We own Storybook and want Chromatic's CLI to feel the same.

🛠️ How it works

chromatic init

  1. Preflight: git present, package manager, CLI and Node versions.
  2. Detect: Storybooks and Playwright, Cypress or Vitest configs. Asks which one only when there are several.
  3. Token: asks for the project token, or press Enter to use CHROMATIC_PROJECT_TOKEN (shows its last four characters). Checks it with the same exchange every build does, and offers to save it to a gitignored .env. Never writes it to package.json or the config file.
  4. Config: writes chromatic.config.json next to the project, with onlyChanged: true except for Playwright and Cypress. Reuses an existing .jsonc or .json5, and says what to add instead of rewriting one (that would drop its comments).
  5. Install once: adds chromatic (and @chromatic-com/<framework> for E2E) and the script, then runs one install.
  6. CI: one choice, with providers whose config files were found listed first. Writes .github/workflows/chromatic.yml, or chromatic-<path>.yml named like the secret for a monorepo project (on push, fetch-depth: 0, workingDir) and prints a snippet for other providers, outside the box so it pastes as valid YAML. Never overwrites a workflow. Skips when a CI job already runs Chromatic for the project.
  7. Check and fix: runs the doctor checks and offers each fix. Like doctor, exits 1 when a check still fails.
  8. Next steps: the secret to add and the command to run.

Flags: --project, --framework, --project-token, --ci <provider>, --skip-ci, --skip-install, --dry-run, --yes. Without a terminal or in CI it never waits for input.

chromatic doctor

  • 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.
  • Which project: from a project it checks that project; from the repo root, every project with Chromatic set up, grouped by project, and lists the rest as not set up.
  • Fixes (each shown as a diff, then confirmed):
Problem Fix
"onlyChanged": "true" (a string, read as a branch name) true
TurboSnap not turned on anywhere (config file, flags, the package's scripts, or the CI step) "onlyChanged": true, creating the config file if needed. Not when the config, a CI job or the chromatic script uploads a prebuilt storybookBuildDir, 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, only when CI runs Chromatic from the project too. 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. Only when the project's CI job passes its own token (CHROMATIC_PROJECT_TOKEN or the Action's projectToken); otherwise it says to give CI the token first
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
Shallow clone in GitHub Actions fetch-depth: 0 on the checkout steps of the job that runs Chromatic. Judged per job, so another job's full clone doesn't hide a shallow one. GitLab gets instructions instead, since a job-level variables block can replace one from an anchor or template
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
  • Warnings with no automatic fix, on purpose: a pull_request trigger and more than one CI running Chromatic for a project (choices, not mistakes), 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.

⚖️ Trade-offs & decisions

  • One CI, not several. Two CIs on the same commits make duplicate builds, so init asks for one and doctor warns when there are more.
  • Token now, browser sign-in later. OAuth needs work on the backend first.
  • 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.
  • Separate token secret per project in monorepos, named after the project's full path (CHROMATIC_PROJECT_TOKEN_PACKAGES_UI), so packages/ui and apps/ui can't collide. A single-project repo keeps CHROMATIC_PROJECT_TOKEN.
  • Parse CI files instead of matching text, and say when unsure. A comment mentioning chromatic used to count as a run, and one job's fetch-depth: 0 passed every job. GitHub and GitLab jobs are read with their working directory, extends order and pipeline defaults; a file that doesn't parse is reported rather than half-read. Other providers (CircleCI, Jenkins, …) get a command scan: "looks like it runs Chromatic", each file counted once toward duplicates, no clone-depth check, and init --ci overrides it. A scanned command with no cd counts for every project, since the provider may set the directory itself (CircleCI's working_directory, Jenkins' dir()), but fixes that change what CI reads rely only on jobs read from GitHub or GitLab.

🧪 Verification

Ran against 1c936df6, with a fake token and a local stand-in for the token check. Rows marked † ran against 5e7c8f0d, 0df2ce0c, 559d351c, 270938b8 or 2140141e and weren't affected by later commits.

Check Command Result
✅ init in a pnpm monorepo with two Storybooks (Vite and webpack) CI=1 node dist/bin.cjs init --yes --skip-install --project packages/ui --ci github Wrote the config, script and workflow; applied "Set buildScriptName to build:storybook"; "Checked again: 9 passed"; secret CHROMATIC_PROJECT_TOKEN_PACKAGES_UI
✅ doctor --fix --yes on a monorepo with a misconfigured config CI=1 node dist/bin.cjs doctor --fix --yes in packages/ui doctor reports 4 passed, 8 warnings, 2 failed and exits 1; --fix --yes applies 8 fixes and ends at 9 passed, 3 warnings (duplicate CI with CircleCI, pull_request, unbuilt storybookBuildDir)
✅ † A comment that mentions chromatic isn't a CI run doctor with # TODO: run npx chromatic in a workflow "No CI runs Chromatic yet"; before: counted as a run
✅ † Clone depth is per job doctor --fix --yes with fetch-depth: 0 only in another job Warns for the Chromatic job; the fix adds fetch-depth: 0 to that job's checkout only and leaves comments, [push, pull_request] and long lines as written
✅ † Existing chromatic.config.jsonc init --yes Leaves it alone and says to add "onlyChanged": true; no .json written next to it. Broken JSON: left alone, exit 0 with a pointer to doctor
✅ † 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 and init with storybook build -c storybook-config "Found Storybook at storybook-config/main.ts"; before: not found
✅ † packages/ui and apps/ui init --project … for each CHROMATIC_PROJECT_TOKEN_APPS_UI / _PACKAGES_UI, and chromatic.yml / chromatic-packages-ui.yml named Chromatic (<path>)
✅ † Tracked main.ts deleted from disk doctor and init 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; init --framework vitest on a Storybook repo Vitest only, onlyChanged kept; fails like a build, then sets "playwright": true; --framework overrides detection
✅ † CI read as it runs doctor with CHROMATIC_PROJECT_TOKEN=$X npx chromatic in CircleCI; two if: steps in one GitHub job; unclosed [ in a workflow; GitLab extends: [.full, .shallow] "looks like it runs Chromatic"; one job, no duplicate warning; "Couldn't read"; shallow, as GitLab's last parent wins
✅ Token never shown or committed doctor --fix --yes with { "projectToken" : "…" }; with .env then !.env in .gitignore; with !.env in packages/ui/.gitignore; init saving a pasted token with the same .gitignore Token never printed; with the negation, no move-token fix and init says "Didn't save the token"; without it, saved to an ignored .env
✅ † Rerun after a failed install init --yes twice with a dependency that doesn't exist Both runs install and exit 1; before: the second skipped the install and said "Chromatic is set up"
✅ † doctor --fix leaves what it can't be sure of Valid prebuilt out/ with onlyChanged; Playwright and Vitest configs with no flag; a shallow checkout behind a YAML alias (with: *clone) Config untouched, exits 0; fails naming both configs, no fix; shallow-clone warning without a fix (before: "Couldn't read")
✅ † GitLab snippet for a subdirectory init --project packages/ui --framework playwright --ci gitlab --dry-run, then ran the emitted lines in one shell with pwd for the commands Each line runs in packages/ui (before: the second cd packages/ui failed)
✅ † Snippets paste as YAML CI=1 init --project packages/ui --ci gitlab --dry-run --yes in a pnpm monorepo; --ci bitbucket in a Playwright app Printed without the box; the 100-character script line stays on one line and the copied text parses with yaml (before: wrapped at 80 columns with │ borders)
✅ † Fixes that depend on CI, 270938b8 vs. 2140141e doctor --fix --yes with: ./.env in .gitignore; storybookBuildDir built by an earlier CI step; a prebuilt out/ with no stats file; CI running from the root with --config-file packages/ui/chromatic.config.json; a token in the config and a workflow without a secret. init --yes --skip-ci with storybookConfigDir: "nowhere" Before (270938b8): ?? .env, removed storybookBuildDir, onlyChanged on a stats-less build, rewrote both paths, moved the token, init exit 0. After: .env ignored, folder kept with a warning, no TurboSnap fix, paths kept, token kept with "Give CI the token … first", init exits 1
✅ Large repos, prebuilt CI uploads and scanned CI, 2140141e vs. this commit doctor in a repo with 40,000 files whose workflow runs npx chromatic --only-changed; init --yes with a workflow whose chromaui/action step sets storybookBuildDir; doctor in packages/ui with CircleCI working_directory: ~/project/packages/ui; init --ci gitlab for two monorepo projects; doctor from a root with one project not set up Before: "No CI runs Chromatic yet", "onlyChanged": true written, "No CI job runs Chromatic for this project", two chromatic: jobs, "Found 1 projects". After: "TurboSnap is set in .github/workflows/ci.yml", no onlyChanged and no TurboSnap fix, "CircleCI looks like it runs Chromatic", chromatic-packages-ui: and chromatic-packages-web:, "Found 1 project"
✅ Yarn 2+ workflow init --ci github --yes in a project with packageManager: yarn@4.18.0 - run: corepack enable after setup-node, before yarn install --immutable
✅ † Round-1 fixes still hold CI=1 init on an open pipe; doctor --fix --yes with a token in the config; a pnpm install that fails Doesn't wait; token never printed and moved to a gitignored .env; install failure exits 1
✅ New tests catch the old bugs Put back, one at a time in ci.ts: file-wide clone depth, default YAML printing, no VAR= prefix, ignored parse errors, first extends parent wins, job's own depth ignored, global before_script ignored, no per-job merging; the ) after chromatic, the alias guard; in the snippet, a bare cd; in tokenEnvironmentEdits: the tracked, root and nested negation checks, dropping the first ignore check, a duplicated token line, no ignore rule after ./.env; in ci.ts: the job's env, the Action's projectToken, the command's CHROMATIC_PROJECT_TOKEN=, a ( before chromatic, a ) after --only-changed, a checkout of another repository, the Action's storybookBuildDir, a command's -d, merging -d across steps, a quote before npx chromatic, scanned runs matching every project (and parsed runs not); in the snippets, the job name and corepack enable; a second # Chromatic section in .gitignore A test fails for each
⚠️ Token check init with CHROMATIC_INDEX_URL pointed at a local stand-in Valid and invalid tokens handled; not run against the real index
⏭️ Publish to Chromatic — Not run: setup only, no build

👀 Reviewer notes

  • Start here: bin-src/setup/init.ts (main at the bottom reads as the step list), then bin-src/setup/doctorInput.ts and ci.ts, which decide what doctor believes.
  • Untested in places. ci.ts and the .env token helper have tests; the doctor checks and doctorInput don't yet. 10 functions still turn off the complexity lint rule (down from 14), 4 of them also max-statements: mostly the check lists, the token step and the two mains.
  • The doctor GIF is out of date: it still shows "Remove storybookBuildDir so Chromatic builds Storybook", which no longer exists. Today the unbuilt folder is a warning, so the run applies 8 fixes and ends at 9 passed, 3 warnings. The init GIF's flow is unchanged.
  • New dependency: yaml, bundled like the others.
  • Aikido's readFile warnings are about reading files in the user's own project, which the user can already read.
📦 Published PR as canary version: 18.15.2--canary.1728.44922920874.0

✨ Test out this PR locally via:

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

init detects the Storybook or E2E project, checks the project token,
writes chromatic.config.json, adds the package and script, sets up one
CI provider, then checks the result and offers fixes.

doctor checks a project's config, git history, token and CI setup,
and --fix applies fixes one at a time after showing a diff.

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 20, 2026
@coderabbitai

coderabbitai Bot commented Nov 20, 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/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/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/checks.ts Outdated
);
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/detect.ts Outdated
return detected.flatMap(({ provider, chromaticFiles }) =>
chromaticFiles
.filter((file) => {
const text = readFileSync(path.join(gitRoot, file), 'utf8').replaceAll(

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.

@github-actions

github-actions Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

📦 Package Size: 8180 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
boxen 9.0.0 —
chalk 1.1.3, 2.4.2, 4.1.2, 6.0.1 1.1.3, 2.4.2, 4.1.2
cli-boxes 4.0.1 —
get-east-asian-width 1.6.0, 1.7.0 1.6.0
is-fullwidth-code-point 1.0.0, 2.0.0, 5.1.0 1.0.0, 2.0.0
kleur 3.0.3 —
prompts 2.4.2 —
slice-ansi 0.0.4, 9.0.1 0.0.4
string-width 1.0.2, 2.1.1, 8.2.1, 8.3.0 1.0.2, 2.1.1, 8.2.1
widest-line 6.0.0 —
yaml — 2.9.1

GitHub Action

No bundled dependency changes.

Comment thread bin-src/setup/shared.ts Outdated

write(file: string, content: string) {
const existed = existsSync(file);
if (existed && readFileSync(file, 'utf8') === content) return false;

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
this.changes.push({ file, kind: existed ? 'updated' : 'created', content });
if (!this.dryRun) {
mkdirSync(path.dirname(file), { recursive: true });
writeFileSync(file, content);

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
*/
export function gitignoreAdditions(gitRoot: string, wanted: string[]) {
const file = path.join(gitRoot, '.gitignore');
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.

Remove the old init flow, now unrouted, with its test and the
dependencies only it used. Rename abbreviations, add JSDoc, move the
token step into its own file, and mark the long step functions as
prototype code for the complexity rules.

Amp-Thread-ID: https://ampcode.com/threads/T-01a1d7b7-69d0-75bd-a184-7440f6a67cef
Comment thread bin-src/setup/checks.ts Outdated
{
for (const { provider, file } of runs) {
// Drop comments, so a comment that mentions pull_request doesn't count as a trigger.
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/checks.ts Outdated
*/
export function environmentTokenEdits(input: DoctorInput, token: string) {
const environmentFile = path.join(input.cwd, '.env');
const text = existsSync(environmentFile) ? readFileSync(environmentFile, '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 Outdated

for (const file of files.filter((f) => STORYBOOK_MAIN.test(f))) {
const directory = path.join(gitRoot, path.dirname(path.dirname(file)));
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.

Comment thread bin-src/setup/detect.ts Outdated
return CI_PROVIDERS.map((provider) => {
const found = files.filter((file) => provider.patterns.some((p) => p.test(file)));
const chromaticFiles = found.filter((file) =>
RUNS_CHROMATIC.test(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/init.ts Outdated
function configStep(session: Session, project: Project) {
const { writer } = session;
const file = path.join(project.directory, '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/init.ts
async function packageStep(session: Session, project: Project, framework: Framework) {
const { writer } = session;
const file = path.join(project.directory, 'package.json');
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 - 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/token.ts Outdated
);
if (!save) return;
const environmentFile = path.join(project.directory, '.env');
const text = existsSync(environmentFile) ? readFileSync(environmentFile, '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.

Section titles are now nouns: Project, Configuration, and Git, token
and CI. The TurboSnap check also looks for --only-changed in the
package's scripts and onlyChanged in the CI step that runs Chromatic,
so it no longer reports TurboSnap off when there's no config file.
Long details wrap at the width Clack uses without a terminal.

Amp-Thread-ID: https://ampcode.com/threads/T-01a1d7b7-69d0-75bd-a184-7440f6a67cef
In a terminal, init shows the error and asks for the token again
instead of exiting. A rejected CHROMATIC_PROJECT_TOKEN is no longer
offered. With --project-token or without a terminal, a rejected token
still stops the run.

Amp-Thread-ID: https://ampcode.com/threads/T-01a1d7b7-69d0-75bd-a184-7440f6a67cef
init no longer waits on a fix prompt in CI, never overwrites an
existing workflow file, lets pnpm and Yarn Berry update the lockfile,
and exits 1 when the install fails. Yarn is detected from the
packageManager version. Fix previews mask project tokens, a token is
never moved into a .env that git tracks, and no fixes are offered when
the config file fails to load. AGENTS.md and the verify skill now
describe the new init and doctor.

Amp-Thread-ID: https://ampcode.com/threads/T-01a1d7b7-69d0-75bd-a184-7440f6a67cef
The CLI loads .env before init writes it, so set the variable the way
dotenv would after saving, and the check no longer reports no token.

Amp-Thread-ID: https://ampcode.com/threads/T-01a1d7b7-69d0-75bd-a184-7440f6a67cef
doctor and init guessed options, targets and CI facts with their own text
heuristics, then edited files on those guesses. They now share the main
command's sources of truth:

- CI files are parsed as YAML, one run per job that runs Chromatic. Clone
  depth, working directory and --only-changed are judged per job, and fixes
  edit only that job with comments and layout kept. Comments that mention
  chromatic no longer count as runs.
- Options come from getPartialOptions with the cwd's .env, so --build-command,
  --vitest, --project-token and conflicting options are honored. Config fixes
  are offered only for plain JSON the option comes from, never over a flag.
- Projects include Storybooks found through a build script's -c, skip
  tracked files deleted from disk, and --framework overrides detection.
- init reuses an existing chromatic.config.jsonc or .json5 instead of
  shadowing it, and leaves files it can't safely rewrite alone.
- In a monorepo, secrets and workflow files are named after the project's
  full path (CHROMATIC_PROJECT_TOKEN_PACKAGES_UI, chromatic-packages-ui.yml).

Amp-Thread-ID: https://ampcode.com/threads/T-01a1d7b7-69d0-75bd-a184-7440f6a67cef
Comment thread bin-src/setup/ci.ts
return 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 - 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.

);
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 - 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/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.

*/
function buildEnvironment(cwd: string) {
const environmentFile = path.join(cwd, '.env');
const fromFile = existsSync(environmentFile) ? dotenv.parse(readFileSync(environmentFile)) : {};

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.

- Fix previews mask the token a fix moves and the environment's token
  wherever they appear, not only in one JSON spelling.
- init and doctor save a token to .env through one helper that asks git
  whether the file is ignored, so a .gitignore that un-ignores .env (or a
  tracked .env) means the token isn't written there.
- init runs its install whenever install isn't skipped. A failed install
  had already edited package.json, so a rerun skipped it and said setup
  was done.
- GitHub workflow files are named like the secret: chromatic.yml, or
  chromatic-packages-ui.yml for a monorepo project.
- Drop the unused addDev commands and FileWriter.changes.

Amp-Thread-ID: https://ampcode.com/threads/T-01a1d7b7-69d0-75bd-a184-7440f6a67cef
Comment thread bin-src/setup/shared.ts
) {}

write(file: string, content: string) {
if (existsSync(file) && readFileSync(file, 'utf8') === content) return false;

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.

}
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.

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.

…s advisory

doctor worked out on its own what a build and a CI runner would do, and
each review found another case where its version differed. Now:

- The Storybook config directory comes from findConfigDirectory with the
  build script's -c, and storybookBuildDir goes through validateFiles, so a
  missing or invalid prebuilt directory fails and a valid one needs no
  Storybook source. Fixes that dropped storybookConfigDir as "the default"
  are gone, since -c would then decide instead.
- No framework is injected from detected config files: an E2E project
  that doesn't say so fails like a build would, with a fix that sets it.
  --config-file resolves from where doctor runs, for every project.
- CI commands may set variables first (as init's own snippets do), YAML
  that doesn't parse is reported instead of half-read, GitLab scripts and
  GIT_DEPTH follow extends order and the pipeline defaults, and steps in
  one job count as one job. GitLab and scanned providers get instructions
  instead of automatic edits, and init's --ci overrides an existing run.

Amp-Thread-ID: https://ampcode.com/threads/T-01a1d7b7-69d0-75bd-a184-7440f6a67cef
- Before writing a token to .env, git checks the ignore rule that would
  be added, so a !.env in a nested .gitignore means no write, like one in
  the root.
- storybookBuildDir is only removed when the folder isn't a valid build,
  as the build fails with it anyway. A valid prebuilt folder keeps its
  TurboSnap warning but gets no fix.
- No E2E fix when the folder has two test configs; which one runs is the
  user's call.
- A shallow checkout reached through a YAML alias is still reported, just
  without a fix, instead of the file being reported as unreadable.
- GitLab and Bitbucket snippets run each line in a subshell, so a cd no
  longer carries into the next line.

Amp-Thread-ID: https://ampcode.com/threads/T-01a1d7b7-69d0-75bd-a184-7440f6a67cef
- Always add the .env ignore rule once git says no existing line ignores
  it, so a rule git reads differently, like ./.env, can't leak the token.
- Never remove storybookBuildDir: CI may build the folder first. A missing
  folder is now a warning; an existing invalid one still fails.
- Don't turn on TurboSnap for a prebuilt Storybook, which may have no
  stats file.
- Rewrite storybookConfigDir or remove storybookBaseDir only when CI runs
  Chromatic from the project, not from the root with --config-file.
- Move projectToken out of the config file only when the project's CI job
  passes a token itself, and say why when there's no fix.
- Print CI snippets outside the note box so they paste as valid YAML.
- init exits 1 when a check still fails, like doctor.
- Read CI once per doctor run, recognise (npx chromatic --only-changed),
  skip checkouts of other repositories, and don't report missing CI when
  a CI file couldn't be read.

Amp-Thread-ID: https://ampcode.com/threads/T-01a1d7b7-69d0-75bd-a184-7440f6a67cef
Comment thread bin-src/setup/shared.ts
// Always add the pattern: git just said no existing line ignores the file, even one that looks
// like it should, such as ./.env.
const gitignore = path.join(gitRoot, '.gitignore');
const ignoreText = existsSync(gitignore) ? readFileSync(gitignore, '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.

…n init and doctor

- Lift git's 1 MiB output limit, so ls-files in a large repository isn't
  cut off and every file made to look absent.
- Don't turn on TurboSnap when a CI job or the chromatic script uploads a
  prebuilt Storybook with storybookBuildDir.
- Enable Corepack before installing with Yarn 2+ in the GitHub workflow,
  and with Yarn 2+ or pnpm in the other snippets.
- Count a scanned CI command with no cd for every project, since its
  provider may set the directory itself, but rely only on parsed jobs for
  fixes that change what CI reads and for duplicate warnings.
- Name each monorepo project's GitLab and CircleCI job like its workflow.
- Add to init's own .gitignore section instead of starting another.
- Fix doctor's project count wording and the empty parentheses after an
  unchanged config, hide init's 'No CI job runs Chromatic' after printing
  a snippet, drop findChromaticRuns and an unreachable branch, and fill
  in the JSDoc stubs lint warned about.

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

Copy link
Copy Markdown
Contributor Author

This PR is now split into a stack of three draft PRs (stack #1732):

  1. Add chromatic doctor to check a project's setup without a build #1729 — chromatic doctor (codykaup/setup-doctor, base main)
  2. Rebuild chromatic init as a step-by-step setup #1730 — the rebuilt chromatic init (codykaup/setup-init, base Add chromatic doctor to check a project's setup without a build #1729)
  3. Show in doctor what the Storybook preview reaches #1731 — doctor's preview reach check (codykaup/doctor-preview-reach, base Rebuild chromatic init as a step-by-step setup #1730)

The top of #1730 has the same tree as this PR's head (1c936df6). The demos, verification and decisions moved to the PR each belongs to. Closing this one in favor of the stack; the branch stays, since other work builds on it.

@codykaup codykaup closed this Nov 21, 2026
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