'How to disable Error (warning 66): unused open! in dune

As per https://github.com/ocaml/ocaml/pull/1110, OCaml 4.08 and later raises a warning for unused module opens, even when they are opened with open!. This creates friction with the common practice of using open! Foo to establish that the following code is to be in the context of module Foo (whether or not any thing is used from inside of Foo). Moreover, since dune treats all warnings as fatal errors by default, this will cause dune builds in the default dev profile to fail with errors like

$ dune build
File "lib/mylib.ml", line 1, characters 0-10:
1 | open! Core
    ^^^^^^^^^^
Error (warning 66): unused open! Core.

How can I disable this warnings and the fatal error?



Solution 1:[1]

The dune FAQ explains how to configure warnings so that they are "non-fatal" (letting the build proceed). However, this will still leave the warnings cluttering your build output. For developers using the open! Foo idiom to establish the context, this is annoying. The best solution is just to selectively disable this warning for the project:

  • Create a dune file at the root of your project
  • Add the following stanza

    (env
      (dev
        (flags (:standard -w -66))))
    

This stanza instructs dune to pass the -w flag with the -66 argument to the OCaml compiler when building under the dev profile. man ocamlc explains how this flag works:

       -w warning-list
          Enable, disable, or mark as fatal the warnings specified by the argument warning-list.
          ...
          -num   Disable warning number num.
          ...
          The warning numbers are as follows.
          ...
          66   Unused open! statement.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1