From 60f99fa09536896e61ce9b4da3d60c9c5ebf739e Mon Sep 17 00:00:00 2001 From: Nathan Rebours Date: Wed, 8 Jun 2022 12:19:54 +0200 Subject: [PATCH] Save CLI arguments to the lock file This can be useful for reproducing the exact command that generated a lock file Signed-off-by: Nathan Rebours --- CHANGES.md | 2 ++ cli/lock.ml | 8 +++++++- lib/lockfile.ml | 19 +++++++++++++++++-- lib/lockfile.mli | 1 + .../cli-args-in-lockfile.opam | 5 +++++ test/bin/cli-args-in-lockfile.t/run.t | 7 +++++++ 6 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 test/bin/cli-args-in-lockfile.t/cli-args-in-lockfile.opam create mode 100644 test/bin/cli-args-in-lockfile.t/run.t diff --git a/CHANGES.md b/CHANGES.md index fbe720129..f3c26ebb3 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -7,6 +7,8 @@ @NathanReb) - Add command line options to complement or overwrite `x-opam-monorepo-*` fields. (#307, @NathanReb) +- Save the `lock` CLI arguments in `x-opam-monorepo-cli-args` when generating a + lock file. (#309, @NathanReb) ### Changed diff --git a/cli/lock.ml b/cli/lock.ml index a27db5deb..1ab09df21 100644 --- a/cli/lock.ml +++ b/cli/lock.ml @@ -449,6 +449,11 @@ let extract_source_config ~adjustment ~opam_monorepo_cwd ~opam_files Source_opam_config.make ~opam_monorepo_cwd ~adjustment ~local_opam_files_config +let raw_cli_args () = + match Array.to_list Sys.argv with + | _bin :: "lock" :: args -> args + | _ -> assert false + let run (`Root root) (`Recurse_opam recurse) (`Build_only build_only) (`Allow_jbuilder allow_jbuilder) (`Ocaml_version ocaml_version) (`Require_cross_compile require_cross_compile) @@ -485,8 +490,9 @@ let run (`Root root) (`Recurse_opam recurse) (`Build_only build_only) Lockfile.create ~source_config ~root_packages:target_packages ~dependency_entries ~root_depexts:target_depexts ~duniverse () in + let cli_args = raw_cli_args () in let* () = - Lockfile.save ~opam_monorepo_cwd:root ~file:lockfile_path lockfile + Lockfile.save ~opam_monorepo_cwd:root ~cli_args ~file:lockfile_path lockfile in Common.Logs.app (fun l -> l diff --git a/lib/lockfile.ml b/lib/lockfile.ml index f54071817..36e69a4a2 100644 --- a/lib/lockfile.ml +++ b/lib/lockfile.ml @@ -261,6 +261,21 @@ module Depexts = struct List.concat all |> List.sort_uniq ~cmp:compare_elm end +module Cli_args = struct + (** Field used to store the raw command line arguments passed to [lock]. + It is set but not read and therefore is not part of the main lock file + type. *) + + type _t = string list + + let name = "cli-args" + let shape = Serial_shape.(list string) + let from_opam_value value = Serial_shape.from_opam_val shape value + let to_opam_value t = Serial_shape.to_opam_val shape t + let field = Extra_field.make ~name ~to_opam_value ~from_opam_value + let add t opam = match t with [] -> opam | _ -> Extra_field.set field t opam +end + type t = { version : Version.t; root_packages : Root_packages.t; @@ -364,8 +379,8 @@ let from_opam ~opam_monorepo_cwd ?file opam = source_config; } -let save ~opam_monorepo_cwd ~file t = - let opam = to_opam ~opam_monorepo_cwd t in +let save ~opam_monorepo_cwd ~cli_args ~file t = + let opam = Cli_args.add cli_args (to_opam ~opam_monorepo_cwd t) in Bos.OS.File.with_oc file (fun oc () -> OpamFile.OPAM.write_to_channel oc opam; diff --git a/lib/lockfile.mli b/lib/lockfile.mli index 97a1b1d3d..f9390f5ac 100644 --- a/lib/lockfile.mli +++ b/lib/lockfile.mli @@ -20,6 +20,7 @@ val ocaml_version : t -> OpamPackage.Version.t option val save : opam_monorepo_cwd:Fpath.t -> + cli_args:string list -> file:Fpath.t -> t -> (unit, [ `Msg of string ]) result diff --git a/test/bin/cli-args-in-lockfile.t/cli-args-in-lockfile.opam b/test/bin/cli-args-in-lockfile.t/cli-args-in-lockfile.opam new file mode 100644 index 000000000..7affd31a2 --- /dev/null +++ b/test/bin/cli-args-in-lockfile.t/cli-args-in-lockfile.opam @@ -0,0 +1,5 @@ +opam-version: "2.0" +depends: [ + "dune" + "ocaml" +] diff --git a/test/bin/cli-args-in-lockfile.t/run.t b/test/bin/cli-args-in-lockfile.t/run.t new file mode 100644 index 000000000..12482e5e6 --- /dev/null +++ b/test/bin/cli-args-in-lockfile.t/run.t @@ -0,0 +1,7 @@ +When generating a lockfile, the CLI arguments passed to lock +should be save to the lockfile + + $ gen-minimal-repo + $ opam-monorepo lock --recurse --opam-provided [b] --opam-repositories '[file://$OPAM_MONOREPO_CWD/minimal-repo]' --ocaml-version 4.13.1 > /dev/null + $ opam show --just-file -fx-opam-monorepo-cli-args ./cli-args-in-lockfile.opam.locked + --recurse, --opam-provided, [b], --opam-repositories, [file://$OPAM_MONOREPO_CWD/minimal-repo], --ocaml-version, 4.13.1