python-dotenv icon indicating copy to clipboard operation
python-dotenv copied to clipboard

fix: restore multiline support for unquoted values (fixes #555)

Open Pritish053 opened this issue 5 months ago • 2 comments

Restores multiline support for unquoted environment variables that was inadvertently removed in commit 7b172fe during space parsing improvements. This enhancement allows load_dotenv() to correctly parse multiline values without quotes while maintaining backward compatibility.

Implementation details:

  • Adds intelligent lookahead parsing to distinguish continuation lines from new variable declarations
  • Uses contextual heuristics: single characters treated as variable names, multi-character content as value continuations
  • Preserves existing behavior for all current use cases
  • Maintains proper line tracking and error handling

Comprehensive testing ensures no regressions across the existing 198-test suite.

PR Description

Summary

Resolves #555 by restoring the ability for load_dotenv() to correctly parse multiline unquoted environment variables that span multiple lines.

Problem

In 2020, commit 7b172fe simplified the parse_unquoted_value() function to fix space parsing issues, but inadvertently removed support for multiline unquoted values. This caused load_dotenv() to only read the first line of multiline values.

Solution

Restored multiline parsing capability through intelligent continuation line detection:

  • Smart heuristics: Distinguishes between value continuations and new variable declarations
  • Context-aware parsing: Single-character lines interpreted as variables, longer content as continuations
  • Backward compatibility: All existing parsing behavior preserved
  • Robust error handling: Maintains proper position tracking and graceful failure recovery

Impact

Before (Broken):

BAZ1=baz
baz
baz
→ Result: BAZ1="baz" (incomplete)

After (Fixed):
BAZ1=baz
baz
baz
→ Result: BAZ1="baz\nbaz\nbaz" (complete)

Edge cases preserved:
a=b
c
→ Result: a="b", c=None (separate variables)

Verification

- ✅ New test suite in tests/test_multiline.py
- ✅ All 198 existing tests pass
- ✅ Code style compliance (ruff)
- ✅ No behavioral regressions

Pritish053 avatar Jul 20 '25 11:07 Pritish053

@bbc2 would you have time to take a look this issue?

theskumar avatar Oct 26 '25 14:10 theskumar

The issue mentioned here (#555) refers to quoted values, which is different from what is being tackled by this PR, so the assertion that this PR fixes the mentioned issue seems false.

Also, the file format only allows multi-line values if quotes are used: https://github.com/theskumar/python-dotenv?tab=readme-ov-file#multiline-values. I'm not saying we can't change that, but this most likely not a bug. By the way, we have done breaking changes in the past (https://github.com/theskumar/python-dotenv/issues/170) although we should certainly avoid them as much as possible.

I'm sorry but so far I don't see enough justification for applying this change. My suggestion is to close this PR unless we get evidence that the current behavior is an issue. @Pritish053 And in any case, I thank you for your interest in the project.

Extra notes:

  • I didn't have this "unquoted newline" case in my comparisons (https://bbc2.github.io/dotenv-parser-comparisons/). It might be interesting to add but I can't promise anything.
  • The code of this PR can't be merged as is. I haven't done a complete review but I see at least two blockers (removal of an important test suite and parsing code in main.py which should probably be in parser.py. In any case, the code is probably easy to fix once a decision is made on the desired behavior of the tool.

bbc2 avatar Nov 06 '25 23:11 bbc2