Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): provide a way to reuse cached graph in CI #29156

Merged
merged 4 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,20 @@ import { getPlugins } from '../../plugins/get-plugins';

export const getTouchedProjectsFromProjectGlobChanges: TouchedProjectLocator =
async (touchedFiles, projectGraphNodes): Promise<string[]> => {
const plugins = await getPlugins();
const globPattern = combineGlobPatterns(configurationGlobs(plugins));
const globPattern = await (async () => {
// TODO: We need a quicker way to get patterns that should not
// require starting up plugin workers
if (process.env.NX_FORCE_REUSE_CACHED_GRAPH === 'true') {
return combineGlobPatterns([
'**/package.json',
'**/project.json',
'project.json',
'package.json',
]);
}
const plugins = await getPlugins();
return combineGlobPatterns(configurationGlobs(plugins));
})();

const touchedProjects = new Set<string>();
for (const touchedFile of touchedFiles) {
Expand Down
10 changes: 10 additions & 0 deletions packages/nx/src/project-graph/build-project-graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@ export function getFileMap(): {
}
}

export function hydrateFileMap(
fileMap: FileMap,
allWorkspaceFiles: FileData[],
rustReferences: NxWorkspaceFilesExternals
) {
storedFileMap = fileMap;
storedAllWorkspaceFiles = allWorkspaceFiles;
storedRustReferences = rustReferences;
}

export async function buildProjectGraphUsingProjectFileMap(
projectRootMap: Record<string, ProjectConfiguration>,
externalNodes: Record<string, ProjectGraphExternalNode>,
Expand Down
25 changes: 24 additions & 1 deletion packages/nx/src/project-graph/project-graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ import { fileExists } from '../utils/fileutils';
import { output } from '../utils/output';
import { stripIndents } from '../utils/strip-indents';
import { workspaceRoot } from '../utils/workspace-root';
import { buildProjectGraphUsingProjectFileMap } from './build-project-graph';
import {
buildProjectGraphUsingProjectFileMap,
hydrateFileMap,
} from './build-project-graph';
import {
AggregateProjectGraphError,
isAggregateProjectGraphError,
Expand All @@ -30,6 +33,7 @@ import {
retrieveWorkspaceFiles,
} from './utils/retrieve-workspace-files';
import { getPlugins } from './plugins/get-plugins';
import { logger } from '../utils/logger';

/**
* Synchronously reads the latest cached copy of the workspace's ProjectGraph.
Expand Down Expand Up @@ -229,6 +233,25 @@ export async function createProjectGraphAsync(
resetDaemonClient: false,
}
): Promise<ProjectGraph> {
if (process.env.NX_FORCE_REUSE_CACHED_GRAPH === 'true') {
try {
const graph = readCachedProjectGraph();
const projectRootMap = Object.fromEntries(
Object.entries(graph.nodes).map(([project, { data }]) => [
data.root,
project,
])
);
const { allWorkspaceFiles, fileMap, rustReferences } =
await retrieveWorkspaceFiles(workspaceRoot, projectRootMap);
hydrateFileMap(fileMap, allWorkspaceFiles, rustReferences);
return graph;
// If no cached graph is found, we will fall through to the normal flow
} catch (e) {
logger.verbose('Unable to use cached project graph', e);
}
}

const projectGraphAndSourceMaps = await createProjectGraphAndSourceMapsAsync(
opts
);
Expand Down
Loading