Skip to content

Commit a906b5d

Browse files
authored
Fix --enable-outside-detected-project (#1156)
* Add tests for --enable-outside-detected-project * Fix --enable-outside-detected-project and --root options no longer working Since 45f324, parsing of option is done later in that file, after those options are checked at toplevel.
1 parent 785726f commit a906b5d

9 files changed

Lines changed: 92 additions & 22 deletions

File tree

CHANGES.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
+ Replace pre_break and if_newline by cbreak (#1090) (Guillaume Petiot)
2020
+ Use opt and fmt_opt to simplify formatting (#1150) (Guillaume Petiot)
2121
+ Replace inplace formatting by dune staging for make fmt (#1151) (Guillaume Petiot)
22-
+ Refactor code that interprets CLI options (#1127) (Jules Aguillon)
22+
+ Refactor code that interprets CLI options (#1127, #1156) (Jules Aguillon)
2323
+ Testing interactions between break-infix and break-infix-before-func (#1136) (Guillaume Petiot)
2424
+ Add dune to repositories checked for regressions (#1129) (Jules Aguillon)
2525
+ Use an expect test for cli tests (#1126) (Etienne Millon)

lib/Conf.ml

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1753,12 +1753,6 @@ let ocp_indent_janestreet_profile =
17531753
; ("align_ops", "true")
17541754
; ("align_params", "always") ]
17551755

1756-
let root =
1757-
Option.map !root ~f:Fpath.(fun x -> v x |> to_absolute |> normalize)
1758-
1759-
let enable_outside_detected_project =
1760-
!enable_outside_detected_project && Option.is_none root
1761-
17621756
let parse_line config ~from s =
17631757
let update ~config ~from ~name ~value =
17641758
let name = String.strip name in
@@ -1828,7 +1822,7 @@ let parse_line config ~from s =
18281822
| name -> update ~config ~from ~name ~value:"true" )
18291823
| _ -> Error (`Malformed s)
18301824

1831-
let is_project_root dir =
1825+
let is_project_root ~root dir =
18321826
match root with
18331827
| Some root -> Fpath.equal dir root
18341828
| None ->
@@ -1843,11 +1837,13 @@ let dot_ocamlformat_ignore = ".ocamlformat-ignore"
18431837

18441838
let dot_ocamlformat_enable = ".ocamlformat-enable"
18451839

1846-
let rec collect_files ~segs ~ignores ~enables ~files =
1840+
let rec collect_files ~enable_outside_detected_project ~root ~segs ~ignores
1841+
~enables ~files =
18471842
match segs with
18481843
| [] | [""] -> (ignores, enables, files, None)
18491844
| "" :: upper_segs ->
1850-
collect_files ~segs:upper_segs ~ignores ~enables ~files
1845+
collect_files ~enable_outside_detected_project ~root ~segs:upper_segs
1846+
~ignores ~enables ~files
18511847
| _ :: upper_segs ->
18521848
let sep = Fpath.dir_sep in
18531849
let dir = String.concat ~sep (List.rev segs) |> Fpath.v in
@@ -1867,9 +1863,11 @@ let rec collect_files ~segs ~ignores ~enables ~files =
18671863
let f_2 = Fpath.(dir / dot_ocp_indent) in
18681864
if Fpath.exists f_2 then `Ocp_indent f_2 :: files else files
18691865
in
1870-
if is_project_root dir && not enable_outside_detected_project then
1871-
(ignores, enables, files, Some dir)
1872-
else collect_files ~segs:upper_segs ~ignores ~enables ~files
1866+
if is_project_root ~root dir && not enable_outside_detected_project
1867+
then (ignores, enables, files, Some dir)
1868+
else
1869+
collect_files ~enable_outside_detected_project ~root ~segs:upper_segs
1870+
~ignores ~enables ~files
18731871

18741872
let read_config_file conf filename_kind =
18751873
match filename_kind with
@@ -1985,13 +1983,14 @@ let is_in_listing_file ~listings ~filename =
19851983
warn "ignoring %a, %s" Fpath.pp listing_file err ;
19861984
None)
19871985

1988-
let build_config ~file ~is_stdin =
1986+
let build_config ~enable_outside_detected_project ~root ~file ~is_stdin =
19891987
let vfile = Fpath.v file in
19901988
let file_abs = Fpath.(vfile |> to_absolute |> normalize) in
19911989
let dir = Fpath.(file_abs |> split_base |> fst) in
19921990
let segs = Fpath.segs dir |> List.rev in
19931991
let ignores, enables, files, project_root =
1994-
collect_files ~segs ~ignores:[] ~enables:[] ~files:[]
1992+
collect_files ~enable_outside_detected_project ~root ~segs ~ignores:[]
1993+
~enables:[] ~files:[]
19951994
in
19961995
let files =
19971996
match (xdg_config, enable_outside_detected_project) with
@@ -2036,9 +2035,10 @@ let build_config ~file ~is_stdin =
20362035
{conf with disable= not conf.disable}
20372036
| None -> conf
20382037

2039-
let build_config ~file ~is_stdin =
2038+
let build_config ~enable_outside_detected_project ~root ~file ~is_stdin =
20402039
let conf, warn_now =
2041-
collect_warnings (fun () -> build_config ~file ~is_stdin)
2040+
collect_warnings (fun () ->
2041+
build_config ~enable_outside_detected_project ~root ~file ~is_stdin)
20422042
in
20432043
if not conf.quiet then warn_now () ;
20442044
conf
@@ -2107,14 +2107,21 @@ type action =
21072107
| Check of [`Impl | `Intf] input list
21082108
| Print_config of t
21092109

2110-
let make_action action inputs =
2110+
let make_action ~enable_outside_detected_project ~root action inputs =
21112111
let make_file ?(with_conf = fun c -> c) ?name kind file =
2112-
let conf = with_conf (build_config ~file ~is_stdin:false) in
2112+
let conf =
2113+
with_conf
2114+
(build_config ~enable_outside_detected_project ~root ~file
2115+
~is_stdin:false)
2116+
in
21132117
let name = Option.value ~default:file name in
21142118
{kind; name; file= File file; conf}
21152119
in
21162120
let make_stdin ?(name = "<standard input>") kind =
2117-
let conf = build_config ~file:name ~is_stdin:false in
2121+
let conf =
2122+
build_config ~enable_outside_detected_project ~root ~file:name
2123+
~is_stdin:false
2124+
in
21182125
{kind; name; file= Stdin; conf}
21192126
in
21202127
match (action, inputs) with
@@ -2128,7 +2135,9 @@ let make_action action inputs =
21282135
let root = Option.value root ~default:(Fpath.cwd ()) in
21292136
(Fpath.(root / dot_ocamlformat |> to_string), true)
21302137
in
2131-
let conf = build_config ~file ~is_stdin in
2138+
let conf =
2139+
build_config ~enable_outside_detected_project ~root ~file ~is_stdin
2140+
in
21322141
Ok (Print_config conf)
21332142
| (`No_action | `Output _ | `Inplace | `Check), `No_input ->
21342143
Error "Must specify at least one input file, or `-` for stdin"
@@ -2159,6 +2168,12 @@ let make_action action inputs =
21592168
| `Check, `Stdin (name, kind) -> Ok (Check [make_stdin ?name kind])
21602169
21612170
let validate () =
2171+
let root =
2172+
Option.map !root ~f:Fpath.(fun x -> v x |> to_absolute |> normalize)
2173+
in
2174+
let enable_outside_detected_project =
2175+
!enable_outside_detected_project && Option.is_none root
2176+
in
21622177
if !disable_outside_detected_project then
21632178
warn
21642179
"option `--disable-outside-detected-project` is deprecated and will \
@@ -2167,7 +2182,9 @@ let validate () =
21672182
let open Result in
21682183
validate_action ()
21692184
>>= fun action ->
2170-
validate_inputs () >>= fun inputs -> make_action action inputs
2185+
validate_inputs ()
2186+
>>= fun inputs ->
2187+
make_action ~enable_outside_detected_project ~root action inputs
21712188
with
21722189
| Error e -> `Error (false, e)
21732190
| Ok action -> `Ok action

test/projects/dune

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
(rule
2+
(targets enable_outside_detected_project.output)
3+
(deps enable_outside_detected_project/dune-project
4+
enable_outside_detected_project/main.ml)
5+
(action
6+
(with-stdout-to
7+
%{targets}
8+
(chdir
9+
enable_outside_detected_project
10+
(run ocamlformat --enable-outside-detected-project main.ml)))))
11+
12+
(alias
13+
(name runtest)
14+
(action
15+
(diff enable_outside_detected_project.expected
16+
enable_outside_detected_project.output)))
17+
18+
(rule
19+
(targets outside_detected_project.output)
20+
(deps outside_detected_project/dune-project outside_detected_project/main.ml)
21+
(action
22+
(with-outputs-to
23+
%{targets}
24+
(chdir
25+
outside_detected_project
26+
(run ocamlformat main.ml)))))
27+
28+
(alias
29+
(name runtest)
30+
(action
31+
(diff outside_detected_project.expected outside_detected_project.output)))
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
(* The following code should be formatted using OCamlformat's config and not
2+
default *)
3+
type t = {foooooo: string; baaaaar: Baaaaar.t}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
(lang dune 1.11)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
(* The following code should be formatted using OCamlformat's config and not default *)
2+
type t = {
3+
foooooo : string;
4+
baaaaar : Baaaaar.t;
5+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
File main.ml
2+
Warning: Ocamlformat disabled because [--enable-outside-detected-project] is not set and no [.ocamlformat] was found within the project (root: ../outside_detected_project)
3+
(* The following code should not be formatted *)
4+
type t = {
5+
foooooo : string;
6+
baaaaar : Baaaaar.t;
7+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
(lang dune 1.11)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
(* The following code should not be formatted *)
2+
type t = {
3+
foooooo : string;
4+
baaaaar : Baaaaar.t;
5+
}

0 commit comments

Comments
 (0)