VSCode 使用本地模式文件的 jsonValidation

VSCode jsonValidation using local schema files

contributes.jsonValidation 的文档说:

The url value can be either a local path to a schema file included in the extension or a remote server URL such as a json schema store.

URL 值按描述工作:

"jsonValidation": [{
    "fileMatch": ".jshintrc",
    "url": "http://json.schemastore.org/jshintrc"
}]

但是,如果我创建了一个测试扩展并提供了我的扩展本地文件的路径,它不会:

"jsonValidation": [{
    "fileMatch": "*.testing",
    "url": "/schemas/testing.schema.json"
}]

我的扩展的根目录下有一个文件夹 schemas,其中有一个文件 testing.schema.json。它只是 jshintrc 模式的一个副本。

当我启动我的测试扩展程序并加载一个与模式匹配的文件时,我在第一个左括号下看到了一点绿色波浪线。如果我将鼠标悬停在绿色曲线上,我会看到一个熟悉的错误:

Unable to load schema from '/schemas/edfoo.schema.json': <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>IIS 10.0 Detailed Error - 404.0 - Not Found</title>

很明显,它正在连接到服务器,而不是从扩展中读取本地文件。但是文档说我可以在扩展中包含一个模式文件。我如何让它发挥作用?

弄清楚了:url 缺少一个前导点,如下所示:

"jsonValidation": [{
    "fileMatch": "*.testing",
    "url": "./schemas/testing.schema.json"
}]

url 以此 ./ 模式开头时,它会被自动理解为附带扩展名的文件。如果前面缺少 ./ 甚至只是 .,它将被视为从服务器获取的 URL。

由于 vscode 是开源的,我可以参考实现此行为的代码行。

在这里:jsonMain.ts#L105