Skip to content

introduce a helper for yaml.NewEncoder() to always close it - #2196

Open
omar-polo wants to merge 1 commit into
mainfrom
op/yaml-decoder-close
Open

introduce a helper for yaml.NewEncoder() to always close it#2196
omar-polo wants to merge 1 commit into
mainfrom
op/yaml-decoder-close

Conversation

@omar-polo

Copy link
Copy Markdown
Contributor

unlike json.NewEncoder(), yaml Encoder needs to be Close()d to properly flush. Introduce an helper and use it across the codebase to avoid forgetting to do so.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces a small YAML marshaling helper to ensure yaml.Encoder is always Close()d (to properly flush), and updates call sites to use it so YAML output isn’t silently truncated due to a missed close.

Changes:

  • Added utils.YAMLEncode(io.Writer, any) error as a single safe entry point for YAML encoding + encoder close.
  • Replaced direct yaml.NewEncoder(...).Encode(...) usage with YAMLEncode(...) in config saving and CLI output paths.
  • Updated imports in affected files to remove direct YAML encoder usage and reference utils instead.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
utils/marshal.go Adds YAMLEncode helper that encodes and closes the YAML encoder.
utils/config_policy.go Uses YAMLEncode for policy config persistence and YAML dumps.
subcommands/service/show.go Uses utils.YAMLEncode for YAML output of service configuration.
subcommands/config/config.go Uses utils.YAMLEncode for YAML output in config store show path.
config/load.go Uses utils.YAMLEncode when writing config YAML files to disk.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread utils/marshal.go
@omar-polo

Copy link
Copy Markdown
Contributor Author

this is still marked as draft as it depends on #2095 to go in first.

@omar-polo
omar-polo force-pushed the op/refactor-config branch 2 times, most recently from 55c8597 to 3f5df7a Compare June 16, 2026 08:51
@omar-polo
omar-polo force-pushed the op/refactor-config branch 3 times, most recently from 41b79d3 to 405c2de Compare July 8, 2026 20:20
Base automatically changed from op/refactor-config to main July 14, 2026 03:50
unlike json.NewEncoder(), yaml Encoder needs to be Close()d to
properly flush.  Introduce an helper and use it across the codebase to
avoid forgetting to do so.
@omar-polo
omar-polo marked this pull request as ready for review July 21, 2026 15:38
Copilot AI review requested due to automatic review settings July 21, 2026 15:39
@omar-polo
omar-polo force-pushed the op/yaml-decoder-close branch from 3f1adff to 6ff0e1a Compare July 21, 2026 15:39

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated no new comments.

Comments suppressed due to low confidence (2)

config/load.go:168

  • tmpFile.Close() errors are ignored. If the close fails (e.g., delayed write/FS error), the code may still proceed as if the YAML write succeeded and (in the success path) rename an incompletely flushed file. Capture the close error and treat it like an encode failure before renaming.
	err = utils.YAMLEncode(tmpFile, src)
	tmpFile.Close()

	if err == nil {
		err = os.Rename(tmpFile.Name(), file)

utils/config_policy.go:211

  • tmpFile.Close() errors are ignored. If the close fails, the file may not be fully flushed but the code can still rename it, producing a partial/corrupted policy file. Capture the close error and treat it like an encode failure before renaming.
	err = YAMLEncode(tmpFile, c)
	tmpFile.Close()
	if err == nil {
		err = os.Rename(tmpFile.Name(), filename)
	}

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