mirror of
https://github.com/facebook/react-native.git
synced 2026-11-18 20:19:07 +00:00
SwiftPM: declare ReactAppHeaders in the codegen placeholder package (#59293)
Summary:
A SwiftPM app can get stuck when setup skips codegen. Setup skips codegen when you pass `--skipCodegen`, when codegen fails ("Continuing anyway"), or when the codegen script is missing. In each case `spm add` / `spm update` writes a placeholder `build/generated/ios/Package.swift`, which the "Sync SPM Autolinking" hook replaces during the build.
The placeholder declared only `ReactCodegen` and `ReactAppDependencyProvider`. Every autolinked or scaffolded library manifest also depends on `ReactAppHeaders` from that package. So Xcode fails package resolution:
```
product 'ReactAppHeaders' required by package 'reactnativesafeareacontext' target 'ReactNativeSafeAreaContext' not found in package 'React-GeneratedCode'.
```
Xcode resolves packages before the scheme pre-action and the build phases, so neither hook runs, and every later build fails the same way. The only way out is to run setup again with codegen, and the error does not say so.
This change makes the placeholder declare `ReactAppHeaders` (every entry of `REACT_CODEGEN_PRODUCTS`) too. `SPM_PRODUCT_PACKAGES` is unchanged, because it also decides which products the app target links, and the app must not link `ReactAppHeaders`.
Known limit, not changed here: after this fix the first build still fails once, because the hook generates the library's codegen headers during that build, after the package graph is resolved (for example `'react/renderer/components/safeareacontext/Props.h' file not found`). The next build succeeds with no other change.
## Changelog:
[IOS] [FIXED] - SwiftPM: a project set up with skipped codegen no longer fails package resolution on `ReactAppHeaders`
Pull Request resolved: https://github.com/react/react-native/pull/59293
Test Plan:
Unit tests: two new tests for `ensureStubPackages` in `generate-spm-xcodeproj-test.js`. One checks that the placeholder declares each codegen product exactly once. One checks that an existing `Package.swift` is not changed.
```
yarn jest --no-cache -i packages/react-native/scripts/spm/__tests__/generate-spm-xcodeproj-test.js
```
- Without the source change: 1 failed (`ReactAppHeaders` declared 0 times).
- With the source change: 66 passed, 66 total.
- ESLint (`--max-warnings 0`) and Prettier pass on both files.
Fresh app (Xcode 27.1): `0.89.0-nightly-20261021-c7429b0e9` + `react-native-safe-area-context` 5.10.1, `npx react-native spm add --deintegrate --skipCodegen`, then `xcodebuild -resolvePackageDependencies` and `xcodebuild build`:
- Without the fix: resolution fails with the error above; the build stops at resolution (exit 74) and no hook runs.
- With the fix applied to the app's copy of the script: resolution passes; the first build runs the sync hook, which replaces the placeholder, and then fails on the missing library codegen headers (exit 65); the second build succeeds.
Not run: full `yarn flow-check` (a scoped Flow check showed no errors in the changed files).
Reviewed By: andrewdacenko
Differential Revision: D126658850
Pulled By: cipolleschi
fbshipit-source-id: 0ef10c5180f5932cfd761b16418e9a368401550b
(cherry picked from commit e3aa9a1e19)
This commit is contained in:
1 parent
a25ec465e4
commit
84e60bc30b
2 files changed
+63
No files matched your search
@@ -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);
|
||||
});
|
||||
});
|
||||
@@ -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');
|
||||
|
||||
Reference in new issue
Block a user