Hello, I am trying to run tests for the first tim...
# general
f
Hello, I am trying to run tests for the first time using pants.
Copy code
src
├── projects
│   └── one_shot
│       ├── one_shot              <------- source code
│       ├── requirements.txt
│       └── test_one_shot         <------- test code, requires one_shot and sail to be installed
└── sail                          <------- old source code
Running
./pants test  ./src/projects/one_shot/test_one_shot/test_file.py
will fail because the
sail
package is not installed. How can I specify this dependency on first-party code in a BUILD file ?
1
c
Hi Thomas, If you depend on
sail
using normal imports, you should not have to specify that dependency, it will be inferred.
The first thing to establish is if your source roots are setup properly, so that your package names are what you expect them to be. https://www.pantsbuild.org/docs/source-roots
f
Hi Andreas, thanks for the quick reply. I did specify my source roots.
Copy code
>> ./pants roots
src/projects/one_shot/one_shot
src/projects/one_shot/test_one_shot
src/sail
But
sail
is not detected.
when running a test within
test_one_shot
Copy code
src/projects/one_shot/test_one_shot/test_anchor.py:7: in <module>
    from sail.one_shot import something
E   ModuleNotFoundError: No module named 'sail'
c
So, for
sail
to be picked up as a module, I think you want one of your source roots to be
src
there..
f
Interesting. I have a different error now that seems to be related to the internals of sail. So this is progress. I do not understand why I have to specify
src/
as root and not
src/sail
.
There are other subdirectories in
src
that I do not wish to use as source code
c
If your root is
src/sail
then the files in that folder won’t know they belong to
sail
(as that is not part of the package/module name)
Ah, not sure how to slice through that, will have to defer a few hours until the US based teammates wakes up..
f
I thought the
sail/BUILD
file would be enough to specify the module name.
I would like to know how to solve this but I can work with your solution ! thanks
c
The module name is solely based on the path from your source root.
The
name
of your
python_sources
is just to give your target an address to refer to in the Pants world. And for
python_distribution
the name in the
python_artifact
(if you use that) is for the python distribution name, and does not affect the runtime characteristics either.
If you don’t have BUILD files in those other folders under
src
that you don’t want to include, and tell
tailor
to skip them (if you use Tailor to setup BUILD files for you), Pants should not pick up any sources from those folders. So should be ok, I think.
f
That would make sense.
👍 1
c
Let us know how you come along, in case you get more questions, or if it just works 😉
f
I will
👌 1
h
Yep, a source root is a root of the package hierarchy. So, for example, if you
from sail.one_shot import something
then you want the parent of
sail
to be a source root
We should probably attempt to auto-detect those, since it is a source of occasional confusion
As Andreas stated - Pants won't use everything under a source root, it uses whatever is covered by BUILD files. The source roots are just to tell it where imports start from.
h
We should probably attempt to auto-detect those
At least via
./pants tailor
and update
pants.toml
h
Yes
So there are two separate concepts here: "Where do imports start from?" (answer: source roots) and "which source files does pants look at/use?" (answer: The ones that are owned by BUILD file targets).
👍 1
c
For debugging/introspection it could possibly also be useful to have a way to get at what module a certain python source file gets, with the given set of source roots. In this case, you could’ve done something like:
Copy code
$ ./pants tell-me-my-module-name src/sail/one_shot.py
one_shot  # using source root `src/sail`