Repository navigation
React: Guard docgen extraction against TypeScript builds without the compiler API - #35834
Conversation
… the compiler API TypeScript 7's package only exports version info from its main entry, so can resolve without the compiler API ( is undefined). Fail fast in the ComponentMetaManager constructor with a clear, actionable error instead of crashing deep in project setup; make the two degradation points log the real reason instead of a misleading "TypeScript not available"; and drop a stray console.log in getTsConfig. Closes storybookjs#35792
3de5f8c to
52685b7
Compare
|
Hey @FrancoKaddour, thanks for this. I rebased the branch on We plan to make docgen work with TypeScript 7 by falling back to We'd like to move forward with the TS 7.1 compatibility work, and I'd like to give credit where it's due, so I'll finalize and merge your PR. The team can then wrap up the remainder of what's required for #35792 in a separate PR. |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info
WalkthroughComponentMetaManager now validates the TypeScript compiler API before setup. The component metadata singleton and React docgen worker log caught TypeScript initialization errors at debug level and continue to return ChangesTypeScript initialization
Priority: ➖ Normal Merge Risk: ⚪ Minimal · up to
|
Check the diff here: storybookjs/storybook@8af3851...50a8282 List of included PRs since previous version: - storybookjs/storybook#36301 (sharadvc/cursor/fix-mcp-connect-agents-link-6fef) - storybookjs/storybook#35863 (SnowingFox/fix/35862-server-channel-error-handler) - storybookjs/storybook#36688 (ghengeveld/vitest-36221-leftovers) - storybookjs/storybook#36746 (fix/bbc-local-story-manifest-snippets) - storybookjs/storybook#36588 (fix/nextjs-router-mock-bfcache) - storybookjs/storybook#36391 (agent/sto-492-eslint-cjs) - storybookjs/storybook#36751 (agent/sto-280-prerelease-version-alignment) - storybookjs/storybook#36768 (svelte-knip-cleanup) - storybookjs/storybook#35834 (FrancoKaddour/fix/docgen-typescript7-missing-compiler-api) - storybookjs/storybook#36770 (bryce-easygo/fix/svelte-csf-null-arg-snippet)
#35792reports thatexperimentalDocgenServer+componentsManifeston TypeScript 7 crashes the docgen worker withCannot read properties of undefined (reading 'fileExists'). Root cause (well diagnosed by the reporter): TS 7'stypescriptpackage only exportslib/version.cjsfrom its main entry, soawait import('typescript')resolves to a namespace withversionbut no compiler API (ts.sysisundefined).On
nextthe hard crash no longer reproduces the same way: theBaseComponentMetaManagerrefactor moved the firstts.sysaccess into construction (createDocumentRegistry(ts.sys.useCaseSensitiveFileNames)), so the existingtry/catcharoundnew ComponentMetaManager(ts)in both entry points already swallows it and falls back toundefined. But that fallback is silent in the docgen worker and mislabeled in the singleton ("TypeScript not available", when TS is installed) — so a TS 7 user just gets empty component docs with no explanation.This PR makes that path explicit and diagnosable:
ComponentMetaManager's constructor now asserts the resolvedtypescriptactually exposes the compiler API and throws a clear, actionable error otherwise, instead of crashing later on a crypticundefinedread. Both call sites already catch and degrade gracefully."TypeScript not available"; both entry points nowlogger.debugthe real reason, so the TS 7 case is visible when debugging.console.log({ tsconfigPath })ingetTsConfigthat leaked to the dev-server stdout.Testing
ComponentMetaManagerwith a version-onlytypescriptstub (the TS 7 shape) throws the clear compiler-API error. Verified red/green — without the guard the constructor throws the crypticCannot read properties of undefined (reading 'useCaseSensitiveFileNames').ComponentMetaManager.test.tsgreen (20/20).Manual testing
yarn task sandbox --template react-vite/default-ts --start-from autoyarn add -D typescript@7and start Storybook with--loglevel debug[reactComponentMeta]message that the resolvedtypescripthas no compiler APIyarn add -D typescript@6and restart: the Controls table shows the Button props againScope
This intentionally does not add TS 7 native-preview support for docgen (the compiler API genuinely isn't there) — it makes the unsupported path degrade cleanly and legibly instead of crashing. Happy to escalate the
logger.debugto a one-timewarnif you'd prefer TS 7 users to see it surfaced.Related to #35792
🤖 AI disclosure: Implemented with AI assistance (Claude Code). The root-cause analysis, the red/green reproduction of the TypeScript 7 shape, and all verification were done and reviewed by me.