psych icon indicating copy to clipboard operation
psych copied to clipboard

Newer SnakeYAML defers emit waiting for comments

Open headius opened this issue 4 years ago • 13 comments

In https://bitbucket.org/asomov/snakeyaml/pull-requests/7 SnakeYAML added features to support parsing and emitting comments. Unfortunately this has had a side effect of deferring some emits, and there does not appear to be a way to turn it off.

An example from https://github.com/jruby/jruby/pull/6680 shows how this is failing:

      def test_float
        @stream.push 1.2
        assert_match(/^--- 1.2/, @io.string)
      end
  2) Failure:
Psych::JSON::TestStream#test_float [/home/travis/build/jruby/jruby/test/mri/psych/json/test_stream.rb:50]:
Expected /^--- 1.2/ to match "".

The issue here is that since the stream is never finished and SnakeYAML is waiting for a comment, the emit of the float scalar never happens. Modifying the code to do @stream.finishafter the push allows this and all similar tests to pass.

So the question is: should the tests or SnakeYAML be modified?

Basically I need to understand whether this deferred emit constitutes a significant-enough break from libyaml to warrant a fix to SnakeYAML, or if the tests are unreasonable in expecting all events to be emitted immediately.

Any input from other Psych maintainers?

headius avatar May 25 '21 16:05 headius

FIled https://bitbucket.org/asomov/snakeyaml/issues/503/comment-handling-causes-deferred-emit

headius avatar May 25 '21 16:05 headius

Patch that would fix the tests in question:

diff --git a/test/mri/psych/json/test_stream.rb b/test/mri/psych/json/test_stream.rb
index 90a770c1b7..5abd2a538d 100644
--- a/test/mri/psych/json/test_stream.rb
+++ b/test/mri/psych/json/test_stream.rb
@@ -27,26 +27,31 @@ module Psych
 
       def test_null
         @stream.push(nil)
+        @stream.finish
         assert_match(/^--- null/, @io.string)
       end
 
       def test_string
         @stream.push "foo"
+        @stream.finish
         assert_match(/(["])foo\1/, @io.string)
       end
 
       def test_symbol
         @stream.push :foo
+        @stream.finish
         assert_match(/(["])foo\1/, @io.string)
       end
 
       def test_int
         @stream.push 10
+        @stream.finish
         assert_match(/^--- 10/, @io.string)
       end
 
       def test_float
         @stream.push 1.2
+        @stream.finish
         assert_match(/^--- 1.2/, @io.string)
       end
 
@@ -55,6 +60,7 @@ module Psych
         @stream.push hash
 
         json = @io.string
+        @stream.finish
         assert_match(/}$/, json)
         assert_match(/^--- \{/, json)
         assert_match(/["]one['"]/, json)
@@ -66,6 +72,7 @@ module Psych
         @stream.push list
 
         json = @io.string
+        @stream.finish
         assert_match(/\]$/, json)
         assert_match(/^--- \[/, json)
         assert_match(/["]one["]/, json)
@@ -96,6 +103,7 @@ module Psych
         time = Time.utc(2010, 10, 10)
         @stream.push({'a' => time })
         json = @io.string
+        @stream.finish
         assert_match "{\"a\": \"2010-10-10 00:00:00.000000000 Z\"}\n", json
       end
 
@@ -103,6 +111,7 @@ module Psych
         time = Time.new(2010, 10, 10).to_datetime
         @stream.push({'a' => time })
         json = @io.string
+        @stream.finish
         assert_match "{\"a\": \"#{time.strftime("%Y-%m-%d %H:%M:%S.%9N %:z")}\"}\n", json
       end
     end

headius avatar May 25 '21 16:05 headius

As commented in the filed issue, updating to snakeYAML 1.29-SNAPSHOT should solve this problem.

SaltyAimbOtter avatar May 28 '21 20:05 SaltyAimbOtter

@headius can you please try the latest snapshot of SnakeYAML ?

asomov avatar May 30 '21 10:05 asomov

@asomov I will check it out, thank you!

headius avatar Jun 03 '21 15:06 headius

Using a snapshot build of snakeyaml 1.29 does appear to fix the delayed output. I will work with the maintainers to try to get a release we can use.

headius avatar Jun 03 '21 16:06 headius

@hsbt @tenderlove If we can get a release of SnakeYAML 1.29, would it be possible to push an updated 3.3 release? The changes in 4.0 are a bit too drastic (safe_load etc) but we would like to pick up the fixed SnakeYAML in a 3.3 version.

headius avatar Jun 03 '21 16:06 headius

@headius ya of course!

tenderlove avatar Jun 03 '21 20:06 tenderlove

I've pushed a 3-3-stable branch so we can backport changes there.

tenderlove avatar Jun 03 '21 20:06 tenderlove

@tenderlove Thank you!

headius avatar Jun 04 '21 05:06 headius

@headius SnakeYAML released

asomov avatar Jun 12 '21 14:06 asomov

I'm not sure if or how I screwed up verifying the snapshot, but the released SnakeYAML 1.29 does not appear to fix my issue. It still appears to defer output until finish. I will follow up with them.

headius avatar Jun 15 '21 18:06 headius

In the SnakeYAML issue listed above I identified a test that would show the broken behavior, but it was patched to work around the issue. Hopefully they will be able to address the root cause now.

headius avatar Jun 30 '21 17:06 headius

I released 4.0.5 and 3.3.3 with SnakeYAML 1.31.

  • https://github.com/ruby/psych/releases/tag/v4.0.5
  • https://github.com/ruby/psych/releases/tag/v3.3.3

@headius Can we still get this issues?

hsbt avatar Sep 16 '22 01:09 hsbt

Oh yes, I will check on that!

headius avatar Sep 18 '22 04:09 headius

@headius feel free to provide the info

asomov avatar Sep 19 '22 08:09 asomov

With latest psych master (using latest SnakeYAML) the failing tests are gone!

I had to commit a small patch to mimic @tenderlove's changes in #580 but with that fixed we have 5F7E. About half of those appear to be differences in line/column position reporting in SnakeYAML, and the rest are a mix of errors I will try to look into.

I will call this one fixed.

headius avatar Sep 19 '22 16:09 headius