commercialhaskell / commercialhaskell/stack

Stack 2 cannot locate artifact of custom build dependency

Open
#5,060 6 comments 0 reactions 0 assignees View on GitHub
further investigation required type: bug
Dominant language
Haskell
Stars
4.1k
Forks
850
Avg merge
10h 37m
Merged PRs (30d)
4

Description

### General summary/comments

I have a project that is currently trying to pull in a dependency over git. The dependency is a rust library that has exported symbols over the C FFI. The dependency includes a .cabal file:

```cabal
cabal-version: 1.12

name: my-dependency
version: 0.0.1
build-type: Custom

library
build-depends: base
exposed-modules: MyDependency
hs-source-dirs: ./hs
default-language: Haskell2010
extra-libraries: my_dependency
```

and its associated Setup.hs file:

```haskell
import Distribution.Simple
import Distribution.Simple.Setup
import Distribution.Types.LocalBuildInfo
import Distribution.Types.GenericPackageDescription
import Distribution.PackageDescription
import Distribution.Types.HookedBuildInfo
import Distribution.Types.BuildInfo
import System.Process
import System.Directory
import Distribution.Types.Library

addExtraLibDir :: FilePath -> Library -> Library
addExtraLibDir dir lib = lib
{ libBuildInfo =
(libBuildInfo lib)
{ extraLibDirs = (dir ++ "/target/release")
: (extraLibDirs . libBuildInfo $ lib)
}
}

make
:: ( (GenericPackageDescription, HookedBuildInfo)
-> ConfigFlags
-> IO LocalBuildInfo
)
-> (GenericPackageDescription, HookedBuildInfo)
-> ConfigFlags
-> IO LocalBuildInfo
make configHook pkgDesc flags = do
_ <- readProcess
"cargo"
[ "build"
, "--lib"
, "--release"
, "--no-default-features"
, "--features"
, "ffi"
]
""
binfo <- configHook pkgDesc flags
dir <- getCurrentDirectory
pure $ binfo
{ localPkgDescr =
(localPkgDescr binfo)
{ library = addExtraLibDir dir
<$> (library . localPkgDescr $ binfo)
}
}

main = defaultMainWithHooks simpleUserHooks
{ confHook = (make $ confHook simpleUserHooks)
}
```

This dependency is located remotely via git. During the build process it downloads and even builds correctly, however, when I get to the point of using it, it complains about the following:

```
: can't load .so/.DLL for: libmy_dependency.dylib (dlopen(libmy_dependency.dylib, 5): image not found)
```

Colleagues of mine have successfully gotten this to run on Stack 1.9. However, multiple different people have tested getting this to work with Stack 2 and it seems to always yield that error.

### Steps to reproduce

1. Create a project that depends on a trivial rust library
2. Ensure the cabal and setup.hs files look similar to the above
3. `stack build` the project that depends on this.

### Expected

It builds without error

### Actual

```
$ stack build
...
: can't load .so/.DLL for: libmy_dependency.dylib (dlopen(libmy_dependency.dylib, 5): image not found)
```

### Stack version

```
$ stack --version
Version 2.1.3, Git revision 0fa51b9925decd937e4a993ad90cb686f88fa282 (7739 commits) x86_64 hpack-0.31.2
```

### Method of installation

Homebrew initially, upgraded via `stack upgrade`

### Speculation

My best guess about the cause of this is that the artifacts being emitted by the cargo build are not properly added to the search space of stack more generally.

It is worth noting additionally that the **bindings** are being successfully located, but the underlying library that the bindings wrap is not. Finally, this error (.so/.DLL not found) is emitted regardless of trying to link statically or not via `--ghc-options='-optl-static -optl-pthread'`, which is odd since I would expect it would at least complain about the .a file missing rather than the dylib/so. To be honest, I'm actually baffled as to why it is **even looking** for the dylib/so because I am linking statically.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.