Skip to content

Commit

Permalink
fix(core): repair nx reset
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxKless committed Apr 16, 2024
1 parent 794752c commit bda37f0
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 83 deletions.
3 changes: 2 additions & 1 deletion packages/nx/bin/nx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ function main() {
if (
process.argv[2] !== 'report' &&
process.argv[2] !== '--version' &&
process.argv[2] !== '--help'
process.argv[2] !== '--help' &&
process.argv[2] !== 'reset'
) {
assertSupportedPlatform();
}
Expand Down
17 changes: 17 additions & 0 deletions packages/nx/src/daemon/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,23 @@ export async function safelyCleanUpExistingProcess(): Promise<void> {
if (daemonProcessJson && daemonProcessJson.processId) {
try {
process.kill(daemonProcessJson.processId);
await new Promise<void>((resolve, reject) => {
let count = 0;
const interval = setInterval(() => {
try {
process.kill(daemonProcessJson.processId, 0);
} catch (e) {
clearInterval(interval);
resolve();
}
if ((count += 1) > 200) {
clearInterval(interval);
reject(
`Daemon process ${daemonProcessJson.processId} didn't exit after 2 seconds.`
);
}
}, 10);
});
} catch {}
}
deleteDaemonJsonProcessCache();
Expand Down
2 changes: 1 addition & 1 deletion packages/nx/src/daemon/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { Hash } from '../../hasher/task-hasher';
import { Task, TaskGraph } from '../../config/task-graph';
import { ConfigurationSourceMaps } from '../../project-graph/utils/project-configuration-utils';
import { DaemonProjectGraphError } from '../daemon-project-graph-error';
import { ProjectGraphError } from '../../project-graph/project-graph';
import { ProjectGraphError } from '../../project-graph/project-graph-error';

const DAEMON_ENV_SETTINGS = {
NX_PROJECT_GLOB_CACHE: 'false',
Expand Down
5 changes: 2 additions & 3 deletions packages/nx/src/native/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const { join, basename } = require('path');
const { copyFileSync, existsSync, mkdirSync } = require('fs');
const Module = require('module');
const { cacheDir } = require("../utils/cache-directory.js")
const { nxVersion } = require('../utils/versions.js');
const { nxVersion} = require("../utils/versions")
const { cacheDir} = require("../utils/cache-directory")

const nxPackages = [
'@nx/nx-android-arm64',
Expand Down Expand Up @@ -48,7 +48,6 @@ Module._load = function (request, parent, isMain) {
) {
const nativeLocation = require.resolve(modulePath);
const fileName = basename(nativeLocation)

const tmpFile = join(cacheDir, nxVersion + '-' + fileName);
if (existsSync(tmpFile)) {
return originalLoad.apply(this, [tmpFile, parent, isMain]);
Expand Down
74 changes: 74 additions & 0 deletions packages/nx/src/project-graph/project-graph-error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { DaemonProjectGraphError } from '../daemon/daemon-project-graph-error';
import { ProjectGraph } from '../devkit-exports';
import {
ProcessDependenciesError,
ProcessProjectGraphError,
} from './build-project-graph';
import {
CreateNodesError,
MergeNodesError,
ProjectsWithConflictingNamesError,
ProjectsWithNoNameError,
} from './error-types';
import { ConfigurationSourceMaps } from './utils/project-configuration-utils';

export class ProjectGraphError extends Error {
readonly #errors: Array<
| CreateNodesError
| MergeNodesError
| ProjectsWithNoNameError
| ProjectsWithConflictingNamesError
| ProcessDependenciesError
| ProcessProjectGraphError
>;
readonly #partialProjectGraph: ProjectGraph;
readonly #partialSourceMaps: ConfigurationSourceMaps;

constructor(
errors: Array<
| CreateNodesError
| MergeNodesError
| ProjectsWithNoNameError
| ProjectsWithConflictingNamesError
| ProcessDependenciesError
| ProcessProjectGraphError
>,
partialProjectGraph: ProjectGraph,
partialSourceMaps: ConfigurationSourceMaps
) {
super(`Failed to process project graph.`);
this.name = this.constructor.name;
this.#errors = errors;
this.#partialProjectGraph = partialProjectGraph;
this.#partialSourceMaps = partialSourceMaps;
this.stack = `${this.message}\n ${errors
.map((error) => error.stack.split('\n').join('\n '))
.join('\n')}`;
}

/**
* The daemon cannot throw errors which contain methods as they are not serializable.
*
* This method creates a new {@link ProjectGraphError} from a {@link DaemonProjectGraphError} with the methods based on the same serialized data.
*/
static fromDaemonProjectGraphError(e: DaemonProjectGraphError) {
return new ProjectGraphError(e.errors, e.projectGraph, e.sourceMaps);
}

/**
* This gets the partial project graph despite the errors which occured.
* This partial project graph may be missing nodes, properties of nodes, or dependencies.
* This is useful mostly for visualization/debugging. It should not be used for running tasks.
*/
getPartialProjectGraph() {
return this.#partialProjectGraph;
}

getPartialSourcemaps() {
return this.#partialSourceMaps;
}

getErrors() {
return this.#errors;
}
}
95 changes: 18 additions & 77 deletions packages/nx/src/project-graph/project-graph.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
import {
readFileMapCache,
readProjectGraphCache,
writeCache,
} from './nx-deps-cache';
import {
CreateDependenciesError,
ProcessDependenciesError,
ProcessProjectGraphError,
buildProjectGraphUsingProjectFileMap,
} from './build-project-graph';
import { output } from '../utils/output';
import { markDaemonAsDisabled, writeDaemonLogs } from '../daemon/tmp-dir';
import { performance } from 'perf_hooks';
import { readNxJson } from '../config/nx-json';
import { ProjectGraph } from '../config/project-graph';
import { stripIndents } from '../utils/strip-indents';
import {
ProjectConfiguration,
ProjectsConfigurations,
} from '../config/workspace-json-project-json';
import { daemonClient } from '../daemon/client/client';
import { markDaemonAsDisabled, writeDaemonLogs } from '../daemon/tmp-dir';
import { fileExists } from '../utils/fileutils';
import { output } from '../utils/output';
import { stripIndents } from '../utils/strip-indents';
import { workspaceRoot } from '../utils/workspace-root';
import { performance } from 'perf_hooks';
import {
CreateDependenciesError,
ProcessDependenciesError,
ProcessProjectGraphError,
buildProjectGraphUsingProjectFileMap,
} from './build-project-graph';
import {
readFileMapCache,
readProjectGraphCache,
writeCache,
} from './nx-deps-cache';

import {
retrieveProjectConfigurations,
retrieveWorkspaceFiles,
} from './utils/retrieve-workspace-files';
import { readNxJson } from '../config/nx-json';
import {
ConfigurationResult,
ConfigurationSourceMaps,
Expand All @@ -39,6 +40,7 @@ import {
} from './error-types';
import { DaemonProjectGraphError } from '../daemon/daemon-project-graph-error';
import { loadNxPlugins, LoadedNxPlugin } from './plugins/internal-api';
import { ProjectGraphError } from './project-graph-error';

/**
* Synchronously reads the latest cached copy of the workspace's ProjectGraph.
Expand Down Expand Up @@ -179,67 +181,6 @@ export async function buildProjectGraphAndSourceMapsWithoutDaemon() {
}
}

export class ProjectGraphError extends Error {
readonly #errors: Array<
| CreateNodesError
| MergeNodesError
| ProjectsWithNoNameError
| ProjectsWithConflictingNamesError
| ProcessDependenciesError
| ProcessProjectGraphError
>;
readonly #partialProjectGraph: ProjectGraph;
readonly #partialSourceMaps: ConfigurationSourceMaps;

constructor(
errors: Array<
| CreateNodesError
| MergeNodesError
| ProjectsWithNoNameError
| ProjectsWithConflictingNamesError
| ProcessDependenciesError
| ProcessProjectGraphError
>,
partialProjectGraph: ProjectGraph,
partialSourceMaps: ConfigurationSourceMaps
) {
super(`Failed to process project graph.`);
this.name = this.constructor.name;
this.#errors = errors;
this.#partialProjectGraph = partialProjectGraph;
this.#partialSourceMaps = partialSourceMaps;
this.stack = `${this.message}\n ${errors
.map((error) => error.stack.split('\n').join('\n '))
.join('\n')}`;
}

/**
* The daemon cannot throw errors which contain methods as they are not serializable.
*
* This method creates a new {@link ProjectGraphError} from a {@link DaemonProjectGraphError} with the methods based on the same serialized data.
*/
static fromDaemonProjectGraphError(e: DaemonProjectGraphError) {
return new ProjectGraphError(e.errors, e.projectGraph, e.sourceMaps);
}

/**
* This gets the partial project graph despite the errors which occured.
* This partial project graph may be missing nodes, properties of nodes, or dependencies.
* This is useful mostly for visualization/debugging. It should not be used for running tasks.
*/
getPartialProjectGraph() {
return this.#partialProjectGraph;
}

getPartialSourcemaps() {
return this.#partialSourceMaps;
}

getErrors() {
return this.#errors;
}
}

function handleProjectGraphError(opts: { exitOnError: boolean }, e) {
if (opts.exitOnError) {
const isVerbose = process.env.NX_VERBOSE_LOGGING === 'true';
Expand Down
2 changes: 1 addition & 1 deletion packages/nx/src/utils/params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type {
ProjectsConfigurations,
} from '../config/workspace-json-project-json';
import { output } from './output';
import type { ProjectGraphError } from '../project-graph/project-graph';
import type { ProjectGraphError } from '../project-graph/project-graph-error';

const LIST_CHOICE_DISPLAY_LIMIT = 10;

Expand Down

0 comments on commit bda37f0

Please sign in to comment.