diff --git a/.github/workflows/build-others.yml b/.github/workflows/build-others.yml index e449c5b042..5f670b2c4f 100644 --- a/.github/workflows/build-others.yml +++ b/.github/workflows/build-others.yml @@ -35,7 +35,8 @@ jobs: ocaml-compiler: ${{ matrix.ocaml-compiler }} - name: Opam dependencies - run: opam install --deps-only -t . + run: | + opam install -y --deps-only -t ./ocamlformat-lib.opam ./ocamlformat.opam ./ocamlformat-rpc-lib.opam - name: Format run: opam exec -- dune fmt diff --git a/lib/Conf_decl.ml b/lib/Conf_decl.ml index aedceffd27..d9065a9524 100644 --- a/lib/Conf_decl.ml +++ b/lib/Conf_decl.ml @@ -171,7 +171,7 @@ let pp_from_src fs = function | `File loc -> ("file", loc) | `Attribute loc -> ("attribute", loc) in - let fname = Fpath.to_string ~relativize:true (Fpath.v pos_fname) in + let fname = normalized_path_to_string (Fpath.v pos_fname) in Format.fprintf fs " (%s %s:%i)" kind fname pos_lnum | `Env -> Format.fprintf fs " (environment variable)" | `Commandline -> Format.fprintf fs " (command line)" diff --git a/lib/bin_conf/Bin_conf.ml b/lib/bin_conf/Bin_conf.ml index 5c357c4a5a..7d3eb57330 100644 --- a/lib/bin_conf/Bin_conf.ml +++ b/lib/bin_conf/Bin_conf.ml @@ -539,9 +539,15 @@ let update_using_cmdline config = | Ok (`Ok conf_modif) -> conf_modif config | Error _ | Ok (`Version | `Help) -> config +let path_to_absolute f = + let f = + if Fpath.is_rel f then Fpath.append (Fpath.v (Stdlib.Sys.getcwd ())) f + else f + in + Fpath.normalize f + let build_config ~enable_outside_detected_project ~root ~file ~is_stdin = - let vfile = Fpath.v file in - let file_abs = Fpath.(vfile |> to_absolute |> normalize) in + let file_abs = path_to_absolute (Fpath.v file) in let fs = File_system.make ~enable_outside_detected_project ~disable_conf_files:!global_conf.disable_conf_files @@ -579,7 +585,7 @@ let build_config ~enable_outside_detected_project ~root ~file ~is_stdin = | Some root -> Format.sprintf "no [.ocamlformat] was found within the project (root: %s)" - (Fpath.to_string ~relativize:true root) + (normalized_path_to_string root) | None -> "no project root was found" in warn ~loc:(Location.in_file file) @@ -598,7 +604,7 @@ let build_config ~enable_outside_detected_project ~root ~file ~is_stdin = if conf.opr_opts.disable.v then "enabled" else "ignored" in if conf.opr_opts.debug.v then - warn ~loc "%a is %s." Fpath.pp file_abs status ; + warn ~loc "%s is %s." (normalized_path_to_string file_abs) status ; Operational.update conf ~f:(fun f -> {f with disable= {f.disable with v= not f.disable.v}} ) | None -> conf @@ -745,8 +751,7 @@ let validate_action () = let validate () = let root = - Option.map !global_conf.root - ~f:Fpath.(fun x -> v x |> to_absolute |> normalize) + Option.map !global_conf.root ~f:Fpath.(fun x -> path_to_absolute (v x)) in let enable_outside_detected_project = !global_conf.enable_outside_detected_project && Option.is_none root diff --git a/lib/bin_conf/File_system.ml b/lib/bin_conf/File_system.ml index 04c4b82f50..13b4289aee 100644 --- a/lib/bin_conf/File_system.ml +++ b/lib/bin_conf/File_system.ml @@ -11,12 +11,14 @@ let project_root_witness = [".git"; ".hg"; "dune-project"] +let file_exists p = Stdlib.Sys.file_exists (Fpath.to_string p) + let is_project_root ~root dir = match root with | Some root -> Fpath.equal dir root | None -> List.exists project_root_witness ~f:(fun name -> - Fpath.(exists (dir / name)) ) + file_exists Fpath.(dir / name) ) let dot_ocp_indent = ".ocp-indent" @@ -29,7 +31,9 @@ let dot_ocamlformat_enable = ".ocamlformat-enable" type configuration_file = Ocamlformat of Fpath.t | Ocp_indent of Fpath.t let root_ocamlformat_file ~root = - let root = Option.value root ~default:(Fpath.cwd ()) in + let root = + match root with Some p -> p | None -> Fpath.v (Stdlib.Sys.getcwd ()) + in Fpath.(root / dot_ocamlformat) let xdg_config () = @@ -44,7 +48,7 @@ let xdg_config () = match xdg_config_home with | Some xdg_config_home -> let filename = Fpath.(xdg_config_home / "ocamlformat") in - if Fpath.exists filename then Some filename else None + if file_exists filename then Some filename else None | None -> None type t = @@ -69,24 +73,24 @@ let make ~enable_outside_detected_project ~disable_conf_files { fs with ignore_files= (let filename = Fpath.(dir / dot_ocamlformat_ignore) in - if Fpath.exists filename then filename :: fs.ignore_files + if file_exists filename then filename :: fs.ignore_files else fs.ignore_files ) ; enable_files= (let filename = Fpath.(dir / dot_ocamlformat_enable) in - if Fpath.exists filename then filename :: fs.enable_files + if file_exists filename then filename :: fs.enable_files else fs.enable_files ) ; configuration_files= ( if disable_conf_files then [] else let f_1 = Fpath.(dir / dot_ocamlformat) in let files = - if Fpath.exists f_1 then + if file_exists f_1 then Ocamlformat f_1 :: fs.configuration_files else fs.configuration_files in if ocp_indent_config then let f_2 = Fpath.(dir / dot_ocp_indent) in - if Fpath.exists f_2 then Ocp_indent f_2 :: files + if file_exists f_2 then Ocp_indent f_2 :: files else files else files ) } in diff --git a/test/cli/global_configuration.t b/test/cli/global_configuration.t index 33ede5db20..529f4d1896 100644 --- a/test/cli/global_configuration.t +++ b/test/cli/global_configuration.t @@ -1,7 +1,7 @@ The user's global configuration should be used when [--enable-outside-detected-project] is passed. $ mkdir -p root xdg - $ export XDG_CONFIG_HOME=$PWD/xdg + $ export XDG_CONFIG_HOME=../xdg $ echo 'break-cases = vertical' > xdg/ocamlformat $ cd root diff --git a/vendor/ocamlformat-stdlib/fpath_ext.ml b/vendor/ocamlformat-stdlib/fpath_ext.ml deleted file mode 100644 index 1afde26e55..0000000000 --- a/vendor/ocamlformat-stdlib/fpath_ext.ml +++ /dev/null @@ -1,16 +0,0 @@ -include Fpath - -let cwd () = Stdlib.Sys.getcwd () |> v - -let exists p = to_string p |> Stdlib.Sys.file_exists - -let to_absolute file = if is_rel file then append (cwd ()) file else file - -let to_string ?(relativize = false) p = - if relativize then - Base.Option.value_map - (Fpath.relativize ~root:(cwd ()) p) - ~default:(to_string p) ~f:to_string - else to_string p - -let pp fmt p = Format.fprintf fmt "%s" (to_string ~relativize:true p) diff --git a/vendor/ocamlformat-stdlib/fpath_ext.mli b/vendor/ocamlformat-stdlib/fpath_ext.mli deleted file mode 100644 index 8c1f3277e6..0000000000 --- a/vendor/ocamlformat-stdlib/fpath_ext.mli +++ /dev/null @@ -1,17 +0,0 @@ -include module type of Fpath - -val cwd : unit -> t -(** Current working directory. *) - -val exists : t -> bool -(** [exists p] returns whether the given path [p] exists. *) - -val to_absolute : t -> t -(** [to_absolute p] returns [cwd]/[p] if the [p] is relative, otherwise - returns [p]. *) - -val to_string : ?relativize:bool -> t -> string -(** If [relativize] is set to [true] (it is set to [false] by default), the - path is relativized according to the [cwd]. *) - -val pp : Format.formatter -> t -> unit diff --git a/vendor/ocamlformat-stdlib/ocamlformat_stdlib.ml b/vendor/ocamlformat-stdlib/ocamlformat_stdlib.ml index 9e86c77264..8c17a6443a 100644 --- a/vendor/ocamlformat-stdlib/ocamlformat_stdlib.ml +++ b/vendor/ocamlformat-stdlib/ocamlformat_stdlib.ml @@ -1,13 +1,18 @@ include Base include Stdio -module Fpath = Fpath_ext +module Fpath = Fpath module List = List_ext module String = String_ext module Warning = Warning module Format = Stdlib.Format module Filename = Stdlib.Filename +let normalized_path_to_string p = + let cwd = Fpath.v (Stdlib.Sys.getcwd ()) in + let p = match Fpath.relativize ~root:cwd p with Some p -> p | None -> p in + fst (Fpath.split_volume p) ^ String.concat ~sep:"/" (Fpath.segs p) + let ( >> ) f g x = g (f x) let impossible msg = failwith msg diff --git a/vendor/ocamlformat-stdlib/ocamlformat_stdlib.mli b/vendor/ocamlformat-stdlib/ocamlformat_stdlib.mli index fec28646c2..1a4278ba32 100644 --- a/vendor/ocamlformat-stdlib/ocamlformat_stdlib.mli +++ b/vendor/ocamlformat-stdlib/ocamlformat_stdlib.mli @@ -1,12 +1,15 @@ include module type of Base include module type of Stdio -module Fpath = Fpath_ext module List = List_ext module String = String_ext module Warning = Warning module Format = Stdlib.Format module Filename = Stdlib.Filename +val normalized_path_to_string : Fpath.t -> string +(** Render a path relative to the current directory using [/] as + separator on every platforms to ensure reproducible output in tests. *) + val ( >> ) : ('a -> 'b) -> ('b -> 'c) -> 'a -> 'c (** Composition of functions: [(f >> g) x] is exactly equivalent to [g (f (x))]. Left associative. *)