@@ -14,7 +14,7 @@ MyPyAspectInfo = provider(
14
14
15
15
# Switch to True only during debugging and development.
16
16
# All releases should have this as False.
17
- DEBUG = False
17
+ DEBUG = True
18
18
19
19
VALID_EXTENSIONS = ["py" , "pyi" ]
20
20
@@ -72,16 +72,38 @@ def _extract_transitive_deps(deps):
72
72
transitive_deps .append (dep [PyInfo ].transitive_sources )
73
73
return transitive_deps
74
74
75
+ def _extract_transitive_inputs (deps ):
76
+ input_depsets = []
77
+ for dep in deps :
78
+ # # Supply the --cache-map files from transitives
79
+ # if MypyInfo in dep:
80
+ # input_depsets.append(dep[MypyInfo].transitive_cache_map)
81
+
82
+ # TODO: relies on PyInfo being a Bazel global symbol.
83
+ # When https://github.com/bazelbuild/rules_python/issues/1645 is fixed we can flip that flag to keep us honest.
84
+ if PyInfo in dep :
85
+ # TODO: maybe we can avoid passing .py source files when the cache-map files were found?
86
+ input_depsets .append (dep [PyInfo ].transitive_sources )
87
+
88
+ # rules_python puts .pyi files into the data attribute of a py_library
89
+ # so the transitive_sources is not sufficient.
90
+ # TODO: should we change rules_python to pass .pyi files in some provider?
91
+ if dep .label .workspace_root .startswith ("external/" ):
92
+ input_depsets .append (dep [DefaultInfo ].default_runfiles .files )
93
+ return input_depsets
94
+
75
95
def _extract_stub_deps (deps ):
76
96
# Need to add the .py files AND the .pyi files that are
77
97
# deps of the rule
78
98
stub_files = []
99
+ print ('deps' , deps )
79
100
for dep in deps :
80
101
if MyPyStubsInfo in dep :
81
102
for stub_srcs_target in dep [MyPyStubsInfo ].srcs :
82
103
for src_f in stub_srcs_target .files .to_list ():
83
104
if src_f .extension == "pyi" :
84
105
stub_files .append (src_f )
106
+ print ('stub_files' , stub_files )
85
107
return stub_files
86
108
87
109
def _extract_imports (imports , label ):
@@ -113,9 +135,12 @@ def _mypy_rule_impl(ctx, is_aspect = False):
113
135
if hasattr (base_rule .attr , "srcs" ):
114
136
direct_src_files = _extract_srcs (base_rule .attr .srcs )
115
137
138
+ transitive_imports = depset ()
116
139
if hasattr (base_rule .attr , "deps" ):
117
140
transitive_srcs_depsets = _extract_transitive_deps (base_rule .attr .deps )
118
141
stub_files = _extract_stub_deps (base_rule .attr .deps )
142
+ transitive_imports = depset (transitive = [dep [PyInfo ].imports for dep in base_rule .attr .deps if PyInfo in dep ])
143
+ print ('transitive_imports' , transitive_imports )
119
144
120
145
if hasattr (base_rule .attr , "imports" ):
121
146
mypypath_parts = _extract_imports (base_rule .attr .imports , ctx .label )
@@ -128,6 +153,7 @@ def _mypy_rule_impl(ctx, is_aspect = False):
128
153
129
154
mypypath_parts += [src_f .dirname for src_f in stub_files ]
130
155
mypypath = ":" .join (mypypath_parts )
156
+ print ('mypypath' , mypypath )
131
157
132
158
# Ideally, a file should be passed into this rule. If this is an executable
133
159
# rule, then we default to the implicit executable file, otherwise we create
@@ -149,18 +175,25 @@ def _mypy_rule_impl(ctx, is_aspect = False):
149
175
# Compose a list of the files needed for use. Note that aspect rules can use
150
176
# the project version of mypy however, other rules should fall back on their
151
177
# relative runfiles.
152
- runfiles = ctx .runfiles (files = src_files + stub_files + [mypy_config_file ])
178
+ transitive_files = depset (transitive = _extract_transitive_inputs (base_rule .attr .deps ))
179
+ print ('transitive_files' , transitive_files )
180
+ runfiles = ctx .runfiles (files = src_files + stub_files + [mypy_config_file ], transitive_files = transitive_files )
181
+ # runfiles = ctx.runfiles(files = src_files + stub_files + [mypy_config_file], transitive_files = transitive_files)
182
+ # runfiles = runfiles.merge(transitive_imports)
153
183
if not is_aspect :
154
184
runfiles = runfiles .merge (ctx .attr ._mypy_cli .default_runfiles )
155
185
156
186
src_root_paths = sets .to_list (
157
187
sets .make ([f .root .path for f in src_files ]),
158
188
)
159
189
190
+ print ('src_root_paths' , src_root_paths )
191
+
160
192
ctx .actions .expand_template (
161
193
template = ctx .file ._template ,
162
194
output = exe ,
163
195
substitutions = {
196
+ "{PYTHONPATH}" : ":" .join (["external/" + t for t in transitive_imports .to_list ()]),
164
197
"{MYPY_EXE}" : ctx .executable ._mypy_cli .path ,
165
198
"{MYPY_ROOT}" : ctx .executable ._mypy_cli .root .path ,
166
199
"{CACHE_MAP_TRIPLES}" : " " .join (_sources_to_cache_map_triples (src_files , is_aspect )),
0 commit comments