Skip to content

Commit facec83

Browse files
authored
Show file paths relative to cwd in messages (JuliaDocs#2659)
This is useful for anyone using a terminal emulator that makes file paths with line numbers clickable (to open an editor at that file and line), such as VS Code or iTerm and many others. It also matches the behavior of most UNIX tools, e.g. `git status` or compiler error messages etc. all tend to be relative to the current working directory.
1 parent b9ca066 commit facec83

File tree

4 files changed

+13
-5
lines changed

4 files changed

+13
-5
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
* Now the cursor remain focused on search box even after selecting the filter. ([#2410])
1111
* The "sandbox" modules used for running the code (doctests, examples) are now cleared after a page has been processed, allowing the garbage collector to reclaim memory and therefore reducing memory usage. ([#2640], [#2662])
12+
* Show file paths in error messages relative to the current working directory instead of relative to the document root. ([#2659])
1213

1314
## Fixed
1415

@@ -1995,6 +1996,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
19951996
[#2652]: https://github.com/JuliaDocs/Documenter.jl/issues/2652
19961997
[#2656]: https://github.com/JuliaDocs/Documenter.jl/issues/2656
19971998
[#2658]: https://github.com/JuliaDocs/Documenter.jl/issues/2658
1999+
[#2659]: https://github.com/JuliaDocs/Documenter.jl/issues/2659
19982000
[#2662]: https://github.com/JuliaDocs/Documenter.jl/issues/2662
19992001
[JuliaLang/julia#36953]: https://github.com/JuliaLang/julia/issues/36953
20002002
[JuliaLang/julia#38054]: https://github.com/JuliaLang/julia/issues/38054

src/makedocs.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ function makedocs(; debug = false, format = HTML(), kwargs...)
269269
# Selectors.dispatch. This is to make sure that we pick up any new selector stages that
270270
# may have been added to the selector pipelines between makedocs calls.
271271
empty!(Selectors.selector_subtypes)
272+
original_pwd[] = pwd()
272273
cd(document.user.root) do
273274
withenv(NO_KEY_ENV...) do
274275
Selectors.dispatch(Builder.DocumentPipeline, document)

src/utilities/utilities.jl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ using .Remotes: Remote, repourl, repofile
1111
using .Remotes: RepoHost, RepoGithub, RepoBitbucket, RepoGitlab, RepoAzureDevOps,
1212
RepoUnknown, format_commit, format_line, repo_host_from_url, LineRangeFormatting
1313

14+
const original_pwd = Ref{String}() # for printing relative paths in error messages
15+
16+
1417
"""
1518
@docerror(doc, tag, msg, exs...)
1619
@@ -70,7 +73,9 @@ end
7073

7174
# Pretty-printing locations
7275
function locrepr(file, line = nothing)
73-
str = Base.contractuser(file) # TODO: Maybe print this relative the doc-root??
76+
basedir = isassigned(original_pwd) ? original_pwd[] : currentdir()
77+
file = abspath(file)
78+
str = Base.contractuser(relpath(file, basedir))
7479
line !== nothing && (str = str * ":$(line.first)-$(line.second)")
7580
return str
7681
end

test/docsxref/make.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,17 @@ end
5454
end
5555
@test isnothing(captured.value)
5656
@test contains(
57-
replace(captured.output, "src\\index" => "src/index"),
57+
replace(captured.output, "\\src\\index" => "/src/index"),
5858
"""
59-
┌ Warning: Cannot resolve @ref for md"[`AbstractSelector`](@ref)" in src/index.md.
59+
┌ Warning: Cannot resolve @ref for md"[`AbstractSelector`](@ref)" in docsxref/src/index.md.
6060
│ - No docstring found in doc for binding `Main.DocsReferencingMain.AbstractSelector`.
6161
│ - Fallback resolution in Main for `AbstractSelector` -> `Documenter.Selectors.AbstractSelector` is only allowed for fully qualified names
6262
"""
6363
)
6464
@test contains(
65-
replace(captured.output, "src\\page" => "src/page"),
65+
replace(captured.output, "\\src\\page" => "/src/page"),
6666
"""
67-
┌ Warning: Cannot resolve @ref for md"[`DocsReferencingMain.f`](@ref)" in src/page.md.
67+
┌ Warning: Cannot resolve @ref for md"[`DocsReferencingMain.f`](@ref)" in docsxref/src/page.md.
6868
│ - Exception trying to find docref for `DocsReferencingMain.f`: unable to get the binding for `DocsReferencingMain.f` in module Documenter.Selectors
6969
│ - Fallback resolution in Main for `DocsReferencingMain.f` -> `Main.DocsReferencingMain.f` is only allowed for fully qualified names
7070
"""

0 commit comments

Comments
 (0)