Skip to content

Conversation

@lerouxb
Copy link
Contributor

@lerouxb lerouxb commented Jul 7, 2025

I was comparing old and new autocomplete when I noticed this bug in old autocomplete.

type:

db.test.aggregate({<tab>

and you get this error:

replSet-cc08112a-19c0-4a5d-9c59-9022c9b28c9c [direct: primary] test> db.test.aggregate({Tab completion error: TypeError: Invalid Version: >=6.0.10 <7.0.0 || >=7.0.2
    at new SemVer (/Users/leroux.bodenstein/mongo/mongosh/node_modules/semver/classes/semver.js:40:13)
    at compare (/Users/leroux.bodenstein/mongo/mongosh/node_modules/semver/functions/compare.js:5:32)
    at Object.gte (/Users/leroux.bodenstein/mongo/mongosh/node_modules/semver/functions/gte.js:4:30)
    at isAcceptable (/Users/leroux.bodenstein/mongo/mongosh/packages/autocomplete/lib/index.js:194:34)
    at /Users/leroux.bodenstein/mongo/mongosh/packages/autocomplete/lib/index.js:218:56
    at eval (eval at innerEval (/Users/leroux.bodenstein/mongo/mongosh/packages/shell-evaluator/lib/shell-evaluator.js:69:17), <anonymous>:104:40)
    at _cr2.filter (eval at innerEval (/Users/leroux.bodenstein/mongo/mongosh/packages/shell-evaluator/lib/shell-evaluator.js:69:17), <anonymous>:106:184)
    at filterQueries (/Users/leroux.bodenstein/mongo/mongosh/packages/autocomplete/lib/index.js:217:30)
    at completer (/Users/leroux.bodenstein/mongo/mongosh/packages/autocomplete/lib/index.js:151:26)
    at /Users/leroux.bodenstein/mongo/mongosh/packages/cli-repl/lib/mongosh-repl.js:263:32
    at innerCompleter (/Users/leroux.bodenstein/mongo/mongosh/packages/cli-repl/lib/mongosh-repl.js:265:19)
    at PrettyREPLServer.<anonymous> (/Users/leroux.bodenstein/mongo/mongosh/packages/cli-repl/lib/mongosh-repl.js:281:51)
    at PrettyREPLServer.Callbackified [as completer] (node:util:386:5)
    at Interface._tabComplete (node:readline:440:8)
    at [_ttyWrite] [as _ttyWrite] (node:internal/readline/interface:1325:31)
    at REPLServer.self._ttyWrite (node:repl:1019:9)
    at LineByLineInput.onkeypress (node:internal/readline/interface:267:20)
    at LineByLineInput.emit (node:events:530:35)
    at LineByLineInput.emit (node:domain:489:12)
    at emitKeys (node:internal/readline/utils:370:14)
    at emitKeys.next (<anonymous>)
    at LineByLineInput.onData (node:internal/readline/emitKeypressEvents:64:36)
    at LineByLineInput.emit (node:events:518:28)
    at LineByLineInput.emit (node:domain:489:12)
    at addChunk (node:internal/streams/readable:561:12)
    at readableAddChunkPushByteMode (node:internal/streams/readable:512:3)
    at Readable.push (node:internal/streams/readable:392:5)
    at LineByLineInput.push (/Users/leroux.bodenstein/mongo/mongosh/packages/cli-repl/lib/line-by-line-input.js:136:26)
    at LineByLineInput._flush (/Users/leroux.bodenstein/mongo/mongosh/packages/cli-repl/lib/line-by-line-input.js:115:18)
    at LineByLineInput._forwardAndBlockOnNewline (/Users/leroux.bodenstein/mongo/mongosh/packages/cli-repl/lib/line-by-line-input.js:89:14)
    at LineByLineInput._onData (/Users/leroux.bodenstein/mongo/mongosh/packages/cli-repl/lib/line-by-line-input.js:14:29)
    at ReadStream.<anonymous> (/Users/leroux.bodenstein/mongo/mongosh/packages/cli-repl/lib/line-by-line-input.js:51:56)
    at ReadStream.emit (node:events:518:28)
    at ReadStream.emit (node:domain:489:12)
    at addChunk (node:internal/streams/readable:561:12)
    at readableAddChunkPushByteMode (node:internal/streams/readable:512:3)
    at Readable.push (node:internal/streams/readable:392:5)
    at TTY.onStreamRead (node:internal/stream_base_commons:189:23)
    at TTY.callbackTrampoline (node:internal/async_hooks:130:17)

Copilot AI review requested due to automatic review settings July 7, 2025 10:21
@lerouxb lerouxb requested a review from a team as a code owner July 7, 2025 10:21
Copy link

Copilot AI left a comment

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 a bug in the old autocomplete logic by properly handling semver ranges in version checks.

  • Introduces a satisfiesVersion helper to use semver.satisfies for both exact versions and ranges.
  • Replaces the direct semver.gte call in isAcceptable with satisfiesVersion.
Comments suppressed due to low confidence (1)

packages/autocomplete/src/index.ts:319

  • Add unit tests for satisfiesVersion to cover both plain version inputs and complex range expressions to ensure future changes don’t regress this logic.
      satisfiesVersion(

Comment on lines +295 to +296
const isGTECheck = /^\d+?\.\d+?\.\d+?$/.test(v2);
return semver.satisfies(v1, isGTECheck ? `>=${v2}` : v2);
Copy link

Copilot AI Jul 7, 2025

Choose a reason for hiding this comment

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

Rather than a custom regex, consider using semver.valid(v2) to detect exact version strings, which will handle pre-release and build metadata more robustly.

Suggested change
const isGTECheck = /^\d+?\.\d+?\.\d+?$/.test(v2);
return semver.satisfies(v1, isGTECheck ? `>=${v2}` : v2);
const isValidVersion = semver.valid(v2);
return isValidVersion && semver.satisfies(v1, isValidVersion.startsWith('>=') ? v2 : `>=${v2}`);

Copilot uses AI. Check for mistakes.
@addaleax addaleax changed the title fix a semver check in old autocomplete fix(autocomplete): fix a semver check in old autocomplete Jul 7, 2025
@lerouxb lerouxb added the no-title-validation Skips validation of PR titles (conventional commit adherence + JIRA ticket inclusion) label Jul 7, 2025
'db.shipwrecks.aggregate([{$sortByCount',
'db.shipwrecks.aggregate([{$unionWith',
'db.shipwrecks.aggregate([{$unset',
'db.shipwrecks.aggregate([{$unwind',
Copy link
Collaborator

Choose a reason for hiding this comment

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

Did this test error before? I think we could just verify that it doesn't throw, rather than asserting against the full list, since this list probably changes over time (while the old autocompleter exists, at least)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

on server 4.4, though? I suppose I can change it.

@lerouxb lerouxb merged commit a472f6a into main Jul 7, 2025
72 of 78 checks passed
@lerouxb lerouxb deleted the fix-semver-check branch July 7, 2025 14:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

no-title-validation Skips validation of PR titles (conventional commit adherence + JIRA ticket inclusion)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants