diff --git a/packages/react-native/scripts/spm/__tests__/scaffold-package-swift-test.js b/packages/react-native/scripts/spm/__tests__/scaffold-package-swift-test.js index e0e72b3c4b3..9fcc3006174 100644 --- a/packages/react-native/scripts/spm/__tests__/scaffold-package-swift-test.js +++ b/packages/react-native/scripts/spm/__tests__/scaffold-package-swift-test.js @@ -140,6 +140,37 @@ describe('translatePodspecToSpmTarget', () => { expect(spec.coreReactNative).toBe(true); }); + it('uses the resolved swiftName (spm.name override) so the manifest matches what the autolinker registers', () => { + const model = podspec({name: 'react-native-worklets'}); + const spec = translatePodspecToSpmTarget( + model, + autolinkedDep({name: 'react-native-worklets', swiftName: 'worklets'}), + ); + expect(spec.swiftName).toBe('worklets'); + }); + + it('falls back to toSwiftName when the dep carries no resolved name', () => { + const spec = translatePodspecToSpmTarget( + podspec(), + autolinkedDep({name: 'react-native-foo'}), + ); + expect(spec.swiftName).toBe('ReactNativeFoo'); + }); + + it('resolves each sibling through the same overrides, not through toSwiftName', () => { + const model = podspec({dependencies: ['RNWorklets']}); + const spec = translatePodspecToSpmTarget( + model, + autolinkedDep({name: 'react-native-reanimated', swiftName: 'reanimated'}), + new Map([['RNWorklets', 'react-native-worklets']]), + new Map([['react-native-worklets', 'worklets']]), + ); + expect(spec.siblingNames).toEqual(['react-native-worklets']); + expect(spec.siblingSwiftNames).toEqual({ + 'react-native-worklets': 'worklets', + }); + }); + it('does not self-wire when a pod dependency maps back to the dep itself', () => { const model = podspec({dependencies: ['RNReanimated']}); const spec = translatePodspecToSpmTarget( @@ -604,6 +635,18 @@ describe('emitScaffoldedPackageSwift', () => { ); }); + it('emits the sibling override name for both the package and the product', () => { + const out = emitScaffoldedPackageSwift( + baseSpec({ + siblingNames: ['react-native-worklets'], + siblingSwiftNames: {'react-native-worklets': 'worklets'}, + }), + ); + expect(out).toContain('.package(name: "worklets", path: "../worklets")'); + expect(out).toContain('.product(name: "worklets", package: "worklets")'); + expect(out).not.toContain('ReactNativeWorklets'); + }); + it('-includes the ObjC prefix header in c/cxx settings when needsObjCPrefix is set', () => { const withPrefix = emitScaffoldedPackageSwift( baseSpec({needsObjCPrefix: true}), @@ -745,6 +788,21 @@ end }; } + it('names the scaffolded package with the spm.name override the autolinker resolved', () => { + makePodspec(); + const result = scaffoldPackageSwiftForDep( + makeDep({swiftName: 'foo'}), + makeCtx(), + ); + expect(result.status).toBe('written'); + const content = fs.readFileSync( + path.join(depRoot, 'Package.swift'), + 'utf8', + ); + expect(content).toContain('name: "foo"'); + expect(content).not.toContain('ReactNativeFoo'); + }); + it('writes Package.swift into the dep root on the happy path', () => { makePodspec(); const result = scaffoldPackageSwiftForDep(makeDep(), makeCtx()); diff --git a/packages/react-native/scripts/spm/scaffold-package-swift.js b/packages/react-native/scripts/spm/scaffold-package-swift.js index ce3103ebedd..c0113ae88ec 100644 --- a/packages/react-native/scripts/spm/scaffold-package-swift.js +++ b/packages/react-native/scripts/spm/scaffold-package-swift.js @@ -239,14 +239,18 @@ function translatePodspecToSpmTarget( // `s.dependency "RNWorklets"` — a pod-style name the `react-native-*` // heuristic can't recognize — to the right sibling package. Empty by default. podToNpm /*: Map */ = new Map(), + // npm name → resolved Swift name for every autolinked dep, so a sibling + // reference honors that sibling's `spm.name` instead of re-deriving it. + swiftNameByNpm /*: Map */ = new Map(), ) /*: SpmScaffoldSpec */ { const warnings = [...model.warnings]; - // Swift target name: ALWAYS toSwiftName(npm-name). The autolinker - // registers each autolinked dep under that name in its aggregator (and in - // any sibling spm.dependencies refs), so the scaffolded Package.swift's - // product/library name must match — otherwise SPM resolution fails with - // a name mismatch on `.product(name: "X", package: "X")`. + // Swift target name: whatever the autolinker resolved for this dep — its + // `spm.name` override when set, else toSwiftName(npm-name). The autolinker + // registers the dep under that name in its aggregator (and in any sibling + // spm.dependencies refs), so the scaffolded Package.swift's product/library + // name must match it exactly — otherwise SPM resolution fails with a name + // mismatch on `.product(name: "X", package: "X")`. // // The podspec's `header_dir` is captured separately: when it changes the // include surface (e.g. `` instead of ``), @@ -255,10 +259,8 @@ function translatePodspecToSpmTarget( // `` resolve through // `-I common/cpp/`). Module-style includes that NEED the target name to // match (e.g. reanimated's `` via SwiftPM's auto-generated - // module map) require an explicit `spm.name` override in - // react-native.config.js — handled by the existing autolinker flow, not - // here. - const swiftName = toSwiftName(dep.name); + // module map) are what `spm.name` is for. + const swiftName = dep.swiftName ?? toSwiftName(dep.name); // Header search paths — substitute Xcode build-setting tokens against the // dep root. Anything we can't substitute is dropped + warned (avoids @@ -516,6 +518,14 @@ function translatePodspecToSpmTarget( ); } + const siblingSwiftNames /*: {[npmName: string]: string} */ = {}; + for (const npmName of siblingNames) { + const resolved = swiftNameByNpm.get(npmName); + if (resolved != null) { + siblingSwiftNames[npmName] = resolved; + } + } + return { swiftName, sources: expandedSources, @@ -524,6 +534,7 @@ function translatePodspecToSpmTarget( needsObjCPrefix, coreReactNative, siblingNames, + siblingSwiftNames, extraFrameworks: model.frameworks, weakFrameworks: model.weakFrameworks, compilerFlags: model.compilerFlags, @@ -692,7 +703,8 @@ function emitScaffoldedPackageSwift( } } for (const siblingName of spec.siblingNames) { - const swiftSibling = toSwiftName(siblingName); + const swiftSibling = + spec.siblingSwiftNames?.[siblingName] ?? toSwiftName(siblingName); // The autolinker references each self-managed (scaffolded) dep through a // `libs/` symlink, and SPM resolves a manifest's relative // package paths against that symlink location — so a sibling lives at @@ -792,6 +804,9 @@ type ScaffoldContext = { // podspec-name → npm-name index over all autolinked deps, so pod-style // `s.dependency` names (e.g. "RNWorklets") wire to the right sibling. podToNpm?: Map, + // npm-name → resolved Swift name over all autolinked deps, so sibling + // references honor each sibling's `spm.name`. + swiftNameByNpm?: Map, }; */ @@ -950,6 +965,7 @@ function scaffoldPackageSwiftForDep( model, dep, ctx.podToNpm ?? new Map(), + ctx.swiftNameByNpm ?? new Map(), ); // Mixed-language fail-closed: SPM can't compile Swift + C-family in one @@ -1172,6 +1188,13 @@ function scaffoldAll( } } + const swiftNameByNpm /*: Map */ = new Map(); + for (const dep of allDeps) { + if (dep.swiftName != null) { + swiftNameByNpm.set(dep.name, dep.swiftName); + } + } + const ctx /*: ScaffoldContext */ = { appRoot, projectRoot, @@ -1180,6 +1203,7 @@ function scaffoldAll( dryRun: opts.dryRun === true, cacheSlotLabel: opts.cacheSlotLabel ?? null, podToNpm, + swiftNameByNpm, }; const skipSet /*: Set */ = new Set(opts.skipDeps ?? []); diff --git a/packages/react-native/scripts/spm/spm-types.js b/packages/react-native/scripts/spm/spm-types.js index deb956e53b4..849fecff563 100644 --- a/packages/react-native/scripts/spm/spm-types.js +++ b/packages/react-native/scripts/spm/spm-types.js @@ -481,10 +481,13 @@ export type SpmScaffoldSpec = { // Bucketed dependency references — pre-computed by the translation layer. // `coreReactNative` is true when ANY React-* / RCT* / RCT-Folly / glog // dep is present (so we add React's invariant header products). - // `siblingNames` are npm names that match other autolinked deps — resolved - // to Swift names by the scaffold orchestrator before emit. + // `siblingNames` are npm names that match other autolinked deps. + // `siblingSwiftNames` carries each one's resolved Swift name (honoring the + // sibling's `spm.name`); a sibling absent from it falls back to + // toSwiftName(npmName) at emit time. coreReactNative: boolean, siblingNames: Array, + siblingSwiftNames?: {[npmName: string]: string}, // Extra frameworks beyond the autolinker's default UIKit/Foundation/CoreGraphics // set. Merged with the defaults at emit time. extraFrameworks: Array,