Hello I'm testing pants with my repo containing so...
# general
r
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?
e
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
pyproject.toml
would do it.
r
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
python_tests(
    name="tests"
)
on py/projects/standalone/tests/BUILD pants can find the tests but can't import what is in src.
e
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.
r
tailor just put a BUILD in standalone/tests but nothing else 😕
e
Can you share your complete
pants.toml
?
r
Copy code
[GLOBAL]
pants_version = "2.12.0+git5d31cdb5"
backend_packages = [
    "pants.backend.python",
    "pants.backend.python.lint.black",
    "pants.backend.python.lint.flake8",
    "pants.backend.python.typecheck.mypy"
]

[anonymous-telemetry]
enabled = false

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

[python]
interpreter_constraints = ["CPython==3.10.*"]
On the shell I get
Copy code
%./pants roots
.
py/libraries/libtutu/src
py/projects/standalone/src
py/projects/tata/src

%./pants tailor
Created py/projects/standalone/tests/BUILD:
  - Add python_tests target tests
e
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
 
 [python]
 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
__init__.py
.
r
ah ok 🙂 The results are pretty simple
python_sources()
is it ok or do you see particular options I should set in general?
e
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.
r
Ok. If I need a 3rd party library, should I declare it manually in BUILD? Like with
python_requirement
?
e
It's up to you. Some folks use a Pip-style
requirements.txt
and a BUILD file next to it with
python_requirements()
, which is a macro that reads in all the
requirements.txt
requirements and creates a `python_requirement`for each for you. Likewise
poetry_requirements()
in a BUILD file next to
pyproject.toml
for Poetry-based projects, If you don't already use a requirements.txt though manually declaring in a BUILD file is fine.
r
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!
e
Yeah, we should definitely add support for PEP-621
dependencies
and probably
requires-python
too (for setting various
interpreter_constraints
options). You're welcome.
PEP-621 dependency support tracking: https://github.com/pantsbuild/pants/issues/16289
r
cool, thanks!