Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): use fork to execute nx generate workspace:preset #29122

Merged
merged 3 commits into from
Dec 3, 2024
Merged
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
36 changes: 23 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,8 @@ 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';
import { getNxRequirePaths } from 'nx/src/utils/installation-directory';

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

const newWorkspaceRoot = join(host.root, opts.directory);
const forkOptions: ForkOptions = {
stdio: 'inherit',
shell: true,
cwd: join(host.root, opts.directory),
windowsHide: false,
cwd: newWorkspaceRoot,
};
const pmc = getPackageManagerCommand();
const executable = `${pmc.exec} nx`;
const nxInstallationPaths = getNxRequirePaths(newWorkspaceRoot);
const nxBinForNewWorkspaceRoot = require.resolve('nx/bin/nx', {
paths: nxInstallationPaths,
});
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, forkOptions).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
Loading