Skip to content

Commit

Permalink
fix(core): fix incremental graph updates
Browse files Browse the repository at this point in the history
  • Loading branch information
vsavkin committed May 26, 2023
1 parent ac41730 commit 5389d17
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -228,10 +228,6 @@ function copyFileMap(m: ProjectFileMap) {
return c;
}

function copyProjectGraph(p: ProjectGraph | null): ProjectGraph | null {
return p ? { ...p } : null;
}

async function createAndSerializeProjectGraph(): Promise<{
error: string | null;
projectGraph: ProjectGraph | null;
Expand All @@ -253,12 +249,7 @@ async function createAndSerializeProjectGraph(): Promise<{
projectsConfigurations,
projectFileMap,
allWorkspaceFiles,
{
fileMap: currentProjectFileMapCache || readProjectFileMapCache(),
projectGraph: copyProjectGraph(
currentProjectGraph || readProjectGraphCache()
),
},
currentProjectFileMapCache || readProjectFileMapCache(),
true
);
currentProjectFileMapCache = projectFileMapCache;
Expand Down
26 changes: 18 additions & 8 deletions packages/nx/src/plugins/js/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { ProjectGraphProcessor } from '../../config/project-graph';
import {
ProjectGraph,
ProjectGraphProcessor,
} from '../../config/project-graph';
import { ProjectGraphBuilder } from '../../project-graph/project-graph-builder';
import { buildNpmPackageNodes } from './project-graph/build-nodes/build-npm-package-nodes';
import { buildExplicitDependencies } from './project-graph/build-dependencies/build-dependencies';
import { readNxJson } from '../../config/configuration';
import { fileExists, readJsonFile } from '../../utils/fileutils';
Expand All @@ -16,7 +18,6 @@ import { projectGraphCacheDirectory } from '../../utils/cache-directory';
import { readFileSync, writeFileSync } from 'fs';
import { workspaceRoot } from '../../utils/workspace-root';
import { ensureDirSync } from 'fs-extra';
import { removeNpmNodes } from './lock-file/remove-npm-nodes';

export const processProjectGraph: ProjectGraphProcessor = async (
graph,
Expand All @@ -29,12 +30,14 @@ export const processProjectGraph: ProjectGraphProcessor = async (
// during the create-nx-workspace lock file might not exists yet
if (lockFileExists()) {
const lockHash = lockFileHash();
let parsedLockFile: ProjectGraph;
if (lockFileNeedsReprocessing(lockHash)) {
removeNpmNodes(graph, builder);
if (!!parseLockFile(builder)) {
writeLastProcessedLockfileHash(lockHash);
}
parsedLockFile = parseLockFile();
writeLastProcessedLockfileHash(lockHash, parsedLockFile);
} else {
parsedLockFile = readParsedLockFile();
}
builder.mergeProjectGraph(parsedLockFile);
}
}

Expand All @@ -44,6 +47,8 @@ export const processProjectGraph: ProjectGraphProcessor = async (
};

const lockFileHashFile = join(projectGraphCacheDirectory, 'lockfile.hash');
const parsedLockFile = join(projectGraphCacheDirectory, 'parsed-lockfile.json');

function lockFileNeedsReprocessing(lockHash: string) {
try {
return readFileSync(lockFileHashFile).toString() !== lockHash;
Expand All @@ -52,9 +57,14 @@ function lockFileNeedsReprocessing(lockHash: string) {
}
}

function writeLastProcessedLockfileHash(hash: string) {
function writeLastProcessedLockfileHash(hash: string, lockFile: ProjectGraph) {
ensureDirSync(dirname(lockFileHashFile));
writeFileSync(lockFileHashFile, hash);
writeFileSync(parsedLockFile, JSON.stringify(lockFile, null, 2));
}

function readParsedLockFile(): ProjectGraph {
return JSON.parse(readFileSync(parsedLockFile).toString());
}

function jsPluginConfig(
Expand Down
2 changes: 1 addition & 1 deletion packages/nx/src/plugins/js/lock-file/lock-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ export function lockFileHash(
* Parses lock file and maps dependencies and metadata to {@link LockFileGraph}
*/
export function parseLockFile(
builder: ProjectGraphBuilder,
packageManager: PackageManager = detectPackageManager(workspaceRoot)
): ProjectGraph {
const builder = new ProjectGraphBuilder(null, null);
try {
if (packageManager === 'yarn') {
const content = readFileSync(YARN_LOCK_PATH, 'utf8');
Expand Down
11 changes: 0 additions & 11 deletions packages/nx/src/plugins/js/lock-file/remove-npm-nodes.ts

This file was deleted.

12 changes: 5 additions & 7 deletions packages/nx/src/project-graph/build-project-graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export async function buildProjectGraphUsingProjectFileMap(
projectsConfigurations: ProjectsConfigurations,
projectFileMap: ProjectFileMap,
allWorkspaceFiles: FileData[],
cache: { fileMap: ProjectFileMapCache; projectGraph: ProjectGraph } | null,
fileMap: ProjectFileMapCache | null,
shouldWriteCache: boolean
): Promise<{
projectGraph: ProjectGraph;
Expand All @@ -69,16 +69,16 @@ export async function buildProjectGraphUsingProjectFileMap(
let filesToProcess;
let cachedFileData;
const useCacheData =
cache?.fileMap &&
fileMap &&
!shouldRecomputeWholeGraph(
cache.fileMap,
fileMap,
packageJsonDeps,
projectsConfigurations,
nxJson,
rootTsConfig
);
if (useCacheData) {
const fromCache = extractCachedFileData(projectFileMap, cache.fileMap);
const fromCache = extractCachedFileData(projectFileMap, fileMap);
filesToProcess = fromCache.filesToProcess;
cachedFileData = fromCache.cachedFileData;
} else {
Expand All @@ -96,7 +96,6 @@ export async function buildProjectGraphUsingProjectFileMap(
nxJson,
context,
cachedFileData,
cache?.projectGraph,
projectGraphVersion
);
const projectFileMapCache = createProjectFileMapCache(
Expand Down Expand Up @@ -142,12 +141,11 @@ async function buildProjectGraphUsingContext(
nxJson: NxJsonConfiguration,
ctx: ProjectGraphProcessorContext,
cachedFileData: { [project: string]: { [file: string]: FileData } },
cachedProjectGraph: ProjectGraph,
projectGraphVersion: string
) {
performance.mark('build project graph:start');

const builder = new ProjectGraphBuilder(cachedProjectGraph, ctx.fileMap);
const builder = new ProjectGraphBuilder(null, ctx.fileMap);
builder.setVersion(projectGraphVersion);

await buildWorkspaceProjectNodes(ctx, builder, nxJson);
Expand Down
8 changes: 8 additions & 0 deletions packages/nx/src/project-graph/project-graph-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ export class ProjectGraphBuilder {
}
}

mergeProjectGraph(p: ProjectGraph) {
this.graph.nodes = { ...this.graph.nodes, ...p.nodes };
this.graph.externalNodes = {
...this.graph.externalNodes,
...p.externalNodes,
};
this.graph.dependencies = { ...this.graph.dependencies, ...p.dependencies };
}
/**
* Adds a project node to the project graph
*/
Expand Down
7 changes: 1 addition & 6 deletions packages/nx/src/project-graph/project-graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,7 @@ export async function buildProjectGraphWithoutDaemon() {
projectConfigurations,
projectFileMap,
allWorkspaceFiles,
cacheEnabled
? {
fileMap: readProjectFileMapCache(),
projectGraph: readProjectGraphCache(),
}
: null,
cacheEnabled ? readProjectFileMapCache() : null,
cacheEnabled
)
).projectGraph;
Expand Down

0 comments on commit 5389d17

Please sign in to comment.