Hi all! :wave: I'm new to pants and currently play...
# general
m
Hi all! 👋 I'm new to pants and currently playing around with it for a side project. Got a very newb question that probably has to do with source roots, but for the life of me I cannot figure out what's going wrong. So basically, I have a directory structure like this:
Copy code
├── dags
    ├── capstone
        ├── pyproject.toml
        ├── src
            ├── capstone
                ├── __init__.py
                ├── ...
    ├── delt
        ├── pyproject.toml
        ├── src
            ├── delt
                ├── __init__.py
                ├── ...
        ├── tests
            ├── test_api.py
├── pants.toml
├── ...
Both 'capstone' and 'delt' are poetry projects. The BUILD files have been created and appear correct. My root patterns are defined as:
Copy code
[source]
root_patterns = [
  "/dags/delt",
  "/dags/capstone"
]
When I run
pants test ::
, I get the following error:
Copy code
E   importlib.metadata.PackageNotFoundError: delt
- generated xml file: /private/var/folders/bw/mm734qgd40g3tb30ncw5gwvc0000gp/T/pants-sandbox-bZS8XK/dags.delt.tests.test_api.py.xml -
=========================== short test summary info ============================
ERROR dags/delt/tests/test_api.py
!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!
=============================== 1 error in 0.13s ===============================
What's going wrong here? Like I said, I suspect it's the root pattern, but I can't figure out how I should configure it properly
1
c
Hi Jasper and welcome to the Pants community! 👋 I think you want your source root to be
"src"
or
"dags/delt/src"
etc.. the source root is the root directory for your python modules. I would assume that neither
src/
nor
tests/
are part of your module names.. so they shouldn’t be children to a source root, if that makes sense.
m
Hi Andreas. Thanks for your reply. I tried changing the source roots to
Copy code
[source]
root_patterns = [
  "src",
  "tests"
]
Without adding 'tests', I see:
Copy code
22:48:21.25 [ERROR] 1 Exception encountered:

Engine traceback:
  in `test` goal

NoSourceRootError: No source root found for `dags/delt/tests/test_api.py`. See <https://www.pantsbuild.org/v2.18/docs/source-roots> for how to define source roots.
But, when I add 'tests' to the root_patterns, I still get
Copy code
E   importlib.metadata.PackageNotFoundError: delt
- generated xml file: /private/var/folders/bw/mm734qgd40g3tb30ncw5gwvc0000gp/T/pants-sandbox-hU4cRR/dags.delt.tests.test_api.py.xml -
=========================== short test summary info ============================
ERROR dags/delt/tests/test_api.py
!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!
=============================== 1 error in 0.15s ===============================
Ah I found the error. I was using importlib to resolve the package version, but of course it's not installed. So looking at the full stack trace I see that that's hwere the problem is
Copy code
/Users/user/.pyenv/versions/3.9.5/lib/python3.9/importlib/__init__.py:127: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
dags/delt/tests/test_api.py:1: in <module>
    from delt.api import _join_endpoint_to_base_url
dags/delt/src/delt/__init__.py:3: in <module>
    __version__ = metadata.version("delt")
/Users/user/.pyenv/versions/3.9.5/lib/python3.9/importlib/metadata.py:551: in version
    return distribution(distribution_name).version
/Users/user/.pyenv/versions/3.9.5/lib/python3.9/importlib/metadata.py:524: in distribution
    return Distribution.from_name(distribution_name)
/Users/user/.pyenv/versions/3.9.5/lib/python3.9/importlib/metadata.py:187: in from_name
    raise PackageNotFoundError(name)
E   importlib.metadata.PackageNotFoundError: delt
Thanks for your help!
👍 1