diff --git a/doc/changes/fixed/12802.md b/doc/changes/fixed/12802.md new file mode 100644 index 00000000000..5eed559026d --- /dev/null +++ b/doc/changes/fixed/12802.md @@ -0,0 +1,3 @@ +- Provide a more informative error message when `(pkg enabled)` is put in + `dune-project` instead of `dune-workspace`. (#12802, fixes #12801, + @benodiwal) diff --git a/src/dune_lang/dune_project.ml b/src/dune_lang/dune_project.ml index 8ffc376bda7..e7653c646c4 100644 --- a/src/dune_lang/dune_project.ml +++ b/src/dune_lang/dune_project.ml @@ -953,6 +953,21 @@ let parse ~dir ~(lang : Lang.Instance.t) ~file = "warnings" ~default:Warning.Settings.empty (Syntax.since Stanza.syntax (3, 11) >>> Warning.Settings.decode) + and+ () = + field_o + "pkg" + (let+ loc = loc + and+ () = junk_everything in + loc) + >>| Option.iter ~f:(fun loc -> + User_error.raise + ~loc + ~hints: + [ Pp.text + "Move this stanza to your dune-workspace file. If you don't have one, \ + create one in your workspace root." + ] + [ Pp.text "The (pkg ...) stanza is only valid in dune-workspace files." ]) in fun (opam_packages : (Loc.t * Package.t Memo.t) Package.Name.Map.t) -> let opam_file_location = diff --git a/test/blackbox-tests/test-cases/pkg/pkg-in-dune-project-error.t b/test/blackbox-tests/test-cases/pkg/pkg-in-dune-project-error.t new file mode 100644 index 00000000000..efad2230df5 --- /dev/null +++ b/test/blackbox-tests/test-cases/pkg/pkg-in-dune-project-error.t @@ -0,0 +1,16 @@ +Test that putting (pkg enabled) in dune-project instead of dune-workspace +gives a helpful error message. + + $ cat > dune-project < (lang dune 3.21) + > (pkg enabled) + > EOF + + $ dune build + File "dune-project", line 2, characters 0-13: + 2 | (pkg enabled) + ^^^^^^^^^^^^^ + Error: The (pkg ...) stanza is only valid in dune-workspace files. + Hint: Move this stanza to your dune-workspace file. If you don't have one, + create one in your workspace root. + [1]