Q. When I run `./pants package` with a `python_dis...
# general
r
Q. When I run
./pants package
with a
python_distribution("dist")
target in
src/ai/backend/common/BUILD
which depends on a
python_sources("lib")
target in the same file which depends on a
python_sources("stubs")
target in
stubs/trafaret/BUILD
, it says "No python_distribution target found to own stubs/trafaret/__init__.pyi:stubs". Shouldn't it be included in the original
src/ai/backend/common:dist
target transitively? I don't want to make a separate package for the type stubs but just embed it inside the
src/ai/backend/common:dist
package. How do I avoid the error without changing the directory structure?
Note that it just works fine with
./pants check
.
c
Package will not include sources that is not in the subtree of the distribution I believe.
r
Then.. can I just omit the stubs?
c
Yes, you can exclude dependencies with
!tgt
And transistive ones with
!!tgt
See “Mapping source files to distributions” in Python/Building distributions docs (can't get proper link on mobile :/ ) And the notes under “dependencies field” from Key concepts/targets and BUILD files.
See related issue (but for pex binary targets) https://github.com/pantsbuild/pants/issues/15454
r
hmm
Excluding the dependency using "!!" seems to make it working, but another error arises:
Copy code
NoSourceRootError: No source root found for `.`
my source roots are
src
,
tests
,
stubs
,
tools/pants-plugins
h
For background: When you rely on Pants to generate a setup.py for you, it has to decide which of your sources to put in the distribution, vs which ones to depend on as an external requirement. This is important because we don't want to put the same source file in multiple distributions, as that can lead to runtime loading problems. The algorithm Pants uses to decide which sources go into a distribution is described here: https://www.pantsbuild.org/docs/python-distributions#mapping-source-files-to-distributions
👀 1
And a consequence of that algorithm is that a python_distribution can only contain sources that are next to or below that BUILD file in the filesystem.
r
I'll take a look at (now I'm on a business meeting..)
h
stubs/trafaret/BUILD
is not under
src/ai/backend/common
, so it cannot be in that distribution
Your options are:
1) Move code around (I understand that you don't want to) 2) Publish stubs/trafaret in a separate python_distribution 3) Exclude the stubs dependency, which you shouldn't need at runtime anyway 4) Don't rely on an autogenerated setup.py, and instead write one manually. Then that algorithm doesn't come into play.
It sounds like 3) is the right answer for your case
👍 1
Using the
!!
to exclude sounds like the way to go
r
image.png
yes, i did that
h
Will need to figure out why you're getting this new error, and if it's related
r
but it says the no source root found error
h
Can you post the full stack trace (run with --print-stacktrace)
r
Copy code
Engine traceback:
  in select
  in pants.core.goals.package.package_asset
  in pants.backend.python.goals.setup_py.package_python_dist (src/ai/backend/common:dist)
  in pants.source.source_root.get_source_roots
Traceback (most recent call last):
  File "/home/joongi/workspace/pants/src/python/pants/engine/internals/selectors.py", line 705, in native_engine_generator_send
    res = func.send(arg)
  File "/home/joongi/workspace/pants/src/python/pants/source/source_root.py", line 249, in get_source_roots
    raise NoSourceRootError(path)
pants.source.source_root.NoSourceRootError: No source root found for `.`. See <https://www.pantsbuild.org/v2.13/docs/source-roots> for how to define source roots.
Copy code
[source]
root_patterns = ["src", "stubs", "tests", "tools/pants-plugins"]
Adding
/
to the
root_patterns
seems to resolve the issue, but i get the missing
python_distribution
error when executing
./pants package ::
because
stubs
directory has
python_sources()
without
python_distribution()
. How can I let pants "ignore" it?
hmm
it turns out that i have to add
!!stubs/trafaret:stubs
to all existing
python_distribution()
declarations
how can i reduce this duplication?