Skip to content

Commit cddc66a

Browse files
authored
fix(bazel): ensure PORT variable is prioritized over CLI flag (#2729)
This is necessary for `server_test` to override the port that may be specified via Starlark, or the CLI flag.
1 parent 9eca781 commit cddc66a

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

bazel/http-server/main.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,23 @@
77
*/
88

99
import yargs from 'yargs';
10+
import assert from 'node:assert';
1011

1112
import {HttpServer} from './server';
1213
import {setupBazelWatcherSupport} from './ibazel';
1314

14-
const {rootPaths, historyApiFallback, enableDevUi, environmentVariables, port, relaxCors} = yargs(
15-
process.argv.slice(2),
16-
)
15+
const {
16+
rootPaths,
17+
historyApiFallback,
18+
enableDevUi,
19+
environmentVariables,
20+
port: cliPort,
21+
relaxCors,
22+
} = yargs(process.argv.slice(2))
1723
.strict()
1824
.option('port', {
1925
type: 'number',
20-
default: process.env['PORT'] !== undefined ? Number(process.env['PORT']) : 4200,
21-
defaultDescription: '${PORT}, or 4200 if unset',
26+
default: 4200,
2227
})
2328
.option('historyApiFallback', {type: 'boolean', default: false})
2429
.option('rootPaths', {type: 'array', string: true, default: ['']})
@@ -27,6 +32,13 @@ const {rootPaths, historyApiFallback, enableDevUi, environmentVariables, port, r
2732
.option('relaxCors', {type: 'boolean', default: false})
2833
.parseSync();
2934

35+
let port = cliPort;
36+
// Process environment port always overrides the CLI, or rule attribute-specified port.
37+
if (process.env.PORT !== undefined) {
38+
port = Number(process.env.PORT);
39+
assert(!isNaN(port), 'Expected `PORT` environment variable to be a valid number.');
40+
}
41+
3042
// In non-test executions, we will never allow for the browser-sync dev UI.
3143
const enableUi = process.env.TEST_TARGET === undefined && enableDevUi;
3244
const server = new HttpServer(

0 commit comments

Comments
 (0)