Newshot

GitHub Actions 接受无效的 YAML

Translation(model translation, not the source's own text)
  • github-actions

TL;DR:GitHub Actions 接受若干无效的 YAML 语法结构,这使得用符合规范的 YAML 解析器解析某些真实场景下的 action 和 workflow 定义变得困难(读作:不可能)。

这与 zizmor#1116 有关,这个问题已经困扰我一年半了。

去年四月,有用户报告称 zizmor 拒绝了一个 workflow,其中包含一个如下所示的 step:


        - name: Create NPM Remote Repository JSON
  uses: jsdaniell/[email protected]
  with:
    name: "npm-remote-template.json"
    json: '{
      "key": "npm-remote",
      "rclass": "remote",
      "packageType": "npm",
      "url": "https://registry.npmjs.org"
    }'
      

除了 json: 键使用了多行 flow-style 字符串之外,这个 step 并没有什么特别之处。

但问题是:它并不是有效的 YAML!YAML 对于嵌套在 block-style 父集合(读作:map 或 array)内部的 flow-style 节点是这样规定的:

Flow 节点的缩进必须比父 block 集合至少多一个空格。注意 flow 节点可以从下一行开始。来源:YAML 1.2.2 § 8.2.3

Flow 节点的缩进必须比父 block 集合至少多一个空格。注意 flow 节点可以从下一行开始。

来源:YAML 1.2.2 § 8.2.3

这意味着正确的语法应该是:


        - name: Create NPM Remote Repository JSON
  uses: jsdaniell/[email protected]
  with:
    name: "npm-remote-template.json"
    json: '{
      "key": "npm-remote",
      "rclass": "remote",
      "packageType": "npm",
      "url": "https://registry.npmjs.org"
     }'
      

两者的差别几乎看不出来:

YAML 是一个庞大而复杂的规范,所以我甚至没想到这是无效的。相反,我开了 tree-sitter-yaml#36 来“修复”它。现在我已经开了 tree-sitter-yaml#51 来撤销那个错误的修复。

在 GitHub 这边,我不确定他们是否有实现规范一致性的清晰路径。Actions 背后的大部分代码是专有的,但他们那边的底层解析器很可能是 YamlDotNet。

Actions 似乎使用的是 YamlDotNet 5.3.0,该版本于 2018 年发布。然而,即使升级到最新版本(撰写本文时为 18.1.0)也无法修复这个特定的 bug,因为该版本中它仍然存在(注意 !isSingleQuoted 这个守卫条件)。

我最好的猜测是,2020 年的一个补丁(YamlDotNet#467)只修复了双引号形式,因为 YAML 的官方测试套件只针对规则 8.2.3 测试了双引号字符串,尽管该规则同时适用于两种形式。

The translation is machine-generated and individual terms may be off. Nothing is rewritten or cut; the original is authoritative.

Published by Lobsters
This site only summarises and aggregates; copyright stays with the original author. Headlines and summaries follow the source — where they differ, the original wins.
Read the original ↗