diff --git a/bin/alias.ml b/bin/alias.ml index 35eb505195f..ed392360d80 100644 --- a/bin/alias.ml +++ b/bin/alias.ml @@ -106,8 +106,17 @@ let dep_on_alias_rec_multi_contexts ~dir:src_dir ~name ~contexts = | true -> Action_builder.return () | false -> let* load_dir = + let selected = !Dune_engine.Clflags.selected_context in + let hint_contexts = + match List.filter contexts ~f:(Context_name.equal selected) with + | [] -> + (match contexts with + | [] -> [] + | ctx :: _ -> [ ctx ]) + | ctxs -> ctxs + in Action_builder.all - @@ List.map contexts ~f:(fun ctx -> + @@ List.map hint_contexts ~f:(fun ctx -> let dir = Source_tree.Dir.path dir |> Path.Build.append_source (Context_name.build_dir ctx) diff --git a/src/dune_engine/clflags.ml b/src/dune_engine/clflags.ml index 5c63238eb81..2eb0731c4b1 100644 --- a/src/dune_engine/clflags.ml +++ b/src/dune_engine/clflags.ml @@ -32,3 +32,4 @@ let display = ref Display.Quiet let can_go_in_shared_cache_default = ref false let diff_command = ref None let wait_for_filesystem_clock = ref false +let selected_context = ref Context_name.default diff --git a/src/dune_engine/clflags.mli b/src/dune_engine/clflags.mli index a887327b5bc..d9d40d351f9 100644 --- a/src/dune_engine/clflags.mli +++ b/src/dune_engine/clflags.mli @@ -44,3 +44,6 @@ val diff_command : string option ref (** Wait for the filesystem clock to advance rather than dropping cached digest entries *) val wait_for_filesystem_clock : bool ref + +(** The build context selected by the user on the command line *) +val selected_context : Context_name.t ref diff --git a/test/blackbox-tests/test-cases/alias/alias-candidates-multi-context.t b/test/blackbox-tests/test-cases/alias/alias-candidates-multi-context.t new file mode 100644 index 00000000000..6a71b4c2172 --- /dev/null +++ b/test/blackbox-tests/test-cases/alias/alias-candidates-multi-context.t @@ -0,0 +1,34 @@ +Alias hint generation should only use the selected build context in a +multi-context workspace. Aliases from other build contexts should not appear +as hints. + + $ cat > dune-project << EOF + > (lang dune 3.17) + > EOF + + $ cat > dune-workspace << EOF + > (lang dune 3.17) + > (context default) + > (context (default (name other))) + > EOF + + $ cat > dune << EOF + > (rule + > (alias foo) + > (action (echo "foo")) + > (enabled_if (= %{context_name} "default"))) + > (rule + > (alias foe) + > (action (echo "foe")) + > (enabled_if (= %{context_name} "other"))) + > EOF + +We have "foo" in the default context and "foe" in the "other" context. When +building a misspelled alias "@fou", hints should only come from the default +context, so only "foo" (not "foe") should be suggested. + + $ dune build @fou + Error: Alias "fou" specified on the command line is empty. + It is not defined in . or any of its descendants. + Hint: did you mean fmt or foo? + [1]