feat(package-metrics): adds package-metrics action - #38
feat(package-metrics): adds package-metrics action#38christopherferreira9 wants to merge 15 commits into
Conversation
📦 Package Metrics ReportCommit
|
…ith results and warning for when checkout happened without a ref
| with: | ||
| ref: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.sha || '' }} |
There was a problem hiding this comment.
we should specify a ref when using this action in order to avoid getting metrics for an ephemeral merge commit
There was a problem hiding this comment.
All files under package-metrics/__fixtures__ are only used for testing the action on this repo itself. They're used as dummy packages repositories.
| export async function bundleSize(pkgPath) { | ||
| try { | ||
| const entry = resolveEntry(pkgPath); | ||
| const result = await esbuild.build({ |
There was a problem hiding this comment.
Not entirely sure if this would follow exactly the same way we build on consumers but it should allow us to get a baseline that would correlate to real values.
| const pkgJsonPath = path.join(packageDir, 'package.json'); | ||
| const pkg = JSON.parse(readFileSync(pkgJsonPath, 'utf8')); | ||
|
|
||
| // peerDependencies aren't rewritten: a plain npm install doesn't |
There was a problem hiding this comment.
I think it depends on npm version. In npm 7 and up peer deps are installed by default.
https://docs.npmjs.com/cli/v10/configuring-npm/package-json#peerdependencies
Also --omit=dev still resolves dependencies (it just doesn't install them on the disc). Which means it would most likely fail during peer deps resolution part of workspace:* for monorepos. Worth looking into this flag instead:
https://docs.npmjs.com/cli/v10/using-npm/config#legacy-peer-deps
There was a problem hiding this comment.
Ah this is a very good point! I think the current approach for considering peerDeps packages for the installSize metric is not consistent to be honest, let me rethink this, will leave the comment open.
There was a problem hiding this comment.
This was broken in two ways: a peeDep resolving to a real registry package was already silently counted despite the comment claiming otherwise, and a workspace-sibling peerDep (to a real but unpublished semver) would 404 and fail the whole install. Fixed by adding peerDependencies to the same sibling-rewrite loop already used for dependencies/optionalDependencies — it now resolves locally instead of hitting the registry, and its size counts toward installSize, matching what a real npm≥7 install would actually do. Fix in: 8204568
|
|
||
| // peerDependencies aren't rewritten: a plain npm install doesn't | ||
| // auto-install them, so it wouldn't affect the measured size. | ||
| for (const depsField of ['dependencies', 'optionalDependencies']) { |
There was a problem hiding this comment.
nit: it may not be applicable today, but consider the following package structure.
my-monorepo/ NOTE: none of a, b, c exist on npmjs.com
├─ pnpm-workspace.yaml
└─ packages/
├─ a/package.json { "name": "a", "version": "1.0.0" }
│ (needs nothing)
│
├─ b/package.json { "name": "b", "version": "1.0.0",
│ "dependencies": { "a": "workspace:*" } }
│
└─ c/package.json { "name": "c", "version": "1.0.0",
"dependencies": { "b": "workspace:*" } }
chain: c ──needs──> b ──needs──> aJust something worth flagging and keeping in mind. Because we only rewrite 1 level down, we'd get an error: "no package named 'a' exists". But imo we can accept it as an edge case and resolve it once we hit it.
There was a problem hiding this comment.
Good catch! This implies changing the algorithm but I think its worth doing now since there are no repositories consuming this action just yet.
There was a problem hiding this comment.
Fixed in d3e48fa. Also made the pnpm monorepo fixture use this exact structure to be tested.
| ``` | ||
|
|
||
| - `schemaVersion` — increments if this shape ever changes in a breaking way. Consumers | ||
| (e.g. a future artifact-scraping/Grafana ingestion job) should check this field. |
There was a problem hiding this comment.
Worth noting that there's a retention period on storing artefacts in github. Not sure what it's for Offchain/ZD but worth taking into account if we don't want to lose some data before we migrate it to Grafana.
Nothing actionable, just something to keep in mind.
There was a problem hiding this comment.
This is a good point. I'd say that for now we need to take it with a grain of salt and work towards storing this data on our end. We can also hold from using this action on the consumer side until the entire stack is in place.
| try { | ||
| entries = readdirSync(packagesRoot, { withFileTypes: true }); | ||
| } catch (err) { | ||
| console.warn(`package-metrics: could not read "${packagesRoot}": ${err.message}`); |
There was a problem hiding this comment.
I think this way we'd actually get to see the warning in the console, instead just output logs.
| console.warn(`package-metrics: could not read "${packagesRoot}": ${err.message}`); | |
| core.warning(`package-metrics: could not read "${packagesRoot}": ${err.message}`); |
There was a problem hiding this comment.
Agree, this will increase visibility. Fixed here: 40ccbfa
|
|
||
| let packages; | ||
| try { | ||
| packages = discoverPackages({ workspaceRoot, isMonorepo, paths }); |
There was a problem hiding this comment.
Shouldn't we throw if packages is empty and let the action fail?
Purpose
This PR adds a new reusable action meant to be used on repositories that consist of developer oriented packages. All runs will upload its own JSON result so we should be able to at later stage scrape results and consume this information in Grafana. This will allow tracking of the metrics outlined below to the specific commit.
It measures:
Workflow Inputs
paths: a list of paths where packages are locatedis-monorepo: when true, paths is ignored and packages will be automatically discovered underpackages/enable-comment: when true, a comment is made to the PR with the output dataNotes
enable-commentis false, a visual queue is still displayed.corepackpnpm packoryarn packwon't be able to resolveworkspace:*referencesSamples
JSON Output:
PR Comment:
📦 Package Metrics Report
Commit
160f6e9· 2026-07-08T16:10:30.934Zfixture-single-pkgFull JSON report →