diff --git a/src/dune_rules/lock_dir.ml b/src/dune_rules/lock_dir.ml index c3343249a1d..4126e245224 100644 --- a/src/dune_rules/lock_dir.ml +++ b/src/dune_rules/lock_dir.ml @@ -177,11 +177,23 @@ let get_source_path_for_context ctx_name = | false -> None | true -> Some ctx) with - | None | Some (Default { lock_dir = None; _ }) -> Memo.return (Some default_source_path) | Some (Default { lock_dir = Some lock_dir_selection; _ }) -> let+ source_lock_dir = select_lock_dir lock_dir_selection in Some source_lock_dir | Some (Opam _) -> Memo.return None + | None | Some (Default { lock_dir = None; _ }) -> + (* CR-someday Alizter: The logic here is hard to follow. Improve the lock + directory selection logic so that it is harder to make a mistake. This + can be done by enforcing invariants about contexts and their lock + directories slightly earlier. *) + Memo.return + @@ + (* Context doesn't specify a lock_dir, check if there's a top-level one *) + (match workspace.lock_dirs with + | [ lock_dir ] -> Some lock_dir.path + | [] | _ :: _ :: _ -> + (* No clear choice, fall back to default *) + Some default_source_path) ;; let get_path ctx_name = diff --git a/test/blackbox-tests/test-cases/pkg/lockdir-switch.t b/test/blackbox-tests/test-cases/pkg/lockdir-switch.t new file mode 100644 index 00000000000..d1c98b10dce --- /dev/null +++ b/test/blackbox-tests/test-cases/pkg/lockdir-switch.t @@ -0,0 +1,67 @@ +This test checks what happens when you lock with default directory then change +workspace to use a custom lock directory. + + $ . ./helpers.sh + $ mkrepo + $ mkpkg foo + $ add_mock_repo_if_needed + + $ cat > dune-project < (lang dune 3.21) + > (package + > (name myproject) + > (depends foo)) + > EOF + +Lock with default directory name: + + $ dune pkg lock + Solution for dune.lock + + Dependencies common to all supported platforms: + - foo.0.0.1 + +Now change the workspace to use a custom lock directory: + + $ cat > dune-workspace < (lang dune 3.21) + > (lock_dir + > (path custom.lock) + > (repositories mock)) + > (repository + > (name mock) + > (url "file://$PWD/mock-opam-repository")) + > EOF + + $ cat > dune < (executable + > (public_name bar)) + > EOF + + $ cat > bar.ml < let () = print_endline "hello from bar" ;; + > EOF + + $ dune exec -- bar + hello from bar + +Now try with a context stanza that references the custom lock directory with a +context stanza: + + $ cat > dune-workspace < (lang dune 3.21) + > (context + > (default + > (lock_dir custom.lock))) + > (lock_dir + > (path custom.lock) + > (repositories mock)) + > (repository + > (name mock) + > (url "file://$PWD/mock-opam-repository")) + > EOF + +This is working as intended. The error is not relevant here. + + $ dune exec -- bar + hello from bar