Skip to content

edit: escape backslashes when quoting dotenv values on write - #450

Merged
yetone merged 3 commits into
yetone:mainfrom
charan-rathore:fix-env-backslash-escape
Oct 1, 2026
Merged

yetone merged 3 commits into
yetone:mainfrom
charan-rathore:fix-env-backslash-escape

Conversation

@charan-rathore

Copy link
Copy Markdown
Contributor

SetEnvFile quotes a value containing a space or special character as a double-quoted dotenv string but only escapes double quotes, not backslashes. A Windows path like MODEL_DIR=C:\new folder\tmp is written as "C:\new folder\tmp", and any dotenv reader that expands escapes (GetEnvFile does, and Gemini CLI's own dotenv does the same) reads it back as "C:ew foldertmp". The file is corrupted on first write.

Escape backslashes before escaping quotes in the quoting branch, so the written form round-trips exactly.

@yetone

yetone commented Oct 1, 2026

Copy link
Copy Markdown
Owner

Thanks! The round trip through magpie's own reader is right, but the file's real reader is Gemini CLI, and its dotenv (npm dotenv, checked against the 0.62 bundle) only expands \n/\r inside double quotes — \\ and \" stay literal. So with this change C:\Program Files\key x is written as "C:\\Program Files\\key x" and Gemini reads C:\\Program Files\\key x (it read it correctly before). Could you write the value single-quoted when it contains no ' or newline (K='C:\new folder\tmp')? Both npm dotenv and GetEnvFile take single-quoted text literally, so it round-trips exactly for both; keep the double-quoted form only as the fallback for values containing '. A test that also checks the written line (K='…') would pin it. (#439 has just been merged, so please rebase on main.)

@charan-rathore

Copy link
Copy Markdown
Contributor Author

Thanks, that makes sense. Merged current main (clean, #439's parser is preserved) and pushed 0571cc0.

Values that need quoting are now written single-quoted when they contain no ', CR or LF, so K='C:\new folder\tmp' is read literally by both GetEnvFile and a dotenv that only expands escapes in double quotes. The double-quoted escaped form stays as the fallback for values with ' or newlines. The round-trip test now also asserts the exact written line.

I checked the single-quoted path against the current npm dotenv (18.0.5): it parses back exactly. I did not test against Gemini CLI's pinned 0.62 bundle, so that part rests on your check. internal/edit and the Gemini/Kimi agent tests pass; I did not run the full agent suite.

@yetone

yetone commented Oct 1, 2026

Copy link
Copy Markdown
Owner

Thanks — re-reviewed at 0571cc0, merged onto current main: looks good to merge.

I checked the written file against the dotenv parser bundled in Gemini CLI 0.62.0 (copied verbatim from the bundle), npm dotenv 17.4.2, python-dotenv 1.2.4, godotenv 1.5.1 and magpie's GetEnvFile: Windows paths like C:\new folder\tmp, say "hi", $HOME/x y and literal backslash sequences now come back exactly from all five (on main, Gemini read the Windows path with a doubled backslash). In a sandbox HOME, magpie gemini auth api-key with a key containing C:\new\tmp writes it single-quoted, keeps the user's other lines, and Gemini's parser reads it back unchanged.

TestEnvBackslashRoundtrip fails without the env.go change; vet, the darwin/linux/windows builds and go test -tags nogui ./... pass.

(Only remaining corner, not blocking: a value with both ' and \ still can't round-trip through Gemini's dotenv, which can't express it in double quotes. Magpie never writes such values here.)

@yetone
yetone merged commit 02276e6 into yetone:main Oct 1, 2026
7 checks passed
@charan-rathore
charan-rathore deleted the fix-env-backslash-escape branch October 1, 2026 19:19
yetone added a commit that referenced this pull request Oct 28, 2026
…plugins #450)

workbuddy 0.1.41 (plugins #450, MYCF711) sends a package with no cycle
total as a "Prepaid credits" balance window (#3950). npm view
--prefer-online showed 0.1.41 before this change.

Run on mote at this tree: gofmt -l internal/provider (clean), go vet
./internal/provider, go test -tags nogui -run 'Mover|Moved|Migrat|
WorkBuddy|Min' ./internal/provider ok. Not run: the full suite (a
one-line version string).
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