Skip to content

fix(mongodb-runner): don't leave a dangling --port flag on secondaries#821

Merged
lerouxb merged 1 commit into
mainfrom
fix-replset-port
Jul 14, 2026
Merged

fix(mongodb-runner): don't leave a dangling --port flag on secondaries#821
lerouxb merged 1 commit into
mainfrom
fix-replset-port

Conversation

@lerouxb

@lerouxb lerouxb commented Jul 14, 2026

Copy link
Copy Markdown
Member

I noticed that npx mongodb-runner start -t replset -- --port 27017 (copy/pasted from the README) wasn't working.


removePortArg() only removed the value after --port, not the flag itself, so replset secondaries got both the original --port and a second --port 0 appended, causing mongod to reject the duplicate flag.

Also fixes two places that only checked args.includes('--port') and therefore missed the --port=X form: the mongos random-port retry logic and the default --port 0 injection. Extracted a shared hasPortArg() helper in util.ts to avoid duplicating this check.

removePortArg() only removed the value after --port, not the flag
itself, so replset secondaries got both the original --port and a
second --port 0 appended, causing mongod to reject the duplicate flag.

Also fixes two places that only checked args.includes('--port') and
therefore missed the --port=X form: the mongos random-port retry
logic and the default --port 0 injection. Extracted a shared
hasPortArg() helper in util.ts to avoid duplicating this check.
Copilot AI review requested due to automatic review settings July 14, 2026 14:59

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes MongoDB process spawning in mongodb-runner so that port handling is consistent across --port <n> and --port=<n> forms, and avoids generating duplicate/invalid --port arguments (notably for replset secondaries).

Changes:

  • Fix removePortArg() so it removes the --port flag itself (not just its value), preventing dangling/duplicate port flags for replset secondaries.
  • Add a shared hasPortArg() helper to correctly detect both --port and --port=<n> across the codebase.
  • Add regression tests covering replset and sharded cluster startup when ports are pre-specified in both forms.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
packages/mongodb-runner/src/util.ts Adds hasPortArg() helper to detect --port / --port=.
packages/mongodb-runner/src/mongoserver.ts Uses hasPortArg() for mongos random-port logic and default --port 0 injection.
packages/mongodb-runner/src/mongocluster.ts Fixes removePortArg() to remove the --port flag itself; centralizes port detection via hasPortArg().
packages/mongodb-runner/src/mongocluster.spec.ts Adds tests for pre-specified ports in replset primary and mongos configs (both --port x and --port=x).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 167 to 171
function removePortArg([...args]: string[]): string[] {
let portArgIndex = -1;
if ((portArgIndex = args.indexOf('--port')) !== -1) {
args.splice(portArgIndex + 1, 1);
args.splice(portArgIndex, 2);
} else if (

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think anyone uses that.

@lerouxb
lerouxb enabled auto-merge (squash) July 14, 2026 15:24
@lerouxb
lerouxb merged commit c159915 into main Jul 14, 2026
11 of 12 checks passed
@lerouxb
lerouxb deleted the fix-replset-port branch July 14, 2026 17:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants