Hey guys, it looks like `pex_binary` erases inform...
# general
s
Hey guys, it looks like
pex_binary
erases information about entry points, and this breaks sphinx. How can I make it work?
Copy code
python_requirement(
    name="sphinx",
    requirements=["sphinx~=7.2"],
)
pex_binary(
    name="sphinx-build",
    script="sphinx-build",
    dependencies=[":sphinx"],
)
run_shell_command(
    name="build",
    command="sphinx-build -M html source/ build/",
    execution_dependencies=[":sphinx-build"],
    runnable_dependencies=[":sphinx-build"],
)
so I have
html_theme = "sphinx_rtd_theme"
in my sphinx conf.py, and it throws an error
Copy code
$ pants run docs:build
21:16:53.46 [INFO] Initialization options changed: reinitializing scheduler...
21:16:57.65 [INFO] Scheduler initialized.
21:17:01.29 [INFO] Completed: Building 1 requirement for docs/sphinx-build.pex from the lockfiles/sphinx.lock resolve: sphinx~=7.2
Running Sphinx v7.2.6

Theme error:
no theme named 'sphinx_rtd_theme' found (missing theme.conf?)
sphinx uses
importlib.metadata.entry_points
to discover plugins https://github.com/sphinx-doc/sphinx/blob/faa33a53a389f6f8bc1f6ae97d6015fa92393c4a/sphinx/theming.py#L139 And I think this information gets erased by pex, how can I fix that?
s
yeah, I tried a bunch of these options, it doesn't help
b
(Btw using
adhoc_tool(runnable=“:sphinx-build”, args=[…])
may work better overall, once you’ve fixed the entry point issue. Pants will help find the right Python versions, etc)
Hm. Is that theme a separate package? If so, does it need to be explicitly installed ?
s
I'm trying to reproduce it with bare pex, sec
I think I did something wrong, this doesn't work
Copy code
command="sphinx-build -M html source/ build/",
it uses sphinx-build from my env, and fails because of it
ha, this works
command="{chroot}/docs/sphinx-build.pex -M html source/ build/",
but now it fails because of the missing first party dependencies
is it possible to get all python_source dependencies?
Copy code
dependencies=[*recursive_dependencies("my_module")]
b
command="{chroot}/docs/sphinx-build.pex
Oh, if you're having to do that, I think the
adhoc_tool
version is better; it manages that for you. I don't know of a good way to all source files; do you have a single file that imports ~everything?
s
I need sphinx to be able to import everything
I'm using autodoc extension, it needs to import everything to collect docs https://www.sphinx-doc.org/en/master/usage/extensions/autodoc.html
b
yes. Some libraries have a
foo/__init__.py
that has various import statements like
from .bar import X
or
import baz
etc. so that users can write
import foo.X
and
import foo; foo.baz
. If that's the case, then one might be able to use that entrypoint as the 'recursive' thing (with some trickery)
s
yeah, but that requires updating the imports
b
it's fine if it's not an option, I was just seeing if you were in that situation.
sounds like no
s
no 🙂
b
it's not the best, but having two owners for the py files might be as good as it gets: e.g. a
python_source(name="py-for-docs", sources=["**/*.py"])
s
I've managed to do this with a inference plugin
Copy code
@rule(desc="Recursively add all python sources as dependencies for unit target")
async def infer_unit_dependencies(
    request: InferUnitDependenciesRequest,
    all_targets: AllTargets,
) -> InferredDependencies:
    spec_path = request.field_set.address.spec_path

    return InferredDependencies(
        t.address
        for t in all_targets
        if t.has_field(PythonSourceField)
        and t.address.spec_path.startswith(spec_path)
        and t.get(PythonResolveField).value == "default"
    )
c
@square-psychiatrist-19087 Would you mind posting a snippet of the full plugin file? I may have just run into the same problem.
s
register.py
Sure, it's a single
register.py
file
Then you can use it like this
Copy code
glob(
    name="glob",
    target_types={"python_source": {"resolve": "default"}},
)
Put it into the root of a directory and it will automatically add dependencies on all python_source targets with resolve=default recursively
c
Thanks!
s
@famous-xylophone-36532 Take a look at this thread, this
glob
target is one way to add all python sources to docker image
👀 1
cc @aloof-airline-74337 here is my plugin for globs ^
a
Oh neat
Thank you