1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

アンカーとエイリアスを使っているYAMLの処理がfedora 36で失敗

Last updated at Posted at 2022-06-13

備忘

発生事象

以前は動いていた vagrant up が失敗。

$ vagrant up
Vagrant failed to initialize at a very early stage:

There was an error loading a Vagrantfile. The file being loaded
and the error message are shown below. This is usually caused by
a syntax error.

Path: /foo/bar/molecule/default/Vagrantfile
Line number: 0
Message: Psych::BadAlias: Unknown alias: platform

PD

PDの結果、YAMLファイルから情報を読み取って使用していたVagrantfileが失敗するようになっていた。

具体的には以下の樣なYAML.load_file箇所が失敗。

require 'yaml'
foo = YAML.load_file('molecule.yml')

具体例

以下のアンカーとエイリアスを用いたYAMLは

- &fuga
  foo: 1
  bar: 2
- <<: *fuga
  bar: 3

以下を意味する。

- foo: 1
  bar: 2
- foo: 1
  bar: 3

しかしYAML.load_fileを掛けると失敗する。

$ echo "- &fuga
  foo: 1
  bar: 2
- <<: *fuga
  bar: 3
" | ruby -r yaml -e "puts YAML.dump_stream(YAML.load(STDIN.read))"
/usr/share/gems/gems/psych-4.0.3/lib/psych/visitors/to_ruby.rb:430:in `visit_Psych_Nodes_Alias': Unknown alias: fuga (Psych::BadAlias)
	from /usr/share/gems/gems/psych-4.0.3/lib/psych/visitors/visitor.rb:30:in `visit'
	from /usr/share/gems/gems/psych-4.0.3/lib/psych/visitors/visitor.rb:6:in `accept'
	from /usr/share/gems/gems/psych-4.0.3/lib/psych/visitors/to_ruby.rb:35:in `accept'
	from /usr/share/gems/gems/psych-4.0.3/lib/psych/visitors/to_ruby.rb:345:in `block in revive_hash'
	from /usr/share/gems/gems/psych-4.0.3/lib/psych/visitors/to_ruby.rb:343:in `each'
	from /usr/share/gems/gems/psych-4.0.3/lib/psych/visitors/to_ruby.rb:343:in `each_slice'
	from /usr/share/gems/gems/psych-4.0.3/lib/psych/visitors/to_ruby.rb:343:in `revive_hash'
	from /usr/share/gems/gems/psych-4.0.3/lib/psych/visitors/to_ruby.rb:167:in `visit_Psych_Nodes_Mapping'
	from /usr/share/gems/gems/psych-4.0.3/lib/psych/visitors/visitor.rb:30:in `visit'
	from /usr/share/gems/gems/psych-4.0.3/lib/psych/visitors/visitor.rb:6:in `accept'
	from /usr/share/gems/gems/psych-4.0.3/lib/psych/visitors/to_ruby.rb:35:in `accept'
	from /usr/share/gems/gems/psych-4.0.3/lib/psych/visitors/to_ruby.rb:338:in `block in register_empty'
	from /usr/share/gems/gems/psych-4.0.3/lib/psych/visitors/to_ruby.rb:338:in `each'
	from /usr/share/gems/gems/psych-4.0.3/lib/psych/visitors/to_ruby.rb:338:in `register_empty'
	from /usr/share/gems/gems/psych-4.0.3/lib/psych/visitors/to_ruby.rb:146:in `visit_Psych_Nodes_Sequence'
	from /usr/share/gems/gems/psych-4.0.3/lib/psych/visitors/visitor.rb:30:in `visit'
	from /usr/share/gems/gems/psych-4.0.3/lib/psych/visitors/visitor.rb:6:in `accept'
	from /usr/share/gems/gems/psych-4.0.3/lib/psych/visitors/to_ruby.rb:35:in `accept'
	from /usr/share/gems/gems/psych-4.0.3/lib/psych/visitors/to_ruby.rb:318:in `visit_Psych_Nodes_Document'
	from /usr/share/gems/gems/psych-4.0.3/lib/psych/visitors/visitor.rb:30:in `visit'
	from /usr/share/gems/gems/psych-4.0.3/lib/psych/visitors/visitor.rb:6:in `accept'
	from /usr/share/gems/gems/psych-4.0.3/lib/psych/visitors/to_ruby.rb:35:in `accept'
	from /usr/share/ruby/psych.rb:335:in `safe_load'
	from /usr/share/ruby/psych.rb:370:in `load'
	from -e:1:in `<main>'

原因

YAML.loadで使用しているpsych Ruby GEMがV3からV4に変更された際に仕様変更され、エイリアス、アンカーをデフォルトで許可しなくなったため。

Ruby GEMを明示的に上げた記憶はないが、FedoraをFedora Linux 36にバージョンアップした際に影響を受けた可能性。

$ gem list psych

*** LOCAL GEMS ***

psych (4.0.3)

対応

YAML.unsafe_load_fileに置き換え。

YAML.unsafe_load_file('molecule.yml')

もしくはYAML.safe_load_fileに置き換え、aliases: true を付与。

YAML.safe_load_file('molecule.yml', aliases: true)

確認

$ echo "
- &fuga
  foo: 1
  bar: 2
- <<: *fuga
  bar: 3
" | ruby -r yaml -e "puts YAML.dump_stream(YAML.safe_load(STDIN.read, aliases: true))"
---
- foo: 1
  bar: 2
- foo: 1
  bar: 3

課題

YAML.load_streamなどはaliases: trueといった制御を持たない様子。
https://github.com/ruby/psych/issues/381

参考

GitHub
https://github.com/ruby/psych

パラメーター確認
https://docs.ruby-lang.org/ja/latest/class/Psych.html

大体ここに書いてあった。
https://secret-garden.hatenablog.com/entry/2021/05/23/200803

1
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?