fix(core): correctly dedup projects with different names

This commit is contained in:
Victor Savkin 2022-10-25 13:08:39 -04:00
parent f1eaa97310
commit d2bec05e6c
2 changed files with 28 additions and 5 deletions

View File

@ -79,13 +79,18 @@ describe('Workspaces', () => {
}), }),
'libs/domain/lib4/project.json': JSON.stringify(domainLibConfig), 'libs/domain/lib4/project.json': JSON.stringify(domainLibConfig),
'libs/domain/lib4/package.json': JSON.stringify({}), 'libs/domain/lib4/package.json': JSON.stringify({}),
'workspace.json': JSON.stringify({
projects: { 'lib1-workspace': 'libs/lib1' },
}),
}, },
'/root' '/root'
); );
const workspaces = new Workspaces('/root'); const workspaces = new Workspaces('/root');
const { projects } = workspaces.readWorkspaceConfiguration(); const { projects } = workspaces.readWorkspaceConfiguration();
expect(projects.lib1).toEqual(lib1Config); // projects got deduped so the workspace one remained
expect(projects['lib1-workspace']).toEqual(lib1Config);
expect(projects['lib1']).toBeUndefined();
expect(projects.lib2).toEqual(lib2Config); expect(projects.lib2).toEqual(lib2Config);
expect(projects['domain-lib3']).toEqual(domainPackageConfig); expect(projects['domain-lib3']).toEqual(domainPackageConfig);
expect(projects['domain-lib4']).toEqual(domainLibConfig); expect(projects['domain-lib4']).toEqual(domainLibConfig);

View File

@ -86,10 +86,10 @@ export class Workspaces {
const workspaceFile = workspaceConfigName(this.root); const workspaceFile = workspaceConfigName(this.root);
if (workspaceFile) { if (workspaceFile) {
workspace.projects = { workspace.projects = this.mergeWorkspaceJsonAndGlobProjects(
...workspace.projects, this.readFromWorkspaceJson().projects,
...this.readFromWorkspaceJson().projects, workspace.projects
}; );
} }
assertValidWorkspaceConfiguration(nxJson); assertValidWorkspaceConfiguration(nxJson);
@ -100,6 +100,24 @@ export class Workspaces {
return this.cachedWorkspaceConfig; return this.cachedWorkspaceConfig;
} }
private mergeWorkspaceJsonAndGlobProjects(
workspaceJsonProjects: { [name: string]: any },
globProjects: { [name: string]: any }
) {
const res = workspaceJsonProjects;
const folders = new Set();
for (let k of Object.keys(res)) {
folders.add(res[k].root);
}
for (let k of Object.keys(globProjects)) {
if (!folders.has(globProjects[k].root)) {
res[k] = globProjects[k];
}
}
return res;
}
private mergeTargetDefaultsIntoProjectDescriptions( private mergeTargetDefaultsIntoProjectDescriptions(
config: ProjectsConfigurations, config: ProjectsConfigurations,
nxJson: NxJsonConfiguration nxJson: NxJsonConfiguration