fix(mongodb-runner): don't leave a dangling --port flag on secondaries#821
Merged
Conversation
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.
nbbeeken
approved these changes
Jul 14, 2026
Contributor
There was a problem hiding this comment.
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--portflag itself (not just its value), preventing dangling/duplicate port flags for replset secondaries. - Add a shared
hasPortArg()helper to correctly detect both--portand--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 ( |
Member
Author
There was a problem hiding this comment.
I don't think anyone uses that.
lerouxb
enabled auto-merge (squash)
July 14, 2026 15:24
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.