From 814d2ec65776c0503ea5108c260aaed7fdb9c6d9 Mon Sep 17 00:00:00 2001
From: Rhys Arkins <rhys@arkins.net>
Date: Mon, 18 Mar 2024 22:48:02 +0100
Subject: [PATCH] fix(yarn): improve yarn proxy configuration (#27984)

---
 docs/usage/self-hosted-experimental.md        |  4 ++++
 .../manager/npm/post-update/yarn.spec.ts      |  2 ++
 lib/modules/manager/npm/post-update/yarn.ts   | 22 +++++++++++--------
 lib/modules/manager/npm/readme.md             |  4 ++++
 4 files changed, 23 insertions(+), 9 deletions(-)

diff --git a/docs/usage/self-hosted-experimental.md b/docs/usage/self-hosted-experimental.md
index 9004995ddf..b05d952b2c 100644
--- a/docs/usage/self-hosted-experimental.md
+++ b/docs/usage/self-hosted-experimental.md
@@ -176,3 +176,7 @@ Don't combine with `redisUrl`, Redis would be preferred over SQlite.
 ## `RENOVATE_X_SUPPRESS_PRE_COMMIT_WARNING`
 
 Suppress the pre-commit support warning in PR bodies.
+
+## `RENOVATE_X_YARN_IGNORE_PROXY`
+
+Skip configuring global Yarn proxy settings if HTTP proxy environment variables are detected.
diff --git a/lib/modules/manager/npm/post-update/yarn.spec.ts b/lib/modules/manager/npm/post-update/yarn.spec.ts
index 005675b1ab..54ff7db991 100644
--- a/lib/modules/manager/npm/post-update/yarn.spec.ts
+++ b/lib/modules/manager/npm/post-update/yarn.spec.ts
@@ -175,7 +175,9 @@ describe('modules/manager/npm/post-update/yarn', () => {
     const res = await yarnHelper.generateLockFile('some-dir', {}, config);
     expect(res.lockFile).toBe('package-lock-contents');
     expect(fixSnapshots(execSnapshots)).toMatchObject([
+      { cmd: 'yarn config unset --home httpProxy' },
       { cmd: 'yarn config set --home httpProxy http://proxy' },
+      { cmd: 'yarn config unset --home httpsProxy' },
       { cmd: 'yarn config set --home httpsProxy http://proxy' },
       {},
     ]);
diff --git a/lib/modules/manager/npm/post-update/yarn.ts b/lib/modules/manager/npm/post-update/yarn.ts
index 1fe7df6f9f..fe4d57a3ba 100644
--- a/lib/modules/manager/npm/post-update/yarn.ts
+++ b/lib/modules/manager/npm/post-update/yarn.ts
@@ -210,15 +210,19 @@ export async function generateLockFile(
       commands.push(`yarn set version ${quote(yarnUpdate.newValue!)}`);
     }
 
-    if (process.env.HTTP_PROXY && !isYarn1) {
-      commands.push(
-        `yarn config set --home httpProxy ${quote(process.env.HTTP_PROXY)}`,
-      );
-    }
-    if (process.env.HTTPS_PROXY && !isYarn1) {
-      commands.push(
-        `yarn config set --home httpsProxy ${quote(process.env.HTTPS_PROXY)}`,
-      );
+    if (!process.env.RENOVATE_X_YARN_IGNORE_PROXY) {
+      if (process.env.HTTP_PROXY && !isYarn1) {
+        commands.push('yarn config unset --home httpProxy');
+        commands.push(
+          `yarn config set --home httpProxy ${quote(process.env.HTTP_PROXY)}`,
+        );
+      }
+      if (process.env.HTTPS_PROXY && !isYarn1) {
+        commands.push('yarn config unset --home httpsProxy');
+        commands.push(
+          `yarn config set --home httpsProxy ${quote(process.env.HTTPS_PROXY)}`,
+        );
+      }
     }
 
     // This command updates the lock file based on package.json
diff --git a/lib/modules/manager/npm/readme.md b/lib/modules/manager/npm/readme.md
index 031060d7dc..aa0be6c827 100644
--- a/lib/modules/manager/npm/readme.md
+++ b/lib/modules/manager/npm/readme.md
@@ -19,3 +19,7 @@ If Renovate detects a `packageManager` setting for Yarn in `package.json` then i
 Yarn itself does not natively recognize/support the `HTTP_PROXY` and `HTTPS_PROXY` environment variables.
 If Renovate detects Yarn 2+, and one or both of those variables are present, then it will run commands like `yarn config set --home httpProxy http://proxy` prior to executing `yarn install`.
 This will result in the `~/.yarnrc.yml` file being created or modified with these settings, and the settings are not removed afterwards.
+
+Configuration/conversion of `NO_PROXY` to Yarn config is not supported.
+
+You can configure `RENOVATE_X_YARN_IGNORE_PROXY=true` as an environment variable to skip the configuring of Yarn proxy (e.g. if you already configure these proxy settings yourself in `~/.yarnrc.yml`);
-- 
GitLab