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

Escape ${ in strings when printing Nix expressions #1019

Merged
merged 5 commits into from
Jan 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions src/Nix/Pretty.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{-# language CPP #-}
{-# language AllowAmbiguousTypes #-}
{-# language ViewPatterns, PatternSynonyms, OverloadedStrings #-}


{-# options_ghc -fno-warn-name-shadowing #-}

Expand Down Expand Up @@ -99,20 +101,27 @@ wrapPath op sub =
("\"${" <> withoutParens sub <> "}\"")
(wasPath sub)


infixr 5 :<
pattern (:<) :: Char -> Text -> Text
pattern t :< ts <- (Text.uncons -> Just (t, ts))
where (:<) = Text.cons

escapeDoubleQuoteString :: Text -> Text
escapeDoubleQuoteString ('"':<xs) = "\\\"" <> escapeDoubleQuoteString xs
escapeDoubleQuoteString ('$':<'{':<xs) = "\\${" <> escapeDoubleQuoteString xs
escapeDoubleQuoteString ('$':<xs) = '$' :< escapeDoubleQuoteString xs
escapeDoubleQuoteString (x:<xs) = maybe (one x) (('\\' :<) . one) (toEscapeCode x)
<> escapeDoubleQuoteString xs
escapeDoubleQuoteString a = a


prettyString :: NString (NixDoc ann) -> Doc ann
prettyString (DoubleQuoted parts) = "\"" <> foldMap prettyPart parts <> "\""
where
-- It serializes Text -> String, because the helper code is done for String,
-- please, can someone break that code.
prettyPart (Plain t) = pretty . foldMap escape . toString $ t
prettyPart (Plain t) = pretty $ escapeDoubleQuoteString t
prettyPart EscapedNewline = "''\\n"
prettyPart (Antiquoted r) = "${" <> withoutParens r <> "}"
escape '"' = "\\\""
escape x =
maybe
(one x)
(('\\' :) . one)
(toEscapeCode x)
prettyString (Indented _ parts) = group $ nest 2 $ vcat
["''", content, "''"]
where
Expand Down Expand Up @@ -382,6 +391,7 @@ prettyNThunk t =
"(" <> fold (one "thunk from: " <> (prettyOriginExpr . _originExpr <$> ps)) <> ")"
]


-- | This function is used only by the testing code.
printNix :: forall t f m . MonadDataContext f m => NValue t f m -> Text
printNix = iterNValueByDiscardWith thk phi
Expand All @@ -390,7 +400,7 @@ printNix = iterNValueByDiscardWith thk phi

phi :: NValue' t f m Text -> Text
phi (NVConstant' a ) = atomText a
phi (NVStr' ns) = show $ ignoreContext ns
phi (NVStr' ns) = "\"" <> escapeDoubleQuoteString (ignoreContext ns) <> "\""
phi (NVList' l ) = "[ " <> unwords l <> " ]"
phi (NVSet' _ s) =
"{ " <>
Expand Down
1 change: 1 addition & 0 deletions tests/NixLanguageTests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ newFailingTests = Set.fromList
, "eval-okay-path" -- #128
, "eval-okay-types"
, "eval-okay-fromTOML"
, "eval-okay-ind-string" -- #1000 #610
]

-- | Upstream tests that test cases that HNix disaded as a misfeature that is used so rarely
Expand Down
2 changes: 1 addition & 1 deletion tests/PrettyTests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ case_string_antiquotation =
do
assertPretty
(mkStr "echo $foo")
"\"echo \\$foo\""
"\"echo $foo\""
assertPretty
(mkStr "echo ${foo}")
"\"echo \\${foo}\""
Expand Down
2 changes: 2 additions & 0 deletions tests/eval-compare/ind-string-18.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# to observe the upstream nix behavior for dollar sign excape in doublequote string
"\${foo \$ $foo"
4 changes: 4 additions & 0 deletions tests/eval-compare/ind-string-19.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# to observe the upstream nix behavior for dollar excape in Indented string
''
''$ $ ''${
''