Skip to content

Commit

Permalink
chore: update nx (#2105)
Browse files Browse the repository at this point in the history
* chore: update nx

* feat: update nx package imports to be made from the workspace deps

* fix: log missing workspace package correctly

* chore: update nx to 19.0.0-beta.6
  • Loading branch information
leosvelperez authored Apr 26, 2024
1 parent b7ba469 commit 92e7763
Show file tree
Hide file tree
Showing 33 changed files with 460 additions and 353 deletions.
3 changes: 3 additions & 0 deletions apps/nxls-e2e/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
"targets": {
"test": {
"command": "jest --detectOpenHandles --runInBand --verbose",
"options": {
"cwd": "apps/nxls-e2e"
},
"dependsOn": [
{
"projects": "nxls",
Expand Down
3 changes: 2 additions & 1 deletion apps/nxls/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@
"@parcel/watcher",
"@nx/nx*",
"webpack",
"fsevents"
"fsevents",
"nx"
],
"target": "es2020",
"outputFileName": "main.js",
Expand Down
6 changes: 4 additions & 2 deletions apps/vscode/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@
"fsevents",
"ts-node",
"pnpapi",
"@angular-devkit/*"
"@angular-devkit/*",
"nx"
],
"bundle": true,
"thirdParty": true,
Expand Down Expand Up @@ -106,7 +107,8 @@
"pnpapi",
"webpack",
"@angular-devkit/*",
"@nx/nx*"
"@nx/nx*",
"nx"
],
"thirdParty": true,
"minify": true,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { isExecutorStringNode } from '@nx-console/language-server/utils';
import {
isExecutorStringNode,
lspLogger,
} from '@nx-console/language-server/utils';
import { getExecutors } from '@nx-console/language-server/workspace';
import { resolveImplementation } from 'nx/src/config/schema-utils';
import { importNxPackagePath } from '@nx-console/shared/npm';
import { dirname } from 'path';
import { JSONDocument } from 'vscode-json-languageservice';
import { DefinitionParams, LocationLink } from 'vscode-languageserver';
Expand Down Expand Up @@ -28,6 +31,9 @@ export async function getDefinition(
return undefined;
}

const { resolveImplementation } = await importNxPackagePath<
typeof import('nx/src/config/schema-utils')
>(workingPath, 'src/config/schema-utils', lspLogger);
const executorFile = resolveImplementation(
executor.implementationPath,
dirname(executor.configPath)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { parseTargetString } from '@nx/devkit/src/executors/parse-target-string';
import { fileExists, readFile } from '@nx-console/shared/file-system';
import {
findProperty,
Expand All @@ -16,6 +15,10 @@ import {
} from 'vscode-json-languageservice';
import { URI } from 'vscode-uri';
import { createRange } from './create-range';
import {
importWorkspaceDependency,
workspaceDependencyPath,
} from '@nx-console/shared/npm';

const tempDocumentCounter = new Map<string, number>();

Expand All @@ -30,6 +33,18 @@ export async function targetLink(
const targetString = node.value;
let project, target, configuration;
try {
const devkitPath = await workspaceDependencyPath(workingPath, '@nx/devkit');
if (!devkitPath) {
lspLogger.log(
`Unable to load the "@nx/devkit" package from the workspace. Please ensure that the proper dependencies are installed locally.`
);
throw 'local @nx/devkit dependency not found';
}

const importPath = join(devkitPath, 'src/executors/parse-target-string');
const { parseTargetString } = await importWorkspaceDependency<
typeof import('@nx/devkit/src/executors/parse-target-string')
>(importPath, lspLogger);
const parsedTargets = parseTargetString(targetString);
project = parsedTargets.project;
target = parsedTargets.target;
Expand Down
11 changes: 7 additions & 4 deletions libs/language-server/utils/src/lib/nx-reset.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { importNxPackagePath } from '@nx-console/shared/npm';
import { Logger } from '@nx-console/shared/schema';
import { exec } from 'node:child_process';
import {
detectPackageManager,
getPackageManagerCommand,
} from 'nx/src/utils/package-manager';

export async function nxReset(workspacePath: string, logger: Logger) {
logger.log('Resetting workspace with `nx reset`');
const { detectPackageManager, getPackageManagerCommand } =
await importNxPackagePath<typeof import('nx/src/utils/package-manager')>(
workspacePath,
'src/utils/package-manager',
logger
);
const packageManagerCommands = getPackageManagerCommand(
detectPackageManager(workspacePath)
);
Expand Down
9 changes: 7 additions & 2 deletions libs/language-server/watcher/src/lib/native-watcher.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { WatchEvent, Watcher } from 'nx/src/native';
import type { WatchEvent, Watcher } from 'nx/src/native';
import { lspLogger } from '@nx-console/language-server/utils';
import { importNxPackagePath } from '@nx-console/shared/npm';
import { normalize } from 'path';
import { match as minimatch } from 'minimatch';

Expand Down Expand Up @@ -35,7 +36,11 @@ export class NativeWatcher {
}

private async initWatcher() {
const native = await import('nx/src/native');
const native = await importNxPackagePath<typeof import('nx/src/native')>(
this.workspacePath,
'src/native/index.js',
lspLogger
);
this.watcher = new native.Watcher(this.workspacePath);

this.watcher.watch((err: string | null, events: WatchEvent[]) => {
Expand Down
11 changes: 8 additions & 3 deletions libs/language-server/watcher/src/lib/watcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { lspLogger } from '@nx-console/language-server/utils';
import { getNxVersion } from '@nx-console/language-server/workspace';
import { debounce } from '@nx-console/shared/utils';
import * as watcher from '@parcel/watcher';
import { getIgnoredGlobs } from 'nx/src/utils/ignore';
import { platform } from 'os';
import { gte } from 'semver';
import { DaemonWatcher } from './daemon-watcher';
import { NativeWatcher } from './native-watcher';
import { importNxPackagePath } from '@nx-console/shared/npm';

let _daemonWatcher: DaemonWatcher | undefined;
let _nativeWatcher: NativeWatcher | undefined;
Expand Down Expand Up @@ -64,7 +64,7 @@ export async function languageServerWatcher(
debouncedCallback();
}
},
watcherOptions(workspacePath)
await watcherOptions(workspacePath)
);

return () => {
Expand All @@ -74,7 +74,12 @@ export async function languageServerWatcher(
}
}

function watcherOptions(workspacePath: string): watcher.Options | undefined {
async function watcherOptions(
workspacePath: string
): Promise<watcher.Options | undefined> {
const { getIgnoredGlobs } = await importNxPackagePath<
typeof import('nx/src/utils/ignore')
>(workspacePath, 'src/utils/ignore', lspLogger);
const options: watcher.Options = {
ignore: getIgnoredGlobs(workspacePath),
};
Expand Down
20 changes: 9 additions & 11 deletions libs/language-server/workspace/src/lib/create-project-graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,13 @@ export async function createProjectGraph(
const { isEncapsulatedNx } = await nxWorkspace(workspacePath);
const projectGraphOutput = await getProjectGraphOutput(workspacePath);

return new Promise<string | undefined>((res, rej) => {
const command = getNxExecutionCommand({
cwd: workspacePath,
displayCommand:
`nx graph ${showAffected ? '--affected' : ''} --file ` +
projectGraphOutput.relativePath,
encapsulatedNx: isEncapsulatedNx,
});

return getNxExecutionCommand({
cwd: workspacePath,
displayCommand:
`nx graph ${showAffected ? '--affected' : ''} --file ` +
projectGraphOutput.relativePath,
encapsulatedNx: isEncapsulatedNx,
}).then((command) => {
logger.log(`Generating graph with command: \`${command}\``);
try {
execSync(command, {
Expand All @@ -37,10 +35,10 @@ export async function createProjectGraph(
stdio: 'ignore',
});

res(undefined);
return undefined;
} catch (e) {
const errorMessage = `${e.output[1] || e}`;
rej('Unable to create project graph: ' + errorMessage);
throw 'Unable to create project graph: ' + errorMessage;
}
});
}
21 changes: 7 additions & 14 deletions libs/language-server/workspace/src/lib/get-nx-workspace-package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import type * as NxProjectGraphFileUtils from 'nx/src/project-graph/file-map-uti
import type * as NxDaemonClient from 'nx/src/daemon/client/client';
import type * as NxDaemonCache from 'nx/src/daemon/cache';
import type * as NxOutput from 'nx/src/utils/output';
import { platform } from 'os';
import { join } from 'path';
import { findNxPackagePath } from '@nx-console/shared/npm';
import {
findNxPackagePath,
importWorkspaceDependency,
} from '@nx-console/shared/npm';
import { Logger } from '@nx-console/shared/schema';

export async function getNxDaemonClient(
Expand Down Expand Up @@ -113,20 +115,11 @@ export async function getNxPackage<T>(
logger: Logger
): Promise<T> {
if (!importPath) {
logger?.log(
`Unable to load the ${importPath} dependency from the workspace. Please ensure that the proper dependencies are installed locally.`
logger.log(
`Unable to load the "nx" package from the workspace. Please ensure that the proper dependencies are installed locally.`
);
throw 'local Nx dependency not found';
}

if (platform() === 'win32') {
importPath = importPath.replace(/\\/g, '/');
}

// eslint-disable-next-line @typescript-eslint/no-var-requires
const imported = require(importPath);

logger?.log(`Using local Nx package at ${importPath}`);

return imported;
return importWorkspaceDependency(importPath, logger);
}
19 changes: 16 additions & 3 deletions libs/language-server/workspace/src/lib/get-project-graph-output.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { lspLogger } from '@nx-console/language-server/utils';
import { findNxPackagePath } from '@nx-console/shared/npm';
import {
findNxPackagePath,
importWorkspaceDependency,
} from '@nx-console/shared/npm';
import { join, relative, normalize } from 'path';
import { getNxPackage } from './get-nx-workspace-package';

export async function getProjectGraphOutput(workspacePath: string) {
const cacheDir = await getCacheDir(workspacePath);
Expand All @@ -21,5 +23,16 @@ async function getCacheDir(workspacePath: string): Promise<string> {
join('src', 'utils', 'cache-directory.js')
);

return ((await getNxPackage(importPath, lspLogger)) as any).cacheDir;
if (!importPath) {
lspLogger.log(
`Unable to load the "nx" package from the workspace. Please ensure that the proper dependencies are installed locally.`
);
throw 'local Nx dependency not found';
}

return (
await importWorkspaceDependency<
typeof import('nx/src/utils/cache-directory')
>(importPath, lspLogger)
).cacheDir;
}
3 changes: 1 addition & 2 deletions libs/language-server/workspace/src/lib/get-source-map.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { TargetConfiguration } from 'nx/src/devkit-exports';
import type { TargetConfiguration } from 'nx/src/devkit-exports';
import { nxWorkspace } from './workspace';
import { normalize, relative } from 'path';
import { lspLogger } from '@nx-console/language-server/utils';

let _sourceMapFilesToProjectMap: Record<string, string> | undefined = undefined;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export async function hasAffectedProjects(
): Promise<boolean> {
const { isEncapsulatedNx } = await nxWorkspace(workspacePath);

const command = getNxExecutionCommand({
const command = await getNxExecutionCommand({
cwd: workspacePath,
displayCommand: `nx show projects --affected`,
encapsulatedNx: isEncapsulatedNx,
Expand Down
4 changes: 2 additions & 2 deletions libs/shared/json-schema/src/lib/project-json-schema.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { TargetConfiguration } from 'nx/src/devkit-exports';
import type { TargetConfiguration } from 'nx/src/devkit-exports';
import { CollectionInfo } from '@nx-console/shared/schema';
import { TargetDefaults } from 'nx/src/config/nx-json';
import type { TargetDefaults } from 'nx/src/config/nx-json';
import type { JSONSchema } from 'vscode-json-languageservice';
import {
implicitDependencies,
Expand Down
49 changes: 47 additions & 2 deletions libs/shared/npm/src/lib/workspace-dependencies.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { directoryExists } from '@nx-console/shared/file-system';
import { WorkspaceProjects } from '@nx-console/shared/schema';
import { directoryExists, fileExists } from '@nx-console/shared/file-system';
import type { Logger, WorkspaceProjects } from '@nx-console/shared/schema';
import { NxVersion } from '@nx-console/shared/types';
import { stat } from 'fs/promises';
import { join } from 'path';
Expand All @@ -10,6 +10,7 @@ import {
pnpDependencies,
pnpDependencyPath,
} from './pnp-dependencies';
import { platform } from 'os';

/**
* Get dependencies for the current workspace.
Expand Down Expand Up @@ -75,6 +76,50 @@ export async function workspaceDependencyPath(
}
}

export function importWorkspaceDependency<T>(
importPath: string,
logger: Logger = {
log(message) {
console.log(message);
},
}
): Promise<T> {
if (platform() === 'win32') {
importPath = importPath.replace(/\\/g, '/');
}

// eslint-disable-next-line @typescript-eslint/no-var-requires
const imported = require(importPath);

logger?.log(`Using local Nx package at ${importPath}`);

return imported;
}

export async function importNxPackagePath<T>(
workspacePath: string,
nestedPath: string,
logger: Logger = {
log(message) {
console.log(message);
},
}
): Promise<T> {
const nxWorkspaceDepPath = await workspaceDependencyPath(workspacePath, 'nx');

if (!nxWorkspaceDepPath) {
logger?.log(
`Unable to load the "nx" package from the workspace. Please ensure that the proper dependencies are installed locally.`
);
throw 'local Nx dependency not found';
}

return importWorkspaceDependency(
join(nxWorkspaceDepPath, nestedPath),
logger
);
}

export async function localDependencyPath(
workspacePath: string,
workspaceDependencyName: string,
Expand Down
2 changes: 1 addition & 1 deletion libs/shared/types/src/lib/project-folder-tree.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ProjectConfiguration } from 'nx/src/devkit-exports';
import type { ProjectConfiguration } from 'nx/src/devkit-exports';

export type TreeNode = {
dir: string;
Expand Down
Loading

0 comments on commit 92e7763

Please sign in to comment.