Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reading empty file causes Decode to return error #805

Open
xyzzyz opened this issue Dec 13, 2021 · 1 comment · May be fixed by #1015
Open

Reading empty file causes Decode to return error #805

xyzzyz opened this issue Dec 13, 2021 · 1 comment · May be fixed by #1015

Comments

@xyzzyz
Copy link

xyzzyz commented Dec 13, 2021

In yaml.v3, when reading from empty file, the Decode function returns io.EOF. It should behave just like Unmarshal function, which will happily unmarshal from empty string, without any errors.

costela added a commit to costela/kong-yaml that referenced this issue Jan 14, 2022
this should work around the following go-yaml issue:
go-yaml/yaml#805
costela added a commit to costela/kong-yaml that referenced this issue Jan 14, 2022
this should work around the following go-yaml issue:
go-yaml/yaml#805
alecthomas pushed a commit to alecthomas/kong-yaml that referenced this issue Jan 14, 2022
this should work around the following go-yaml issue:
go-yaml/yaml#805
mikelolasagasti added a commit to mikelolasagasti/exporter-toolkit that referenced this issue Jul 23, 2023
Also, modify `empty_config_yml` because with yaml.v3 reading empty file
causes Decode to return EOF error, as described in
go-yaml/yaml#805 issue.
mikelolasagasti added a commit to mikelolasagasti/exporter-toolkit that referenced this issue Jul 23, 2023
Also, modify `empty_config_yml` because with yaml.v3 reading empty file
causes Decode to return EOF error, as described in
go-yaml/yaml#805 issue.

Signed-off-by: Mikel Olasagasti Uranga <[email protected]>
mikelolasagasti added a commit to mikelolasagasti/exporter-toolkit that referenced this issue Jul 23, 2023
Also, modify `empty_config_yml` because with yaml.v3 reading empty file
causes Decode to return EOF error, as described in
go-yaml/yaml#805 issue.

Signed-off-by: Mikel Olasagasti Uranga <[email protected]>
@lukemassa
Copy link

I originally thought this was a bug as well, and went to try to fix it, but now I believe it might be working as intended.

https://pkg.go.dev/gopkg.in/yaml.v2#Decoder.Decode says

Decode reads the next YAML-encoded value from its input and stores it in the value pointed to by v.

Compare https://pkg.go.dev/gopkg.in/yaml.v3#Unmarshal

Unmarshal decodes the first document found within the in byte slice

Decode reads one document at a time, whereas Unmarshal only reads the first document. Because of this I imagine some clients of Decode() expect to loop until EOF, thus an empty file cannot return nil.

I found this out by trying to do the naive thing and Decode() return nil instead of io.EOF and saw found this code was stuck in an infinite loop: https://github.com/go-yaml/yaml/blob/v3/decode_test.go#L898

Note that the original tests explicitly call out this discrepancy between Decode() and Unmarshal(): https://github.com/go-yaml/yaml/blob/v3/decode_test.go#L852

My view then is to call this out in the documentation (I'll write something up now) and leave the behavior unchanged. Open to other suggestions though.

mikelolasagasti added a commit to mikelolasagasti/exporter-toolkit that referenced this issue Sep 10, 2024
Also, modify `empty_config_yml` because with yaml.v3 reading empty file
causes Decode to return EOF error, as described in
go-yaml/yaml#805 issue.

Signed-off-by: Mikel Olasagasti Uranga <[email protected]>
mikelolasagasti added a commit to mikelolasagasti/exporter-toolkit that referenced this issue Sep 10, 2024
Also, modify `empty_config_yml` because with yaml.v3 reading empty file
causes Decode to return EOF error, as described in
go-yaml/yaml#805 issue.

Signed-off-by: Mikel Olasagasti Uranga <[email protected]>
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 a pull request may close this issue.

2 participants