Skip to content

edit: keep a CRLF file's line endings when SetYAML, DelYAML and EditYAMLStrings rewrite it - #831

Merged
yetone merged 2 commits into
yetone:mainfrom
charan-rathore:edit-keep-crlf-yaml
Oct 4, 2026
Merged

yetone merged 2 commits into
yetone:mainfrom
charan-rathore:edit-keep-crlf-yaml

Conversation

@charan-rathore

Copy link
Copy Markdown
Contributor

Same family as #829 and #830, for the YAML round trip. writeYAML renders the whole document with the YAML encoder, which writes \n. A YAML file saved with \r\n came back entirely LF after any SetYAML, DelYAML or EditYAMLStrings, so changing one key rewrote every line.

The result is now written back through keepCRLF, which turns every line break into \r\n when the original used \r\n for all of them. An LF or mixed file is written as before.

This branch is stacked on #830 (it uses the keepCRLF that PR adds), so the diff shows that commit until #830 merges; the YAML change is the last commit, internal/edit/yaml.go and crlf_yaml_test.go.

Tests: TestSetYAMLKeepsCRLF fails on the old code; TestSetYAMLKeepsLF pins that an LF file is unchanged.

Run: go vet and go test -tags nogui ./internal/edit ./internal/agent ./internal/library ./internal/settings pass here, and ./internal/edit passes on current main with this merged. Not run: the full ./... suite and the GUI tests. Prepared with AI assistance.

…replaced

SetJSON splices new lines into the file with \n. In a JSON or JSONC file saved with \r\n (VS Code on Windows does that), a member added to an object or a nested value came back with a bare \n among the \r\n lines. SetJSON of "b" on "{\r\n  \"a\": 1\r\n}\r\n" gave "{\n  \"b\": 2,\r\n  \"a\": 1\r\n}\r\n".

The result is written back through keepCRLF, which turns every line break into \r\n when the original used \r\n for all of them. Untouched lines already carry their \r, so only the added lines change; a file that mixed endings is written as before.

TestSetJSONKeepsCRLF fails on the old code; TestSetJSONKeepsMixedEndings pins that a mixed file is left alone. go vet and go test ./internal/edit pass here and on current main with this merged.
…AMLStrings rewrite it

writeYAML renders the whole document with the YAML encoder, which writes \n. A YAML file saved with \r\n (the usual case for one edited on Windows) came back entirely LF after any SetYAML, DelYAML or EditYAMLStrings, so an edit of one key rewrote every line of the file and showed up as a full-file diff.

The result is written back through keepCRLF, which turns every line break into \r\n when the original used \r\n for all of them. A LF or mixed file is written as before.

TestSetYAMLKeepsCRLF fails on the old code; TestSetYAMLKeepsLF pins that an LF file is unchanged. This builds on the keepCRLF added for SetJSON in the previous commit (#830). go vet and go test ./internal/edit ./internal/agent ./internal/library ./internal/settings pass.
@yetone
yetone merged commit 05295e5 into yetone:main Oct 4, 2026
8 checks passed
yetone added a commit that referenced this pull request Oct 4, 2026
… with no trailing break, in a multi-line TOML string, a YAML block scalar and after DelJSON (review of #829, #830, #831) — tests only. TestReviewLFUntouched and TestReviewCRLFEdges; the CRLF cases fail on the code before those PRs.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@yetone

yetone commented Oct 4, 2026

Copy link
Copy Markdown
Owner

Thanks @charan-rathore, merged and released in v0.1.915.

I pulled #829, #830 and #831 together onto main and checked them:

  • go vet passes, the darwin, linux-nogui and windows builds pass, and go test -tags nogui ./... passes under a temp HOME.
  • Your CRLF tests fail on the old code, as they should.

I also added review tests (internal/edit/crlf_review_test.go, 2bfed7a). They cover:

  • LF files coming through SetEnvFile, DelEnvFile, the TOML setters, SetYAMLTop, DelYAMLTop, SetJSON, DelJSON, SetYAML and EditYAMLStrings byte-for-byte LF;
  • a CRLF dotenv file with no trailing line break;
  • a multi-line TOML string, which keeps \r\n inside it;
  • a YAML block scalar, which still reads back as one\ntwo\n;
  • DelJSON on a CRLF file.

All of them pass.

@charan-rathore
charan-rathore deleted the edit-keep-crlf-yaml branch October 4, 2026 20:32
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.

2 participants