Skip to content

Add clean subcommand#89

Merged
6 commits merged intoocaml:masterfrom
rdavison:add-clean-subcommand
May 26, 2017
Merged

Add clean subcommand#89
6 commits merged intoocaml:masterfrom
rdavison:add-clean-subcommand

Conversation

@rdavison
Copy link
Copy Markdown
Contributor

Added clean subcommand in reference to issue #80

bin/main.ml Outdated
let doc = "Clean the project." in
let man =
[ `S "DESCRIPTION"
; `P {|Removes files added by jbuilder including _build and .merlin|}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Maybe precise .install as well?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Certainly! Great idea

bin/main.ml Outdated
in
let go common =
set_common common ~targets:[];
let log = Log.create () in
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

We should probably not create the log, since it's stored in _build/log.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good point, I'll take it out.

bin/main.ml Outdated
Future.Scheduler.go ~log
begin
(* Attach this first before we run anything else *)
at_exit (fun () ->
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Why do this in a at_exit handler?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Because there's another at_exit handler which dumps a a trace file called .db

Since the at_exit handlers are processed LIFO, I need this one to get in the stack first.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Ok. In the future there will be a problem with the method used anyway, as computing rules might require running commands. This will be required to support plugins properly.

There is a simpler approach we could use here:

  • delete the _build directory
  • load the trace and delete the files that are in the source tree. The keys in Build_system.Trace.t are the targets generated since the initial build from a clean tree

Then we don't need to create the build system at all. You can expose a function in Build_system to read the trace and returns all the targets.

@ghost
Copy link
Copy Markdown

ghost commented May 22, 2017

Thanks, can you also add "clean" do doc/jbuild so that the man page is generated?

@rdavison
Copy link
Copy Markdown
Contributor Author

So the only question I have now is, what do we do in the case when the trace file is missing/corrupt? Currently I'm erring on the safe side and just outputting an error message to let the user fix it, but I'm not particularly proud of that solution. Any ideas?

bin/main.ml Outdated
let doc = "Clean the project." in
let man =
[ `S "DESCRIPTION"
; `P {|Removes files added by jbuilder such as _build, .install, and .merlin|}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I'd prefer: "_build, <package>.install and .merlin."

bin/main.ml Outdated
| None ->
Format.eprintf
"@{<error>Error@}: Missing trace file: _build/.db\n\
Hint: Is the _build directory corrupt? Try rebuilding? @."
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

No trace is likely to mean nothing was built yet. We should just ignore it.

bin/main.ml Outdated
| Some trace ->
begin
Hashtbl.iter trace ~f:(fun ~key ~data:(_) -> Path.unlink key);
Path.(rm_rf (append root (of_string "_build")))
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The _build directory could exist even though there is no trace. The removal of _build should be done unconditionally.

trace
end

let load_trace () = Option.some_if (Sys.file_exists Trace.file) (Trace.load ())
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This doesn't really work. Both arguments will be evaluated before Option.some_if is called.


val load_trace
: unit
-> (Path.t, string) Hashtbl.t option
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The hash table doesn't make much sense outside of this module. We should just return the list of keys:

val all_targets_ever_built : unit -> string list

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Should that be string list or Path.t list?

bin/main.ml Outdated
Hint: Is the _build directory corrupt? Try rebuilding? @."
| Some trace ->
begin
Hashtbl.iter trace ~f:(fun ~key ~data:(_) -> Path.unlink key);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Some of the files might not exist anymore, for instance if the user renamed a directory there will still be an entry for the old .merlin file. We need to use Path.unlink_no_error.

@ghost
Copy link
Copy Markdown

ghost commented May 24, 2017

If the trace is missing we can just ignore it. We can't know whether the _build directory was created just for the log or if something really went wrong. We could prevent the trace being corrupted by writing it to a temporary file and renaming it. But it's not specific to this PR, so I'm fine merging it without dealing with this problem.

@ghost
Copy link
Copy Markdown

ghost commented May 26, 2017

Thanks, looks good!

@ghost ghost merged commit 91d03de into ocaml:master May 26, 2017
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant