Dune cannot find packages that were installed in custom location

(cross-posting from Dune cannot find packages that were installed in custom location · Issue #6358 · ocaml/dune · GitHub)

While trying to package dune to work with the Bazel build system, I run into this issue.
From reading the documentation, it seems that custom installation locations can be supported by setting OCAMLPATH appropriately, but I cannot get that to work.

If there is another way to make dune discover packages that have been installed in custom locations, I’d appreciate pointers on that as well.

Expected Behavior

After installing a package pkg1 using --prefix=$SOMEDIR, Dune should find that package from a different project if configured appropriately using OCAMLPATH=$SOMEDIR/lib.

Actual Behavior

When compiling pkg2, which depends on pkg1, dune cannot find pkg1, despite configuration of OCAMLPATH to point to the custom prefix used for installation of pkg1.

+ export OCAMLPATH="$PWD/_install/lib"
+ pushd pkg1
+ dune build --release --build-dir=_build @install
+ dune install --release --build-dir=_build --prefix=$PWD/_install
Installing $PWD/_install/lib/pkg1/META
Installing $PWD/_install/lib/pkg1/dune-package
Installing $PWD/_install/lib/pkg1/foo.ml
Installing $PWD/_install/lib/pkg1/pkg1.a
Installing $PWD/_install/lib/pkg1/pkg1.cma
Installing $PWD/_install/lib/pkg1/pkg1.cmi
Installing $PWD/_install/lib/pkg1/pkg1.cmt
Installing $PWD/_install/lib/pkg1/pkg1.cmx
Installing $PWD/_install/lib/pkg1/pkg1.cmxa
Installing $PWD/_install/lib/pkg1/pkg1.ml
Installing $PWD/_install/lib/pkg1/pkg1__Foo.cmi
Installing $PWD/_install/lib/pkg1/pkg1__Foo.cmt
Installing $PWD/_install/lib/pkg1/pkg1__Foo.cmx
Installing $PWD/_install/lib/pkg1/pkg1.cmxs
+ popd
+ tree $PWD
.
├── _install
│   └── lib
│       └── pkg1
│           ├── META
│           ├── dune-package
│           ├── foo.ml
│           ├── pkg1.a
│           ├── pkg1.cma
│           ├── pkg1.cmi
│           ├── pkg1.cmt
│           ├── pkg1.cmx
│           ├── pkg1.cmxa
│           ├── pkg1.cmxs
│           ├── pkg1.ml
│           ├── pkg1__Foo.cmi
│           ├── pkg1__Foo.cmt
│           └── pkg1__Foo.cmx
├── pkg1
│   ├── _build
│   │   ├── default
│   │   │   ├── META.pkg1
│   │   │   ├── lib
│   │   │   │   ├── foo.ml
│   │   │   │   ├── pkg1.a
│   │   │   │   ├── pkg1.cma
│   │   │   │   ├── pkg1.cmxa
│   │   │   │   ├── pkg1.cmxs
│   │   │   │   └── pkg1.ml-gen
│   │   │   ├── pkg1.dune-package
│   │   │   ├── pkg1.install
│   │   │   └── pkg1.opam
│   │   ├── install
│   │   │   └── default
│   │   │       └── lib
│   │   │           └── pkg1
│   │   │               ├── META -> ../../../../default/META.pkg1
│   │   │               ├── dune-package -> ../../../../default/pkg1.dune-package
│   │   │               ├── foo.ml -> ../../../../default/lib/foo.ml
│   │   │               ├── pkg1.a -> ../../../../default/lib/pkg1.a
│   │   │               ├── pkg1.cma -> ../../../../default/lib/pkg1.cma
│   │   │               ├── pkg1.cmi -> ../../../../default/lib/.pkg1.objs/byte/pkg1.cmi
│   │   │               ├── pkg1.cmt -> ../../../../default/lib/.pkg1.objs/byte/pkg1.cmt
│   │   │               ├── pkg1.cmx -> ../../../../default/lib/.pkg1.objs/native/pkg1.cmx
│   │   │               ├── pkg1.cmxa -> ../../../../default/lib/pkg1.cmxa
│   │   │               ├── pkg1.cmxs -> ../../../../default/lib/pkg1.cmxs
│   │   │               ├── pkg1.ml -> ../../../../default/lib/pkg1.ml-gen
│   │   │               ├── pkg1__Foo.cmi -> ../../../../default/lib/.pkg1.objs/byte/pkg1__Foo.cmi
│   │   │               ├── pkg1__Foo.cmt -> ../../../../default/lib/.pkg1.objs/byte/pkg1__Foo.cmt
│   │   │               └── pkg1__Foo.cmx -> ../../../../default/lib/.pkg1.objs/native/pkg1__Foo.cmx
│   │   └── log
│   ├── dune-project
│   ├── dune-workspace
│   ├── lib
│   │   ├── dune
│   │   └── foo.ml
│   └── pkg1.opam

└── pkg2
    ├── dune-project
    ├── dune-workspace
    └── lib
        ├── bar.ml
        └── dune

+ pushd pkg2
+ dune build --release --build-dir=_build @install
File "lib/dune", line 4, characters 12-16:
4 |  (libraries pkg1) 
                ^^^^
Error: Library "pkg1" not found.
-> required by library "pkg2" in _build/default/lib
-> required by _build/default/META.pkg2
-> required by _build/install/default/lib/pkg2/META
-> required by _build/default/pkg2.install
-> required by alias install
(cd _build/default && $OPAM_SWITCH_PREFIX/bin/ocamldep.opt -modules -impl lib/bar.ml) > _build/default/lib/.pkg2.objs/bar.ml.d
File "lib/bar.ml", line 3, characters 19-20:
+ dune build --release --build-dir=_build @install
File "lib/dune", line 4, characters 12-16:
4 |  (libraries pkg1) 
                ^^^^
Error: Library "pkg1" not found.
-> required by library "pkg2" in _build/default/lib
-> required by _build/default/META.pkg2
-> required by _build/install/default/lib/pkg2/META
-> required by _build/default/pkg2.install
-> required by alias install
(cd _build/default && $OPAM_SWITCH_PREFIX/bin/ocamldep.opt -modules -impl lib/bar.ml) > _build/default/lib/.pkg2.objs/bar.ml.d
File "lib/bar.ml", line 3, characters 19-20:
3 | let url : Sometype = "https://github.com"
                       ^
Error: Syntax error

Reproduction

Run ./build.sh in GitHub - jgertm/dune-ocamlpath-demo

Specifications

  • Version of dune (output of dune --version):
    3.2.0

  • Version of ocaml (output of ocamlc --version)
    4.14.0

  • Operating system (distribution and version):
    macOS Monterey 12.6

Submitted a PR that should make it work Does this make it work for you? by anmonteiro · Pull Request #1 · jgertm/dune-ocamlpath-demo · GitHub