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
Press \/ to change every backslash to a forward slash, in the current line. Press \\ to change every forward slash to a backslash, in the current line.
You can use forward slashes ( / ) instead of backward slashes ( \ ) on Windows (as is the case with Linux® and UNIX). If you use backward-slashes, the file name on Windows might be treated as a relative-path instead of an absolute-path.
Settings editor# To open the Settings editor, use the following VS Code menu command: On Windows/Linux - File > Preferences > Settings. On macOS - Code > Preferences > Settings.
Use variable extension.commandvariable.workspace.workspaceFolderPosix
from extension command-variable where you need ${workspaceFolder}
with forward slashes. There are also other useful substitutions in this extension.
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 sh
explicitly. That is because I also
have git for Windows on my $PATH,
and it comes before my Cygwin path so that vscode will use that
git
. But git for Windows also has sh.exe
, and if that one
is used here, make
blows up with a Cygwin DLL error.
I had to change the default VSCode shell to cmd.exe
, whereas
the default is powershell.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 with cmd.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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With