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 pre and post build hooks #9899

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

erikd
Copy link
Member

@erikd erikd commented Apr 17, 2024

Run a program (named "preBuildHook") before doing a package build and another program (named "postBuildHook") after the package is built.

These two programs simply need to be on the users $PATH variable and are completely generic.

Co-authored: Moritz Angermann moritz.angermann@gmail.com

Related to: #9892

Please read Github PR Conventions and then fill in one of these two templates.


Template Α: This PR modifies cabal behaviour

Include the following checklist in your PR:

  • Patches conform to the coding conventions.
  • Any changes that could be relevant to users have been recorded in the changelog.
  • The documentation has been updated, if necessary.
  • Manual QA notes have been included.
  • Tests have been added. (Ask for help if you don’t know how to write them! Ask for an exemption if tests are too complex for too little coverage!)

Template Β: This PR does not modify cabal behaviour (documentation, tests, refactoring, etc.)

Include the following checklist in your PR:

  • Patches conform to the coding conventions.
  • Is this a PR that fixes CI? If so, it will need to be backported to older cabal release branches (ask maintainers for directions).

@erikd erikd marked this pull request as draft April 17, 2024 04:34
@erikd erikd self-assigned this Apr 17, 2024
@erikd erikd force-pushed the erikd/pre-post-build-hooks branch 3 times, most recently from f40888c to 56349de Compare April 17, 2024 04:52
@angerman angerman requested a review from hasufell April 17, 2024 05:38
@erikd erikd force-pushed the erikd/pre-post-build-hooks branch 2 times, most recently from c5cd321 to ed08c4e Compare April 17, 2024 06:08
Copy link
Member

@hasufell hasufell left a comment

Choose a reason for hiding this comment

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

Needs documentation (readthedocs)

Comment on lines +693 to +697
when (code /= ExitSuccess) $ do
runBuild
Copy link
Member

Choose a reason for hiding this comment

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

This looks like it's not really a hook in the original sense anymore, because it causes an internal cabal phase to be skipped entirely. Why?

The way e.g. git hooks are designed is they just augment existing phases. An error code of e.g. a pre-commit hook, will make the commit as a whole fail.

Copy link
Collaborator

Choose a reason for hiding this comment

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

We could be more explicit in using the exit code to signal whether or not we want the subsequent action to happen or be skipped. You may want the hook to allow skipping under certain conditions.

`catchIO` (\_ -> return (ExitFailure 10))
when (code /= ExitSuccess) $ do
runBuild
-- not sure, if we want to care about a failed postBuildHook?
Copy link
Member

Choose a reason for hiding this comment

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

I argue we should. The author of the script can always hide errors of whatever commands they're executing inside the hook.

Copy link
Member Author

Choose a reason for hiding this comment

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

What would you propose?

Comment on lines +685 to +694
code <-
rawSystemExitCode
verbosity
(Just srcdir)
(hookDir </> "preBuildHook")
[ (unUnitId $ installedUnitId rpkg)
, (getSymbolicPath srcdir)
, (getSymbolicPath builddir)
]
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 much more discussion.

  1. relying on PATH to find hooks is very improper imo (I see you already changed it)
    • this should be in something like ~/.cabal/hooks and the user should be able to configure the location via the cabal config
  2. just like git, we should only execute hooks if they're marked as executable (windows is a special case, see how we did it in stack as an example)
  3. we need a discussion what information we want to expose (e.g. env vars that are available to all hooks, like CABAL_VERSION or GHC_VERSION?)

Copy link
Member Author

Choose a reason for hiding this comment

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

The PATH thing has been replaced with looking up ~/.cabal/hooks/.

, (getSymbolicPath srcdir)
, (getSymbolicPath builddir)
]
`catchIO` (\_ -> return (ExitFailure 10))
Copy link
Member

Choose a reason for hiding this comment

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

I'm not convinced we should overwrite the exit code of the script?

Copy link
Collaborator

Choose a reason for hiding this comment

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

there is no exit code. This is file not found mapped to ExitFailure 10. Yes 10 is just a magic constant.

@@ -678,7 +679,32 @@ buildAndInstallUnpackedPackage
runConfigure
PBBuildPhase{runBuild} -> do
noticeProgress ProgressBuilding
runBuild
hookDir <- (</> ".cabal/hooks") <$> getEnv "HOME"
Copy link
Member

Choose a reason for hiding this comment

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

I think this isn't quite what we want. Cabal has its own logic (e.g. XDG dir, honoring config values, etc.). There must be a set of functions providing those locations.

Copy link
Member Author

Choose a reason for hiding this comment

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

Replaced this with:

          cabalDir <- dropFileName <$> defaultConfigFile
          let hookDir = cabalDir </> "hooks"

Copy link
Collaborator

Choose a reason for hiding this comment

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

I suggest getDefaultDir XdgConfig "hooks".

@erikd erikd force-pushed the erikd/pre-post-build-hooks branch 4 times, most recently from aa2decd to 5951fc0 Compare April 23, 2024 00:58
Run a program (named "preBuildHook") before doing a package build and
another program (named "postBuildHook") after the package is built.

These two programs need to be in ~/.cabal/hooks/ directory. If two or
more projects need to use different hooks, dispatch can be done using
environment variables.

Co-authored: Moritz Angermann <moritz.angermann@gmail.com>
@erikd erikd force-pushed the erikd/pre-post-build-hooks branch from 5951fc0 to 7f4ea63 Compare April 24, 2024 01:20
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.

None yet

4 participants