Hi, I am trying to evaluate `pants` for a mono rep...
# general
g
Hi, I am trying to evaluate
pants
for a mono repo in my project. The repository still does not have a build system yet. The requirement for me is to migrate existing libraries which are packaged using
setuptools
and
setuptools-scm
with
pyproject.toml
. I followed the documentation and created the
BUILD
file for the project. However,
pants
is not able to create a package from the source code. Below is the error that I get:
Copy code
config = read_configuration(filepath, True, ignore_option_errors, dist)
usage: backend_shim.py [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]
   or: backend_shim.py --help [cmd1 cmd2 ...]
   or: backend_shim.py --help-commands
   or: backend_shim.py cmd --help

error: invalid command 'bdist_wheel'
Can anyone help me resolve this issue?
l
Did you create one
BUILD
file, or all
BUILD
files?
g
My folder structure is like this
Copy code
.
├── my-lib
│   ├── BUILD
│   ├── lib
│   │   ├── __init__.py
│   │   └── main.py
│   └── pyproject.toml
└── pants.toml
The contents of
BUILD
is as shown below:
Copy code
resource(name="pyproject", source="pyproject.toml")

python_distribution(
    name="my-lib",
    dependencies=[
        ":pyproject",
        # Dependencies on code to be packaged into the distribution.
    ],
    provides=python_artifact(
        name="my-lib",
    ),
    # Example of setuptools config, other build backends may have other config.
    wheel_config_settings={"--build-option": ["--python-tag", "py310.py311"]},
)
I got the issue with the
bdist_wheel
I had to inlcude
wheel
in the requirements of the
build-system
. Now however I get a different error that it cannot find the package
lib
. My
pyproject.toml
is something like this.
Copy code
[build-system]
    requires = ["setuptools", "wheel"]
    build-backend = "setuptools.build_meta"

[project]
    name = "my-lib"
    authors = [
        {name = "Shravan Kulkarni"},
    ]
    description = "sample lib"
    requires-python = ">=3.10"
    classifiers = [
        "Programming Language :: Python :: 3",
    ]
    dependencies = ["numpy", "sqlalchemy"]
    version="0.0.1.dev"

[tool.setuptools]
    packages = ["lib"]
The error I get now is
Copy code
config = read_configuration(filepath, True, ignore_option_errors, dist)
error: package directory 'lib' does not exist
l
pants
requires a build in every folder. Call
pants tailor ::
this should create the missing
BUILD
file and add some required targets. Regarding the
python_distribution
target I think you should change your
weel_config_settings
to
wheel_config_settings={"--global-option": ["--python-tag", "py39"]}.
Further, try to set
generate_setup=True
if you want pants to infer information from your
pyproject.toml
.
w
Small adjustment, Pants doesn’t “require” a build file in every folder. I use recursive globs in my single BUILD file per monorepo service/project
l
Yes not require. But I would say it is somewhat of a best practice.
🤷 1
h
It is my ultimate goal to get rid of that requirement...
g
I ran
pants tailor ::
and adapted the BUILD file as mentioned above. It still gives an error that the lib package doesnt exist.
l
@happy-kitchen-89482 That would be nice, but since
tailor
does most of the work for the developer it is not to big of a pain, for me at least. Further, compared to bazel it is not even in the same league. In its current state, would you suggest using one
BUILD
file per directory, or as @wide-midnight-78598 does it one per service/project leveraging recursive globs?
@gentle-florist-96289 How does your
python_sources
target look. I think you have to add the lib package as dependency in your
python_distribution
.
g
@lemon-yak-80782 I added
:lib
in
python_distribution
as a dependency, and it worked. Thanks. @wide-midnight-78598 How to use recursive globs without creating multiple BUILD files?
l
@gentle-florist-96289 You can define
source
in the
python_sources
target. For example:
foo/**/*.py
.
g
@lemon-yak-80782 This doesn't seem to work properly. if I give sources as
["lib/**/*.py"]
and if I have nested directories inside lib, it doesn't copy them correctly in the
chroot
directory in the temp. I get the contents of the lib folder in the chroot at the same level as
pyproject.toml
and it doesn't copy the inner directories.
l
Take a look here, https://www.pantsbuild.org/docs/validating-dependencies#anchoring-mode-for-path-globs. I think anchoring the glob (
//
or
/
) should help.
g
Okay I seem to be getting a hang of the project setup. Glob is working now. However, can you tell me how to include the data files from the lib as well? Currently the
python_sources
only accepts
*.py
files. I have some
json
and
sqlite3
files that do not get packaged. I believe these files should be created as a separate source and add as a dependency in the
python_distribution
. Can you please help with this?
I was able to figure out how to include
json
data as well. 👍
l
Yes, there is a resource target.
h
The one BUILD file per directory (as implemented by tailor) is useful when you need to start overriding settings for specific packages. You can do that in higher-level build file (with the
overrides={...}
field) but it's a bit more clunky if you have many of them.
So it really depends on how complex and "overridden" your project is