From b6d5b6f794420eb9d47839fcc373fff645e69b26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leosvel=20P=C3=A9rez=20Espinosa?= Date: Thu, 15 Feb 2024 11:14:28 +0100 Subject: [PATCH] feat(core): support migrating to canary versions of nx for testing --- docs/shared/reference/environment-variables.md | 9 +++++---- packages/nx/src/command-line/migrate/command-object.ts | 5 ++--- packages/nx/src/command-line/migrate/migrate.spec.ts | 6 ++++++ packages/nx/src/command-line/migrate/migrate.ts | 6 ++++-- 4 files changed, 17 insertions(+), 9 deletions(-) diff --git a/docs/shared/reference/environment-variables.md b/docs/shared/reference/environment-variables.md index f5e0c1e4c5cec..816555a87026b 100644 --- a/docs/shared/reference/environment-variables.md +++ b/docs/shared/reference/environment-variables.md @@ -4,7 +4,7 @@ The following environment variables are ones that you can set to change the beha | Property | Type | Description | | -------------------------------- | ------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| NX_ADD_PLUGINS | boolean | If set to false, Nx will not add plugins to infer tasks. This is true by default. Workspaces created before Nx 18 will have this disabled via a migration for backwards compatibility | +| NX_ADD_PLUGINS | boolean | If set to `false`, Nx will not add plugins to infer tasks. This is `true` by default. Workspaces created before Nx 18 will have this disabled via a migration for backwards compatibility | | NX_BASE | string | The default base branch to use when calculating the affected projects. Can be overridden on the command line with `--base`. | | NX_CACHE_DIRECTORY | string | The cache for task outputs is stored in `node_modules/.cache/nx` by default. Set this variable to use a different directory. | | NX_CACHE_PROJECT_GRAPH | boolean | If set to `false`, disables the project graph cache. Most useful when developing a plugin that modifies the project graph. | @@ -16,18 +16,19 @@ The following environment variables are ones that you can set to change the beha | NX_PROJECT_GRAPH_CACHE_DIRECTORY | string | The project graph cache is stored in `node_modules/.cache/nx` by default. Set this variable to use a different directory. | | NX_PROJECT_GRAPH_MAX_WORKERS | number | The number of workers to use when calculating the project graph. | | NX_PARALLEL | number | The number of tasks Nx should run in parallel. Overrides any configured value inside nx.json | -| NX_RUNNER | string | The name of task runner from the config to use. Can be overridden on the command line with `--runner`. Not read if NX_TASKS_RUNNER is set. | +| NX_RUNNER | string | The name of task runner from the config to use. Can be overridden on the command line with `--runner`. Not read if `NX_TASKS_RUNNER` is set. | | NX_SKIP_NX_CACHE | boolean | Rerun the tasks even when the results are available in the cache | -| NX_TASKS_RUNNER | string | The name of task runner from the config to use. Can be overridden on the command line with `--runner`. Preferred over NX_RUNNER. | +| NX_TASKS_RUNNER | string | The name of task runner from the config to use. Can be overridden on the command line with `--runner`. Preferred over `NX_RUNNER`. | | NX_TASKS_RUNNER_DYNAMIC_OUTPUT | boolean | If set to `false`, will use non-dynamic terminal output strategy (what you see in CI), even when you terminal can support the dynamic version | | NX_VERBOSE_LOGGING | boolean | If set to `true`, will print debug information useful for troubleshooting | | NX_DRY_RUN | boolean | If set to `true`, will perform a dry run of the generator. No files will be created and no packages will be installed. | | NX_INTERACTIVE | boolean | If set to `true`, will allow Nx to prompt you in the terminal to answer some further questions when running generators. | | NX_GENERATE_QUIET | boolean | If set to `true`, will prevent Nx logging file operations during generate | -| NX_PREFER_TS_NODE | boolean | If set to `true`, Nx will use ts-node for local execution of plugins even if `@swc-node/register` is installed. | +| NX_PREFER_TS_NODE | boolean | If set to `true`, Nx will use `ts-node` for local execution of plugins even if `@swc-node/register` is installed. | | NX_IGNORE_CYCLES | boolean | If set to `true`, Nx will ignore errors created by a task graph circular dependency. Can be overriden on the command line with `--nxIgnoreCycles` | | NX_BATCH_MODE | boolean | If set to `true`, Nx will run task(s) in batches for executors which support batches. | | NX_SKIP_LOG_GROUPING | boolean | If set to `true`, Nx will not group command's logs on CI. | +| NX_MIGRATE_CLI_VERSION | string | The version of Nx to use for running the `nx migrate` command. If not set, it defaults to `latest`. | Nx will set the following environment variables so they can be accessible within the process even outside of executors and generators. diff --git a/packages/nx/src/command-line/migrate/command-object.ts b/packages/nx/src/command-line/migrate/command-object.ts index 5bca8fec40cf3..e462a0276408f 100644 --- a/packages/nx/src/command-line/migrate/command-object.ts +++ b/packages/nx/src/command-line/migrate/command-object.ts @@ -131,13 +131,12 @@ function runMigration() { } function nxCliPath() { + const version = process.env.NX_MIGRATE_CLI_VERSION || 'latest'; try { const packageManager = getPackageManagerCommand(); const { dirSync } = require('tmp'); const tmpDir = dirSync().name; - const version = - process.env.NX_MIGRATE_USE_NEXT === 'true' ? 'next' : 'latest'; writeJsonFile(path.join(tmpDir, 'package.json'), { dependencies: { nx: version, @@ -157,7 +156,7 @@ function nxCliPath() { return path.join(tmpDir, `node_modules`, '.bin', 'nx'); } catch (e) { console.error( - 'Failed to install the latest version of the migration script. Using the current version.' + `Failed to install the ${version} version of the migration script. Using the current version.` ); if (process.env.NX_VERBOSE_LOGGING) { console.error(e); diff --git a/packages/nx/src/command-line/migrate/migrate.spec.ts b/packages/nx/src/command-line/migrate/migrate.spec.ts index 0407f27ae2ee2..cf1f4accb0416 100644 --- a/packages/nx/src/command-line/migrate/migrate.spec.ts +++ b/packages/nx/src/command-line/migrate/migrate.spec.ts @@ -1585,6 +1585,12 @@ describe('Migration', () => { targetPackage: 'nx', targetVersion: 'next', }); + expect( + await parseMigrationsOptions({ packageAndVersion: 'canary' }) + ).toMatchObject({ + targetPackage: 'nx', + targetVersion: 'canary', + }); expect( await parseMigrationsOptions({ packageAndVersion: '13.10.0' }) ).toMatchObject({ diff --git a/packages/nx/src/command-line/migrate/migrate.ts b/packages/nx/src/command-line/migrate/migrate.ts index 5365d28e3f2e5..af471728e719d 100644 --- a/packages/nx/src/command-line/migrate/migrate.ts +++ b/packages/nx/src/command-line/migrate/migrate.ts @@ -716,6 +716,7 @@ async function parseTargetPackageAndVersion( if ( args === 'latest' || args === 'next' || + args === 'canary' || valid(args) || args.match(/^\d+(?:\.\d+)?(?:\.\d+)?$/) ) { @@ -724,7 +725,8 @@ async function parseTargetPackageAndVersion( // on the registry const targetVersion = await normalizeVersionWithTagCheck('nx', args); const targetPackage = - !['latest', 'next'].includes(args) && lt(targetVersion, '14.0.0-beta.0') + !['latest', 'next', 'canary'].includes(args) && + lt(targetVersion, '14.0.0-beta.0') ? '@nrwl/workspace' : 'nx'; @@ -1177,7 +1179,7 @@ async function updateInstallationDetails( async function isMigratingToNewMajor(from: string, to: string) { from = normalizeVersion(from); - to = ['latest', 'next'].includes(to) ? to : normalizeVersion(to); + to = ['latest', 'next', 'canary'].includes(to) ? to : normalizeVersion(to); if (!valid(from)) { from = await resolvePackageVersionUsingRegistry('nx', from); }