I’m trying to figure out an error I’m getting when...
# general
f
I’m trying to figure out an error I’m getting when I try to run a program using generated code. “NoSourceRootError: No source root found for
comanage
.” This is a directory in which I’m generating code from an openapi spec. The directory is at the top level of the repo and looks like
Copy code
comanage/
├── BUILD
├── coreapi
│   └── src
│       └── python
│           ├── coreapi_client
│           └── coreapi_client_README.md
└── coreapi.yaml
This is similar to what I’ve done successfully for other package code that I’ve written, except in those cases I would put a BUILD file in the
coreapi_client
directory. roots are set as
Copy code
[source]
root_patterns = [
    "src/*", "test/*"
]
and pants roots says
Copy code
comanage/coreapi/src/python
registry/get_user/src/python
and the code gets included in the virtual environment I’m using in the IDE. I’m guessing the error is actually about the
comanage
directory, but I’m not understanding what it is telling me.
comanage/BUILD
Copy code
file(name="openapi_schema", source="coreapi.yaml")

run_shell_command(
    name="build_client",
    command="docker run --rm -v $PWD:/local openapitools/openapi-generator-cli generate " \
        "-i /local/coreapi.yaml -g python -o /local/coreapi/src/python " \
        "--package-name coreapi_client " \
        "--additional-properties='generateSourceCodeOnly=True'",
    execution_dependencies=[":openapi_schema"]
)

experimental_wrap_as_python_sources(name="coreapi_client",
                                    inputs=[":build_client"])

python_sources(name="coreapi",
               sources=["coreapi/**/*.py"],
               dependencies=[":coreapi_client"])
OK, I took out the python_sources in the BUILD and then allowed pants tailor to add BUILD files in the generated directories, and managed to get past the error. However, is there another way of doing this?
c
I think this might be a bug, actually. Pants expects (but doesn't require) your source targets to be in the same directory as the sources. Here, it seems pants is looking for the source root for your
coreapi
python sources target starting in the directory where the target is defined, rather than in the directory where the sources are. That is, you are absolutely allowed to use recursive globs for the sources, however, it seems that pants still needs the target to be within a source root. (so, perhaps not a bug, but a designed limitation.) I guess this could be a practical limitation due to implementation details (to avoid situations where you could end up with a single target with sources from multiple source roots, which could confuse pants rules, potentially..) What you could do, is move your coreapi target into the source root:
Copy code
# comanage/coreapi/src/python/BUILD

python_sources(name="coreapi", sources=["**/*.py"], ..)
f
Changing to this, as suggested, did the trick. Thanks!
Copy code
# comanage/coreapi/src/python/BUILD
python_sources(name="coreapi",
               sources=["**/*.py"],
               dependencies=["comanage:coreapi_client"])
👍 1