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

pypi libraries installed to system python are implicitly available to builds #27

Closed
alecbz opened this issue Oct 12, 2017 · 19 comments
Closed

Comments

@alecbz
Copy link

alecbz commented Oct 12, 2017

It looks like any libraries installed to my system python's site-packages are available to bazel, whether or not they are expressed as a dependency to bazel.

This seems like an issue from a reproducible build standpoint. I.e.: it'd be easy to forget to include an external library in bazel, and create implicit dependencies for others using your BUILD files or for any par files or similar archives you build.

@mattmoor
Copy link
Contributor

I believe this is a problem with the Bazel core rules, and maybe google/subpar too. @damienmg @duggelz

@damienmg
Copy link

It is a consequence of us trying to make it easy for people to use Bazel on their system. Specifying --experimental_use_strict_env should do the trick.

@mattmoor
Copy link
Contributor

Hmm, after watching this last night, I'm not so sure it is that simple (unless you guys reimplemented virtualenv?).

@mattmoor
Copy link
Contributor

@damienmg Can you move this to the main repo or assign this to the appropriate Bazel owner?

@alecbz
Copy link
Author

alecbz commented Oct 12, 2017

@damienmg --experimental_use_strict_env doesn't seem to exist AFAICT? My bazel version is:

$ bazel version
Build label: 0.6.1-homebrew
Build target: bazel-out/darwin_x86_64-opt/bin/src/main/java/com/google/devtools/build/lib/bazel/BazelServer_deploy.jar
Build time: Fri Oct 6 02:36:58 2017 (1507257418)
Build timestamp: 1507257418
Build timestamp as int: 1507257418

Is it a new flag? I also don't see it in the source though? https://github.com/bazelbuild/bazel/search?utf8=%E2%9C%93&q=experimental_use_strict_env&type= (unless there's a different repo where flags get defined?)

@lucaskolstad
Copy link

I believe he may have meant "--experimental_strict_action_env" rather than "--experimental_use_strict_env"?

It is listed in the CLI reference.

@drigz
Copy link
Contributor

drigz commented Nov 8, 2017

With 0.7.0, --experimental_strict_action_env doesn't work for me, but activating an empty virtualenv before bazel run does:

> echo > WORKSPACE
> cat BUILD.bazel 
py_binary(
    name = "main",
    srcs = ["main.py"],
)
> cat main.py 
import google
print google.__path__
> bazel run :main
[SNIP: build output]
['/usr/local/lib/python2.7/dist-packages/google']
> bazel run --experimental_strict_action_env :main
[SNIP: build output]
['/usr/local/lib/python2.7/dist-packages/google']
> virtualenv -p python2 empty_env
> source empty_env/bin/activate
(empty_env) > bazel run :main
[SNIP: build output]
Traceback (most recent call last):
  File "/usr/local/google/home/rodrigoq/.cache/bazel/_bazel_rodrigoq/bca9229df5db49ddbafab09d12924513/execroot/__main__/bazel-out/local-fastbuild/bin/main.runfiles/__main__/main.py", line 1, in <module>
    import google
  File "/usr/local/buildtools/current/sitecustomize/sitecustomize.py", line 152, in SetupPathsAndImport
    return real_import(name, globals, locals, fromlist, level)
ImportError: No module named google
ERROR: Non-zero return code '1' from command: Process exited with status 1

@evanj
Copy link

evanj commented Dec 22, 2017

My recollection is that PEX removes site-packages from sys.path in an attempt to avoid this problem. In theory, it only uses the "standard library" from the Python interpreter, and assumes the PEX zip contains all other required Python libraries. See minimum_sys_path for the weirdness it does: https://github.com/pantsbuild/pex/blob/39bcfaaa1e64c7edd57c8514c8086e624dbd1c56/pex/pex.py#L191

I suspect subpar could probably include a similar mechanism which would probably resolve this.

@duggelz
Copy link

duggelz commented Feb 16, 2018

As a longer-term fix, these rules could start creating virtualenv trees, instead of or alongside the runfiles trees, which isolate a Python interpreter and its standard library from other third-party packages.

@alecbz
Copy link
Author

alecbz commented Oct 27, 2018

Would it be any easier to check at build-time for any undeclared dependencies, instead of modifying the artifact to not have access to them at run-time?

I think pants does the latter via the lint goal, by creating a virtualenv and importing a file to in turn run all of that file's imports (though any non-file-level imports would get missed I imagine), in addition to pexes not having access to undeclared dependencies per @evanj's comment.

@NathanHowell
Copy link

@duggelz @AlecBenzer it looks like this is possible using Python toolchains, see https://gist.github.com/NathanHowell/5cf4a353a8dd3a1025e682c4707d5bac for an example

@pstradomski
Copy link
Contributor

pstradomski commented Nov 12, 2019 via email

@NathanHowell
Copy link

@pstradomski yeah I can do that, I have a couple other things I want to add to the poetry rules too so maybe I'll drop it in that repo later this week

alexeagle pushed a commit to alexeagle/rules_python that referenced this issue Aug 19, 2020
@github-actions
Copy link

This issue has been automatically marked as stale because it has not had any activity for 180 days. It will be closed if no further activity occurs in 30 days.
Collaborators can add an assignee to keep this open indefinitely. Thanks for your contributions to rules_python!

@github-actions github-actions bot added the Can Close? Will close in 30 days if there is no new activity label Apr 29, 2021
@thundergolfer
Copy link

Don't close. This is still a problem when using the non-hermetic default Python toolchain. When toolchain support is better supported here was can document the problem with the default toolchain, direct users at a sane way create a hermetic toolchain, and then close this issue.

@github-actions github-actions bot removed the Can Close? Will close in 30 days if there is no new activity label May 11, 2021
@github-actions
Copy link

github-actions bot commented Nov 8, 2021

This issue has been automatically marked as stale because it has not had any activity for 180 days. It will be closed if no further activity occurs in 30 days.
Collaborators can add an assignee to keep this open indefinitely. Thanks for your contributions to rules_python!

@github-actions github-actions bot added the Can Close? Will close in 30 days if there is no new activity label Nov 8, 2021
@thundergolfer thundergolfer self-assigned this Nov 9, 2021
@thundergolfer thundergolfer removed the Can Close? Will close in 30 days if there is no new activity label Nov 9, 2021
@hrfuller
Copy link
Contributor

It is possible to avoid loading site-packages using python -S but we would have to upstream a change like that to the bazelbuild python stub script.

@chrislovecnm
Copy link
Collaborator

What is the status on this issue?

@groodt groodt removed the untriaged label Aug 24, 2024
@groodt
Copy link
Collaborator

groodt commented Aug 24, 2024

This is very stale. Closing.

@groodt groodt closed this as completed Aug 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests