CODEGEN-848 - Fix CLI --version flag#10858
Conversation
🦋 Changeset detectedLatest commit: 78bc323 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
| "example-apollo-client-swc-plugin", | ||
| "example-react-nextjs-swr" |
There was a problem hiding this comment.
Drive-by fixing these, as they are breaking CI.
These projects have a rust- prefix now so changeset complains they no longer exist
💻 Website PreviewThe latest changes are available as preview in: https://pr-10858.graphql-code-generator.pages.dev |
There was a problem hiding this comment.
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-versionscript that writessrc/_version.tsfrom 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.
| "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", |
e38eb33 to
97e109f
Compare
18355f0 to
a3a953c
Compare
a3a953c to
ef75932
Compare
ef75932 to
03b09e9
Compare
03b09e9 to
425805d
Compare
Description
--versionflag was broken because it wasn't explicitly supplied toyargs, which meant it does an automatic lookup starting with thepackage.jsonat 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:
package.jsonat postbuild timeyargsThis approach was chosen over the many options:
import {} from '../package.json'does not work in ESM without import attributes i.e.with { type: 'json' }.createRequirefromnode:moduleneedsimport.meta.urlwhich works for ESM, but doesn't work for CJS because it is a syntax error at parse-time.fs.readFilereads fromcwd, which means we need to use__dirnameorimport.meta.urlas the starting point.import.meta.urlis a syntax error at parse-time for CJS..mtsfile and dynamically import it at runtime for ESMnodenextinstead ofesnextformoduleandmoduleResolution. Maybe a big change?Result
Local test
Alpha version test
Type of change