diff --git a/CHANGES.md b/CHANGES.md index 24d8515f152..3145e6bd2d2 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -46,6 +46,9 @@ Unreleased - Add support for instrumentation dependencies (#4210, fixes #3983, @nojb) +- Workaround incorrect exception raised by Unix.utimes (OCaml PR#8857) in + Path.touch on Windows (#4223, @dra27) + 2.8.2 (21/01/2021) ------------------ diff --git a/src/stdune/path.ml b/src/stdune/path.ml index a5a0f358dfe..eb63e7ef8e0 100644 --- a/src/stdune/path.ml +++ b/src/stdune/path.ml @@ -1176,9 +1176,19 @@ let touch ?(create = true) p = | In_build_dir k -> Kind.to_string (Kind.append_local (Fdecl.get Build.build_dir) k) in - try Unix.utimes p 0.0 0.0 - with Unix.Unix_error (Unix.ENOENT, _, _) -> - if create then Unix.close (Unix.openfile p [ Unix.O_CREAT ] 0o777) + let create = + if create then + fun () -> + Unix.close (Unix.openfile p [ Unix.O_CREAT ] 0o777) + else + Fun.id + in + try Unix.utimes p 0.0 0.0 with + | Unix.Unix_error (Unix.ENOENT, _, _) -> create () + | Unix.Unix_error (Unix.EUNKNOWNERR 0, _, _) + when Sys.win32 && not (Sys.file_exists p) -> + (* OCaml PR#8857 *) + create () let compare x y = match (x, y) with