Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a default alias #926

Merged
4 commits merged into from Jul 1, 2018
Merged

Add a default alias #926

4 commits merged into from Jul 1, 2018

Conversation

ghost
Copy link

@ghost ghost commented Jun 28, 2018

This PR adds a special default alias which become the new default target.

Definition

When the default alias is not explictely defined in a directory, it is implicitely defined as follow:

(alias
 (name default)
 (deps (alias_rec install)))

Otherwise the user definition is used.

Default target

The default target is changed from building recursively the alias install to building the default only in the current directory. This allows to redefine what dune build in different directories.

In order to represent that, a new syntax is added to the command line: @@x means building alias x in the current directory. In the end dune build is equivalent to dune build @@default.

dune build -p x still means dune build -p x @install. This is done by adding a command line option --default-target and making -p imply --default-target @install.

Future work

It'd be nice to allow to define what the default target means in any directory of a project. A nice way to do that would be to allow a copy_in_subdirs field to alias stanza that would cause the the alias stanza to be copied in all directories of the project, so that a user may write in the dune-project file:

(alias
 ((name default)
  (copy_in_subdirs)
  (deps (alias_rec install) (alias_rec runtest))))

Implementation details

(alias_rec ...) requires recursive throught the file tree. Since it is now uses for every single directory through the default alias, a bit of laziness was added to Build.t.

@ghost ghost requested review from rgrinberg and emillon June 28, 2018 17:39
@ghost
Copy link
Author

ghost commented Jun 28, 2018

This behavior is only enable for dune BTW. The behavior for jbuilder is unchanged.

@ghost ghost added this to the 1.0.0 milestone Jun 28, 2018
CHANGES.md Outdated
@@ -93,6 +93,13 @@ next

- Make `dev` the default build profile (#920, @diml)

- Add the ability to build an alias non-recursively from the command
line by writing `@@alias` (#926, @diml)
Copy link
Collaborator

Choose a reason for hiding this comment

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

About the syntax: I wonder if instead (or in addition to) this @@alias syntax, we could use a directory-based syntax like @./alias. Maybe that could be extended to other directories to support #771?

Copy link
Author

Choose a reason for hiding this comment

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

You can already write a directory after @ (and @@ in this PR). For instance @foo/runtest means alias runtest recursively from foo and @@foo/runtest means alias runtest only in foo.

match Path.extract_build_context dir with
| None -> (contexts, dir, name)
| Some ("install", _) ->
die "Invalid alias: %s.\n\
Copy link
Collaborator

Choose a reason for hiding this comment

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

Would it be useful to pass a loc to have a nicer error message? (or return a result from here)

Copy link
Author

Choose a reason for hiding this comment

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

This is from the command line, so we don't have a loc for it.

let no_targets_allowed () =
Exn.code_error "No targets allowed under a [Build.lazy_no_targets] \
or [Build.if_file_exists]" []
[@@inline never]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Out of curiosity, what is the purpose of that annotation? is it just an optimization or is it required?

Copy link
Author

Choose a reason for hiding this comment

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

It's just an optimization, so that the error case code is not inlined in the main function. It's kind of a cold annotation.

let find_dir_specified_on_command_line ~dir ~file_tree =
match File_tree.find_dir file_tree dir with
| None ->
die "From the command line:\n\
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same remark about die - can we inject a loc here?

Copy link
Author

Choose a reason for hiding this comment

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

This is from the command line as well, so we don't have a more precise location either

@emillon
Copy link
Collaborator

emillon commented Jun 29, 2018

Was it possible to define an alias name with a @ in it?

let is_standard = function
| "runtest" | "install" | "doc" | "doc-private" | "lint" -> true
| "runtest" | "install" | "doc" | "doc-private" | "lint" | "default" -> true
Copy link
Member

Choose a reason for hiding this comment

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

is this a breaking change for jbuild projects? IIRC, some users defined their own default aliases. In any case, I'm not sure this will make a big difference.

Copy link
Author

Choose a reason for hiding this comment

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

Not if you use the jbuilder binary, but possibly if you use the dune one. Which seems acceptable to me.

name (Path.to_string_maybe_quoted src_dir)

let default = make "DEFAULT"
@{<error>Error@}: Alias %s is empty.\n\
Copy link
Member

Choose a reason for hiding this comment

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

To print things consistently, let' use %S for the alias name.

Copy link
Author

Choose a reason for hiding this comment

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

updated

@ghost
Copy link
Author

ghost commented Jun 29, 2018

Was it possible to define an alias name with a @ in it?

@emillon yes. It is still possible to refer to it with: @./@toto. We should probably allow the @@ syntax only for dune .

Copy link
Member

@rgrinberg rgrinberg left a comment

Choose a reason for hiding this comment

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

Looks good. I'm a bit curious as to how much benefit that optimization brings us. IIRC, you've asked me to try something similar before and I remember not really gaining much. But I think this construct might be applicable elsewhere as well.

fail fn
else
let fn = Path.append (Path.relative Path.build_dir ctx) fn' in
die "Trying to build alias %s but build context %s doesn't exist.%s"
Copy link
Member

Choose a reason for hiding this comment

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

Is this case possible? I'm actually not sure if we can exercise it.

Copy link
Author

Choose a reason for hiding this comment

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

It can happen if you request an alias in _build explicitely from the command line.

@rgrinberg
Copy link
Member

Oh, and please accept the corrections before merging.

Signed-off-by: Jeremie Dimino <jeremie@dimino.org>
Signed-off-by: Jeremie Dimino <jeremie@dimino.org>
- if "default" is specified by the user explicitely, use this
  definition
- otherwise assume the following definition:

  (alias
   (name default)
   (deps (alias_rec install)))

Signed-off-by: Jeremie Dimino <jeremie@dimino.org>
This is useful for (alias_rec ...) since at definition site we recurse
through all sub-directories. This is especially relevant now that we
have the default alias which defaults to (alias_rec install).

Signed-off-by: Jeremie Dimino <jeremie@dimino.org>
@ghost
Copy link
Author

ghost commented Jul 1, 2018

Maybe this optimization doesn't buy us that much. I just got into the habbit of doing it... Ideally the compiler would do this optimization itself

@ghost ghost merged commit fc9f335 into ocaml:master Jul 1, 2018
@rgrinberg
Copy link
Member

rgrinberg commented Jul 2, 2018 via email

@ghost
Copy link
Author

ghost commented Jul 2, 2018

There is a validation on the command line. We should be able to hit this case by writing something like this in a toplevel dune file:

  (deps (alias ../invalid-build-context/foo))

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.

3 participants