Strange error in dune building

I use the library to LZMA 7z SDK with ctypes and if I build it locally it builds without any problem. But as long as I do it from the higher-level directory in docker it fails with these errors:

Error: No rule found for lib/lzma_7z/7zip/liblzma_7z.a
File "lib/lzma_7z/dune", line 12, characters 0-350:
...

Error: No rule found for lib/lzma_7z/7zip/liblzma_7z.so
File "lib/lzma_7z/dune", line 12, characters 0-350:
(rule
  (deps (source_tree 7zip))
  (targets liblzma_7z_stubs.a dlllzma_7z_stubs.so)
  (action (progn
	  (chdir 7zip (run make clean))
	  (chdir 7zip (run make liblzma_7z.a))
	  (chdir 7zip (run make liblzma_7z.so))
	  (copy 7zip/liblzma_7z.a liblzma_7z_stubs.a)
	  (copy 7zip/liblzma_7z.so dlllzma_7z_stubs.so)
	  (chdir 7zip (run make clean))
)))

See https://github.com/XVilka/ocaml-lzma_7z/blob/master/dune#L12 for more context. Any idea what it possibly can be?

I have encountered the same issue when working on a recent PR to eigen, I ended up using system ("cp: https://github.com/owlbarn/eigen/pull/8/files#diff-c660002a37dacfa9776df75a7e6a3085R8

But now that I think about it, it may be a bug. Ping @rgrinberg or @jeremiedimino

Ah, Dune is trying to be too clever here: when it sees (copy a b), it will assume that a is a dependency and b is a target. However, it can’t know that (run make liblzma_7z.a) produces liblzma_7z.a, so it thinks that 7zip/liblzma_7z.a is a dependency of the whole rule. I suppose we could add an option to disable dependency/target inference.

Indeed, that’s unfortunate. A workaround here could be to split the rules so that run make liblzma_7z.a is its own rule.

Even if I split it to two rules like this it returns the same error:

(rule
  (deps (source_tree 7zip))
  (targets liblzma_7z.a liblzma_7z.so)
  (action (progn
	  (chdir 7zip (run make clean))
	  (chdir 7zip (run make liblzma_7z.a))
	  (chdir 7zip (run make liblzma_7z.so))
  )))
(rule
  (deps (source_tree 7zip))
  (targets liblzma_7z_stubs.a dlllzma_7z_stubs.so)
  (action (progn
	  (copy 7zip/liblzma_7z.a liblzma_7z_stubs.a)
	  (copy 7zip/liblzma_7z.so dlllzma_7z_stubs.so)
	  (chdir 7zip (run make clean))
  )))
Error: No rule found for lib/lzma_7z/7zip/liblzma_7z.a

You need to move the first block inside a dune file in the 7zip folder and add only the two libraries to the deps in the second stanza: I have just tried on my eigen PR and seems to work fine: https://github.com/owlbarn/eigen/pull/8/commits/a8ece00fe3eccc598c99bd8d2591fefb5e20433f

To make it easier, I created an issue to track this in GitHub instead: https://github.com/ocaml/dune/issues/2006

See also the Dune: depend on non-Dune libraries in the workspace