rules_haskell icon indicating copy to clipboard operation
rules_haskell copied to clipboard

haskell_cabal_library targets may end up silently ignored in haskell_repl

Open aherrmann opened this issue 2 years ago • 0 comments

Describe the bug If a haskell_cabal_library target is not loaded "from binary" but instead "from sources" based on the experimental_from_binary|sources patterns passed to haskell_repl, then it will effectively not be loaded at all, potentially leading to missing modules when GHCi tries to load the modules provided by that Cabal library target.

To Reproduce Apply the following patch to master as of e2a74e5c29588f2107daae7c438e0d4117fcafb3.

diff --git a/tests/haskell_cabal_library/BUILD.bazel b/tests/haskell_cabal_library/BUILD.bazel
index 3dbea676..4cf1ded5 100644
--- a/tests/haskell_cabal_library/BUILD.bazel
+++ b/tests/haskell_cabal_library/BUILD.bazel
@@ -4,6 +4,7 @@ load(
     "@rules_haskell//haskell:defs.bzl",
     "haskell_test",
     "haskell_toolchain_library",
+    "haskell_repl",
 )

 haskell_toolchain_library(name = "base")
@@ -72,6 +73,14 @@ haskell_test(
     ],
 )

+haskell_repl(
+    name = "repl",
+    deps = [":haskell_cabal_library"],
+    testonly = True,
+    # Uncomment to work around the issue
+    # experimental_from_source = [":haskell_cabal_library"],
+)
+
 filegroup(
     name = "all_files",
     testonly = True,

and run the following Bazel command:

$ bazel run //tests/haskell_cabal_library:repl
...
Target //tests/haskell_cabal_library:repl up-to-date:
  bazel-bin/tests/haskell_cabal_library/repl@repl
...
[1 of 1] Compiling Main             ( tests/haskell_cabal_library/Main.hs, interpreted )

tests/haskell_cabal_library/Main.hs:3:1: error:
    Could not load module ‘Lib’
    It is a member of the hidden package ‘libiserv-8.10.7’.
    You can run ‘:set -package libiserv’ to expose it.
    (Note: this unloads all the modules in the current scope.)
    Use -v (or `:set -v` in ghci) to see a list of the files searched for.
  |
3 | import Lib
  | ^^^^^^^^^^

tests/haskell_cabal_library/Main.hs:4:1: error:
    Could not find module ‘SecondLib’
    Use -v (or `:set -v` in ghci) to see a list of the files searched for.
  |
4 | import SecondLib
  | ^^^^^^^^^^^^^^^^
Failed, no modules loaded.
[1 of 1] Compiling Main             ( tests/haskell_cabal_library/Main.hs, interpreted )

tests/haskell_cabal_library/Main.hs:3:1: error:
    Could not load module ‘Lib’
    It is a member of the hidden package ‘libiserv-8.10.7’.
    You can run ‘:set -package libiserv’ to expose it.
    (Note: this unloads all the modules in the current scope.)
    Use -v (or `:set -v` in ghci) to see a list of the files searched for.
  |
3 | import Lib
  | ^^^^^^^^^^

tests/haskell_cabal_library/Main.hs:4:1: error:
    Could not find module ‘SecondLib’
    Use -v (or `:set -v` in ghci) to see a list of the files searched for.
  |
4 | import SecondLib
  | ^^^^^^^^^^^^^^^^
Failed, no modules loaded.
...

Expected behavior The REPL should load successfully. The simplest solution is probably to inject such Cabal library dependencies as binary dependencies, i.e. discover when a dependency is a Cabal package and in that case override the experimental_from_source|binary configuration to enforce a load from binary. More involved, but perhaps preferable at some point, be able to load Cabal packages into haskell_repl from source as well.

Environment

  • OS name + version: Ubuntu 21.10
  • Bazel version: 4.2.1
  • Version of the rules: e2a74e5c29588f2107daae7c438e0d4117fcafb3

Additional context The issue occurs here where Cabal libraries might end up sorted into the load_info collection even though they don't provide any sources to load.

aherrmann avatar Apr 14 '22 12:04 aherrmann