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

fix(manger/npm): apply config.npmrc during extraction, not in post-update #19812

Merged
merged 7 commits into from Jan 20, 2023
Merged

fix(manger/npm): apply config.npmrc during extraction, not in post-update #19812

merged 7 commits into from Jan 20, 2023

Conversation

simon-abbott
Copy link
Contributor

@simon-abbott simon-abbott commented Jan 12, 2023

Changes

This moves the application of the global npmrc config option from the post-update phase to the extract phase.

Context

Sub-packages in monorepos sometimes have their own .npmrc files. These should not be shared between packages. This fixes a particularly nasty bug, reported in #12891.

Here's what is actually happening to cause #12891:

  1. Create a monorepo using Lerna
  2. Make two sub-packages foo and bar
  3. Add a .npmrc file to foo with the contents legacy-peer-deps=true
  4. Have an out-of-date dependency in foo
  5. Run Renovate

When Renovate runs it will execute branchifyUpgrades(), which, in turn, calls flattenUpdates, which will return a branch config object with { npmrc: "legacy-peer-deps=true" }. Later on in the process that config is passed to writeExistingFiles for each package, which dutifully creates a .npmrc file with legacy-peer-deps=true in each subpackage. This is the real bug, since now legacy-peer-deps has been turned on for package bar, even though it's only defined in foo. Then, when the updates are finished Renovate runs lerna bootstrap to update the lockfiles. When Lerna runs with legacy-peer-deps=true it regenerates the lock files, likely because the dependency resolution graph is different since it can meet peer requirements more easily.

To fix this issue, we now apple the npmrc config when extracting per-package-file configs, and write the result to disk writeExistingFiles.

Closes #12891

Documentation (please check one with an [x])

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

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

@simon-abbott
Copy link
Contributor Author

Another approach I thought of to fix this would be to always set the packageFile.npmrc setting in extractPackageFile, and only use that in writeExistingFiles.

Copy link
Member

@viceice viceice left a comment

Choose a reason for hiding this comment

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

this needs a unit test

@viceice
Copy link
Member

viceice commented Jan 12, 2023

Another approach I thought of to fix this would be to always set the packageFile.npmrc setting in extractPackageFile, and only use that in writeExistingFiles.

that would probably the better solution.

@simon-abbott
Copy link
Contributor Author

that would probably the better solution.

It shall be done.

this needs a unit test

Agreed, I just wasn't entirely sure how to do that, but doing the other approach would make that easier as well I think.

@simon-abbott simon-abbott changed the title fix: don't copy package npmrc config into branch config fix: apply config.npmrc during extration, not in post-update Jan 13, 2023
@simon-abbott simon-abbott changed the title fix: apply config.npmrc during extration, not in post-update fix: apply config.npmrc during extraction, not in post-update Jan 13, 2023
@simon-abbott
Copy link
Contributor Author

Force-pushed because this is a complete rewrite of the patch.

Copy link
Member

@viceice viceice left a comment

Choose a reason for hiding this comment

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

looks good so far. please create some sample repos to do some real tests. this change is risky to break something.

@simon-abbott
Copy link
Contributor Author

simon-abbott commented Jan 16, 2023

looks good so far. please create some sample repos to do some real tests. this change is risky to break something.

What do you mean by that? I didn't see any repos as part of the test suite, did I miss something?

EDIT: If you mean that I should test it on real repos, I have done that. I tested extensively against our private repo before pushing the changes, and I have confirmed that it fixes the issue described in #12891, and that the --npmrc and --npmrcMerge CLI flags both still work as intended (and thus that their config file equivalents will also still work). If that's not what you meant, then I don't fully understand what you're asking.

@viceice
Copy link
Member

viceice commented Jan 16, 2023

yes, real repo tests was my intention. thanks for confirming.

@viceice viceice changed the title fix: apply config.npmrc during extraction, not in post-update fix(manger/npm): apply config.npmrc during extraction, not in post-update Jan 20, 2023
@viceice viceice requested a review from rarkins January 20, 2023 08:22
@rarkins rarkins enabled auto-merge (squash) January 20, 2023 11:21
@rarkins rarkins merged commit 8c44d6b into renovatebot:main Jan 20, 2023
@renovate-release
Copy link
Collaborator

🎉 This PR is included in version 34.107.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

@simon-abbott simon-abbott deleted the fix-12891 branch January 20, 2023 17:05
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 20, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Renovate removing dependencies in "unaffected" package in monorepo
4 participants