I reproduced the steps opam-dune-lint is doing here in utop session with the same results:
utop # #require "opam-state";;
utop # #require "opam-format";;
utop # let root = OpamStateConfig.opamroot ();;
val root : OpamTypes.dirname = <abstr>
utop # ignore (OpamStateConfig.load_defaults ~lock_kind:`Lock_read root);;
- : unit = ()
utop # let switch = OpamStateConfig.get_switch () ;;
val switch : OpamTypes.switch = <abstr>
utop # let gt = ref (Obj.magic ());;
val gt : '_weak1 ref = {contents = <poly>}
utop # OpamGlobalState.with_ `Lock_none @@ fun x -> gt := x;;
[ERROR] Opam has not been initialised, please run `opam init'
Exception: OpamStd.OpamSys.Exit 50.
This is happening in CI environment, where opam is initialized as follows:
opam init --bare --no-setup --bypass-checks --no-opamrc --disable-sandboxing --enable-shell-hook
opam itself works just fine in this environment. Any help would be much appreciated.
Hm, I’ve found some discrepancy. OpamGlobalState.with_
calls OpamGlobalState.lock
, which starts like this:
let load lock_kind =
let root = OpamStateConfig.(!r.root_dir) in
log "LOAD-GLOBAL-STATE %@ %a" (slog OpamFilename.Dir.to_string) root;
(* Always take a global read lock, this is only used to prevent concurrent
~/.opam format changes *)
let has_root = OpamFilename.exists_dir root in
let global_lock =
if has_root then
OpamFilename.flock `Lock_read (OpamPath.lock root)
else OpamSystem.lock_none
in
(* The global_state lock actually concerns the global config file only (and
the consistence thereof with the repository and switch sets, and the
currently installed shell init scripts) *)
if not has_root then
OpamConsole.error_and_exit `Configuration_error
"Opam has not been initialised, please run `opam init'";
That is the error which is happening. I see some discrepancy between opam root dir, returned by OpamStateConfig.opamroot ()
and what is observed by let load
:
utop # OpamFilename.Dir.to_string root;;
- : string = "/home/opam/.ci-opam-root"
utop # OpamStateConfig.(!r.root_dir) |> OpamFilename.Dir.to_string;;
- : string = "/home/opam/.opam"
In my CI I use custom opam root, and opam-dune-lint is initializing opam libs in a way that does not support that?
The below hack did the trick for me:
ln -s /home/opam/.ci-opam-root /home/opam/.opam