Skip to content

Commit 10b8f28

Browse files
committed
fix: update correct requirements lock file when using os specific lock files
1 parent 756264a commit 10b8f28

File tree

6 files changed

+123
-17
lines changed

6 files changed

+123
-17
lines changed

.bazelci/presubmit.yml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,54 @@ tasks:
454454
- "! git diff --exit-code"
455455
- "bazel run //:requirements.update"
456456
- "git diff --exit-code"
457+
integration_test_compile_pip_os_specific_requirements_ubuntu:
458+
<<: *reusable_build_test_all
459+
name: compile_pip_os_specific_requirements integration tests on Ubuntu
460+
working_directory: tests/compile_pip_requirements
461+
platform: ubuntu2004
462+
shell_commands:
463+
# Make a change to the locked requirements and then assert that //:os_specific_requirements.update does the
464+
# right thing.
465+
- "echo '' > requirements_lock_linux.txt"
466+
- "! git diff --exit-code"
467+
- "bazel run //:os_specific_requirements.update"
468+
- "git diff --exit-code"
469+
integration_test_compile_pip_os_specific_requirements_debian:
470+
<<: *reusable_build_test_all
471+
name: compile_pip_os_specific_requirements integration tests on Debian
472+
working_directory: tests/compile_pip_requirements
473+
platform: debian11
474+
shell_commands:
475+
# Make a change to the locked requirements and then assert that //:os_specific_requirements.update does the
476+
# right thing.
477+
- "echo '' > requirements_lock_linux.txt"
478+
- "! git diff --exit-code"
479+
- "bazel run //:os_specific_requirements.update"
480+
- "git diff --exit-code"
481+
integration_test_compile_pip_os_specific_requirements_macos:
482+
<<: *reusable_build_test_all
483+
name: compile_pip_os_specific_requirements integration tests on macOS
484+
working_directory: tests/compile_pip_requirements
485+
platform: macos
486+
shell_commands:
487+
# Make a change to the locked requirements and then assert that //:os_specific_requirements.update does the
488+
# right thing.
489+
- "echo '' > requirements_lock_darwin.txt"
490+
- "! git diff --exit-code"
491+
- "bazel run //:os_specific_requirements.update"
492+
- "git diff --exit-code"
493+
integration_test_compile_pip_os_specific_requirements_windows:
494+
<<: *reusable_build_test_all
495+
name: compile_pip_os_specific_requirements integration tests on Windows
496+
working_directory: tests/compile_pip_requirements
497+
platform: windows
498+
shell_commands:
499+
# Make a change to the locked requirements and then assert that //:os_specific_requirements.update does the
500+
# right thing.
501+
- "echo '' > requirements_lock_windows.txt"
502+
- "! git diff --exit-code"
503+
- "bazel run //:os_specific_requirements.update"
504+
- "git diff --exit-code"
457505

458506
integration_test_pip_repository_entry_points_ubuntu_min:
459507
<<: *minimum_supported_version

python/pip_install/tools/dependency_resolver/dependency_resolver.py

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ def _select_golden_requirements_file(
8888
requirements_in_path = Path(requirements_in)
8989
resolved_requirements_in = str(requirements_in_path.resolve())
9090

91+
requirements_file = _select_golden_requirements_file(
92+
requirements_txt, requirements_linux, requirements_darwin, requirements_windows
93+
)
94+
9195
# Before loading click, set the locale for its parser.
9296
# If it leaks through to the system setting, it may fail:
9397
# RuntimeError: Click will abort further execution because Python 3 was configured to use ASCII
@@ -108,11 +112,11 @@ def _select_golden_requirements_file(
108112
sys.argv.append(os.environ["TEST_TMPDIR"])
109113
# Make a copy for pip-compile to read and mutate.
110114
requirements_out = os.path.join(
111-
os.environ["TEST_TMPDIR"], os.path.basename(requirements_txt) + ".out"
115+
os.environ["TEST_TMPDIR"], os.path.basename(requirements_file) + ".out"
112116
)
113117
# Those two files won't necessarily be on the same filesystem, so we can't use os.replace
114118
# or shutil.copyfile, as they will fail with OSError: [Errno 18] Invalid cross-device link.
115-
shutil.copy(requirements_txt, requirements_out)
119+
shutil.copy(requirements_file, requirements_out)
116120

117121
update_command = os.getenv("CUSTOM_COMPILE_COMMAND") or "bazel run %s" % (
118122
update_target_label,
@@ -123,28 +127,28 @@ def _select_golden_requirements_file(
123127

124128
sys.argv.append("--generate-hashes")
125129
sys.argv.append("--output-file")
126-
sys.argv.append(requirements_txt if UPDATE else requirements_out)
130+
sys.argv.append(requirements_file if UPDATE else requirements_out)
127131
sys.argv.append(
128132
requirements_in if requirements_in_path.exists() else resolved_requirements_in
129133
)
130134

131135
if UPDATE:
132-
print("Updating " + requirements_txt)
136+
print("Updating " + requirements_file)
133137
if "BUILD_WORKSPACE_DIRECTORY" in os.environ:
134138
workspace = os.environ["BUILD_WORKSPACE_DIRECTORY"]
135-
requirements_txt_tree = os.path.join(workspace, requirements_txt)
136-
# In most cases, requirements_txt will be a symlink to the real file in the source tree.
137-
# If symlinks are not enabled (e.g. on Windows), then requirements_txt will be a copy,
139+
requirements_file_tree = os.path.join(workspace, requirements_file)
140+
# In most cases, requirements_file will be a symlink to the real file in the source tree.
141+
# If symlinks are not enabled (e.g. on Windows), then requirements_file will be a copy,
138142
# and we should copy the updated requirements back to the source tree.
139-
if not os.path.samefile(requirements_txt, requirements_txt_tree):
143+
if not os.path.samefile(requirements_file, requirements_file_tree):
140144
atexit.register(
141-
lambda: shutil.copy(requirements_txt, requirements_txt_tree)
145+
lambda: shutil.copy(requirements_file, requirements_file_tree)
142146
)
143147
cli()
144148
else:
145149
# cli will exit(0) on success
146150
try:
147-
print("Checking " + requirements_txt)
151+
print("Checking " + requirements_file)
148152
cli()
149153
print("cli() should exit", file=sys.stderr)
150154
sys.exit(1)
@@ -158,13 +162,7 @@ def _select_golden_requirements_file(
158162
)
159163
sys.exit(1)
160164
elif e.code == 0:
161-
golden_filename = _select_golden_requirements_file(
162-
requirements_txt,
163-
requirements_linux,
164-
requirements_darwin,
165-
requirements_windows,
166-
)
167-
golden = open(golden_filename).readlines()
165+
golden = open(requirements_file).readlines()
168166
out = open(requirements_out).readlines()
169167
if golden != out:
170168
import difflib

tests/compile_pip_requirements/BUILD.bazel

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,33 @@ compile_pip_requirements(
3232
requirements_in = "requirements.txt",
3333
requirements_txt = "requirements_lock.txt",
3434
)
35+
36+
genrule(
37+
name = "generate_os_specific_requirements_in",
38+
srcs = [],
39+
outs = ["requirements_os_specific.in"],
40+
cmd = """
41+
cat > $@ <<EOF
42+
pip==22.3.0 ; sys_platform == "linux"
43+
pip==22.2.2 ; sys_platform == "darwin"
44+
pip==22.2.1 ; sys_platform == "win32"
45+
EOF
46+
""",
47+
)
48+
49+
compile_pip_requirements(
50+
name = "requirements_os_specific",
51+
data = [
52+
"requirements_extra.in",
53+
"requirements_os_specific.in",
54+
],
55+
extra_args = [
56+
"--allow-unsafe",
57+
"--resolver=backtracking",
58+
],
59+
requirements_darwin = "requirements_lock_darwin.txt",
60+
requirements_in = "requirements_os_specific.in",
61+
requirements_linux = "requirements_lock_linux.txt",
62+
requirements_txt = "requirements_lock.txt",
63+
requirements_windows = "requirements_lock_windows.txt",
64+
)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#
2+
# This file is autogenerated by pip-compile with Python 3.9
3+
# by the following command:
4+
#
5+
# bazel run //:requirements_os_specific.update
6+
#
7+
pip==22.2.2 ; sys_platform == "darwin" \
8+
--hash=sha256:3fd1929db052f056d7a998439176d3333fa1b3f6c1ad881de1885c0717608a4b \
9+
--hash=sha256:b61a374b5bc40a6e982426aede40c9b5a08ff20e640f5b56977f4f91fed1e39a
10+
# via -r ./requirements_os_specific.in
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#
2+
# This file is autogenerated by pip-compile with Python 3.9
3+
# by the following command:
4+
#
5+
# bazel run //:requirements_os_specific.update
6+
#
7+
pip==22.3 ; sys_platform == "linux" \
8+
--hash=sha256:1daab4b8d3b97d1d763caeb01a4640a2250a0ea899e257b1e44b9eded91e15ab \
9+
--hash=sha256:8182aec21dad6c0a49a2a3d121a87cd524b950e0b6092b181625f07ebdde7530
10+
# via -r ./requirements_os_specific.in
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#
2+
# This file is autogenerated by pip-compile with Python 3.9
3+
# by the following command:
4+
#
5+
# bazel run //:requirements_os_specific.update
6+
#
7+
pip==22.2.1 ; sys_platform == "win32" \
8+
--hash=sha256:0bbbc87dfbe6eed217beff0021f8b7dea04c8f4a0baa9d31dc4cff281ffc5b2b \
9+
--hash=sha256:50516e47a2b79e77446f0d05649f0d53772c192571486236b1905492bfc24bac
10+
# via -r ./requirements_os_specific.in

0 commit comments

Comments
 (0)