Skip to content

CODEGEN-848 - Fix CLI --version flag#10858

Merged
eddeee888 merged 4 commits into
masterfrom
fix-cli-version
May 31, 2026
Merged

CODEGEN-848 - Fix CLI --version flag#10858
eddeee888 merged 4 commits into
masterfrom
fix-cli-version

Conversation

@eddeee888
Copy link
Copy Markdown
Collaborator

@eddeee888 eddeee888 commented May 29, 2026

Description

--version flag was broken because it wasn't explicitly supplied to yargs, which meant it does an automatic lookup starting with the package.json at cwd when run.
For a consumer repo, this will always return the wrong version because the cwd would be the project root.

Related #10397

This PR fixes this issue by preparing the version ahead of runtime:

  • by extracting the version in package.json at postbuild time
  • inject into a file
  • supply to yargs

This approach was chosen over the many options:

  1. import {} from '../package.json' does not work in ESM without import attributes i.e. with { type: 'json' }.
  2. createRequire from node:module needs import.meta.url which works for ESM, but doesn't work for CJS because it is a syntax error at parse-time.
  3. fs.readFile reads from cwd, which means we need to use __dirname or import.meta.url as the starting point. import.meta.url is a syntax error at parse-time for CJS.
  4. Create a shim .mts file and dynamically import it at runtime for ESM
  5. Use nodenext instead of esnext for module and moduleResolution. Maybe a big change?
  6. Ship ESM-only. This is a big change.

Result

Local test

Screenshot 2026-05-30 at 12 35 26 am

Alpha version test

Screenshot 2026-05-30 at 12 40 24 am

Type of change

  • Bug fix (non-breaking change which fixes an issue)

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented May 29, 2026

🦋 Changeset detected

Latest commit: 78bc323

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@graphql-codegen/cli Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Comment thread .changeset/config.json
Comment on lines -13 to -14
"example-apollo-client-swc-plugin",
"example-react-nextjs-swr"
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Drive-by fixing these, as they are breaking CI.

These projects have a rust- prefix now so changeset complains they no longer exist

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 29, 2026

🚀 Snapshot Release (alpha)

The latest changes of this PR are available as alpha on npm (based on the declared changesets):

Package Version Info
@graphql-codegen/cli 7.1.1-alpha-20260531030001-78bc32357d299cbfd3987a33579e7c5533fafe6c npm ↗︎ unpkg ↗︎

Comment thread dev-test/general/package.json Outdated
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 29, 2026

💻 Website Preview

The latest changes are available as preview in: https://pr-10858.graphql-code-generator.pages.dev

Copy link
Copy Markdown

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 the CLI --version output by generating a build-time version module for @graphql-codegen/cli and wiring that value into yargs.

Changes:

  • Adds a prepare-cli-version script that writes src/_version.ts from the CLI package version.
  • Passes the generated version to yargs in CLI argument parsing.
  • Adds dev-test workflow coverage and a patch changeset for the CLI package.

Reviewed changes

Copilot reviewed 7 out of 8 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
scripts/prepare-cli-version.mjs Generates the CLI version source file before build/publish.
packages/graphql-codegen-cli/src/config.ts Imports the generated version and supplies it to yargs.
package.json Adds preparation and dev-test scripts.
dev-test/general/package.json Adds CJS/ESM scripts for invoking --version.
.gitignore Ignores the generated version source file.
.github/workflows/main.yml Runs CLI version checks in dev-test CI.
.changeset/fresh-crabs-open.md Adds a patch changeset for the CLI fix.
.changeset/config.json Updates ignored package patterns for changesets.

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

Comment thread scripts/prepare-cli-version.mjs Outdated
Comment thread dev-test/general/package.json Outdated
Comment on lines +10 to +11
"test-cli-version:cjs": "node ../../packages/graphql-codegen-cli/dist/cjs/bin.js --version",
"test-cli-version:esm": "node ../../packages/graphql-codegen-cli/dist/esm/bin.js --version",
Comment thread package.json
Copy link
Copy Markdown

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

Copilot reviewed 7 out of 7 changed files in this pull request and generated 3 comments.

Comment thread scripts/prepare-cli-version.mjs Outdated
Comment thread package.json Outdated
Comment thread package.json Outdated
Copy link
Copy Markdown

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

Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.

Comment thread eslint.config.mjs Outdated
Comment thread packages/graphql-codegen-cli/src/config.ts
@eddeee888 eddeee888 merged commit 3fa901b into master May 31, 2026
18 checks passed
@eddeee888 eddeee888 deleted the fix-cli-version branch May 31, 2026 03:04
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.

2 participants