diff --git a/.chronus/changes/fix-install-vs-extension-2024-6-3-17-17-1.md b/.chronus/changes/fix-install-vs-extension-2024-6-3-17-17-1.md new file mode 100644 index 0000000000..8844b87943 --- /dev/null +++ b/.chronus/changes/fix-install-vs-extension-2024-6-3-17-17-1.md @@ -0,0 +1,7 @@ +--- +changeKind: fix +packages: + - "@typespec/compiler" +--- + +Fix 'typespec vs install' command on windows \ No newline at end of file diff --git a/packages/compiler/src/core/cli/utils.ts b/packages/compiler/src/core/cli/utils.ts index 7d4ada8310..089a6f6bab 100644 --- a/packages/compiler/src/core/cli/utils.ts +++ b/packages/compiler/src/core/cli/utils.ts @@ -83,14 +83,23 @@ export function run( command += ".cmd"; } + const useShell = process.platform === "win32"; + const hasSpace = command.includes(" "); + let commandToSpawn = command; + if (useShell && hasSpace) { + // for command having space (which should be a path), we need to wrap it into " for it to be executed properly in shell + // but for short command like 'npm', we shouldn't wrap it which would trigger error + commandToSpawn = `"${command}"`; + logger.trace(`Command to spawn updated to: ${commandToSpawn}\n`); + } const finalOptions: SpawnSyncOptionsWithStringEncoding = { encoding: "utf-8", stdio: "inherit", - shell: process.platform === "win32", + shell: useShell, ...(options ?? {}), }; - const proc = spawnSync(command, commandArgs, finalOptions); + const proc = spawnSync(commandToSpawn, commandArgs, finalOptions); logger.trace(inspect(proc, { depth: null })); if (proc.error) {