Skip to content

Commit 699ede4

Browse files
MeAkibclydin
authored andcommitted
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. (cherry picked from commit 3123cf2)
1 parent c2ae475 commit 699ede4

1 file changed

Lines changed: 5 additions & 5 deletions

File tree

‎packages/angular/build/src/utils/check-port.ts‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ function createInUseError(port: number): Error {
1515
}
1616

1717
export async function checkPort(port: number, host: string): Promise<number> {
18-
// Disabled due to Vite not handling port 0 and instead always using the default value (5173)
19-
// TODO: Enable this again once Vite is fixed
20-
// if (port === 0) {
21-
// return 0;
22-
// }
18+
// A port of 0 requests an OS-assigned ephemeral port. Vite now binds this correctly
19+
// (https://github.com/vitejs/vite/pull/23158); webpack-dev-server always has.
20+
if (port === 0) {
21+
return 0;
22+
}
2323

2424
return new Promise<number>((resolve, reject) => {
2525
const server = createServer();

0 commit comments

Comments
 (0)