Skip to content

Commit

Permalink
fix(core): fix project file map for root projects (#17894)
Browse files Browse the repository at this point in the history
(cherry picked from commit 1590461)
  • Loading branch information
FrozenPandaz committed Jun 30, 2023
1 parent f3fc658 commit 515bfda
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 10 deletions.
50 changes: 50 additions & 0 deletions packages/nx/src/native/tests/workspace_files.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};
Expand Down
16 changes: 6 additions & 10 deletions packages/nx/src/native/workspace/get_nx_workspace_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)>>();

Expand Down

0 comments on commit 515bfda

Please sign in to comment.