Skip to content

Commit ad3f95b

Browse files
feat: Show hint for --verbose on pip-compile error
1 parent 89987b2 commit ad3f95b

File tree

3 files changed

+17
-10
lines changed

3 files changed

+17
-10
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ Unreleased changes template.
133133
configure them.
134134
* (toolchains) Expose `$(PYTHON2_ROOTPATH)` and `$(PYTHON3_ROOTPATH)` which are runfiles
135135
locations equivalents of `$(PYTHON2)` and `$(PYTHON3) respectively.
136+
* (pypi) A hint to run `//requirements.update` or `//requirements_test` with `--verbose` is now outputted if pip-compile throws an unexpected error
136137

137138

138139
{#v0-0-0-removed}

python/private/pypi/dependency_resolver/dependency_resolver.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,15 @@ def _locate(bazel_runfiles, file):
8585
@click.command(context_settings={"ignore_unknown_options": True})
8686
@click.option("--src", "srcs", multiple=True, required=True)
8787
@click.argument("requirements_txt")
88-
@click.argument("update_target_label")
88+
@click.argument("target_label_prefix")
8989
@click.option("--requirements-linux")
9090
@click.option("--requirements-darwin")
9191
@click.option("--requirements-windows")
9292
@click.argument("extra_args", nargs=-1, type=click.UNPROCESSED)
9393
def main(
9494
srcs: Tuple[str, ...],
9595
requirements_txt: str,
96-
update_target_label: str,
96+
target_label_prefix: str,
9797
requirements_linux: Optional[str],
9898
requirements_darwin: Optional[str],
9999
requirements_windows: Optional[str],
@@ -155,9 +155,10 @@ def main(
155155
# or shutil.copyfile, as they will fail with OSError: [Errno 18] Invalid cross-device link.
156156
shutil.copy(resolved_requirements_file, requirements_out)
157157

158-
update_command = os.getenv("CUSTOM_COMPILE_COMMAND") or "bazel run %s" % (
159-
update_target_label,
158+
update_command = (
159+
os.getenv("CUSTOM_COMPILE_COMMAND") or f"bazel run {target_label_prefix}.update"
160160
)
161+
test_command = f"bazel test {target_label_prefix}_test"
161162

162163
os.environ["CUSTOM_COMPILE_COMMAND"] = update_command
163164
os.environ["PIP_CONFIG_FILE"] = os.getenv("PIP_CONFIG_FILE") or os.devnull
@@ -196,15 +197,15 @@ def main(
196197
atexit.register(
197198
lambda: shutil.copy(absolute_output_file, requirements_file_tree)
198199
)
199-
_run_pip_compile()
200+
_run_pip_compile(verbose_command=f"{update_command} -- --verbose")
200201
requirements_file_relative_path = Path(requirements_file_relative)
201202
content = requirements_file_relative_path.read_text()
202203
content = content.replace(absolute_path_prefix, "")
203204
requirements_file_relative_path.write_text(content)
204205
else:
205206
print("Checking " + requirements_file)
206207
sys.stdout.flush()
207-
_run_pip_compile()
208+
_run_pip_compile(verbose_command=f"{test_command} --test_arg=--verbose")
208209
golden = open(_locate(bazel_runfiles, requirements_file)).readlines()
209210
out = open(requirements_out).readlines()
210211
out = [line.replace(absolute_path_prefix, "") for line in out]
@@ -213,7 +214,7 @@ def main(
213214

214215
print("".join(difflib.unified_diff(golden, out)), file=sys.stderr)
215216
print(
216-
"Lock file out of date. Run '" + update_command + "' to update.",
217+
f"Lock file out of date. Run '{update_command}' to update.",
217218
file=sys.stderr,
218219
)
219220
sys.exit(1)
@@ -223,6 +224,7 @@ def run_pip_compile(
223224
args: List[str],
224225
*,
225226
srcs_relative: List[str],
227+
verbose_command: str,
226228
) -> None:
227229
try:
228230
cli(args, standalone_mode=False)
@@ -241,13 +243,17 @@ def run_pip_compile(
241243
print(
242244
"pip-compile exited with code 2. This means that pip-compile found "
243245
"incompatible requirements or could not find a version that matches "
244-
f"the install requirement in one of {srcs_relative}.",
246+
f"the install requirement in one of {srcs_relative}.\n"
247+
"Try re-running with verbose:\n"
248+
f" {verbose_command}",
245249
file=sys.stderr,
246250
)
247251
sys.exit(1)
248252
else:
249253
print(
250-
f"pip-compile unexpectedly exited with code {e.code}.",
254+
f"pip-compile unexpectedly exited with code {e.code}.\n"
255+
"Try re-running with verbose:\n"
256+
f" {verbose_command}",
251257
file=sys.stderr,
252258
)
253259
sys.exit(1)

python/private/pypi/pip_compile.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def pip_compile(
110110

111111
args = ["--src=%s" % loc.format(src) for src in srcs] + [
112112
loc.format(requirements_txt),
113-
"//%s:%s.update" % (native.package_name(), name),
113+
"//%s:%s" % (native.package_name(), name),
114114
"--resolver=backtracking",
115115
"--allow-unsafe",
116116
]

0 commit comments

Comments
 (0)