Skip to content

Commit

Permalink
Revert "fix(core): fix misc issues (#29114)"
Browse files Browse the repository at this point in the history
This reverts commit 9ad6b8c.
  • Loading branch information
AgentEnder committed Dec 3, 2024
1 parent b95776e commit 9a9aa86
Show file tree
Hide file tree
Showing 29 changed files with 165 additions and 265 deletions.
8 changes: 3 additions & 5 deletions packages/nx/bin/post-install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@ import { setupWorkspaceContext } from '../src/utils/workspace-context';
if (isMainNxPackage() && fileExists(join(workspaceRoot, 'nx.json'))) {
assertSupportedPlatform();
setupWorkspaceContext(workspaceRoot);
if (daemonClient.enabled()) {
try {
await daemonClient.stop();
} catch (e) {}
}
try {
await daemonClient.stop();
} catch (e) {}
const tasks: Array<Promise<any>> = [
buildProjectGraphAndSourceMapsWithoutDaemon(),
];
Expand Down
43 changes: 3 additions & 40 deletions packages/nx/plugins/package-json.ts
Original file line number Diff line number Diff line change
@@ -1,50 +1,13 @@
import { createNodesFromFiles, NxPluginV2 } from '../src/project-graph/plugins';
import type { NxPluginV2 } from '../src/project-graph/plugins';
import { workspaceRoot } from '../src/utils/workspace-root';
import { createNodeFromPackageJson } from '../src/plugins/package-json';
import { workspaceDataDirectory } from '../src/utils/cache-directory';
import { join } from 'path';
import { ProjectConfiguration } from '../src/config/workspace-json-project-json';
import { readJsonFile, writeJsonFile } from '../src/utils/fileutils';

export type PackageJsonConfigurationCache = {
[hash: string]: ProjectConfiguration;
};

const cachePath = join(workspaceDataDirectory, 'package-json.hash');

export function readPackageJsonConfigurationCache() {
try {
return readJsonFile<PackageJsonConfigurationCache>(cachePath);
} catch (e) {
return {};
}
}

function writeCache(cache: PackageJsonConfigurationCache) {
writeJsonFile(cachePath, cache);
}

const plugin: NxPluginV2 = {
name: 'nx-all-package-jsons-plugin',
createNodesV2: [
createNodes: [
'*/**/package.json',
(configFiles, options, context) => {
const cache = readPackageJsonConfigurationCache();

const result = createNodesFromFiles(
(f) => createNodeFromPackageJson(f, workspaceRoot, cache),
configFiles,
options,
context
);

writeCache(cache);

return result;
},
(f) => createNodeFromPackageJson(f, workspaceRoot),
],
};

module.exports = plugin;
module.exports.readPackageJsonConfigurationCache =
readPackageJsonConfigurationCache;
4 changes: 1 addition & 3 deletions packages/nx/src/command-line/affected/affected.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@ export async function affected(

await connectToNxCloudIfExplicitlyAsked(nxArgs);

const projectGraph = await createProjectGraphAsync({
exitOnError: true,
});
const projectGraph = await createProjectGraphAsync({ exitOnError: true });
const projects = await getAffectedGraphNodes(nxArgs, projectGraph);

try {
Expand Down
11 changes: 4 additions & 7 deletions packages/nx/src/command-line/format/format.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { exec, execSync } from 'node:child_process';
import * as path from 'node:path';
import * as yargs from 'yargs';
import { calculateFileChanges, FileData } from '../../project-graph/file-utils';
import { FileData, calculateFileChanges } from '../../project-graph/file-utils';
import {
getProjectRoots,
NxArgs,
getProjectRoots,
parseFiles,
splitArgsIntoNxArgsAndOverrides,
} from '../../utils/command-line-utils';
Expand Down Expand Up @@ -52,7 +52,7 @@ export async function format(
const patterns = (await getPatterns({ ...args, ...nxArgs } as any)).map(
// prettier removes one of the \
// prettier-ignore
(p) => `"${p.replace(/\$/g, '\\\$')}"`
(p) => `"${p.replace(/\$/g, "\\\$")}"`
);

// Chunkify the patterns array to prevent crashing the windows terminal
Expand Down Expand Up @@ -156,9 +156,7 @@ async function getPatternsFromApps(
allWorkspaceFiles: FileData[],
projectGraph: ProjectGraph
): Promise<string[]> {
const graph = await createProjectGraphAsync({
exitOnError: true,
});
const graph = await createProjectGraphAsync({ exitOnError: true });
const affectedGraph = await filterAffected(
graph,
calculateFileChanges(affectedFiles, allWorkspaceFiles)
Expand Down Expand Up @@ -270,7 +268,6 @@ function sortTsConfig() {
}

let prettierPath: string;

function getPrettierPath() {
if (prettierPath) {
return prettierPath;
Expand Down
9 changes: 5 additions & 4 deletions packages/nx/src/command-line/run-many/run-many.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,8 @@ describe('run-many', () => {
}
});

it('should be able to select and exclude via patterns', () => {
const start = performance.now();
it('should be able to select and exclude via patterns', async () => {
performance.mark('start');
projectsToRun(
{
targets: ['test'],
Expand All @@ -199,8 +199,9 @@ describe('run-many', () => {
},
projectGraph
);
const end = performance.now();
expect(end - start).toBeLessThan(10000);
performance.mark('end');
const measure = performance.measure('projects', 'start', 'end');
expect(measure.duration).toBeLessThan(10000);
});
});
});
Expand Down
5 changes: 0 additions & 5 deletions packages/nx/src/command-line/show/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import { generateGraph } from '../graph/graph';
export async function showProjectHandler(
args: ShowProjectOptions
): Promise<void> {
performance.mark('code-loading:end');
performance.measure('code-loading', 'init-local', 'code-loading:end');
const graph = await createProjectGraphAsync();
const node = graph.nodes[args.projectName];
if (!node) {
Expand Down Expand Up @@ -72,8 +70,5 @@ export async function showProjectHandler(
}
}
}

// TODO: Find a better fix for this
await new Promise((res) => setImmediate(res));
await output.drain();
}
4 changes: 0 additions & 4 deletions packages/nx/src/command-line/show/projects.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,9 @@ jest.mock('../../project-graph/project-graph', () => ({
.mockImplementation(() => Promise.resolve(graph)),
}));

performance.mark = jest.fn();
performance.measure = jest.fn();

describe('show projects', () => {
beforeEach(() => {
jest.spyOn(console, 'log').mockImplementation(() => {});
performance.mark('init-local');
});
afterEach(() => {
jest.clearAllMocks();
Expand Down
4 changes: 0 additions & 4 deletions packages/nx/src/command-line/show/projects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ import { ShowProjectsOptions } from './command-object';
export async function showProjectsHandler(
args: ShowProjectsOptions
): Promise<void> {
performance.mark('code-loading:end');
performance.measure('code-loading', 'init-local', 'code-loading:end');
let graph = await createProjectGraphAsync();
const nxJson = readNxJson();
const { nxArgs } = splitArgsIntoNxArgsAndOverrides(
Expand Down Expand Up @@ -84,8 +82,6 @@ export async function showProjectsHandler(
}
}

// TODO: Find a better fix for this
await new Promise((res) => setImmediate(res));
await output.drain();
}

Expand Down
2 changes: 2 additions & 0 deletions packages/nx/src/daemon/server/handle-request-project-graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { serializeResult } from '../socket-utils';
import { serverLogger } from './logger';
import { getCachedSerializedProjectGraphPromise } from './project-graph-incremental-recomputation';
import { HandlerResult } from './server';
import { getPlugins } from './plugins';
import { readNxJson } from '../../config/nx-json';

export async function handleRequestProjectGraph(): Promise<HandlerResult> {
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { hashObject } from '../../hasher/file-hasher';
import { readNxJson } from '../../config/nx-json';
import { LoadedNxPlugin, loadNxPlugins } from './internal-api';
import {
LoadedNxPlugin,
loadNxPlugins,
} from '../../project-graph/plugins/internal-api';
import { workspaceRoot } from '../../utils/workspace-root';

let currentPluginsConfigurationHash: string;
Expand Down Expand Up @@ -34,27 +37,6 @@ export async function getPlugins() {
return result;
}

let loadedDefaultPlugins: LoadedNxPlugin[];
let cleanupDefaultPlugins: () => void;

export async function getOnlyDefaultPlugins() {
// If the plugins configuration has not changed, reuse the current plugins
if (loadedDefaultPlugins) {
return loadedPlugins;
}

// Cleanup current plugins before loading new ones
if (cleanupDefaultPlugins) {
cleanupDefaultPlugins();
}

const [result, cleanupFn] = await loadNxPlugins([], workspaceRoot);
cleanupDefaultPlugins = cleanupFn;
loadedPlugins = result;
return result;
}

export function cleanupPlugins() {
cleanup();
cleanupDefaultPlugins();
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ import { serverLogger } from './logger';
import { NxWorkspaceFilesExternals } from '../../native';
import { ConfigurationResult } from '../../project-graph/utils/project-configuration-utils';
import { LoadedNxPlugin } from '../../project-graph/plugins/internal-api';
import { getPlugins } from './plugins';
import {
DaemonProjectGraphError,
ProjectConfigurationsError,
isAggregateProjectGraphError,
} from '../../project-graph/error-types';
import { getPlugins } from '../../project-graph/plugins/get-plugins';

interface SerializedProjectGraph {
error: Error | null;
Expand Down
2 changes: 1 addition & 1 deletion packages/nx/src/daemon/server/shutdown-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import { serverLogger } from './logger';
import { serializeResult } from '../socket-utils';
import { deleteDaemonJsonProcessCache } from '../cache';
import type { Watcher } from '../../native';
import { cleanupPlugins } from './plugins';
import {
DaemonProjectGraphError,
ProjectGraphError,
} from '../../project-graph/error-types';
import { removeDbConnections } from '../../utils/db-connection';
import { cleanupPlugins } from '../../project-graph/plugins/get-plugins';

export const SERVER_INACTIVITY_TIMEOUT_MS = 10800000 as const; // 10800000 ms = 3 hours

Expand Down
8 changes: 6 additions & 2 deletions packages/nx/src/executors/utils/convert-nx-executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ 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 { getPlugins } from '../../project-graph/plugins/get-plugins';
import { loadNxPlugins } from '../../project-graph/plugins/internal-api';

/**
* Convert an Nx Executor into an Angular Devkit Builder
Expand All @@ -20,7 +20,10 @@ export function convertNxExecutor(executor: Executor) {
const promise = async () => {
const nxJsonConfiguration = readNxJson(builderContext.workspaceRoot);

const plugins = await getPlugins();
const [plugins, cleanup] = await loadNxPlugins(
nxJsonConfiguration.plugins,
builderContext.workspaceRoot
);
const projectsConfigurations: ProjectsConfigurations = {
version: 2,
projects: readProjectConfigurationsFromRootMap(
Expand All @@ -33,6 +36,7 @@ export function convertNxExecutor(executor: Executor) {
).projects
),
};
cleanup();
const context: ExecutorContext = {
root: builderContext.workspaceRoot,
projectName: builderContext.target.project,
Expand Down
25 changes: 12 additions & 13 deletions packages/nx/src/plugins/package-json/create-nodes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe('nx package.json workspaces plugin', () => {
'/root'
);

expect(createNodeFromPackageJson('package.json', '/root', {}))
expect(createNodeFromPackageJson('package.json', '/root'))
.toMatchInlineSnapshot(`
{
"projects": {
Expand Down Expand Up @@ -90,9 +90,8 @@ describe('nx package.json workspaces plugin', () => {
},
}
`);
expect(
createNodeFromPackageJson('packages/lib-a/package.json', '/root', {})
).toMatchInlineSnapshot(`
expect(createNodeFromPackageJson('packages/lib-a/package.json', '/root'))
.toMatchInlineSnapshot(`
{
"projects": {
"packages/lib-a": {
Expand Down Expand Up @@ -133,9 +132,8 @@ describe('nx package.json workspaces plugin', () => {
},
}
`);
expect(
createNodeFromPackageJson('packages/lib-b/package.json', '/root', {})
).toMatchInlineSnapshot(`
expect(createNodeFromPackageJson('packages/lib-b/package.json', '/root'))
.toMatchInlineSnapshot(`
{
"projects": {
"packages/lib-b": {
Expand Down Expand Up @@ -733,12 +731,13 @@ describe('nx package.json workspaces plugin', () => {
);

expect(
createNodeFromPackageJson('apps/myapp/package.json', '/root', {})
.projects['apps/myapp'].projectType
createNodeFromPackageJson('apps/myapp/package.json', '/root').projects[
'apps/myapp'
].projectType
).toEqual('application');

expect(
createNodeFromPackageJson('packages/mylib/package.json', '/root', {})
createNodeFromPackageJson('packages/mylib/package.json', '/root')
.projects['packages/mylib'].projectType
).toEqual('library');
});
Expand All @@ -761,7 +760,7 @@ describe('nx package.json workspaces plugin', () => {
);

expect(
createNodeFromPackageJson('package.json', '/root', {}).projects['.']
createNodeFromPackageJson('package.json', '/root').projects['.']
.projectType
).toEqual('library');
});
Expand All @@ -787,11 +786,11 @@ describe('nx package.json workspaces plugin', () => {
);

expect(
createNodeFromPackageJson('packages/mylib/package.json', '/root', {})
createNodeFromPackageJson('packages/mylib/package.json', '/root')
.projects['packages/mylib'].projectType
).toEqual('library');
expect(
createNodeFromPackageJson('example/package.json', '/root', {}).projects[
createNodeFromPackageJson('example/package.json', '/root').projects[
'example'
].projectType
).toBeUndefined();
Expand Down
Loading

0 comments on commit 9a9aa86

Please sign in to comment.