fix(codegen): read react-native.config.cjs so ESM projects are not silently skipped - #58394
fix(codegen): read react-native.config.cjs so ESM projects are not silently skipped#58394CAMOBAP wants to merge 1 commit into
Conversation
|
Hi @CAMOBAP! Thank you for your pull request and welcome to our community. Action RequiredIn 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. ProcessIn 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 If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks! |
…lently skipped
readReactNativeConfig resolves the config with a single hardcoded extension:
const rnConfigFilePath = path.resolve(projectRoot, 'react-native.config.js');
A project whose package.json declares "type": "module" cannot use that
filename. Node parses .js as ESM there, and the require() two lines below
either throws (CommonJS body -- uncaught here, so codegen dies) or returns
{__esModule, default}, so rnConfig.dependencies is undefined and
findLibrariesFromReactNativeConfig returns [] without logging.
.cjs is the only extension that is both require()-able and legal in an ESM
package, and @react-native-community/cli already treats it as first-class via
searchPlacesForCJS. Codegen has its own resolver, does not use cosmiconfig, and
was never brought in line.
The result is that autolinking and codegen disagree about the same file:
autolinking reads the config and installs the pod, codegen does not and
generates nothing for that library, and the pod then fails to compile because
its Fabric component includes a header that was never generated.
Probe .cjs in addition to .js. .js is checked first, so projects that have one
resolve exactly as before.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
813fee8 to
847da2a
Compare
|
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
|
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
|
@vzaidman has imported this pull request. If you are a Meta employee, you can view this in D119188806. |
…lently skipped (react#58394) Summary: `readReactNativeConfig` resolves the config with a single hardcoded extension: ```js const rnConfigFilePath = path.resolve(projectRoot, 'react-native.config.js'); ``` A project whose `package.json` declares `"type": "module"` cannot use that filename. Node parses `.js` as ESM there, and the `require()` two lines below either throws or returns the wrong shape: | Config file | `require()` result | | --- | --- | | `react-native.config.js`, CommonJS body | throws `ReferenceError: module is not defined in ES module scope` — uncaught here, so codegen dies | | `react-native.config.js`, ESM body | returns `{__esModule, default}`, so `rnConfig.dependencies` is `undefined` | | `react-native.config.cjs` | returns the config correctly | `.cjs` is the only extension that is both `require()`-able and legal in an ESM package — and `react-native-community/cli` already treats it as first-class: ```js // react-native-community/cli-config/build/readConfigFromDisk.js const searchPlacesForCJS = ['react-native.config.js', 'react-native.config.cjs', 'react-native.config.ts']; ``` added for react-native-community/cli#2167. Codegen has its own resolver, does not use cosmiconfig, and was never brought in line. This PR closes that gap. ### Why it matters Autolinking and codegen disagree about the same file, so the failure is silent and deferred. Autolinking reads the `.cjs` config and installs the pod; codegen doesn't, so it generates nothing for that library; the pod then compiles and its Fabric component includes a header that was never generated: ``` .../react/renderer/components/RNCSlider/RNCSliderShadowNode.h:5:10: fatal error: 'react/renderer/components/RNCSlider/Props.h' file not found ``` Nothing in that error points at the config. `findLibrariesFromReactNativeConfig` bails via `if (!rnConfig.dependencies) return [];` without logging. The other two config sources do not cover it: - `build/generated/autolinking/autolinking.json` is written by `use_native_modules!` during `pod install`, so it is absent whenever codegen runs first (pipelines that codegen before installing pods, or `generate-codegen-artifacts.js` invoked directly). - The `package.json` dependency scan only sees **direct** dependencies. A library reached through `dependencies[...].root` — the whole reason to write a config file — is by definition not one. ## Changelog: [General] [Fixed] - Codegen now also reads `react-native.config.cjs`, so libraries declared in it are no longer skipped in ESM projects Pull Request resolved: react#58394 Test Plan: Four cases added to `packages/react-native/scripts/codegen/__tests__/generate-artifacts-executor-test.js`, following the existing `mkdtempSync` fixture pattern: ``` yarn jest packages/react-native/scripts/codegen/__tests__/generate-artifacts-executor-test.js readReactNativeConfig ✓ reads react-native.config.js ✓ reads react-native.config.cjs when there is no .js config ✓ prefers react-native.config.js when both exist ✓ returns an empty config when neither exists Test Suites: 1 passed, 1 total Tests: 48 passed, 48 total Snapshots: 24 passed, 24 total ``` All 48 tests in the file pass, including the 24 existing snapshots — the change is additive for projects that have a `.js` config. **Negative control:** with the `utils.js` change reverted and the tests left in place, the `.cjs` case fails as expected: ``` ✕ reads react-native.config.cjs when there is no .js config Tests: 1 failed, 47 skipped, 48 total ``` ## Notes The log line in the `else` branch now names the directory searched rather than a single path, since there are two candidates: ```diff - codegenLog(`Could not find React Native config at: ${rnConfigFilePath}`); + codegenLog(`Could not find React Native config in: ${projectRoot}`); ``` Happy to extend this to `.mjs` / `.ts` to fully match the CLI's `searchPlaces`, but neither is a drop-in — `.mjs` cannot be `require()`d for the right default-export shape, and `.ts` needs a loader — so this PR keeps to the one case that is. Reviewed By: christophpurrer Differential Revision: D119188806 Pulled By: vzaidman fbshipit-source-id: f1565bd316738fcf43380a2a2dd28adadfc80cf3
Summary
readReactNativeConfigresolves the config with a single hardcoded extension:A project whose
package.jsondeclares"type": "module"cannot use that filename. Node parses.jsas ESM there, and therequire()two lines below either throws or returns the wrong shape:require()resultreact-native.config.js, CommonJS bodyReferenceError: module is not defined in ES module scope— uncaught here, so codegen diesreact-native.config.js, ESM body{__esModule, default}, sornConfig.dependenciesisundefinedreact-native.config.cjs.cjsis the only extension that is bothrequire()-able and legal in an ESM package — and@react-native-community/clialready treats it as first-class:added for react-native-community/cli#2167. Codegen has its own resolver, does not use cosmiconfig, and was never brought in line. This PR closes that gap.
Why it matters
Autolinking and codegen disagree about the same file, so the failure is silent and deferred. Autolinking reads the
.cjsconfig and installs the pod; codegen doesn't, so it generates nothing for that library; the pod then compiles and its Fabric component includes a header that was never generated:Nothing in that error points at the config.
findLibrariesFromReactNativeConfigbails viaif (!rnConfig.dependencies) return [];without logging.The other two config sources do not cover it:
build/generated/autolinking/autolinking.jsonis written byuse_native_modules!duringpod install, so it is absent whenever codegen runs first (pipelines that codegen before installing pods, orgenerate-codegen-artifacts.jsinvoked directly).package.jsondependency scan only sees direct dependencies. A library reached throughdependencies[...].root— the whole reason to write a config file — is by definition not one.Changelog:
[General] [Fixed] - Codegen now also reads
react-native.config.cjs, so libraries declared in it are no longer skipped in ESM projectsTest Plan
Four cases added to
packages/react-native/scripts/codegen/__tests__/generate-artifacts-executor-test.js, following the existingmkdtempSyncfixture pattern:All 48 tests in the file pass, including the 24 existing snapshots — the change is additive for projects that have a
.jsconfig.Negative control: with the
utils.jschange reverted and the tests left in place, the.cjscase fails as expected:Notes
The log line in the
elsebranch now names the directory searched rather than a single path, since there are two candidates:Happy to extend this to
.mjs/.tsto fully match the CLI'ssearchPlaces, but neither is a drop-in —.mjscannot berequire()d for the right default-export shape, and.tsneeds a loader — so this PR keeps to the one case that is.