Skip to content

Commit

Permalink
fix(core): use fork to execute nx generate workspace:preset
Browse files Browse the repository at this point in the history
  • Loading branch information
Coly010 committed Dec 2, 2024
1 parent ec5a5e6 commit ef6a0e4
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions packages/workspace/src/generators/new/generate-preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { getNpmPackageVersion } from '../utils/get-npm-package-version';
import { NormalizedSchema } from './new';
import { join } from 'path';
import * as yargsParser from 'yargs-parser';
import { spawn, SpawnOptions } from 'child_process';
import { fork, ForkOptions } from 'child_process';

export function addPresetDependencies(host: Tree, options: NormalizedSchema) {
const { dependencies, dev } = getPresetDependencies(options);
Expand All @@ -32,25 +32,33 @@ export function generatePreset(host: Tree, opts: NormalizedSchema) {
interactive: true,
},
});
const spawnOptions: SpawnOptions = {

const newWorkspaceRoot = join(host.root, opts.directory);
const spawnOptions: ForkOptions = {
stdio: 'inherit',
shell: true,
cwd: join(host.root, opts.directory),
windowsHide: false,
cwd: newWorkspaceRoot,
};
const pmc = getPackageManagerCommand();
const executable = `${pmc.exec} nx`;
const nxBinForNewWorkspaceRoot = require.resolve('nx/bin/nx', {
paths: [join(newWorkspaceRoot, 'node_modules')],
});
const args = getPresetArgs(opts);

return new Promise<void>((resolve, reject) => {
spawn(executable, args, spawnOptions).on('close', (code: number) => {
if (code === 0) {
resolve();
} else {
const message = 'Workspace creation failed, see above.';
reject(new Error(message));
// This needs to be `fork` instead of `spawn` because `spawn` is failing on Windows with pnpm + yarn
// The root cause is unclear. Spawn causes the `@nx/workspace:preset` generator to be called twice
// and the second time it fails with `Project {projectName} already exists.`
fork(nxBinForNewWorkspaceRoot, args, spawnOptions).on(
'close',
(code: number) => {
if (code === 0) {
resolve();
} else {
const message = 'Workspace creation failed, see above.';
reject(new Error(message));
}
}
});
);
});

function getPresetArgs(options: NormalizedSchema) {
Expand Down

0 comments on commit ef6a0e4

Please sign in to comment.