Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support REPL softscope for Julia 1.5. #1232

Merged
merged 5 commits into from
Jan 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Documenter.jl changelog

## Version `v0.24.5`

* ![Enhancement][badge-enhancement] ![Bugfix][badge-bugfix] Documenter now correctly emulates the "REPL softscope" (Julia 1.5) in REPL-style doctest blocks and `@repl` blocks. ([#1232][github-1232])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* ![Enhancement][badge-enhancement] ![Bugfix][badge-bugfix] Documenter now correctly emulates the "REPL softscope" (Julia 1.5) in REPL-style doctest blocks and `@repl` blocks. ([#1232][github-1232])
* ![Enhancement][badge-enhancement] ![Bugfix][badge-bugfix] Documenter now correctly emulates the "REPL softscope" in REPL-style doctest blocks and `@repl` blocks (Julia 1.5 and above). ([#1232][github-1232])


## Version `v0.24.4`

* ![Enhancement][badge-enhancement] Change the inline code to less distracting black color in the HTML light theme. ([#1212][github-1212], [#1222][github-1222])
Expand Down Expand Up @@ -508,6 +512,7 @@
[github-1216]: https://github.com/JuliaDocs/Documenter.jl/pull/1216
[github-1222]: https://github.com/JuliaDocs/Documenter.jl/pull/1222
[github-1223]: https://github.com/JuliaDocs/Documenter.jl/pull/1223
[github-1232]: https://github.com/JuliaDocs/Documenter.jl/pull/1232

[documenterlatex]: https://github.com/JuliaDocs/DocumenterLaTeX.jl
[documentermarkdown]: https://github.com/JuliaDocs/DocumenterMarkdown.jl
Expand Down
6 changes: 6 additions & 0 deletions docs/src/man/doctests.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ and will suppress the output, although the line is still evaluated.

Note that not all features of the REPL are supported such as shell and help modes.

!!! note "Soft vs hard scope"

Julia 1.5 changed the REPL to use the _soft scope_ when handling global variables in
`for` loops etc. When using Documenter with Julia 1.5 or above, Documenter uses the soft
scope in `@repl`-blocks and REPL-type doctests.

## Exceptions

Doctests can also test for thrown exceptions and their stacktraces. Comparing of the actual
Expand Down
6 changes: 6 additions & 0 deletions docs/src/man/syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,12 @@ Named `@repl <name>` blocks behave in the same way as named `@example <name>` bl
will be written to, and the paths in `include` calls are interpreted to be relative to
`pwd`. This can be customized with the `workdir` keyword of [`makedocs`](@ref).

!!! note "Soft vs hard scope"

Julia 1.5 changed the REPL to use the _soft scope_ when handling global variables in
`for` loops etc. When using Documenter with Julia 1.5 or above, Documenter uses the soft
scope in `@repl`-blocks and REPL-type doctests.

## `@setup <name>` block

These are similar to `@example` blocks, but both the input and output are hidden from the
Expand Down
5 changes: 5 additions & 0 deletions src/DocTests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,11 @@ function eval_repl(block, sandbox, meta::Dict, doc::Documents.Document, page)
for (ex, str) in Utilities.parseblock(input, doc, page; keywords = false, raise=false)
# Input containing a semi-colon gets suppressed in the final output.
result.hide = REPL.ends_with_semicolon(str)
if VERSION >= v"1.5.0-DEV.178"
# Use the REPL softscope for REPL jldoctests,
# see https://github.com/JuliaLang/julia/pull/33864
ex = REPL.softscope!(ex)
end
(value, success, backtrace, text) = Utilities.withoutput() do
Core.eval(sandbox, ex)
end
Expand Down
5 changes: 5 additions & 0 deletions src/Expanders.jl
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,11 @@ function Selectors.runner(::Type{REPLBlocks}, x, page, doc)
for (ex, str) in Utilities.parseblock(x.code, doc, page; keywords = false)
buffer = IOBuffer()
input = droplines(str)
if VERSION >= v"1.5.0-DEV.178"
# Use the REPL softscope for REPLBlocks,
# see https://github.com/JuliaLang/julia/pull/33864
ex = REPL.softscope!(ex)
end
(value, success, backtrace, text) = Utilities.withoutput() do
cd(page.workdir) do
Core.eval(mod, ex)
Expand Down
17 changes: 16 additions & 1 deletion test/doctests/doctests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ function onormalize(s)
s = replace(s, r"(@ Documenter.DocTests )(.*)$"m => s"\1{PATH}")

# Remove stacktraces
s = replace(s, r"(│\s+Stacktrace:)(\n(│\s+)\[[0-9]+\].*)*" => s"\1\\n\3{STACKTRACE}")
s = replace(s, r"(│\s+Stacktrace:)(\n(│\s+)\[[0-9]+\].*)+" => s"\1\\n\3{STACKTRACE}")

return s
end
Expand Down Expand Up @@ -211,6 +211,21 @@ rfile(filename) = joinpath(@__DIR__, "stdouts", filename)
@test success
@test is_same_as_file(output, rfile("32.stdout"))
end

if VERSION >= v"1.5.0-DEV.178"
# Julia 1.5 REPL softscope,
# see https://github.com/JuliaLang/julia/pull/33864
run_makedocs(["softscope.md"]) do result, success, backtrace, output
@test success
@test is_same_as_file(output, rfile("41.stdout"))
end
else
# Old REPL scoping behaviour on older Julia version
run_makedocs(["hardscope.md"]) do result, success, backtrace, output
@test success
@test is_same_as_file(output, rfile("42.stdout"))
end
end
end

using Documenter.DocTests: remove_common_backtrace
Expand Down
51 changes: 51 additions & 0 deletions test/doctests/src/hardscope.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
REPL scoping behaviour when Julia < 1.5

```jldoctest; filter = r"Stacktrace:(\n \[[0-9]+\].*)+"
julia> s = 0 # global
0

julia> for i = 1:10
t = s + i # new local `t`
s = t # assign global `s`
end
ERROR: UndefVarError: s not defined
Stacktrace:
[1] top-level scope at ./none:2
[...]
```

```jldoctest; filter = r"Stacktrace:(\n \[[0-9]+\].*)+"
julia> code = """
s = 0 # global
for i = 1:10
t = s + i # new local `t`
s = t # new local `s` with warning
end
s, # global
@isdefined(t) # global
""";

julia> include_string(Main, code)
ERROR: LoadError: UndefVarError: s not defined
Stacktrace:
[1] top-level scope at ./string:3
[2] include_string(::Module, ::String, ::String) at ./loading.jl:1075
[...]
```

```jldoctest; filter = r"Stacktrace:(\n \[[0-9]+\].*)+"
s = 0 # global
for i = 1:10
t = s + i # new local `t`
s = t # new local `s` with warning
end
s, # global
@isdefined(t) # global

# output

ERROR: UndefVarError: s not defined
Stacktrace:
[1] top-level scope at ./none:2
[...]
```
57 changes: 57 additions & 0 deletions test/doctests/src/softscope.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
Julia 1.5's REPL softscope

```jldoctest
julia> s = 0 # global
0

julia> for i = 1:10
t = s + i # new local `t`
s = t # assign global `s`
end

julia> s # global
55

julia> @isdefined(t) # global
false
```

```jldoctest; filter = r"Stacktrace:(\n \[[0-9]+\].*)*"
julia> code = """
s = 0 # global
for i = 1:10
t = s + i # new local `t`
s = t # new local `s` with warning
end
s, # global
@isdefined(t) # global
""";

julia> include_string(Main, code)
┌ Warning: Assignment to `s` in soft scope is ambiguous because a global variable by the same name exists: `s` will be treated as a new local. Disambiguate by using `local s` to suppress this warning or `global s` to assign to the existing global variable.
└ @ string:4
ERROR: LoadError: UndefVarError: s not defined
Stacktrace:
[1] top-level scope at ./string:3
[2] include_string(::Module, ::String, ::String) at ./loading.jl:1080
[...]
```

```jldoctest; filter = r"Stacktrace:(\n \[[0-9]+\].*)*"
s = 0 # global
for i = 1:10
t = s + i # new local `t`
s = t # new local `s` with warning
end
s, # global
@isdefined(t) # global

# output

┌ Warning: Assignment to `s` in soft scope is ambiguous because a global variable by the same name exists: `s` will be treated as a new local. Disambiguate by using `local s` to suppress this warning or `global s` to assign to the existing global variable.
└ @ none:3
ERROR: UndefVarError: s not defined
StefanKarpinski marked this conversation as resolved.
Show resolved Hide resolved
Stacktrace:
[1] top-level scope at ./none:2
[...]
```
8 changes: 8 additions & 0 deletions test/doctests/stdouts/41.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[ Info: SetupBuildDirectory: setting up build directory.
[ Info: Doctest: running doctests.
[ Info: ExpandTemplates: expanding markdown templates.
[ Info: CrossReferences: building cross-references.
[ Info: CheckDocument: running document checks.
[ Info: Populate: populating indices.
[ Info: RenderDocument: rendering document.
[ Info: HTMLWriter: rendering HTML pages.
8 changes: 8 additions & 0 deletions test/doctests/stdouts/42.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[ Info: SetupBuildDirectory: setting up build directory.
[ Info: Doctest: running doctests.
[ Info: ExpandTemplates: expanding markdown templates.
[ Info: CrossReferences: building cross-references.
[ Info: CheckDocument: running document checks.
[ Info: Populate: populating indices.
[ Info: RenderDocument: rendering document.
[ Info: HTMLWriter: rendering HTML pages.