Skip to content

Commit f34aba9

Browse files
committed
Make a trailing colon for stanzas a parse failure
Here's a mistake I make semi-regularly: source-repository-package: type: git location: https://github.com/parsonsmatt/foundation tag: 688c32ccd9a951bc96dd09423a6e6684f091d510 subdir: basement subdir: foundation Cabal treats this as a warning, so it prints: Warning: cabal.project: Unrecognized field 'source-repository-package' on line 52 This is fine (if you already know the mistake you've made, at least!), but it's very easy to miss amidst lots of output. I often re-run `cabal` when I see a ton of output to attempt to get a smaller error message. (Usually it works and I get an error message that's got less "compiling module such and such" noise in it.) However, re-running `cabal` will discard this warning entirely! Let's make it a hard error instead. This is a backwards-compatibility break.
1 parent f4c0583 commit f34aba9

File tree

7 files changed

+51
-7
lines changed

7 files changed

+51
-7
lines changed

cabal-install/src/Distribution/Client/ParseUtils.hs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
{-# LANGUAGE ExistentialQuantification #-}
22
{-# LANGUAGE NamedFieldPuns #-}
33
{-# LANGUAGE RankNTypes #-}
4+
{-# LANGUAGE ScopedTypeVariables #-}
5+
{-# LANGUAGE TypeApplications #-}
46

57
-----------------------------------------------------------------------------
68

@@ -53,6 +55,7 @@ import Distribution.Deprecated.ParseUtils
5355
( Field (..)
5456
, FieldDescr (..)
5557
, LineNo
58+
, PError (..)
5659
, ParseResult (..)
5760
, liftField
5861
, lineNo
@@ -292,13 +295,16 @@ parseFieldsAndSections fieldDescrs sectionDescrs fgSectionDescrs =
292295
setField a (F line name value) =
293296
case Map.lookup name fieldMap of
294297
Just (FieldDescr _ _ set) -> set line value a
295-
Nothing -> do
296-
warning $
297-
"Unrecognized field '"
298-
++ name
299-
++ "' on line "
300-
++ show line
301-
return a
298+
Nothing ->
299+
case Left <$> Map.lookup name sectionMap <|> Right <$> Map.lookup name fgSectionMap of
300+
Just _ -> ParseFailed $ FieldShouldBeStanza name line
301+
Nothing -> do
302+
warning $
303+
"Unrecognized field '"
304+
++ name
305+
++ "' on line "
306+
++ show line
307+
return a
302308
setField a (Section line name param fields) =
303309
case Left <$> Map.lookup name sectionMap <|> Right <$> Map.lookup name fgSectionMap of
304310
Just (Left (SectionDescr _ fieldDescrs' sectionDescrs' _ set sectionEmpty)) -> do

cabal-install/src/Distribution/Deprecated/ParseUtils.hs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ data PError
9191
= AmbiguousParse String LineNo
9292
| NoParse String LineNo
9393
| TabsError LineNo
94+
| FieldShouldBeStanza String LineNo
9495
| FromString String (Maybe LineNo)
9596
deriving (Eq, Show)
9697

@@ -186,6 +187,10 @@ locatedErrorMsg (NoParse f n) =
186187
, "Parse of field '" ++ f ++ "' failed."
187188
)
188189
locatedErrorMsg (TabsError n) = (Just n, "Tab used as indentation.")
190+
locatedErrorMsg (FieldShouldBeStanza name lineNumber) =
191+
( Just lineNumber
192+
, "'" ++ name ++ "' is a stanza, not a field. Remove the trailing ':' to parse a stanza."
193+
)
189194
locatedErrorMsg (FromString s n) = (n, s)
190195

191196
syntaxError :: LineNo -> String -> ParseResult a
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# cabal build
2+
Error: [Cabal-7090]
3+
Error parsing project file <ROOT>/cabal.project:4:
4+
'source-repository-package' is a stanza, not a field. Remove the trailing ':' to parse a stanza.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
packages: .
2+
3+
-- This is an error; a trailing `:` is syntax for a field, not a stanza!
4+
source-repository-package:
5+
type: git
6+
location: https://github.com/haskell-hvr/Only
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import Test.Cabal.Prelude
2+
3+
main = cabalTest $ do
4+
result <- fails $ cabal' "build" []
5+
assertOutputContains "Error parsing project file" result
6+
assertOutputContains "'source-repository-package' is a stanza, not a field." result
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module MyLib (someFunc) where
2+
3+
someFunc :: IO ()
4+
someFunc = putStrLn "someFunc"
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
cabal-version: 3.0
2+
name: test
3+
version: 0.1.0.0
4+
license: NONE
5+
6+
maintainer: Rebecca Turner
7+
build-type: Simple
8+
9+
library
10+
exposed-modules: MyLib
11+
build-depends: base
12+
hs-source-dirs: src
13+
default-language: Haskell2010

0 commit comments

Comments
 (0)