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

Better support for running scripts. #7851

Merged
merged 28 commits into from Dec 31, 2021
Merged

Conversation

bacchanalia
Copy link
Collaborator

@bacchanalia bacchanalia commented Dec 2, 2021

Add caching to cabal run script and script support with caching to build, repl, and clean.

This is intended to close #7842, #6354, and #6149

edit: Title and body changed to reflect end of WIP.

bacchanalia added a commit to bacchanalia/cabal that referenced this pull request Dec 2, 2021
@jneira jneira marked this pull request as draft December 3, 2021 06:05
@jneira jneira self-requested a review December 3, 2021 06:42
@jneira jneira requested a review from fendor December 3, 2021 06:43
@jneira
Copy link
Member

jneira commented Dec 3, 2021

hi! many thanks for open the wip pr, i hope it could help to get some general feedback from maintainers.

Maybe you have thought on but for completeness i think the pr would need:

  • documentation for all new public functions (it seems they all already have it)
  • some tests i can think of:
    • cabal build/repl/run script.hs generates the expected known directory with build products
    • all cabal run script.hs run effectively the script
    • cabal repl script.hs effectively opens a repl with the script module in scope
    • a second cabal repl/build/run script.hs does not rebuild anything
    • after a cabal build script.hs, cabal run/repl script,hs does not rebuild anything

Not sure if you have some knowledge about the functional test suite, feel free to ask anything about. Surely it will contain similar tests over the script feature, and some about build/repl which can be used as a model.

Although this is still a WIP i would ask maintainers to share their general thoughts and the possible drawbacks about the chosen implementation, specially if they could be a blocker for merging the pr, thanks in advance!

One of the concerns could be the interaction of this with the fake-package machinery. Afaiu the plan is to eventually remove it and replace it with a all-in-memory implementation (see #6977).
One posible adaptation could be replace caching of all build products with the caching only the executable. This could work for two consecutive runs or a build + run. Not sure how cabal repl script.hs could be cached without writing anything to disk though. Arguably cabal repl will be faster without cache but it could be noticeable for larger scripts.

//cc @gbaz @fgaz @Kleidukos

@bacchanalia
Copy link
Collaborator Author

bacchanalia commented Dec 3, 2021

Also for completeness it needs changes to the non-haddock docs, because it changes the interface for build, clean and repl

@bacchanalia
Copy link
Collaborator Author

bacchanalia commented Dec 4, 2021

The following tests are still failing and it's not clear to me why (callProcess exits with 1 and no further info is printed, but they both seem to be behaving correctly):

  • cabal-testsuite/PackageTests/Regression/T4202/cabal.test.hs
  • cabal-testsuite/PackageTests/BuildTools/Internal/cabal.test.hs

In addition, the existing tests caught an issue with --builddir being set. Are there any other flags I should potentially worry about that existing tests might not catch?

@Mikolaj
Copy link
Member

Mikolaj commented Dec 6, 2021

The testsuite has gaps, so it can fail to catch anything. It's definitely worth debugging the return code 1. Processes are fragile and there are known issues, but we should at least try to determine what exactly is misbehaving and perhaps file a bug report about that.

@bacchanalia
Copy link
Collaborator Author

bacchanalia commented Dec 6, 2021

@Mikolaj I found a problem in my code that may have been causing those tests to fail (I was passing the wrong ComponentKind for build), but I'm in the middle of of a refactor to solve another issue I found while writing tests (repl didn't handle the case when the script was in a directory with a project correctly). Hopefully those tests will pass after I finish my current work.

@bacchanalia
Copy link
Collaborator Author

I added the tests suggested by @jneira, with the except that I don't test that build artifacts are created in the cache dir because the test runner always passes a --builddir flag which overrides that location. I do test that the fake project artifacts are created in the cache dir. I have manually tested that the cache dir is used correctly when --builddir is not passed.

In addition, it would in theory be nice to tests for the code path in which the script resides outside of a project directory, but this is impossible afaik because all tests live inside the cabal-tests project directory.

@Mikolaj
Copy link
Member

Mikolaj commented Dec 7, 2021

I don't remember --- does cabal walk up the directory tree checking all ancestors for a project file? If so, can't a test script create a directory in /tmp, cd to it and run there? I don't count on such functionality already in the test suite and probably not worth adding it, but can such an ad-hoc solution work for this particular test?

@bacchanalia
Copy link
Collaborator Author

@Mikolaj cabal does walk up the tree. If we wanted to add the functionality, then placing a test directory in /tmp would probably be how to do it, but I think it might require a significant changes to the test runner.

bacchanalia added a commit to bacchanalia/cabal that referenced this pull request Dec 7, 2021
@bacchanalia bacchanalia changed the title [WIP] Better support for running scripts. #7842 Better support for running scripts. Dec 7, 2021
@bacchanalia bacchanalia marked this pull request as ready for review December 7, 2021 17:24
@emilypi
Copy link
Member

emilypi commented Dec 7, 2021

Champion! I'll get a review out asap today :3

bacchanalia added a commit to bacchanalia/cabal that referenced this pull request Dec 8, 2021
bacchanalia added a commit to bacchanalia/cabal that referenced this pull request Dec 8, 2021
Test logs showed that the failures where because the tests depended on a
module from cabal-install that some ghc versions could not find.

Instead of depending on cabal-install, I copied the needed function into
Test.Cabal.Prelude (It seemed like an acceptable place for it)

PR haskell#7851
bacchanalia added a commit to bacchanalia/cabal that referenced this pull request Dec 8, 2021
bacchanalia added a commit to bacchanalia/cabal that referenced this pull request Dec 8, 2021
Tests failing on pre-AMP ghcs due to unsanctioned use of (<$>)

PR haskell#7851
Copy link
Collaborator

@fendor fendor left a comment

Choose a reason for hiding this comment

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

Looking great! (Assuming everything works as intended ;D)
I basically have only minor nitpicks, regarding docs, formatting, etc...

Really great work, can't wait to have cabal repl support to finally unbreak HLS on cabal scripts 🤯

cabal-install/src/Distribution/Client/ScriptUtils.hs Outdated Show resolved Hide resolved
cabal-install/src/Distribution/Client/ScriptUtils.hs Outdated Show resolved Hide resolved
cabal-install/src/Distribution/Client/ScriptUtils.hs Outdated Show resolved Hide resolved
cabal-install/src/Distribution/Client/ScriptUtils.hs Outdated Show resolved Hide resolved
cabal-install/src/Distribution/Client/ScriptUtils.hs Outdated Show resolved Hide resolved
doc/cabal-commands.rst Outdated Show resolved Hide resolved
doc/cabal-commands.rst Show resolved Hide resolved
cabal-install/src/Distribution/Client/ScriptUtils.hs Outdated Show resolved Hide resolved
cabal-install/src/Distribution/Client/ScriptUtils.hs Outdated Show resolved Hide resolved
cabal-install/src/Distribution/Client/ScriptUtils.hs Outdated Show resolved Hide resolved
Copy link
Collaborator

@fendor fendor left a comment

Choose a reason for hiding this comment

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

Can we have a test that documents what the expected output is if v2-run is not run on a script?

cabal-install/src/Distribution/Client/CmdClean.hs Outdated Show resolved Hide resolved
Test logs showed that the failures where because the tests depended on a
module from cabal-install that some ghc versions could not find.

Instead of depending on cabal-install, I copied the needed function into
Test.Cabal.Prelude (It seemed like an acceptable place for it)

PR haskell#7851
Tests failing on pre-AMP ghcs due to unsanctioned use of (<$>)

PR haskell#7851
- remove partial selectors
- make a constant for fake-package.cabal

PR haskell#7851
- Set the hs-source-dir to the location of the script for build and run,
  the same as with repl
- This removes the need to copy the script
- repl no longer needs a separate cache because all three commands
  use identical project files
- Adds multi-module support to scripts for free (haskell#6787)
- Add new build/repl test and run multi-module test

PR haskell#7851
- Pass info about cwd to repl through --repl-options instead of hacking
  it into the project file.
- Improve paths output by makeRelativeCanonical, makeRelativeToDir, and
  makeRelativeToCwd.
- Script multi-module support works, but with warning in repl.
- Remove script multi-module mention support in docs.

PR haskell#7851
Move argument truncation from targetStrings out of
withScriptContextAndSelectors to runAction

PR haskell#7851
- instead pass absolute path to script in main-is
- resolves issue with relative paths on Windows
- simplifies code and gives prettier build output
- update tests because build output has changed
- removes ability to use multi-module scripts
  (which was never officially endorsed)
- remove test for multi-module scripts
- add checks for unsupported fields in scripts

PR haskell#7851
@mergify
Copy link
Contributor

mergify bot commented Dec 31, 2021

rebase

✅ Branch has been successfully rebased

@mergify mergify bot merged commit bbc11f1 into haskell:master Dec 31, 2021
georgefst added a commit to georgefst/monpad that referenced this pull request Jan 9, 2022
This enables running it via simply `./Build.hs`.

It's difficult (possibly not standardized at all, I can't remember) to specify arguments here, so for now this will only work if GHC 8.10.7 is the version enabled globally. And using `cabal install --lib` (or `cabal-env`, or my script for extracting environment files from cabal scripts) is still preferable due to the slowness of `cabal run`, and HLS support. Cabal scripts are set to become more usable in 3.8: haskell/cabal#7851.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
squash+merge me Tell Mergify Bot to squash-merge
Projects
None yet
6 participants