Skip to content

Add cabal-doctest exe as cabal external command - #424

Closed
wismill wants to merge 1 commit into
sol:mainfrom
wismill:cabal-external-command
Closed

wismill wants to merge 1 commit into
sol:mainfrom
wismill:cabal-external-command

Conversation

@wismill

@wismill wismill commented May 31, 2024 •

Copy link
Copy Markdown

Following the discussion in haskell/cabal#10057, this PR adds the executable cabal-doctest to be use as a cabal external command, like so:

# Equivalent of cabal repl --with-compiler=doctest
# *if* the GHC currently in PATH is the same used to compiled doctest
cabal doctest
# Equivalent of the previous, although not the intended use
CABAL=cabal cabal-doctest
# If we have various doctests compiled with different GHC version, such as `doctest-X.Y`,
# this will run just fine while cabal repl --with-compiler=doctest-X.Y may raise an error
# due to ghc-pkg version mismatch
cabal doctest-9.2
cabal doctest-9.4
…
cabal doctest-9.8

Fixes #396

@wismill wismill mentioned this pull request May 31, 2024
2 of 5 tasks
@wismill

wismill commented May 31, 2024 •

Copy link
Copy Markdown
Author

We should maybe raise a warning if conflicting options are passed, such as:

# Conflict!  --with-compiler is already set by cabal-doctest and will be overriden
cabal doctest --with-compiler ghc-9.4

@wismill

wismill commented Jul 5, 2024

Copy link
Copy Markdown
Author

@sol cabal-install 3.12 has been release with support for external command.

@sol sol left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Comment thread doctest.cabal
, process
default-language: Haskell2010
if impl(ghc >= 9.8)
ghc-options: -fno-warn-x-partial

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

This is a generated file. Please edit package.yaml instead.

Comment thread driver/CabalWrapper.hs
exitWith exitCode
where
mkArgs exePath opts = baseOpts exePath ++ opts
baseOpts exePath = ["repl", "--with-compiler", exePath, "--with-hc-pkg", ghc_pkg]

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Will Cabal ensure that the project has been built before invoking external commands?

My default assumption is "no", and if that's the case, then I think we still need --with-repl to make this robust.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Or given that we are in control now, maybe we are in a position to first invoke cabal with -w ghc and then with -w doctest.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Will Cabal ensure that the project has been built before invoking external commands?

Yes, please give it a try.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

From what I tried, I think it doesn't.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I just tried again:

  • Delete dist-newstyle (or use --builddir`)
  • Run cabal doctest-9.8 -b QuickCheck
    It does run the tests.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Ok. I do not understand why this is relevant then. Doctest run the tests with success, which seems the only thing that matters in my opinion.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

If you don't understand something, please always feel free to ask.

This is the relevant part from the README:

Technically, cabal build is not necessary. cabal repl --with-ghc=doctest will build any dependencies as needed. However, it's more robust to run cabal build first (specifically it is not a good idea to build ghc-paths with --with-ghc=doctest).

Or in other words, before invoking cabal repl,, you have to run cabal build.

I'm giving this a stab right now, BTW.

@wismill wismill Jul 12, 2024 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

If you don't understand something, please always feel free to ask.

If you want to ask something, please always feel free to give the full context.

All examples I have seen only involved cabal repl, and my current PR just works in my testing. So I believe you if you say it’s more robust, even if I do not know in what way: what can go wrong without cabal build?

If you would have mentioned the note in your first comment it would have been clear and we would have both avoid losing time. This is my first contribution to this project, I do not know all the edge cases and it is not clear where the technical documentation of this one is.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

I do not know in what way: what can go wrong without cabal build?

OK, I think that's the source of the misunderstanding. I mentioned that before here:

haskell/cabal#10057 (comment)

There's still the issue that when doctest acts as a proxy for GHC, then theghc-paths package is not complied correctly (not sure if any other package are affected). For that reason a user should always do a clean build first, before invoking cabal repl --with-ghc=doctest.

That's why I assumed you have the full picture.

That's also the reason why I kept pressing for--with-repl, which would eliminate the problem + I think that's also the reason why Matthew included this line in his script.

If you want to understand for what reason ghc-paths is not compiled correctly with --with-ghc=doctest then study https://github.com/simonmar/ghc-paths/blob/master/Setup.hs#L68.

Let's keep this constructive.

There are actual problems that need solving (e.g. this line, the cabal exec, is there for reason, and I think it makes things complicated, more on that later…).

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

While I really appreciate that you now took time to give me the full picture, I do not feel I can be helpful here anymore.

I missed the important point of your comment when I created this PR 2 months ago, so it was unlikely I would understood the issue without some reference.

I am glad that at least my attempts motivate you to tackle #396.

@wismill

wismill commented Jul 10, 2024 •

Copy link
Copy Markdown
Author

I kind of like the approach @mpickering took here: https://github.com/mpickering/cabal-doctest-demo/blob/master/cabal-doctest#L11-L14

I fail to see how this script allow you to select the GHC version apart than executing before it ghcup set ghc …. Selecting an exact GHC version is the motivation of this PR in order to fix #396.

Or do you mean only the bits to ensure doctest is installed for the specific GHC version? I am not sure this is easily feasible with my current approach.

But I think something like cabal doctest -w ghc-9.10 would be quite ergonomic if it could check for e.g. existing doctest-9.10.1 else install it. The idea would be to first install cabal-doctest from this MR with whatever GHC and without suffix, then:

  • cabal doctest (without -w) runs the cabal-doctest we initially installed.
  • cabal doctest -w ghc-9.10.1 checks for cabal-doctest-9.10.1:
    1. if found, cabal-doctest invokes cabal-doctest-9.10.1 with the same args, except -w.
    2. if not found, run cabal install -z --overwrite-policy=always -w ghc-9.10.1 doctest cabal install -z --overwrite-policy=never -w ghc-NNN --suffix "-XXX" doctest; where NNN is the GHC version suffix (here 9.10.1, but 9.10 would also work in my setting) and the XXX part would be the output of ghc-NNN -v0 --numeric-version. Then go to 1.

Note that the suffix should be the corresponding full GHC numeric version.

Is this something you would be interested to explore further?

I am currently quite busy.

@sol

sol commented Jul 10, 2024

Copy link
Copy Markdown
Owner

I fail to see how this script allow you to select the GHC version apart than executing before it ghcup set ghc ….

I think you could do something like:

ghcup run --ghc 9.10 cabal doctest

Or do you mean only the bits to ensure doctest is installed for the specific GHC version?

I think that makes sense as a starting point.

But I think something like cabal doctest -w ghc-9.10 would be quite ergonomic if it could check for e.g. existing doctest-9.10.1 else install it.

Yes, possibly, but I think it's better to defer that.

First get the base case right, then explore this as a follow up.

The idea would be to first install cabal-doctest from this MR with whatever GHC and without suffix, then:

Yes, exactly 👍

  • cabal doctest (without -w) runs the cabal-doctest we initially installed.

I think it's more robust to always install doctest with the ghc that is on the path.

  • cabal doctest -w ghc-9.10.1 checks for cabal-doctest-9.10.1:
  1. if found, cabal-doctest invokes cabal-doctest-9.10.1 with the same args, except -w.

I would try to directly invoke cabal with doctest-9.10.1. I think that's easier to understand control-flow wise + you don't have to fitter the environment anymore.

You will need to determine ghc-pkg without relying on ghc-paths, but I think that's a solvable problem.

  1. if not found, run cabal install -z --overwrite-policy=always -w ghc-9.10.1 doctest, then go to 1.

I think just always do the install, if it's in the store it's literally just creating a symlink. That simplifies the control flow.

Note that the suffix should be the corresponding full GHC numeric version.

If you always install, this doesn't really matter anymore. You can just use some temporary path.

@wismill

wismill commented Jul 11, 2024 •

Copy link
Copy Markdown
Author

ghcup run --ghc 9.10 cabal doctest

While it would work, it creates a temporary directory and symlink all the toolchain. I would like to save any useless write to my SSD and keep the run fast.

  • cabal doctest (without -w) runs the cabal-doctest we initially installed.

I think it's more robust to always install doctest with the ghc that is on the path.

Fair enough.

I would try to directly invoke cabal with doctest-9.10.1. I think that's easier to understand control-flow wise + you don't have to fitter the environment anymore.

And detect if it fails? It may fail for other reason. No, I think it is much easier to detect if the executable is in path.

You will need to determine ghc-pkg without relying on ghc-paths, but I think that's a solvable problem.

And we are back at the initial issue 😅

I think just always do the install, if it's in the store it's literally just creating a symlink. That simplifies the control flow.

As I wrote above, I would prefer to keep the workflow I propose. But I would change to --overwrite-policy=never to avoid unnecessary writes. The only issue is that cabal would fail if it already exists with exit code 1, which is too generic. I guess we could discard expected “error” by quickly checking for Cabal-7149 in the stderr.

Note that the suffix should be the corresponding full GHC numeric version.

If you always install, this doesn't really matter anymore. You can just use some temporary path.

This is puzzling me. Why would you create a temporary dir at each execution?

In fact I meant to write (fixed):

  1. if not found, run cabal install -z --overwrite-policy=never -w ghc-NNN --suffix "-XXX" doctest; where NNN is the GHC version suffix (here 9.10.1) and the XXX part would be the output of ghc-NNN -v0 --numeric-version. Then go to 1.

@sol

sol commented Jul 11, 2024

Copy link
Copy Markdown
Owner

ghcup run --ghc 9.10 cabal doctest

While it would work, it creates a temporary directory and symlink all the toolchain. I would like to save any useless write to my SSD and keep the run fast.

  1. You can pass --quick to ghcup to make it somewhat faster.
  2. Technically, I think ghcup could just augment the PATH instead. If you think the current approach is "problematic", then please open an issue for ghcup.
  3. Also, as a sidenote, e.g. stack --silent --resolver=ghc-9.10.1 path --compiler-bin can give you a GHC without creating symliknks.
  • cabal doctest (without -w) runs the cabal-doctest we initially installed.

I think it's more robust to always install doctest with the ghc that is on the path.

Fair enough.

I would try to directly invoke cabal with doctest-9.10.1. I think that's easier to understand control-flow wise + you don't have to fitter the environment anymore.

And detect if it fails? It may fail for other reason. No, I think it is much easier to detect if the executable is in path.

You will need to determine ghc-pkg without relying on ghc-paths, but I think that's a solvable problem.

And we are back at the initial issue 😅

Not really. Before we were trying to determine ghc-pkg from doctest. Now we are trying to determine it from ghc, which is trivia. But we can also augment doctest --info with the path of ghc-pkg, which effectively delegates to ghc-paths.

I think just always do the install, if it's in the store it's literally just creating a symlink. That simplifies the control flow.

As I wrote above, I would prefer to keep the workflow I propose. But I would change to --overwrite-policy=never to avoid unnecessary writes. The only issue is that cabal would fail if it already exists with exit code 1, which is too generic. I guess we could discard expected “error” by quickly checking for Cabal-7149 in the stderr.

  1. This makes the code more complicated.
  2. Caching is an optimization step. Early optimization is the root of all…
  3. Technically, cabal could be extended to give you the path to an executable in the store. That would remove the necessity to create symlinks. I think this could be neat, not only for doctest.

Note that the suffix should be the corresponding full GHC numeric version.

If you always install, this doesn't really matter anymore. You can just use some temporary path.

This is puzzling me. Why would you create a temporary dir at each execution?

I think it's better not to modify the user environment.

Also note, that any invocation of ghc (and by extension doctest) creates temporary files when running CPP, pre-processors, TH, etc...

Unless proven otherwise I assume that creating a temporary directory with two symlinks is negligible for the overall picture.

We could add cashing in a second step, but again I would do that in some doctest specific location. From what I remember, this is also the approach that Matthew took, right?

@wismill

wismill commented Jul 11, 2024

Copy link
Copy Markdown
Author

3. Also, as a sidenote, e.g. stack --silent --resolver=ghc-9.10.1 path --compiler-bin can give you a GHC without creating symliknks.

The whole issue is about cabal, not stack. I am not using stack.

Not really. Before we were trying to determine ghc-pkg from doctest. Now we are trying to determine it from ghc, which is trivia. But we can also augment doctest --info with the path of ghc-pkg, which effectively delegates to ghc-paths.

I do not follow. I thought the recent discussion was to add ergonomics:

  • Ensure the correct doctest is installed for the corresponding GHC
  • Allow to choose GHC version with -w.

What I propose seems simple to me:

  • Detect if the current doctest is compiled with the correct GHC version.
  • If yes, then proceed normally.
  • Else:
    • Check if the target doctest is already installed.
    • If so, run it with the same cli args.
    • Else install it, then run it with the same cli args.

I do not see why we would need to change the ghc-pkg detection or involve ghcup.

This makes the code more complicated.

I do not think so. But this is hard to debate until there is actually some code.

Caching is an optimization step. Early optimization is the root of all…

I think the design is sound. And this is just using the cabal options properly, so not exactly an optimization.

I think it's better not to modify the user environment.

Hmm if we install doctest we do modify the user environment: the cabal store. If a user do not want to modify the default store, they can specify explicitly another path for a new store. But I do not see why installing e.g. doctest-9.10.1 the usual way, without temp dir, is an issue.

Also note, that any invocation of ghc (and by extension doctest) creates temporary files when running CPP, pre-processors, TH, etc...

I do not see how this is relevant here: I am not criticizing temporary files in general, only your proposal because I do not see the added value.

Unless proven otherwise I assume that creating a temporary directory with two symlinks is negligible for the overall picture.

Right, but the added value has to be proven.


I am now demotivated with adding any new feature to this PR, because it seems our views diverge too much.

So far you have not even commented the current feature of the PR. You keep referring to the work of Matthew, so I may end just closing this PR and create a doctest-cabal package, or also just keep my fork. But I would prefer that such package lives in this repo.

This is my second attempt to fix #396 after haskell/cabal#10057. It works quite well and only misses the setup step. If there is so much debate about the setup step, maybe it is simply a false good idea, because there are too different use cases. In those cases I usually stick to the Unix philosophy: do one thing well, e.g. the current feature of this PR.

@sol

sol commented Jul 11, 2024

Copy link
Copy Markdown
Owner
  1. Also, as a sidenote, e.g. stack --silent --resolver=ghc-9.10.1 path --compiler-bin can give you a GHC without creating symliknks.

The whole issue is about cabal, not stack. I am not using stack.

  1. I'm talking about stack vs ghcup as a tool for managing GHC versions.
  2. You can use stack provided GHCs with cabal.
  3. I'm not saying that you should use stack to manage GHC versions. My point is, that it is possible to bring a specific version of GHC into scope, without creating symlinks (specifically PATH=$(stack --silent --resolver=ghc-9.10.1 path --compiler-bin):$PATH cabal doctest). For what reason ghcup creates symlinks, I don't know.

@sol

sol commented Jul 11, 2024 •

Copy link
Copy Markdown
Owner

This makes the code more complicated.

I do not think so. But this is hard to debate until there is actually some code.

That's a fair point, #425 is my stab on this.

Unless proven otherwise I assume that creating a temporary directory with two symlinks is negligible for the overall picture.

It turns out that cabal takes over 2s to create those symlinks 🤦 At this point I agree that we want to cache the executable.

  • cabal doctest (without -w) runs the cabal-doctest we initially installed.

I think it's more robust to always install doctest with the ghc that is on the path.

Fair enough.

This is my most important concern, so if we at least agree on this point then I think we still have some common ground.

I haven't mentioned this explicitly, why is this important?

  1. As a user I expect that cabal build and cabal doctest use the same version of GHC. This is true for Add cabal-doctest #425, but not for this PR.

In addition:

  1. I think it's also desirable that cabal doctest and cabal-doctest do the same thing.
  2. I still insist, that cabal-doctest does not modify the user environment. Creating a semi-permanent cache at some private location or populating the cabal store (which is essentially just a cache for build products) is fine with me. But placing executables into ~/.local/bin is off-limits.

(both are true for #425, but not for this PR)

EDIT: @wismill Actually (3) is true for your current code.

If you're still interested in collaborating on this, then I'd love to get your feedback on #425.

At this point, I think this is what's missing:

  • Cache the doctest executables.
  • Add support for -w, e.g. -w 9.10.1.

@wismill wismill closed this Jul 12, 2024
@sol sol mentioned this pull request Jul 12, 2024
4 of 10 tasks
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.

doctest in multi-GHC setting

2 participants