Skip to content

Commit

Permalink
fix(core): reorganize global installation check for better clarity (#…
Browse files Browse the repository at this point in the history
…17373)

(cherry picked from commit 6c84353)
  • Loading branch information
AgentEnder authored and FrozenPandaz committed Jun 2, 2023
1 parent eb77820 commit 940a8d7
Showing 1 changed file with 32 additions and 8 deletions.
40 changes: 32 additions & 8 deletions packages/nx/bin/nx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ function main() {
}

if (!workspace) {
handleNoWorkspace();
handleNoWorkspace(GLOBAL_NX_VERSION);
}

if (!localNx) {
Expand All @@ -79,7 +79,7 @@ function main() {
}
}

function handleNoWorkspace() {
function handleNoWorkspace(globalNxVersion?: string) {
output.log({
title: `The current directory isn't part of an Nx workspace.`,
bodyLines: [
Expand All @@ -94,6 +94,9 @@ function handleNoWorkspace() {
output.note({
title: `For more information please visit https://nx.dev/`,
});

warnIfUsingOutdatedGlobalInstall(globalNxVersion);

process.exit(1);
}

Expand Down Expand Up @@ -169,12 +172,10 @@ function warnIfUsingOutdatedGlobalInstall(
return;
}

const isOutdatedGlobalInstall =
globalNxVersion &&
((localNxVersion && major(globalNxVersion) < major(localNxVersion)) ||
(!localNxVersion &&
getLatestVersionOfNx() &&
major(globalNxVersion) < major(getLatestVersionOfNx())));
const isOutdatedGlobalInstall = checkOutdatedGlobalInstallation(
globalNxVersion,
localNxVersion
);

// Using a global Nx Install
if (isOutdatedGlobalInstall) {
Expand All @@ -194,6 +195,29 @@ function warnIfUsingOutdatedGlobalInstall(
}
}

function checkOutdatedGlobalInstallation(
globalNxVersion?: string,
localNxVersion?: string
) {
// We aren't running a global install, so we can't know if its outdated.
if (!globalNxVersion) {
return false;
}
if (localNxVersion) {
// If the global Nx install is at least a major version behind the local install, warn.
return major(globalNxVersion) < major(localNxVersion);
}
// No local installation was detected. This can happen if the user is running a global install
// that contains an older version of Nx, which is unable to detect the local installation. The most
// recent case where this would have happened would be when we stopped generating workspace.json by default,
// as older global installations used it to determine the workspace root. This only be hit in rare cases,
// but can provide valuable insights for troubleshooting.
const latestVersionOfNx = getLatestVersionOfNx();
if (latestVersionOfNx && major(globalNxVersion) < major(latestVersionOfNx)) {
return true;
}
}

function getLocalNxVersion(workspace: WorkspaceTypeAndRoot): string | null {
try {
const { packageJson } = readModulePackageJson(
Expand Down

0 comments on commit 940a8d7

Please sign in to comment.