Skip to content

Commit ef5421b

Browse files
fix: propagate resolved port to build when default port is unavailable (#2783)
* fix: propagate resolved port to build when default port is unavailable 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. * fix: return discovered port when packager is already running on a different 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.
1 parent b762402 commit ef5421b

File tree

3 files changed

+9
-0
lines changed

3 files changed

+9
-0
lines changed

packages/cli-platform-android/src/commands/runAndroid/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ async function runAndroid(_argv: Array<string>, config: Config, args: Flags) {
7070
args.terminal,
7171
);
7272
}
73+
74+
if (newPort !== port) {
75+
args.port = newPort;
76+
}
7377
}
7478

7579
if (config.reactNativeVersion !== 'unknown') {

packages/cli-platform-apple/src/commands/runCommand/createRun.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,10 @@ const createRun =
124124
args.terminal,
125125
);
126126
}
127+
128+
if (newPort !== port) {
129+
args.port = newPort;
130+
}
127131
}
128132

129133
if (ctx.reactNativeVersion !== 'unknown') {

packages/cli-tools/src/handlePortUnavailable.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const handlePortUnavailable = async (
1818

1919
if (!start) {
2020
packager = false;
21+
port = nextPort;
2122
logAlreadyRunningBundler(nextPort);
2223
} else {
2324
const {change} = await askForPortChange(port, nextPort);

0 commit comments

Comments
 (0)