Skip to content

Commit

Permalink
lock: add '--keep-local' argument to add local pins (unresolved ones)…
Browse files Browse the repository at this point in the history
… to 'pin-depends:' field
  • Loading branch information
rjbou committed Mar 6, 2025
1 parent 4bd1a89 commit 5de7c5f
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 11 deletions.
2 changes: 2 additions & 0 deletions master_changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ users)

## Lock
* [BUG] Fix `pin-depends` for `with-*` dependencies [#5471 @rjbou - fix #5428]
* [NEW] Add `--keep-local` to keep (unresolved) local pins in `pin-depends` field [#6411 @rjbou - fix #4897]

## Clean

Expand Down Expand Up @@ -225,6 +226,7 @@ users)
* `OpamArg`: export `require_checksums` and `no_checksums`, that are shared with `build_options` [#5563 @rjbou]
* `OpamArg.hash_kinds`: was added [#5960 @kit-ty-kate]
* `OpamRepositoryCommand.switch_repos`: expose the function [#5014 @kit-ty-kate]
* `OpamLockCommand.lock_opam`: add `?keep_local` argument to add (unresolved) local pins to pin-depends [#6411 @rjbou]

## opam-repository
* `OpamDownload.get_output`: fix `wget` option for `POST` requests [#6036 @rjbou]
Expand Down
17 changes: 12 additions & 5 deletions src/client/opamCommands.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4392,9 +4392,10 @@ let lock cli =
fixed and the same filter is on all dependencies that are added from \
them";
`P "- $(i,pin-depends) are kept and new ones are added if in the \
dependencies some packages are pinned ";
dependencies some packages are pinned";
`P "- pins are resolved: if a package is locally pinned, opam tries to get \
its remote url and branch, and sets this as the target URL";
its remote url and branch, and sets this as the target URL; otherwise \
it ignore them. You can use $(i,--keep-local) to keep them.";
`S Manpage.s_arguments;
`S Manpage.s_options;
]
Expand All @@ -4404,7 +4405,11 @@ let lock cli =
"Only lock direct dependencies, rather than the whole dependency tree."
in
let lock_suffix = OpamArg.lock_suffix cli in
let lock global_options only_direct lock_suffix atom_locs () =
let keep_local_flag =
mk_flag ~cli (cli_from cli2_4) ["keep-local"]
"Do not discard local pins from pin-depends."
in
let lock global_options only_direct lock_suffix keep_local atom_locs () =
apply_global_options cli global_options;
OpamGlobalState.with_ `Lock_none @@ fun gt ->
OpamSwitchState.with_ `Lock_none gt @@ fun st ->
Expand All @@ -4420,7 +4425,9 @@ let lock cli =
let pkg_done =
OpamPackage.Set.fold (fun nv msgs ->
let opam = OpamSwitchState.opam st nv in
let locked = OpamLockCommand.lock_opam ~only_direct st opam in
let locked =
OpamLockCommand.lock_opam ~only_direct ~keep_local st opam
in
let locked_fname =
OpamFilename.add_extension
(OpamFilename.of_string (OpamPackage.name_to_string nv))
Expand All @@ -4443,7 +4450,7 @@ let lock cli =
in
mk_command ~cli (cli_from cli2_1) "lock" ~doc ~man
Term.(const lock $global_options cli $only_direct_flag $lock_suffix
$atom_or_local_list)
$keep_local_flag $atom_or_local_list)

(* HELP *)
let help =
Expand Down
13 changes: 9 additions & 4 deletions src/client/opamLockCommand.ml
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ let get_git_url url nv dir =
(OpamPackage.to_string nv);
None)

let lock_opam ?(only_direct=false) st opam =
let lock_opam ?(only_direct=false) ?(keep_local=false) st opam =
let nv = OpamFile.OPAM.package opam in
(* Depends *)
let all_depends =
Expand Down Expand Up @@ -294,9 +294,14 @@ let lock_opam ?(only_direct=false) st opam =
match OpamUrl.local_dir u with
| Some d ->
let local_warn () =
OpamConsole.warning "Dependency %s is pinned to local target %s"
(OpamPackage.to_string nv) (OpamUrl.to_string u);
acc
if keep_local then
(OpamConsole.note "Dependency %s is pinned to local target %s, keeping it."
(OpamPackage.to_string nv) (OpamUrl.to_string u);
(nv,u)::acc)
else
(OpamConsole.warning "Dependency %s is pinned to local target %s"
(OpamPackage.to_string nv) (OpamUrl.to_string u);
acc)
in
(match u.OpamUrl.backend with
| #OpamUrl.version_control ->
Expand Down
10 changes: 8 additions & 2 deletions src/client/opamLockCommand.mli
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,11 @@ val select_packages:
[ `Atom of atom | `Filename of filename | `Dirname of dirname ] list ->
'a switch_state -> 'a switch_state * package_set

(** Returns the locked opam file, according its depends, depopts, and pins. *)
val lock_opam: ?only_direct:bool -> 'a switch_state -> OpamFile.OPAM.t -> OpamFile.OPAM.t
(** Returns the locked opam file, according its depends, depopts, and pins.
If [only_direct] is set to true, it only adds direct dependencies specified
in the opam file. If [keep_local] is set to true, local pins that can not
be resolved to a distant url are added with their local path in
'pin_depends:' field. *)
val lock_opam:
?only_direct:bool -> ?keep_local:bool -> 'a switch_state -> OpamFile.OPAM.t
-> OpamFile.OPAM.t
40 changes: 40 additions & 0 deletions tests/reftests/lock.test
Original file line number Diff line number Diff line change
Expand Up @@ -1318,3 +1318,43 @@ opam-version: "2.0"
pin-depends: [["a-doc-dep.dev" "git+https://github.com/dbuenzli/cmdliner#e8f8890fe48"] ["a-simple-dep.dev" "git+https://github.com/dbuenzli/cmdliner#f239981642a"] ["a-test-dep.dev" "git+https://github.com/dbuenzli/cmdliner#1de361182ab"]]
synopsis: "A word"
version: "dev"
### : Keep local pins as pin-depends
### <pin:local-one/local-one.opam>
opam-version: "2.0"
### <pin:local-two/local-two.opam>
opam-version: "2.0"
### <pkg:distant-x.1>
opam-version: "2.0"
depends: [ "local-one" "local-two" ]
### opam switch create locality --empty
### opam pin ./local-one -n
[NOTE] Package local-one does not exist in opam repositories registered in the current switch.
local-one is now pinned to file://${BASEDIR}/local-one (version dev)
### opam pin ./local-two -n
[NOTE] Package local-two does not exist in opam repositories registered in the current switch.
local-two is now pinned to file://${BASEDIR}/local-two (version dev)
### opam install distant-x
The following actions will be performed:
=== install 3 packages
- install distant-x 1
- install local-one dev (pinned) [required by distant-x]
- install local-two dev (pinned) [required by distant-x]

<><> Processing actions <><><><><><><><><><><><><><><><><><><><><><><><><><><><>
-> retrieved local-one.dev (file://${BASEDIR}/local-one)
-> installed local-one.dev
-> retrieved local-two.dev (file://${BASEDIR}/local-two)
-> installed local-two.dev
-> installed distant-x.1
Done.
### opam lock distant-x --keep-local
[NOTE] Dependency local-one.dev is pinned to local target file://${BASEDIR}/local-one, keeping it.
[NOTE] Dependency local-two.dev is pinned to local target file://${BASEDIR}/local-two, keeping it.
Generated lock files for:
- distant-x.1: ${BASEDIR}/distant-x.opam.locked
### opam-cat distant-x.opam.locked
depends: ["local-one" {= "dev"} "local-two" {= "dev"}]
name: "distant-x"
opam-version: "2.0"
pin-depends: [["local-one.dev" "file://${BASEDIR}/local-one"] ["local-two.dev" "file://${BASEDIR}/local-two"]]
version: "1"

0 comments on commit 5de7c5f

Please sign in to comment.