Skip to content

Commit

Permalink
fix(core): move a few api points to return root maps directly (#22949)
Browse files Browse the repository at this point in the history
(cherry picked from commit 355d5c5)
  • Loading branch information
AgentEnder authored and FrozenPandaz committed Apr 25, 2024
1 parent 0b03198 commit ccddf0e
Show file tree
Hide file tree
Showing 17 changed files with 196 additions and 98 deletions.
36 changes: 33 additions & 3 deletions packages/devkit/src/utils/add-plugin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { addPlugin, generateCombinations } from './add-plugin';

describe('addPlugin', () => {
let tree: Tree;
let createNodes: CreateNodes;
let createNodes: CreateNodes<{ targetName: string }>;
let graph: ProjectGraph;
let fs: TempFs;

Expand All @@ -30,9 +30,27 @@ describe('addPlugin', () => {
targets: {},
},
},
app2: {
name: 'app2',
type: 'app',
data: {
root: 'app2',
targets: {},
},
},
app3: {
name: 'app3',
type: 'app',
data: {
root: 'app3',
targets: {},
},
},
},
dependencies: {
app1: [],
app2: [],
app3: [],
},
};
createNodes = [
Expand All @@ -45,12 +63,19 @@ describe('addPlugin', () => {
[targetName]: { command: 'next build' },
},
},
app2: {
name: 'app2',
targets: {
[targetName]: { command: 'next build' },
},
},
},
}),
];

await fs.createFiles({
'app1/next.config.js': '',
'app2/next.config.js': '',
});
});

Expand All @@ -61,6 +86,10 @@ describe('addPlugin', () => {
describe('adding the plugin', () => {
it('should not conflicting with the existing graph', async () => {
graph.nodes.app1.data.targets.build = {};
graph.nodes.app2.data.targets.build1 = {};
// app 3 doesn't have a next config, so it having this
// target should not affect the plugin options
graph.nodes.app3.data.targets.build2 = {};

await addPlugin(
tree,
Expand All @@ -69,15 +98,15 @@ describe('addPlugin', () => {
createNodes,

{
targetName: ['build', '_build'],
targetName: ['build', 'build1', 'build2'],
},
true
);

expect(readJson(tree, 'nx.json').plugins).toContainEqual({
plugin: '@nx/next/plugin',
options: {
targetName: '_build',
targetName: 'build2',
},
});
});
Expand Down Expand Up @@ -372,6 +401,7 @@ describe('addPlugin', () => {
});
});
});

describe('generateCombinations', () => {
it('should return all combinations for a 2x2 array of strings', () => {
const input = {
Expand Down
22 changes: 21 additions & 1 deletion packages/devkit/src/utils/convert-nx-executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ import type { Executor, ExecutorContext } from 'nx/src/config/misc-interfaces';
import type { ProjectsConfigurations } from 'nx/src/devkit-exports';

import { requireNx } from '../../nx';
import { NX_VERSION } from './package-json';
import { lt } from 'semver';

const {
Workspaces,
readNxJsonFromDisk,
retrieveProjectConfigurationsWithAngularProjects,
readProjectConfigurationsFromRootMap,
} = requireNx();

/**
Expand All @@ -32,7 +35,24 @@ export function convertNxExecutor(executor: Executor) {
projects: await retrieveProjectConfigurationsWithAngularProjects(
builderContext.workspaceRoot,
nxJsonConfiguration
).then((p) => (p as any).projectNodes ?? p.projects),
).then((p) => {
if ((p as any).projectNodes) {
return (p as any).projectNodes;
}
// v18.3.4 changed projects to be keyed by root
// rather than project name
if (lt(NX_VERSION, '18.3.4')) {
return p.projects;
}

if (readProjectConfigurationsFromRootMap) {
return readProjectConfigurationsFromRootMap(p.projects);
}

throw new Error(
'Unable to successfully map Nx executor -> Angular Builder'
);
}),
}
: // TODO(v19): remove retrieveProjectConfigurations. This is to be backwards compatible with Nx 16.5 and below.
(workspaces as any).readProjectsConfigurations({
Expand Down
2 changes: 1 addition & 1 deletion packages/nx/src/config/workspaces.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ describe('Workspaces', () => {
}
);
console.log(projects);
expect(projects['my-package']).toEqual({
expect(projects['packages/my-package']).toEqual({
name: 'my-package',
root: 'packages/my-package',
sourceRoot: 'packages/my-package',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,29 +234,29 @@ async function processFilesAndCreateAndSerializeProjectGraph(
const nxJson = readNxJson(workspaceRoot);
global.NX_GRAPH_CREATION = true;

let graphNodes: ConfigurationResult;
let projectConfigurationsResult: ConfigurationResult;
let projectConfigurationsError;

try {
graphNodes = await retrieveProjectConfigurations(
projectConfigurationsResult = await retrieveProjectConfigurations(
plugins,
workspaceRoot,
nxJson
);
} catch (e) {
if (e instanceof ProjectConfigurationsError) {
graphNodes = e.partialProjectConfigurationsResult;
projectConfigurationsResult = e.partialProjectConfigurationsResult;
projectConfigurationsError = e;
} else {
throw e;
}
}
await processCollectedUpdatedAndDeletedFiles(
graphNodes,
projectConfigurationsResult,
updatedFileHashes,
deletedFiles
);
const g = await createAndSerializeProjectGraph(graphNodes);
const g = await createAndSerializeProjectGraph(projectConfigurationsResult);

delete global.NX_GRAPH_CREATION;

Expand Down Expand Up @@ -284,7 +284,7 @@ async function processFilesAndCreateAndSerializeProjectGraph(
error: new DaemonProjectGraphError(
errors,
g.projectGraph,
graphNodes.sourceMaps
projectConfigurationsResult.sourceMaps
),
projectGraph: null,
projectFileMapCache: null,
Expand Down
1 change: 1 addition & 0 deletions packages/nx/src/devkit-internals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export { getExecutorInformation } from './command-line/run/executor-utils';
export { readNxJson as readNxJsonFromDisk } from './config/nx-json';
export { calculateDefaultProjectName } from './config/calculate-default-project-name';
export { retrieveProjectConfigurationsWithAngularProjects } from './project-graph/utils/retrieve-workspace-files';
export { readProjectConfigurationsFromRootMap } from './project-graph/utils/project-configuration-utils';
export { splitTarget } from './utils/split-target';
export { combineOptionsForExecutor } from './utils/params';
export { sortObjectByKeys } from './utils/object-sort';
Expand Down
17 changes: 10 additions & 7 deletions packages/nx/src/executors/utils/convert-nx-executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { Observable } from 'rxjs';
import { readNxJson } from '../../config/nx-json';
import { Executor, ExecutorContext } from '../../config/misc-interfaces';
import { retrieveProjectConfigurations } from '../../project-graph/utils/retrieve-workspace-files';
import { readProjectConfigurationsFromRootMap } from '../../project-graph/utils/project-configuration-utils';
import { ProjectsConfigurations } from '../../config/workspace-json-project-json';
import { loadNxPlugins } from '../../project-graph/plugins/internal-api';

Expand All @@ -25,13 +26,15 @@ export function convertNxExecutor(executor: Executor) {
);
const projectsConfigurations: ProjectsConfigurations = {
version: 2,
projects: (
await retrieveProjectConfigurations(
plugins,
builderContext.workspaceRoot,
nxJsonConfiguration
)
).projects,
projects: readProjectConfigurationsFromRootMap(
(
await retrieveProjectConfigurations(
plugins,
builderContext.workspaceRoot,
nxJsonConfiguration
)
).projects
),
};
cleanup();
const context: ExecutorContext = {
Expand Down
4 changes: 2 additions & 2 deletions packages/nx/src/generators/utils/project-configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ function readAndCombineAllProjectConfigurations(tree: Tree): {
(r) => deletedFiles.indexOf(r) === -1
);

const rootMap: Map<string, ProjectConfiguration> = new Map();
const rootMap: Record<string, ProjectConfiguration> = {};
for (const projectFile of projectFiles) {
if (basename(projectFile) === 'project.json') {
const json = readJson(tree, projectFile);
Expand All @@ -230,7 +230,7 @@ function readAndCombineAllProjectConfigurations(tree: Tree): {
projectFile,
readNxJson(tree)
);
if (!rootMap.has(config.root)) {
if (!rootMap[config.root]) {
mergeProjectConfigurationIntoRootMap(
rootMap,
// Inferred targets, tags, etc don't show up when running generators
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,19 @@ describe('explicit package json dependencies', () => {
projects: {
proj: {
root: 'libs/proj',
name: 'proj',
},
proj2: {
root: 'libs/proj2',
name: 'proj2',
},
proj3: {
root: 'libs/proj3',
name: 'proj3',
},
proj4: {
root: 'libs/proj4',
name: 'proj4',
},
},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,9 @@ async function createContext(

return {
externalNodes: builder.getUpdatedProjectGraph().externalNodes,
projects: projects,
projects: Object.fromEntries(
Object.entries(projects).map(([root, config]) => [config.name, config])
),
nxJsonConfiguration: nxJson,
filesToProcess: fileMap,
fileMap: fileMap,
Expand Down
16 changes: 8 additions & 8 deletions packages/nx/src/project-graph/build-project-graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export function getFileMap(): {
}

export async function buildProjectGraphUsingProjectFileMap(
projects: Record<string, ProjectConfiguration>,
projectRootMap: Record<string, ProjectConfiguration>,
externalNodes: Record<string, ProjectGraphExternalNode>,
fileMap: FileMap,
allWorkspaceFiles: FileData[],
Expand All @@ -75,6 +75,12 @@ export async function buildProjectGraphUsingProjectFileMap(
storedAllWorkspaceFiles = allWorkspaceFiles;
storedRustReferences = rustReferences;

const projects: Record<string, ProjectConfiguration> = {};
for (const root in projectRootMap) {
const project = projectRootMap[root];
projects[project.name] = project;
}

const nxJson = readNxJson();
const projectGraphVersion = '6.0';
assertWorkspaceValidity(projects, nxJson);
Expand Down Expand Up @@ -233,15 +239,9 @@ function createContext(
fileMap: FileMap,
filesToProcess: FileMap
): CreateDependenciesContext {
const clonedProjects = Object.keys(projects).reduce((map, projectName) => {
map[projectName] = {
...projects[projectName],
};
return map;
}, {} as Record<string, ProjectConfiguration>);
return {
nxJsonConfiguration: nxJson,
projects: clonedProjects,
projects,
externalNodes,
workspaceRoot,
fileMap,
Expand Down
3 changes: 3 additions & 0 deletions packages/nx/src/project-graph/file-map-utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,19 @@ describe('fileMapUtils', () => {
version: 2,
projects: {
demo: {
name: 'demo',
root: 'apps/demo',
sourceRoot: 'apps/demo/src',
projectType: 'application' as ProjectType,
},
'demo-e2e': {
name: 'demo-e2e',
root: 'apps/demo-e2e',
sourceRoot: 'apps/demo-e2e/src',
projectType: 'application' as ProjectType,
},
ui: {
name: 'ui',
root: 'libs/ui',
sourceRoot: 'libs/ui/src',
projectType: 'library' as ProjectType,
Expand Down
2 changes: 1 addition & 1 deletion packages/nx/src/project-graph/file-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ function getProjectsSyncNoInference(root: string, nxJson: NxJsonConfiguration) {
...getDefaultPluginsSync(root),
];

const projectRootMap: Map<string, ProjectConfiguration> = new Map();
const projectRootMap: Record<string, ProjectConfiguration> = {};

// We iterate over plugins first - this ensures that plugins specified first take precedence.
for (const plugin of plugins) {
Expand Down
19 changes: 12 additions & 7 deletions packages/nx/src/project-graph/plugins/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
registerTsConfigPaths,
} from '../../plugins/js/utils/register';
import {
ProjectRootMappings,
createProjectRootMappingsFromProjectConfigurations,
findProjectForPath,
} from '../utils/find-project-for-path';
Expand Down Expand Up @@ -111,31 +112,35 @@ function lookupLocalPlugin(
projects: Record<string, ProjectConfiguration>,
root = workspaceRoot
) {
const plugin = findNxProjectForImportPath(importPath, projects, root);
if (!plugin) {
const projectConfig = findNxProjectForImportPath(importPath, projects, root);
if (!projectConfig) {
return null;
}

const projectConfig: ProjectConfiguration = projects[plugin];
return { path: path.join(root, projectConfig.root), projectConfig };
}

function findNxProjectForImportPath(
importPath: string,
projects: Record<string, ProjectConfiguration>,
root = workspaceRoot
): string | null {
): ProjectConfiguration | null {
const tsConfigPaths: Record<string, string[]> = readTsConfigPaths(root);
const possiblePaths = tsConfigPaths[importPath]?.map((p) =>
normalizePath(path.relative(root, path.join(root, p)))
);
if (possiblePaths?.length) {
const projectRootMappings =
createProjectRootMappingsFromProjectConfigurations(projects);
const projectRootMappings: ProjectRootMappings = new Map();
const projectNameMap = new Map<string, ProjectConfiguration>();
for (const projectRoot in projects) {
const project = projects[projectRoot];
projectRootMappings.set(project.root, project.name);
projectNameMap.set(project.name, project);
}
for (const tsConfigPath of possiblePaths) {
const nxProject = findProjectForPath(tsConfigPath, projectRootMappings);
if (nxProject) {
return nxProject;
return projectNameMap.get(nxProject);
}
}
logger.verbose(
Expand Down
Loading

0 comments on commit ccddf0e

Please sign in to comment.