Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support comments in multiline input #16

Merged
merged 1 commit into from Aug 30, 2022

Conversation

domodwyer
Copy link

Support parsing lines that contain comments & quotes.

Previously this input:

# Start of .env file
# Comment line with single ' quote
# Comment line with double " quote
 # Comment line with double " quote and starts with a space
TESTKEY=test_val # A '" comment
# End of .env file

Would fail the regression test (test_issue_12):

running 1 test
test test_issue_12 ... FAILED

failures:

---- test_issue_12 stdout ----
thread 'test_issue_12' panicked at 'should succeed: LineParse("# Comment line with single ' quote\n# Comment line with double \" quote\n # Comment line with double \" quote and starts with a space\nTESTKEY=test_val # A '\" comment\n# End of .env file\n", 181)', dotenv/tests/test-multiline-comment.rs:21:14
stack backtrace:
   0: rust_begin_unwind
             at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/std/src/panicking.rs:584:5
   1: core::panicking::panic_fmt
             at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/core/src/panicking.rs:142:14
   2: core::result::unwrap_failed
             at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/core/src/result.rs:1805:5
   3: core::result::Result<T,E>::expect
             at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/core/src/result.rs:1055:23
   4: test_multiline_comment::test_issue_12
             at /Users/dom/Documents/rust/dotenvy/dotenv/tests/test-multiline-comment.rs:21:5
   5: test_multiline_comment::test_issue_12::{{closure}}
             at /Users/dom/Documents/rust/dotenvy/dotenv/tests/test-multiline-comment.rs:8:1
   6: core::ops::function::FnOnce::call_once
             at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/core/src/ops/function.rs:248:5
   7: core::ops::function::FnOnce::call_once
             at /rustc/4b91a6ea7258a947e59c6522cd5898e7c0a6a88f/library/core/src/ops/function.rs:248:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

After this commit, the env var is set as expected (test_val).

Fixes #12.


  • fix: support comments in multiline input (0bcc818)

    Fixes the line parser to respect comments when evaluating quote state.
    
    Fixes allan2/dotenvy#12.
    

Fixes the line parser to respect comments when evaluating quote state.

Fixes allan2#12.
@allan2 allan2 merged commit f9c1921 into allan2:master Aug 30, 2022
@domodwyer domodwyer deleted the dom/fix-multiline branch August 30, 2022 08:46
_ => QuoteState::StrongOpen,
ParseState::WeakOpenEscape => ParseState::WeakOpen,
ParseState::StrongOpen => match c {
'#' => return ParseState::Comment,
Copy link

Choose a reason for hiding this comment

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

Here, a regression is introduced:
This line makes KEY='ABC#DEF' get truncated to KEY='ABC.

The # can be escaped for the line parser as \#, but the value parser does not handle escape characters within strong quotes, resulting in ABC\#DEF rather than ABC#DEF as final value.

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.

Regression: Multiline support breaks .env files with quotes in comments
3 participants