Skip to content

“File name does not match module name” error is swallowed with `-fdefer-diagnostics`

Summary

When a file's path doesn't match its declared module name (e.g., src/MyLib.hs begins with module MyLbi where), GHC raises a “File name does not match module name” error. However, when the -fdefer-diagnostics GHC flag is used, this error message is silently swallowed. This leads to very confusing errors:

$ cabal build mwb:lib:test-dev
Build profile: -w ghc-9.6.1 -O0
In order, the following will be built (use -v for more details):
 - mwb-0 (lib:test-dev) (first run)
Preprocessing library 'test-dev' for mwb-0..
Building library 'test-dev' for mwb-0..
Error: cabal: Failed to build lib:test-dev from mwb-0.

Steps to reproduce

With bad-module-name.cabal:

name:               bad-module-name
version:            0.1.0.0
build-type:         Simple

library
    exposed-modules:  MyLib
    build-depends:    base
    hs-source-dirs:   src
    ghc-options:      -fdefer-diagnostics

And src/MyLib.hs:

module MyLibb () where

Then we get:

$ cabal build
Build profile: -w ghc-9.6.1 -O1
In order, the following will be built (use -v for more details):
 - bad-module-name-0.1.0.0 (lib:bad-module-name) (file src/MyLib.hs changed)
Preprocessing library for bad-module-name-0.1.0.0..
Building library for bad-module-name-0.1.0.0..
Error: cabal: Failed to build bad-module-name-0.1.0.0.

If we remove the -fdefer-diagnostics flag from the .cabal file, the error message is reported correctly:

$ cabal build
Warning: The package list for 'hackage.haskell.org' is 34 days old.
Run 'cabal update' to get the latest list of available packages.
Resolving dependencies...
Build profile: -w ghc-9.6.1 -O1
In order, the following will be built (use -v for more details):
 - bad-module-name-0.1.0.0 (lib:bad-module-name) (configuration changed)
Warning: bad-module-name.cabal:0:0: A package using section syntax must
specify at least
'cabal-version: >= 1.2'.
Configuring bad-module-name-0.1.0.0...
Preprocessing library for bad-module-name-0.1.0.0..
Building library for bad-module-name-0.1.0.0..

src/MyLib.hs:1:8: error: [GHC-28623]
    File name does not match module name:
    Saw     : ‘MyLibb’
    Expected: ‘MyLib’
  |
1 | module MyLibb () where
  |        ^^^^^^
Error: cabal: Failed to build bad-module-name-0.1.0.0.

Expected behavior

The error message should be reported to the user even with -fdefer-diagnostics.

Environment

  • GHC version used: 9.6.1

Optional:

  • Operating System: macOS 13
  • System Architecture: aarch64
$ uname -a
Darwin san-fransisco 22.4.0 Darwin Kernel Version 22.4.0: Mon Mar  6 20:59:28 PST 2023; root:xnu-8796.101.5~3/RELEASE_ARM64_T6000 arm64
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information