From 4b29a1b0352a1166e3cd5e95518d5b89d552f161 Mon Sep 17 00:00:00 2001 From: Shuaib Hasan Akib Date: Wed, 11 Nov 2026 20:20:35 +0600 Subject: [PATCH] fix(@angular/build): pass an explicit port of 0 through to the dev server Previously, requesting an ephemeral port (`--port 0`) was resolved by binding a throwaway net.Server to learn the real port before handing that concrete number to Vite or webpack-dev-server, which then bound it again. This avoided a Vite bug where `server.port: 0` was ignored and the default port (5173) was used instead. That bug is fixed upstream (vitejs/vite#7529, vitejs/vite#23158), and webpack-dev-server has always bound port 0 correctly via Node's own http.Server. Pass 0 straight through instead of pre-resolving it, removing the redundant double-bind. Verified via the existing port_spec.ts "random free port" test for both the Vite-based and webpack-based dev server, which covers this exact scenario. --- packages/angular/build/src/utils/check-port.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/angular/build/src/utils/check-port.ts b/packages/angular/build/src/utils/check-port.ts index d7c04f0b9f72..7f68c3b266fd 100644 --- a/packages/angular/build/src/utils/check-port.ts +++ b/packages/angular/build/src/utils/check-port.ts @@ -15,11 +15,11 @@ function createInUseError(port: number): Error { } export async function checkPort(port: number, host: string): Promise { - // Disabled due to Vite not handling port 0 and instead always using the default value (5173) - // TODO: Enable this again once Vite is fixed - // if (port === 0) { - // return 0; - // } + // A port of 0 requests an OS-assigned ephemeral port. Vite now binds this correctly + // (https://github.com/vitejs/vite/pull/23158); webpack-dev-server always has. + if (port === 0) { + return 0; + } return new Promise((resolve, reject) => { const server = createServer();