1616
1717import atexit
1818import os
19+ import re
1920import shutil
2021import sys
2122from pathlib import Path
@@ -117,7 +118,6 @@ def main(
117118 absolute_path_prefix = resolved_requirements_file [
118119 : - (len (requirements_file ) - len (repository_prefix ))
119120 ]
120-
121121 # As srcs might contain references to generated files we want to
122122 # use the runfiles file first. Thus, we need to compute the relative path
123123 # from the execution root.
@@ -162,12 +162,18 @@ def main(
162162 argv .append (
163163 f"--output-file={ requirements_file_relative if UPDATE else requirements_out } "
164164 )
165- argv . extend (
165+ src_files = [
166166 (src_relative if Path (src_relative ).exists () else resolved_src )
167167 for src_relative , resolved_src in zip (srcs_relative , resolved_srcs )
168- )
168+ ]
169+ argv .extend (src_files )
169170 argv .extend (extra_args )
170171
172+ # Replace in the output lock file
173+ # the lines like: # via -r /absolute/path/to/<requirements_file>
174+ # with: # via -r <requirements_file>
175+ absolute_src_prefixes = [str (Path (src ).absolute ().parent ) + "/" for src in src_files ]
176+
171177 if UPDATE :
172178 print ("Updating " + requirements_file_relative )
173179
@@ -185,14 +191,14 @@ def main(
185191 # and we should copy the updated requirements back to the source tree.
186192 if not absolute_output_file .samefile (requirements_file_tree ):
187193 atexit .register (
188- lambda : shutil .copy (
189- absolute_output_file , requirements_file_tree
190- )
194+ lambda : shutil .copy (absolute_output_file , requirements_file_tree )
191195 )
192- cli (argv , standalone_mode = False )
196+ cli (argv , standalone_mode = False )
193197 requirements_file_relative_path = Path (requirements_file_relative )
194198 content = requirements_file_relative_path .read_text ()
195199 content = content .replace (absolute_path_prefix , "" )
200+ for absolute_src_prefix in absolute_src_prefixes :
201+ content = content .replace (absolute_src_prefix , "" )
196202 requirements_file_relative_path .write_text (content )
197203 else :
198204 # cli will exit(0) on success
@@ -214,6 +220,15 @@ def main(
214220 golden = open (_locate (bazel_runfiles , requirements_file )).readlines ()
215221 out = open (requirements_out ).readlines ()
216222 out = [line .replace (absolute_path_prefix , "" ) for line in out ]
223+
224+ def replace_via_minus_r (line ):
225+ if "# via -r " in line :
226+ for absolute_src_prefix in absolute_src_prefixes :
227+ line = line .replace (absolute_src_prefix , "" )
228+ return line
229+ return line
230+
231+ out = [replace_via_minus_r (line ) for line in out ]
217232 if golden != out :
218233 import difflib
219234
0 commit comments