Open 9999years opened 5 months ago
Whatever kind of dark magic Nix does here, I'd say that not implementing that and giving an error is an acceptable thing to do here
Likely the carriage return stops the one-line comment: https://github.com/NixOS/nix/blob/master/src/libexpr/lexer.l#L284
eol
from Megaparsec only understands \n
. Something like this might work, but change things elsewhere? position values might get messed up? Have to make sure the "\r\n" sequence is replaced first.
diff --git a/src/Nixfmt/Lexer.hs b/src/Nixfmt/Lexer.hs
index b331359..996e847 100644
--- a/src/Nixfmt/Lexer.hs
+++ b/src/Nixfmt/Lexer.hs
@@ -17,7 +17,7 @@ import Data.Text as Text
import Text.Megaparsec
(SourcePos(..), anySingle, chunk, getSourcePos, hidden, many, manyTill, some,
try, unPos, (<|>))
-import Text.Megaparsec.Char (eol)
+import Text.Megaparsec.Char (string, eol)
import Nixfmt.Types (Ann(..), Parser, TrailingComment(..), Trivia, Trivium(..))
import Nixfmt.Util (manyP)
@@ -32,7 +32,7 @@ preLexeme :: Parser a -> Parser a
preLexeme p = p <* manyP (\x -> isSpace x && x /= '\n' && x /= '\r')
newlines :: Parser ParseTrivium
-newlines = PTNewlines <$> Prelude.length <$> some (preLexeme eol)
+newlines = PTNewlines <$> Prelude.length <$> some (preLexeme (eol <|> string "\r"))
splitLines :: Text -> [Text]
splitLines = dropWhile Text.null . dropWhileEnd Text.null
Description
nixfmt
errors when a carriage return is used as a linebreak in a comment.Small example input
(Where
^M
is a literal carriage return character.)We can see that
nix-instantiate
parses the file OK:Actual output