Skip to content

fix: windows node spawn einval error #77

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

Merged
merged 2 commits into from
Sep 12, 2024
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
27 changes: 23 additions & 4 deletions packages/nx/src/utils/executors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import { resolve as nodeResolve } from 'path';
import { parse, build } from 'plist';
import { parseString, Builder } from 'xml2js';
import { readFileSync, writeFileSync } from 'fs-extra';
import { quoteString } from './helpers';

const isWindows = process.platform === 'win32';

export interface BuildExecutorSchema {
debug?: boolean;
Expand Down Expand Up @@ -126,7 +129,7 @@ export function commonExecutor(options: BuildExecutorSchema | TestExecutorSchema
}
}

const nsOptions = [];
let nsOptions = [];
if (isTesting) {
nsOptions.push('test');
}
Expand Down Expand Up @@ -228,7 +231,7 @@ export function commonExecutor(options: BuildExecutorSchema | TestExecutorSchema
};
// additional cli flags
// console.log('projectTargetCmdIndex:', projectTargetCmdIndex)
const additionalArgs = [];
let additionalArgs = [];
if (options.flags) {
// persisted flags in configurations
additionalArgs.push(...options.flags.split(' '));
Expand Down Expand Up @@ -259,6 +262,11 @@ export function commonExecutor(options: BuildExecutorSchema | TestExecutorSchema
icon = '🥽';
}
}
if (isWindows) {
// https://github.com/NativeScript/nativescript-cli/pull/5808
nsOptions = nsOptions.map((arg) => quoteString(arg));
additionalArgs = additionalArgs.map((arg) => quoteString(arg));
}
console.log(`―――――――――――――――――――――――― ${icon}`);
console.log(`Running NativeScript ${isTesting ? 'unit tests' : 'CLI'} within ${projectCwd}`);
console.log(' ');
Expand All @@ -272,6 +280,7 @@ export function commonExecutor(options: BuildExecutorSchema | TestExecutorSchema
const child = childProcess.spawn(/^win/.test(process.platform) ? 'ns.cmd' : 'ns', [...nsOptions, ...additionalArgs], {
cwd: projectCwd,
stdio: 'inherit',
shell: isWindows ? true : undefined,
});
child.on('close', (code) => {
console.log(`Done.`);
Expand All @@ -282,8 +291,13 @@ export function commonExecutor(options: BuildExecutorSchema | TestExecutorSchema

const checkAppId = function () {
return new Promise((resolve) => {
const child = childProcess.spawn(/^win/.test(process.platform) ? 'ns.cmd' : 'ns', ['config', 'get', `id`], {
let args = ['config', 'get', `id`];
if (isWindows) {
args = args.map((arg) => quoteString(arg));
}
const child = childProcess.spawn(/^win/.test(process.platform) ? 'ns.cmd' : 'ns', args, {
cwd: projectCwd,
shell: isWindows ? true : undefined,
});
child.stdout.setEncoding('utf8');
child.stdout.on('data', function (data) {
Expand All @@ -304,9 +318,14 @@ export function commonExecutor(options: BuildExecutorSchema | TestExecutorSchema
checkAppId().then((id) => {
if (options.id !== id) {
// set custom app bundle id before running the app
const child = childProcess.spawn(/^win/.test(process.platform) ? 'ns.cmd' : 'ns', ['config', 'set', `${options.platform}.id`, options.id], {
let args = ['config', 'set', `${options.platform}.id`, options.id];
if (isWindows) {
args = args.map((arg) => quoteString(arg));
}
const child = childProcess.spawn(/^win/.test(process.platform) ? 'ns.cmd' : 'ns', args, {
cwd: projectCwd,
stdio: 'inherit',
shell: isWindows ? true : undefined,
});
child.on('close', (code) => {
child.kill('SIGKILL');
Expand Down
26 changes: 26 additions & 0 deletions packages/nx/src/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -733,3 +733,29 @@ export namespace PluginFeatureHelpers {
return moveTo;
}
}

// Copied from: https://github.com/NativeScript/nativescript-cli/blob/16064affee98c837e8cbe0865254dcb5b81f0bbe/lib/common/helpers.ts#L246C1-L268C2
// For https://github.com/NativeScript/nativescript-cli/pull/5808
function bashQuote(s: string): string {
if (s[0] === "'" && s[s.length - 1] === "'") {
return s;
}
// replace ' with '"'"' and wrap in ''
return "'" + s.replace(/'/g, "'\"'\"'") + "'";
}

function cmdQuote(s: string): string {
if (s[0] === '"' && s[s.length - 1] === '"') {
return s;
}
// replace " with \" and wrap in ""
return '"' + s.replace(/"/g, '\\"') + '"';
}

export function quoteString(s: string): string {
if (!s) {
return s;
}

return process.platform === 'win32' ? cmdQuote(s) : bashQuote(s);
}