Skip to content

Commit

Permalink
tree: fix --no-switch option
Browse files Browse the repository at this point in the history
Instead of emptying switch from it installed packages, load a virtual
switch at the beginning when `--no-switch` is given.
  • Loading branch information
rjbou committed Dec 12, 2023
1 parent 6c2b09d commit 786edb8
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 30 deletions.
1 change: 1 addition & 0 deletions master_changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ users)
* Allow packages with a specific version, directories or local opam files, as input [#5613 @kit-ty-kate]
* Add handling of `--recurse` and `--subpath` for directory arguments [#5613 @kit-ty-kate]
* Fix `--dev` option, force dev dependencies when option is given [#5687 @rjbou - fix #5675]
* Fix `--no-switch` option, instead of emptying switch from it installed packages, load a virtual switch at the beginning when `--no-switch` is given [#5687 @rjbou - fix #5675]

## Exec

Expand Down
9 changes: 7 additions & 2 deletions src/client/opamCommands.ml
Original file line number Diff line number Diff line change
Expand Up @@ -828,7 +828,12 @@ let tree ?(why=false) cli =
else
(apply_global_options cli global_options;
OpamGlobalState.with_ `Lock_none @@ fun gt ->
OpamSwitchState.with_ `Lock_none gt @@ fun st ->
OpamRepositoryState.with_ `Lock_none gt @@ fun rt ->
(if no_switch then
fun k ->
k @@ OpamSwitchState.load_virtual gt rt
else
OpamSwitchState.with_ `Lock_none ~rt gt) @@ fun st ->
let st, atoms =
OpamAuxCommands.simulate_autopin
st ~recurse ?subpath ~quiet:true
Expand All @@ -840,7 +845,7 @@ let tree ?(why=false) cli =
depopts = false;
build = true;
} in
OpamTreeCommand.run st tog ~no_constraint ~no_switch mode filter atoms;
OpamTreeCommand.run st tog ~no_constraint mode filter atoms;
`Ok ())
in
mk_command_ret ~cli (cli_from cli2_2) "tree" ~doc ~man
Expand Down
26 changes: 2 additions & 24 deletions src/client/opamTreeCommand.ml
Original file line number Diff line number Diff line change
Expand Up @@ -403,26 +403,7 @@ let dry_install tog st universe install =
simulate_new_state tog st universe install
(OpamPackage.Name.Set.of_list (List.map fst install))

let raw_state tog st install =
let OpamListCommand.{doc; test; dev_setup; dev; _} = tog in
let names = OpamPackage.Name.Set.of_list (List.map fst install) in
let requested =
OpamPackage.packages_of_names
(Lazy.force st.available_packages)
names
in
let universe =
OpamSwitchState.universe st ~doc ~test ~dev_setup ~force_dev_deps:dev
~requested Query
in
let universe =
{ universe
with u_installed = OpamPackage.Set.empty;
u_installed_roots = OpamPackage.Set.empty }
in
simulate_new_state tog st universe install names

let run st tog ?no_constraint ?(no_switch=false) mode filter atoms =
let run st tog ?no_constraint mode filter atoms =
let open OpamPackage.Set.Op in
let select, missing =
List.fold_left (fun (select, missing) atom ->
Expand All @@ -439,9 +420,6 @@ let run st tog ?no_constraint ?(no_switch=false) mode filter atoms =
match mode, filter, missing with
| Deps, _, [] -> st, universe
| Deps, Roots_from, _::_ ->
if no_switch then
raw_state tog st missing
else
dry_install tog st universe missing
| Deps, Leads_to, _::_
| ReverseDeps, _, _ ->
Expand All @@ -465,6 +443,6 @@ let run st tog ?no_constraint ?(no_switch=false) mode filter atoms =
in
print ?no_constraint forest;
if OpamClientConfig.(!r.json_out) <> None then
(if not no_switch then
(if OpamSwitch.compare st.switch OpamSwitch.unset <> 0 then
OpamJson.append "switch" (OpamSwitch.to_json st.switch);
OpamJson.append "tree" (forest_to_json ?no_constraint forest))
2 changes: 0 additions & 2 deletions src/client/opamTreeCommand.mli
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,4 @@ val run :
OpamListCommand.dependency_toggles ->
(* output format options *)
?no_constraint:bool ->
(* do no keep switch consistency *)
?no_switch:bool ->
mode -> tree_filter -> OpamTypes.atom list -> unit
7 changes: 7 additions & 0 deletions tests/reftests/json.unix.test
Original file line number Diff line number Diff line change
Expand Up @@ -1302,6 +1302,13 @@ a.1
]
}
### opam tree c --no-switch --json=out.json
The following actions are simulated:
=== install 4 packages
- install a 1 [required by c]
- install b 1 [required by c]
- install c 1
- install c-build 1 [required by c]

c.1
|-- a.1
|-- b.1
Expand Down
14 changes: 12 additions & 2 deletions tests/reftests/tree.test
Original file line number Diff line number Diff line change
Expand Up @@ -303,10 +303,20 @@ l.1
|-- a.1
'-- l-dev.1
### opam tree l --no-switch
The following actions are simulated:
=== install 2 packages
- install a 1 [required by l]
- install l 1

l.1
|-- a.1
'-- l-dev.1
'-- a.1
### opam tree l --dev --no-switch
The following actions are simulated:
=== install 3 packages
- install a 1 [required by l]
- install l 1
- install l-dev 1 [required by l]

l.1
|-- a.1
'-- l-dev.1 (dev)
Expand Down

0 comments on commit 786edb8

Please sign in to comment.