Skip to content

Commit 4cea20a

Browse files
committed
support non-worker invocation when rules try to use a worker invocation
1 parent d968327 commit 4cea20a

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

sphinxdocs/private/sphinx.bzl

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,12 @@ def _run_sphinx(ctx, format, source_path, inputs, output_prefix, use_persistent_
269269
run_args = [] # Copy of the args to forward along to debug runner
270270
args = ctx.actions.args() # Args passed to the action
271271

272+
# An args file is required for persistent workers, but we don't know if
273+
# the action will use worker mode or not (settings we can't see may
274+
# force non-worker mode). For consistency, always use a params file.
275+
args.use_param_file("@%s", use_always = True)
276+
args.set_param_file_format("multiline")
277+
272278
# NOTE: sphinx_build.py relies on the first two args being the srcdir and
273279
# outputdir, in that order.
274280
args.add(source_path)
@@ -289,7 +295,8 @@ def _run_sphinx(ctx, format, source_path, inputs, output_prefix, use_persistent_
289295

290296
if use_persistent_workers:
291297
# * Normally Sphinx puts doctrees in the output dir. We can't do that
292-
# because Bazel will clear the output directory every invocation.
298+
# because Bazel will clear the output directory every invocation. To
299+
# work around that, create it as a sibling directory.
293300
# * Use a non-dot prefixed name so it shows up more visibly.
294301
args.add(paths.join(output_dir.path + "_doctrees"), format = "--doctree-dir=%s")
295302

@@ -318,10 +325,11 @@ def _run_sphinx(ctx, format, source_path, inputs, output_prefix, use_persistent_
318325
for tool in ctx.attr.tools:
319326
tools.append(tool[DefaultInfo].files_to_run)
320327

328+
# NOTE: Command line flags or RBE capabilities may override the execution
329+
# requirements and disable workers. Thus, we can't assume that these
330+
# exec requirements will actually be respected.
321331
execution_requirements = {}
322332
if use_persistent_workers:
323-
args.use_param_file("@%s", use_always = True)
324-
args.set_param_file_format("multiline")
325333
execution_requirements["supports-workers"] = "1"
326334
execution_requirements["requires-worker-protocol"] = "json"
327335

sphinxdocs/private/sphinx_build.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -201,14 +201,29 @@ def _handle_env_get_outdated(self, app, env, added, changed, removed):
201201
env.path2doc(p)
202202
for p in self.changed_paths
203203
}
204+
204205
logger.info("changed docs: %s", changed)
205206
return changed
206207

208+
def _worker_main(stdin, stdout, exec_root):
209+
with Worker(stdin, stdout, exec_root) as worker:
210+
return worker.run()
211+
212+
213+
def _non_worker_main(argv):
214+
args = []
215+
for arg in sys.argv:
216+
if arg.startswith("@"):
217+
with open(arg.removeprefix("@")) as fp:
218+
lines = [line.strip() for line in fp if line.strip()]
219+
args.extend(lines)
220+
else:
221+
args.append(arg)
222+
sys.argv[:] = args
223+
return main()
207224

208225
if __name__ == "__main__":
209226
if "--persistent_worker" in sys.argv:
210-
with Worker(sys.stdin, sys.stdout, os.getcwd()) as worker:
211-
sys.exit(worker.run())
227+
sys.exit(_worker_main(sys.stdin, sys.stdout, os.getcwd()))
212228
else:
213-
raise Exception("non-worker path taken")
214-
sys.exit(main())
229+
sys.exit(_non_worker_main())

0 commit comments

Comments
 (0)