storybookjs / storybook

Storybook is the industry standard workshop for building, documenting, and testing UI components in isolation
https://storybook.js.org
MIT License
84.62k stars 9.31k forks source link

[Bug]: `bare-mdx-stories-glob` added duplicate `export const stories` #21127

Closed IanVS closed 1 year ago

IanVS commented 1 year ago

Describe the bug

I upgraded my project to beta.48, and allowed the bare-mdx-stories-glob to run.

To Reproduce

I don't have time to confirm the reproduction steps, but I have a main.ts with :

export const stories = [
  './Introduction.stories.mdx',
  '../src/App.stories.tsx',
  {
    directory: '../src/components/atoms',
    titlePrefix: 'Atoms',
  },
  {
    directory: '../src/components/patterns',
    titlePrefix: 'Patterns',
  },
  {
    directory: '../src/screens',
    titlePrefix: 'Screens',
  },
  {
    directory: '../src/global/styles',
    titlePrefix: 'Styles',
  },
];

Running the migration did not replace the original stories, it created a second stories export.

System

Environment Info:

  System:
    OS: macOS 13.1
    CPU: (16) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
  Binaries:
    Node: 18.12.1 - ~/.volta/tools/image/node/18.12.1/bin/node
    Yarn: 1.22.19 - ~/.volta/bin/yarn
    npm: 9.2.0 - ~/.volta/tools/image/npm/9.2.0/bin/npm
  Browsers:
    Firefox: 110.0
    Safari: 16.2
  npmPackages:
    @storybook/addon-a11y: 7.0.0-beta.48 => 7.0.0-beta.48
    @storybook/addon-actions: 7.0.0-beta.48 => 7.0.0-beta.48
    @storybook/addon-docs: 7.0.0-beta.48 => 7.0.0-beta.48
    @storybook/addon-essentials: 7.0.0-beta.48 => 7.0.0-beta.48
    @storybook/addon-interactions: 7.0.0-beta.48 => 7.0.0-beta.48
    @storybook/addon-links: 7.0.0-beta.48 => 7.0.0-beta.48
    @storybook/addon-postcss: ^3.0.0-alpha.1 => 3.0.0-alpha.1
    @storybook/addons: 7.0.0-beta.48 => 7.0.0-beta.48
    @storybook/expect: ^27.5.2-0 => 27.5.2-0
    @storybook/jest: ^0.0.11-next.0 => 0.0.11-next.0
    @storybook/react: 7.0.0-beta.48 => 7.0.0-beta.48
    @storybook/react-vite: 7.0.0-beta.48 => 7.0.0-beta.48
    @storybook/test-runner: 0.10.0-next.8 => 0.10.0-next.8
    @storybook/testing-library: ^0.0.14-next.1 => 0.0.14-next.1
    @storybook/testing-react: 2.0.0-next.0 => 2.0.0-next.0

Additional context

Migration log
🔎 checking possible migrations..

🔎 found a 'mdx1to2' migration:
╭ Automigration detected ──────────────────────────────────────────────────────────────────────────────────────────╮
│                                                                                                                  │
│   We've found 1 '.stories.mdx' files in your project.                                                            │
│                                                                                                                  │
│   Storybook has upgraded to MDX2 (https://mdxjs.com/blog/v2/), which contains breaking changes from MDX1.        │
│   We can try to automatically upgrade your MDX files to MDX2 format using some common patterns.                  │
│                                                                                                                  │
│   After this install completes, and before you start Storybook, we strongly recommend reading the MDX2 section   │
│   of the 7.0 migration guide. It contains useful tools for detecting and fixing any remaining issues.            │
│                                                                                                                  │
│   https://storybook.js.org/migration-guides/7.0                                                                  │
│                                                                                                                  │
╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
? Do you want to run the 'mdx1to2' migration on your project? › (Y/n)✔ Do you want to run the 'mdx1to2' migration on your project? … no

🔎 found a 'bare-mdx-stories-glob' migration:
╭ Automigration detected ────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│                                                                                                                                │
│   We've detected your project has one or more globs in your 'stories' config that matches .stories.mdx files:                  │
│     "./Introduction.stories.mdx"                                                                                               │
│     "../src/App.stories.tsx"                                                                                                   │
│     {                                                                                                                          │
│       "directory": "../src/components/atoms",                                                                                  │
│       "titlePrefix": "Atoms"                                                                                                   │
│     }                                                                                                                          │
│     {                                                                                                                          │
│       "directory": "../src/components/patterns",                                                                               │
│       "titlePrefix": "Patterns"                                                                                                │
│     }                                                                                                                          │
│     {                                                                                                                          │
│       "directory": "../src/screens",                                                                                           │
│       "titlePrefix": "Screens"                                                                                                 │
│     }                                                                                                                          │
│     {                                                                                                                          │
│       "directory": "../src/global/styles",                                                                                     │
│       "titlePrefix": "Styles"                                                                                                  │
│     }                                                                                                                          │
│                                                                                                                                │
│   In Storybook 7, we have deprecated defining stories in MDX files, and consequently have changed the suffix to simply .mdx.   │
│                                                                                                                                │
│   We can automatically migrate your 'stories' config to include any .mdx file instead of just .stories.mdx.                    │
│   That would result in the following 'stories' config:                                                                         │
│     "./Introduction.mdx"                                                                                                       │
│     "../src/App.stories.tsx"                                                                                                   │
│     {                                                                                                                          │
│       "directory": "../src/components/atoms",                                                                                  │
│       "titlePrefix": "Atoms"                                                                                                   │
│     }                                                                                                                          │
│     {                                                                                                                          │
│       "directory": "../src/components/patterns",                                                                               │
│       "titlePrefix": "Patterns"                                                                                                │
│     }                                                                                                                          │
│     {                                                                                                                          │
│       "directory": "../src/screens",                                                                                           │
│       "titlePrefix": "Screens"                                                                                                 │
│     }                                                                                                                          │
│     {                                                                                                                          │
│       "directory": "../src/global/styles",                                                                                     │
│       "titlePrefix": "Styles"                                                                                                  │
│     }                                                                                                                          │
│                                                                                                                                │
│   To learn more about this change, see: https://github.com/storybookjs/storybook/blob/next/MIGRATION.md#mdx-docs-files         │
│                                                                                                                                │
╰────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
? Do you want to run the 'bare-mdx-stories-glob' migration on your project? › (Y/n)✔ Do you want to run the 'bare-mdx-stories-glob' migration on your project? … yes
✅ Setting 'stories' config:
[
  "./Introduction.mdx",
  "../src/App.stories.tsx",
  {
    "directory": "../src/components/atoms",
    "titlePrefix": "Atoms"
  },
  {
    "directory": "../src/components/patterns",
    "titlePrefix": "Patterns"
  },
  {
    "directory": "../src/screens",
    "titlePrefix": "Screens"
  },
  {
    "directory": "../src/global/styles",
    "titlePrefix": "Styles"
  }
]
✅ ran bare-mdx-stories-glob migration

⚠️  failed to check fix autodocsTrue

SyntaxError: Identifier 'stories' has already been declared. (55:13)
    at instantiate (/Users/ianvs/.npm/_npx/eebb2d3a7d26a7f1/node_modules/@storybook/csf-tools/dist/index.js:15:2249)
    at constructor (/Users/ianvs/.npm/_npx/eebb2d3a7d26a7f1/node_modules/@storybook/csf-tools/dist/index.js:17:11555)
    at Parser.raise (/Users/ianvs/.npm/_npx/eebb2d3a7d26a7f1/node_modules/@storybook/csf-tools/dist/index.js:19:23819)
    at TypeScriptScopeHandler.checkRedeclarationInScope (/Users/ianvs/.npm/_npx/eebb2d3a7d26a7f1/node_modules/@storybook/csf-tools/dist/index.js:17:47378)
    at TypeScriptScopeHandler.declareName (/Users/ianvs/.npm/_npx/eebb2d3a7d26a7f1/node_modules/@storybook/csf-tools/dist/index.js:17:46611)
    at TypeScriptScopeHandler.declareName (/Users/ianvs/.npm/_npx/eebb2d3a7d26a7f1/node_modules/@storybook/csf-tools/dist/index.js:21:8994)
    at Parser.declareNameFromIdentifier (/Users/ianvs/.npm/_npx/eebb2d3a7d26a7f1/node_modules/@storybook/csf-tools/dist/index.js:21:97510)
    at Parser.checkIdentifier (/Users/ianvs/.npm/_npx/eebb2d3a7d26a7f1/node_modules/@storybook/csf-tools/dist/index.js:21:97410)
    at Parser.checkLVal (/Users/ianvs/.npm/_npx/eebb2d3a7d26a7f1/node_modules/@storybook/csf-tools/dist/index.js:21:95973)
    at Parser.parseVarId (/Users/ianvs/.npm/_npx/eebb2d3a7d26a7f1/node_modules/@storybook/csf-tools/dist/index.js:22:38688)

⚠️  failed to check fix missing-babelrc

SyntaxError: Identifier 'stories' has already been declared. (55:13)
    at instantiate (/Users/ianvs/.npm/_npx/eebb2d3a7d26a7f1/node_modules/@storybook/csf-tools/dist/index.js:15:2249)
    at constructor (/Users/ianvs/.npm/_npx/eebb2d3a7d26a7f1/node_modules/@storybook/csf-tools/dist/index.js:17:11555)
    at Parser.raise (/Users/ianvs/.npm/_npx/eebb2d3a7d26a7f1/node_modules/@storybook/csf-tools/dist/index.js:19:23819)
    at TypeScriptScopeHandler.checkRedeclarationInScope (/Users/ianvs/.npm/_npx/eebb2d3a7d26a7f1/node_modules/@storybook/csf-tools/dist/index.js:17:47378)
    at TypeScriptScopeHandler.declareName (/Users/ianvs/.npm/_npx/eebb2d3a7d26a7f1/node_modules/@storybook/csf-tools/dist/index.js:17:46611)
    at TypeScriptScopeHandler.declareName (/Users/ianvs/.npm/_npx/eebb2d3a7d26a7f1/node_modules/@storybook/csf-tools/dist/index.js:21:8994)
    at Parser.declareNameFromIdentifier (/Users/ianvs/.npm/_npx/eebb2d3a7d26a7f1/node_modules/@storybook/csf-tools/dist/index.js:21:97510)
    at Parser.checkIdentifier (/Users/ianvs/.npm/_npx/eebb2d3a7d26a7f1/node_modules/@storybook/csf-tools/dist/index.js:21:97410)
    at Parser.checkLVal (/Users/ianvs/.npm/_npx/eebb2d3a7d26a7f1/node_modules/@storybook/csf-tools/dist/index.js:21:95973)
    at Parser.parseVarId (/Users/ianvs/.npm/_npx/eebb2d3a7d26a7f1/node_modules/@storybook/csf-tools/dist/index.js:22:38688)

╭ Migration check ran with failures ───────────────────────────────────────────────────────────────────────────────────────────────────╮
│                                                                                                                                      │
│                                                                                                                                      │
│   Successful migrations:                                                                                                             │
│                                                                                                                                      │
│   bare-mdx-stories-glob                                                                                                              │
│                                                                                                                                      │
│   Failed migrations:                                                                                                                 │
│                                                                                                                                      │
│   autodocsTrue:                                                                                                                      │
│   Identifier 'stories' has already been declared. (55:13)                                                                            │
│                                                                                                                                      │
│   missing-babelrc:                                                                                                                   │
│   Identifier 'stories' has already been declared. (55:13)                                                                            │
│                                                                                                                                      │
│                                                                                                                                      │
│   You can find the full logs in /Users/ianvs/code/defined/webclient/migration-storybook.log                                          │
│                                                                                                                                      │
│   Skipped migrations:                                                                                                                │
│                                                                                                                                      │
│   mdx1to2                                                                                                                            │
│                                                                                                                                      │
│   ─────────────────────────────────────────────────                                                                                  │
│                                                                                                                                      │
│   If you'd like to run the migrations again, you can do so by running 'npx storybook@next automigrate'                               │
│                                                                                                                                      │
│   The automigrations try to migrate common patterns in your project, but might not contain everything needed to migrate to the       │
│   latest version of Storybook.                                                                                                       │
│                                                                                                                                      │
│   Please check the changelog and migration guide for manual migrations and more information:                                         │
│   https://storybook.js.org/migration-guides/7.0                                                                                      │
│   And reach out on Discord if you need help: https://discord.gg/storybook                                                            │
│                                                                                                                                      │
╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯

yannbf commented 1 year ago

Thanks Ian! Reproduction:

// main.js
export const stories = ['./Introduction.stories.mdx', '../src/App.stories.tsx']
export const framework = '@storybook/react-webpack5'

Cause: https://github.com/storybookjs/storybook/blob/next/code/lib/csf-tools/src/ConfigFile.ts#L280

shilman commented 1 year ago

Yowza!! I just released https://github.com/storybookjs/storybook/releases/tag/v7.0.0-beta.53 containing PR #21190 that references this issue. Upgrade today to the @next NPM tag to try it out!

npx sb@next upgrade --prerelease