Want ${workspaceFolder} to emit forward slashes on Windows?
Asked Answered
G

2

8

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

Gagliano answered 12/11, 2017 at 14:36 Comment(3)
same pb, my script work on linux and mac but not with windowsRepp
You probably have the cygpath and sh commands available. I would try replacing make APPDIR=xxx with sh -c "make APPDIR=$(cygpath 'xxx')". Beware of the tricky quotes!Isreal
This might be your answer.Crocodilian
T
1

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.

Tirrell answered 1/7, 2020 at 7:49 Comment(1)
The command-variable extension worked for me. Use ${command:extension.commandvariable.workspace.folder} since extension.commandvariable.workspace.workspaceFolderPosix is already deprecated.Elegiac
S
3

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:

  1. 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.

  2. 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.

Strati answered 30/8, 2019 at 17:26 Comment(3)
It seems like it doesn't work any more, because VSCode put own quotes into the command: Executing task: c:/cygwin64/bin/sh -c 'make APPDIR=$(cygpath -m 'C:\work\project')' C:\work\project): -c: line 0: unexpected EOF while looking for matching )' C:\work\project): -c: line 1: syntax error: unexpected end of file` With double quotes it doesn't work either.Marking
@Marking Did you change your shell to cmd.exe? What version of VSCode are you using?Strati
You're right. I missed the explanation about changing shell from PS to CMD. Thanks, it works now.Marking
T
1

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.

Tirrell answered 1/7, 2020 at 7:49 Comment(1)
The command-variable extension worked for me. Use ${command:extension.commandvariable.workspace.folder} since extension.commandvariable.workspace.workspaceFolderPosix is already deprecated.Elegiac

© 2022 - 2024 — McMap. All rights reserved.