Skip to content

Commit

Permalink
Merge pull request #321 from NathanReb/fix-empty-duniverse-dirs
Browse files Browse the repository at this point in the history
Fix parsing of lock files with empty duniverses
  • Loading branch information
NathanReb committed Aug 1, 2022
2 parents a3e29d2 + 82026ad commit 004c17b
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

- Enable locking of packages with depexts even with uninitialized system
package manager state (#322, @Leonidas-from-XIV)
- Fix a bug where `pull` would crash if the lock file contained no package to
vendor (#321, @NathanReb)

### Removed

Expand Down
18 changes: 12 additions & 6 deletions lib/lockfile.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ open Import
module Extra_field = struct
include Opam.Extra_field

let get ?file t opam =
let get ?file ?default t opam =
let open Result.O in
let* value_opt = get t opam in
match value_opt with
| Some result -> Ok result
| None ->
match (value_opt, default) with
| Some result, _ -> Ok result
| None, Some default -> Ok default
| None, None ->
let file_suffix_opt = Option.map ~f:(Printf.sprintf " %s") file in
let file_suffix = Option.value ~default:"" file_suffix_opt in
Error
Expand Down Expand Up @@ -87,6 +88,8 @@ module Root_packages = struct

let field =
Extra_field.make ~name:"root-packages" ~to_opam_value ~from_opam_value

let get ?file opam = Extra_field.get ?file field opam
end

module Depends = struct
Expand Down Expand Up @@ -236,6 +239,9 @@ module Duniverse_dirs = struct

let field =
Extra_field.make ~name:"duniverse-dirs" ~to_opam_value ~from_opam_value

let default = OpamUrl.Map.empty
let get ?file opam = Extra_field.get ?file ~default field opam
end

module Depexts = struct
Expand Down Expand Up @@ -362,10 +368,10 @@ let from_opam ~opam_monorepo_cwd ?file opam =
let open Result.O in
let* version = Extra_field.get ?file Version.field opam in
let* () = Version.compatible version in
let* root_packages = Extra_field.get ?file Root_packages.field opam in
let* root_packages = Root_packages.get ?file opam in
let* depends = Depends.from_filtered_formula (OpamFile.OPAM.depends opam) in
let pin_depends = OpamFile.OPAM.pin_depends opam in
let* duniverse_dirs = Extra_field.get ?file Duniverse_dirs.field opam in
let* duniverse_dirs = Duniverse_dirs.get ?file opam in
let depexts = OpamFile.OPAM.depexts opam in
let* source_config = Source_opam_config.get ~opam_monorepo_cwd opam in
Ok
Expand Down
8 changes: 8 additions & 0 deletions test/bin/empty-duniverse.t/empty-duniverse.opam
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
opam-version: "2.0"
depends: [
"ocaml"
"dune"
]
x-opam-monorepo-opam-repositories: [
"file://$OPAM_MONOREPO_CWD/minimal-repo"
]
27 changes: 27 additions & 0 deletions test/bin/empty-duniverse.t/run.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
One should be able to generate a lock file where no dependencies need to
be vendored and that should result in an empty duniverse.
That can happen if one has no external deps besides 'dune' and 'ocaml'
or if those are marked as 'opam-provided' for example.

Here our local package only depends on 'ocaml' and 'dune':

$ opam show --just-file -fdepends ./empty-duniverse.opam
ocaml, dune

We should be able to successfully lock:

$ gen-minimal-repo
$ opam-monorepo lock > /dev/null

And the lock file should not contain anything to vendor:

$ opam show --just-file -fdepends ./empty-duniverse.opam.locked | grep "\?vendor"
[1]

Finally, we should be able to pull this lock file, the tool will inform us that
there is nothing to pull and will not create a duniverse:

$ opam-monorepo pull
==> Using lockfile $TESTCASE_ROOT/empty-duniverse.opam.locked
==> No dependencies to pull, there's nothing to be done here!
$ find . -type d -name 'duniverse'

0 comments on commit 004c17b

Please sign in to comment.