Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 31 additions & 12 deletions packages/nx/src/command-line/migrate/migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { exec, execSync, type StdioOptions } from 'child_process';
import { prompt } from 'enquirer';
import { handleImport } from '../../utils/handle-import';
import { dirname, join } from 'path';
import { createRequire } from 'module';
import { joinPathFragments } from '../../utils/path';
import {
clean,
Expand Down Expand Up @@ -898,20 +899,46 @@ function createInstalledPackageVersionsResolver(
root: string
): MigratorOptions['getInstalledPackageVersion'] {
const cache: Record<string, string> = {};
const nxRequires = getNxRequirePaths(root).map((path) =>
createRequire(join(path, 'package.json'))
);

function getInstalledPackageVersion(
packageName: string,
overrides?: Record<string, string>
): string | null {
try {
if (overrides?.[packageName]) {
return overrides[packageName];
if (overrides?.[packageName]) {
return overrides[packageName];
}

if (packageName === 'nx') {
const nxVersion =
cache[packageName] ??
(() => {
for (const req of nxRequires) {
try {
const packageJsonPath = req.resolve('nx/package.json');
if (packageJsonPath.startsWith(workspaceRoot)) {
return readJsonFile<PackageJson>(packageJsonPath).version;
}
} catch {}
}

return getInstalledPackageVersion('@nrwl/workspace', overrides);
})();

if (nxVersion) {
cache[packageName] = nxVersion;
}

return nxVersion;
}

try {
if (!cache[packageName]) {
const { packageJson, path } = readModulePackageJson(
packageName,
getNxRequirePaths()
getNxRequirePaths(root)
);
// old workspaces would have the temp installation of nx in the cache,
// so the resolved package is not the one we need
Expand All @@ -923,14 +950,6 @@ function createInstalledPackageVersionsResolver(

return cache[packageName];
} catch {
// Support migrating old workspaces without nx package
if (packageName === 'nx') {
cache[packageName] = getInstalledPackageVersion(
'@nrwl/workspace',
overrides
);
return cache[packageName];
}
return null;
}
}
Expand Down
3 changes: 2 additions & 1 deletion packages/nx/src/utils/provenance.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { execSync } from 'child_process';
import { createRequire } from 'module';
import { join } from 'path';
import { promisify } from 'util';
import { readJsonFile } from './fileutils';
Expand Down Expand Up @@ -168,7 +169,7 @@ export class ProvenanceError extends Error {
}

export function getNxPackageGroup(): string[] {
const packageJsonPath = join(__dirname, '../../package.json');
const packageJsonPath = createRequire(__filename).resolve('nx/package.json');
const packageJson = readJsonFile(packageJsonPath);

if (!packageJson['nx-migrations']?.packageGroup) {
Expand Down
Loading