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. |
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.