From 515bfdaba5894ac57ed7e5ae6deb92e5824af6cc Mon Sep 17 00:00:00 2001
From: Jason Jean <jasonjean1993@gmail.com>
Date: Fri, 30 Jun 2023 16:04:18 -0400
Subject: [PATCH] fix(core): fix project file map for root projects (#17894)

(cherry picked from commit 15904616dffee8620dc1f30955da821cd5e49c2f)
---
 .../src/native/tests/workspace_files.spec.ts  | 50 +++++++++++++++++++
 .../workspace/get_nx_workspace_files.rs       | 16 +++---
 2 files changed, 56 insertions(+), 10 deletions(-)

diff --git a/packages/nx/src/native/tests/workspace_files.spec.ts b/packages/nx/src/native/tests/workspace_files.spec.ts
index de31dd3dc697c..50f8cf3c1565e 100644
--- a/packages/nx/src/native/tests/workspace_files.spec.ts
+++ b/packages/nx/src/native/tests/workspace_files.spec.ts
@@ -128,6 +128,56 @@ describe('workspace files', () => {
       ]
     `);
   });
+
+  it('should assign files to the root project if it exists', async () => {
+    const fs = new TempFs('workspace-files');
+    const nxJson: NxJsonConfiguration = {};
+    await fs.createFiles({
+      './nx.json': JSON.stringify(nxJson),
+      './package.json': JSON.stringify({
+        name: 'repo-name',
+        version: '0.0.0',
+        dependencies: {},
+      }),
+      './project.json': JSON.stringify({
+        name: 'repo-name',
+      }),
+      './src/index.js': '',
+      './jest.config.js': '',
+    });
+    const globs = ['project.json', '**/project.json', '**/package.json'];
+    const { globalFiles, projectFileMap } = getWorkspaceFilesNative(
+      fs.tempDir,
+      globs
+    );
+
+    expect(globalFiles).toEqual([]);
+    expect(projectFileMap['repo-name']).toMatchInlineSnapshot(`
+      [
+        {
+          "file": "jest.config.js",
+          "hash": "3244421341483603138",
+        },
+        {
+          "file": "nx.json",
+          "hash": "1389868326933519382",
+        },
+        {
+          "file": "package.json",
+          "hash": "14409636362330144230",
+        },
+        {
+          "file": "project.json",
+          "hash": "4357927788053707201",
+        },
+        {
+          "file": "src/index.js",
+          "hash": "3244421341483603138",
+        },
+      ]
+    `);
+  });
+
   it('should dedupe configuration files', async () => {
     const fs = new TempFs('workspace-files');
     const nxJson: NxJsonConfiguration = {};
diff --git a/packages/nx/src/native/workspace/get_nx_workspace_files.rs b/packages/nx/src/native/workspace/get_nx_workspace_files.rs
index e903474b27793..706a37e438ff5 100644
--- a/packages/nx/src/native/workspace/get_nx_workspace_files.rs
+++ b/packages/nx/src/native/workspace/get_nx_workspace_files.rs
@@ -46,20 +46,16 @@ pub fn get_workspace_files_native(
         .into_par_iter()
         .map(|file_data| {
             let file_path = Path::new(&file_data.file);
-            trace!(?file_path);
             let mut parent = file_path.parent().unwrap_or_else(|| Path::new(""));
-            trace!(?parent);
-            while root_map.get(parent).is_none() {
-                parent = parent.parent().unwrap_or_else(|| Path::new(""));
 
-                if parent == Path::new("") {
-                    return (FileLocation::Global, file_data);
-                }
+            while root_map.get(parent).is_none() && parent != Path::new("") {
+                parent = parent.parent().unwrap_or_else(|| Path::new(""));
             }
 
-            let project_name = root_map.get(parent).unwrap();
-
-            (FileLocation::Project(project_name.clone()), file_data)
+            match root_map.get(parent) {
+                Some(project_name) => (FileLocation::Project(project_name.clone()), file_data),
+                None => (FileLocation::Global, file_data),
+            }
         })
         .collect::<Vec<(FileLocation, FileData)>>();