11

I've been using VS Code, and I was wondering how to build a task.json file that will have these commands. cargo build, cargo run [ARGS] cargo run --release -- [ARGS]

I've tried making one with the documentation on task.json. I kept getting No such subcommand errors.

Sample:

{
"version": "0.1.0",
// The command is tsc. Assumes that tsc has been installed using npm install -g typescript
"command": "cargo",

// The command is a shell script
"isBuildCommand": true,

// Show the output window only if unrecognized errors occur. 
"showOutput": "silent",

"tasks": [{
   "taskName": "run test",
   "version": "0.1.0",
   "command": "run -- --exclude-dir=node_modules C:/Users/Aaron/Documents/Github/",
   "isShellCommand": true,
   "showOutput": "always"
},
{
   "taskName": "run",
   "version": "0.1.0",
   "args": [  "--"
           , "--exclude-dir=node_modules"
           , "C:/Users/Aaron/Documents/Github/"
           ]
   "isShellCommand": true,
   "showOutput": "always"
}]
}
2

3 Answers 3

3

Possibly these answers are out of date, here is my tasks.json, which implements cargo run, cargo build and a cargo command to run the currently open example...

A key thing is to specify the problem matcher, so you can click through errors:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "cargo run example",
            "type": "shell",
            "command": "cargo run --example ${fileBasenameNoExtension}",
            "problemMatcher": [
                "$rustc"
            ]
        },
        {
            "label": "cargo run",
            "type": "shell",
            "command": "cargo run",
            "problemMatcher": [
                "$rustc"
            ]
        },
        {
            "label": "cargo build",
            "type": "shell",
            "command": "cargo build",
            "problemMatcher": [
                "$rustc"
            ]
        },
    ]
}
2

The command property is only supported at the top level. In addition, arguments have to be passed via the args property. If they are put into the command, the command is treated as a command with whitespaces in its name. An example of the run task would look like this:

{
    "version": "0.1.0",
    "command": "cargo",
    "isShellCommand": true, // Only needed if cargo is a .cmd file
    "tasks": [
        {
           "taskName": "run",
           "args": [
               "--release"
               // More args
           ],
           "showOutput": "always"
        }
    ]
}
1

Here is how I configured my tasks.json file

{
    "version": "0.1.0",
    "command": "cargo",
    "isShellCommand": true,
    "args": ["run"],
    "showOutput": "always"
}

Entering the build command (ctrl+shift+b) will build and run the code.

1
  • Curious if this still works for anyone or is out of date. On my system it just produces a blank error: message
    – fostandy
    May 15, 2019 at 14:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.