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

Use shell for execution of .cmd (tsp-server.cmd) #4430

Merged
merged 3 commits into from
Sep 20, 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
7 changes: 7 additions & 0 deletions .chronus/changes/use-shell-for-cmd-2024-8-13-11-39-59.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: fix
packages:
- typespec-vscode
---

Use "shell" when spawning execution of .cmd file(i.e. tsp-server.cmd) in windows
10 changes: 5 additions & 5 deletions packages/typespec-vscode/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from "vscode-languageclient/node.js";
import logger from "./extension-logger.js";
import { TypeSpecLogOutputChannel } from "./typespec-log-output-channel.js";
import { normalizeSlash } from "./utils.js";
import { normalizeSlash, useShellInExec } from "./utils.js";

let client: LanguageClient | undefined;
/**
Expand Down Expand Up @@ -233,7 +233,7 @@ async function resolveTypeSpecCli(absoluteTargetPath: string): Promise<Executabl
logger.debug(
`Can't resolve compiler path for tsp task, try to use default value ${executable}.`
);
return { command: executable, args: [], options };
return useShellInExec({ command: executable, args: [], options });
} else {
logger.debug(`Compiler path resolved as: ${compilerPath}`);
const jsPath = join(compilerPath, "cmd/tsp.js");
Expand Down Expand Up @@ -281,7 +281,7 @@ async function resolveTypeSpecServer(context: ExtensionContext): Promise<Executa
if (!serverPath) {
const executable = process.platform === "win32" ? "tsp-server.cmd" : "tsp-server";
logger.debug(`Can't resolve server path, try to use default value ${executable}.`);
return { command: executable, args, options };
return useShellInExec({ command: executable, args, options });
}
const variableResolver = new VSCodeVariableResolver({
workspaceFolder,
Expand All @@ -297,9 +297,9 @@ async function resolveTypeSpecServer(context: ExtensionContext): Promise<Executa
const command =
process.platform === "win32" && !serverPath.endsWith(".cmd")
? `${serverPath}.cmd`
: "tsp-server";
: serverPath;
RodgeFu marked this conversation as resolved.
Show resolved Hide resolved

return { command, args, options };
return useShellInExec({ command, args, options });
} else {
serverPath = join(serverPath, "cmd/tsp-server.js");
}
Expand Down
22 changes: 22 additions & 0 deletions packages/typespec-vscode/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,26 @@
import { Executable } from "vscode-languageclient/node.js";

/** normalize / and \\ to / */
export function normalizeSlash(str: string): string {
return str.replaceAll(/\\/g, "/");
}

/**
*
* @param exe
* @param win32Only only use Shell when the process.platform is "win32"
RodgeFu marked this conversation as resolved.
Show resolved Hide resolved
* @returns
*/
export function useShellInExec(exe: Executable, win32Only: boolean = true): Executable {
if (!win32Only || process.platform === "win32") {
if (exe.options) {
exe.options.shell = true;
} else {
exe.options = { shell: true };
}
if (exe.command.includes(" ")) {
exe.command = `"${exe.command}"`;
}
}
return exe;
}
Loading