Stack outputs executable in source directory?

It’s really weird. I have a minimal project here:

aaya ~/g/really-weird> ls -a
.  ..  .gitignore  package.yaml  src  stack.yaml
aaya ~/g/really-weird> ls src -a
.  ..  Lib.hs  Main.hs

cat stack.yaml

snapshot: lts-24.63

packages:
- .

cat package.yaml

name:                really-weird
version:             0.1.0.0

dependencies:
  base: ">= 4.7 && < 5"

library:
  source-dirs: src

executable:
  main:                Main.hs
  source-dirs:         src
  ghc-options:
  - -threaded
  - -rtsopts
  - -with-rtsopts=-N
  dependencies:
  - really-weird

cat src/Lib.hs

module Lib
    ( someFunc
    ) where

someFunc :: IO ()
someFunc = putStrLn "someFunc"

cat src/Main.hs

module Main (main) where

import Lib

main :: IO ()
main = someFunc

Running stack build suddenly produces an executable at src/Main. Is it a bug or am I doing something wrong? This does not happen when the executable has a source directory separate from the library (e.g. source-dirs: app)

aaya ~/g/really-weird> stack build 
evaluation warning: stdenv.isLinux is deprecated, use stdenv.hostPlatform.isLinux instead
really-weird> configure (lib + exe)
Configuring really-weird-0.1.0.0...
really-weird> build (lib + exe) with ghc-9.10.3
Preprocessing library for really-weird-0.1.0.0...
Building library for really-weird-0.1.0.0...
[1 of 4] Compiling Lib
[2 of 4] Compiling Main
[3 of 4] Compiling Paths_really_weird
[4 of 4] Linking src/Main
Preprocessing executable 'really-weird' for really-weird-0.1.0.0...
Building executable 'really-weird' for really-weird-0.1.0.0...
[1 of 3] Compiling Lib
[2 of 3] Compiling Main
[3 of 3] Compiling Paths_really_weird
[4 of 4] Linking .stack-work/dist/x86_64-linux-nix/ghc-9.10.3/build/really-weird/really-weird
really-weird> copy/register
Installing library in /home/rini/git/really-weird/.stack-work/install/x86_64-linux-nix/3a180421ae9d2b6680fd33c73778ff5111f7e3346a08bfd2c574c745d51073a9/9.10.3/lib/x86_64-linux-ghc-9.10.3-b0b4/really-weird-0.1.0.0-JiATi7XOJX657wgAEDq587
Installing executable really-weird in /home/rini/git/really-weird/.stack-work/install/x86_64-linux-nix/3a180421ae9d2b6680fd33c73778ff5111f7e3346a08bfd2c574c745d51073a9/9.10.3/bin
Registering library for really-weird-0.1.0.0...
aaya ~/g/really-weird> ls src
Lib.hs  Main  Main.hs
aaya ~/g/really-weird> file src/Main
src/Main: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /nix/store/h4wfwic161kxrr74jlzla5lsm28hgary-glibc-2.44-25/lib/ld-linux-x86-64.so.2, for GNU/Linux 3.10.0, not stripped

I see your platform from the output. What is the output of stack --version?

EDIT: I can reproduce on Windows 11 with Stack 3.11.1. I’ll see if I can work out what is going on.

I have package.yaml:

spec-version: 0.36.0 # To avoid references to Paths_really_weird in Cabal file

name:                really-weird
version:             0.1.0.0

dependencies:
- base >= 4.7 && < 5

library:
  source-dirs: src

executables:
  really-weird-exe:
    main:                Main.hs
    source-dirs:         src
    ghc-options:
    - -threaded
    - -rtsopts
    - -with-rtsopts=-N
    dependencies:
    - really-weird

which generates the following really-weird.cabal. Note that the library stanza has exposed modules Lib AND Main:

cabal-version: 1.12

-- This file has been generated from package.yaml by hpack version 0.39.6.
--
-- see: https://github.com/sol/hpack

name:           really-weird
version:        0.1.0.0
license:        BSD3
license-file:   LICENSE
build-type:     Simple

library
  exposed-modules:
      Lib
      Main
  hs-source-dirs:
      src
  build-depends:
      base >=4.7 && <5
  default-language: Haskell2010

executable really-weird-exe
  main-is: Main.hs
  other-modules:
      Lib
  hs-source-dirs:
      src
  ghc-options: -threaded -rtsopts -with-rtsopts=-N
  build-depends:
      base >=4.7 && <5
    , really-weird
  default-language: Haskell2010

EDIT2: I think it must be Cabal (the library)'s doing, as Stack’s invocation of Cabal (which can be understood from stack --verbose build) seems unremarkable:

# The configure step:
Run process within D:\Users\mike\Code\Haskell\really-weird\: 
  D:\sr\setup-exe-cache\x86_64-windows\Cabal-simple_O_vy6YIf_3.12.1.0_ghc-9.10.3.exe 
    --verbose=1 
    --builddir=D:\Users\mike\Code\Haskell\really-weird\.stack-work\dist\1a191874 
    configure 
    --with-ghc=D:\sr\programs\x86_64-windows\ghc-9.10.3\bin\ghc-9.10.3.exe 
    --with-ghc-pkg=D:\sr\programs\x86_64-windows\ghc-9.10.3\bin\ghc-pkg-9.10.3.exe 
    --user 
    --package-db=clear 
    --package-db=global 
    --package-db=D:\sr\snapshots\fafe412f\pkgdb 
    --package-db=D:\Users\mike\Code\Haskell\really-weird\.stack-work\install\fafe412f\pkgdb 
    --libdir=D:\Users\mike\Code\Haskell\really-weird\.stack-work\install\fafe412f\lib 
    --bindir=D:\Users\mike\Code\Haskell\really-weird\.stack-work\install\fafe412f\bin 
    --datadir=D:\Users\mike\Code\Haskell\really-weird\.stack-work\install\fafe412f\share 
    --libexecdir=D:\Users\mike\Code\Haskell\really-weird\.stack-work\install\fafe412f\libexec 
    --sysconfdir=D:\Users\mike\Code\Haskell\really-weird\.stack-work\install\fafe412f\etc 
    --docdir=D:\Users\mike\Code\Haskell\really-weird\.stack-work\install\fafe412f\doc\really-weird-0.1.0.0 
    --htmldir=D:\Users\mike\Code\Haskell\really-weird\.stack-work\install\fafe412f\doc\really-weird-0.1.0.0 
    --haddockdir=D:\Users\mike\Code\Haskell\really-weird\.stack-work\install\fafe412f\doc\really-weird-0.1.0.0 
    --dependency=base=base-4.20.2.0-39f9 
    --ghc-options -haddock 
    --extra-include-dirs=D:\sr\programs\x86_64-windows\msys2-20240727\clang64\include 
    --extra-lib-dirs=D:\sr\programs\x86_64-windows\msys2-20240727\clang64\lib 
    --extra-lib-dirs=D:\sr\programs\x86_64-windows\msys2-20240727\clang64\bin 
    --exact-configuration 
    --ghc-option=-fhide-source-paths

# The build step:
Run process within D:\Users\mike\Code\Haskell\really-weird\: 
  D:\sr\setup-exe-cache\x86_64-windows\Cabal-simple_O_vy6YIf_3.12.1.0_ghc-9.10.3.exe 
    --verbose=1 
    --builddir=D:\Users\mike\Code\Haskell\really-weird\.stack-work\dist\1a191874 
    build 
    lib:really-weird 
    exe:really-weird-exe 
    --ghc-options " -fdiagnostics-color=always"

A more minimal example has just the library stanza, with Cabal file:

cabal-version: 1.12

name:           really-weird
version:        0.1.0.0
build-type:     Simple

library
  exposed-modules:
      Lib
      Main
  hs-source-dirs:
      src
  build-depends:
      base >=4.7 && <5
  default-language: Haskell2010

The command that Cabal (the library) is sending to GHC 9.10.3 (which can be understood from stack --verbose build --cabal-verbose) is:

Running: "D:\sr\programs\x86_64-windows\ghc-9.10.3\bin\ghc-9.10.3.exe"
"--make" 
"-fbuilding-cabal-package" 
"-O" 
"-outputdir" "D:\Users\mike\Code\Haskell\really-weird\.stack-work\dist\1a191874\build" 
"-odir" "D:\Users\mike\Code\Haskell\really-weird\.stack-work\dist\1a191874\build" 
"-hidir" "D:\Users\mike\Code\Haskell\really-weird\.stack-work\dist\1a191874\build" 
"-hiedir" "D:\Users\mike\Code\Haskell\really-weird\.stack-work\dist\1a191874\build\extra-compilation-artifacts\hie" 
"-stubdir" "D:\Users\mike\Code\Haskell\really-weird\.stack-work\dist\1a191874\build" 
"-i" 
"-isrc" 
"-iD:\Users\mike\Code\Haskell\really-weird\.stack-work\dist\1a191874\build" 
"-iD:\Users\mike\Code\Haskell\really-weird\.stack-work\dist\1a191874\build\autogen" 
"-iD:\Users\mike\Code\Haskell\really-weird\.stack-work\dist\1a191874\build\global-autogen" 
"-ID:\Users\mike\Code\Haskell\really-weird\.stack-work\dist\1a191874\build\autogen" 
"-ID:\Users\mike\Code\Haskell\really-weird\.stack-work\dist\1a191874\build\global-autogen" 
"-ID:\Users\mike\Code\Haskell\really-weird\.stack-work\dist\1a191874\build" 
"-ID:\sr\programs\x86_64-windows\msys2-20240727\clang64\include" 
"-optP-include" "-optPD:\Users\mike\Code\Haskell\really-weird\.stack-work\dist\1a191874\build\autogen\cabal_macros.h" 
"-this-unit-id" "really-weird-0.1.0.0-AgioqsgBM8B23qwj0Kdd7R" 
"-hide-all-packages" 
"-Wmissing-home-modules" 
"-no-user-package-db" 
"-package-db" "D:\sr\snapshots\fafe412f\pkgdb" 
"-package-db" "D:\Users\mike\Code\Haskell\really-weird\.stack-work\install\fafe412f\pkgdb" 
"-package-db" "D:\Users\mike\Code\Haskell\really-weird\.stack-work\dist\1a191874\package.conf.inplace" 
"-package-id" "base-4.20.2.0-39f9" 
"-XHaskell2010" 
"Lib" 
"Main" 
"-haddock" 
"-fhide-source-paths" 
"-fdiagnostics-color=always"

Note that Cabal is not passing an -o <filename> option to GHC 9.10.3 …

So, I think what is happening is this:

  • you are effectively specifying that the library stanza of your package includes a module Main
  • GHC recognises that module Main is named Main and exports a function main, so it builds an executable …
  • … and because -o <filename> is not specified by Cabal (and why would it: it is building a library stanza), that executable file is named Main and put in the same directory as the source file Main.hs.

The ‘solution’ is that your package description needs to state expressly that, for the library stanza, only the module Lib is relevant, like so:

# package.yaml
spec-version: 0.36.0

name:                really-weird
version:             0.1.0.0

dependencies:
- base >= 4.7 && < 5

library:
  source-dirs: src
  # We need to be express, because the library directory is sharing source 
  # files with an executable:
  exposed-modules:
  - Lib
  other-modules: []

executables:
  really-weird-exe:
    main:                Main.hs
    source-dirs:         src
    ghc-options:
    - -threaded
    - -rtsopts
    - -with-rtsopts=-N
    dependencies:
    - really-weird

Is this something to file as a bug, then? I avoided opening an issue on stack because it requires me to test in the latest version, which also wasn’t trivial to get on my system. Thank you for testing though, I am guessing this setup isn’t very common? Is having src+app directories the norm?

Personally, I would not classify it as a ‘bug’, in the sense that Cabal (the library) and GHC are behaving as expected and documented (even if that behaviour may be unintuitive). However, the fact that you were caught out by it motives me to think how Stack’s documentation might be updated. EDIT: I’ve added a question about “Must package components have distinct source file directories?” to 2. Package description - Stack