Skip to content

Commit

Permalink
fix: handle 0.0.0-pr- releases when comparing versions (#2309)
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxKless authored Nov 11, 2024
1 parent 7255d56 commit 134c2b7
Show file tree
Hide file tree
Showing 45 changed files with 295 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class NxGraphBrowser(project: Project) : NxGraphBrowserBase(project) {
!errorsToShow.isNullOrEmpty() &&
(nxWorkspace?.isPartial != true ||
!hasProjects ||
nxWorkspace.nxVersion.major < 19 ||
!nxWorkspace.nxVersion.gte(19) ||
needsNonExistentProject)
) {
val errorHtml = getErrorHtml(errorsToShow)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ private val logger = logger<OldNxGraphService>()
class OldNxGraphBrowser(
project: Project,
private val state: StateFlow<NxGraphStates>,
private val nxVersion: Deferred<NxVersion?>
private val nxVersion: Deferred<NxVersion?>,
) : NxGraphBrowserBase(project) {
private val cs = OldNxGraphBrowserCoroutineHolder.getInstance(project).cs

Expand All @@ -57,10 +57,10 @@ class OldNxGraphBrowser(
fun selectAllProjects() {
executeWhenLoaded {
lastCommand = Command.SelectAll
val major = nxVersion.await()?.major
if (major == null || major.toInt() > 14) {
val nxVersion = nxVersion.await()
if (nxVersion == null || nxVersion.gte(15)) {
browser.executeJavaScriptAsync("window.externalApi?.selectAllProjects()")
} else if (major.toInt() == 14) {
} else if (nxVersion.major == 14) {
browser.executeJavaScriptAsync(
"window.externalApi.depGraphService.send({type: 'selectAll'})"
)
Expand All @@ -73,10 +73,10 @@ class OldNxGraphBrowser(
fun focusProject(projectName: String) {
executeWhenLoaded {
lastCommand = Command.FocusProject(projectName)
val major = nxVersion.await()?.major
if (major == null || major.toInt() > 14) {
val nxVersion = nxVersion.await()
if (nxVersion == null || nxVersion.gte(15)) {
browser.executeJavaScriptAsync("window.externalApi.focusProject('$projectName')")
} else if (major.toInt() == 14) {
} else if (nxVersion.major == 14) {
browser.executeJavaScriptAsync(
"window.externalApi.depGraphService.send({type: 'focusProject', projectName: '$projectName'})"
)
Expand Down Expand Up @@ -185,19 +185,22 @@ class OldNxGraphBrowser(
padding: 12px;
}
</style>
"""
""",
)
.replace(
"</head>",
"""
$nxConsoleEnvironmentScriptTag
</head>
"""
""",
)
.replace(Regex("</body>"), """
.replace(
Regex("</body>"),
"""
</body>
""")
""",
)

browser.loadHTML(transformedGraphHtml, "https://nx-graph")

Expand Down Expand Up @@ -404,7 +407,7 @@ class OldNxGraphBrowser(
Notifier.notifyAnything(
project,
"Couldn't find file at path $path",
NotificationType.ERROR
NotificationType.ERROR,
)
return@addHandler null
}
Expand All @@ -431,7 +434,7 @@ class OldNxGraphBrowser(
TelemetryService.getInstance(project)
.featureUsed(
TelemetryEvent.MISC_SHOW_PROJECT_CONFIGURATION,
mapOf("source" to TelemetryEventSource.GRAPH_INTERACTION)
mapOf("source" to TelemetryEventSource.GRAPH_INTERACTION),
)

project.nxWorkspace()?.workspace?.projects?.get(msg)?.apply {
Expand Down Expand Up @@ -464,7 +467,7 @@ class OldNxGraphBrowser(
TelemetryService.getInstance(project)
.featureUsed(
TelemetryEvent.TASKS_RUN,
mapOf("source" to TelemetryEventSource.GRAPH_INTERACTION)
mapOf("source" to TelemetryEventSource.GRAPH_INTERACTION),
)

val (projectName, targetName) = msg.split(":")
Expand Down
12 changes: 11 additions & 1 deletion apps/intellij/src/main/kotlin/dev/nx/console/models/NxVersion.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ import kotlinx.serialization.Serializable

@Serializable()
data class NxVersion(val minor: Int, val major: Int, val full: String) {
public fun gte(other: NxVersion): Boolean {
fun gte(other: NxVersion): Boolean {
if (this.full.startsWith("0.0.0-pr-")) {
return true
}
if (other.full.startsWith("0.0.0-pr-")) {
return false
}
val semVerThis = SemVer.parseFromText(this.full)
val semVerOther = SemVer.parseFromText(other.full)
if (semVerThis != null && semVerOther != null) {
Expand All @@ -19,6 +25,10 @@ data class NxVersion(val minor: Int, val major: Int, val full: String) {
return false
}

fun gte(other: Int): Boolean {
return gte(NxVersion(other, 0, "$other.0.0"))
}

fun equals(other: NxVersion): Boolean {
return this.major == other.major && this.minor == other.minor && this.full == other.full
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ class OldProjectDetailsBrowser(project: Project, private val file: VirtualFile)
(!hasProjects ||
nxWorkspace?.isPartial != true ||
!hasProject ||
version.major < 19)
!version.gte(19))
) {
withContext(Dispatchers.EDT) {
if (browser.isDisposed) return@withContext
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
X_COMPLETION_TYPE,
} from '@nx-console/shared/json-schema';
import { Logger } from '@nx-console/shared/schema';
import { NxVersion } from '@nx-console/shared/types';
import { NxVersion } from '@nx-console/shared/nx-version';
import {
ASTNode,
CompletionItem,
Expand Down Expand Up @@ -99,7 +99,7 @@ function completionItems(
completion: CompletionType,
glob?: string
): Promise<CompletionItem[]> => {
// const supportsInterpolation = nxVersion.major >= 16;
// const supportsInterpolation = gte(nxVersion, '16.0.0');
// todo(jcammisuli): change this once executors support {workspaceRoot} and {projectRoot} in their options
const supportsInterpolation = false;
switch (completion) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import {
importWorkspaceDependency,
workspaceDependencyPath,
} from '@nx-console/shared/npm';
import { gte } from 'semver';
import type { ProjectGraph, Target } from 'nx/src/devkit-exports';
import { gte } from '@nx-console/shared/nx-version';

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

Expand Down Expand Up @@ -55,7 +55,7 @@ export async function targetLink(
typeof import('@nx/devkit/src/executors/parse-target-string')
>(importPath, lspLogger);
let parsedTarget: Target;
if (gte(nxVersion.full, '17.0.6')) {
if (gte(nxVersion, '17.0.6')) {
// the nx console data structure to handle projects is not the same as ProjectGraph
// we create a partial project graph to pass to the parseTargetString function
// we only need a single project in it so we don't have to map over the entire workspace data
Expand Down
2 changes: 1 addition & 1 deletion libs/language-server/types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import {
GeneratorContext,
GeneratorSchema,
} from '@nx-console/shared/generate-ui-types';
import { NxVersion } from '@nx-console/shared/nx-version';
import {
GeneratorCollectionInfo,
Option,
TaskExecutionSchema,
} from '@nx-console/shared/schema';
import {
CloudOnboardingInfo,
NxVersion,
NxWorkspace,
PDVData,
TreeNode,
Expand Down
4 changes: 2 additions & 2 deletions libs/language-server/watcher/src/lib/watcher.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { lspLogger } from '@nx-console/language-server/utils';
import { getNxVersion } from '@nx-console/language-server/workspace';
import { debounce } from '@nx-console/shared/utils';
import { gte } from 'semver';
import { DaemonWatcher } from './daemon-watcher';
import { NativeWatcher } from './native-watcher';
import { ParcelWatcher } from './parcel-watcher';
import { gte } from '@nx-console/shared/nx-version';

let _daemonWatcher: DaemonWatcher | undefined;
let _nativeWatcher: NativeWatcher | undefined;
Expand All @@ -16,7 +16,7 @@ export async function languageServerWatcher(
const version = await getNxVersion(workspacePath);
const debouncedCallback = debounce(callback, 1000);

if (gte(version.full, '16.4.0')) {
if (gte(version, '16.4.0')) {
if (process.platform === 'win32') {
if (_nativeWatcher) {
_nativeWatcher.stop();
Expand Down
6 changes: 4 additions & 2 deletions libs/language-server/workspace/src/lib/get-nx-version.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { lspLogger } from '@nx-console/language-server/utils';
import { findNxPackagePath } from '@nx-console/shared/npm';
import { NxVersion } from '@nx-console/shared/types';
import { NxVersion } from '@nx-console/shared/nx-version';
import { coerce, SemVer } from 'semver';

let nxWorkspacePackageJson: { version: string } | undefined;
Expand Down Expand Up @@ -31,7 +31,9 @@ export async function getNxVersion(workspacePath: string): Promise<NxVersion> {
full: defaultSemver.version,
};
}
const nxVersion = coerce(nxWorkspacePackageJson.version);
const nxVersion = coerce(nxWorkspacePackageJson.version, {
includePrerelease: true,
});
if (!nxVersion) {
return {
major: defaultSemver.major,
Expand Down
21 changes: 10 additions & 11 deletions libs/language-server/workspace/src/lib/get-nx-workspace-config.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import { lspLogger } from '@nx-console/language-server/utils';
import { readAndCacheJsonFile } from '@nx-console/shared/file-system';
import { Logger } from '@nx-console/shared/schema';
import {
NxError,
NxVersion,
NxWorkspaceConfiguration,
} from '@nx-console/shared/types';
import { NxError, NxWorkspaceConfiguration } from '@nx-console/shared/types';
import type {
NxJsonConfiguration,
ProjectFileMap,
Expand All @@ -14,7 +10,6 @@ import type {
} from 'nx/src/devkit-exports';
import { join } from 'path';
import { performance } from 'perf_hooks';
import { gte } from 'semver';
import {
getNxDaemonClient,
getNxOutput,
Expand All @@ -25,6 +20,7 @@ import {
import type { ProjectGraphError } from 'nx/src/project-graph/error-types';
import type { ConfigurationSourceMaps } from 'nx/src/project-graph/utils/project-configuration-utils';
import { readJsonFile, readNxJson } from '@nx-console/shared/npm';
import { gte, NxVersion } from '@nx-console/shared/nx-version';

let _defaultProcessExit: typeof process.exit;

Expand All @@ -47,7 +43,10 @@ export async function getNxWorkspaceConfig(
const start = performance.now();
logger.log('Retrieving workspace configuration');

if (nxVersion.major < 12) {
lspLogger.log(
`${JSON.stringify(nxVersion)}, gte: ${gte(nxVersion, '12.0.0')}`
);
if (!gte(nxVersion, '12.0.0')) {
lspLogger.log('Major version is less than 12');
return readWorkspaceConfigs(workspacePath);
}
Expand Down Expand Up @@ -85,7 +84,7 @@ export async function getNxWorkspaceConfig(

let workspaceConfiguration: NxWorkspaceConfiguration | undefined =
undefined;
if (!gte(nxVersion.full, '17.3.0')) {
if (!gte(nxVersion, '17.3.0')) {
try {
workspaceConfiguration = nxWorkspacePackage.readWorkspaceConfig({
format: 'nx',
Expand Down Expand Up @@ -120,9 +119,9 @@ export async function getNxWorkspaceConfig(
};
}

if (nxVersion.major < 13) {
if (!gte(nxVersion, '13.0.0')) {
projectGraph = (nxProjectGraph as any).createProjectGraph();
} else if (gte(nxVersion.full, '17.2.0')) {
} else if (gte(nxVersion, '17.2.0')) {
lspLogger.log('createProjectGraphAndSourceMapsAsync');
try {
const projectGraphAndSourceMaps = await (
Expand Down Expand Up @@ -167,7 +166,7 @@ export async function getNxWorkspaceConfig(
}

let projectFileMap: ProjectFileMap = {};
if (gte(nxVersion.full, '16.3.1') && projectGraph) {
if (gte(nxVersion, '16.3.1') && projectGraph) {
projectFileMap =
(await nxProjectGraphUtils?.createProjectFileMapUsingProjectGraph(
projectGraph
Expand Down
4 changes: 2 additions & 2 deletions libs/language-server/workspace/src/lib/get-pdv-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import type {
ProjectGraphProjectNode,
} from 'nx/src/devkit-exports';
import { join, relative } from 'path';
import { gte } from 'semver';
import { getNxCloudStatus } from './get-nx-cloud-status';
import { getNxVersion } from './get-nx-version';
import { getProjectByPath } from './get-project-by-path';
import { getSourceMapFilesToProjectsMap } from './get-source-map';
import { nxWorkspace } from './workspace';
import { gte } from '@nx-console/shared/nx-version';

export async function getPDVData(
workspacePath: string,
Expand All @@ -32,7 +32,7 @@ export async function getPDVData(

const nxVersion = await getNxVersion(workspacePath);

if (!gte(nxVersion.full, '19.8.0')) {
if (!gte(nxVersion, '19.8.0')) {
return {
resultType: 'OLD_NX_VERSION',
graphBasePath,
Expand Down
2 changes: 1 addition & 1 deletion libs/shared/generate-ui-types/src/lib/generator-schema.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Option } from '@nx-console/shared/schema';
import { NxVersion } from '@nx-console/shared/types';
import { NxVersion } from '@nx-console/shared/nx-version';

export type GeneratorSchema = {
collectionName: string;
Expand Down
5 changes: 3 additions & 2 deletions libs/shared/json-schema/src/lib/common-json-schema.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { JSONSchema } from 'vscode-json-languageservice';
import { CompletionType } from './completion-type';
import { NxVersion } from '@nx-console/shared/types';
import { NxVersion } from '@nx-console/shared/nx-version';
import { gte } from '@nx-console/shared/nx-version';

export const implicitDependencies: JSONSchema = {
type: 'array',
Expand Down Expand Up @@ -72,7 +73,7 @@ export const tags: JSONSchema = {
};

const projects = (nxVersion: NxVersion): JSONSchema => {
if (nxVersion.major < 16) {
if (!gte(nxVersion, '16.0.0')) {
return {
type: 'string',
enum: ['self', 'dependencies'],
Expand Down
2 changes: 1 addition & 1 deletion libs/shared/json-schema/src/lib/nx-json-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { JSONSchema } from 'vscode-json-languageservice';
import { targets } from './common-json-schema';
import { CompletionType } from './completion-type';
import { createBuildersAndExecutorsSchema } from './create-builders-and-executors-schema';
import { NxVersion } from '@nx-console/shared/types';
import { NxVersion } from '@nx-console/shared/nx-version';
import { workspaceDependencyPath } from '@nx-console/shared/npm';
import { join } from 'path';
import { readFileSync } from 'fs';
Expand Down
2 changes: 1 addition & 1 deletion libs/shared/json-schema/src/lib/package-json-schema.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { NxVersion } from '@nx-console/shared/types';
import { NxVersion } from '@nx-console/shared/nx-version';
import {
implicitDependencies,
namedInputs,
Expand Down
2 changes: 1 addition & 1 deletion libs/shared/json-schema/src/lib/project-json-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from './common-json-schema';
import { CompletionType, EnhancedJsonSchema } from './completion-type';
import { createBuildersAndExecutorsSchema } from './create-builders-and-executors-schema';
import { NxVersion } from '@nx-console/shared/types';
import { NxVersion } from '@nx-console/shared/nx-version';

type JSONSchemaMap = NonNullable<JSONSchema['properties']>;

Expand Down
5 changes: 3 additions & 2 deletions libs/shared/npm/src/lib/workspace-dependencies.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
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 { NxVersion } from '@nx-console/shared/nx-version';
import { stat } from 'fs/promises';
import { join } from 'path';
import { npmDependencies } from './npm-dependencies';
Expand All @@ -11,6 +11,7 @@ import {
pnpDependencyPath,
} from './pnp-dependencies';
import { platform } from 'os';
import { gte } from '@nx-console/shared/nx-version';

/**
* Get dependencies for the current workspace.
Expand Down Expand Up @@ -144,7 +145,7 @@ async function localDependencies(
}

// Local plugins do not work with nxVersion less than 13
if (version.major < 13) {
if (!gte(version, '13.0.0')) {
return [];
}

Expand Down
Loading

0 comments on commit 134c2b7

Please sign in to comment.