Skip to content

Commit

Permalink
Silently mark packages requiring an unsupported version of opam as un…
Browse files Browse the repository at this point in the history
…available
  • Loading branch information
kit-ty-kate committed Sep 18, 2024
1 parent 6126c2b commit 537d216
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 67 deletions.
1 change: 1 addition & 0 deletions master_changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ users)

## Repository
* Mitigate curl/curl#13845 by falling back from --write-out to --fail if exit code 43 is returned by curl [#6168 @dra27 - fix #6120]
* Silently mark packages requiring an unsupported version of opam as unavailable [#5665 @kit-ty-kate - fix #5631]

## Lock

Expand Down
8 changes: 5 additions & 3 deletions src/format/opamFile.ml
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ module MakeIO (F : IO_Arg) = struct
with
| OpamSystem.File_not_found _ ->
None
| e ->
| Pp.Bad_format _ as e ->
OpamStd.Exn.fatal e;
if OpamFormatConfig.(!r.strict) then
(OpamConsole.error "%s"
Expand Down Expand Up @@ -160,7 +160,7 @@ module MakeIO (F : IO_Arg) = struct

let read_from_f f input =
try f input with
| (Pp.Bad_version _ | Pp.Bad_format _) as e->
| Pp.Bad_format _ as e ->
if OpamFormatConfig.(!r.strict) then
(OpamConsole.error "%s" (Pp.string_of_bad_format e);
OpamConsole.error_and_exit `File_error "Strict mode: aborting")
Expand Down Expand Up @@ -1181,7 +1181,9 @@ module SyntaxFile(X: SyntaxFileArg) : IO_FILE with type t := X.t = struct
{pelem = Section {section_kind = {pelem = "#"; _}; _}; pos}]; _}
when OpamVersion.(compare (nopatch (of_string ver))
(nopatch OpamVersion.current)) <= 0 ->
raise (OpamPp.Bad_version (Some pos, "Parse error"))
raise
(OpamPp.Bad_version ((Some pos, "Parse error"),
Some (OpamVersion.of_string ver)))
| opamfile -> opamfile

let of_channel filename (ic:in_channel) =
Expand Down
22 changes: 11 additions & 11 deletions src/format/opamPp.ml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ open OpamStd.Op

type bad_format = pos option * string

exception Bad_version of bad_format
exception Bad_version of bad_format * OpamVersion.t option
exception Bad_format of bad_format
exception Bad_format_list of bad_format list

Expand All @@ -25,35 +25,35 @@ let bad_format ?pos fmt =
raise (Bad_format (pos,str)))
fmt

let bad_version ?pos fmt =
let bad_version v ?pos fmt =
Printf.ksprintf
(fun str ->
raise (Bad_version (pos,str)))
raise (Bad_version ((pos,str), v)))
fmt

let add_pos pos = function
| Bad_format (pos_opt,msg) as e ->
if pos_opt = None || pos_opt = Some pos_null
then Bad_format (Some pos, msg)
else e
| Bad_version (pos_opt,msg) as e ->
| Bad_version ((pos_opt,msg),v) as e ->
if pos_opt = None || pos_opt = Some pos_null
then Bad_version (Some pos, msg)
then Bad_version ((Some pos, msg),v)
else e

| e -> e

let rec string_of_bad_format ?file e =
match e, file with
| Bad_version (None, msg), Some filename
| Bad_version ((None, msg), _), Some filename
| Bad_format (None, msg), Some filename
| Bad_version (Some {filename; start = -1, -1 ; stop = -1,-1 }, msg), _
| Bad_version ((Some {filename; start = -1, -1 ; stop = -1,-1 }, msg), _), _
| Bad_format (Some {filename; start = -1, -1 ; stop = -1,-1 }, msg), _ ->
Printf.sprintf "In %s:\n%s" filename msg
| Bad_version (Some pos, msg), _
| Bad_version ((Some pos, msg), _), _
| Bad_format (Some pos, msg), _ ->
Printf.sprintf "At %s:\n%s" (string_of_pos pos) msg
| Bad_version (None, msg), None
| Bad_version ((None, msg), _), None
| Bad_format (None, msg), None ->
Printf.sprintf "Input error:\n%s" msg
| Bad_format_list bfl, _ ->
Expand Down Expand Up @@ -137,13 +137,13 @@ let ignore = {
name_constr = (fun _ -> "<ignored>");
}

let check ?name ?(raise=bad_format) ?errmsg f =
let check ?name ?(raise=fun _ ?pos fmt -> bad_format ?pos fmt) ?errmsg f =
pp
?name
(fun ~pos x ->
if not (f x) then
match errmsg with
| Some m -> raise ~pos "%s" m
| Some m -> raise x ~pos "%s" m
| None -> unexpected ()
else x)
(fun x ->
Expand Down
6 changes: 3 additions & 3 deletions src/format/opamPp.mli
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ type bad_format = pos option * string
input does not have the right format. *)
exception Bad_format of bad_format
exception Bad_format_list of bad_format list
exception Bad_version of bad_format
exception Bad_version of bad_format * OpamVersion.t option

(** Raise [Bad_format]. *)
val bad_format: ?pos:pos -> ('a, unit, string, 'b) format4 -> 'a

(** Raise [Bad_version]. *)
val bad_version: ?pos:pos -> ('a, unit, string, 'b) format4 -> 'a
val bad_version: OpamVersion.t option -> ?pos:pos -> ('a, unit, string, 'b) format4 -> 'a

val string_of_bad_format: ?file:string -> exn -> string

Expand Down Expand Up @@ -101,7 +101,7 @@ val ignore : ('a, 'b option) t
[Bad_format]. *)
val check :
?name:string ->
?raise:(?pos:pos -> (string -> 'a, unit, string, 'a) format4
?raise:('a -> ?pos:pos -> (string -> 'a, unit, string, 'a) format4
-> string -> 'a) ->
?errmsg:string -> ('a -> bool) -> ('a, 'a) t

Expand Down
19 changes: 18 additions & 1 deletion src/state/opamFileTools.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1176,7 +1176,7 @@ let lint_gen ?check_extra_files ?check_upstream ?(handle_dirname=false)
[0, `Error, "File does not exist"], None
| OpamLexer.Error _ | Parsing.Parse_error ->
[1, `Error, "File does not parse"], None
| OpamPp.Bad_version bf | OpamPp.Bad_format bf -> [warn_of_bad_format bf], None
| OpamPp.Bad_version (bf, _) | OpamPp.Bad_format bf -> [warn_of_bad_format bf], None
| OpamPp.Bad_format_list bfl -> List.map warn_of_bad_format bfl, None
in
let check_extra_files = match check_extra_files with
Expand Down Expand Up @@ -1409,6 +1409,23 @@ let read_opam dir =
(OpamPp.string_of_bad_format (OpamPp.Bad_format (snd err)));
None
| None, None -> None
| exception OpamPp.Bad_version ((_, _errmsg), Some version) ->
let sversion = OpamVersion.to_string version in
let scurrent = OpamVersion.to_string OpamVersion.current_nopatch in
log "opam-version %S unsupported on %s. Added as dummy unavailable package."
sversion (OpamFile.to_string opam_file);
Some
(OpamFile.OPAM.empty
|> OpamFile.OPAM.with_available
(FOp (FIdent ([], OpamVariable.of_string "opam-version", None),
`Geq, FString sversion))
|> OpamFile.OPAM.with_descr_body
(Printf.sprintf
"This package uses opam %s file format which opam %s cannot \
read.\n\n\
In order to install or view information on this package, please \
upgrade your opam installation to at least version %s."
sversion scurrent sversion))

let read_repo_opam ~repo_name ~repo_root dir =
let open OpamStd.Option.Op in
Expand Down
45 changes: 30 additions & 15 deletions tests/reftests/pin.test
Original file line number Diff line number Diff line change
Expand Up @@ -269,9 +269,12 @@ Continue anyway? [y/n] y
opam-version: "2.1"
### opam install ./pin-at-two-one
[ERROR] In ${BASEDIR}/pin-at-two-one/pin-at-two-one.opam:
unsupported or missing file format version; should be 2.0 or older
[ERROR] Strict mode: aborting
# Return code 30 #
unsupported or missing file format version; should be 2.0 or older [skipped]

[ERROR] Invalid opam file in pin-at-two-one source from file://${BASEDIR}/pin-at-two-one:
error 2: File format error: unsupported or missing file format version; should be 2.0 or older
[ERROR] No package named pin-at-two-one found.
# Return code 5 #
### OPAMSTRICT=0 opam install ./pin-at-two-one
[ERROR] In ${BASEDIR}/pin-at-two-one/pin-at-two-one.opam:
unsupported or missing file format version; should be 2.0 or older [skipped]
Expand All @@ -284,9 +287,12 @@ opam-version: "2.1"
opam-version: "2.2"
### opam install ./pin-at-two-two
[ERROR] In ${BASEDIR}/pin-at-two-two/pin-at-two-two.opam:
unsupported or missing file format version; should be 2.0 or older
[ERROR] Strict mode: aborting
# Return code 30 #
unsupported or missing file format version; should be 2.0 or older [skipped]

[ERROR] Invalid opam file in pin-at-two-two source from file://${BASEDIR}/pin-at-two-two:
error 2: File format error: unsupported or missing file format version; should be 2.0 or older
[ERROR] No package named pin-at-two-two found.
# Return code 5 #
### OPAMSTRICT=0 opam install ./pin-at-two-two
[ERROR] In ${BASEDIR}/pin-at-two-two/pin-at-two-two.opam:
unsupported or missing file format version; should be 2.0 or older [skipped]
Expand All @@ -299,9 +305,12 @@ opam-version: "2.2"
opam-version: "2.3"
### opam install ./pin-at-two-three
[ERROR] In ${BASEDIR}/pin-at-two-three/pin-at-two-three.opam:
unsupported or missing file format version; should be 2.0 or older
[ERROR] Strict mode: aborting
# Return code 30 #
unsupported or missing file format version; should be 2.0 or older [skipped]

[ERROR] Invalid opam file in pin-at-two-three source from file://${BASEDIR}/pin-at-two-three:
error 2: File format error: unsupported or missing file format version; should be 2.0 or older
[ERROR] No package named pin-at-two-three found.
# Return code 5 #
### OPAMSTRICT=0 opam install ./pin-at-two-three
[ERROR] In ${BASEDIR}/pin-at-two-three/pin-at-two-three.opam:
unsupported or missing file format version; should be 2.0 or older [skipped]
Expand All @@ -314,9 +323,12 @@ opam-version: "2.3"
opam-version: "50.0"
### opam install ./pin-at-future
[ERROR] In ${BASEDIR}/pin-at-future/pin-at-future.opam:
unsupported or missing file format version; should be 2.0 or older
[ERROR] Strict mode: aborting
# Return code 30 #
unsupported or missing file format version; should be 2.0 or older [skipped]

[ERROR] Invalid opam file in pin-at-future source from file://${BASEDIR}/pin-at-future:
error 2: File format error: unsupported or missing file format version; should be 2.0 or older
[ERROR] No package named pin-at-future found.
# Return code 5 #
### OPAMSTRICT=0 opam install ./pin-at-future
[ERROR] In ${BASEDIR}/pin-at-future/pin-at-future.opam:
unsupported or missing file format version; should be 2.0 or older [skipped]
Expand Down Expand Up @@ -373,9 +385,12 @@ echo GARBAGE>>"$1"
### sh junk.sh pin-at-future/pin-at-future.opam
### opam install ./pin-at-future
[ERROR] In ${BASEDIR}/pin-at-future/pin-at-future.opam:
unsupported or missing file format version; should be 2.0 or older
[ERROR] Strict mode: aborting
# Return code 30 #
unsupported or missing file format version; should be 2.0 or older [skipped]

[ERROR] Invalid opam file in pin-at-future source from file://${BASEDIR}/pin-at-future:
error 2: File format error: unsupported or missing file format version; should be 2.0 or older
[ERROR] No package named pin-at-future found.
# Return code 5 #
### OPAMSTRICT=0 opam install ./pin-at-future
[ERROR] In ${BASEDIR}/pin-at-future/pin-at-future.opam:
unsupported or missing file format version; should be 2.0 or older [skipped]
Expand Down
Loading

0 comments on commit 537d216

Please sign in to comment.