Skip to content

Commit

Permalink
Presence of .merlin.skip-if-not-cwd skips config in dir
Browse files Browse the repository at this point in the history
Mitigation for ocaml#1869
  • Loading branch information
Jonah Beckford committed Dec 1, 2024
1 parent 2b9cd21 commit 8da537e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 17 deletions.
47 changes: 31 additions & 16 deletions src/kernel/mconfig_dot.ml
Original file line number Diff line number Diff line change
Expand Up @@ -404,25 +404,40 @@ let find_project_context start_dir =
then Some dir
else None
in
let cwd = Sys.getcwd () in
let cwd = Misc.canonicalize_filename ~cwd cwd in

let rec loop workdir dir =
try
Some
(List.find_map [ ".merlin"; "dune-project"; "dune-workspace" ]
~f:(fun f ->
let fname = Filename.concat dir f in
if Sys.file_exists fname && not (Sys.is_directory fname) then
(* When starting [dot-merlin-reader] from [dir]
the workdir is always [dir] *)
let workdir = if f = ".merlin" then None else workdir in
let workdir = Option.value ~default:dir workdir in
Some
( { workdir;
process_dir = dir;
configurator = Option.get (Configurator.of_string_opt f)
},
fname )
else None))
Some (
List.find_map [
".merlin.skip-if-not-cwd";
".merlin"; "dune-project"; "dune-workspace"
]
~f:(fun f ->
let fname = Filename.concat dir f in
if Sys.file_exists fname && not (Sys.is_directory fname)
then (
(* Special case:
1. exists .merlin.skip-if-not-cwd
2. not cwd (aka. `cwd <> dir`) *)
if f = ".merlin.skip-if-not-cwd" then (
if cwd <> Misc.canonicalize_filename ~cwd dir then
raise Not_found
else None)
else
(* When starting [dot-merlin-reader] from [dir]
the workdir is always [dir] *)
let workdir = if f = ".merlin" then None else workdir in
let workdir = Option.value ~default:dir workdir in
Some ({
workdir;
process_dir = dir;
configurator = Option.get (Configurator.of_string_opt f)
}, fname))
else None
)
)
with Not_found ->
let parent = Filename.dirname dir in
if parent <> dir then
Expand Down
10 changes: 9 additions & 1 deletion src/kernel/mconfig_dot.mli
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@ val get_config : context -> string -> config * string list
- dune-project
- dune-workspace
They are detected in that order. [dune] and [jbuild] file do not need to be taken into account because any project using a recent version of dune should have a dune-project file which is even auto-generated when it is missing. And only recent versions of dune will stop writing .merlin files.
They are detected in that order. [dune] and [jbuild] file do not need to
be taken into account because any project using a recent version of dune
should have a dune-project file which is even auto-generated when it is
missing. And only recent versions of dune will stop writing .merlin files.
The presence of the file [".merlin.skip-if-not-cwd"] in a directory means
that the three (3) project configuration files are {b not} checked if the
directory containing [".merlin.skip-if-not-cwd"] is not the current
working directory.
*)
val find_project_context : string -> (context * string) option

0 comments on commit 8da537e

Please sign in to comment.