Hello I'm testing pants with my repo containing so...
# general
Hello I'm testing pants with my repo containing some Python code and I have troubles to figure out what to put as source roots and where to place BUILD files. ./pants tailor doesn't find anything. My projects have a dir structure with a src and tests directory and I didn't find any examples on the doc with that. It looks like that:
Copy code
├── pants
├── pants.toml
└── py
    ├── libraries
    │   └── libtutu
    │       ├── LICENSE
    │       ├── pyproject.toml
    │       ├── README.md
    │       ├── setup.cfg
    │       └── src
    │           └── libtutu
    │               └── __init__.py
    └── projects
        ├── tata
        │   ├── LICENSE
        │   ├── pyproject.toml
        │   ├── README.md
        │   ├── setup.cfg
        │   └── src
        │       ├── tata
        │           └── __init__.py
        └── standalone
            ├── LICENSE
            ├── pyproject.toml
            ├── README.md
            ├── setup.cfg
            ├── src
            │   ├── standalone
            │       └── __init__.py
            └── tests
                └── test_dummy.py
Does anyone have an example I could follow for when you have the src/ and tests/ directory?
For Python, source roots == PYTHONPATH. So the answer is whatever the PYTHONPATH would need to be (without installing any projects) to
import tata
(for example). So, it looks like your source roots are
["py/libraries/libtutu/src", "py/projects/tata/src", "py/projects/standalone/src"]
For many common Python / pytest setups, you don't need to include source roots for tests; so you might just start with the source roots above.
In your case though, it's probably easiest to configure a source root marker file: https://www.pantsbuild.org/docs/source-roots#configuring-source-roots-using-marker-files It looks like
would do it.
Hum, if I set marker_filenames then the source root is one level before src, is that a problem? Then I'm not sure where to place the BUILD files and what to place inside. For example, if I just place
Copy code
on py/projects/standalone/tests/BUILD pants can find the tests but can't import what is in src.
Ah, yeah. I'm sorry, bad advice. Marker filenames won't work; so just list them out like in my 1st example. After you have the source roots configured, try
./pants tailor
again and just go with the BUILD files it generates unless you have some strong objections or things still aren't working.
tailor just put a BUILD in standalone/tests but nothing else 😕
Can you share your complete
Copy code
pants_version = "2.12.0+git5d31cdb5"
backend_packages = [

enabled = false

#marker_filenames = ["pyproject.toml"]
#root_patterns = ["/"]

interpreter_constraints = ["CPython==3.10.*"]
On the shell I get
Copy code
%./pants roots

%./pants tailor
Created py/projects/standalone/tests/BUILD:
  - Add python_tests target tests
Ok, thanks. I'm poking around a bit here using that.
I repro. Your example repo was just a bit too anemic for tailor:
Copy code
^jsirois@gill /tmp/debug (main *) $ git diff
diff --git a/pants.toml b/pants.toml
index ffb58b9..1a1e030 100644
--- a/pants.toml
+++ b/pants.toml
@@ -16,4 +16,4 @@ enabled = false
 interpreter_constraints = ["CPython==3.10.*"]
+tailor_ignore_solitary_init_files = false
^jsirois@gill /tmp/debug (main *) $ ./pants tailor
17:18:31.51 [INFO] Initializing scheduler...
17:18:31.61 [INFO] Scheduler initialized.
Created py/libraries/libtutu/src/libtutu/BUILD:
  - Add python_sources target libtutu
Created py/projects/standalone/src/standalone/BUILD:
  - Add python_sources target standalone
Created py/projects/tata/src/tata/BUILD:
  - Add python_sources target tata
If you had actual code in there, it would have worked. But since not yet, I turned on this option: https://www.pantsbuild.org/docs/reference-python#section-tailor-ignore-solitary-init-files
You'll probably want to turn that option back off later. I assume you'll actually have real modules and sub-packages later and not just solitary
ah ok 🙂 The results are pretty simple
is it ok or do you see particular options I should set in general?
It looks Ok. The simple BUILD files are a reflection of where we're headed, which is no BUILD files unless you need to actually tell Pants something particular about your code. These ~empty BUILD files are just a reflection of the past.
Ok. If I need a 3rd party library, should I declare it manually in BUILD? Like with
It's up to you. Some folks use a Pip-style
and a BUILD file next to it with
, which is a macro that reads in all the
requirements and creates a `python_requirement`for each for you. Likewise
in a BUILD file next to
for Poetry-based projects, If you don't already use a requirements.txt though manually declaring in a BUILD file is fine.
Well, I use either a pyproject.toml but more the PEP621 version than poetry, I don't know if there is a difference. Otherwise, I use setup.cfg + setuptools. I have a requirements.txt but this is usually for the dev dependencies like pytest or coverage.
Looks very different 🙂
Ok I think I have a good starting point now. Thanks a lot for your help!
Yeah, we should definitely add support for PEP-621
and probably
too (for setting various
options). You're welcome.
PEP-621 dependency support tracking: https://github.com/pantsbuild/pants/issues/16289
cool, thanks!