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

StrPart parser unescapes double-single-quote strings containing double quotes incorrectly #69

Closed
fogti opened this issue Jan 12, 2022 · 3 comments · Fixed by #101
Closed
Labels
bug Something isn't working
Milestone

Comments

@fogti
Copy link
Contributor

fogti commented Jan 12, 2022

Describe the bug

Fails to parse a segment of nixpkgs/lib/systems/parse.nix containing a double-single-quote string which contains double quotes.

Code Snippet to reproduce

https://github.com/NixOS/nixpkgs/blob/698d8759c90f2e811b1044b999b91a0e963772bb/lib/systems/parse.nix#L343-L345

''
  The "android" ABI is not for 32-bit ARM. Use "androideabi" instead.
''

Test case for value.rs

diff --git a/src/value.rs b/src/value.rs
index 05d5674..4b093f2 100644
--- a/src/value.rs
+++ b/src/value.rs
@@ -330,6 +330,18 @@ mod tests {
                 ))
             ]
         );
+
+        assert_eq!(
+            string_parts(&string_node(
+                // found in https://github.com/NixOS/nixpkgs/blob/698d8759c90f2e811b1044b999b91a0e963772bb/lib/systems/parse.nix#L343-L345
+                r#"The "android" ABI is not for 32-bit ARM. Use "androideabi" instead."#
+            )),
+            vec![
+                StrPart::Literal(String::from(
+                    "The \"android\" ABI is not for 32-bit ARM. Use \"androideabi\" instead.\n"
+                ))
+            ]
+        );
     }
     #[test]
     fn values() {

Current behavoir

Stops parsing the string after "The ".
see also: https://gist.github.com/zseri/bb8ed4f47123960f4231d6fa928599ac#file-parse-js-L1093

Expected behavior

The string should parse correctly

Additional context

rnix version: 2e94865

@fogti fogti added the bug Something isn't working label Jan 12, 2022
@Ma27
Copy link
Member

Ma27 commented Jan 12, 2022

I'd expect the test-case you posted to fail since there are no quotes for Nix to parse after the r#"that seems to be the rust-level quotation.

Also, an expression such as the one you posted -

''
  The "android" ABI is not for 32-bit ARM. Use "androideabi" instead.
''

seems to be parsed perfectly fine:

$ cargo run --example dump-ast foo
    Finished dev [unoptimized + debuginfo] target(s) in 0.03s
     Running `target/debug/examples/dump-ast foo`
NODE_ROOT 0..76 {
  NODE_STRING 0..75 {
    TOKEN_STRING_START("\'\'") 0..2
    TOKEN_STRING_CONTENT("\n  The \"android\" ABI is not for 32-bit ARM. Use \"androideabi\" instead.\n") 2..73
    TOKEN_STRING_END("\'\'") 73..75
  }
  TOKEN_WHITESPACE("\n") 75..76
}

Am I missing something?

@fogti
Copy link
Contributor Author

fogti commented Jan 12, 2022

The StrPart API in value.rs doesn't correctly reconstruct the single contained StrPart::Literal string.
Keep in mind that the test case I posted uses the string_node function which adds the double-single-quotes.

Probably at fault:

rnix-parser/src/value.rs

Lines 267 to 275 in 2e94865

if multiline {
*text = remove_indent(text, i == 0, common);
if i == literals - 1 {
// Last index
remove_trailing(text);
}
}
*text = unescape(text, multiline);
i += 1;

Some('"') if multiline => break,

@fogti fogti changed the title Fails to parse double-single-quote strings containing double quotes StrPart parser unescapes double-single-quote strings containing double quotes incorrectly Jan 12, 2022
@Ma27
Copy link
Member

Ma27 commented Jan 17, 2022

Ohh I see. I'll try to take a look soonish.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants