0
Fork 0
mirror of https://github.com/mastra-ai/mastra.git synced 2026-11-17 08:44:46 +00:00

fix: ensure CLI port takes precedence over env PORT (#3025)

## Port Configuration Priority Fix

   ### Problem
The Mastra CLI's port configuration had conflicting priority between the
`--port` flag and environment variables, where the `--port` flag was
being ignored when `PORT` environment variable existed.

   ### Solution
Modified the port configuration priority in the `startServer` function
to:
   1. Give CLI arguments (`--port`) the highest priority
   2. Fall back to environment variables (`PORT`) if no CLI argument
   3. Use default value (`4111`) as the lowest priority

Added a warning when the CLI port overrides an existing PORT environment
variable for better transparency.

   ### Changes
- Reordered environment variable assignment to ensure CLI port takes
precedence
   - Added warning message when overriding environment PORT variable
   - Added changeset for patch version bump

   ### Before
   ```typescript
   env: {
     PORT: port.toString() || '4111',
     ...Object.fromEntries(env),  // Could override PORT
   }
   ```

   ### After
   ```typescript
   env: {
     ...Object.fromEntries(env),
     PORT: port.toString() || process.env.PORT || '4111',
   }
   ```

   ### Testing
- Verified CLI port takes precedence when both `--port` flag and `PORT`
env var are set
   - Tested fallback to environment PORT when no CLI port is specified
- Validated default port (4111) is used when neither CLI nor env PORT is
set

   Fixes #3024
This commit is contained in:
Doug silkstone 2025-03-18 17:07:26 +01:00 • committed by GitHub
commit 2f6a8b615f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 7 additions and 3 deletions