Avoid bundling package.json in JavaScript files#3573
Conversation
- `package.json` is bundled by `esbuild` because we depend on it in `actions-util.ts` - That is so we can access the `version` property - We now use `build.mjs` to define a constant for it instead - We also set this constant in `ava.setup.mjs` for tests - This reduces the size of the generated `.js` files and avoids changing them entirely in some cases
There was a problem hiding this comment.
Pull request overview
This PR updates the build and test setup so the bundled JavaScript no longer embeds the full package.json just to access the action version. Instead, the version is injected at build/test time via a __CODEQL_ACTION_VERSION__ constant.
Changes:
- Replace
require("../package.json")usage insrc/actions-util.tswith a build-defined__CODEQL_ACTION_VERSION__constant. - Update
build.mjsto readpackage.jsonand pass the version into esbuild viadefine. - Add AVA config/setup files to preload the version constant for unit tests, and remove the old AVA config block from
package.json.
Reviewed changes
Copilot reviewed 18 out of 18 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| src/actions-util.ts | Uses __CODEQL_ACTION_VERSION__ instead of requiring package.json at runtime. |
| build.mjs | Loads package.json during build and injects the version via esbuild define. |
| ava.setup.mjs | Preloads a global version value for tests. |
| ava.config.mjs | Moves AVA configuration from package.json into a config file and preloads ava.setup.mjs. |
| eslint.config.mjs | Ignores the new AVA config/setup files (and other non-linted paths). |
| package.json | Removes embedded AVA configuration (now in ava.config.mjs). |
| lib/analyze-action.js | Generated bundle updated to inline the version constant and omit bundling root package.json. |
| lib/analyze-action-post.js | Generated bundle updated to inline the version constant and omit bundling root package.json. |
| lib/autobuild-action.js | Generated bundle updated to inline the version constant and omit bundling root package.json. |
| lib/init-action.js | Generated bundle updated to inline the version constant and omit bundling root package.json. |
| lib/init-action-post.js | Generated bundle updated to inline the version constant and omit bundling root package.json. |
| lib/resolve-environment-action.js | Generated bundle updated to inline the version constant and omit bundling root package.json. |
| lib/setup-codeql-action.js | Generated bundle updated to inline the version constant and omit bundling root package.json. |
| lib/start-proxy-action.js | Generated bundle updated to inline the version constant and omit bundling root package.json. |
| lib/start-proxy-action-post.js | Generated bundle updated to inline the version constant and omit bundling root package.json. |
| lib/upload-lib.js | Generated bundle updated to inline the version constant and omit bundling root package.json. |
| lib/upload-sarif-action.js | Generated bundle updated to inline the version constant and omit bundling root package.json. |
| lib/upload-sarif-action-post.js | Generated bundle updated to inline the version constant and omit bundling root package.json. |
redsun82
left a comment
There was a problem hiding this comment.
Nice.
This does make me wonder though: couldn't we completely detach from the version in package.json, fix it to a dummy value, and rely on git tags instead in build.mjs? On the other hand, during release we have in any case to update the changelog, so this wouldn't necessarily simplify the release (by requiring one less PR).
Perhaps, but I think we currently rely on the |
Currently, we bundle the entire contents of
package.jsonin our generated JavaScript files. This is because we depend on the"version"property inactions-util.ts.This PR changes this so that we load
package.jsoninbuild.mjs, retrieve theversion, and use it to define a constant named__CODEQL_ACTION_VERSION__that's available to the bundled code instead. Therefore, we no longer depend onpackage.json, andesbuildomits it.To allow the tests to continue functioning (which don't go through the build process first), an equivalent change for
avanow runsava.setup.mjsbefore tests which also loadspackage.jsonand sets__CODEQL_ACTION_VERSION__to theversionvalue.Risk assessment
For internal use only. Please select the risk level of this change:
Which use cases does this change impact?
Workflow types:
dynamicworkflows (Default Setup, Code Quality, ...).Products:
analysis-kinds: code-scanning.analysis-kinds: code-quality.upload-sarifaction.Environments:
github.comand/or GitHub Enterprise Cloud with Data Residency.How did/will you validate this change?
.test.tsfiles).pr-checks).If something goes wrong after this change is released, what are the mitigation and rollback strategies?
How will you know if something goes wrong after this change is released?
Are there any special considerations for merging or releasing this change?
Merge / deployment checklist