Hey guys, I'm using `__dependents_rules__` to forb...
# general
s
Hey guys, I'm using
__dependents_rules__
to forbid importing a deprecated module. Is there a way to add a comment to the rule, so that people can see why exactly the lint fails?
b
I don't think so, except via hacks: • comments on the rule that someone goes to find • smuggling an explanation into a "glob" somehow Sounds like a good feature though. Would you be interested in implement it?
f
hey! This was relevant to me for some time ago as well. I think a feature that may be missing is to be able to pass arbitrary messages for various Pants errors. This should be generic enough so that we don't need to write custom code to accommodate every possible failure that can occur. Also, users usually want to do it "their way", so it's hard to get it "right" for everyone. The reason behind is that in many organizations, lots of people have no idea about Pants and what kind of errors that it can throw - they just want to get the builds green, so whatever gets in their way is a nuisance. The error messages one can get from Pants often are helpful, but some time require prior knowledge of some of the build concepts (e.g. targets, ownership, etc). They way I "solved" it is by providing custom error messages, like an epilogue, after an error message. This can be done in a plugin.
Copy code
$ pants dependencies root/sources/project/mod.py                        
...
11:52:30.23 [ERROR] 1 Exception encountered:

Engine traceback:
  in `dependencies` goal

UnownedDependencyError: Pants cannot infer owners for the following imports in the target root/sources/project/mod.py:

  * foobar (line: 62)

If you do not expect an import to be inferrable, add `# pants: no-infer-dep` to the import line. Otherwise, see <https://www.pantsbuild.org/v2.19/docs/troubleshooting#import-errors-and-missing-dependencies> for common problems.

-----------------------------------------------------------------------------
To learn how to fix import ownership rules errors, visit:
<internal wiki address>
-----------------------------------------------------------------------------
the plugin:
Copy code
from internal_plugins.epilogues.messages import construct_epilogue_body, UnownedDependencyErrorEpilogue
from pants.backend.python.dependency_inference import rules as dep_infer_rules
from pants.engine.rules import collect_rules


class UnownedDependencyError(Exception):
    def __init__(self, message):
        super(UnownedDependencyError, self).__init__(  
            message + "\n\n" + construct_epilogue_body(UnownedDependencyErrorEpilogue)
        )


def rules(): 
    dep_infer_rules.UnownedDependencyError = UnownedDependencyError
    return collect_rules()
You won't be able to "override" the error message and often you don't want to, but adding something that makes sense in your organization can be hugely helpful
for visibility rules, this is the plugin's code:
Copy code
from internal_plugins.epilogues.messages import construct_epilogue_body, DependencyRuleActionDeniedErrorEpilogue
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()
s
Cool! Thanks Alexey, I'll try that
🙌 1