From 7792499680b66b33e91fa4348f478c659da042f4 Mon Sep 17 00:00:00 2001 From: Sachin Beniwal Date: Thu, 27 Nov 2025 19:19:26 +0530 Subject: [PATCH 1/8] fix: good error message for pkg enabled put in dune-project Signed-off-by: Sachin Beniwal --- src/dune_lang/dune_project.ml | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/dune_lang/dune_project.ml b/src/dune_lang/dune_project.ml index 8ffc376bda7..3c97e4e408a 100644 --- a/src/dune_lang/dune_project.ml +++ b/src/dune_lang/dune_project.ml @@ -953,7 +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+ pkg_field = + field_o + "pkg" + (let+ loc = loc + and+ () = junk_everything in + loc) in + (match pkg_field with + | Some loc -> + User_error.raise + ~loc + [ Pp.text "(pkg ...) belongs in dune-workspace, not dune-project" + ; Pp.text "Hint: move this stanza to your dune-workspace file" + ] + | None -> ()); fun (opam_packages : (Loc.t * Package.t Memo.t) Package.Name.Map.t) -> let opam_file_location = Option.value opam_file_location ~default:(opam_file_location_default ~lang) From 0cbcd314c561418de0e09540dd3b169f638e2a63 Mon Sep 17 00:00:00 2001 From: Sachin Beniwal Date: Thu, 27 Nov 2025 19:19:55 +0530 Subject: [PATCH 2/8] tests Signed-off-by: Sachin Beniwal --- .../workspaces/pkg-in-dune-project.t/dune-project | 2 ++ .../test-cases/workspaces/pkg-in-dune-project.t/run.t | 10 ++++++++++ 2 files changed, 12 insertions(+) create mode 100644 test/blackbox-tests/test-cases/workspaces/pkg-in-dune-project.t/dune-project create mode 100644 test/blackbox-tests/test-cases/workspaces/pkg-in-dune-project.t/run.t diff --git a/test/blackbox-tests/test-cases/workspaces/pkg-in-dune-project.t/dune-project b/test/blackbox-tests/test-cases/workspaces/pkg-in-dune-project.t/dune-project new file mode 100644 index 00000000000..8d529532b0b --- /dev/null +++ b/test/blackbox-tests/test-cases/workspaces/pkg-in-dune-project.t/dune-project @@ -0,0 +1,2 @@ +(lang dune 3.20) +(pkg enabled) diff --git a/test/blackbox-tests/test-cases/workspaces/pkg-in-dune-project.t/run.t b/test/blackbox-tests/test-cases/workspaces/pkg-in-dune-project.t/run.t new file mode 100644 index 00000000000..7d8c713ccb1 --- /dev/null +++ b/test/blackbox-tests/test-cases/workspaces/pkg-in-dune-project.t/run.t @@ -0,0 +1,10 @@ +Test that putting (pkg enabled) in dune-project instead of dune-workspace +gives a helpful error message. + + $ dune build + File "dune-project", line 2, characters 0-13: + 2 | (pkg enabled) + ^^^^^^^^^^^^^ + Error: (pkg ...) belongs in dune-workspace, not dune-project + Hint: move this stanza to your dune-workspace file + [1] From 5e3dcd92f1c6b9195eaeb3576fa2f9056768896f Mon Sep 17 00:00:00 2001 From: Sachin Beniwal Date: Thu, 27 Nov 2025 19:52:50 +0530 Subject: [PATCH 3/8] fix: improve code Signed-off-by: Sachin Beniwal --- src/dune_lang/dune_project.ml | 12 ++++++------ .../run.t => pkg/pkg-in-dune-project-error.t} | 5 +++++ .../workspaces/pkg-in-dune-project.t/dune-project | 2 -- 3 files changed, 11 insertions(+), 8 deletions(-) rename test/blackbox-tests/test-cases/{workspaces/pkg-in-dune-project.t/run.t => pkg/pkg-in-dune-project-error.t} (81%) delete mode 100644 test/blackbox-tests/test-cases/workspaces/pkg-in-dune-project.t/dune-project diff --git a/src/dune_lang/dune_project.ml b/src/dune_lang/dune_project.ml index 3c97e4e408a..fa204ad8dce 100644 --- a/src/dune_lang/dune_project.ml +++ b/src/dune_lang/dune_project.ml @@ -956,17 +956,17 @@ let parse ~dir ~(lang : Lang.Instance.t) ~file = and+ pkg_field = field_o "pkg" - (let+ loc = loc - and+ () = junk_everything in - loc) + (Syntax.since Stanza.syntax (3, 21) + >>> let+ loc = loc + and+ () = junk_everything in + loc) in (match pkg_field with | Some loc -> User_error.raise ~loc - [ Pp.text "(pkg ...) belongs in dune-workspace, not dune-project" - ; Pp.text "Hint: move this stanza to your dune-workspace file" - ] + ~hints:[ Pp.text "move this stanza to your dune-workspace file" ] + [ Pp.text "(pkg ...) belongs in dune-workspace, not dune-project" ] | None -> ()); 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/workspaces/pkg-in-dune-project.t/run.t b/test/blackbox-tests/test-cases/pkg/pkg-in-dune-project-error.t similarity index 81% rename from test/blackbox-tests/test-cases/workspaces/pkg-in-dune-project.t/run.t rename to test/blackbox-tests/test-cases/pkg/pkg-in-dune-project-error.t index 7d8c713ccb1..ee075c9a3dc 100644 --- a/test/blackbox-tests/test-cases/workspaces/pkg-in-dune-project.t/run.t +++ b/test/blackbox-tests/test-cases/pkg/pkg-in-dune-project-error.t @@ -1,6 +1,11 @@ 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) diff --git a/test/blackbox-tests/test-cases/workspaces/pkg-in-dune-project.t/dune-project b/test/blackbox-tests/test-cases/workspaces/pkg-in-dune-project.t/dune-project deleted file mode 100644 index 8d529532b0b..00000000000 --- a/test/blackbox-tests/test-cases/workspaces/pkg-in-dune-project.t/dune-project +++ /dev/null @@ -1,2 +0,0 @@ -(lang dune 3.20) -(pkg enabled) From 6a6228e14e7ca6249fb366c0a641d584bc32d9a4 Mon Sep 17 00:00:00 2001 From: Sachin Beniwal Date: Fri, 28 Nov 2025 15:52:09 +0530 Subject: [PATCH 4/8] fix: a more helpful message Signed-off-by: Sachin Beniwal --- src/dune_lang/dune_project.ml | 11 +++++++++-- .../test-cases/pkg/pkg-in-dune-project-error.t | 6 ++++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/dune_lang/dune_project.ml b/src/dune_lang/dune_project.ml index fa204ad8dce..91dfc440b5c 100644 --- a/src/dune_lang/dune_project.ml +++ b/src/dune_lang/dune_project.ml @@ -965,8 +965,15 @@ let parse ~dir ~(lang : Lang.Instance.t) ~file = | Some loc -> User_error.raise ~loc - ~hints:[ Pp.text "move this stanza to your dune-workspace file" ] - [ Pp.text "(pkg ...) belongs in dune-workspace, not dune-project" ] + ~hints: + [ Pp.text + "Add this configuration to your dune-workspace file (create one in your \ + workspace root if you don't have one)." + ] + [ Pp.text + "The (pkg ...) configuration is only valid in dune-workspace, not a \ + dune-project." + ] | None -> ()); 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 index ee075c9a3dc..56f67bce7fa 100644 --- 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 @@ -10,6 +10,8 @@ gives a helpful error message. File "dune-project", line 2, characters 0-13: 2 | (pkg enabled) ^^^^^^^^^^^^^ - Error: (pkg ...) belongs in dune-workspace, not dune-project - Hint: move this stanza to your dune-workspace file + Error: The (pkg ...) configuration is only valid in dune-workspace, not a + dune-project. + Hint: Add this configuration to your dune-workspace file (create one in your + workspace root if you don't have one). [1] From b8fca75035a31d0a8a07e3ee787bef77ebf68dec Mon Sep 17 00:00:00 2001 From: Sachin Beniwal Date: Fri, 28 Nov 2025 15:59:15 +0530 Subject: [PATCH 5/8] chore: added entry for CHANGES.md Signed-off-by: Sachin Beniwal --- doc/changes/fixed/12802.md | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 doc/changes/fixed/12802.md 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) From a95cd99b99090d18f054e930681461b3b9c4850a Mon Sep 17 00:00:00 2001 From: Sachin Beniwal Date: Fri, 28 Nov 2025 17:22:45 +0530 Subject: [PATCH 6/8] fix: remove unnecessary version check for pkg field in dune-project Signed-off-by: Sachin Beniwal --- src/dune_lang/dune_project.ml | 35 ++++++++++++++++------------------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/src/dune_lang/dune_project.ml b/src/dune_lang/dune_project.ml index 91dfc440b5c..56e729ca7de 100644 --- a/src/dune_lang/dune_project.ml +++ b/src/dune_lang/dune_project.ml @@ -953,28 +953,25 @@ let parse ~dir ~(lang : Lang.Instance.t) ~file = "warnings" ~default:Warning.Settings.empty (Syntax.since Stanza.syntax (3, 11) >>> Warning.Settings.decode) - and+ pkg_field = + and+ () = field_o "pkg" - (Syntax.since Stanza.syntax (3, 21) - >>> let+ loc = loc - and+ () = junk_everything in - loc) + (let+ loc = loc + and+ () = junk_everything in + loc) + >>| Option.iter ~f:(fun loc -> + User_error.raise + ~loc + ~hints: + [ Pp.text + "Add this configuration to your dune-workspace file (create one in your \ + workspace root if you don't have one)." + ] + [ Pp.text + "The (pkg ...) configuration is only valid in dune-workspace, not a \ + dune-project." + ]) in - (match pkg_field with - | Some loc -> - User_error.raise - ~loc - ~hints: - [ Pp.text - "Add this configuration to your dune-workspace file (create one in your \ - workspace root if you don't have one)." - ] - [ Pp.text - "The (pkg ...) configuration is only valid in dune-workspace, not a \ - dune-project." - ] - | None -> ()); fun (opam_packages : (Loc.t * Package.t Memo.t) Package.Name.Map.t) -> let opam_file_location = Option.value opam_file_location ~default:(opam_file_location_default ~lang) From 97623ceca9e796395bb40e6d2b9dddf84bbb78ed Mon Sep 17 00:00:00 2001 From: Sachin Beniwal Date: Mon, 1 Dec 2025 16:27:14 +0530 Subject: [PATCH 7/8] fix: improved error text Signed-off-by: Sachin Beniwal --- src/dune_lang/dune_project.ml | 9 +++------ .../test-cases/pkg/pkg-in-dune-project-error.t | 7 +++---- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/src/dune_lang/dune_project.ml b/src/dune_lang/dune_project.ml index 56e729ca7de..a963a549539 100644 --- a/src/dune_lang/dune_project.ml +++ b/src/dune_lang/dune_project.ml @@ -964,13 +964,10 @@ let parse ~dir ~(lang : Lang.Instance.t) ~file = ~loc ~hints: [ Pp.text - "Add this configuration to your dune-workspace file (create one in your \ - workspace root if you don't have one)." + "Move this stanza to your dune-workspace file. If you don't have one,\n\ + \ create one in your workspace root." ] - [ Pp.text - "The (pkg ...) configuration is only valid in dune-workspace, not a \ - dune-project." - ]) + [ 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 index 56f67bce7fa..c04e4512d8e 100644 --- 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 @@ -10,8 +10,7 @@ gives a helpful error message. File "dune-project", line 2, characters 0-13: 2 | (pkg enabled) ^^^^^^^^^^^^^ - Error: The (pkg ...) configuration is only valid in dune-workspace, not a - dune-project. - Hint: Add this configuration to your dune-workspace file (create one in your - workspace root if you don't have one). + 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] From f240732b88a6d10e592b1f051660bd556783775d Mon Sep 17 00:00:00 2001 From: Sachin Beniwal Date: Mon, 1 Dec 2025 16:30:16 +0530 Subject: [PATCH 8/8] fix: fmt Signed-off-by: Sachin Beniwal --- src/dune_lang/dune_project.ml | 4 ++-- .../blackbox-tests/test-cases/pkg/pkg-in-dune-project-error.t | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/dune_lang/dune_project.ml b/src/dune_lang/dune_project.ml index a963a549539..e7653c646c4 100644 --- a/src/dune_lang/dune_project.ml +++ b/src/dune_lang/dune_project.ml @@ -964,8 +964,8 @@ let parse ~dir ~(lang : Lang.Instance.t) ~file = ~loc ~hints: [ Pp.text - "Move this stanza to your dune-workspace file. If you don't have one,\n\ - \ create one in your workspace root." + "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 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 index c04e4512d8e..efad2230df5 100644 --- 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 @@ -12,5 +12,5 @@ gives a helpful error message. ^^^^^^^^^^^^^ 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. + create one in your workspace root. [1]