Skip to content

Commit

Permalink
fix(core): ensurePackage fails on pnpm workspaces (#16002)
Browse files Browse the repository at this point in the history
(cherry picked from commit b3a3f2a)
  • Loading branch information
meeroslav authored and FrozenPandaz committed Apr 11, 2023
1 parent 926908e commit 0867dba
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 73 deletions.
8 changes: 5 additions & 3 deletions e2e/utils/command-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ import { ChildProcess, exec, execSync, ExecSyncOptions } from 'child_process';
import { join } from 'path';
import * as isCI from 'is-ci';
import { Workspaces } from '../../packages/nx/src/config/workspaces';
import { updateFile } from './file-utils';
import { exists, updateFile } from './file-utils';
import { logError, stripConsoleColors } from './log-utils';
import { existsSync } from 'fs-extra';

export interface RunCmdOpts {
silenceError?: boolean;
Expand Down Expand Up @@ -118,6 +119,7 @@ export function getPackageManagerCommand({
} {
const npmMajorVersion = getNpmMajorVersion();
const publishedVersion = getPublishedVersion();
const isPnpmWorkspace = existsSync(join(path, 'pnpm-workspace.yaml'));

return {
npm: {
Expand Down Expand Up @@ -158,8 +160,8 @@ export function getPackageManagerCommand({
runUninstalledPackage: 'pnpm dlx',
install: 'pnpm i',
ciInstall: 'pnpm install --frozen-lockfile',
addProd: `pnpm add`,
addDev: `pnpm add -D`,
addProd: isPnpmWorkspace ? 'pnpm add -w' : 'pnpm add',
addDev: isPnpmWorkspace ? 'pnpm add -Dw' : 'pnpm add -D',
list: 'npm ls --depth 10',
runLerna: `pnpm exec lerna`,
},
Expand Down
4 changes: 2 additions & 2 deletions e2e/workspace-create-npm/jest.config.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/* eslint-disable */
export default {
transform: {
'^.+\\.[tj]sx?$': 'ts-jest',
'^.+\\.[tj]sx?$': ['ts-jest', { tsconfig: '<rootDir>/tsconfig.spec.json' }],
},
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'html'],
maxWorkers: 1,
globals: { 'ts-jest': { tsconfig: '<rootDir>/tsconfig.spec.json' } },
globals: {},
displayName: 'e2e-workspace-create-npm',
preset: '../../jest.preset.js',
};
100 changes: 36 additions & 64 deletions e2e/workspace-create-npm/src/create-nx-workspace-npm.test.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,48 @@
import {
checkFilesExist,
cleanupProject,
getSelectedPackageManager,
packageInstall,
readJson,
runCLI,
runCommand,
runCreateWorkspace,
uniq,
} from '@nrwl/e2e/utils';

describe('create-nx-workspace --preset=npm', () => {
let wsName;
const wsName = uniq('npm');

let orginalGlobCache;

beforeAll(() => {
orginalGlobCache = process.env.NX_PROJECT_GLOB_CACHE;
// glob cache is causing previous projects to show in Workspace for maxWorkers overrides
// which fails due to files no longer being available
process.env.NX_PROJECT_GLOB_CACHE = 'false';

beforeEach(() => {
wsName = uniq('npm');
runCreateWorkspace(wsName, {
preset: 'npm',
packageManager: getSelectedPackageManager(),
});
});

afterEach(() => cleanupProject());
afterEach(() => {
// cleanup previous projects
runCommand(`rm -rf packages/** tsconfig.base.json`);
});

afterAll(() => {
process.env.NX_PROJECT_GLOB_CACHE = orginalGlobCache;
cleanupProject({ skipReset: true });
});

it('should add angular application', () => {
packageInstall('@nrwl/angular', wsName);
const appName = uniq('my-app');

expect(() => {
runCLI(
`generate @nrwl/angular:app ${appName} --skipPackageJson --no-interactive`
);
runCLI(`generate @nrwl/angular:app ${appName} --no-interactive`);
}).not.toThrowError();
checkFilesExist('tsconfig.base.json');
}, 1_000_000);
Expand All @@ -37,9 +52,7 @@ describe('create-nx-workspace --preset=npm', () => {
const libName = uniq('lib');

expect(() => {
runCLI(
`generate @nrwl/angular:lib ${libName} --skipPackageJson --no-interactive`
);
runCLI(`generate @nrwl/angular:lib ${libName} --no-interactive`);
}).not.toThrowError();
checkFilesExist('tsconfig.base.json');
const tsconfig = readJson(`tsconfig.base.json`);
Expand All @@ -48,32 +61,13 @@ describe('create-nx-workspace --preset=npm', () => {
});
}, 1_000_000);

it('should add workspace library', () => {
packageInstall('@nrwl/workspace', wsName);

const libName = uniq('lib');

expect(() =>
runCLI(
`generate @nrwl/workspace:library ${libName} --skipPackageJson --no-interactive`
)
).not.toThrowError();
checkFilesExist('tsconfig.base.json');
const tsconfig = readJson(`tsconfig.base.json`);
expect(tsconfig.compilerOptions.paths).toEqual({
[libName]: [`packages/${libName}/src/index.ts`],
});
});

it('should add js library', () => {
packageInstall('@nrwl/js', wsName);

const libName = uniq('lib');

expect(() =>
runCLI(
`generate @nrwl/js:library ${libName} --skipPackageJson --no-interactive`
)
runCLI(`generate @nrwl/js:library ${libName} --no-interactive`)
).not.toThrowError();
checkFilesExist('tsconfig.base.json');
const tsconfig = readJson(`tsconfig.base.json`);
Expand All @@ -88,9 +82,7 @@ describe('create-nx-workspace --preset=npm', () => {
const appName = uniq('my-app');

expect(() =>
runCLI(
`generate @nrwl/web:app ${appName} --skipPackageJson --no-interactive`
)
runCLI(`generate @nrwl/web:app ${appName} --no-interactive`)
).not.toThrowError();
checkFilesExist('tsconfig.base.json');
});
Expand All @@ -101,9 +93,7 @@ describe('create-nx-workspace --preset=npm', () => {
const appName = uniq('my-app');

expect(() => {
runCLI(
`generate @nrwl/react:app ${appName} --skipPackageJson --no-interactive`
);
runCLI(`generate @nrwl/react:app ${appName} --no-interactive`);
}).not.toThrowError();
checkFilesExist('tsconfig.base.json');
});
Expand All @@ -114,9 +104,7 @@ describe('create-nx-workspace --preset=npm', () => {
const libName = uniq('lib');

expect(() => {
runCLI(
`generate @nrwl/react:lib ${libName} --skipPackageJson --no-interactive`
);
runCLI(`generate @nrwl/react:lib ${libName} --no-interactive`);
}).not.toThrowError();
checkFilesExist('tsconfig.base.json');
const tsconfig = readJson(`tsconfig.base.json`);
Expand All @@ -131,9 +119,7 @@ describe('create-nx-workspace --preset=npm', () => {
const appName = uniq('my-app');

expect(() => {
runCLI(
`generate @nrwl/next:app ${appName} --skipPackageJson --no-interactive`
);
runCLI(`generate @nrwl/next:app ${appName} --no-interactive`);
}).not.toThrowError();
checkFilesExist('tsconfig.base.json');
});
Expand All @@ -144,9 +130,7 @@ describe('create-nx-workspace --preset=npm', () => {
const libName = uniq('lib');

expect(() => {
runCLI(
`generate @nrwl/next:lib ${libName} --skipPackageJson --no-interactive`
);
runCLI(`generate @nrwl/next:lib ${libName} --no-interactive`);
}).not.toThrowError();
checkFilesExist('tsconfig.base.json');
const tsconfig = readJson(`tsconfig.base.json`);
Expand All @@ -162,7 +146,7 @@ describe('create-nx-workspace --preset=npm', () => {

expect(() => {
runCLI(
`generate @nrwl/react-native:app ${appName} --install=false --skipPackageJson --no-interactive`
`generate @nrwl/react-native:app ${appName} --install=false --no-interactive`
);
}).not.toThrowError();
checkFilesExist('tsconfig.base.json');
Expand All @@ -174,9 +158,7 @@ describe('create-nx-workspace --preset=npm', () => {
const libName = uniq('lib');

expect(() => {
runCLI(
`generate @nrwl/react-native:lib ${libName} --skipPackageJson --no-interactive`
);
runCLI(`generate @nrwl/react-native:lib ${libName} --no-interactive`);
}).not.toThrowError();
checkFilesExist('tsconfig.base.json');
const tsconfig = readJson(`tsconfig.base.json`);
Expand All @@ -191,9 +173,7 @@ describe('create-nx-workspace --preset=npm', () => {
const appName = uniq('my-app');

expect(() => {
runCLI(
`generate @nrwl/node:app ${appName} --skipPackageJson --no-interactive`
);
runCLI(`generate @nrwl/node:app ${appName} --no-interactive`);
}).not.toThrowError();
checkFilesExist('tsconfig.base.json');
});
Expand All @@ -204,9 +184,7 @@ describe('create-nx-workspace --preset=npm', () => {
const libName = uniq('lib');

expect(() => {
runCLI(
`generate @nrwl/node:lib ${libName} --skipPackageJson --no-interactive`
);
runCLI(`generate @nrwl/node:lib ${libName} --no-interactive`);
}).not.toThrowError();
checkFilesExist('tsconfig.base.json');
const tsconfig = readJson(`tsconfig.base.json`);
Expand All @@ -221,9 +199,7 @@ describe('create-nx-workspace --preset=npm', () => {
const appName = uniq('my-app');

expect(() => {
runCLI(
`generate @nrwl/nest:app ${appName} --skipPackageJson --no-interactive`
);
runCLI(`generate @nrwl/nest:app ${appName} --no-interactive`);
}).not.toThrowError();
checkFilesExist('tsconfig.base.json');
});
Expand All @@ -234,9 +210,7 @@ describe('create-nx-workspace --preset=npm', () => {
const libName = uniq('lib');

expect(() => {
runCLI(
`generate @nrwl/nest:lib ${libName} --skipPackageJson --no-interactive`
);
runCLI(`generate @nrwl/nest:lib ${libName} --no-interactive`);
}).not.toThrowError();
checkFilesExist('tsconfig.base.json');
const tsconfig = readJson(`tsconfig.base.json`);
Expand All @@ -251,9 +225,7 @@ describe('create-nx-workspace --preset=npm', () => {
const appName = uniq('my-app');

expect(() => {
runCLI(
`generate @nrwl/express:app ${appName} --skipPackageJson --no-interactive`
);
runCLI(`generate @nrwl/express:app ${appName} --no-interactive`);
}).not.toThrowError();
checkFilesExist('tsconfig.base.json');
});
Expand Down
20 changes: 16 additions & 4 deletions packages/devkit/src/utils/package-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@ import { requireNx } from '../../nx';
import { dirSync } from 'tmp';
import { join } from 'path';

const { readJson, updateJson, getPackageManagerCommand, workspaceRoot } =
requireNx();
const {
readJson,
updateJson,
getPackageManagerCommand,
workspaceRoot,
detectPackageManager,
} = requireNx();

const UNIDENTIFIED_VERSION = 'UNIDENTIFIED_VERSION';
const NON_SEMVER_TAGS = {
Expand Down Expand Up @@ -448,9 +453,16 @@ export function ensurePackage<T extends any = any>(
const tempDir = dirSync().name;

console.log(`Fetching ${pkg}...`);
execSync(`${getPackageManagerCommand().addDev} ${pkg}@${requiredVersion}`, {
const packageManager = detectPackageManager();
let addCommand = getPackageManagerCommand(packageManager).addDev;
if (packageManager === 'pnpm') {
addCommand = 'pnpm add -D'; // we need to ensure that we are not using workspace command
}

const isVerbose = process.env.NX_VERBOSE_LOGGING === 'true';
execSync(`${addCommand} ${pkg}@${requiredVersion}`, {
cwd: tempDir,
stdio: 'ignore',
stdio: isVerbose ? 'inherit' : 'ignore',
});

addToNodePath(join(workspaceRoot, 'node_modules'));
Expand Down

0 comments on commit 0867dba

Please sign in to comment.