Fix port assignment to respect user-defined PORT vars

This commit is contained in:
Chris Wanstrath 2026-03-10 10:08:53 -07:00
parent e97be11a0c
commit 0a1100aaf7

View File

@ -94,16 +94,15 @@ $ true
console.error("--port-from must be an integer")
process.exit(1)
}
let nextPort = portFrom ?? 0
let nextPort = portFrom
const runOne = async (filePath: string) => {
const runOne = async (filePath: string, port: number | undefined) => {
const content = await readFile(filePath, "utf-8")
const parsed = parse(relative(cwd, filePath), content)
// Resolve directives in a single pass. Setup @env is collected separately
// so that the user file's @env always takes precedence.
const envVars: Record<string, string> = {}
if (portFrom !== undefined) envVars["PORT"] = String(nextPort++)
const setupEnvVars: Record<string, string> = {}
const userEnvVars: Record<string, string> = {}
const setupCommands: Command[] = []
@ -124,6 +123,9 @@ $ true
}
}
Object.assign(envVars, setupEnvVars, userEnvVars)
if (port !== undefined && !("PORT" in userEnvVars) && !("PORT" in setupEnvVars)) {
envVars["PORT"] = String(port)
}
const merged: ShoutFile = { ...parsed, commands: [...setupCommands, ...parsed.commands] }
const fileResult = await runFile(merged, {
@ -188,8 +190,7 @@ $ true
}
if (opts.parallel) {
const tasks = files.map(f => runOne(f))
const all = await Promise.all(tasks)
const all = await Promise.all(files.map(f => runOne(f, nextPort !== undefined ? nextPort++ : undefined)))
for (const r of all) {
printDots(r)
results.push(r)
@ -197,7 +198,7 @@ $ true
process.stdout.write("\n")
} else {
for (const filePath of files) {
const r = await runOne(filePath)
const r = await runOne(filePath, nextPort !== undefined ? nextPort++ : undefined)
printDots(r)
results.push(r)
}