Skip to content

Commit

Permalink
Use hash of directory to disambiguate libraries instead of random ID
Browse files Browse the repository at this point in the history
This has the advantage that the rewriting step is reproducible.
  • Loading branch information
Leonidas-from-XIV committed Jan 31, 2023
1 parent 38aa318 commit 5c87fbc
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 13 deletions.
12 changes: 3 additions & 9 deletions lib/dune_file.ml
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,10 @@ module Packages = struct
module Set = Set.Make (String)
module Map = Map.Make (String)

type t = Random.State.t
type t = Digest.t

let init = Random.State.make_self_init

let random_valid_identifier t =
let int_range ~start ~end_ = Random.State.int t (end_ - start) + start in
let lower_case_letter_range () = int_range ~start:97 ~end_:122 in
(* Seq.init exists only in 4.14+ *)
List.init ~len:8 ~f:(fun _ -> Char.chr (lower_case_letter_range ()))
|> List.to_seq |> String.of_seq
let init disambiguation = Digest.string disambiguation
let random_valid_identifier t = Digest.to_hex t

let random_public_name v original =
let suffix = random_valid_identifier v in
Expand Down
2 changes: 1 addition & 1 deletion lib/dune_file.mli
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ module Packages : sig

type t

val init : unit -> t
val init : string -> t

type 'a rename_result = {
changed : bool;
Expand Down
8 changes: 5 additions & 3 deletions lib/pull.ml
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ let rec pp_sexp ppf = function
let s = String.escaped s in
Fmt.pf ppf {|"%s"|} s)

let postprocess_project ~keep directory =
let postprocess_project ~keep ~disambiguation directory =
let open Result.O in
let is_dune_file path =
let filename = Fpath.filename path in
Ok (String.equal filename "dune")
in
let elements = `Sat is_dune_file in
let dfp = Dune_file.Packages.init () in
let dfp = Dune_file.Packages.init disambiguation in
let renames = Dune_file.Packages.Map.empty in
(* determine files and their mappings first *)
let* files, renames =
Expand Down Expand Up @@ -99,7 +99,9 @@ let pull ?(trim_clone = false) ~global_state ~duniverse_dir src_dep =
Opam.pull_tree ~url ~hashes ~dir:output_dir global_state @@| fun result ->
let* () = result in
let* () = if trim_clone then do_trim_clone output_dir else Ok () in
let* () = postprocess_project ~keep:dune_packages output_dir in
let* () =
postprocess_project ~keep:dune_packages ~disambiguation:dir output_dir
in
Ok ()
else
let error =
Expand Down

0 comments on commit 5c87fbc

Please sign in to comment.