New opam install : Invalid import of Libs, compiled with -unsafe-string

I ran into some issues here recently trying to get opam to install some packages. I was getting the following message:

10:49 $ opam install dune
The following actions will be performed:
  βˆ— install dune 2.7.1

<><> Gathering sources ><><><><><><><><><><><><><><><><><><><><><><><><><><>  🐫
[dune.2.7.1] downloaded from cache at https://opam.ocaml.org/cache

<><> Processing actions <><><><><><><><><><><><><><><><><><><><><><><><><><>  🐫
[ERROR] The compilation of dune failed at "/Users/jdonaldson/.opam/opam-init/hooks/sandbox.sh build ocaml bootstrap.ml -j 7".

#=== ERROR while compiling dune.2.7.1 =========================================#
# context     2.0.7 | macos/x86_64 | ocaml-base-compiler.4.11.1 | https://opam.ocaml.org#b9edad04
# path        ~/.opam/default/.opam-switch/build/dune.2.7.1
# command     ~/.opam/opam-init/hooks/sandbox.sh build ocaml bootstrap.ml -j 7
# exit-code   2
# env-file    ~/.opam/log/dune-39298-6cc03f.env
# output-file ~/.opam/log/dune-39298-6cc03f.out
### output ###
# ocamlc -output-complete-exe -w -24 -g -o .duneboot.exe -I boot unix.cma boot/libs.ml boot/duneboot.ml
# File "boot/duneboot.ml", line 1:
# Error: Invalid import of Libs, compiled with -unsafe-string.
#        This compiler has been configured in strict safe-string mode (-force-safe-string)

There’s several other packages I’m interested in installing that are failing with the same errors:

<><> Error report <><><><><><><><><><><><><><><><><><><><><><><><><><><><><>  🐫
β”Œβ”€ The following actions failed
β”‚ Ξ» build camlp5     8.00~alpha04
β”‚ Ξ» build dune       2.7.1
β”‚ Ξ» build ocamlbuild 0.14.0
β”‚ Ξ» build ocamlfind  1.8.1

I also cannot switch to an older version that does not enforce safe strings. I tried 4.06:

10:57 $ opam switch create 4.06.0

<><> Gathering sources ><><><><><><><><><><><><><><><><><><><><><><><><><><>  🐫
[ocaml-base-compiler.4.06.0] downloaded from cache at https://opam.ocaml.org/cache

<><> Processing actions <><><><><><><><><><><><><><><><><><><><><><><><><><>  🐫
βˆ— installed base-bigarray.base
βˆ— installed base-threads.base
βˆ— installed base-unix.base
[ERROR] The compilation of ocaml-base-compiler failed at "/Users/jdonaldson/.opam/opam-init/hooks/sandbox.sh build make world".

#=== ERROR while compiling ocaml-base-compiler.4.06.0 =========================#
# context     2.0.7 | macos/x86_64 |  | https://opam.ocaml.org#b9edad04
# path        ~/.opam/4.06.0/.opam-switch/build/ocaml-base-compiler.4.06.0
# command     ~/.opam/opam-init/hooks/sandbox.sh build make world
# exit-code   2
# env-file    ~/.opam/log/ocaml-base-compiler-42289-c743ac.env
# output-file ~/.opam/log/ocaml-base-compiler-42289-c743ac.out
### output ###
# [...]
# cc -O2 -pipe -c -O2 -fno-strict-aliasing -fwrapv -Wall -D_FILE_OFFSET_BITS=64 -D_REENTRANT -DCAML_NAME_SPACE  -I../byterun -o skeleton.o skeleton.c
# cc -O2 -pipe -c -O2 -fno-strict-aliasing -fwrapv -Wall -D_FILE_OFFSET_BITS=64 -D_REENTRANT -DCAML_NAME_SPACE  -I../byterun -o symtab.o symtab.c
# cc -O2 -pipe -c -O2 -fno-strict-aliasing -fwrapv -Wall -D_FILE_OFFSET_BITS=64 -D_REENTRANT -DCAML_NAME_SPACE  -I../byterun -o verbose.o verbose.c
# cc -O2 -pipe -c -O2 -fno-strict-aliasing -fwrapv -Wall -D_FILE_OFFSET_BITS=64 -D_REENTRANT -DCAML_NAME_SPACE  -I../byterun -o warshall.o warshall.c
# cc -O2 -pipe -O2 -fno-strict-aliasing -fwrapv -Wall -D_FILE_OFFSET_BITS=64 -D_REENTRANT -DCAML_NAME_SPACE   -Wl,-no_compact_unwind -o ocamlyacc closure.o error.o lalr.o lr0.o main.o mkpar.o output.o reader.o skeleton.o symtab.o verbose.o warshall.o
# cp yacc/ocamlyacc boot/ocamlyacc
# /Applications/Xcode.app/Contents/Developer/usr/bin/make -C stdlib  \
#         COMPILER="../boot/ocamlc -use-prims ../byterun/primitives" all
# ../boot/ocamlrun ../boot/ocamlc -use-prims ../byterun/primitives -strict-sequence -absname -w +a-4-9-41-42-44-45-48 -g -warn-error A -bin-annot -nostdlib -safe-string -strict-formats `sh ./Compflags camlinternalFormatBasics.cmi` -c camlinternalFormatBasics.mli
# Fatal error: exception Invalid_argument("Sys.getcwd not implemented")
# make[1]: *** [camlinternalFormatBasics.cmi] Error 2
# make: *** [coldstart] Error 2



<><> Error report <><><><><><><><><><><><><><><><><><><><><><><><><><><><><>  🐫
β”Œβ”€ The following actions failed
β”‚ Ξ» build ocaml-base-compiler 4.06.0
└─

I’ve been using opam/ocaml for a few years now, and had problems with safe strings before, but the issue was always with packages. This seems to be a low level configuration option that I’m missing.

I don’t know about the first error but that error about 4.06:

Looks like this problem (see also here) with xcode12. Try with a compiler >= 4.08

1 Like

I can confirm that version 4.08 works for my needs. Thanks!

Let me know if there’s other information I can post about my system/environment.

Also another way out may be to try an older version of dune e.g. by doing opam install dune.$VERSION.

I’ve tried that here for dune.2.3.0 through dune.2.7.0 with no luck.