bazelbuild / bazelbuild/stardoc

stardoc NoSuchFileException when the `input` not the same as rule `name`

Open
#48 1 comment 0 reactions 0 assignees View on GitHub
P4 type: bug
Dominant language
Java
Stars
118
Forks
51
PR merge metrics
No merged PRs in 30d

Description

I had cause to write a rule that generated a `.bzl` file and then pass that to the `stardoc` function as an input.
The problem was that the file that stardoc then looks for is the `name` of the rule and NOT the name of the file passed in.

For example:

Here is my rule:

```
def _stub_impl(ctx):
methods_str = "\n"
for m in ctx.attr.methods:
methods_str += "def %s:\n pass\n" % m

grep_strings = []
for m in ctx.attr.methods:
grep_strings.append("grep -v \"\\\"%s\\\"\"" % m)
grep_string = " | ".join(grep_strings)

# we append a series of mock methods to the end of the file
ctx.actions.run_shell(
inputs = [ctx.file.input],
outputs = [ctx.outputs.output],
command = "echo '%s' | cat %s - | %s > %s" % (methods_str, ctx.file.input.path, grep_string, ctx.outputs.output.path),
)

stub = rule(
implementation = _stub_impl,
attrs = {
"input": attr.label(allow_single_file = True),
"methods": attr.string_list(),
},
outputs = {
"output": "%{name}.bzl",
},
)
```

This will stub out the methods from a `.bzl` file that depend on external libraries that dont play well with stardoc (rules_go) for example.

The BUILD file then looked like:

```
stub(
name = "stub",
input = "file.bzl",
methods = ["go_binary"]
)
stardoc(
name = "docs",
input = ":stub.bzl",
out = "doc.md",
)
```

This will not work because the argument passed to the StarDoc java bin is ` '--input=//tools/go:stub'` and not ` '--input=//tools/go:stub.bzl'` so it causes a

```
Exception in thread "main" java.nio.file.NoSuchFileException: tools/go/stub
at com.google.devtools.build.skydoc.SkydocMain.getInputSource(SkydocMain.java:490)
```

The solution to this is to remove the `.bzl` from output, e.g. `"output": "%{name}",` and change the name of the rule to be `stub.bzl`, this raises a warning in Bazel (so not great).

The goal here is to either:
1. Allow for better handling of third_party `bzl` files in stardoc, so I wont need to `stub`
2. use the actual `ctx.file.input.path` as input to the docs and stop guessing the location using `dep_roots`
3. Allow a rule of a different `name` as `input` and still find file.

Cheers

Contributor guide

Open the contributing guide

Research direction

Reproduce the BUILD example with the generated stub.bzl input, then inspect SkydocMain.java around getInputSource at line 490 and the handling of the --input argument and dep_roots. Compare the target label with the generated file path and verify that stardoc can resolve an input whose rule name differs from its output filename.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
build-system, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.