Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

requirements.update doesn't work on windows when requirements.in in a subpackage #1431

Closed
carpenterjc opened this issue Sep 26, 2023 · 2 comments · Fixed by #2457
Closed

Comments

@carpenterjc
Copy link

carpenterjc commented Sep 26, 2023

Affected Rule

The issue is caused by the rule: compile_pip_requirements

Is this a regression?

Yes this regressed between: 0.18.0...0.19.0

Description

If you have a rule in a sub package (so it contains a directory separator) then on windows you will get an issue where it cannot find file path for requirements.txt.

Note: the slashes are the wrong direction for windows.

🔬 Minimal Reproduction

In 0.25.0 branch of rules_python


cd .\examples\pip_parse\
REM This works
bazel run requirements.update

mkdir bad
copy .\BUILD.bazel bad
copy .\requirements.in bad
copy .\requirements_lock.txt bad

REM This fails with Error: Could not open file 'bad/requirements_lock.txt': No such file or directory
bazel run //bad:requirements.update

🔥 Exception or Error



INFO: Running command line: bazel-bin/tools/thirdparty/requirements.update.exe tools/thirdparty/requirements.in tools/thirdparty/requirements.txt None None None //tools/thirdparty:requirements.update --allow-unsafe --no-reuse-hashes
Updating tools/thirdparty/requirements.txt
Traceback (most recent call last):
  File "C:\Users\JAMESC~1\AppData\Local\Temp\Bazel.runfiles_gwh6jf4s\runfiles\rules_python\python\pip_install\tools\dependency_resolver\dependency_resolver.py", line 139, in 
    if not os.path.samefile(requirements_txt, requirements_txt_tree):
  File "C:\Users\JAMESC~1\AppData\Local\Temp\Bazel.runfiles_gwh6jf4s\runfiles\python3_10_x86_64-pc-windows-msvc\lib\genericpath.py", line 100, in samefile
    s1 = os.stat(f1)
FileNotFoundError: [WinError 3] The system cannot find the path specified: 'tools/thirdparty/requirements.txt'

🌍 Your Environment

Operating System:

  
windows 
  

Output of bazel version:

  
Build label: 6.2.0
Build target: bazel-out/x64_windows-opt/bin/src/main/java/com/google/devtools/build/lib/bazel/BazelServer_deploy.jar
Build time: Tue May 9 18:34:17 2023 (1683657257)
Build timestamp: 1683657257
Build timestamp as int: 1683657257
  

Rules_python version:

  
0.25.0
  

Anything else relevant?
Same code works fine on macos and WSL.

@pat-jpnk
Copy link

pat-jpnk commented May 6, 2024

I just experienced this bug as well.

I moved a project written on from Ubuntu Linux 22.04 to a Windows 10 machine. I wanted to test if calling "bazel run requirements.update" without the requirements_lock.txt file existing will result in Error in read: java.io.FileNotFoundException. It does. When I tried to re-create the file and run the command again, I got the following error:

Starting local Bazel server and connecting to it...
INFO: Invocation ID: 346f2cac-30e8-46be-a367-3b4e5ad0e2f5
INFO: Analyzed target //:requirements.update (61 packages loaded, 4166 targets configured).
INFO: Found 1 target...
Target //:requirements.update up-to-date:
  bazel-bin/requirements.update.zip
  bazel-bin/requirements.update.exe
INFO: Elapsed time: 164.462s, Critical Path: 10.80s
INFO: 7 processes: 5 internal, 2 local.
INFO: Build completed successfully, 7 total actions
INFO: Running command line: bazel-bin/requirements.update.exe pip_example/pypi/requirements.in pip_example/pypi/requirements_lock.txt //:requirements.update '--resolver=backtracking' --allow-unsafe --generate-hashes
Updating pypi/requirements_lock.txt
WARNING: --strip-extras is becoming the default in version 8.0.0. To silence this warning, either use --strip-extras to opt into the new default or use --no-strip-extras to retain the existing behavior.
Error: Could not open file 'pypi/requirements_lock.txt': No such file or directory

I thought it was an encoding/character set issue, but even copying the original code from the folder and trying to run it does not work.

In my project, the "requirements.in" and "requirements_lock.txt" files were both in a subfolder of the root directory. When I moved them to the root directory, it worked without any errors.

I am using Bazel version 7.1.1 and rules_python version 0.31.0 on both machines.

@bhogan-bdai
Copy link

This is old, but leaving this here for future folks.

I also ran into this issue. When I enabled symlink support doc, the step started working because it could find the output lock file.

.bazelrc

startup --windows_enable_symlinks
build --enable_runfiles

github-merge-queue bot pushed a commit that referenced this issue Dec 1, 2024
… and --windows_enable_symlinks is used (#2457)

`compile_pip_requirements` doesn't work as expected on Windows unless
both `--enable_runfiles` and `--windows_enable_symlinks` are used. Both
options default to off on Windows because the filesystem on Windows
makes setting up the runfiles directories with actual files very slow.
This means that anyone on Windows with a default set up has to search
around the Github issues to try and figure out why things don't work as
advertised.

The `dependency_resolver.py` doesn't inherently require these options,
it just had two bugs that prevented it from working.

1. calling pip_compile exits the whole program so it never gets to run
the code that should copy the output to the source tree. Things just
happen to work on linux because the runfiles are symlinks, and it does
not need to copy anything.
2. it assumed the `runfiles` resolved file would be in the runfiles
tree, but on Windows, when `--enable_runfiles` is not set, it actually
gets resolved to a file in the source tree.

Before:
```sh
bazel run //third_party/python:requirements.update
Starting local Bazel server and connecting to it...
INFO: Invocation ID: 8aa3e832-78ce-4999-912b-c43e7ca3212b
INFO: Analyzed target //third_party/python:requirements.update (129 packages loaded, 9563 targets configured).
INFO: Found 1 target...
Target //third_party/python:requirements.update up-to-date:
  bazel-bin/third_party/python/requirements.update.zip
  bazel-bin/third_party/python/requirements.update.exe
INFO: Elapsed time: 60.964s, Critical Path: 0.77s
INFO: 8 processes: 2 remote cache hit, 6 internal.
INFO: Build completed successfully, 8 total actions
INFO: Running command line: bazel-bin/third_party/python/requirements.update.exe '--src=_main/third_party/python/requirements.txt' _main/third_party/python/requirements_lock.txt //third_party/python:requirements.update '--resolver=backtracking' --allow-unsafe --generate-hashes '--requirements-windows=_main/third_party/python/requirements_windows.txt' --strip-extras
Updating third_party/python/requirements_windows.txt
Error: Could not open file 'third_party/python/requirements_windows.txt': No such file or directory
```

After:
```sh
bazel run //third_party/python:requirements.update
INFO: Invocation ID: 39f999a0-6c1d-4b2c-a1be-3d71e838916a
INFO: Analyzed target //third_party/python:requirements.update (5 packages loaded, 45 targets configured).
INFO: Found 1 target...
Target //third_party/python:requirements.update up-to-date:
  bazel-bin/third_party/python/requirements.update.zip
  bazel-bin/third_party/python/requirements.update.exe
INFO: Elapsed time: 5.410s, Critical Path: 4.79s
INFO: 2 processes: 1 internal, 1 local.
INFO: Build completed successfully, 2 total actions
INFO: Running command line: bazel-bin/third_party/python/requirements.update.exe '--src=_main/third_party/python/requirements.txt' _main/third_party/python/requirements_lock.txt //third_party/python:requirements.update '--resolver=backtracking' --allow-unsafe --generate-hashes '--requirements-windows=_main/third_party/python/requirements_windows.txt' --strip-extras
Updating third_party/python/requirements_windows.txt
#
# This file is autogenerated by pip-compile with Python 3.13
# by the following command:
#
#    bazel run //third_party/python:requirements.update
#
mpmath==1.3.0 \
    --hash=sha256:7a28eb2a9774d00c7bc92411c19a89209d5da7c4c9a9e227be8330a23a25b91f \
    --hash=sha256:a0b2b9fe80bbcd81a6647ff13108738cfb482d481d826cc0e02f5b35e5c88d2c
    # via sympy
sympy==1.13.3 \
    --hash=sha256:54612cf55a62755ee71824ce692986f23c88ffa77207b30c1368eda4a7060f73 \
    --hash=sha256:b27fd2c6530e0ab39e275fc9b683895367e51d5da91baa8d3d64db2565fec4d9
    # via -r G:/projects/bedrock-engine/third_party/python/requirements.txt
```
And `//third_part/python:requirements_windows.txt` is updated.

Fixes #1943 
Fixes #1431

---------

Co-authored-by: Ignas Anikevicius <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants