Ban/limit a dependency in dune-project

In dune-project you can specify the dependency versions of the project (and generate opam files)

Is there any way to ban a specific dependency, so that it can’t be used as a dependency in dune files (in libraries)?
For example if str is discouraged, how can you ban it from appearing in dune files?

I take it you mean other than just calling a custom action from the build + grepping the opam file + erroring?

https://dune.readthedocs.io/en/stable/reference/actions.html

You might also want to install it in a local switch, after adding a fake conflict with the libraries you don’t want to appear in your dependency cone.

Not quite what you asked, but there is a field (forbidden_libraries) in the (executable) stanza which can be used to forbid certain libraries from being linked into an executable. See Stanza Reference — Dune documentation for the details.

Cheers,
Nicolas

An empty test executable with the field you gave does the trick:

$ cat bad/bad.ml
let x = Str.regexp_string "x"
$ cat bad/dune
(library (name bad) (libraries str))
$ cat test/dune
(test
 (name no_str)
 (libraries bad)
 (forbidden_libraries str))
$ cat test/no_str.ml #empty
$ dune runtest
File "test/dune", line 4, characters 22-25:
4 |  (forbidden_libraries str))
                          ^^^
Error: Library "str" was pulled in.
-> required by library "bad" in _build/default/bad
-> required by executable no_str in test/dune:2
-> required by _build/default/test/no_str.exe