<#19362 Make it possible to add post-run actions f...
# github-notifications
c
#19362 Make it possible to add post-run actions for Pants goals Issue created by AlexTereshenkov Is your feature request related to a problem? Please describe. Not every Pants user is going to be closely familiar with the errors that Pants can throw and their meaning (particularly what this means in the context of their engineering organization as this may vary between companies). Most likely, Pants admins / build systems engineers will have some internal documentation / troubleshooting guides tailored to the engineering organization jargon / context. This means it may be helpful to let this group of advanced Pants users to extend what information is shown in the console upon completion of a goal run if there are any errors / warnings. This could be in a form of epilogue message that is shown after the error message is printed (similarly to how Python 3.11 exception notes work), but could really be any action that can be triggered upon a certain action. This is how it may look like:
Copy code
❯ pants dependencies ::
12:44:06.76 [ERROR] 1 Exception encountered:

Engine traceback:
  in `dependencies` goal

DependencyRuleActionDeniedError: cheeseshop/cli/utils/utils.py has 1 dependency violation:

  * cheeseshop/cli/utils/BUILD[!*] -> cheeseshop/repository/parsing : DENY
    python_sources cheeseshop/cli/utils/utils.py -> python_sources cheeseshop/repository/parsing/casts.py

---------------------------------------------------------------------------------------------------------------
To learn more about the visibility rules violations and how to fix them, 
please see <https://mycompany.org/development-guidelines/pants#visibility-violations>.
---------------------------------------------------------------------------------------------------------------
Describe the solution you'd like Be able to write custom plugin that would let Pants users specify the actions that should take place upon certain exceptions thrown or upon completion of a certain goal. Describe alternatives you've considered This is currently possible to achieve (for certain conditions) and definitely with certain limitations by adding code into the
rules()
call inside the plugin module containing rules code (as the
rules()
is guaranteed to be executed on any Pants run). This is achieved by monkey patching the exception so that a custom one (with an epilogue added to the message) will be used instead.
Copy code
from pants.engine import target
from pants.engine.rules import collect_rules


class DependencyRuleActionDeniedError(Exception):
    @classmethod
    def create(cls, description_of_origin: str) -> "DependencyRuleActionDeniedError":
        return cls(f"Dependency rule violation for {description_of_origin}")

    def __init__(self, message):
        super(DependencyRuleActionDeniedError, self).__init__(
            message + "\n\n" + construct_epilogue_body(DependencyRuleActionDeniedErrorEpilogue)
        )


def rules():
    target.DependencyRuleActionDeniedError = DependencyRuleActionDeniedError
    return collect_rules()
This isn't the most optimal way to achieve this as this may require some duplication of the Pants code in a plugin which may be brittle. pantsbuild/pants