Skip to content

Commit

Permalink
fix(core): provide a way to reuse cached graph in CI (#29156)
Browse files Browse the repository at this point in the history
<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

<!-- If this is a particularly complex change or feature addition, you
can request a dedicated Nx release for this pull request branch. Mention
someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they
will confirm if the PR warrants its own release for testing purposes,
and generate it for you if appropriate. -->

## Current Behavior
<!-- This is the behavior we have today -->

## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #
  • Loading branch information
AgentEnder authored Dec 3, 2024
1 parent 9ad6b8c commit 6bd8615
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
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

0 comments on commit 6bd8615

Please sign in to comment.