From 71d1a2544ca931a82160ab83e8b5d64646169219 Mon Sep 17 00:00:00 2001 From: Guillaume Petiot Date: Fri, 15 Sep 2023 15:41:12 +0100 Subject: [PATCH 1/4] Display a warning for new enable/disable-outside-detected-project behavior --- doc/manpage_ocamlformat.mld | 18 +++++-- lib/Conf.ml | 10 +--- lib/bin_conf/Bin_conf.ml | 51 +++++++++++++------ .../outside_detected_project.expected | 2 +- 4 files changed, 50 insertions(+), 31 deletions(-) diff --git a/doc/manpage_ocamlformat.mld b/doc/manpage_ocamlformat.mld index 2977fddff3..f23dab98ae 100644 --- a/doc/manpage_ocamlformat.mld +++ b/doc/manpage_ocamlformat.mld @@ -32,6 +32,10 @@ OPTIONS (CODE FORMATTING STYLE) file defined in $XDG_CONFIG_HOME (or in $HOME/.config if $XDG_CONFIG_HOME is undefined) is used. + Warning: in the next release, OcamlFormat will be enabled by default, + the presence of a project root will not be checked unless + [--disable-outside-detected-project] is used. + If the disable option is not set, an .ocamlformat-ignore file specifies files that OCamlFormat should ignore. Each line in an .ocamlformat-ignore file specifies a filename relative to the @@ -470,11 +474,6 @@ OPTIONS (REMOVED) --align-variants-decl=VAL This option has been removed in version 0.22. - --disable-outside-detected-project=VAL - This option has been removed in version 0.22. OCamlFormat is - disabled outside of a detected project by default, to enable the - opposite behavior use `enable-outside-detected-project`. - --doc-comments-val=VAL This option has been removed in version 0.16. If you are using `doc-comments-val=before` in combination with @@ -535,6 +534,15 @@ OPTIONS --disable-conf-files Disable .ocamlformat configuration files. + --disable-outside-detected-project + If no .ocamlformat config files have been detected, disable the + formatting. OCamlFormat is disabled outside of a detected project + by default, to enable the opposite behavior use + --enable-outside-detected-project. Warning: in the next release, + OCamlFormat will be enabled by default, the presence of a project + root will not be checked unless --disable-outside-detected-project + is used. + --doc Parse input as an odoc documentation. diff --git a/lib/Conf.ml b/lib/Conf.ml index 5199751279..e08101e39c 100644 --- a/lib/Conf.ml +++ b/lib/Conf.ml @@ -277,14 +277,6 @@ module V = struct let v0_22 = Version.make ~major:0 ~minor:22 ~patch:None end -let disable_outside_detected_project = - let msg = - "OCamlFormat is disabled outside of a detected project by default, to \ - enable the opposite behavior use `enable-outside-detected-project`." - in - let names = ["disable-outside-detected-project"] in - Decl.removed_option ~names ~since:V.v0_22 ~msg - let profile = let doc = "Select a preset profile which sets $(i,all) options, overriding lower \ @@ -349,7 +341,7 @@ let profile = {conf with profile= elt; fmt_opts= p} ) (fun conf -> conf.profile) -let options = Store.[elt disable_outside_detected_project; elt profile] +let options = Store.[elt profile] (** Options affecting formatting *) module Formatting = struct diff --git a/lib/bin_conf/Bin_conf.ml b/lib/bin_conf/Bin_conf.ml index 05280bf891..0b54df6243 100644 --- a/lib/bin_conf/Bin_conf.ml +++ b/lib/bin_conf/Bin_conf.ml @@ -82,6 +82,10 @@ let info = $(b,ocamlformat) file defined in $(b,\\$XDG_CONFIG_HOME) (or in \ $(b,\\$HOME/.config) if $(b,\\$XDG_CONFIG_HOME) is undefined) is \ used." + ; `P + "$(b,Warning:) in the next release, OcamlFormat will be enabled by \ + default, the presence of a project root will not be checked unless \ + [--disable-outside-detected-project] is used." ; `P "If the $(b,disable) option is not set, an $(b,.ocamlformat-ignore) \ file specifies files that OCamlFormat should ignore. Each line in \ @@ -116,23 +120,38 @@ let enable_outside_detected_project = (List.map File_system.project_root_witness ~f:(fun name -> Format.sprintf "$(b,%s)" name ) ) in - let doc = - Format.sprintf - "Read $(b,.ocamlformat) config files outside the current project when \ - no project root has been detected for the input file. The project \ - root of an input file is taken to be the nearest ancestor directory \ - that contains a %s file. If $(b,.ocamlformat) config files are \ - located in the same directory or parents they are applied, if no \ - $(b,.ocamlformat) file is found then the global configuration \ - defined in $(b,\\$XDG_CONFIG_HOME/.ocamlformat) (or in \ - $(b,\\$HOME/.config/.ocamlformat) if $(b,\\$XDG_CONFIG_HOME) is \ - undefined) is applied." - witness + let enable = + let doc_enable = + Format.sprintf + "Read $(b,.ocamlformat) config files outside the current project \ + when no project root has been detected for the input file. The \ + project root of an input file is taken to be the nearest ancestor \ + directory that contains a %s file. If $(b,.ocamlformat) config \ + files are located in the same directory or parents they are \ + applied, if no $(b,.ocamlformat) file is found then the global \ + configuration defined in $(b,\\$XDG_CONFIG_HOME/.ocamlformat) (or \ + in $(b,\\$HOME/.config/.ocamlformat) if $(b,\\$XDG_CONFIG_HOME) is \ + undefined) is applied." + witness + in + Arg.info ["enable-outside-detected-project"] ~doc:doc_enable ~docs + in + let disable = + let doc_disable = + "If no $(b,.ocamlformat) config files have been detected, disable the \ + formatting. OCamlFormat is disabled outside of a detected project by \ + default, to enable the opposite behavior use \ + $(b,--enable-outside-detected-project).\n\ + $(b,Warning:) in the next release, OCamlFormat will be enabled by \ + default, the presence of a project root will not be checked unless \ + $(b,--disable-outside-detected-project) is used." + in + Arg.info ["disable-outside-detected-project"] ~doc:doc_disable ~docs in Term.( const (fun enable_outside_detected_project conf -> {conf with enable_outside_detected_project} ) - $ Arg.(value & flag & info ["enable-outside-detected-project"] ~doc ~docs) ) + $ Arg.(value & vflag false [(true, enable); (false, disable)]) ) let inplace = let doc = "Format in-place, overwriting input file(s)." in @@ -140,8 +159,6 @@ let inplace = ~set:(fun inplace conf -> {conf with inplace}) Arg.(value & flag & info ["i"; "inplace"] ~doc ~docs) -(* Other Flags *) - let check = let doc = "Check whether the input files already are formatted. Mutually \ @@ -578,7 +595,9 @@ let build_config ~enable_outside_detected_project ~root ~file ~is_stdin = in warn ~loc:(Location.in_file file) "Ocamlformat disabled because [--enable-outside-detected-project] is \ - not set and %s" + not set and %s. In the next release, OcamlFormat will be enabled by \ + default, the presence of a project root will not be checked unless \ + [--disable-outside-detected-project] is used." why ) ; Operational.update conf ~f:(fun f -> {f with disable= {f.disable with v= true}} ) ) diff --git a/test/projects/outside_detected_project.expected b/test/projects/outside_detected_project.expected index 4e005e28f3..2f9d83cbfe 100644 --- a/test/projects/outside_detected_project.expected +++ b/test/projects/outside_detected_project.expected @@ -1,5 +1,5 @@ File "main.ml", line 1: -Warning: Ocamlformat disabled because [--enable-outside-detected-project] is not set and no [.ocamlformat] was found within the project (root: ../outside_detected_project) +Warning: Ocamlformat disabled because [--enable-outside-detected-project] is not set and no [.ocamlformat] was found within the project (root: ../outside_detected_project). In the next release, OcamlFormat will be enabled by default, the presence of a project root will not be checked unless [--disable-outside-detected-project] is used. (* The following code should not be formatted *) type t = { foooooo : string; From b746b425a8760ae6fff0c694ca172201ea5295a7 Mon Sep 17 00:00:00 2001 From: Guillaume Petiot Date: Thu, 30 Nov 2023 10:52:32 +0800 Subject: [PATCH 2/4] Remove warnings --- doc/manpage_ocamlformat.mld | 9 +-------- lib/bin_conf/Bin_conf.ml | 15 ++++----------- test/projects/outside_detected_project.expected | 2 +- 3 files changed, 6 insertions(+), 20 deletions(-) diff --git a/doc/manpage_ocamlformat.mld b/doc/manpage_ocamlformat.mld index f23dab98ae..f0f4cdc0db 100644 --- a/doc/manpage_ocamlformat.mld +++ b/doc/manpage_ocamlformat.mld @@ -32,10 +32,6 @@ OPTIONS (CODE FORMATTING STYLE) file defined in $XDG_CONFIG_HOME (or in $HOME/.config if $XDG_CONFIG_HOME is undefined) is used. - Warning: in the next release, OcamlFormat will be enabled by default, - the presence of a project root will not be checked unless - [--disable-outside-detected-project] is used. - If the disable option is not set, an .ocamlformat-ignore file specifies files that OCamlFormat should ignore. Each line in an .ocamlformat-ignore file specifies a filename relative to the @@ -538,10 +534,7 @@ OPTIONS If no .ocamlformat config files have been detected, disable the formatting. OCamlFormat is disabled outside of a detected project by default, to enable the opposite behavior use - --enable-outside-detected-project. Warning: in the next release, - OCamlFormat will be enabled by default, the presence of a project - root will not be checked unless --disable-outside-detected-project - is used. + --enable-outside-detected-project. --doc Parse input as an odoc documentation. diff --git a/lib/bin_conf/Bin_conf.ml b/lib/bin_conf/Bin_conf.ml index 0b54df6243..26b15c6f7c 100644 --- a/lib/bin_conf/Bin_conf.ml +++ b/lib/bin_conf/Bin_conf.ml @@ -82,10 +82,6 @@ let info = $(b,ocamlformat) file defined in $(b,\\$XDG_CONFIG_HOME) (or in \ $(b,\\$HOME/.config) if $(b,\\$XDG_CONFIG_HOME) is undefined) is \ used." - ; `P - "$(b,Warning:) in the next release, OcamlFormat will be enabled by \ - default, the presence of a project root will not be checked unless \ - [--disable-outside-detected-project] is used." ; `P "If the $(b,disable) option is not set, an $(b,.ocamlformat-ignore) \ file specifies files that OCamlFormat should ignore. Each line in \ @@ -141,10 +137,7 @@ let enable_outside_detected_project = "If no $(b,.ocamlformat) config files have been detected, disable the \ formatting. OCamlFormat is disabled outside of a detected project by \ default, to enable the opposite behavior use \ - $(b,--enable-outside-detected-project).\n\ - $(b,Warning:) in the next release, OCamlFormat will be enabled by \ - default, the presence of a project root will not be checked unless \ - $(b,--disable-outside-detected-project) is used." + $(b,--enable-outside-detected-project)." in Arg.info ["disable-outside-detected-project"] ~doc:doc_disable ~docs in @@ -159,6 +152,8 @@ let inplace = ~set:(fun inplace conf -> {conf with inplace}) Arg.(value & flag & info ["i"; "inplace"] ~doc ~docs) +(* Other Flags *) + let check = let doc = "Check whether the input files already are formatted. Mutually \ @@ -595,9 +590,7 @@ let build_config ~enable_outside_detected_project ~root ~file ~is_stdin = in warn ~loc:(Location.in_file file) "Ocamlformat disabled because [--enable-outside-detected-project] is \ - not set and %s. In the next release, OcamlFormat will be enabled by \ - default, the presence of a project root will not be checked unless \ - [--disable-outside-detected-project] is used." + not set and %s" why ) ; Operational.update conf ~f:(fun f -> {f with disable= {f.disable with v= true}} ) ) diff --git a/test/projects/outside_detected_project.expected b/test/projects/outside_detected_project.expected index 2f9d83cbfe..4e005e28f3 100644 --- a/test/projects/outside_detected_project.expected +++ b/test/projects/outside_detected_project.expected @@ -1,5 +1,5 @@ File "main.ml", line 1: -Warning: Ocamlformat disabled because [--enable-outside-detected-project] is not set and no [.ocamlformat] was found within the project (root: ../outside_detected_project). In the next release, OcamlFormat will be enabled by default, the presence of a project root will not be checked unless [--disable-outside-detected-project] is used. +Warning: Ocamlformat disabled because [--enable-outside-detected-project] is not set and no [.ocamlformat] was found within the project (root: ../outside_detected_project) (* The following code should not be formatted *) type t = { foooooo : string; From faf60270160329c2405f197606ed4181f411450c Mon Sep 17 00:00:00 2001 From: Jules Aguillon Date: Thu, 30 Nov 2023 11:39:52 +0100 Subject: [PATCH 3/4] Add test --- .../disable_outside_detected_project.expected | 7 ++++++ .../dune-project | 1 + .../disable_outside_detected_project/main.ml | 5 +++++ test/projects/dune | 22 +++++++++++++++++++ 4 files changed, 35 insertions(+) create mode 100644 test/projects/disable_outside_detected_project.expected create mode 100644 test/projects/disable_outside_detected_project/dune-project create mode 100644 test/projects/disable_outside_detected_project/main.ml diff --git a/test/projects/disable_outside_detected_project.expected b/test/projects/disable_outside_detected_project.expected new file mode 100644 index 0000000000..84259279e7 --- /dev/null +++ b/test/projects/disable_outside_detected_project.expected @@ -0,0 +1,7 @@ +File "main.ml", line 1: +Warning: Ocamlformat disabled because [--enable-outside-detected-project] is not set and no [.ocamlformat] was found within the project (root: ../disable_outside_detected_project) +(* The following code should not be formatted *) +type t = { + foooooo : string; + baaaaar : Baaaaar.t; +} diff --git a/test/projects/disable_outside_detected_project/dune-project b/test/projects/disable_outside_detected_project/dune-project new file mode 100644 index 0000000000..0636ab6acf --- /dev/null +++ b/test/projects/disable_outside_detected_project/dune-project @@ -0,0 +1 @@ +(lang dune 1.11) diff --git a/test/projects/disable_outside_detected_project/main.ml b/test/projects/disable_outside_detected_project/main.ml new file mode 100644 index 0000000000..8f4b7ca2f4 --- /dev/null +++ b/test/projects/disable_outside_detected_project/main.ml @@ -0,0 +1,5 @@ +(* The following code should not be formatted *) +type t = { + foooooo : string; + baaaaar : Baaaaar.t; +} diff --git a/test/projects/dune b/test/projects/dune index 986f52b6c2..04b905377a 100644 --- a/test/projects/dune +++ b/test/projects/dune @@ -41,6 +41,28 @@ (action (diff outside_detected_project.expected outside_detected_project.output))) +(rule + (deps + disable_outside_detected_project/dune-project + disable_outside_detected_project/main.ml) + (package ocamlformat) + (enabled_if + (<> %{os_type} Win32)) + (action + (with-outputs-to + disable_outside_detected_project.output + (chdir + disable_outside_detected_project + (run ocamlformat --disable-outside-detected-project main.ml))))) + +(rule + (alias runtest) + (package ocamlformat) + (enabled_if + (<> %{os_type} Win32)) + (action + (diff disable_outside_detected_project.expected disable_outside_detected_project.output))) + (rule (deps (source_tree outside_detected_project_with_name)) From 5a7af5111b54831e5f555290c05edf25b524a1f6 Mon Sep 17 00:00:00 2001 From: Jules Aguillon Date: Thu, 30 Nov 2023 11:43:09 +0100 Subject: [PATCH 4/4] Update CHANGES --- CHANGES.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index ad69eae2e5..57abdac4a1 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -8,6 +8,8 @@ profile. This started with version 0.26.0. ### Changed +- Added back the flag `--disable-outside-detected-project` (#2439, @gpetiot) + It was removed in version 0.22. - \* Consistent formatting of comments (#2371, @Julow) - Documentation comments are now formatted by default (#2390, @Julow) Use the option `parse-docstrings = false` to disable.