Skip to content

Commit

Permalink
Fix opam switch set-invariant without further arguments
Browse files Browse the repository at this point in the history
to make it follow the documented behaviour:
`Without arguments, an invariant is chosen automatically.`

previous version just emptied it.
  • Loading branch information
AltGr committed Mar 31, 2021
1 parent ea79437 commit e8a4d16
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
26 changes: 18 additions & 8 deletions src/client/opamCommands.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2538,15 +2538,14 @@ let switch cli =
apply_build_options build_options;
let invariant_arg ?repos rt args =
match args, packages, formula, empty with
| [], None, None, false ->
OpamFile.Config.default_invariant rt.repos_global.config
| [], None, None, false -> None
| _::_ as packages, None, None, false ->
OpamSwitchCommand.guess_compiler_invariant ?repos rt packages
Some (OpamSwitchCommand.guess_compiler_invariant ?repos rt packages)
| [], Some atoms, None, false ->
let atoms = List.map (fun p -> Atom p) atoms in
OpamFormula.of_atom_formula (OpamFormula.ands atoms)
| [], None, (Some f), false -> f
| [], None, None, true -> OpamFormula.Empty
Some (OpamFormula.of_atom_formula (OpamFormula.ands atoms))
| [], None, (Some f), false -> Some f
| [], None, None, true -> Some OpamFormula.Empty
| _ ->
OpamConsole.error_and_exit `Bad_arguments
"Individual packages, options --packages, --formula and --empty may \
Expand Down Expand Up @@ -2614,7 +2613,12 @@ let switch cli =
in
(match invariant_arg ?repos rt pkg_params with
| exception Failure e -> `Error (false, e)
| invariant ->
| invariant_opt ->
let invariant =
OpamStd.Option.default
(OpamFile.Config.default_invariant rt.repos_global.config)
invariant_opt
in
let (), st =
OpamSwitchCommand.create gt ~rt
?synopsis:descr ?repos
Expand Down Expand Up @@ -2759,9 +2763,15 @@ let switch cli =
OpamRepositoryState.with_ `Lock_none gt @@ fun rt ->
(match invariant_arg rt params with
| exception Failure e -> `Error (false, e)
| invariant ->
| invariant_opt ->
OpamSwitchState.with_ `Lock_write gt @@ fun st ->
let invariant = match invariant_opt with
| Some i -> i
| None -> OpamSwitchState.infer_switch_invariant st
in
let st = OpamSwitchCommand.set_invariant ~force st invariant in
OpamConsole.msg "The switch invariant was set to %s\n"
(OpamFormula.to_string invariant);
let st =
if no_action || OpamFormula.satisfies_depends st.installed invariant
then st
Expand Down
2 changes: 1 addition & 1 deletion src/format/opamFormula.ml
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ let string_of_formula string_of_a f =
else s
in
match f with
| Empty -> "0"
| Empty -> "[]"
| Atom a -> paren_if (string_of_a a)
| Block x -> Printf.sprintf "(%s)" (aux x)
| And(x,y) ->
Expand Down
2 changes: 2 additions & 0 deletions tests/reftests/cli-versioning.test
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Added 'cli: "version"' to field variables in switch cli-versioning
### OPAMCLI=2.0 opam config set cli version
Added 'cli: "version"' to field variables in switch cli-versioning
### opam switch set-invariant ocaml.4.05.0
The switch invariant was set to ocaml = 4.05.0
### OPAMCLI=2.0 opam install ocaml.4.10.0
[ERROR] Package conflict!
* No agreement on the version of ocaml:
Expand Down Expand Up @@ -101,6 +102,7 @@ env-2-0 is now pinned to file://${BASEDIR}/opams (version ~dev)
Package env-2-1 does not exist, create as a NEW package? [Y/n] y
env-2-1 is now pinned to file://${BASEDIR}/opams (version ~dev)
### opam switch set-invariant --formula "[]"
The switch invariant was set to []
### opam install env-2-0

<><> Synchronising pinned packages ><><><><><><><><><><><><><><><><><><><><><><>
Expand Down

0 comments on commit e8a4d16

Please sign in to comment.