Skip to content

fix: write set_key values containing single quotes as double-quoted - #647

Open
SAY-5 wants to merge 1 commit into
theskumar:mainfrom
SAY-5:fix-set-key-single-quote
Open

SAY-5 wants to merge 1 commit into
theskumar:mainfrom
SAY-5:fix-set-key-single-quote

Conversation

@SAY-5

@SAY-5 SAY-5 commented May 13, 2026

Copy link
Copy Markdown

When a value contains a ', set_key previously emitted \' inside a single-quoted shell value, which is not valid POSIX shell (so source .env fails) and does not round-trip through dotenv's parser. Such values are now written double-quoted with \ and " escaped, which is shell-source-safe and parses back to the original value. Closes #543.

@SAY-5

SAY-5 commented Aug 16, 2026

Copy link
Copy Markdown
Author

Ping on this. The gist is that set_key writes \' inside a single quoted value, which isn't valid POSIX shell, so source .env fails and the value doesn't round trip through the parser either (#543). Writes those double quoted now.

@SAY-5
SAY-5 force-pushed the fix-set-key-single-quote branch from 693a637 to 88ab642 Compare August 23, 2026 06:34
@SAY-5

SAY-5 commented Aug 23, 2026

Copy link
Copy Markdown
Author

Rebased on top of #680. That one fixed the parser round-trip by escaping the quote inside the single-quoted value, but the line it writes (VAR='I\'m a bug') still isn't valid shell, which is the part #543 is actually about, so this keeps the double-quoted form for values containing a ' and leaves #680's backslash escaping in place for everything else. Test expectations updated accordingly.

Signed-off-by: Sai Asish Y <say.apm35@gmail.com>
@SAY-5
SAY-5 force-pushed the fix-set-key-single-quote branch from 88ab642 to ea79fc9 Compare October 5, 2026 19:12
@lexingtonhibiki

Copy link
Copy Markdown

AI-assisted independent check at ea79fc9d983589d77603c912c9c5bc1e9548bedd: the default, non-export output still changes a literal value when sourced.

With DOTENV_PROBE=expanded, this call:

set_key(path, "VALUE", "I'm $DOTENV_PROBE")

writes VALUE="I'm $DOTENV_PROBE". dotenv_values(path, interpolate=False)["VALUE"] returns the original I'm $DOTENV_PROBE, but sourcing the file in bash gives I'm expanded.

Could a regression test require the original value through both readers? #670 handles shell escaping for export=True, but does not cover this default path. Simply escaping the dollar also needs a dotenv-readback check: VALUE="I'm \$DOTENV_PROBE" currently retains the extra backslash in the parsed value.

I reproduced this using only temporary files and generated literal values on Linux/Python 3.12. The variable case fails with both always and auto; plain apostrophe and multiline/backslash controls pass. Main is not a passing shell baseline either: its apostrophe output fails shell syntax. This is an incomplete round-trip fix, not a claim that the original shell path worked. I have not run the full upstream suite; no human-review approval is claimed.

This branch has not been deployed

No deployments
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.

set_key does not handle single quote ' inside value properly

2 participants