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

[Bug]: The tests parent directory (".") is added to the PYTHONPATH when using py_pytest_main #479

Open
keunhong opened this issue Dec 12, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@keunhong
Copy link

What happened?

When using py_pytest_main the target's parent directory is added to the PYTHONPATH causing the same issues as in #368.

Version

Development (host) and target OS/architectures:

Output of bazel --version: bazel 8.0.0

Version of the Aspect rules, or other relevant rules from your
WORKSPACE or MODULE.bazel file:

bazel_dep(name = "rules_python", version = "1.0.0")
bazel_dep(name = "aspect_rules_py", version = "1.0.0", dev_dependency = True)

python_version = "3.10"
python = use_extension("@rules_python//python/extensions:python.bzl", "python")
python.toolchain(
    configure_coverage_tool = True,
    is_default = True,
    python_version = python_version,
)
use_repo(python, "python_3_10", "python_versions", "pythons_hub")

Language(s) and/or frameworks involved: python

How to reproduce

py_pytest_main(
    name = "__test__",
    deps = ["@pip//pytest"],
)

py_test(
    name = "my_test",
    srcs = ["my_test.py", "//:__test__"],
    main = "__test__.py",
    deps = [
        "//:__test__",
        "@pip//pytest",
    ],
)

If my_test.py has an import for e.g. concurrent.futures and there is a concurrent.py in the same directory it will cause a clash.



### Any other information?

_No response_
@keunhong keunhong added the bug Something isn't working label Dec 12, 2024
@keunhong
Copy link
Author

This seems to be because pytest adds the test directory to sys.path through the mechanism described here: https://docs.pytest.org/en/latest/explanation/pythonpath.html#pythonpath

Setting --import-mode=append fixes this.

@alexeagle
Copy link
Member

Cool, thanks for tracking down the culprit and a workaround. Do you think rules_py should include that flag for all users of py_pytest_main? Document its existence?

@keunhong
Copy link
Author

Cool, thanks for tracking down the culprit and a workaround. Do you think rules_py should include that flag for all users of py_pytest_main? Document its existence?

I think append is a less surprising default, but I haven't considered all of the use cases. I think at least documenting it would be nice so no one has to go on a wild goose chase like I had to.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants