Dune Flag Propagation and OCaml -w Syntax: Overlapping Range Precedence
0 reputation · 05 Nov 2023, 15:48 UTC
0 reputation · 05 Nov 2023, 15:48 UTC
When configuring OCaml compiler warnings through the -w flag, the interaction between Dune's build defaults and the compiler's own warning syntax creates ambiguity in complex projects. The -w flag accepts comma-separated warning numbers or ranges with +, -, or = prefixes, but the precise precedence rules remain unclear when a warning number falls inside both an enabled range and a later disabled range.
Dune automatically forwards -w settings from dune-project or dune files to the compiler, yet it may also apply its own default warning set. This raises uncertainty about whether Dune's defaults override, merge with, or defer to user-specified -w flags when overlapping ranges are present. The syntax itself has evolved across compiler releases, with older versions using a single prefix for the entire list and newer versions supporting per-warning prefixes, which complicates configuration portability.
Given that warning numbers shift between OCaml releases and broad ranges can silently suppress useful diagnostics, what is the authoritative precedence model when overlapping per-warning prefixes and ranges are combined? How should a project structure its -w specification in Dune to avoid conflicts with build-system defaults? Which compiler version boundaries define the supported syntax for per-warning prefixes versus whole-list prefixes?
Precedence for -w is last flag wins in the command line string that reaches the OCaml compiler. If a warning number is covered by multiple overlapping ranges or per-warning prefixes, the last occurrence of a flag that mentions that number determines its final enabled/disabled state. In Dune, flags from dune-project and per-stanza (flags ...) are forwarded to the compiler and appended to Dune's internal command. User-specified flags in a local dune file therefore typically appear later than Dune's own defaults and override them.
-w arguments linearly left to right. Overlapping ranges are resolved by the last matching specification.-w settings from dune-project and dune files to ocamlc/ocamlopt by appending them to the build command.1-5 can refer to different diagnostics in different compiler versions.Per-warning prefix syntax like -w:+a -w:-5 is supported in modern OCaml releases, while older releases required a single prefix for the entire list after -w. The exact version boundary for per-warning prefixes is not confirmed in the supplied data and should be verified for your toolchain.
Dune's default warning set is applied by Dune itself. Whether it merges with or is overridden by user flags depends on the order Dune constructs the command. In practice user flags placed in a local stanza appear later and win, but this is implementation-dependent and should be verified.
dune build --verbose-json | jq '.[] | select(.target?) | .command[]'
Look for the sequence of -w arguments.
ocamlc -version
Test per-warning prefix support with a minimal file:
ocamlc -w:+1-10 -w:-5 -c test.ml
If warning 5 is suppressed, last-wins behavior is active.
-w string in dune-project or at the library/executable stanza, e.g. (flags (:standard -w +A-4-41-42-44-45-48-70)).-w string in the order you want them applied.One missing diagnostic detail that changes the recommendation: the exact OCaml version and Dune version in use. Per-warning prefix support and Dune's flag ordering changed over time, so confirming ocaml -version and dune --version is required before locking a portable -w specification.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.