Skip to content

Conversation

@addaleax
Copy link
Collaborator

Right now, we generate the yargs config based on the zod schema introduced in 57b6add as part of argument parsing.

Since in practice, the yargs config used by mongosh is static, we can use currying to do that part of the call early and include its result in the startup snapshot, removing runtime execution cost.

Right now, we generate the yargs config based on the zod schema introduced
in 57b6add as part of argument parsing.

Since in practice, the yargs config used by mongosh is static, we
can use currying to do that part of the call early and include
its result in the startup snapshot, removing runtime execution cost.
Copilot AI review requested due to automatic review settings December 11, 2025 15:00
@addaleax addaleax requested a review from a team as a code owner December 11, 2025 15:00
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 refactors the argument parsing to enable startup snapshot optimization by currying the yargs configuration generation. The change separates schema-to-yargs conversion (which can be done at build time) from actual argument parsing (which must happen at runtime).

Key changes:

  • Split parseArgs into createParseArgs that returns a curried parser function
  • Split parseArgsWithCliOptions into createParseArgsWithCliOptions similarly
  • Updated the CLI REPL to call the factory function at module load time, ensuring the yargs config is included in the startup snapshot

Reviewed changes

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

File Description
packages/cli-repl/src/arg-parser.ts Imports the new factory function and creates the parser at module load time for snapshot inclusion
packages/arg-parser/src/arg-parser.ts Refactors parsing functions into factory pattern with separate types for creation vs execution options
packages/arg-parser/src/arg-parser.spec.ts Adds wrapper functions to maintain backward compatibility in tests while using the new curried API

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

Comment on lines +19 to +24
const parseArgs = <T extends z.ZodObject>(
opts: ParserCreationOptions<T> & ArgsListOptions
) => createParseArgs(opts)(opts);
const parseArgsWithCliOptions = <T extends z.ZodObject>(
opts: Partial<ParserCreationOptions<T>> & ArgsListOptions
) => createParseArgsWithCliOptions(opts)(opts);
Copy link

Copilot AI Dec 11, 2025

Choose a reason for hiding this comment

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

The test helper functions call createParseArgs and createParseArgsWithCliOptions on every test invocation, recreating the parser each time. Since these tests don't benefit from snapshot optimization and the approach differs from production usage (where the parser is created once), consider adding a comment explaining this test-only pattern to clarify why it differs from the production implementation in cli-repl/src/arg-parser.ts.

Copilot uses AI. Check for mistakes.
...options,
});
const { _: positional, ...parsedArgs } = argv;
return ({ args }: { args: string[] }) => {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Recommend reviewing this with whitespace diffs turned off (https://github.com/mongodb-js/mongosh/pull/2619/changes?w=1)

Copy link
Contributor

@gagik gagik left a comment

Choose a reason for hiding this comment

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

nice! hopefully this resolves the difference in speed

@addaleax addaleax added the no-title-validation Skips validation of PR titles (conventional commit adherence + JIRA ticket inclusion) label Dec 11, 2025
@addaleax addaleax merged commit 3edbc17 into main Dec 11, 2025
148 of 155 checks passed
@addaleax addaleax deleted the zod-yargs-snapshot branch December 11, 2025 17:08
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