From f414cf9a5455a476d63e587d0b8616729e163ec4 Mon Sep 17 00:00:00 2001
From: Michael Kriese <michael.kriese@visualon.de>
Date: Fri, 5 Aug 2022 09:49:58 +0200
Subject: [PATCH] fix(manager/npm): support yarn update without yarnPath
 (#16988)

---
 .../manager/npm/post-update/index.spec.ts      | 18 +++++++++++++++++-
 lib/modules/manager/npm/post-update/index.ts   | 13 +++++++++++--
 lib/modules/manager/npm/post-update/types.ts   |  4 ++++
 3 files changed, 32 insertions(+), 3 deletions(-)

diff --git a/lib/modules/manager/npm/post-update/index.spec.ts b/lib/modules/manager/npm/post-update/index.spec.ts
index cf845f8ff6..19b2b2d4f1 100644
--- a/lib/modules/manager/npm/post-update/index.spec.ts
+++ b/lib/modules/manager/npm/post-update/index.spec.ts
@@ -1,7 +1,7 @@
 // TODO: add tests
 import upath from 'upath';
 import { Fixtures } from '../../../../../test/fixtures';
-import { fs, git, partial } from '../../../../../test/util';
+import { fs, git, logger, partial } from '../../../../../test/util';
 import { GlobalConfig } from '../../../../config/global';
 import type { FileChange } from '../../../../util/git/types';
 import type { PostUpdateConfig } from '../../types';
@@ -302,6 +302,22 @@ describe('modules/manager/npm/post-update/index', () => {
       expect(existingYarnrcYmlContent).toMatch(oldYarnrcYml);
       expect(updatedArtifacts).toBeEmpty();
     });
+
+    it('should support Yarn with corepack', async () => {
+      git.getFile.mockResolvedValueOnce('');
+      fs.readLocalFile.mockResolvedValueOnce('');
+      fs.readLocalFile.mockResolvedValueOnce('');
+      const updatedArtifacts: FileChange[] = [];
+      const yarnrcYmlContent = await updateYarnBinary(
+        lockFileDir,
+        updatedArtifacts,
+        ''
+      );
+      expect(yarnrcYmlContent).toBe('');
+      expect(updatedArtifacts).toEqual([]);
+      expect(logger.logger.debug).not.toHaveBeenCalled();
+      expect(logger.logger.error).not.toHaveBeenCalled();
+    });
   });
 
   describe('getAdditionalFiles()', () => {
diff --git a/lib/modules/manager/npm/post-update/index.ts b/lib/modules/manager/npm/post-update/index.ts
index ee8697e385..f1c8636996 100644
--- a/lib/modules/manager/npm/post-update/index.ts
+++ b/lib/modules/manager/npm/post-update/index.ts
@@ -36,6 +36,7 @@ import type {
   ArtifactError,
   DetermineLockFileDirsResult,
   WriteExistingFilesResult,
+  YarnRcYmlFile,
 } from './types';
 import * as yarn from './yarn';
 
@@ -408,6 +409,7 @@ async function updateYarnOffline(
   }
 }
 
+// TODO: move to ./yarn.ts
 // exported for testing
 export async function updateYarnBinary(
   lockFileDir: string,
@@ -423,8 +425,15 @@ export async function updateYarnBinary(
       return existingYarnrcYmlContent;
     }
 
-    const oldYarnPath = (load(yarnrcYml) as Record<string, string>).yarnPath;
-    const newYarnPath = (load(newYarnrcYml) as Record<string, string>).yarnPath;
+    const oldYarnPath = (load(yarnrcYml) as YarnRcYmlFile)?.yarnPath;
+    const newYarnPath = (load(newYarnrcYml) as YarnRcYmlFile)?.yarnPath;
+    if (
+      !is.nonEmptyStringAndNotWhitespace(oldYarnPath) ||
+      !is.nonEmptyStringAndNotWhitespace(newYarnPath)
+    ) {
+      return existingYarnrcYmlContent;
+    }
+
     const oldYarnFullPath = upath.join(lockFileDir, oldYarnPath);
     const newYarnFullPath = upath.join(lockFileDir, newYarnPath);
     logger.debug({ oldYarnPath, newYarnPath }, 'Found updated Yarn binary');
diff --git a/lib/modules/manager/npm/post-update/types.ts b/lib/modules/manager/npm/post-update/types.ts
index 7ba05ca902..cf282628da 100644
--- a/lib/modules/manager/npm/post-update/types.ts
+++ b/lib/modules/manager/npm/post-update/types.ts
@@ -32,3 +32,7 @@ export interface GenerateLockFileResult {
 export interface PnpmLockFile {
   lockfileVersion?: number;
 }
+
+export interface YarnRcYmlFile {
+  yarnPath?: string | null;
+}
-- 
GitLab