Skip to content

Use JSON.parse instead of Gem::SafeYAML for cargo metadata#714

Merged
gjtorikian merged 1 commit intooxidize-rb:mainfrom
rwstauner:cargo-metadata-json
Mar 12, 2026
Merged

Use JSON.parse instead of Gem::SafeYAML for cargo metadata#714
gjtorikian merged 1 commit intooxidize-rb:mainfrom
rwstauner:cargo-metadata-json

Conversation

@rwstauner
Copy link
Contributor

@rwstauner rwstauner commented Mar 11, 2026

cargo metadata --format-version 1 output is JSON. Parsing it with Gem::SafeYAML.safe_load worked incidentally because JSON is valid YAML when parsed by Psych, but Ruby 4.1.0dev (as of ruby/ruby@2c9e4befbf01) sets Gem.use_psych? to false, causing SafeYAML to fall through to Gem::YAMLSerializer which returns a string instead of a hash and rb-sys raises.

The test_cargo_metadata.rb test fails on ruby-head but passes with this change.

refs ruby/rubygems#9352

@hsbt
Copy link

hsbt commented Mar 12, 2026

Psych is based on YAML 1.1 by libyaml and is not fully JSON-compliant. I also recommended to use json for guaranteed compatibility.

`cargo metadata --format-version 1` output is JSON. Parsing it with
Gem::SafeYAML.safe_load worked incidentally because JSON is valid YAML
when parsed by Psych, but Ruby 4.1.0dev sets Gem.use_psych? to false,
causing SafeYAML to fall through to Gem::YAMLSerializer which cannot
parse JSON correctly.
@rwstauner rwstauner force-pushed the cargo-metadata-json branch from ea7ff56 to cd3078a Compare March 12, 2026 18:01
@gjtorikian gjtorikian merged commit 4c9848e into oxidize-rb:main Mar 12, 2026
57 checks passed
@gjtorikian
Copy link
Member

thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants