Skip to content

fix(cli): swizzle copies nested component source recursively - #3507

Closed
oneshot2001 wants to merge 1 commit into
meta:mainfrom
oneshot2001:fix/swizzle-recursive-copy
Closed

oneshot2001 wants to merge 1 commit into
meta:mainfrom
oneshot2001:fix/swizzle-recursive-copy

Conversation

@oneshot2001

Copy link
Copy Markdown

Summary

Fixes #3506.

astryx swizzle <Name> copied only the top-level files of a component directory and silently skipped nested subdirectories. For components whose source spans folders (e.g. Table/plugins/*), this produced a broken partial eject: the copied entry file still export … from './plugins/…', but that subtree was never copied, so the swizzled component fails module resolution — while the CLI reports success.

Root cause in packages/cli/src/commands/swizzle.mjs: the copy loop read the component dir non-recursively (if (!stat.isFile()) continue), and rewriteImports only handled top-level files (it mapped every ../seg/... to owner/seg).

Changes

  • Recursive copy. New collectSourceFiles() walks the component tree, preserving nested structure and excluding tests/docs/README at every level. It's the single source of truth for the pre-flight collision check, the copy loop, and the reported file list. Symlinks are not followed.
  • Location-aware import rewriting. rewriteImports now accepts an optional {fromDir, componentDir}. Each ../ import is resolved from the source file's own directory; only imports that escape the component are rewritten to the owner package's subpath. Intra-component imports from nested files (e.g. ../../types in Table/plugins/pagination/*) stay relative instead of becoming a broken @astryxdesign/core/... Without the location arg the legacy behavior is unchanged, so the existing exported rewriteImports(content) contract and its tests are preserved.
  • Path safety. Each destination is asserted within the output dir (assertWithin) before any mkdir/write.

Testing

  • pnpm exec vitest run packages/cli/src/commands/swizzle.*.test.mjs — 21/21 pass (added 5 unit tests for nested-file rewriting; existing routing + path-safety suites still green).
  • End-to-end: astryx swizzle Table now emits the full plugins/ subtree; spot-checked Table/plugins/pagination/useTablePagination.tsx — escaping imports resolve to @astryxdesign/core/theme / @astryxdesign/core/Pagination while the intra-component ../../types stays relative. Top-level index.ts's ./plugins/* re-exports now resolve.
  • pnpm check:repo (sync, package-boundaries, changesets, demo-media) passes; changeset added (@astryxdesign/cli patch, [fix]).

Notes

Behavior change is additive — top-level-only components swizzle identically to before; multi-directory components now eject completely. Happy to adjust naming/placement to match maintainer preference.

I've read the CONTRIBUTING guide and will complete the Meta CLA.

🤖 Generated with Claude Code

`astryx swizzle <Name>` copied only the top-level files of a component
directory and silently skipped nested subdirectories, producing a broken
partial eject for components whose source spans folders (e.g. Table/plugins/*).
The copied entry file still imported from the dropped subtree, so the swizzled
component failed to resolve — while the CLI reported success.

- Walk the component directory recursively, preserving nested structure
  (collectSourceFiles), and use that single list for the collision check,
  the copy loop, and the reported file list.
- Make import rewriting location-aware: resolve each `../` import from the
  source file's own directory and only rewrite imports that escape the
  component. Intra-component imports from nested files (e.g. `../../types`)
  stay relative instead of becoming a broken `@astryxdesign/core/..`.
  Top-level behavior and the existing exported `rewriteImports(content)`
  contract are unchanged.
- Assert each destination path stays within the output dir before writing.

Adds unit tests for nested-file rewriting; verified end-to-end that
`swizzle Table` now emits the full plugins/ tree with correct imports.

Fixes meta#3506

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@vercel

vercel Bot commented Jul 3, 2026

Copy link
Copy Markdown

@oneshot2001 is attempting to deploy a commit to the Meta Open Source Team on Vercel.

A member of the Team first needs to authorize it.

@meta-cla

meta-cla Bot commented Jul 3, 2026

Copy link
Copy Markdown

Hi @oneshot2001!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at [email protected]. Thanks!

@cixzhang

cixzhang commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Thanks, this is a good catch.

@cixzhang cixzhang left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified end-to-end: swizzle Table now ejects the full plugins/ subtree (36 files), every relative import resolves, and escaping imports rewrite correctly while intra-component ones stay relative — 21/21 tests pass. LGTM; please sign the Meta CLA so we can merge.

@github-actions github-actions Bot added the community Authored by a community contributor (not on the eng/design team) label Jul 11, 2026
@cixzhang

Copy link
Copy Markdown
Contributor

Thanks for spotting this and taking the time to write it up, @oneshot2001 — the non-recursive copy is a real bug. Unfortunately we can't merge this as-is: the Meta CLA hasn't been signed, and the PR has fallen well behind main since the CLI was reorganized, so it no longer applies cleanly. Closing for now — if you'd like to pursue it, please sign the CLA at https://code.facebook.com/cla and open a fresh PR against current main, and we'll gladly review it. The underlying issue #3506 stays open in the meantime. Appreciate the contribution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community Authored by a community contributor (not on the eng/design team)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

swizzle silently drops nested subdirectories, producing a broken partial eject (e.g. Table/plugins)

2 participants