Skip to content

Commit 2038975

Browse files
authored
fix: make sphinxdocs support directory inputs (bazel-contrib#2375)
The logic to relocate files assumed that all the inputs were plain file artifacts. When a directory artifact was used, then `ctx.actions.symlink()` would fail because it requires the output artifact and input target artifact to be the same type of file (plain file or directory). To fix, use `File.is_directory` to detect if the input is a directory or file, then call `declare_file()` or `declare_directory()` as appropriate. The later `symlink()` call is then happy the two args match. Fixes bazel-contrib#2374
1 parent 218f8e1 commit 2038975

File tree

5 files changed

+93
-1
lines changed

5 files changed

+93
-1
lines changed

sphinxdocs/private/sphinx.bzl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,12 @@ def _sphinx_source_tree_impl(ctx):
325325
def _relocate(source_file, dest_path = None):
326326
if not dest_path:
327327
dest_path = source_file.short_path.removeprefix(ctx.attr.strip_prefix)
328-
dest_file = ctx.actions.declare_file(paths.join(source_prefix, dest_path))
328+
329+
dest_path = paths.join(source_prefix, dest_path)
330+
if source_file.is_directory:
331+
dest_file = ctx.actions.declare_directory(dest_path)
332+
else:
333+
dest_file = ctx.actions.declare_file(dest_path)
329334
ctx.actions.symlink(
330335
output = dest_file,
331336
target_file = source_file,
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
load("@bazel_skylib//rules:build_test.bzl", "build_test")
2+
load("//python/private:util.bzl", "IS_BAZEL_7_OR_HIGHER") # buildifier: disable=bzl-visibility
3+
load("//sphinxdocs:sphinx.bzl", "sphinx_build_binary", "sphinx_docs")
4+
load(":defs.bzl", "gen_directory")
5+
6+
# We only build for Linux and Mac because:
7+
# 1. The actual doc process only runs on Linux
8+
# 2. Mac is a common development platform, and is close enough to Linux
9+
# it's feasible to make work.
10+
# Making CI happy under Windows is too much of a headache, though, so we don't
11+
# bother with that.
12+
_TARGET_COMPATIBLE_WITH = select({
13+
"@platforms//os:linux": [],
14+
"@platforms//os:macos": [],
15+
"//conditions:default": ["@platforms//:incompatible"],
16+
}) if IS_BAZEL_7_OR_HIGHER else ["@platforms//:incompatible"]
17+
18+
sphinx_docs(
19+
name = "docs",
20+
srcs = glob(["*.md"]) + [
21+
":generated_directory",
22+
],
23+
config = "conf.py",
24+
formats = ["html"],
25+
sphinx = ":sphinx-build",
26+
target_compatible_with = _TARGET_COMPATIBLE_WITH,
27+
)
28+
29+
gen_directory(
30+
name = "generated_directory",
31+
)
32+
33+
sphinx_build_binary(
34+
name = "sphinx-build",
35+
tags = ["manual"], # Only needed as part of sphinx doc building
36+
deps = [
37+
"@dev_pip//myst_parser",
38+
"@dev_pip//sphinx",
39+
],
40+
)
41+
42+
build_test(
43+
name = "build_tests",
44+
targets = [":docs"],
45+
)

sphinxdocs/tests/sphinx_docs/conf.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Configuration file for the Sphinx documentation builder.
2+
#
3+
# For the full list of built-in configuration values, see the documentation:
4+
# https://www.sphinx-doc.org/en/master/usage/configuration.html
5+
6+
# -- Project info
7+
8+
project = "Sphinx Docs Test"
9+
10+
extensions = [
11+
"myst_parser",
12+
]
13+
myst_enable_extensions = [
14+
"colon_fence",
15+
]

sphinxdocs/tests/sphinx_docs/defs.bzl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"""Supporting code for tests."""
2+
3+
def _gen_directory_impl(ctx):
4+
out = ctx.actions.declare_directory(ctx.label.name)
5+
6+
ctx.actions.run_shell(
7+
outputs = [out],
8+
command = """
9+
echo "# Hello" > {outdir}/index.md
10+
""".format(
11+
outdir = out.path,
12+
),
13+
)
14+
15+
return [DefaultInfo(files = depset([out]))]
16+
17+
gen_directory = rule(
18+
implementation = _gen_directory_impl,
19+
)

sphinxdocs/tests/sphinx_docs/index.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Sphinx docs test
2+
3+
:::{toctree}
4+
:glob:
5+
6+
**
7+
genindex
8+
:::

0 commit comments

Comments
 (0)