Show
OSCHINA 小程序 —— 关注技术领域的头条文章聚合全网技术文章,根据你的阅读喜好进行个性推荐 首页 关键词 a problem occurred starting process 'command 'npm'' 加载中 加载中 {{o.href}} {{o.content | formatHtml}} {{o.author.name}} {{o.author.name}} {{o.pubDate | formatDate}} {{parseInt(o.viewCount) | bigNumberTransform}} {{parseInt(o.replyCount) | bigNumberTransform}}
暂无相关内容OSCHINA 社区关于我们 联系我们 加入我们 合作伙伴 Open API 在线工具Gitee.com 企业研发管理 CopyCat-代码克隆检测 实用在线工具 国家反诈中心APP下载 攻略项目运营 Awesome 软件(持续更新中) QQ群
530688128 公众号视频号OSCHINA 小程序聚合全网技术文章,根据你的阅读喜好进行个性推荐 OSC小程序 ©OSCHINA(OSChina.NET) 工信部 开源软件推进联盟 指定官方社区 深圳市奥思网络科技有限公司版权所有 粤ICP备12009483号 Integrate with External Tools via TasksLots of tools exist to automate tasks like linting, building, packaging, testing, or deploying software systems. Examples include the TypeScript Compiler, linters like ESLint and TSLint as well as build systems like Make, Ant, Gulp, Jake, Rake, and MSBuild. These tools are mostly run from the command line and automate jobs inside and outside the inner software development loop (edit, compile, test, and debug). Given their importance in the development life cycle, it is helpful to be able to run tools and analyze their results from within VS Code. Tasks in VS Code can be configured to run scripts and start processes so that many of these existing tools can be used from within VS Code without
having to enter a command line or write new code. Workspace or folder specific tasks are configured from the Extensions can also contribute tasks using a Task Provider, and these contributed tasks can add workspace-specific configurations defined in the
TypeScript Hello WorldLet's start with a simple "Hello World" TypeScript program that we want to compile to JavaScript. Create an empty folder "mytask", generate a
Now create a
Pressing ⇧⌘B (Windows, Linux Ctrl+Shift+B) or running Run Build Task from the global Terminal menu show the following picker: The first entry executes the TypeScript compiler and
translates the TypeScript file to a JavaScript file. When the compiler has finished, there should be a You can also define the TypeScript build or watch task as the default build task so that it is executed directly when triggering Run Build Task
(⇧⌘B (Windows, Linux Ctrl+Shift+B)). To do so, select Configure Default Build Task from the global Terminal menu. This shows you a picker with the available build tasks. Select tsc: build or tsc: watch and VS Code will generate a
The Task auto-detectionVS Code currently auto-detects tasks for the following systems: Gulp, Grunt, Jake, and npm. We are working with the corresponding extension authors to add support for Maven and the C# If you have not already done so, install the necessary npm modules by running Executing the task produces one error, shown in the Problems view: In addition, VS Code created a
This instructs VS Code to scan the output of the npm lint script for problems using the ESLint stylish format. For Gulp, Grunt, and Jake, the task auto-detection works the same. Below is an example of the tasks detected for the vscode-node-debug extension.
Task auto detection can be disabled using the following settings:
Custom tasksNot all tasks or scripts can be auto-detected in your workspace. Sometimes it is necessary to define your own custom tasks. Assume you have a script to run your tests in order to set up some environment correctly.
The script is stored in a script folder inside your workspace and named
We are working on more auto-detection support, so this list will get smaller and smaller in the future. Since we want to write our own custom task, select Others from the list. This opens the
The task's properties have the following semantic:
You can see the full set of task properties and values with IntelliSense in your You can also review the tasks.json schema. Shell commands need special treatment when it comes to commands and arguments that
contain spaces or other special characters like
Besides escaping, the following values are supported:
If the command itself contains spaces, VS Code will by default strong quote the command as well. As with arguments, the user can control the quoting of the command using the same literal style. There are more task properties to configure your workflow. You can use IntelliSense with ⌃Space (Windows, Linux Ctrl+Space) to get an overview of the valid properties. In addition to the global menu bar, task commands can be accessed using the Command Palette (⇧⌘P (Windows, Linux Ctrl+Shift+P)). You can filter on 'task' and can see the various task related commands. Compound tasksYou can also
compose tasks out of simpler tasks with the The
If you specify
User level tasksYou can create user level tasks that are not tied to a specific workspace or folder using the Tasks: Open User Tasks command. Only Output behaviorSometimes you want to control how the Integrated Terminal panel behaves when running tasks. For instance, you may want to maximize editor space and only look at task output if you think there is a problem. The behavior of the terminal can be controlled using the
You can modify the terminal panel behavior for auto-detected tasks as
well. For example, if you want to change the output behavior for the npm: run lint from the ESLint example from above, add the
You can also mix custom tasks with configurations for detected tasks. A
Run behaviorYou can specify a task's run
behaviors using the
Customizing auto-detected tasksAs mentioned above, you can customize auto-detected tasks in the
Executing Run Task from the global Terminal menu will show the following picker: Press the gear icon. This will create the following
Usually you would now add a problem matcher (in this case Processing task output with problem matchersVS Code can process the output from a task with a problem matcher. Problem matchers scan the task output text for known warning or error strings, and report these inline in the editor and in the Problems panel. VS Code ships with several problem matchers 'in-the-box':
You can also create your own problem matcher, which we'll discuss in a later section. Binding keyboard shortcuts to tasksIf you need to run a task frequently, you can define a keyboard shortcut for the task. For example, to bind
Variable substitutionWhen authoring tasks configurations, it is useful to have a set of predefined common variables such as the active file (
Below is an example of a custom task configuration that passes the current opened file to the TypeScript compiler.
Similarly, you can reference your project's configuration
settings by prefixing the name with ${config:. For example, Below is an example of a custom task configuration, which executes autopep8 on the current file using the autopep8 executable defined by the
If you want to specify the selected Python interpreter used by the Python extension for If simple variable substitution isn't
enough, you can also get input from the user of your task by adding an For more information about Operating system specific propertiesThe task system supports defining values (for example, the command to be executed) specific to an operating system. To do so, put an operating system specific literal into the Below is an example that uses the Node.js executable as a command and is treated differently on Windows and Linux:
Valid operating
properties are Task properties can also be defined in the global scope. If present, they will be used for specific tasks unless they define the same property with a different value. In the example below, there is a global
Character escaping in PowerShellWhen the default shell is PowerShell, or when a task is configured to use PowerShell, you might see unexpected space and quote escaping. The unexpected escaping only occurs with cmdlets because VS Code doesn't know if your command contains cmdlets. Example 1 below shows a case where you'll get escaping that doesn't work with PowerShell. Example 2 shows the best, cross-platform, way to get good escaping. In some cases, you might not be able to follow example 2 and you'll need to do the manual escaping shown in example 3.
Changing the encoding for a task outputTasks frequently act with files on disk. If these files are stored on disk with an encoding different than the system encoding, you need to let the command executed as a task know which encoding to use. Since this depends on the operating system and the shell used, there is no general solution to control this. Below are advice and examples on how to make it work. If you need to tweak the encoding, you should check whether it makes sense to change the default encoding used by your operating system or at least change it for the shell you use by tweaking the shell's profile file. If you only need to tweak it for a specific task, then add the OS-specific
command necessary to change the encoding to the tasks command line. The following example is for Windows using code page of 437 as its default. The task shows the output of a file containing Cyrillic characters and therefore needs code page 866. The task to list the file looks like this assuming that the default shell is set to
If the task is executed in Examples of tasks in actionTo highlight the power of tasks, here are a few examples of how VS Code can use tasks to integrate external tools like linters and compilers. Transpiling TypeScript to JavaScriptThe TypeScript topic includes an example that creates a task to transpile TypeScript to JavaScript and observe any related errors from within VS Code. Transpiling Less and SCSS into CSSThe CSS topic provides examples of how to use Tasks to generate CSS files.
Defining a problem matcherVS Code ships some of the most common problem matchers 'in-the-box'. However, there are lots of compilers and linting tools out there, all of which produce their own style of errors and warnings so you may want to create your own problem matcher. We have a
We want to produce a problem matcher that can capture the message in the output and show a corresponding problem in VS Code. Problem matchers heavily rely on regular expressions. The section below assumes you are familiar with regular expressions.
A matcher that captures the above warning (and errors) looks like this:
Note that the file, line, and message properties are mandatory. The Here is a finished
Running it inside VS Code and pressing ⇧⌘M (Windows, Linux Ctrl+Shift+M) to get the list of problems gives you the following output:
There are a couple more properties that can be used inside a pattern. These are:
You can also define a problem matcher that captures only a file. To do so, define a
Defining a multiline problem matcherSome tools spread problems found in a source file over several lines, especially if stylish reporters are used. An example is ESLint; in stylish mode it produces output like this:
Our problem matcher is line-based so we need to capture the file name (test.js) with a different regular expression than the actual problem location and message (1:0 error Missing "use strict" statement). To do this, use an array of problem patterns for the The following problem pattern matches the output from ESLint in stylish mode - but still has one small issue that we need to resolve next. The code below has a first regular expression to capture the file name and the second to capture the line, column, severity, message, and error code:
However, this pattern will not work if there is more than one problem on a resource. For instance, imagine the following output from ESLint:
The pattern's first regular expression will match "test.js", the second "1:0 error ...". The next line "1:9 error ..." is processed but not matched by the first regular expression and so no problem is captured. To make this work, the last regular expression of a multiline pattern can specify the
The information captured by the first pattern, which in this case matches Here is a problem matcher to fully capture ESLint stylish problems:
Note: If you have multiple problems that occur on the same resource with the exact same line and column, then only one problem will be shown. This applies to all problem matchers, not just multiline problem matchers. Modifying an existing problem matcherIf an existing problem matcher is close to what you need, you can modify it in your
Other modifiable problem matcher properties include Background / watching tasksSome tools support running in the background while watching the file system for changes and then
triggering an action when a file changes on disk. With To provide feedback that a background task is active in VS Code and producing problem results, a problem matcher has to use additional information to detect these
When a file changes on disk that contains a problem, the following output appears:
Looking at the output shows the following pattern:
To capture this information, a problem matcher can provide a For the
In addition to the A full handcrafted
Next stepsThat was tasks - let's keep going...
Common questionsCan a task use a different shell than the one specified for the Integrated Terminal?Yes. You can use the
Alternatively, you can override a task's shell with the
Can a background task be used as a prelaunchTask in launch.json?Yes. Since a
background task will run until killed, a background task on its own has no signal that it has "completed". To use a background task as a Your task could be:
Note: The You can then use
the task as a
For more on background tasks, go to Background / watching tasks. Why do I get "command not found" when running a task?The message "command not found" happens when the task command you're trying
to run is not recognized by your terminal as something runnable. Most often, this occurs because the command is configured as part of your shell's startup scripts. Tasks are run as non-login and non-interactive, which means that the startup scripts for your shell won't be run. There are several ways to resolve this issue:
The above 9/1/2022 |