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

\" doesn't work correctly in tasks.json #31722

Closed
imba-tjd opened this issue Jul 30, 2017 · 17 comments
Closed

\" doesn't work correctly in tasks.json #31722

imba-tjd opened this issue Jul 30, 2017 · 17 comments
Assignees
Labels
*duplicate Issue identified as a duplicate of another issue(s) tasks Task system issues
Milestone

Comments

@imba-tjd
Copy link
Contributor

imba-tjd commented Jul 30, 2017

My OS: Windows LTSB 2015 x64
VS Code Version: 1.14.2

When the path doesn't contain a space, it can be compiled normally.
When the path contains a space, I try to use \" in tasks.json to make the path a string, but it is useless. Just the same as no \".
Forget that red wavy line because I didn't configure c_cpp_properties.json.
image

@vscodebot vscodebot bot added the tasks Task system issues label Jul 30, 2017
@Lixire
Copy link
Contributor

Lixire commented Jul 31, 2017

Does it work if you move \"${file}\" to args:?

@imba-tjd
Copy link
Contributor Author

imba-tjd commented Aug 1, 2017

Nope, it has no change.

@dbaeumer dbaeumer added the bug Issue identified by VS Code Team member as probable bug label Aug 2, 2017
@dbaeumer dbaeumer added this to the August 2017 milestone Aug 2, 2017
@Lixire Lixire removed their assignment Aug 24, 2017
@dbaeumer
Copy link
Member

@imba-tjd which shell are you using. Cmd, PowerShell or bash ?

@imba-tjd
Copy link
Contributor Author

@dbaeumer powershell.
Besides, just now I type "terminal.external.windowsExec": "C:\\Windows\\System32\\cmd.exe in settings.json. Then I press ctrl+`, but it still shown powershell in terminal. Is this correct? = =

@dbaeumer
Copy link
Member

@imba-tjd the setting to control the integrated shell to use is: "terminal.integrated.shell.windows"

@dbaeumer dbaeumer added info-needed Issue requires more information from poster and removed bug Issue identified by VS Code Team member as probable bug labels Aug 31, 2017
@dbaeumer
Copy link
Member

@imba-tjd where is gcc installed ?

@imba-tjd
Copy link
Contributor Author

imba-tjd commented Aug 31, 2017

@dbaeumer Actually, I installed Clang 4.0.1 pre-build version for windows x64 and MinGW-w64 7.1.0. And merge mingw into clang's directory. So the path of gcc is c:\llvm\bin. My host system is 1703, virtual system is ltsb 2015. VS Code version is 1.15.1. I can still reproduce this problem by running task. But if I manually type the command in terminal, it works fine from the beginning.

But personally I don't think this issue is connected to any external program or extensions. Because even I create this program:

using System;

namespace PrintPara
{
    class Program
    {
        static void Main(string[] args)
        {
            foreach (var item in args)
            {
                Console.WriteLine(item);
            }
        }
    }
}

And in tasks.json I write:

{
    "version": "2.0.0",
    "tasks": [
        {
            "taskName": "Compile",
            "command": ".\\printpara.exe",
            "type": "shell",
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "args": [
                "\"${file}\""
            ]
        }
    ]
}

The result is still the same. It will split the argument by sapces and ignore quotes.
qq 20170831155237

@dbaeumer
Copy link
Member

dbaeumer commented Aug 31, 2017

This works for me with a simple dir command executed in cmd.exe. The example is

tasks.json

{
	// See https://go.microsoft.com/fwlink/?LinkId=733558
	// for the documentation about the tasks.json format
	"version": "2.0.0",
	"tasks": [
		{
			"taskName": "dir",
			"type": "shell",
			"command": "dir",
			"args": [
				"\"${file}\""
			]
		}
	]
}

Where $file is in a directory with space.

cast

However it doesn't work with PowerShell. @imba-tjd which shell are you using: cmd, PowerShell or same bash implementation?

@dbaeumer
Copy link
Member

@imba-tjd just saw that you are using PowerShell. Need to understand what makes this different under PowerShell.

@dbaeumer
Copy link
Member

You can configure the task to always run in cmd.exe since there the quoting with " works. Something like this:

{
	// See https://go.microsoft.com/fwlink/?LinkId=733558
	// for the documentation about the tasks.json format
	"version": "2.0.0",
	"tasks": [
		{
			"taskName": "dir",
			"type": "shell",
			"options": {
				"shell": {
					"executable": "C:\\WINDOWS\\System32\\cmd.exe",
					"args": [
						"/d", "/c"
					]
				}
			},
			"command": "dir",
			"args": [
				"\"${file}\""
			]
		}
	]
}

@imba-tjd
Copy link
Contributor Author

@dbaeumer Yeah, configuring like that doesn't have this problem. So it's clear the issue exists, right? I know little about powershell and vs code so I can't help much.
And I suggest the document should mention about adding \" if the command contains a blank after this issue is solved.

@dbaeumer
Copy link
Member

dbaeumer commented Aug 31, 2017

So I read about PowerShell and the right quoting for powershell is ' not ". Since we are open to the shell a user is using there is little VS Code can do to enforce the right quoting in case a argument contains spaces. So this works in PowerShell

{
	// See https://go.microsoft.com/fwlink/?LinkId=733558
	// for the documentation about the tasks.json format
	"version": "2.0.0",
	"tasks": [
		{
			"taskName": "dir",
			"type": "shell",
			"command": "dir '${file}'",
			"problemMatcher": []
		}
	]
}

In your concrete example which calls gcc which I assume is an executable you could even do the following:

{
	// See https://go.microsoft.com/fwlink/?LinkId=733558
	// for the documentation about the tasks.json format
	"version": "2.0.0",
	"tasks": [
		{
			"taskName": "Compile",
			"type": "process",
			"command": "gcc",
			"args": [
				"${file}"
			],
			"problemMatcher": []
		}
	]
}

Please note that this uses a task of type process that mean VS Code does start gcc directly without starting PowerShell first. This bypasses shell interpretation which is always complicated to escape if arguments have spaces.

I will keep the item open to see if there is more VS Code can do in terms of smartness or guiding the user to the rights quotes.

@imba-tjd
Copy link
Contributor Author

@dbaeumer OK, I konw it. Thank you.

@dbaeumer
Copy link
Member

Here is what we should do:

  • keep the idea that the user of the tasks.json should do the quoting since he knows best what to do.
  • if a variable is resolved (e.g. ${file}} and the resolved value contains a space we should escape the space. Only downside is that cmd.exe doesn't support space escaping.

@dbaeumer dbaeumer modified the milestones: On Deck, August 2017 Aug 31, 2017
@dbaeumer dbaeumer added bug Issue identified by VS Code Team member as probable bug and removed info-needed Issue requires more information from poster labels Aug 31, 2017
@DollarAkshay
Copy link

@dbaeumer Thank you. You are a hero

@dbaeumer dbaeumer modified the milestones: On Deck, March 2018 Mar 8, 2018
@dbaeumer
Copy link
Member

I added support that you now can control the quoting in task 2.0. You can now for example write something like this:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "echo",
            "type": "shell",
            "command": "dir",
            "args": [
                "fo lder"
            ],
            "problemMatcher": []
        }
    ]
}

In this case the task framework will quote the fo lder arg using the strong quoting rule of the shell used to execute the task. If users want to enforce a certain quoting they can do the following:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "echo",
            "type": "shell",
            "command": "echo",
            "args": [
                {
                    "value": "$(3 * 2)",
                    "quoting": "weak"
                }
            ],
            "problemMatcher": []
        }
    ]
}

which when executed in PowerShell prints 6

@dbaeumer
Copy link
Member

Marking as dup of #45576 since it contains the change set to fix this.

@dbaeumer dbaeumer added *duplicate Issue identified as a duplicate of another issue(s) and removed bug Issue identified by VS Code Team member as probable bug labels Mar 12, 2018
@vscodebot vscodebot bot locked and limited conversation to collaborators Apr 26, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
*duplicate Issue identified as a duplicate of another issue(s) tasks Task system issues
Projects
None yet
Development

No branches or pull requests

4 participants