Skip to content

Instantly share code, notes, and snippets.

@deadalusai
Last active June 7, 2025 14:53
Show Gist options
  • Select an option

  • Save deadalusai/9e13e36d61ec7fb72148 to your computer and use it in GitHub Desktop.

Select an option

Save deadalusai/9e13e36d61ec7fb72148 to your computer and use it in GitHub Desktop.
VS Code tasks.json for Rust/Cargo
/*
Before using this tasks.json you may like to consider trying one of the following VS Code extensions:
rust-analyzer
Marketplace: https://marketplace.visualstudio.com/items?itemName=matklad.rust-analyzer
Source: https://github.com/rust-analyzer/rust-analyzer
rls
Marketplace: https://marketplace.visualstudio.com/items?itemName=rust-lang.rust
Source: https://github.com/rust-lang/rls-vscode
*/
// Available variables which can be used inside of strings.
// ${workspaceRoot}: the root folder of the team
// ${file}: the current opened file
// ${fileBasename}: the current opened file's basename
// ${fileDirname}: the current opened file's dirname
// ${fileExtname}: the current opened file's extension
// ${cwd}: the current working directory of the spawned process
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"args": ["build", "-v"],
"command": "cargo",
"group": "build",
"problemMatcher": [
{
"owner": "rust",
"fileLocation": ["relative", "${workspaceRoot}"],
"pattern": {
"regexp": "^(.*):(\\d+):(\\d+):\\s+(\\d+):(\\d+)\\s+(warning|error):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"endLine": 4,
"endColumn": 5,
"severity": 6,
"message": 7
}
}
]
},
{
"label": "clean",
"args": ["clean"],
"command": "cargo",
"presentation": {
"reveal": "always"
}
},
{
"label": "run",
"args": ["run", "-v"],
"command": "cargo",
"presentation": {
"reveal": "always"
},
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": [
{
"owner": "rust",
"fileLocation": ["relative", "${workspaceRoot}"],
"pattern": {
"regexp": "^(.*):(\\d+):(\\d+):\\s+(\\d+):(\\d+)\\s+(warning|error):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"endLine": 4,
"endColumn": 5,
"severity": 6,
"message": 7
}
}
]
},
{
"label": "test",
"args": ["test"],
"command": "cargo",
"presentation": {
"reveal": "always"
},
"group": {
"kind": "z",
"isDefault": false
},
"problemMatcher": [
{
"owner": "rust",
"fileLocation": ["relative", "${workspaceRoot}"],
"pattern": {
"regexp": "^(.*):(\\d+):(\\d+):\\s+(\\d+):(\\d+)\\s+(warning|error):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"endLine": 4,
"endColumn": 5,
"severity": 6,
"message": 7
}
},
{
"owner": "rust",
"fileLocation": ["relative", "${workspaceRoot}"],
"severity": "error",
"pattern": {
"regexp": "^.*panicked\\s+at\\s+'(.*)',\\s+(.*):(\\d+)$",
"message": 1,
"file": 2,
"line": 3
}
}
]
},
{
"label": "bench",
"args": ["bench"],
"command": "cargo",
"presentation": {
"reveal": "always"
},
"group": "test",
"problemMatcher": [
{
"owner": "rust",
"fileLocation": ["relative", "${workspaceRoot}"],
"pattern": {
"regexp": "^(.*):(\\d+):(\\d+):\\s+(\\d+):(\\d+)\\s+(warning|error):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"endLine": 4,
"endColumn": 5,
"severity": 6,
"message": 7
}
},
{
"owner": "rust",
"fileLocation": ["relative", "${workspaceRoot}"],
"severity": "error",
"pattern": {
"regexp": "^.*panicked\\s+at\\s+'(.*)',\\s+(.*):(\\d+)$",
"message": 1,
"file": 2,
"line": 3
}
}
]
}
]
}
@Tower450

Tower450 commented Sep 4, 2017

Copy link
Copy Markdown

thanks 👍

@plumpNation

Copy link
Copy Markdown

Appreciated.

@Zmq0max

Zmq0max commented Jan 2, 2018

Copy link
Copy Markdown

Many Thanks!

@jaganathanb

Copy link
Copy Markdown

Here is the updated one for config v2.0.0

// Available variables which can be used inside of strings.
// ${workspaceRoot}: the root folder of the team
// ${file}: the current opened file
// ${fileBasename}: the current opened file's basename
// ${fileDirname}: the current opened file's dirname
// ${fileExtname}: the current opened file's extension
// ${cwd}: the current working directory of the spawned process
{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "build",
      "args": ["build", "-v"],
      "command": "cargo",
      "group": "build",
      "problemMatcher": [
        {
          "owner": "rust",
          "fileLocation": ["relative", "${workspaceRoot}"],
          "pattern": {
            "regexp": "^(.*):(\\d+):(\\d+):\\s+(\\d+):(\\d+)\\s+(warning|error):\\s+(.*)$",
            "file": 1,
            "line": 2,
            "column": 3,
            "endLine": 4,
            "endColumn": 5,
            "severity": 6,
            "message": 7
          }
        }
      ]
    },
    {
      "label": "clean",
      "args": ["clean"],
      "presentation": {
        "reveal": "always"
      }
    },
    {
      "label": "run",
      "args": ["run", "-v"],
      "presentation": {
        "reveal": "always"
      },
      "group": "build",
      "problemMatcher": [
        {
          "owner": "rust",
          "fileLocation": ["relative", "${workspaceRoot}"],
          "pattern": {
            "regexp": "^(.*):(\\d+):(\\d+):\\s+(\\d+):(\\d+)\\s+(warning|error):\\s+(.*)$",
            "file": 1,
            "line": 2,
            "column": 3,
            "endLine": 4,
            "endColumn": 5,
            "severity": 6,
            "message": 7
          }
        }
      ]
    },
    {
      "label": "test",
      "args": ["test"],
      "presentation": {
        "reveal": "always"
      },
      "group": "test",
      "problemMatcher": [
        {
          "owner": "rust",
          "fileLocation": ["relative", "${workspaceRoot}"],
          "pattern": {
            "regexp": "^(.*):(\\d+):(\\d+):\\s+(\\d+):(\\d+)\\s+(warning|error):\\s+(.*)$",
            "file": 1,
            "line": 2,
            "column": 3,
            "endLine": 4,
            "endColumn": 5,
            "severity": 6,
            "message": 7
          }
        },
        {
          "owner": "rust",
          "fileLocation": ["relative", "${workspaceRoot}"],
          "severity": "error",
          "pattern": {
            "regexp": "^.*panicked\\s+at\\s+'(.*)',\\s+(.*):(\\d+)$",
            "message": 1,
            "file": 2,
            "line": 3
          }
        }
      ]
    },
    {
      "label": "bench",
      "args": ["bench"],
      "presentation": {
        "reveal": "always"
      },
      "group": "test",
      "problemMatcher": [
        {
          "owner": "rust",
          "fileLocation": ["relative", "${workspaceRoot}"],
          "pattern": {
            "regexp": "^(.*):(\\d+):(\\d+):\\s+(\\d+):(\\d+)\\s+(warning|error):\\s+(.*)$",
            "file": 1,
            "line": 2,
            "column": 3,
            "endLine": 4,
            "endColumn": 5,
            "severity": 6,
            "message": 7
          }
        },
        {
          "owner": "rust",
          "fileLocation": ["relative", "${workspaceRoot}"],
          "severity": "error",
          "pattern": {
            "regexp": "^.*panicked\\s+at\\s+'(.*)',\\s+(.*):(\\d+)$",
            "message": 1,
            "file": 2,
            "line": 3
          }
        }
      ]
    }
  ]
}

@tomerze

tomerze commented Jun 26, 2019

Copy link
Copy Markdown

How do you use it ?

@Eligioo

Eligioo commented Nov 29, 2019

Copy link
Copy Markdown

@TomerZeitune

How do you use it ?

In the root of your project (containing the Cargo.toml file) create a folder called .vscode.
Inside .vscode, create a file called tasks.json and place the content of this gist inside the json file.
Then press CTRL+SHIFT+b when your main.rs is open.

I use this one

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "cargo run",
      "type": "shell",
      "command": "cargo",
      "args": ["run"],
      "group": {
        "kind": "build",
        "isDefault": true
      }
    }
  ]
}

When pressing CTRL+SHIFT+b, it will directly build and run since the tast is set to "isDefault" : true

@mbartelsm

Copy link
Copy Markdown

The updated version doesn't work properly (only build is available). Try this one

// Available variables which can be used inside of strings.
// ${workspaceRoot}: the root folder of the team
// ${file}: the current opened file
// ${fileBasename}: the current opened file's basename
// ${fileDirname}: the current opened file's dirname
// ${fileExtname}: the current opened file's extension
// ${cwd}: the current working directory of the spawned process
{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "build",
      "args": ["build", "-v"],
      "command": "cargo",
      "group": "build",
      "problemMatcher": [
        {
          "owner": "rust",
          "fileLocation": ["relative", "${workspaceRoot}"],
          "pattern": {
            "regexp": "^(.*):(\\d+):(\\d+):\\s+(\\d+):(\\d+)\\s+(warning|error):\\s+(.*)$",
            "file": 1,
            "line": 2,
            "column": 3,
            "endLine": 4,
            "endColumn": 5,
            "severity": 6,
            "message": 7
          }
        }
      ]
    },
    {
      "label": "clean",
      "args": ["clean"],
      "command": "cargo",
      "presentation": {
        "reveal": "always"
      }
    },
    {
      "label": "run",
      "args": ["run", "-v"],
      "command": "cargo",
      "presentation": {
        "reveal": "always"
      },
      "group": {
          "kind": "build",
          "isDefault": true
      },
      "problemMatcher": [
        {
          "owner": "rust",
          "fileLocation": ["relative", "${workspaceRoot}"],
          "pattern": {
            "regexp": "^(.*):(\\d+):(\\d+):\\s+(\\d+):(\\d+)\\s+(warning|error):\\s+(.*)$",
            "file": 1,
            "line": 2,
            "column": 3,
            "endLine": 4,
            "endColumn": 5,
            "severity": 6,
            "message": 7
          }
        }
      ]
    },
    {
      "label": "test",
      "args": ["test"],
      "command": "cargo",
      "presentation": {
        "reveal": "always"
      },
      "group": {
          "kind": "z",
          "isDefault": false
      },
      "problemMatcher": [
        {
          "owner": "rust",
          "fileLocation": ["relative", "${workspaceRoot}"],
          "pattern": {
            "regexp": "^(.*):(\\d+):(\\d+):\\s+(\\d+):(\\d+)\\s+(warning|error):\\s+(.*)$",
            "file": 1,
            "line": 2,
            "column": 3,
            "endLine": 4,
            "endColumn": 5,
            "severity": 6,
            "message": 7
          }
        },
        {
          "owner": "rust",
          "fileLocation": ["relative", "${workspaceRoot}"],
          "severity": "error",
          "pattern": {
            "regexp": "^.*panicked\\s+at\\s+'(.*)',\\s+(.*):(\\d+)$",
            "message": 1,
            "file": 2,
            "line": 3
          }
        }
      ]
    },
    {
      "label": "bench",
      "args": ["bench"],
      "command": "cargo",
      "presentation": {
        "reveal": "always"
      },
      "group": "test",
      "problemMatcher": [
        {
          "owner": "rust",
          "fileLocation": ["relative", "${workspaceRoot}"],
          "pattern": {
            "regexp": "^(.*):(\\d+):(\\d+):\\s+(\\d+):(\\d+)\\s+(warning|error):\\s+(.*)$",
            "file": 1,
            "line": 2,
            "column": 3,
            "endLine": 4,
            "endColumn": 5,
            "severity": 6,
            "message": 7
          }
        },
        {
          "owner": "rust",
          "fileLocation": ["relative", "${workspaceRoot}"],
          "severity": "error",
          "pattern": {
            "regexp": "^.*panicked\\s+at\\s+'(.*)',\\s+(.*):(\\d+)$",
            "message": 1,
            "file": 2,
            "line": 3
          }
        }
      ]
    }
  ]
}

The default build task here is cargo run but you can switch it with cargo build

@deadalusai

deadalusai commented Apr 19, 2020

Copy link
Copy Markdown
Author

Thanks for the new syntax @mbartelsm - I've updated the Gist with your suggestions.

If you haven't already, I recommend checking out the rust language server-based VS Code extension:
https://marketplace.visualstudio.com/items?itemName=rust-lang.rust

It's replaced this tasks.json for me entirely.

@brainstorm

Copy link
Copy Markdown

@deadalusai How so? I have those extensions installed (rls, rust-language-server and code-lldb) and Ctrl+Shift+B still asks me to define a tasks.json definition for cargo build et al :_/

@deadalusai

deadalusai commented Apr 28, 2020

Copy link
Copy Markdown
Author

@brainstorm On my machine after creating a new rust project and opening it in VS Code:

> cargo new footest
> code footest

Hitting Ctrl+Shift+B shows the following menu:
image
Just hitting Enter starts the build.
Clicking on the cog icon in that menu ejects a tasks.json file which you can then customize and commit to source control.

Edit: here is the README - https://github.com/rust-lang/rls-vscode#tasks

@brainstorm

brainstorm commented Apr 28, 2020

Copy link
Copy Markdown

Interesting. On my OSX machine Cmd+Pand then > Cargo: Build seems to do the right thing, collecting warnings/errors right. On the other hand, Cmd+Shift+B shows up this:

Skärmavbild 2020-04-28 kl  14 29 25

I am working on a remote (SSH plugin) VSCode project, for more context.

@deadalusai

Copy link
Copy Markdown
Author

Might be an OSX vs Windows difference, though that seems unlikely. I'd start checking that everything is up to date perhaps. This is the only VS Code extension I have installed:
https://marketplace.visualstudio.com/items?itemName=rust-lang.rust

@brainstorm

Copy link
Copy Markdown

No worries, a year later it works :P

@deadalusai

Copy link
Copy Markdown
Author

👍

Just to move the goalposts again, apparently the rust-analyzer project now provides a better experience.

https://marketplace.visualstudio.com/items?itemName=matklad.rust-analyzer

@wonbyte

wonbyte commented Jun 16, 2021

Copy link
Copy Markdown

@deadalusai This work ok with the rust-analyzer plugin?

@deadalusai

Copy link
Copy Markdown
Author

@wonbyte as I mentioned, I'd suggest using rust-analyzer instead of this tasks.json.

@wonbyte

wonbyte commented Jun 18, 2021

Copy link
Copy Markdown

i see got it!

@epenabella

Copy link
Copy Markdown

use --manifest-path

image

@rehierl

rehierl commented Jun 7, 2025

Copy link
Copy Markdown

(On Linux Mint) I find the tasks in the $project/.vscode/tasks.json files quite useful. After all, VS code allows to set keyboard shortcuts (Ctrl+K) to open the task selection popup (Ctrl+P / Tasks: RunTask) and to re-run the last task (Ctrl+P / Tasks: Rerun Last Task). Same with $project/.vscode/launch.json - just hit F5 to start debugging. In a project with plenty of source files it tends to get a little tedious having to find the entry point, and then to click on Codelens' links - i.e. "Run" or "Debug".

@mbartelsm

  • In the build and run tasks, you can now use "$rustc" instead of the regex in the "problemMatcher" property.
  • More alternatives will be shown in the auto-completion popup when you enter the problem matcher's value - i.e. $rust-panic, $rustc, $rustc-json, $rustc-watch.
  • The "test" task complains about a missing "type" property, which should most likely be "type": "cargo",
  • In the "test" task the linter complains about the "kind" property in the "group" object - only "build", "test" or "none" are allowed.
{//- $project/.vscode/tasks.json
  {//- run "cargo test"
    "label": "test",
    "group": "test",
    "type": "cargo",
    "command": "test",
    "args": [],
    "presentation": {
      "reveal": "always"
    },
    "problemMatcher": ...,
  }
}

@epenabella

Thanks for the hints on launch.json. However, the following also seems to work.

{//- $project/.vscode/launch.json
  "version": "0.2.0",
  "configurations": [
    {
      "name": "debug rust",
      "type": "lldb",
      "request": "launch",
      "preLaunchTask": "build",//- name of the task in tasks.json
      "program": "${workspaceFolder}/target/debug/rust-test",
      "args": [],
      "cwd": "${workspaceFolder}",
      "console": "integratedTerminal",
    },
  ]
}

Anyways, thanks to all for this gist!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment