cabal icon indicating copy to clipboard operation
cabal copied to clipboard

No recompilation when using `addDependentFile` or `-fforce-recomp` in a build dependent package (stanza)

Open KristianBalaj opened this issue 5 months ago • 2 comments

Describe the bug

Importing a library that is in the package's build-depends doesn't recompile when it should. The following are the cases when recompilation should happen in a dependent package but it doesn't happen:

  • when using addDependentFile and the file changes
  • when explicitly marking some file using {-# OPTIONS_GHC -fforce-recomp #-}

The only workaround I have is either to do cabal clean and then compile again from scratch.... Or modify one of the library source files.

To Reproduce

Steps to reproduce the behavior:

Create a new cabal project with 2 stanzas

  1. library my-lib
  2. test-suite my-tests

Like the following:

...
test-suite my-tests
  import:         lang
  type:           exitcode-stdio-1.0
  main-is:        Spec.hs
  hs-source-dirs: my-tests
  build-depends:
    , my-lib

library my-lib
  import: lang
  hs-source-dirs:   my-lib
  exposed-modules:
    Foo.hs

The contents of the Foo.hs are the following:

{-# OPTIONS_GHC -fforce-recomp #-}

module Foo where
....

And the contents of the Spec.hs are the following:

module Spec where

import Foo
...

Now when I run cabal test my-tests for the first time everything necessary normally compiles along with the Foo.hs. And now when I run cabal test my-tests the second time, I expect the Foo.hs to compile again, but it doesn't compile again even though it uses the -fforce-recomp option.

The similar example would be using the addDependentFile and the dependent file changes, it doesn't compile either.

Expected behavior

When there's {-# OPTIONS_GHC -fforce-recomp #-} used in any of the dependent library source files, it should compile again every single time. Also, when there's addDependentFile used in the dependent library, it should also recompile when the dependent file changes.

System information

OS: x86-darwin Cabal and GHC: cabal-install version 3.6.2.0 The Glorious Glasgow Haskell Compilation System, version 8.10.7

Additional context

I believe this is a related issue - https://github.com/haskell/cabal/issues/4746

KristianBalaj avatar Feb 27 '24 18:02 KristianBalaj

As discussed in the related issues, you can use extra-source-files, and this is the recommended way. Have you tried doing so, and are there any problems in doing so?

This also has the advantage that the file in extra-source-files is actually packaged up by sdist properly, which a file added by addDependentFile cannot be.

Further, I think this is the correct semantics for -fforce-recomp. The flag is intended to say "if you are recompiling the package, then you should recompile the so-flagged files" not "always recompile this package"

gbaz avatar Feb 27 '24 18:02 gbaz

Further, I think this is the correct semantics for -fforce-recomp. The flag is intended to say "if you are recompiling the package, then you should recompile the so-flagged files" not "always recompile this package"

I see, I'm okay with that then.

As discussed in the related issues, you can use extra-source-files, and this is the recommended way. Have you tried doing so, and are there any problems in doing so?

I haven't yet, but in my use case the dependent file path is based on env variable, so I'm curious whether I can use env variable in the cabal file for the extra-source-files?

EDIT: Also thanks for a quick response 🙌

KristianBalaj avatar Feb 27 '24 18:02 KristianBalaj