Skip to content

Add parse error location to unparsable environment file warning#11996

Open
wasimat404 wants to merge 4 commits into
haskell:masterfrom
wasimat404:fix/11963-env-file-parse-error
Open

Add parse error location to unparsable environment file warning#11996
wasimat404 wants to merge 4 commits into
haskell:masterfrom
wasimat404:fix/11963-env-file-parse-error

Conversation

@wasimat404

Copy link
Copy Markdown

Fixes #11963.

When a GHC environment file cannot be parsed, cabal previously warned only that the file "is unparsable", giving no indication of where or why. The underlying parser already produces a Parsec error carrying the line, column, and the unexpected/expected tokens, but that information was discarded at the warning site in getExistingEnvEntries. This surfaces it.

Before:

Warning: The environment file /home/user/.ghc/.../environments/default is unparsable. Libraries cannot be installed.

After:

Warning: The environment file /home/user/.ghc/.../environments/default is unparsable. Libraries cannot be installed.
"/home/user/.ghc/.../environments/default" (line 1, column 1):
unexpected 't'
expecting "--", "package-id", "global-package-db", "user-package-db", "package-db", "clear-package-db" or end of input

The reporter's case was a stray trailing newline, which the parser reports as "unexpected end of input" at the offending line, so this change points directly at it. The fix is message-only; parser behaviour is unchanged.

Added a cabal-testsuite package test under PackageTests/EnvironmentFile/UnparsableWarning that points cabal at a malformed environment file and asserts the warning includes the location. Verified the test fails on unpatched cabal (the location line is absent) and passes with the change.

QA Notes

Create a malformed GHC environment file, e.g. a file bad.env whose first line is this is not valid. Then run:

cabal install --lib base --package-env=bad.env

The warning should name the parse location, for example:

Warning: The environment file bad.env is unparsable. Libraries cannot be installed.
"bad.env" (line 1, column 1):
unexpected 't'
expecting "--", "package-id", ...

Before this change, only the first line ("is unparsable") was printed, with no location.

When a GHC environment file cannot be parsed, the warning now includes
the line, column, and reason from the underlying parser, instead of only
stating that the file is unparsable.

Fixes haskell#11963

Signed-off-by: wasimat404 <huaweiwasim7@gmail.com>
Signed-off-by: wasimat404 <huaweiwasim7@gmail.com>
Text.Parsec.Error.ParseError has no Exception instance on older GHCs,
so displayException does not type-check. Its Show instance already
renders the line, column, and reason.

Signed-off-by: wasimat404 <huaweiwasim7@gmail.com>
@wasimat404 wasimat404 marked this pull request as ready for review June 21, 2026 01:02
-- merely state that the file is unparsable.
-- See https://github.com/haskell/cabal/issues/11963
res <- cabal' "install" ["--lib", "base", "--package-env=" ++ envFile]
assertOutputContains "line 1, column 1" res

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a really poor test. Couldn't we get a clearer test by removing DoNotRecord? Or should we capture more output from assertOutputContains?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went with capturing more output. It now asserts on the full warning (location, unexpected character, and expected tokens), not just the line and column.
and tried removing DoNotRecord first, but the install exits successfully here, so golden recording treats that as a failure. Should I switch to golden?

Assert on the full warning message (location, unexpected character, and
expected tokens) rather than only the line and column, addressing review
feedback on test coverage.

Signed-off-by: wasimat404 <huaweiwasim7@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

warning message " ... environments/default is unparsable" should give more info (reason/location)

2 participants