如何在VSCode任务错误中配置文件路径

3
我在VSCode中配置了一个任务来编译Delphi 2005 dpk。它可以工作并在“问题视图”中返回错误,但不会在文件中显示这些错误。
我认为这是因为当我点击错误时,出现了以下错误信息: “无法打开'sr075pro.pas':找不到文件 (...projectfolder\sr075pro.pas)”
但是该文件位于“...projectfolder\webservices\sr075pro.pas”。
我找不到一种方法来告诉任务该文件位于子文件夹中。我尝试使用“fileLocation”标签上的“relative”选项,但没有成功。
返回的错误如下:
Compiling sa_webservices...
Borland Delphi  Version 13.0  Copyright (c) 1983,99 Inprise Corporation
sr075pro.pas(99) Error: Undeclared identifier: 'ni'
sa_webservices.dpk(802) Fatal: Could not compile used unit 'sr075pro.pas'

我的任务配置:

{
  "version": "0.1.0",
  "name": "Compilar",
  "command": "C:\\Compilers\\compile.bat",
  "suppressTaskName": true,
  "isShellCommand": true,
  "isBuildCommand": true,
  "tasks": [
{
      "taskName": "Compile sa_webservices",
      "isBuildCommand": false,
      "isTestCommand": false,
      "showOutput": "always",
      "args": [
        "sa_webservices"
      ],
      "problemMatcher": {
        "owner": "external",
        "fileLocation": "relative",
        "pattern": {
          "regexp": "^([\\w]+\\.(pas|dpr|dpk))\\((\\d+)\\)\\s(Fatal|Error|Warning|Hint):(.*)",
          "file": 1,
          "line": 3,
          "message": 5
        }
      }
    }

我的compile.bat文件:


@echo off
@P:
@set arg1=%1
shift

...
if "%arg1%" == "sa_webservices" set arg2="webservices" 
...
echo Compiling %arg1%...
cd\%arg2% 
dcc32.exe -H -W -Q %arg1%.dpk
2个回答

1
您的任务配置有误。首先,您没有关闭所有括号,但我猜这是在StackOverflow上复制和粘贴时出现的错误。否则,任务配置根本无法工作。
现在来到“真正”的问题: DCC32生成包含相对文件路径的提示和警告。这些路径是“相对于项目文件”的。在您的任务配置中,您通过设置编译器的输出包含相对路径来定义它们。
"fileLocation": "relative"

Visual Studio Code不知道如何从编译器消息中给出的相对路径构建正确的绝对路径。因此,它猜测您当前的${workspaceRoot}(在您的情况下为projectfolder)将是绝对路径。
这就解释了为什么您会看到包含错误文件路径的错误和警告。为了获得正确的路径,您需要告诉VSCode要与相对路径结合的正确路径。 您只需将正确的路径添加到tasks.json中的fileLocation条目即可:
"fileLocation": ["relative", "${workspaceRoot}\\webservices"]

整个 tasks.json 的内容如下:
{
    "version": "0.1.0",
    "name": "Compilar",
    "command": "C:\\Compilers\\compile.bat",
    "suppressTaskName": true,
    "isShellCommand": true,
    "isBuildCommand": true,
    "tasks": [
        {
            "taskName": "Compile sa_webservices",
            "isBuildCommand": false,
            "isTestCommand": false,
            "showOutput": "always",
            "args": [
                "sa_webservices"
            ],
            "problemMatcher": {
                "owner": "external",
                "fileLocation": ["relative", "${workspaceRoot}\\webservices"],
                "pattern": {
                    "regexp": "^([\\w]+\\.(pas|dpr|dpk))\\((\\d+)\\)\\s(Fatal|Error|Warning|Hint):(.*)",
                    "file": 1,
                    "line": 3,
                    "message": 5
                }
            }
        }
    ]
}

成功了!我不知道第二个“fileLocation”选项。谢谢。 - user2133093

0

在VSCode 1.74中,可以更轻松地在problemMatcher中查找文件,请参阅文件位置搜索:v1.74发布说明fileLocation属性有一个新选项search

New file location method; search

Previously, problem matchers needed to know exactly where to look for the problematic files, via the fileLocation property. The supported methods were absolute, relative, or autoDetect (i.e., check for relative paths first and opt to absolute paths in case of failure).

However, in workspaces that need to invoke various scripts residing in nested sub-directories, the developers could have a hard time setting up their tasks; since such scripts seldom report file paths in a unified manner (e.g., relative to the workspace's base directory).

To help alleviate the problem, a new file location method, named search, is introduced in this version. With this method, a deep file system search will be initiated to locate any captured path. See the example below on how to setup the search file location method (although, all parameters are optional):

    // ...
    "fileLocation": [
        "search",
        {
            "include": [ // Optional; defaults to ["${workspaceFolder}"]
                "${workspaceFolder}/src",
                "${workspaceFolder}/extensions"
            ],
            "exclude": [ // Optional
                "${workspaceFolder}/extensions/node_modules"
            ]
        }
    ],
    // ... } ```

⚠️ Of course, users should be wary of the possibly **heavy file system
searches** (e.g., looking inside `node_modules` directories) and set
the `exclude` property with discretion.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接