fix(core): preserve user-supplied nx.metadata in package.json projects#35489
fix(core): preserve user-supplied nx.metadata in package.json projects#35489wilcoxmd wants to merge 2 commits intonrwl:masterfrom
Conversation
👷 Deploy request for nx-docs pending review.Visit the deploys page to approve it
|
👷 Deploy request for nx-dev pending review.Visit the deploys page to approve it
|
cac77a4 to
6beca84
Compare
The package.json plugin currently only emits auto-generated metadata keys (`targetGroups`, `description`, `js`) from `getMetadataFromPackageJson`, discarding any custom keys a user puts under `nx.metadata` in their project's package.json. The user-supplied object briefly survives the `...packageJson.nx` spread in `buildProjectConfigurationFromPackageJson`, but is then immediately overwritten by the literal's `metadata:` key. Spread `nx?.metadata` into the result of `getMetadataFromPackageJson` so that user keys are preserved and override auto-generated keys with the same name when explicitly provided. This makes `nx.metadata` a viable extension point for user tooling that wants to attach annotations to projects without forking the project graph.
6beca84 to
fb4f8e3
Compare
| packageMain: main, | ||
| isInPackageManagerWorkspaces, | ||
| }, | ||
| ...nx?.metadata, |
There was a problem hiding this comment.
We should probably use this util here:
Its not gonna be super ergonomic, but invocation would be smth like
mergeMetadata(null,null,null,inferredMetadata,packageJson.nx?.metadata ?? {})
There was a problem hiding this comment.
Oh, thanks for the pointer! Will update the change to use this!
There was a problem hiding this comment.
@AgentEnder I updated the PR to use that util! I have the arguments ordered so that user-supplied keys will still take priority, which is a slightly different argument order than you shared. But if you ordered your example intentionally because you don't want user-specified keys to have priority then let me know and we can switch the args around.
610de31 to
64c268b
Compare
Address review feedback to reuse the existing mergeMetadata util from target-merging instead of a bespoke object spread. User-supplied nx.metadata still wins on key collisions, and per-key merging of nested objects (e.g. targetGroups) is now consistent with how metadata is merged elsewhere.
64c268b to
8528aa4
Compare
|
View your CI Pipeline Execution ↗ for commit 8528aa4
☁️ Nx Cloud last updated this comment at |
Current Behavior
User-supplied
nx.metadatakeys in a project'spackage.jsonare discarded by the package-json plugin. Given:nx show project my-app --json | jq .metadatareturns only{ "targetGroups": {...}, "description": ... }— thefookey is gone.The cause is in
getMetadataFromPackageJson: the function destructuresnxfrompackageJsonbut does not spreadnx?.metadatainto the returned object. That object then replaces (rather than merges with) the user's metadata when the plugin spreads it onto the project configuration inbuildProjectConfigurationFromPackageJson.This makes
nx.metadataunusable as an extension point for user tooling that wants to attach annotations to projects via package.json.Expected Behavior
User-supplied keys on
nx.metadataare preserved alongside the auto-generatedtargetGroupsanddescription. When a key collides, the user-supplied value wins.After this change:
Related Issue(s)
N/A