From 0e2415755f44f0b747c94411514105dcda045dfe Mon Sep 17 00:00:00 2001 From: Rudi Grinberg Date: Fri, 5 Apr 2019 11:33:26 +0700 Subject: [PATCH] Warn instead of failing on opam files The contents of these files doesn't really matter so we should simply ignore them if we fail to read them Signed-off-by: Rudi Grinberg --- CHANGES.md | 3 +++ src/dune_project.ml | 18 +++++++++++++++--- src/stdune/exn.ml | 5 ++++- src/stdune/exn.mli | 2 ++ 4 files changed, 24 insertions(+), 4 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 797adab75dc..d05d8f8d4f0 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -28,6 +28,9 @@ unreleased - Do not list private modules in the generated index. (#2009, fix #2008, @rgrinberg) +- Warn instead of failing if an opam file fails to parse. This opam file can + still be used to define scope. (#2023, @rgrinberg) + 1.8.2 (10/03/2019) ------------------ diff --git a/src/dune_project.ml b/src/dune_project.ml index 25db6804b1e..832a3efc660 100644 --- a/src/dune_project.ml +++ b/src/dune_project.ml @@ -612,9 +612,21 @@ let load ~dir ~files = match Filename.split_extension fn with | (pkg, ".opam") when pkg <> "" -> let version_from_opam_file = - let opam = Opam_file.load (Path.relative dir fn) in - match Opam_file.get_field opam "version" with - | Some (String (_, s)) -> Some s + let open Option.O in + let* opam = + let opam_file = Path.relative dir fn in + match Opam_file.load opam_file with + | s -> Some s + | exception exn -> + Errors.warn (Loc.in_file opam_file) + "Unable to read opam file. This package's version field will\ + be ignored.@.Reason: %a@." + Exn.pp exn; + None + in + let* version = Opam_file.get_field opam "version" in + match version with + | String (_, s) -> Some s | _ -> None in let name = Package.Name.of_string pkg in diff --git a/src/stdune/exn.ml b/src/stdune/exn.ml index 5926f248f03..89fc8e3dd58 100644 --- a/src/stdune/exn.ml +++ b/src/stdune/exn.ml @@ -52,7 +52,10 @@ let pp_uncaught ~backtrace fmt exn = | @{Internal error@}: Uncaught exception.\n\ %s\n\ \\%s@." - line s line; + line s line + +let pp fmt exn = + Format.pp_print_string fmt (Printexc.to_string exn) include ((struct diff --git a/src/stdune/exn.mli b/src/stdune/exn.mli index 10abdeed9f2..05a8e658fb8 100644 --- a/src/stdune/exn.mli +++ b/src/stdune/exn.mli @@ -34,6 +34,8 @@ val protectx : 'a -> f:('a -> 'b) -> finally:('a -> unit) -> 'b val pp_uncaught : backtrace:string -> Format.formatter -> t -> unit +val pp : Format.formatter -> t -> unit + val raise_with_backtrace: exn -> Printexc.raw_backtrace -> _ val equal : t -> t -> bool