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

feat: introduce the @pkg-install alias #11046

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

maiste
Copy link
Collaborator

@maiste maiste commented Oct 28, 2024

This PR solves one part of #10949. It introduces a global alias with the source deps as a dependency. When you use it, you will automatically build the source deps of the project. Regarding the PR, I have various questions:

  • Should I update the documentation as it is related to package management? (I know we don't want to "pollute" the documentation with not released part)
  • Is there a better way to declare an alias, or is this way fine? I tried to take inspiration from other aliases.
  • For the test, would there be a cleaner way to check we only installed what we want at each stage. Here, it seems we can't use the printing solution, as we need to install a complete package.

Copy link
Collaborator

@Leonidas-from-XIV Leonidas-from-XIV left a comment

Choose a reason for hiding this comment

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

I am unsure what our policy is about looking into _build in tests, but I have some ideas how to avoid doing it.

$ dune build @pkg-deps
$ ls _build/_private/default/.pkg/
foo
$ ls _build/default/
Copy link
Collaborator

Choose a reason for hiding this comment

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

I was thinking about the way to detect that the build is happening is to maybe enable the console output and have the opam file echo "foo is building".

Copy link
Member

Choose a reason for hiding this comment

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

Why should there be a build happening? This alias is supposed to only download the deps.

Verify that building the `bar.exe` file is working:
$ dune build ./bar.exe
$ ls _build/default/bar.exe
_build/default/bar.exe
Copy link
Collaborator

Choose a reason for hiding this comment

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

And this could then just be replaced by dune exec ./bar.exe?

This has the slight downside that bar.exe could've been built before so maybe building bar.exe should also echo something to stdout?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It would mean to add a rule that depends on the bar.exe no?

Copy link
Collaborator

Choose a reason for hiding this comment

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

dune exec ./bar.exe automatically adds a dependency. Unless I misunderstand your question.

> (depends foo))
> EOF
$ add_mock_repo_if_needed
$ dune pkg lock
Copy link
Member

Choose a reason for hiding this comment

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

Why do you need to involve the solver in this test?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It was the easiest way to write the test. However, it should be possible to write it without using the lock command. I'll rework it.


$ . ./helpers.sh

Create a fake library to install in the target project as a dependency:
Copy link
Member

Choose a reason for hiding this comment

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

I didn't really get why you need a fake library and executable in this test. I see the following as sufficient:

  1. Write a lock directory
  2. Build your new alias
  3. Verify that the sources exist

Everything else seems redundant.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

As we want to build the library and ensure it is installed, you need the fake library to make sure dune build correctly. Otherwise, it will fail when you try to use it as a dependency.

@rgrinberg
Copy link
Member

To be consistent with dune, aliases should be attached to source directories. In particular:

The following should build all the sources in dune.lock

dune build @dune.lock/pkg-deps

The following should build all the sources in the workspace:

dune build @pkg-deps

If you'd like to download the sources for a particular package, maybe you could add an alias like dune build @dune.lock/$pkg-deps

@@ -25,4 +25,5 @@ let check = standard "check"
let install = standard "install"
let ocaml_index = standard "ocaml-index"
let runtest = standard "runtest"
let pkg_deps = standard "pkg-deps"
Copy link
Member

Choose a reason for hiding this comment

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

The point of this aliases is to download the sources? If it is, a more suggestive name would be pkg-sources or even just sources

@maiste
Copy link
Collaborator Author

maiste commented Oct 30, 2024

@rgrinberg, I feel there is a misunderstanding with the purpose of this PR... The goal is to introduce an alias that allows to download and build the sources of a project. In the issue related to it, we discuss two aliases with two different purposes:

  • @pkg-fetch whose role is to download the sources without building them. The use case targetted here is when you know you are going to be offline.
  • @pkg-deps whose role is to download the source and build them. Doing this allow to add a caching layer with Docker when building image. Docker can cache the build dependencies. Hence, when you update your own code, the engine won't try to rebuild the source. This is what this PR is trying to do.

The following should build all the sources in dune.lock
dune build @dune.lock/pkg-deps

I see. Where would be the best part to add the alias? gen_rules.ml? Also, could I take inspiration from runtest? I feel this is pretty related to this mechanism.

If you'd like to download the sources for a particular package, maybe you could add an alias like dune build @dune.lock/$pkg-deps

Isn't it something that should be attached to @pkg-fetch as it seems more relevant?

@rgrinberg
Copy link
Member

Okay, I think got confused by your implementation. It's currently adding the source deps to the alias. If you'd like the package build to included in the alias, you need to add the target directory of the package as a dependency.

@rgrinberg
Copy link
Member

I see. Where would be the best part to add the alias? gen_rules.ml? Also, could I take inspiration from runtest? I feel this is pretty related to this mechanism.

pkg_rules would be the ideal place

Isn't it something that should be attached to @pkg-fetch as it seems more relevant?

Don't think there's any difference between fetching and building here. But if you decide that you don't need per package granularity, that's OK too.

Also, if you want to check that a package has been built, inspecting _build or using of the shell commands in helpers.sh seems like the better bet. Including extra libraries or executables is still unnecessary moving parts. Our tests suffer from being slow, redundant, and rather hard to understand when failing. Less is more here.

@maiste
Copy link
Collaborator Author

maiste commented Nov 6, 2024

@rgrinberg, I tried to follow the advises given during the Dune Dev Meeting. However, there are still some parts I don't understand or misunderstood. I push my broken code to have a support to discuss on (I didn't rewrite the test yet).

As you can see in pkg_rules, I added a new rule that builds the target paths from the package universe and adds them as a dependency to the alias @pkg-install. Then, Dune register this rule as a dependency in gen_rules.ml as it was done for the other aliases. However, I encountered two errors:

  • I remember you told me to attach this rule to the source directory, not the _build directory. Unfortunately, I'm not able to find a way to do it in the current situation. If I try to attach the alias to project.root (not in this version of the code but on my machine), I get an error that I must not do this. What is wrong with the way I have done it here? In this version, it keeps telling me it is not attached:
    Error: Alias "pkg-install" specified on the command line is empty.
    It is not defined in . or any of its descendants.
    How can I attach it to the workspace?
  • My first version was trying to register this rule for all the packages, including the one not using package management. This is fixed by the last tmp commit I pushed.

Otherwise, was it similar to the approach you had in mind?

Signed-off-by: Etienne Marais <[email protected]>
Attached it to the workspace.

Signed-off-by: Etienne Marais <[email protected]>
@maiste maiste changed the title feat: introduce the @pkg-deps alias feat: introduce the @pkg-install alias Nov 12, 2024
Signed-off-by: Etienne Marais <[email protected]>
let pkg_name = Dune_lang.Package_name.to_string pkg in
Path.Build.L.relative
Private_context.t.build_dir
[ Context_name.to_string ctx_name; ".pkg"; pkg_name; "target" ]
Copy link
Member

Choose a reason for hiding this comment

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

You should go through the paths module to get this path

@@ -25,4 +25,5 @@ let check = standard "check"
let install = standard "install"
let ocaml_index = standard "ocaml-index"
let runtest = standard "runtest"
let pkg_install = standard "pkg-install"
Copy link
Member

Choose a reason for hiding this comment

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

It's incorrect to mark this alias as "standard" as it's not defined in every single directory.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

If I don't mark it as standard, it will complain that it is not attached to any folder in the source. What is the best way to attached it to the source if I don't declare it as standard?

@@ -355,6 +368,7 @@ let gen_project_rules =
and+ () = Odoc.gen_project_rules sctx project
and+ () = Odoc_new.gen_project_rules sctx project
and+ () = Ocaml_index.project_rule sctx project
and+ () = gen_project_rule_for_package_alias sctx project
Copy link
Member

Choose a reason for hiding this comment

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

This needs to be done only once per context rather than for every project

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants