Question on PEX binaries. How do arbitrary file de...
# general
l
Question on PEX binaries. How do arbitrary file dependencies get included into the Pex binary or sandbox if at all? Context: We have a bit of a nonstandard setup for creating a Pex test runner which involves invoking
pytest.main('./tests')
where it's located under
dir/
from the repo root. I tried to get this to work via
pants run dir:runner-target
but it doesn't work because pytest can't find that directory.
cd dir && pants run :runner-target
also doesn't work. So I included
files(sources=["**"])
as a dependency to the Pex target.
pants dependencies dir/tests_runner.py
shows that all files targets are includes as deps. But running the Pex still doesn't work in any case. So a ran with
--preserve-sandboxes=always
and went inside the sandbox. I see
dir/test_runner.py
instead, but nothing else in the directory.
c
As hinted at in the docs for pex-files, only python sources and resources are included, so your
files
are silently ignored.
l
Hmm... that's a bit surprising. I would think the Pex itself might exclude this, but the sandbox should still respect the files passed to it as deps
e.g. IIRC Bazel behaves this way with all explicit deps
c
see https://www.pantsbuild.org/docs/assets#when-to-use-each-asset-target-type for more on the files vs resources in pants.
if you tell pants to run a pex, only the files used by that pex will be included..
l
But what if you just need an asset like an SVG file?
c
if you want it bundled up in the built pex, you’ll need a
resource
target for it.
👍 1
and it needs to be within a source root of the code being packaged
l
It doesn't seem to work. Inside the sandbox it still looks like this even with
resources(name="test_files", sources=["**"])
Copy code
❯ tree
.
├── __run.sh
├── dir
│   ├── __pycache__
│   │   └── tests_runner.cpython-311.pyc
│   └── tests_runner.py
├── root.pex
│   ├── PEX-INFO
│   ├── __main__.py
│   └── __pex__
│       └── __init__.py
├── root.pex_bin_python_shim.sh
└── root.pex_pex_shim.sh
The source root patterns is
/dir
in this case
It seems that a workaround is to include
dir/tests:tests
as a dependency and then run
pytest.main("./e2e_testing/tests")
from the test runner. I actually think this is a better solution than using resources. I'll keep it this way, although I think the behavior of resources / files here is not what you'd expect.