From 67e2bb455fd39f00fbf2b4d4f2f019327929efa6 Mon Sep 17 00:00:00 2001 From: Tommy Nguyen <4123478+tido64@users.noreply.github.com> Date: Mon, 19 Feb 2024 11:59:51 +0100 Subject: [PATCH] fix: fix codegen not finding all third-party libraries (#42943) * fix: fix codegen failing in pnpm setups * fix third-party libs not being detected --- .../codegen/generate-artifacts-executor.js | 29 ++++++++++++------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/packages/react-native/scripts/codegen/generate-artifacts-executor.js b/packages/react-native/scripts/codegen/generate-artifacts-executor.js index 025f80c78663c3..23faf9ca37097d 100644 --- a/packages/react-native/scripts/codegen/generate-artifacts-executor.js +++ b/packages/react-native/scripts/codegen/generate-artifacts-executor.js @@ -32,7 +32,13 @@ const REACT_NATIVE_PACKAGE_ROOT_FOLDER = path.join(__dirname, '..', '..'); const CODEGEN_DEPENDENCY_NAME = '@react-native/codegen'; const CODEGEN_REPO_PATH = `${REACT_NATIVE_REPOSITORY_ROOT}/packages/react-native-codegen`; -const CODEGEN_NPM_PATH = `${REACT_NATIVE_PACKAGE_ROOT_FOLDER}/../${CODEGEN_DEPENDENCY_NAME}`; +// This is a change for 0.73-stable only since this piece of code was replaced: +// https://github.com/facebook/react-native/commit/9071a3a0b0e11ad711927651bcb2412f553b6fe9 +const CODEGEN_NPM_PATH = path.dirname( + require.resolve(path.join(CODEGEN_DEPENDENCY_NAME, 'package.json'), { + paths: [REACT_NATIVE_PACKAGE_ROOT_FOLDER], + }), +); const CORE_LIBRARIES_WITH_OUTPUT_FOLDER = { rncore: path.join(REACT_NATIVE_PACKAGE_ROOT_FOLDER, 'ReactCommon'), FBReactNativeSpec: null, @@ -189,33 +195,34 @@ function handleThirdPartyLibraries( codegenConfigKey, ) { // Determine which of these are codegen-enabled libraries - const configDir = - baseCodegenConfigFileDir || - path.join(REACT_NATIVE_PACKAGE_ROOT_FOLDER, '..'); + const configDir = baseCodegenConfigFileDir || process.cwd(); console.log( `\n\n[Codegen] >>>>> Searching for codegen-enabled libraries in ${configDir}`, ); // Handle third-party libraries + const resolveOptions = {paths: [configDir]}; Object.keys(dependencies).forEach(dependency => { if (dependency === REACT_NATIVE_DEPENDENCY_NAME) { // react-native should already be added. return; } - const codegenConfigFileDir = path.join(configDir, dependency); - const configFilePath = path.join( - codegenConfigFileDir, - codegenConfigFilename, - ); - if (fs.existsSync(configFilePath)) { + + try { + const configFilePath = require.resolve( + `${dependency}/${codegenConfigFilename}`, + resolveOptions, + ); const configFile = JSON.parse(fs.readFileSync(configFilePath)); extractLibrariesFromJSON( configFile, libraries, codegenConfigKey, dependency, - codegenConfigFileDir, + path.dirname(configFilePath), ); + } catch (_) { + // ignore } }); }