rules_haskell icon indicating copy to clipboard operation
rules_haskell copied to clipboard

Cabal >= 3.4 failing to load haskell_library dependencies with no version set

Open jonathanlking opened this issue 1 year ago • 0 comments

If you have a haskell_library target that's a dependency (i.e. referred to in a build-depends) of a haskell_cabal_library and the haskell_library doesn't have a version attribute set, upgrading beyond Cabal 3.2.1.0 will cause builds to fail.

From bisecting, https://github.com/haskell/cabal/commit/df0abdf9d84421f14f8dca7f7c33218c0ab8f16c appears to be the offending commit.

This looks like an issue with Cabal (or maybe just me relying on undefined behaviour!), rather than rules_haskell, but I wanted to share it here in the hope that it might save others time if they also run into this when upgrading. There might be a way within rules_haskell to give better warnings/error messages too?

Minimal example

example/BUILD.bazel:

load("@rules_haskell//haskell:cabal.bzl", "haskell_cabal_library")
load(
    "@rules_haskell//haskell:defs.bzl",
    "haskell_library",
)

haskell_library(
    name = "lib",
    srcs = glob(["lib/**/*.hs"]),
    src_strip_prefix = "lib",
    deps = [
        "@stackage//:base",
    ],  
)

haskell_cabal_library(
    name = "example",
    package_name = "example",
    srcs = glob(["src/**/*"]),
    setup_deps = ["@stackage//:Cabal"],
    version = "0.1.0.0",
    deps = [
        "//example:lib",
    ],
)

example/lib/Lib.hs:

module Lib where

import Prelude

number :: Int
number = 42

example/src/example.cabal:

cabal-version: >=1.10
name: example
version: 0.1.0.0
build-type: Simple

library
  build-depends: base, lib
  exposed-modules: Example

example/src/Example.hs:

module Example where

import Lib (number)

example = number

Building the minimal example

Running bazel build //example:example will succeed with Cabal 3.2.1.0, but fail on 3.4.0.0 with:

Use --sandbox_debug to see verbose messages from the sandbox
Setup.hs: Encountered missing or private dependencies:
lib

Curiously, if you remove lib from the build-depends, you get this error:

Use --sandbox_debug to see verbose messages from the sandbox

Example.hs:3:1: error:
    Could not load module ‘Lib’
    It is a member of the hidden package ‘lib’.
    Perhaps you need to add ‘lib’ to the build-depends in your .cabal file.
    Use -v (or `:set -v` in ghci) to see a list of the files searched for.
  |
3 | import Lib (number)
  | ^^^^^^^^^^^^^^^^^^^^^^^

It wasn't obvious to me that the issue was with a version number being missing and I only figured it out by looking at the contents of the Cabal commit :detective:

jonathanlking avatar Mar 15 '23 20:03 jonathanlking