React-Docgen: Try .tsx fallback when resolving .js ESM imports in docgen resolvers#34393
Conversation
When resolving ESM-style .js imports that point to TypeScript files, the docgen resolver previously only tried .ts as a fallback. React components are commonly .tsx files, so .js → .ts would fail and the component would not be found. Fix: after .ts resolution fails, fall back to .tsx. The same fix is applied to all three copies of the resolver pattern (react-vite, react-webpack, componentManifest).
📝 WalkthroughWalkthroughThe pull request updates Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (2)
code/renderers/react/src/componentManifest/reactDocgen/docgenResolver.ts (1)
41-50: Consider centralizing this fallback logic or at least adding a sync note here.The same
.ts-then-TSX selection now lives in this renderer resolver plus the Vite and webpack copies. Since this fix already had to touch all three places, a shared helper—or a local “keep in sync” note in this file—would reduce the odds of the next resolver tweak drifting.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@code/renderers/react/src/componentManifest/reactDocgen/docgenResolver.ts` around lines 41 - 50, The .cjs branch currently duplicates the ".ts then .tsx" fallback (see the case '.cjs' block using base, resolve.sync and newFilename) which is repeated across other resolvers; extract that selection logic into a small shared helper (e.g., resolveTsThenTsx(filename, resolveOptions)) and call it from docgenResolver.ts (replace the try/catch and newFilename assignment with that helper), or if extracting a helper is out-of-scope, add a clear sync note/TODO above the case '.cjs' block documenting the exact behavior and where the Vite and webpack copies live so future edits stay consistent. Ensure the helper returns the resolved filename or throws the same error shape as the original resolve.sync usage and preserve the existing variable names (base/newFilename) so surrounding code requires minimal changes.code/frameworks/react-vite/src/plugins/docgen-resolver.ts (1)
58-67: Please add regression coverage for this fallback branch.This fixes a reported resolver edge case, but the diff does not show a behavior test for
./Chip.js -> ./Chip.tsxand the corresponding.mjs/.cjscases. A small resolver-level test would make this much harder to regress. Happy to sketch that fixture if useful.Based on learnings: "Applies to **/*.{test,spec}.{ts,tsx,js,jsx} : Test real behavior, not just syntax patterns".
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@code/frameworks/react-vite/src/plugins/docgen-resolver.ts` around lines 58 - 67, Add a regression test that exercises the fallback branch in docgen-resolver.ts where a `.cjs` (and similarly `.mjs`) import for `./Chip.js` should resolve to `./Chip.tsx`; create a resolver-level test (matching the project test patterns e.g. *.test.(ts|tsx|js|jsx)) that sets up fixtures: a module importing `./Chip.js` and filesystem entries `Chip.ts` (absent) and `Chip.tsx` (present), then assert the resolver used by the code in docgen-resolver.ts returns the `Chip.tsx` path; include both `.cjs` and `.mjs` cases to cover the try/catch fallback logic in the switch block handling `.cjs`/`.mjs` so the `${base}tsx` branch is exercised and cannot regress.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@code/frameworks/react-vite/src/plugins/docgen-resolver.ts`:
- Around line 58-67: Add a regression test that exercises the fallback branch in
docgen-resolver.ts where a `.cjs` (and similarly `.mjs`) import for `./Chip.js`
should resolve to `./Chip.tsx`; create a resolver-level test (matching the
project test patterns e.g. *.test.(ts|tsx|js|jsx)) that sets up fixtures: a
module importing `./Chip.js` and filesystem entries `Chip.ts` (absent) and
`Chip.tsx` (present), then assert the resolver used by the code in
docgen-resolver.ts returns the `Chip.tsx` path; include both `.cjs` and `.mjs`
cases to cover the try/catch fallback logic in the switch block handling
`.cjs`/`.mjs` so the `${base}tsx` branch is exercised and cannot regress.
In `@code/renderers/react/src/componentManifest/reactDocgen/docgenResolver.ts`:
- Around line 41-50: The .cjs branch currently duplicates the ".ts then .tsx"
fallback (see the case '.cjs' block using base, resolve.sync and newFilename)
which is repeated across other resolvers; extract that selection logic into a
small shared helper (e.g., resolveTsThenTsx(filename, resolveOptions)) and call
it from docgenResolver.ts (replace the try/catch and newFilename assignment with
that helper), or if extracting a helper is out-of-scope, add a clear sync
note/TODO above the case '.cjs' block documenting the exact behavior and where
the Vite and webpack copies live so future edits stay consistent. Ensure the
helper returns the resolved filename or throws the same error shape as the
original resolve.sync usage and preserve the existing variable names
(base/newFilename) so surrounding code requires minimal changes.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 83951f85-cb03-4634-86c4-8a11618888c3
📒 Files selected for processing (3)
code/frameworks/react-vite/src/plugins/docgen-resolver.tscode/presets/react-webpack/src/loaders/docgen-resolver.tscode/renderers/react/src/componentManifest/reactDocgen/docgenResolver.ts
…back React-Docgen: Try .tsx fallback when resolving .js ESM imports in docgen resolvers (cherry picked from commit 069e745)
What
When resolving ESM-style
.jsimports that point to TypeScript files, the docgen resolver tried.tsas a fallback but never tried.tsx. This means React components using.tsxextension with ESM-style imports fail MCP manifest generation with "No component file found".Fixes #34387
Changes
Updated the
.js/.mjs/.cjsfallback logic in all three copies of the docgen resolver pattern to try.tsfirst, then fall back to.tsx:code/frameworks/react-vite/src/plugins/docgen-resolver.tscode/presets/react-webpack/src/loaders/docgen-resolver.tscode/renderers/react/src/componentManifest/reactDocgen/docgenResolver.tsBefore:
After:
Summary by CodeRabbit