storybookjs / storybook

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

[Bug]: Monorepo `getAbsolutePath` results in broken `project.json` #29664

Open shilman opened 6 days ago

shilman commented 6 days ago

Describe the bug

Currently Storybook's project.json reports the addons that are listed in .storybook/main.ts. It does this by calling getActualPacakgeVersion on each of the addons:

https://github.com/storybookjs/storybook/blob/next/code/core/src/telemetry/package-json.ts

However, in monorepos we recommend using absolute paths with the following pattern:

import { dirname, join } from 'node:path';

const getAbsolutePath = <I extends string>(input: I): I =>
  dirname(require.resolve(join(input, 'package.json'))) as any;

const config: StorybookConfig = {
  stories: ['../stories/**/*.mdx', '../stories/**/*.stories.@(js|jsx|mjs|ts|tsx)'],
  addons: [
    getAbsolutePath('@storybook/addon-onboarding'),
    getAbsolutePath('@storybook/addon-essentials'),
    getAbsolutePath('@chromatic-com/storybook'),
  ]
},

This generates addon entries like the following:

info     "addons": {
info       "$SNIP/node_modules/.pnpm/@storybook+addon-onboarding@8.5.0-alpha.8_react@18.3.1_storybook@8.5.0-alpha.8/node_modules/@storybook/addon-onboarding": {
info         "version": null
info       },
info       "$SNIP/node_modules/.pnpm/@storybook+addon-essentials@8.5.0-alpha.8_@types+react@18.3.12_storybook@8.5.0-alpha.8/node_modules/@storybook/addon-essentials": {
info         "version": null
info       },
info       "$SNIP/node_modules/.pnpm/@chromatic-com+storybook@3.2.2_react@18.3.1_storybook@8.5.0-alpha.8/node_modules/@chromatic-com/storybook": {
info         "version": null
info       },

Instead, it should produce:

info     "addons": {
info       "@storybook/addon-onboarding": {
info         "version": "8.5.0-alpha.8"
info       },
info       "@storybook/addon-essentials": {
info         "version": "8.5.0-alpha.8"
info       },
info       "@chromatic-com/storybook": {
info         "version": "3.2.2"
info       },

Reproduction link

N/A

Reproduction steps

See above

System

Any

Additional context

No response