Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix parsing of lock files with empty duniverses #321

Merged
merged 4 commits into from
Aug 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'