Skip to content

fix(core): preserve user-supplied nx.metadata in package.json projects#35489

Open
wilcoxmd wants to merge 2 commits intonrwl:masterfrom
wilcoxmd:mwilcox/preserve-user-metadata-in-package-json-plugin
Open

fix(core): preserve user-supplied nx.metadata in package.json projects#35489
wilcoxmd wants to merge 2 commits intonrwl:masterfrom
wilcoxmd:mwilcox/preserve-user-metadata-in-package-json-plugin

Conversation

@wilcoxmd
Copy link
Copy Markdown

@wilcoxmd wilcoxmd commented Apr 29, 2026

Current Behavior

User-supplied nx.metadata keys in a project's package.json are discarded by the package-json plugin. Given:

// apps/my-app/package.json
{
  "name": "my-app",
  "description": "my app",
  "nx": {
    "metadata": { "foo": "bar" }
  }
}

nx show project my-app --json | jq .metadata returns only { "targetGroups": {...}, "description": ... } — the foo key is gone.

The cause is in getMetadataFromPackageJson: the function destructures nx from packageJson but does not spread nx?.metadata into the returned object. That object then replaces (rather than merges with) the user's metadata when the plugin spreads it onto the project configuration in buildProjectConfigurationFromPackageJson.

This makes nx.metadata unusable as an extension point for user tooling that wants to attach annotations to projects via package.json.

Expected Behavior

User-supplied keys on nx.metadata are preserved alongside the auto-generated targetGroups and description. When a key collides, the user-supplied value wins.

After this change:

$ nx show project my-app --json | jq .metadata
{
  "targetGroups": { "NPM Scripts": [...] },
  "description": "...",
  "foo": "bar"
}

Related Issue(s)

N/A

@netlify
Copy link
Copy Markdown

netlify Bot commented Apr 29, 2026

👷 Deploy request for nx-docs pending review.

Visit the deploys page to approve it

Name Link
🔨 Latest commit 8528aa4

@netlify
Copy link
Copy Markdown

netlify Bot commented Apr 29, 2026

👷 Deploy request for nx-dev pending review.

Visit the deploys page to approve it

Name Link
🔨 Latest commit 8528aa4

@wilcoxmd wilcoxmd force-pushed the mwilcox/preserve-user-metadata-in-package-json-plugin branch from cac77a4 to 6beca84 Compare April 29, 2026 04:58
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.
@wilcoxmd wilcoxmd force-pushed the mwilcox/preserve-user-metadata-in-package-json-plugin branch from 6beca84 to fb4f8e3 Compare April 29, 2026 05:06
@wilcoxmd wilcoxmd marked this pull request as ready for review April 29, 2026 05:21
@wilcoxmd wilcoxmd requested a review from a team as a code owner April 29, 2026 05:21
@wilcoxmd wilcoxmd requested a review from FrozenPandaz April 29, 2026 05:21
Comment thread packages/nx/src/utils/package-json.ts Outdated
packageMain: main,
isInPackageManagerWorkspaces,
},
...nx?.metadata,
Copy link
Copy Markdown
Member

@AgentEnder AgentEnder Apr 29, 2026

Choose a reason for hiding this comment

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

We should probably use this util here:

export function mergeMetadata<T = ProjectMetadata | TargetMetadata>(

Its not gonna be super ergonomic, but invocation would be smth like

mergeMetadata(null,null,null,inferredMetadata,packageJson.nx?.metadata ?? {})

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Oh, thanks for the pointer! Will update the change to use this!

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

@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.

@wilcoxmd wilcoxmd force-pushed the mwilcox/preserve-user-metadata-in-package-json-plugin branch 3 times, most recently from 610de31 to 64c268b Compare April 29, 2026 23:28
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.
@wilcoxmd wilcoxmd force-pushed the mwilcox/preserve-user-metadata-in-package-json-plugin branch from 64c268b to 8528aa4 Compare April 29, 2026 23:30
@nx-cloud
Copy link
Copy Markdown
Contributor

nx-cloud Bot commented Apr 29, 2026

View your CI Pipeline Execution ↗ for commit 8528aa4

Command Status Duration Result
nx affected --targets=lint,test,build,e2e,e2e-c... ✅ Succeeded 1h 1m 8s View ↗
nx run-many -t check-imports check-lock-files c... ✅ Succeeded 3s View ↗
nx-cloud record -- pnpm nx-cloud conformance:check ✅ Succeeded 16s View ↗
nx build workspace-plugin ✅ Succeeded <1s View ↗
nx-cloud record -- nx sync:check ✅ Succeeded 23s View ↗
nx-cloud record -- nx format:check ✅ Succeeded 6s View ↗

☁️ Nx Cloud last updated this comment at 2026-04-30 01:02:14 UTC

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