diff --git a/master_changes.md b/master_changes.md index 342b8cc201a..edd292e4ed0 100644 --- a/master_changes.md +++ b/master_changes.md @@ -34,6 +34,7 @@ users) ## Remove ## Switch + * ◈ Add `opam switch import --deps-only` option to install only dependencies of root package at import [#5388 @rjbou - fix #5200] ## Config @@ -151,6 +152,7 @@ users) # API updates ## opam-client + * `OpamSwitchCommand.import`: add optional `?deps_only` argument to install only dependencies of root packages [#5388 @rjbou] ## opam-repository * `OpamRepository.fetch_from_cache`: when an archive is found, add a symlink (or copy) for the ones found in opam file but not in cache [#6068 @kit-ty-kate] diff --git a/src/client/opamCommands.ml b/src/client/opamCommands.ml index dc72f544ff3..ff626d7a8b4 100644 --- a/src/client/opamCommands.ml +++ b/src/client/opamCommands.ml @@ -2779,7 +2779,8 @@ let switch cli = mk_flag ~cli cli_original ["deps-only"] "When creating a local switch in a project directory (i.e. a directory \ containing opam package definitions), install the dependencies of the \ - project but not the project itself." + project but not the project itself. When importing a switch, do not \ + install root packages." in let force = mk_flag ~cli (cli_from cli2_1) ["force"] @@ -2997,7 +2998,7 @@ let switch cli = ~update_config:(not no_switch) switch @@ fun st -> - let st = OpamSwitchCommand.import st import_source in + let st = OpamSwitchCommand.import st ~deps_only import_source in let invariant = OpamSwitchState.infer_switch_invariant st in let st = OpamSwitchCommand.set_invariant_raw st invariant in st.switch_global, st @@ -3011,7 +3012,8 @@ let switch cli = OpamConsole.warning "Switch exists, '--repositories' argument ignored"; OpamSwitchState.with_ `Lock_write gt ~switch @@ fun st -> - OpamSwitchState.drop @@ OpamSwitchCommand.import st import_source + OpamSwitchState.drop + @@ OpamSwitchCommand.import st ~deps_only import_source end; `Ok () | Some `remove, switches -> diff --git a/src/client/opamSwitchCommand.ml b/src/client/opamSwitchCommand.ml index 32fe93fc6e3..de1254e093c 100644 --- a/src/client/opamSwitchCommand.ml +++ b/src/client/opamSwitchCommand.ml @@ -390,7 +390,7 @@ let switch_previous lock gt = OpamConsole.error_and_exit `Not_found "No previously used switch could be found" -let import_t ?ask importfile t = +let import_t ?ask ?(deps_only=false) importfile t = log "import switch"; let extra_files = importfile.OpamFile.SwitchExport.extra_files in @@ -462,6 +462,11 @@ let import_t ?ask importfile t = t.compiler_packages, import_sel.sel_installed -- import_sel.sel_compiler in + let to_install = + if deps_only then + to_install -- (import_sel.sel_roots -- import_sel.sel_compiler) + else to_install + in let t = { t with @@ -679,7 +684,7 @@ let reinstall init_st = overlays = OpamPackage.Name.Map.empty; } st -let import st filename = +let import st ?deps_only filename = let import_str = match filename with | None -> OpamSystem.string_of_channel stdin | Some f -> OpamFilename.read (OpamFile.filename f) @@ -695,7 +700,7 @@ let import st filename = overlays = OpamPackage.Name.Map.empty } with e1 -> OpamStd.Exn.fatal e1; raise e in - import_t importfile st + import_t ?deps_only importfile st let set_invariant ?(force=false) st invariant = let satisfied = OpamFormula.satisfies_depends st.installed invariant in diff --git a/src/client/opamSwitchCommand.mli b/src/client/opamSwitchCommand.mli index 8e56b5c8fd6..a2e396707f9 100644 --- a/src/client/opamSwitchCommand.mli +++ b/src/client/opamSwitchCommand.mli @@ -39,9 +39,11 @@ val install_compiler: ?additional_installs:atom list -> ?deps_only:bool -> ?ask:bool -> rw switch_state -> rw switch_state -(** Import a file which contains the packages to install. *) +(** Import a file which contains the packages to install. + If [deps_only] is true do not install root packages. *) val import: rw switch_state -> + ?deps_only:bool -> OpamFile.SwitchExport.t OpamFile.t option -> rw switch_state diff --git a/tests/reftests/switch-import.test b/tests/reftests/switch-import.test index df641df897c..8b3a23afbda 100644 --- a/tests/reftests/switch-import.test +++ b/tests/reftests/switch-import.test @@ -349,3 +349,49 @@ url { src: "file://${BASEDIR}/export-extra-files" } } +### : import with deps-only : +### +opam-version: "2.0" +flags: compiler +### +opam-version: "2.0" +flags: compiler +### +opam-version: "2.0" +depends: "baz" +### +opam-version: "2.0" +### opam switch create for-deps foo.1 + +<><> Installing new switch packages <><><><><><><><><><><><><><><><><><><><><><> +Switch invariant: ["foo" {= "1"}] + +<><> Processing actions <><><><><><><><><><><><><><><><><><><><><><><><><><><><> +-> installed foo.1 +Done. +### opam install bar +The following actions will be performed: +=== install 2 packages + - install bar 1 + - install baz 1 [required by bar] + +<><> Processing actions <><><><><><><><><><><><><><><><><><><><><><><><><><><><> +-> installed baz.1 +-> installed bar.1 +Done. +### opam switch export deps.xp +### opam-cat deps.xp +compiler: ["foo.1"] +installed: ["bar.1" "baz.1" "foo.1"] +opam-version: "2.0" +roots: ["bar.1" "foo.1"] +### opam switch import deps.xp --switch deps --deps-only +The following actions will be performed: +=== install 2 packages + - install baz 1 + - install foo 1 + +<><> Processing actions <><><><><><><><><><><><><><><><><><><><><><><><><><><><> +-> installed baz.1 +-> installed foo.1 +Done.