Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Add runtime error when Metro config cannot be resolved #1889

Merged
merged 3 commits into from
Mar 29, 2023

Conversation

huntie
Copy link
Collaborator

@huntie huntie commented Mar 27, 2023

Summary:

Relates to:

While preparing facebook/react-native#36623, we discovered that (post monorepo) a few scripts in the React Native repo were starting RN CLI in directories outside of the project dir and weren't reading metro.config.js as expected — printing obscure errors due to missing config items.

This PR updates the CLI to throw a runtime error when resolveConfig fails for the target working directory (ctx.root). This is an important DevX enhancement, since a complete metro.config.js file will be required from React Native 0.72.

Test Plan:

Pre-steps:

  • Run yarn link in local clone of RN CLI.
  • Run yarn link @react-native-community/cli-plugin-metro inside packages/rn-tester (local facebook/react-native repo clone).

✅ Now throws for yarn start in packages/rn-tester.

  • Previously, this would start Metro and throw harder-to-understand errors due to missing config items.

image

(Note: I'm open to suggestions for an improved error message/method of erroring to improve the CLI output.)

✅ Does not throw when yarn start command is fixed

  • (I've added a local console.log() to print the config file name)

image

@szymonrybczak
Copy link
Collaborator

Thanks for the fix 🎉 I see that you changed the command from direct call ../react-native/scripts/packager.sh to react-native start, will you update that inside rn-tester?

@huntie
Copy link
Collaborator Author

huntie commented Mar 27, 2023

@szymonrybczak Yes! This PR is basically a test plan for that package.json change haha:

@szymonrybczak
Copy link
Collaborator

szymonrybczak commented Mar 27, 2023

Cool! So in future we can get rid of launchPackager.command and packager.sh because they'll useless - nothing will use those scripts. (Since we have your PR, #1812, and I plan to remove calling this script in "Start packager" build phase).

@huntie
Copy link
Collaborator Author

huntie commented Mar 28, 2023

Updated to address feedback. Ready to merge.

Copy link
Member

@thymikee thymikee left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@huntie I've updated the wording and replaced console.warn with logger.warn. LGTM

@thymikee thymikee merged commit e29c2e9 into react-native-community:main Mar 29, 2023
@huntie huntie deleted the rn-metro-config branch March 29, 2023 08:52
@tido64
Copy link
Contributor

tido64 commented Mar 30, 2023

Hi folks, can we pick this into 11.x? Things are currently broken without it.

facebook-github-bot pushed a commit to facebook/metro that referenced this pull request Mar 30, 2023
Summary:
Related: react-native-community/cli#1889.

Changelog: **[Types]** Fix return type on `'metro-config'.resolveConfig`

Reviewed By: motiz88

Differential Revision: D44540075

fbshipit-source-id: 182c3d7ac3c75446a905cb2988bef8044dea2a76
@zhiqingchen
Copy link
Contributor

hi @huntie
Do you know why the reporter needs to be overridden again?

if (options.reporter) {
  overrideConfig.reporter = options.reporter;
}

loadConfig() already set reporter from options

loadConfig({cwd: ctx.root, ...options})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants