Skip to content

Better support for running scripts. - #7851

Merged
mergify[bot] merged 28 commits into
haskell:masterfrom
bacchanalia:master
Dec 31, 2021
Merged

mergify[bot] merged 28 commits into
haskell:masterfrom
bacchanalia:master

Conversation

@bacchanalia

@bacchanalia bacchanalia commented Dec 2, 2021 •

Copy link
Copy Markdown
Collaborator

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

jneira commented Dec 3, 2021 •

Copy link
Copy Markdown
Member

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

bacchanalia commented Dec 3, 2021 •

Copy link
Copy Markdown
Collaborator Author

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

@bacchanalia

bacchanalia commented Dec 4, 2021 •

Copy link
Copy Markdown
Collaborator Author

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

Mikolaj commented Dec 6, 2021

Copy link
Copy Markdown
Member

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

bacchanalia commented Dec 6, 2021 •

Copy link
Copy Markdown
Collaborator Author

@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
Copy Markdown
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

Mikolaj commented Dec 7, 2021

Copy link
Copy Markdown
Member

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
Copy Markdown
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

emilypi commented Dec 7, 2021

Copy link
Copy Markdown
Member

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

@fendor fendor left a comment •

Copy link
Copy Markdown
Collaborator

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 🤯

Comment thread doc/cabal-commands.rst Resolved
Comment thread doc/cabal-commands.rst Resolved

@fendor fendor left a comment

Copy link
Copy Markdown
Collaborator

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?

ScriptUtils:
- Hash prefix for cache dirs was applied incorrectly.
- Overwriting fake-package files causes repeated work in some cases.
CmdClean:
- Clean distdir for script when --builddir is passed
- Always clean orphans because because there is no good way to specify
they should be cleaned. This may be bad behaviour in some obscure cases
(a cache is temporarily orphaned and an unrelated clean is run), but at
worst results in a cache rebuild.
- Basic script support for build/repl/clean which checks for cached
project files
- Add check for cached project files to basic run script test
- No repeated work for build/build, build/run, run/run, and repl/repl
- Clean does not remove cache for existing scripts
- Clean does remove orphaned script caches
- clean was trying to read source-builds even if it didn't exist
- add test specific to this case with other clean tests
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

mergify Bot commented Dec 31, 2021

Copy link
Copy Markdown
Contributor

rebase

✅ Branch has been successfully rebased

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

Development

Successfully merging this pull request may close these issues.

Better support for running scripts. cabal triggers a full build each time a script is executed cabal v2-repl scriptfile.hs

6 participants