问题
I'm configuring a VSCode task in tasks.json, and I need to pass the ${workspaceFolder} to a 'make' command, however it needs to be forward slashes, not back slashes.
{
"version": "2.0.0",
"echoCommand": true,
"tasks": [
{
"label": "build",
"type": "shell",
"group": {
"kind": "build",
"isDefault": true
},
"command": "make",
"args": [
"APPDIR=\"${workspaceFolder}\""
]
. . .
Is there any way to modify ${workspaceFolder} to emit forward slashes on Windows? Or, is there such a thing as a macro, where I can search and replace?
EDIT: My root issue is that GNU make seems to escape the backslashes incoming from APPDIR, for example: C:\somedirectory\someotherdirectory\athirddirectory. I thought if I could switch to forward slashes, it would fix the issue. I have no control over, and cannot edit, the make file.
Thanks
-John
回答1:
Although not explicitly stated, it sounds like you're on Windows and using Cygwin make.
Basically using Johan's suggestion, here is a complete tasks.json that uses cygpath -m to pass forward slashes to make:
{
"version": "2.0.0",
"tasks": [
{
"label": "build hello world",
"type": "shell",
"command": "d:/cygwin64/bin/sh",
"args": [
"-c",
"make APPDIR=$(cygpath -m '${workspaceFolder}')"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
And here is a sample Makefile to be called:
$(info APPDIR is "$(APPDIR)")
helloworld.exe: helloworld.cpp
g++ -g -Wall -std=c++11 -o $@ helloworld.cpp
When I press Ctrl+Shift+B to invoke this task, I see in the Terminal window:
> Executing task in folder cpphello: d:/cygwin64/bin/sh -c "make APPDIR=$(cygpath -m 'D:\wrk\learn\vscode\cpphello')" <
APPDIR is "D:/wrk/learn/vscode/cpphello"
make: 'helloworld.exe' is up to date.
Terminal will be reused by tasks, press any key to close it.
This uses the -m (mixed) switch to cygpath to get what looks like a Windows path but using forward slashes. cygpath has other options; see cygpath --help.
Two subtleties here:
I specify the path to
shexplicitly. That is because I also have git for Windows on my $PATH, and it comes before my Cygwin path so that vscode will use thatgit. But git for Windows also hassh.exe, and if that one is used here,makeblows up with a Cygwin DLL error.I had to change the default VSCode shell to
cmd.exe, whereas the default ispowershell.exe. The problem with powershell here is that VSCode uses single quotes when passing arguments to it, whereas my solution requires that VSCode use double-quotes, which it does withcmd.exe. To change the shell, use the "Terminal: Select Default Shell" command from the palette (Ctrl+Shift+P).
Finally, I'll note that all of this nonsense can be avoided if, in your situation, you can create an intermediate bash shell script rather than invoking make directly. The tasks.json language is not very powerful, and the quirky shells VSCode knows how to invoke on Windows (namely cmd.exe and powershell.exe) add extra complexity and fragility.
来源:https://stackoverflow.com/questions/47250021/want-workspacefolder-to-emit-forward-slashes-on-windows