happy-psychiatrist-90774
07/22/2024, 5:11 PM__file__ to do that, but that's not very elegant
Is there a builtin way of doing that? Or maybe at least access GLOBAL.pythonpath from pants.toml ?curved-television-6568
07/23/2024, 8:12 AMGLOBAL.pythonpath would be able to substitute for __file__..?)
See https://www.pantsbuild.org/2.21/docs/writing-plugins/the-rules-api/options-and-subsystems
and for example on using global options: https://github.com/pantsbuild/pants/blob/71749c04c7cf3cd9c22aea3e99fded57d5f41ebc/src/python/pants/core/util_rules/distdir.py#L29-L31
It is a good idea to have a rule, as in the case above, that extracts the option you care about, and depend on that value only from your other rules that require it. The reason being that if you depend on GlobalOptions directly, your rule will be invalidated on any change to any global option, even if it doesn't affect your rule at all. Isolating the option extraction like this, will still invalidate the option extraction rule, but if the option you care about is unchanged, then it will not invalidate further.happy-psychiatrist-90774
07/23/2024, 10:38 AMGlobalOptions and BuildRoot are not mentioned anywhere in the docs, though...happy-psychiatrist-90774
07/23/2024, 10:40 AM@dataclass(frozen=True)
class OwnPath:
"""
The directory of this plugin.
"""
path: Path
@rule
async def own_path(buildroot: BuildRoot) -> OwnPath:
own_root = Path(__file__).parent
build_root = buildroot.pathlib_path
return OwnPath(
own_root.relative_to(build_root) if own_root.is_absolute() else own_root
)
Does it look correct?curved-television-6568
07/24/2024, 12:01 PMhappy-psychiatrist-90774
09/11/2024, 8:38 AMown_path rule, I'm experiencing an issue because own_root is located at /tmp/pants-sandbox-... and build_root is /tmp/_BUILD_ROOT... so own_root.relative_to(build_root) fails
I tried to patch buildroot.pathlib_path but it doesn't seem to work...
This rule is tested as part of a goal using run_goal_rule()
Any idea what could be done here?happy-kitchen-89482
09/15/2024, 4:56 PMhappy-kitchen-89482
09/15/2024, 4:56 PMhappy-kitchen-89482
09/15/2024, 4:57 PMhappy-kitchen-89482
09/15/2024, 4:58 PMhappy-kitchen-89482
09/15/2024, 4:58 PMhappy-kitchen-89482
09/15/2024, 4:59 PMown_root is in a sandboxhappy-kitchen-89482
09/15/2024, 4:59 PMhappy-psychiatrist-90774
09/15/2024, 5:36 PMrun_rule_with_mocks() and run_goal_rule() in two separate tests
The logic of the rule relies on __file__ because we need to find where the plugin files are stored, and we need that path to be relative to build_root for obvious reasonshappy-psychiatrist-90774
09/15/2024, 5:38 PM__file__ and it worked, but I'm not happy with this solution
patch('plugin.rule_file.__file__', f'{rule_runner.build_root}/plugin_dir')happy-kitchen-89482
09/15/2024, 7:31 PMpytest process).happy-kitchen-89482
09/15/2024, 7:32 PMhappy-kitchen-89482
09/15/2024, 7:32 PMhappy-psychiatrist-90774
09/15/2024, 9:46 PMhappy-kitchen-89482
09/15/2024, 10:18 PMhappy-kitchen-89482
09/15/2024, 10:18 PMhappy-kitchen-89482
09/15/2024, 10:19 PMhappy-kitchen-89482
09/15/2024, 10:19 PMrun_* rules in your test, you will be invoking a whole new Pants instancehappy-kitchen-89482
09/15/2024, 10:20 PMhappy-psychiatrist-90774
09/16/2024, 4:41 PM