From 1becc6e03c8c39b478cf42fe87add71fbbd5c772 Mon Sep 17 00:00:00 2001 From: Rudi Grinberg Date: Fri, 11 Oct 2024 10:05:04 +0100 Subject: [PATCH] fix(pkg): add location hash mismatch error (#11007) We use the lcok directory as the location rather than pointing at the hash, as the user shouldn't really touch the hash. Signed-off-by: Rudi Grinberg --- src/dune_pkg/package_universe.ml | 5 ++--- src/dune_pkg/package_universe.mli | 2 +- src/dune_rules/gen_rules.ml | 6 ++++-- src/dune_rules/lock_dir.ml | 10 ++++++---- src/dune_rules/lock_dir.mli | 1 + test/blackbox-tests/test-cases/pkg/lock-out-of-sync.t | 1 + 6 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/dune_pkg/package_universe.ml b/src/dune_pkg/package_universe.ml index d069ffb016b..a72d1a2e99d 100644 --- a/src/dune_pkg/package_universe.ml +++ b/src/dune_pkg/package_universe.ml @@ -143,9 +143,8 @@ let up_to_date local_packages ~dependency_hash:saved_dependency_hash = non_local_dependencies_hash -> `Valid | None, Some _ -> `Valid (* This case happens when the user writes themselves their lock.dune. *) - | Some _, Some non_local_dependencies_hash -> - `Invalid (Some non_local_dependencies_hash) - | Some _, None -> `Invalid None + | Some _, Some _ -> `Invalid + | Some _, None -> `Invalid ;; let validate_dependency_hash local_packages ~saved_dependency_hash = diff --git a/src/dune_pkg/package_universe.mli b/src/dune_pkg/package_universe.mli index 2d467e45a55..4fb5ffaeffd 100644 --- a/src/dune_pkg/package_universe.mli +++ b/src/dune_pkg/package_universe.mli @@ -16,7 +16,7 @@ val create val up_to_date : Local_package.t Package_name.Map.t -> dependency_hash:Local_package.Dependency_hash.t option - -> [ `Valid | `Invalid of Local_package.Dependency_hash.t option ] + -> [ `Valid | `Invalid ] (** Returns the dependencies of the specified package within the package universe *) diff --git a/src/dune_rules/gen_rules.ml b/src/dune_rules/gen_rules.ml index 17d6e04e19e..afc233e06f1 100644 --- a/src/dune_rules/gen_rules.ml +++ b/src/dune_rules/gen_rules.ml @@ -663,7 +663,7 @@ let raise_on_lock_dir_out_of_sync = let* lock_dir_available = Lock_dir.lock_dir_active ctx in if lock_dir_available then - let* lock_dir = Lock_dir.get_exn ctx in + let* path, lock_dir = Lock_dir.get_with_path ctx >>| User_error.ok_exn in let+ local_packages = Dune_load.packages () >>| Dune_lang.Package.Name.Map.map ~f:Dune_pkg.Local_package.of_package @@ -674,9 +674,11 @@ let raise_on_lock_dir_out_of_sync = ~dependency_hash:(Option.map ~f:snd lock_dir.dependency_hash) with | `Valid -> () - | `Invalid _ -> + | `Invalid -> + let loc = Loc.in_file (Path.source (Path.Source.relative path "lock.dune")) in let hints = Pp.[ text "run dune pkg lock" ] in User_error.raise + ~loc ~hints [ Pp.text "The lock dir is not sync with your dune-project" ] else Memo.return ()) diff --git a/src/dune_rules/lock_dir.ml b/src/dune_rules/lock_dir.ml index 92be6043bb9..8fbba4bae35 100644 --- a/src/dune_rules/lock_dir.ml +++ b/src/dune_rules/lock_dir.ml @@ -128,9 +128,10 @@ let get_workspace_lock_dir ctx = Workspace.find_lock_dir workspace path ;; -let get ctx = - let* result = get_path ctx >>| Option.value_exn >>= Load.load in - match result with +let get_with_path ctx = + let* path = get_path ctx >>| Option.value_exn in + Load.load path + >>= function | Error e -> Memo.return (Error e) | Ok lock_dir -> let+ workspace_lock_dir = get_workspace_lock_dir ctx in @@ -140,9 +141,10 @@ let get ctx = Solver_stats.Expanded_variable_bindings.validate_against_solver_env lock_dir.expanded_solver_variable_bindings (workspace_lock_dir.solver_env |> Option.value ~default:Solver_env.empty)); - Ok lock_dir + Ok (path, lock_dir) ;; +let get ctx = get_with_path ctx >>| Result.map ~f:snd let get_exn ctx = get ctx >>| User_error.ok_exn let of_dev_tool dev_tool = diff --git a/src/dune_rules/lock_dir.mli b/src/dune_rules/lock_dir.mli index 2942710067e..8d69758f5ae 100644 --- a/src/dune_rules/lock_dir.mli +++ b/src/dune_rules/lock_dir.mli @@ -3,6 +3,7 @@ module Pkg = Dune_pkg.Lock_dir.Pkg type t := Dune_pkg.Lock_dir.t +val get_with_path : Context_name.t -> (Path.Source.t * t, User_message.t) result Memo.t val get : Context_name.t -> (t, User_message.t) result Memo.t val get_exn : Context_name.t -> t Memo.t val of_dev_tool : Dune_pkg.Dev_tool.t -> t Memo.t diff --git a/test/blackbox-tests/test-cases/pkg/lock-out-of-sync.t b/test/blackbox-tests/test-cases/pkg/lock-out-of-sync.t index 8c918bb7e35..a5ec28c795a 100644 --- a/test/blackbox-tests/test-cases/pkg/lock-out-of-sync.t +++ b/test/blackbox-tests/test-cases/pkg/lock-out-of-sync.t @@ -41,6 +41,7 @@ We add the bar dependency to the test package It fails as we have not regenerated the lock: $ dune build + File "dune.lock/lock.dune", line 1, characters 0-0: Error: The lock dir is not sync with your dune-project Hint: run dune pkg lock [1]