fix: propagate resolved port to build when default port is unavailable#2783
Merged
thymikee merged 2 commits intoreact-native-community:mainfrom Mar 24, 2026
Merged
Conversation
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.
Member
|
Released in 20.1.3 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.
handlePortUnavailablereturns wrong port when packager is already running elsewhereWhen port 8081 is busy and
getNextPortdiscovers a packager already running on a different port (e.g. 8082) for the same project,handlePortUnavailablelogs 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 = nextPortin the!startbranch ofhandlePortUnavailable.2.
createRun(iOS) andrunAndroiddon't propagate the resolved port to the buildWhen
findDevServerPortreturns 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 toargs.port. This causes:RCT_METRO_PORTenv var passed toxcodebuildstill contains the original port-PreactNativeDevServerPortgradle arg andadb reversestill use the original portFix: Update
args.port = newPortwhen the port changes in bothcreateRun.tsandrunAndroid/index.ts.Test plan
nc -l 8081)npx react-native start— accept port 8082npx react-native run-ios(orrun-android)npx react-native run-androidwithout Metro prompt-PreactNativeDevServerPort=8082andadb reverseuses port 8082