Incorrect build environment inside Emacs

I’m using Emacs (technically Spacemacs) to build an OCaml project on macOS High Sierra using OPAM and jbuilder, but it looks like the build command (which just calls jbuilder build) is picking up the wrong environment. Firstly, the OCaml compiler invoked is /usr/local/bin/ocamlopt.opt even though I have a local 4.04 installed via opam switch. Second the libraries passed to it with -I flag are a combination of directories from the local switch and from /usr/local/lib.

The actual errors I get are:

File unavailable: /usr/local/lib/ocaml/nums.cmxa

and

File "_none_", line 1:
Error: The file /Users/basus/.opam/4.04.1/lib/ocaml-migrate-parsetree/driver-main/migrate_parsetree_driver_main.cmxa is not a compilation unit description

I don’t get this error either from compiling from the shell, or from compiling under Spacemacs with the same config on Arch Linux. I don’t know how to go about fixing or debugging this.

this sounds like the usual environment trouble on os x: the spacemacs app may not inherit the shell environment that opam tells you to set up?

This sounds like your .emacs doesn’t have the right paths etc. in it. You will need to edit it appropriately. However, you should explain how you are invoking the build from within emacs so we can be sure…

It was indeed a problem with PATH. I had a line (setq shell-command-switch "-lc") in my configuration that was causing the issues. Removing that seems to let things work properly again.