diff --git a/packages/react-native/scripts/spm/__tests__/generate-spm-xcodeproj-test.js b/packages/react-native/scripts/spm/__tests__/generate-spm-xcodeproj-test.js index 1aa0e6d55d5..dd47cec9b8d 100644 --- a/packages/react-native/scripts/spm/__tests__/generate-spm-xcodeproj-test.js +++ b/packages/react-native/scripts/spm/__tests__/generate-spm-xcodeproj-test.js @@ -15,6 +15,7 @@ const { buildEmbedFrameworksScript, buildSchemePreActionScript, buildSyncAutolinkingScript, + ensureStubPackages, flavorForBuildConfiguration, frameworkConditionalSettings, generateReactNativeXcconfig, @@ -895,3 +896,52 @@ describe('readScriptPhasesManifest', () => { ]); }); }); + +describe('ensureStubPackages', () => { + const {REACT_CODEGEN_PRODUCTS} = require('../spm-utils'); + let appRoot; + let logSpy; + + beforeEach(() => { + appRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'spm-stub-packages-')); + logSpy = jest.spyOn(console, 'log').mockImplementation(() => {}); + }); + afterEach(() => { + logSpy.mockRestore(); + fs.rmSync(appRoot, {recursive: true, force: true}); + }); + + const codegenManifestPath = () => + path.join(appRoot, 'build', 'generated', 'ios', 'Package.swift'); + + it('declares every codegen product exactly once in the codegen stub', () => { + ensureStubPackages(appRoot); + const manifest = fs.readFileSync(codegenManifestPath(), 'utf8'); + + const products = [ + 'ReactCodegen', + 'ReactAppDependencyProvider', + ...REACT_CODEGEN_PRODUCTS, + ]; + const declarationCounts = Object.fromEntries( + products.map(product => [ + product, + manifest.split(`.library(name: "${product}"`).length - 1, + ]), + ); + + expect(declarationCounts).toEqual( + Object.fromEntries(products.map(product => [product, 1])), + ); + }); + + it('leaves an existing codegen Package.swift untouched', () => { + const existing = '// swift-tools-version: 5.9\n// real codegen output\n'; + fs.mkdirSync(path.dirname(codegenManifestPath()), {recursive: true}); + fs.writeFileSync(codegenManifestPath(), existing, 'utf8'); + + ensureStubPackages(appRoot); + + expect(fs.readFileSync(codegenManifestPath(), 'utf8')).toBe(existing); + }); +}); diff --git a/packages/react-native/scripts/spm/generate-spm-xcodeproj.js b/packages/react-native/scripts/spm/generate-spm-xcodeproj.js index fe1d021d663..91c01bb3578 100644 --- a/packages/react-native/scripts/spm/generate-spm-xcodeproj.js +++ b/packages/react-native/scripts/spm/generate-spm-xcodeproj.js @@ -54,6 +54,7 @@ const { AUTOLINKED_PACKAGE_NAME, REACT_CODEGEN_APP_PRODUCTS, REACT_CODEGEN_PACKAGE_NAME, + REACT_CODEGEN_PRODUCTS, REACT_NATIVE_PACKAGE_NAME, REACT_NATIVE_PRODUCTS, isValidScriptPhaseId, @@ -1136,6 +1137,18 @@ function ensureStubPackages(appRoot /*: string */) /*: void */ { } } + // Library manifests depend on these products, but the app must not link + // them, so they are stubbed here instead of listed in SPM_PRODUCT_PACKAGES. + for (const def of byPath.values()) { + if (def.packageName === REACT_CODEGEN_PACKAGE_NAME) { + for (const product of REACT_CODEGEN_PRODUCTS) { + if (!def.products.includes(product)) { + def.products.push(product); + } + } + } + } + for (const [relPath, def] of byPath) { const pkgDir = path.join(appRoot, relPath); const pkgSwiftPath = path.join(pkgDir, 'Package.swift');