Skip to content

Commit 5655b59

Browse files
authored
Handle parsing errors in @docs blocks better (#2809)
In particular, show the precise location where the error occurred.
1 parent 5da6aa3 commit 5655b59

File tree

6 files changed

+120
-2
lines changed

6 files changed

+120
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1616
* Page category is removed from the search index and now everything is in section category. ([#2762], [#2413])
1717
* Changed the docstring block accordions from a custom implementation to HTML details+summary tag. ([#2772], [#2773])
1818
* Improved the search tokenizer and custom trimmer to improve search results. ([#1457], [#2114], [#2744])
19-
* Improved several warning/error messages to (more accurately) report the location (filename, line range) in which the warning/error originated. ([#2803])
19+
* Improved several warning/error messages to (more accurately) report the location (filename, line range) in which the warning/error originated. ([#2426], [#2803], [#2809])
2020

2121
### Fixed
2222

@@ -2093,6 +2093,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
20932093
[#2415]: https://github.com/JuliaDocs/Documenter.jl/issues/2415
20942094
[#2423]: https://github.com/JuliaDocs/Documenter.jl/issues/2423
20952095
[#2424]: https://github.com/JuliaDocs/Documenter.jl/issues/2424
2096+
[#2426]: https://github.com/JuliaDocs/Documenter.jl/issues/2426
20962097
[#2430]: https://github.com/JuliaDocs/Documenter.jl/issues/2430
20972098
[#2434]: https://github.com/JuliaDocs/Documenter.jl/issues/2434
20982099
[#2438]: https://github.com/JuliaDocs/Documenter.jl/issues/2438
@@ -2184,6 +2185,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
21842185
[#2787]: https://github.com/JuliaDocs/Documenter.jl/issues/2787
21852186
[#2792]: https://github.com/JuliaDocs/Documenter.jl/issues/2792
21862187
[#2803]: https://github.com/JuliaDocs/Documenter.jl/issues/2803
2188+
[#2809]: https://github.com/JuliaDocs/Documenter.jl/issues/2809
21872189
[#2812]: https://github.com/JuliaDocs/Documenter.jl/issues/2812
21882190
[#2813]: https://github.com/JuliaDocs/Documenter.jl/issues/2813
21892191
[JuliaLang/julia#36953]: https://github.com/JuliaLang/julia/issues/36953

src/expander_pipeline.jl

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,21 @@ function Selectors.runner(::Type{Expanders.DocsBlocks}, node, page, doc)
455455
push!(docsnodes, admonition)
456456
continue
457457
end
458-
typesig = Core.eval(curmod, Documenter.DocSystem.signature(ex, str))
458+
typesig = try
459+
Core.eval(curmod, Documenter.DocSystem.signature(ex, str))
460+
catch err
461+
@docerror(
462+
doc, :docs_block,
463+
"""
464+
failed to evaluate `$(strip(str))` in `@docs` block in $(Documenter.locrepr(doc, page, lines))
465+
```$(x.info)
466+
$(x.code)
467+
```
468+
""", exception = err
469+
)
470+
push!(docsnodes, admonition)
471+
continue
472+
end
459473
object = make_object(binding, typesig, is_canonical, doc, page)
460474
# We can't include the same object more than once in a document.
461475
if haskey(doc.internal.objects, object)

test/runtests.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ end
2929
@info "Building docsxref/make.jl"
3030
include("docsxref/make.jl")
3131

32+
# Warnings
33+
@info "Building warnings/make.jl"
34+
include("warnings/make.jl")
35+
3236
# Error reporting.
3337
@info "Building errors/make.jl"
3438
@quietly include("errors/make.jl")

test/warnings/make.jl

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
module WarningTests
2+
3+
# Test warnings
4+
# see https://github.com/JuliaDocs/Documenter.jl/issues/2805
5+
6+
using Test
7+
using Documenter
8+
using IOCapture
9+
10+
isdefined(Main, :Documenter) || @eval Main import Documenter
11+
12+
function run_warnings_test(name::String)
13+
captured = IOCapture.capture() do
14+
makedocs(;
15+
sitename = name,
16+
pages = ["index.md", "$name.md"],
17+
pagesonly = true,
18+
warnonly = true,
19+
format = Documenter.HTML(
20+
prettyurls = false,
21+
inventory_version = "",
22+
),
23+
)
24+
end
25+
@assert isnothing(captured.value)
26+
27+
# sanitize the output
28+
output = captured.output
29+
output = replace(output, r"\@ Documenter.*" => "@ Documenter")
30+
output = replace(output, "src\\" => "src/")
31+
return print(output)
32+
end
33+
34+
@doc raw"""
35+
```jldoctest; setup=:(using ..WarningTests)
36+
julia> WarningTests.run_warnings_test("at-docs")
37+
[ Info: SetupBuildDirectory: setting up build directory.
38+
[ Info: Doctest: running doctests.
39+
[ Info: ExpandTemplates: expanding markdown templates.
40+
┌ Warning: undefined binding 'Base.nonsenseBindingThatDoesNotExist' in `@docs` block in src/at-docs.md:4-6
41+
│ ```@docs
42+
│ Base.nonsenseBindingThatDoesNotExist()
43+
│ ```
44+
└ @ Documenter
45+
┌ Warning: no docs found for 'Base.sin()' in `@docs` block in src/at-docs.md:9-11
46+
│ ```@docs
47+
│ Base.sin()
48+
│ ```
49+
└ @ Documenter
50+
┌ Warning: failed to evaluate `Base.sin(::NonsenseTypeThatDoesNotExist)` in `@docs` block in src/at-docs.md:14-16
51+
│ ```@docs
52+
│ Base.sin(::NonsenseTypeThatDoesNotExist)
53+
│ ```
54+
│ exception =
55+
│ UndefVarError: `NonsenseTypeThatDoesNotExist` not defined in `Main`
56+
│ Suggestion: check for spelling errors or missing imports.
57+
└ @ Documenter
58+
[ Info: CrossReferences: building cross-references.
59+
[ Info: CheckDocument: running document checks.
60+
[ Info: Populate: populating indices.
61+
[ Info: RenderDocument: rendering document.
62+
[ Info: HTMLWriter: rendering HTML pages.
63+
```
64+
"""
65+
module AtDocsWarningTests end
66+
67+
fixtests = haskey(ENV, "DOCUMENTER_FIXTESTS")
68+
69+
# run the doctests in Julia >= 1.10 (some outputs have minor difference in
70+
# older Julia versions, and it just doesn't seem worth the trouble of coping
71+
# with that "properly")
72+
VERSION >= v"1.10" && makedocs(;
73+
sitename = "",
74+
doctest = fixtests ? :fix : :only,
75+
modules = [WarningTests],
76+
remotes = nothing,
77+
)
78+
79+
end

test/warnings/src/at-docs.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Test warnings in `@docs` block
2+
3+
Binding that does not exist.
4+
```@docs
5+
Base.nonsenseBindingThatDoesNotExist()
6+
```
7+
8+
Binding that exists but has no documentation.
9+
```@docs
10+
Base.sin()
11+
```
12+
13+
Syntax error due to invalid type in argument list.
14+
```@docs
15+
Base.sin(::NonsenseTypeThatDoesNotExist)
16+
```

test/warnings/src/index.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Landing page
2+
3+
This is here to avoid a warning about a missing landing page.

0 commit comments

Comments
 (0)