From 16b3862821ee52814580aa9e184da352c3b89030 Mon Sep 17 00:00:00 2001
From: Rhys Arkins <rhys@arkins.net>
Date: Fri, 10 Nov 2023 20:29:10 +0100
Subject: [PATCH] fix(npm): better detect workspace packages (#25692)

---
 .../manager/npm/post-update/npm.spec.ts       | 11 ++++++++
 lib/modules/manager/npm/post-update/npm.ts    | 26 +++++++++++++------
 2 files changed, 29 insertions(+), 8 deletions(-)

diff --git a/lib/modules/manager/npm/post-update/npm.spec.ts b/lib/modules/manager/npm/post-update/npm.spec.ts
index 170ca700c5..3637ecbd08 100644
--- a/lib/modules/manager/npm/post-update/npm.spec.ts
+++ b/lib/modules/manager/npm/post-update/npm.spec.ts
@@ -459,6 +459,17 @@ describe('modules/manager/npm/post-update/npm', () => {
           workspacesPackages: ['docs/*', 'web/*'],
         },
       },
+      {
+        packageFile: 'some-missing-dir/docs/a/package.json',
+        packageName: 'hello',
+        depType: 'dependencies',
+        newVersion: '1.1.1',
+        newValue: '^1.0.0',
+        isLockfileUpdate: true,
+        managerData: {
+          workspacesPackages: ['docs/*', 'web/*'],
+        },
+      },
     ];
 
     it('workspace in sub-folder', async () => {
diff --git a/lib/modules/manager/npm/post-update/npm.ts b/lib/modules/manager/npm/post-update/npm.ts
index 3540350264..4189c64479 100644
--- a/lib/modules/manager/npm/post-update/npm.ts
+++ b/lib/modules/manager/npm/post-update/npm.ts
@@ -265,18 +265,28 @@ export function divideWorkspaceAndRootDeps(
         // stop when the first match is found and
         // add workspaceDir to workspaces set and upgrade object
         for (const workspacePattern of workspacePatterns) {
-          if (minimatch(workspacePattern).match(workspaceDir)) {
+          const massagedPattern = (workspacePattern as string).replace(
+            /^\.\//,
+            '',
+          );
+          if (minimatch(massagedPattern).match(workspaceDir)) {
             workspaceName = workspaceDir;
             break;
           }
         }
-        if (
-          workspaceName &&
-          !rootDeps.has(upgrade.managerData.packageKey) // prevent same dep from existing in root and workspace
-        ) {
-          workspaces.add(workspaceName);
-          upgrade.workspace = workspaceName;
-          lockWorkspacesUpdates.push(upgrade);
+        if (workspaceName) {
+          if (
+            !rootDeps.has(upgrade.managerData.packageKey) // prevent same dep from existing in root and workspace
+          ) {
+            workspaces.add(workspaceName);
+            upgrade.workspace = workspaceName;
+            lockWorkspacesUpdates.push(upgrade);
+          }
+        } else {
+          logger.warn(
+            { workspacePatterns, workspaceDir },
+            'workspaceDir not found',
+          );
         }
         continue;
       }
-- 
GitLab