Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(npm): support pnpm catalogs #33376

Open
wants to merge 46 commits into
base: main
Choose a base branch
from

Conversation

fpapado
Copy link
Contributor

@fpapado fpapado commented Jan 2, 2025

Changes

This PR adds support for pnpm catalogs, extracting dependencies from / updating dependencies in pnpm-workspace.yaml files.

I took @rarkins' suggestion in #30079 (comment), and added this as part of the npm manager. I tried to keep the functionality contained in pnpm files inside the relevant modules, to make it easier to split out in the future, if needed.

I took care of the following things:

  • pnpm-workspace.yaml files are picked up by default; their dependencies are extracted, and any updates are applied
  • Updates should preserve formatting as much as possible (tests back this up, but YAML is complicated)
  • pnpm-workspace.yaml files can be renamed and specified in fileMatch. The npm manager tries to parse a workspace YAML file first, before falling back to pakcage.json-like parsing.
  • The pnpmShrinkwrap file gets picked up, to use for updating. I believe I implemented that correctly, but I might have missed something.
  • Locked catalog dependencies are resolved from the lockfile. This should allow range replacements and other more advanced use-cases.

These questions have been resolved; I leave the text here for posterity

Open question 1: ways of picking up pnpm-workspace.yaml for extraction

I have placed pnpm-workspace.yaml in the fileMatch defaultConfig of the npm manager, and added an extra branch to extractAllPackageFiles.

An alternative would be to do this work in postExtract, by looking up sibling/parent pnpm-workspace.yaml file, and doing similar processing. This avoids the need for changing the config, but might have downsides that I cannot think of. What do you think?

Open question 2: depType and allowing granular updates to named catalogs

In the current version, I gave catalog-related upgrades a depType of pnpm.catalog, just for namespacing. However, I am wondering whether we could go more granular with this, such as pnpm.catalog.<catalogName>

One of the pnpm examples for catalogs, is allowing incremental updates, by defining named catalogs:

catalogs:
  # Can be referenced through "catalog:react17"
  react17:
    react: ^17.0.2
    react-dom: ^17.0.2

  # Can be referenced through "catalog:react18"
  react18:
    react: ^18.2.0
    react-dom: ^18.2.0

In such a scenario, I imagine users wanting to keep react 17 and 18 each in their range (allowing for bugfixes), without bumping the major. To do that, we would need to expose the catalog name to the user for matching, such as {matchDepTypes: ['pnpm.catalog.react17']}.

Have you had similar scenarios in the past? I noticed that pnpm.overrides kind of works like this, when using the pkg-a>pkg-b syntax (reference, example), but based on the depName. This could probably be done in a follow-up PR, but it's good to think about 💭

Context

Closes #30079

pnpm catalogs are described in the pnpm docs

This is my first Renovate contribution, I'm open to any and all ideas 😊

Documentation (please check one with an [x])

  • I have updated the documentation, or
  • [] No documentation update is required

I made a small documentation addition for the readme.md of the npm manager, in order to mention pnpm.catalogs.<name> as a depType.

How I've tested my work (please select one)

I have verified these changes via:

  • Code inspection only, or
  • Newly added/modified unit tests, or
  • No unit tests but ran on a real repository, or
  • Both unit tests + ran on a real repository

I added unit tests for the extraction and updates, the latter being a similar set to the package.json update tests. I will double-check the code coverage on CI, I think I missed something when running coverage locally.

I did a fair bit of repository testing at /~https://github.com/fpapado/renovate-testing-catalogs/pulls, to understand how the npm extraction/update phases work for package.json files.

@fpapado fpapado marked this pull request as ready for review January 2, 2025 17:47
@secustor secustor self-requested a review January 2, 2025 21:18
@viceice viceice self-requested a review January 4, 2025 16:11
@fpapado
Copy link
Contributor Author

fpapado commented Jan 5, 2025

Ah, I noticed now in my test repo that the lockfile does not get updated after a standalone catalog dependency upgrade. I missed that codepath when trying to keep the functionality minimal. I'll add it to my todo 🗒️

Update: this has since been fixed 🎉

@rarkins rarkins marked this pull request as draft January 8, 2025 12:27
@fpapado
Copy link
Contributor Author

fpapado commented Jan 9, 2025

@secustor and @viceice, apologies for the ping, do you have any pointers for the open questions here? If you think the PR is not in a reviewable state, I'm happy to implement with my gut feeling and re-request a review later down the line. I'm asking because I don't want to waste your time if something ends up being unworkable or unidiomatic, but I also don't want this to be stuck in limbo 😌

@viceice
Copy link
Member

viceice commented Jan 9, 2025

we usually don't review draft PRs, will try to have a look tomorrow

Copy link
Collaborator

@secustor secustor left a comment

Choose a reason for hiding this comment

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

I had it on my plate, but haven't come around to finish it.

Open question 1: ways of picking up pnpm-workspace.yaml for extraction

pnpm-workspace.yaml should be part of fileMatch.
The expectation is that users will have use cases which deviating filenames e.g. because they are overwriting this name in a config.
Ideally we detect the fact that it is a workspace file via schema or other marker and are not relying on it. That way we are not bound to the filename and it "magically" works.

Open question 2: depType and allowing granular updates to named catalogs

Having pnpm.catalog.foo is preferable as with string pattern matching users can match all catalogs if needed.
Can you add an example of this in the readme.md that way it will show up in the manager docs.

lib/modules/manager/npm/extract/pnpm.ts Outdated Show resolved Hide resolved
lib/modules/manager/npm/extract/pnpm.ts Outdated Show resolved Hide resolved
lib/modules/manager/npm/extract/pnpm.ts Outdated Show resolved Hide resolved
lib/modules/manager/npm/extract/pnpm.ts Outdated Show resolved Hide resolved
lib/modules/manager/npm/extract/pnpm.ts Outdated Show resolved Hide resolved
lib/modules/manager/npm/update/dependency/pnpm.spec.ts Outdated Show resolved Hide resolved
lib/modules/manager/npm/update/dependency/pnpm.ts Outdated Show resolved Hide resolved
lib/modules/manager/npm/update/dependency/pnpm.ts Outdated Show resolved Hide resolved
lib/modules/manager/npm/update/dependency/common.ts Outdated Show resolved Hide resolved
lib/modules/manager/npm/update/dependency/common.ts Outdated Show resolved Hide resolved
@rarkins
Copy link
Collaborator

rarkins commented Jan 10, 2025

@secuster can't we discover the pnpm-workspace.yaml file automatically instead of needing it in fileMatch?

@secustor
Copy link
Collaborator

@secuster can't we discover the pnpm-workspace.yaml file automatically instead of needing it in fileMatch?

If we are fine with only supporting this file name, we can drop it out of fileMatch.
There are requests regarding the possiblitly to change the file name and workarounds

@rarkins
Copy link
Collaborator

rarkins commented Jan 10, 2025

Ideally the name should be referenced from package.json. If not, I agree that we need to support it in fileMatch, but it's undesirable

Comment on lines +247 to +248
packageFile.path.endsWith('package.json') ||
packageFile.path.endsWith('pnpm-workspace.yaml')
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The hardcoding here tripped me up, I kept not seeing the lockfiles changing 😅 Does this not beat some of the point of allowing pnpm-workspace.yaml (and package.json) being renamed via fileMatch? I wonder what the original context for the check was.

Copy link
Collaborator

Choose a reason for hiding this comment

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

K, I think this will lead to too much changes in this PR. :/

Let's support pnpm-workspace.yaml and then support more names as needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, it turned out to be a bit complicated indeed 😅 Would you rather I keep pnpm-workspace.yaml in fileMatch still? It probably does not hurt to keep it there, just to avoid any more changes, but I'm happy to remove it as well.

@fpapado
Copy link
Contributor Author

fpapado commented Jan 14, 2025

Thank you all kindly for your earler reviews! They helped clarify the direction quite a bit. I've made relevant changes now, tidied up the tests, implemented lockfile updating and locked version detection, and the coverage should be ok too.

I'll put this up for a review now. Let me know if you spot anything that can be improved, and I'll get to it 🔧

@fpapado fpapado marked this pull request as ready for review January 14, 2025 20:27
@fpapado fpapado requested review from secustor and viceice January 14, 2025 20:27
lib/modules/manager/npm/extract/index.ts Outdated Show resolved Hide resolved
lib/modules/manager/npm/extract/index.ts Outdated Show resolved Hide resolved
lib/modules/manager/npm/extract/pnpm.spec.ts Outdated Show resolved Hide resolved
lib/modules/manager/npm/extract/pnpm.ts Outdated Show resolved Hide resolved
lib/modules/manager/npm/extract/pnpm.ts Outdated Show resolved Hide resolved
@fpapado fpapado requested a review from secustor January 19, 2025 18:12
Copy link
Collaborator

@secustor secustor left a comment

Choose a reason for hiding this comment

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

Sry, for the back and forth.

Comment on lines +247 to +248
packageFile.path.endsWith('package.json') ||
packageFile.path.endsWith('pnpm-workspace.yaml')
Copy link
Collaborator

Choose a reason for hiding this comment

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

K, I think this will lead to too much changes in this PR. :/

Let's support pnpm-workspace.yaml and then support more names as needed.

lib/modules/manager/npm/extract/pnpm.spec.ts Outdated Show resolved Hide resolved
fpapado and others added 29 commits January 22, 2025 18:54
Now that we use the CST for substitutions, we can accept inputs
that do not end in newlines. Even though that is not per the
yaml spec, it does mean that we leave any source files formatting
intact.
It turns out that including managerData in one of the extraction deps,
would cause the top-level (packageFile) managerData to be overwritten
for the corresponding upgrade. In practice, this meant that
{managerData: {pnpmShrinkWrap: ''}} was not found, and no lockfile
update was triggered.
Adding pnpm-workspace.yaml to the default matchFiles means that
a few of the assertions were failing.
…ockfile

This ensures that the lockfile update command will see the updated
workspace file, and will update correctly. There was a hardcoded
package.json check, that was previously precluding that.
Co-authored-by: Sebastian Poxhofer <secustor@users.noreply.github.com>
This required a few more changes in the rest of the file, in order
to mock fs and use the utils consistently.
This introduces a tryParsePnpmWorkspaceYaml function, which
parses a string as YAML, and validates it via  a schema, returning
the result, and whether parsing succeeded. This allows the
npm manager extraction call-site to only parse the content once,
and pass on the parsed contents (if any) for extraction.
Previously, this was being tested _incidentally_, due to not mocking
the filesystem calls.
@fpapado fpapado force-pushed the feat/30079-support-pnpm-catalogs branch from 33b479c to a0316a3 Compare January 22, 2025 17:32
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.

Support pnpm Catalogs
4 participants