Source roots
Configuring Pants to understand your imports.
Go does have a notion of source roots: where your go.mod
is located. However, that is handled automatically by Pants without you needing to follow this page.
Shell does not have any notion of source roots.
What are source roots?
Some project layouts use top-level folders for namespace purposes, but have the code live underneath. However, the code's imports will ignore these top-level folders, thanks to mechanisms like the $PYTHONPATH
and the JVM classpath. Source roots are a generic equivalent of these concepts.
For example, given this Python project:
src
└── python
└── project
├── __init__.py
├── app.py
├── config
│ ├── __init__.py
│ └── prod.json
└── util
├── __init__.py
└── math.py
You would likely set PYTHONPATH=src/python
and use imports like this:
from project.app import App
from project.util.math import add_two
pkgutil.get_data("project.config", "prod.json")
In the example above, src/python
is a source root. So, when some code says from project.app import App
, Pants can know that this corresponds to the code in src/python/project/app.py
.
Configuring source roots
There are two ways to configure source roots:
- Using patterns
- Using marker files
You can mix and match between both styles. Run pants roots
to see what Pants is using:
pants roots
src/assets
src/python
src/rust
Configuring source roots using patterns
You can provide a set of patterns that match your source roots:
[source]
root_patterns = [
'/src/python',
'/test/python',
]
The /
prefix means that the source root is located at the build root, so it will match src/python
, but not project1/src/python
.
You can leave off the /
prefix to match any directory whose suffix matches a pattern. For example, root_patterns = ["src/python"]
would consider all of these to be source roots, if they exist:
src/python
project1/src/python
You can use *
as a glob. For example, root_patterns = ["/src/*"]
would consider all of these to be source roots:
src/python
src/java
src/assets
Configuring no source roots
Many projects do not have any top-level folders used for namespacing.
For example, given this Python project:
project
├── __init__.py
├── app.py
├── config
│ ├── __init__.py
│ └── prod.json
└── util
├── __init__.py
└── math.py
You would likely not set PYTHONPATH
and would still use imports like this:
from project.app import App
from project.util.math import add_two
pkgutil.get_data("project.config", "prod.json")
If you have no source roots, use this config:
[source]
root_patterns = ["/"]
The default value of the root_patterns
config key is ["/", "src", "src/python", "src/py", "src/java", "src/scala", "src/thrift", "src/protos", "src/protobuf"]
.
These capture a range of common cases, including a source root at the root of the repository. If your source roots match these patterns, you don't need to explicitly configure them.
Configuring source roots using marker files
You can also denote your source roots using specially-named marker files. To do so, first pick a name (or multiple names) to use:
[source]
marker_filenames = ["SOURCE_ROOT"]
Then, place a file of that name in each of the source roots. The contents of those files don't matter. They can be empty.
For example, given this Python repo, where we have a setup.py
for each distinct project:
.
├── server