From 2b6459cb789377a2559243f810f944b51aea0cd6 Mon Sep 17 00:00:00 2001 From: Omar Jatoi Date: Sat, 7 Feb 2026 14:02:41 -0500 Subject: [PATCH] Fix parser warnings order See https://github.com/haskell/cabal/issues/11269 for more information. This PR sorts the parse results by position so they're displayed in file order. Resolves: #11269 --- Cabal-syntax/src/Distribution/Fields/ParseResult.hs | 10 ++++++---- .../tests/ParserTests/regressions/common.format | 4 ++-- Cabal-tests/tests/ParserTests/regressions/elif.format | 2 +- .../ParserTests/regressions/wl-pprint-indef.format | 4 ++-- changelog.d/issue-11269.md | 10 ++++++++++ 5 files changed, 21 insertions(+), 9 deletions(-) create mode 100644 changelog.d/issue-11269.md diff --git a/Cabal-syntax/src/Distribution/Fields/ParseResult.hs b/Cabal-syntax/src/Distribution/Fields/ParseResult.hs index 6c2dbe5e552..0abb2e3c54b 100644 --- a/Cabal-syntax/src/Distribution/Fields/ParseResult.hs +++ b/Cabal-syntax/src/Distribution/Fields/ParseResult.hs @@ -68,12 +68,14 @@ runParseResult pr = unPR pr emptyPRState initialCtx failure success where initialCtx = PRContext PUnknownSource - failure (PRState warns [] v) = (warns, Left (v, PErrorWithSource PUnknownSource (PError zeroPos "panic") :| [])) - failure (PRState warns (err : errs) v) = (warns, Left (v, err :| errs)) + failure (PRState warns [] v) = (sortWarns warns, Left (v, PErrorWithSource PUnknownSource (PError zeroPos "panic") :| [])) + failure (PRState warns (err : errs) v) = (sortWarns warns, Left (v, err :| errs)) - success (PRState warns [] _) x = (warns, Right x) + success (PRState warns [] _) x = (sortWarns warns, Right x) -- If there are any errors, don't return the result - success (PRState warns (err : errs) v) _ = (warns, Left (v, err :| errs)) + success (PRState warns (err : errs) v) _ = (sortWarns warns, Left (v, err :| errs)) + + sortWarns = sortBy (comparing (pwarningPosition . pwarning)) -- | Chain parsing operations that involve 'IO' actions. liftParseResult :: (a -> IO (ParseResult src b)) -> ParseResult src a -> IO (ParseResult src b) diff --git a/Cabal-tests/tests/ParserTests/regressions/common.format b/Cabal-tests/tests/ParserTests/regressions/common.format index 7fb317a16a5..6f46a4cbc04 100644 --- a/Cabal-tests/tests/ParserTests/regressions/common.format +++ b/Cabal-tests/tests/ParserTests/regressions/common.format @@ -1,6 +1,6 @@ -common.cabal:29:3: Unknown field: import. You should set cabal-version: 2.2 or larger to use common stanzas -common.cabal:20:3: Unknown field: import. You should set cabal-version: 2.2 or larger to use common stanzas common.cabal:14:1: Ignoring section: common. You should set cabal-version: 2.2 or larger to use common stanzas. +common.cabal:20:3: Unknown field: import. You should set cabal-version: 2.2 or larger to use common stanzas +common.cabal:29:3: Unknown field: import. You should set cabal-version: 2.2 or larger to use common stanzas cabal-version: >=1.10 name: common version: 0 diff --git a/Cabal-tests/tests/ParserTests/regressions/elif.format b/Cabal-tests/tests/ParserTests/regressions/elif.format index 949d4432c1f..e55d76716c7 100644 --- a/Cabal-tests/tests/ParserTests/regressions/elif.format +++ b/Cabal-tests/tests/ParserTests/regressions/elif.format @@ -1,5 +1,5 @@ -elif.cabal:19:3: invalid subsection "else" elif.cabal:17:3: invalid subsection "elif". You should set cabal-version: 2.2 or larger to use elif-conditionals. +elif.cabal:19:3: invalid subsection "else" cabal-version: >=1.10 name: elif version: 0 diff --git a/Cabal-tests/tests/ParserTests/regressions/wl-pprint-indef.format b/Cabal-tests/tests/ParserTests/regressions/wl-pprint-indef.format index 1a16e964b06..7290991fc61 100644 --- a/Cabal-tests/tests/ParserTests/regressions/wl-pprint-indef.format +++ b/Cabal-tests/tests/ParserTests/regressions/wl-pprint-indef.format @@ -1,6 +1,6 @@ -wl-pprint-indef.cabal:28:3: The field "mixins" is available only since the Cabal specification version 2.0. This field will be ignored. -wl-pprint-indef.cabal:27:3: The field "signatures" is available only since the Cabal specification version 2.0. This field will be ignored. wl-pprint-indef.cabal:23:3: The field "mixins" is available only since the Cabal specification version 2.0. This field will be ignored. +wl-pprint-indef.cabal:27:3: The field "signatures" is available only since the Cabal specification version 2.0. This field will be ignored. +wl-pprint-indef.cabal:28:3: The field "mixins" is available only since the Cabal specification version 2.0. This field will be ignored. cabal-version: >=1.6 name: wl-pprint-indef version: 1.2 diff --git a/changelog.d/issue-11269.md b/changelog.d/issue-11269.md new file mode 100644 index 00000000000..b1eebc203f7 --- /dev/null +++ b/changelog.d/issue-11269.md @@ -0,0 +1,10 @@ +--- +synopsis: Fix parse warnings being emitted in reverse order +packages: [Cabal-syntax] +prs: 11479 +issues: 11269 +--- + +Parse warnings were prepended to the accumulator list, causing them to +be emitted in reverse file order. They are now sorted so they come out +in file order, consistent with GHC's behavior.