Skip to content

fix: propagate resolved port to build when default port is unavailable#2783

Merged
thymikee merged 2 commits intoreact-native-community:mainfrom
janicduplessis:fix/port-fallback-not-propagated
Mar 24, 2026
Merged

fix: propagate resolved port to build when default port is unavailable#2783
thymikee merged 2 commits intoreact-native-community:mainfrom
janicduplessis:fix/port-fallback-not-propagated

Conversation

@janicduplessis
Copy link
Contributor

@janicduplessis janicduplessis commented Mar 24, 2026

Summary

When the default Metro port (8081) is unavailable, the resolved port is not properly propagated to the native build, causing the app to fail to connect to Metro.

There are two issues fixed:

1. handlePortUnavailable returns wrong port when packager is already running elsewhere

When port 8081 is busy and getNextPort discovers a packager already running on a different port (e.g. 8082) for the same project, handlePortUnavailable logs the correct port but returns the original unavailable port. This is the main bug — it affects both the interactive prompt flow and the "already running" flow.

Fix: Set port = nextPort in the !start branch of handlePortUnavailable.

2. createRun (iOS) and runAndroid don't propagate the resolved port to the build

When findDevServerPort returns a different port than requested (after the user accepts a port change prompt), the new port is only used to start Metro but never written back to args.port. This causes:

  • iOS: RCT_METRO_PORT env var passed to xcodebuild still contains the original port
  • Android: -PreactNativeDevServerPort gradle arg and adb reverse still use the original port

Fix: Update args.port = newPort when the port changes in both createRun.ts and runAndroid/index.ts.

Test plan

  • Start a process on port 8081 (e.g. nc -l 8081)
  • Run npx react-native start — accept port 8082
  • In another terminal, run npx react-native run-ios (or run-android)
  • Verify the app connects to Metro on 8082
  • Also test: with Metro already running on 8082, run npx react-native run-android without Metro prompt
  • Verify gradle receives -PreactNativeDevServerPort=8082 and adb reverse uses port 8082

When the default port (8081) is busy and the user accepts an alternative
port, the resolved port was only used to start Metro but not propagated
to args.port. This caused the iOS build (via RCT_METRO_PORT env var)
and Android build (via -PreactNativeDevServerPort and adb reverse) to
still use the original port, so the app would fail to connect to Metro.
…ferent port

When the default port is unavailable and `getNextPort` finds a packager
already running on a different port for the same project,
`handlePortUnavailable` correctly logs the discovered port but returns
the original (unavailable) port. This causes the build to pass the wrong
port to gradle/xcodebuild.
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.

Thank you!

@thymikee thymikee merged commit ef5421b into react-native-community:main Mar 24, 2026
8 checks passed
@thymikee
Copy link
Member

Released in 20.1.3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants