1818Uses a workspace marker file (like MODULE.bazel) to find the repo
1919root.
2020
21- Pylint is imported as a Python dependency provided by Bazel,
22- not invoked from PATH .
21+ The pylint executable is provided by Bazel as a py_binary and
22+ passed via the --pylint argument .
2323"""
2424
2525import argparse
2626import os
2727import pathlib
28+ import subprocess
2829import sys
2930
30- from pylint import lint
31-
3231
3332def workspace_root (marker ):
3433 """Resolve the workspace root from a marker file."""
@@ -52,11 +51,33 @@ def find_py_files(root, paths, exclude_patterns):
5251 return files
5352
5453
54+ def resolve_runfile (path ):
55+ """Resolve a Bazel rootpath to an absolute path via runfiles."""
56+ # In a py_test, the working directory is the runfiles tree
57+ # so rootpaths can be resolved relative to cwd
58+ resolved = os .path .realpath (path )
59+ if os .path .exists (resolved ):
60+ return resolved
61+ # Try via RUNFILES_DIR
62+ runfiles_dir = os .environ .get ("RUNFILES_DIR" , "" )
63+ workspace = os .environ .get ("TEST_WORKSPACE" , "_main" )
64+ if runfiles_dir :
65+ candidate = os .path .join (runfiles_dir , workspace , path )
66+ if os .path .exists (candidate ):
67+ return os .path .realpath (candidate )
68+ return resolved
69+
70+
5571def main ():
5672 """Parse arguments and run pylint on discovered sources."""
5773 parser = argparse .ArgumentParser (
5874 description = "Run pylint on Python source files."
5975 )
76+ parser .add_argument (
77+ "--pylint" ,
78+ required = True ,
79+ help = "Path to the pylint executable." ,
80+ )
6081 parser .add_argument (
6182 "--workspace" ,
6283 required = True ,
@@ -82,6 +103,12 @@ def main():
82103 )
83104 args = parser .parse_args ()
84105
106+ # Resolve paths before changing directories
107+ pylint_exe = resolve_runfile (args .pylint )
108+ rcfile_path = None
109+ if args .rcfile :
110+ rcfile_path = os .path .realpath (args .rcfile )
111+
85112 root = workspace_root (args .workspace )
86113 sources = find_py_files (root , args .paths , args .exclude )
87114
@@ -93,15 +120,14 @@ def main():
93120 # (which uses os.getcwd()) can resolve project imports
94121 os .chdir (root )
95122
96- pylint_args = []
97- if args .rcfile :
98- rcfile_path = os .path .realpath (args .rcfile )
99- pylint_args .append (f"--rcfile={ rcfile_path } " )
100- pylint_args .extend (str (s ) for s in sources )
123+ cmd = [pylint_exe ]
124+ if rcfile_path :
125+ cmd .append (f"--rcfile={ rcfile_path } " )
126+ cmd .extend (str (s ) for s in sources )
101127
102- print (f"Running: pylint { ' ' .join (pylint_args )} " )
103- result = lint . Run ( pylint_args , exit = False )
104- sys .exit (result .linter . msg_status )
128+ print (f"Running: { ' ' .join (cmd )} " )
129+ result = subprocess . run ( cmd , check = False )
130+ sys .exit (result .returncode )
105131
106132
107133if __name__ == "__main__" :
0 commit comments