Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions src/client/opamAction.ml
Original file line number Diff line number Diff line change
Expand Up @@ -846,7 +846,8 @@ let remove_package_aux
| Some build_dir -> build_dir
| None ->
let dir = OpamPath.Switch.remove root t.switch nv in
if not (OpamFilename.exists_dir dir) then
if not OpamStateConfig.(!r.dryrun)
&& not (OpamFilename.exists_dir dir) then
OpamFilename.mkdir dir;
dir
in
Expand Down Expand Up @@ -907,18 +908,19 @@ let remove_package_aux

(* Removes build dir and source cache of package if unneeded *)
let cleanup_package_artefacts t nv =
log "Cleaning up artefacts of %a" (slog OpamPackage.to_string) nv;
if OpamStateConfig.(!r.dryrun) then () else
(log "Cleaning up artefacts of %a" (slog OpamPackage.to_string) nv;

let build_dir = OpamPath.Switch.build t.switch_global.root t.switch nv in
if not OpamClientConfig.(!r.keep_build_dir) then OpamFilename.rmdir build_dir;
let remove_dir = OpamPath.Switch.remove t.switch_global.root t.switch nv in
if OpamFilename.exists_dir remove_dir then OpamFilename.rmdir remove_dir;
let dev_dir = OpamSwitchState.source_dir t nv in
if not (OpamPackage.Set.mem nv t.installed) then
(log "Removing the local metadata";
OpamSwitchAction.remove_metadata t (OpamPackage.Set.singleton nv);
if not (OpamPackage.Set.mem nv t.pinned) then
OpamFilename.rmdir dev_dir)
let build_dir = OpamPath.Switch.build t.switch_global.root t.switch nv in
if not OpamClientConfig.(!r.keep_build_dir) then OpamFilename.rmdir build_dir;
let remove_dir = OpamPath.Switch.remove t.switch_global.root t.switch nv in
if OpamFilename.exists_dir remove_dir then OpamFilename.rmdir remove_dir;
let dev_dir = OpamSwitchState.source_dir t nv in
if not (OpamPackage.Set.mem nv t.installed) then
(log "Removing the local metadata";
OpamSwitchAction.remove_metadata t (OpamPackage.Set.singleton nv);
if not (OpamPackage.Set.mem nv t.pinned) then
OpamFilename.rmdir dev_dir))

let sources_needed st g =
PackageActionGraph.fold_vertex (fun act acc ->
Expand Down
11 changes: 7 additions & 4 deletions src/client/opamClient.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2427,10 +2427,13 @@ let remove_t ?ask ~autoremove ~force ?(formula=OpamFormula.Empty) atoms t =
let nv = OpamPackage.max_version candidates (fst atom) in
OpamConsole.note "Forcing removal of (uninstalled) %s"
(OpamPackage.to_string nv);
let d = OpamPath.Switch.remove t.switch_global.root t.switch nv in
OpamFilename.rmdir d;
OpamFilename.mkdir d;
OpamProcess.Job.run (OpamAction.remove_package t nv);
if OpamStateConfig.(!r.dryrun) then
OpamProcess.Job.dry_run (OpamAction.remove_package t nv)
else
(let d = OpamPath.Switch.remove t.switch_global.root t.switch nv in
OpamFilename.rmdir d;
OpamFilename.mkdir d;
OpamProcess.Job.run (OpamAction.remove_package t nv));
OpamAction.cleanup_package_artefacts t nv;
false
with Not_found ->
Expand Down
31 changes: 16 additions & 15 deletions src/client/opamCommands.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3084,21 +3084,22 @@ let switch cli =
end;
`Ok ()
| Some `remove, switches ->
OpamGlobalState.with_ `Lock_write @@ fun gt ->
let _gt =
List.fold_left
(fun gt switch ->
let opam_dir = OpamFilename.Op.(
OpamFilename.Dir.of_string switch / OpamSwitch.external_dirname
) in
if OpamFilename.is_symlink_dir opam_dir then
(OpamFilename.rmdir opam_dir;
gt)
else OpamSwitchCommand.remove gt (OpamSwitch.of_string switch))
gt
switches
in
`Ok ()
if OpamStateConfig.(!r.dryrun) then `Ok () else
OpamGlobalState.with_ `Lock_write @@ fun gt ->
let _gt =
List.fold_left
(fun gt switch ->
let opam_dir = OpamFilename.Op.(
OpamFilename.Dir.of_string switch / OpamSwitch.external_dirname
) in
if OpamFilename.is_symlink_dir opam_dir then
(OpamFilename.rmdir opam_dir;
gt)
else OpamSwitchCommand.remove gt (OpamSwitch.of_string switch))
gt
switches
in
`Ok ()
| Some `reinstall, switch ->
let switch = match switch with
| [sw] -> OpamSwitch.of_string sw
Expand Down
26 changes: 19 additions & 7 deletions src/client/opamPinCommand.ml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,12 @@ exception Fetch_Fail of string

let get_source_definition ?version ?subpath ?locked st nv url =
let root = st.switch_global.root in
let srcdir = OpamPath.Switch.pinned_package root st.switch nv.name in
let srcdir =
if OpamStateConfig.(!r.dryrun) then
OpamFilename.mk_tmp_dir ()

@rjbou rjbou Sep 4, 2025

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i didn't check if all these temp dir created are well cleaned

else
OpamPath.Switch.pinned_package root st.switch nv.name
in
let fix opam =
OpamFile.OPAM.with_url url @@
(match version with
Expand Down Expand Up @@ -374,7 +379,12 @@ let fetch_all_pins st ?working_dir pins =
let command pinned =
let { pinned_name = name; pinned_url = url;
pinned_subpath = subpath; _ } = pinned in
let srcdir = OpamPath.Switch.pinned_package root st.switch name in
let srcdir =
if OpamStateConfig.(!r.dryrun) then
OpamFilename.mk_tmp_dir ()
else
OpamPath.Switch.pinned_package root st.switch name
in
let name = OpamPackage.Name.to_string name in
OpamProcess.Job.Op.(
OpamRepository.pull_tree ~cache_dir ?subpath ?working_dir
Expand Down Expand Up @@ -620,6 +630,7 @@ and source_pin

let opam = copy_files st opam in

if not OpamStateConfig.(!r.dryrun) then
OpamFile.OPAM.write_with_preserved_format
?format_from:(OpamPinned.orig_opam_file st name opam)
(OpamPath.Switch.Overlay.opam st.switch_global.root st.switch nv.name)
Expand Down Expand Up @@ -699,11 +710,12 @@ let unpin st names =
log "unpin %a"
(slog @@ OpamStd.List.concat_map " " OpamPackage.Name.to_string) names;
List.fold_left (fun st name ->
OpamFilename.rmdir
(OpamPath.Switch.pinned_package st.switch_global.root st.switch name);
OpamFilename.rmdir
(OpamPath.Switch.Overlay.package
st.switch_global.root st.switch name);
if not OpamStateConfig.(!r.dryrun) then
(OpamFilename.rmdir
(OpamPath.Switch.pinned_package st.switch_global.root st.switch name);
OpamFilename.rmdir
(OpamPath.Switch.Overlay.package
st.switch_global.root st.switch name));
match OpamPinned.package_opt st name with
| Some nv ->
let pin_str =
Expand Down
63 changes: 34 additions & 29 deletions src/client/opamSolution.ml
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,8 @@ let parallel_apply t
try true, OpamPackage.Map.find nv inplace
with Not_found ->
let dir = OpamPath.Switch.build t.switch_global.root t.switch nv in
if not OpamClientConfig.(!r.reuse_build_dir) then
if not OpamClientConfig.(!r.reuse_build_dir)
&& not OpamStateConfig.(!r.dryrun) then
OpamFilename.rmdir dir;
false, dir
in
Expand All @@ -659,10 +660,12 @@ let parallel_apply t
OpamStateConfig.(!r.dev_setup) && found
in
let source_dir = source_dir nv in
(if OpamFilename.exists_dir source_dir
(if OpamStateConfig.(!r.dryrun) then () else
if OpamFilename.exists_dir source_dir
then (if not is_inplace then
OpamFilename.copy_dir ~src:source_dir ~dst:build_dir)
else OpamFilename.mkdir build_dir;
else
OpamFilename.mkdir build_dir;
OpamAction.prepare_package_source t nv build_dir @@+ function
| Some exn -> store_time (); Done (`Exception exn)
| None ->
Expand All @@ -688,7 +691,8 @@ let parallel_apply t
store_time ();
Done (`Exception exn))
| `Remove nv ->
(if OpamAction.removal_needs_download t nv then
(if not OpamStateConfig.(!r.dryrun)
&& OpamAction.removal_needs_download t nv then
let d = OpamPath.Switch.remove t.switch_global.root t.switch nv in
OpamFilename.rmdir d;
let source_dir = source_dir nv in
Expand Down Expand Up @@ -823,31 +827,32 @@ let parallel_apply t
(* 2/ Display errors and finalize *)

let save_installed_cache failed =
OpamSwitchState.Installed_cache.save
(OpamPath.Switch.installed_opams_cache t.switch_global.root t.switch)
(OpamPackage.Set.fold (fun nv opams ->
(* NOTE: We need to know whether an action was successful
or not to know which version of the opam file to store
in the case: the previous one if it failed, or the new
one if it succeeded. *)
let pkg_failed =
List.exists (function
| `Fetch ps -> List.for_all (OpamPackage.equal nv) ps
| `Build p | `Change (_, _, p) | `Install p
| `Reinstall p | `Remove p -> OpamPackage.equal nv p)
failed
in
let add_to_opams opam =
let opam = OpamFile.OPAM.with_metadata_dir None opam in
OpamPackage.Map.add nv opam opams
in
if pkg_failed then
match OpamPackage.Map.find_opt nv t.installed_opams with
| None -> opams
| Some opam -> add_to_opams opam
else
add_to_opams (OpamSwitchState.opam t nv))
t.installed OpamPackage.Map.empty);
if not OpamStateConfig.(!r.dryrun) then
OpamSwitchState.Installed_cache.save
(OpamPath.Switch.installed_opams_cache t.switch_global.root t.switch)
(OpamPackage.Set.fold (fun nv opams ->
(* NOTE: We need to know whether an action was successful
or not to know which version of the opam file to store
in the case: the previous one if it failed, or the new
one if it succeeded. *)
let pkg_failed =
List.exists (function
| `Fetch ps -> List.for_all (OpamPackage.equal nv) ps
| `Build p | `Change (_, _, p) | `Install p
| `Reinstall p | `Remove p -> OpamPackage.equal nv p)
failed
in
let add_to_opams opam =
let opam = OpamFile.OPAM.with_metadata_dir None opam in
OpamPackage.Map.add nv opam opams
in
if pkg_failed then
match OpamPackage.Map.find_opt nv t.installed_opams with
| None -> opams
| Some opam -> add_to_opams opam
else
add_to_opams (OpamSwitchState.opam t nv))
t.installed OpamPackage.Map.empty);
in
begin match action_results with
| `Exception _ | `Error Aborted -> ()
Expand Down
3 changes: 2 additions & 1 deletion src/state/opamSwitchAction.ml
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ let add_to_reinstall st ~unpinned_only packages =
let reinstall =
current_reinstall ++ add_reinst_packages
in
if OpamPackage.Set.equal current_reinstall reinstall then ()
if OpamStateConfig.(!r.dryrun)
|| OpamPackage.Set.equal current_reinstall reinstall then ()
else if OpamPackage.Set.is_empty reinstall then
OpamFilename.remove (OpamFile.filename reinstall_file)
else
Expand Down
4 changes: 2 additions & 2 deletions src/state/opamSwitchState.ml
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ let load lock_kind gt rt switch =
let switch_config =
{switch_config with invariant = Some invariant; opam_version}
in
if lock_kind = `Lock_write then
if lock_kind = `Lock_write && not OpamStateConfig.(!r.dryrun) then
OpamFile.Switch_config.write
(OpamPath.Switch.switch_config gt.root switch)
switch_config;
Expand Down Expand Up @@ -1285,7 +1285,7 @@ let update_pin nv opam st =
{ st with sys_packages; available_packages }

let do_backup lock st = match lock with
| `Lock_write ->
| `Lock_write when not OpamStateConfig.(!r.dryrun) ->
let file = OpamPath.Switch.backup st.switch_global.root st.switch in
let previous_selections = selections st in
OpamFile.SwitchSelections.write file previous_selections;
Expand Down
64 changes: 41 additions & 23 deletions src/state/opamUpdate.ml
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,24 @@ let pinned_package st ?version ?(autolock=false) ?(working_dir=false) name =
Filename.dir_sep ^ OpamFilename.SubPath.to_string sp) subpath))
@@+ fun () ->
(* Do the update *)
let srcdir0 = srcdir in
let srcdir, srcdir_find =
if OpamStateConfig.(!r.dryrun) then
let tmpdir = OpamFilename.mk_tmp_dir () in
tmpdir, OpamFilename.SubPath.(tmpdir /? subpath)
else
srcdir, srcdir_find
in
fetch_dev_package urlf srcdir ~working_dir ?subpath nv @@+ fun result ->
let result =
match OpamStateConfig.(!r.dryrun), result with
| true, Result () ->
let base f = OpamFilename.Base.of_string (OpamFilename.Dir.to_string f) in
(match OpamRepositoryBackend.get_diff (OpamFilename.Dir.of_string "/") (base srcdir) (base srcdir0) with

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

temporary hack, get_diff needs to change arguments and take absolute path, or have 2 functions.

| Some _ -> Result ()
| None -> Up_to_date ())
| _, (Result () | Up_to_date () | Not_available _) -> result
in
let new_source_opam =
OpamPinned.find_opam_file_in_source ?locked name srcdir_find
>>= fun (f, lock) ->
Expand Down Expand Up @@ -332,16 +349,6 @@ let pinned_package st ?version ?(autolock=false) ?(working_dir=false) name =
| Some a, Some b -> not (equal_opam a b)
in
let save_overlay opam =
OpamFilename.mkdir overlay_dir;
let opam_file = OpamPath.Switch.Overlay.opam root st.switch name in
List.iter OpamFilename.remove
OpamPath.Switch.Overlay.([
OpamFile.filename opam_file;
OpamFile.filename (url root st.switch name);
OpamFile.filename (descr root st.switch name);
]);
let files_dir = OpamPath.Switch.Overlay.files root st.switch name in
OpamFilename.rmdir files_dir;
let opam =
OpamFile.OPAM.with_url urlf @@
OpamFile.OPAM.with_name name opam
Expand All @@ -351,19 +358,30 @@ let pinned_package st ?version ?(autolock=false) ?(working_dir=false) name =
then OpamFile.OPAM.with_version version opam
else opam
in
List.iter (fun (rel_file, content, hash) ->
match content with
| Some (lazy content) when OpamHash.check_string content hash ->
OpamFilename.write (OpamFilename.create files_dir rel_file) content
| None | Some _ ->
OpamConsole.warning "Ignoring file %s with invalid hash"
(OpamFilename.Base.to_string rel_file))
(OpamFile.OPAM.get_extra_files
~get_repo_files:(OpamRepositoryState.get_repo_files st.switch_repos)
opam);
OpamFile.OPAM.write opam_file
(OpamFile.OPAM.with_extra_files_opt None opam);
opam
if OpamStateConfig.(!r.dryrun) then opam else
(OpamFilename.mkdir overlay_dir;
let opam_file = OpamPath.Switch.Overlay.opam root st.switch name in
List.iter OpamFilename.remove
OpamPath.Switch.Overlay.([
OpamFile.filename opam_file;
OpamFile.filename (url root st.switch name);
OpamFile.filename (descr root st.switch name);
]);
let files_dir = OpamPath.Switch.Overlay.files root st.switch name in
OpamFilename.rmdir files_dir;
List.iter (fun (rel_file, content, hash) ->
match content with
| Some (lazy content) when OpamHash.check_string content hash ->
OpamFilename.write (OpamFilename.create files_dir rel_file) content
| None | Some _ ->
OpamConsole.warning "Ignoring file %s with invalid hash"
(OpamFilename.Base.to_string rel_file))
(OpamFile.OPAM.get_extra_files
~get_repo_files:(OpamRepositoryState.get_repo_files st.switch_repos)
opam);
OpamFile.OPAM.write opam_file
(OpamFile.OPAM.with_extra_files_opt None opam);
opam)
in
match result, new_source_opam with
| Result _, Some new_opam
Expand Down
Loading
Loading