Skip to content

Commit

Permalink
fix(core): fix misc issues (#29114)
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 -->

This PR fixes several issues:

1. Affected loaded plugins again.
2. `package-json` plugin takes a while and is loaded often.
3. Performance logging was not showing up properly.

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

1. Affected does not load plugins again.
2. `package-json` plugin utilizes a cache so that it is fast even when
it is loaded often.
3. Performance logging shows up properly

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

Fixes #

---------

Co-authored-by: Craigory Coppola <[email protected]>
  • Loading branch information
FrozenPandaz and AgentEnder authored Dec 3, 2024
1 parent d32ca37 commit 9ad6b8c
Show file tree
Hide file tree
Showing 29 changed files with 265 additions and 165 deletions.
8 changes: 5 additions & 3 deletions packages/nx/bin/post-install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ import { setupWorkspaceContext } from '../src/utils/workspace-context';
if (isMainNxPackage() && fileExists(join(workspaceRoot, 'nx.json'))) {
assertSupportedPlatform();
setupWorkspaceContext(workspaceRoot);
try {
await daemonClient.stop();
} catch (e) {}
if (daemonClient.enabled()) {
try {
await daemonClient.stop();
} catch (e) {}
}
const tasks: Array<Promise<any>> = [
buildProjectGraphAndSourceMapsWithoutDaemon(),
];
Expand Down
43 changes: 40 additions & 3 deletions packages/nx/plugins/package-json.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,50 @@
import type { NxPluginV2 } from '../src/project-graph/plugins';
import { createNodesFromFiles, 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',
createNodes: [
createNodesV2: [
'*/**/package.json',
(f) => createNodeFromPackageJson(f, workspaceRoot),
(configFiles, options, context) => {
const cache = readPackageJsonConfigurationCache();

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

writeCache(cache);

return result;
},
],
};

module.exports = plugin;
module.exports.readPackageJsonConfigurationCache =
readPackageJsonConfigurationCache;
4 changes: 3 additions & 1 deletion packages/nx/src/command-line/affected/affected.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ 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: 7 additions & 4 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 { FileData, calculateFileChanges } from '../../project-graph/file-utils';
import { calculateFileChanges, FileData } from '../../project-graph/file-utils';
import {
NxArgs,
getProjectRoots,
NxArgs,
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,7 +156,9 @@ 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 @@ -268,6 +270,7 @@ function sortTsConfig() {
}

let prettierPath: string;

function getPrettierPath() {
if (prettierPath) {
return prettierPath;
Expand Down
9 changes: 4 additions & 5 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', async () => {
performance.mark('start');
it('should be able to select and exclude via patterns', () => {
const start = performance.now();
projectsToRun(
{
targets: ['test'],
Expand All @@ -199,9 +199,8 @@ describe('run-many', () => {
},
projectGraph
);
performance.mark('end');
const measure = performance.measure('projects', 'start', 'end');
expect(measure.duration).toBeLessThan(10000);
const end = performance.now();
expect(end - start).toBeLessThan(10000);
});
});
});
Expand Down
5 changes: 5 additions & 0 deletions packages/nx/src/command-line/show/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ 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 @@ -70,5 +72,8 @@ export async function showProjectHandler(
}
}
}

// TODO: Find a better fix for this
await new Promise((res) => setImmediate(res));
await output.drain();
}
4 changes: 4 additions & 0 deletions packages/nx/src/command-line/show/projects.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@ 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: 4 additions & 0 deletions packages/nx/src/command-line/show/projects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ 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 @@ -82,6 +84,8 @@ export async function showProjectsHandler(
}
}

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

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

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

const [plugins, cleanup] = await loadNxPlugins(
nxJsonConfiguration.plugins,
builderContext.workspaceRoot
);
const plugins = await getPlugins();
const projectsConfigurations: ProjectsConfigurations = {
version: 2,
projects: readProjectConfigurationsFromRootMap(
Expand All @@ -36,7 +33,6 @@ export function convertNxExecutor(executor: Executor) {
).projects
),
};
cleanup();
const context: ExecutorContext = {
root: builderContext.workspaceRoot,
projectName: builderContext.target.project,
Expand Down
25 changes: 13 additions & 12 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,8 +90,9 @@ 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 @@ -132,8 +133,9 @@ 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 @@ -731,13 +733,12 @@ 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 @@ -760,7 +761,7 @@ describe('nx package.json workspaces plugin', () => {
);

expect(
createNodeFromPackageJson('package.json', '/root').projects['.']
createNodeFromPackageJson('package.json', '/root', {}).projects['.']
.projectType
).toEqual('library');
});
Expand All @@ -786,11 +787,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 9ad6b8c

Please sign in to comment.