Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hard format upgrade triggered by a reinit acts as a successful command #4756

Merged
merged 3 commits into from
Jul 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ are not marked). Those prefixed with "(+)" are new command/option (since
2.1.0:
* Set DEBIAN_FRONTEND=noninteractive for unsafe-yes confirmation level
[#4735 @dra27 - partially fix #4731]
* Fix 2.1~alpha2 to 2.1 format upgrade with reinit [#4750 @rjbou - fix #4748]
* Fix 2.1~alpha2 to 2.1 format upgrade with reinit [#4750 #4756 @rjbou - fix #4748]
* Fix bypass-check handling on reinit [#4750 @rjbou]
* fish: fix deprecated redirection syntax `^` [#4736 @vzaliva]
* Bump src_exts and fix build compat with Dune 2.9.0 [#4754 @dra27]
Expand Down
18 changes: 12 additions & 6 deletions src/client/opamCliMain.ml
Original file line number Diff line number Diff line change
Expand Up @@ -340,14 +340,20 @@ let rec main_catch_all f =
Windows anyway. *)
else
Unix.execvpe cmd args env
| OpamFormatUpgrade.Upgrade_done conf ->
| OpamFormatUpgrade.Upgrade_done (conf, reinit) ->
main_catch_all @@ fun () ->
OpamConsole.header_msg "Rerunning init and update";
OpamClient.reinit ~interactive:true ~update_config:false ~bypass_checks:true
conf (OpamStd.Sys.guess_shell_compat ());
OpamConsole.msg
"Update done, please now retry your command.\n";
exit (OpamStd.Sys.get_exit_code `Aborted)
(match reinit with
| Some reinit ->
reinit conf;
OpamConsole.msg "Update done.\n";
exit (OpamStd.Sys.get_exit_code `Success)
| None ->
OpamClient.reinit ~interactive:true ~update_config:false
~bypass_checks:true conf (OpamStd.Sys.guess_shell_compat ());
OpamConsole.msg
"Update done, please now retry your command.\n";
exit (OpamStd.Sys.get_exit_code `Aborted))
| e ->
flush stdout;
flush stderr;
Expand Down
19 changes: 12 additions & 7 deletions src/client/opamCommands.ml
Original file line number Diff line number Diff line change
Expand Up @@ -371,21 +371,26 @@ let init cli =
get_init_config ~no_sandboxing
~no_default_config_file:no_config_file ~add_config_file:config_file
in
let reinit conf =
OpamClient.reinit ~init_config ~interactive ~dot_profile
?update_config ?env_hook ?completion ~inplace ~bypass_checks
~check_sandbox:(not no_sandboxing)
conf shell
in
let config =
match OpamStateConfig.load ~lock_kind:`Lock_write root with
| Some c -> c
| exception (OpamPp.Bad_version _ as e) ->
OpamFormatUpgrade.hard_upgrade_from_2_1_intermediates root;
OpamFormatUpgrade.hard_upgrade_from_2_1_intermediates ~reinit root;
raise e
| None -> OpamFile.Config.empty
in
OpamClient.reinit ~init_config ~interactive ~dot_profile
?update_config ?env_hook ?completion ~inplace ~bypass_checks
~check_sandbox:(not no_sandboxing)
config shell
reinit config
else
(if not interactive &&
update_config <> Some true && completion <> Some true && env_hook <> Some true then
(if not interactive
&& update_config <> Some true
&& completion <> Some true
&& env_hook <> Some true then
OpamConsole.msg
"Opam was already initialised. If you want to set it up again, \
use `--interactive', `--reinit', or choose a different \
Expand Down
10 changes: 5 additions & 5 deletions src/state/opamFormatUpgrade.ml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ open OpamFilename.Op
let log fmt = OpamConsole.log "FMT_UPG" fmt
let slog = OpamConsole.slog

exception Upgrade_done of OpamFile.Config.t
exception Upgrade_done of OpamFile.Config.t * (OpamFile.Config.t -> unit) option

(* - Package and aux functions - *)

Expand Down Expand Up @@ -1094,7 +1094,7 @@ let remove_missing_switches root conf =
in
OpamFile.Config.with_installed_switches exists conf, missing

let as_necessary requested_lock global_lock root config =
let as_necessary ?reinit requested_lock global_lock root config =
let root_version =
match OpamFile.Config.opam_root_version_opt config with
| Some v -> v
Expand Down Expand Up @@ -1237,7 +1237,7 @@ let as_necessary requested_lock global_lock root config =
let config = hard config |> light in
OpamConsole.msg "Format upgrade done.\n";
(* We need to re run init in case of hard upgrade *)
raise (Upgrade_done config)
raise (Upgrade_done (config, reinit))
else
OpamStd.Sys.exit_because `Aborted
else
Expand All @@ -1255,7 +1255,7 @@ let as_necessary requested_lock global_lock root config =
OpamConsole.error_and_exit `Locked
"Could not acquire lock for performing format upgrade."

let hard_upgrade_from_2_1_intermediates ?global_lock root =
let hard_upgrade_from_2_1_intermediates ?reinit ?global_lock root =
let config_f = OpamPath.config root in
let opam_root_version = OpamFile.Config.raw_root_version config_f in
match opam_root_version with
Expand Down Expand Up @@ -1290,7 +1290,7 @@ let hard_upgrade_from_2_1_intermediates ?global_lock root =
| None -> OpamFilename.flock `Lock_read (OpamPath.lock root)
in
(* it will trigger only hard upgrades that won't get back *)
ignore @@ as_necessary `Lock_write global_lock root
ignore @@ as_necessary `Lock_write global_lock root ?reinit
(OpamFile.Config.with_opam_root_version v2_1_alpha2 config)

let opam_file ?(quiet=false) ?filename opam =
Expand Down
12 changes: 8 additions & 4 deletions src/state/opamFormatUpgrade.mli
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ open OpamTypes
open OpamStateTypes

(** Raised when the opam root has been updated to a newer format, and further
action (opam init/update) is needed. *)
exception Upgrade_done of OpamFile.Config.t
action (opam init/update) is needed.
[Upgrade_done conf reinit] specifies the new config file and a reinit
function to call instead of default (see [OpamCliMain.main_catch_all]). *)
exception Upgrade_done of OpamFile.Config.t * (OpamFile.Config.t -> unit) option

(** The latest version of the opam root format, that normal operation of this
instance of opam requires *)
Expand All @@ -32,15 +34,17 @@ val latest_version: OpamVersion.t
when it's done raises [Upgrade_done updated_config]. Otherwise, it returns
the upgraded or unchanged config file.*)
val as_necessary:
'a lock -> OpamSystem.lock -> dirname -> OpamFile.Config.t -> OpamFile.Config.t
?reinit:(OpamFile.Config.t -> unit) -> 'a lock -> OpamSystem.lock -> dirname ->
OpamFile.Config.t -> OpamFile.Config.t

(* Try to launch a hard upgrade from 2;1 alpha's & beta's root
to 2.1~rc one. Raises [Upgrade_done] (catched by main
function) if an upgrade is done, otherwise do nothing.
It is intend to be called after a config file that error with
[OpamPp.Bad_version] *)
val hard_upgrade_from_2_1_intermediates:
?global_lock: OpamSystem.lock -> dirname -> unit
?reinit:(OpamFile.Config.t -> unit) -> ?global_lock: OpamSystem.lock ->
dirname -> unit

(** Converts the opam file format, including rewriting availability conditions
based on OCaml-related variables into dependencies. The filename is used to
Expand Down
3 changes: 1 addition & 2 deletions tests/reftests/opamroot-versions.test
Original file line number Diff line number Diff line change
Expand Up @@ -1628,8 +1628,7 @@ RSTATE Cache found

<><> Updating repositories ><><><><><><><><><><><><><><><><><><><><><><><><><><>
[default] no changes from file://${BASEDIR}/default
Update done, please now retry your command.
# Return code 10 #
Update done.
### opam switch --short
GSTATE LOAD-GLOBAL-STATE @ ${BASEDIR}/OPAM
${BASEDIR}
Expand Down