DocFX Bug Relative Links To Docfx.json With ~ Are Invalid
Understanding the DocFX Link Resolution Bug
When working with DocFX, a popular documentation generator for .NET projects, developers sometimes encounter issues with relative links, especially when using the tilde (~) notation. This article delves into a specific bug where relative links using the tilde (~) to reference files relative to the docfx.json
file are incorrectly flagged as invalid. This problem can be particularly frustrating for projects where the docfx.json
file is located in a specific directory (like doc/
) and folder links are used extensively to navigate the documentation structure.
The Core of the Issue: Tilde (~) Notation and Relative Paths
The heart of the problem lies in how DocFX resolves relative paths when the tilde (~) is used. In many systems, the tilde (~) is a shorthand notation for the user's home directory. However, in the context of DocFX, it is intended to represent the root directory where the docfx.json
file is located. This can be incredibly useful for creating links that remain valid even if the documentation structure is reorganized, as the links are always relative to the project's root.
Consider a scenario where the docfx.json
file is located in the doc/
directory of a repository. A Markdown file within a deeper subdirectory, such as doc/articles/getting-started/
, might need to link to a source code file located in src/SomeFolderName/
. Without the tilde notation, the link might look like this:
- [Explore the sourcecode](../../../src/SomeFolderName/)
This link, while functional, is fragile. If the Markdown file is moved to a different directory, the link will break. The tilde notation offers a more robust solution:
- [Explore the sourcecode](~/../src/SomeFolderName/)
This link is always relative to the docfx.json
file, making it less prone to breakage. However, the bug arises when DocFX fails to correctly resolve these tilde-based links, flagging them as invalid despite their actual validity.
Reproducing the Bug: A Practical Example
To illustrate the bug, let's consider a real-world example. The DevTKSS.Uno.SampleApps repository provides a clear case study. In a specific commit (9f3202c8c648992d163e8af9837fa8dbbabfb55a), the following steps can be used to reproduce the issue:
- Clone the repository or navigate to the specified commit.
- Run the command
docfx metadata doc/docfx.json
. This command generates metadata for the documentation, and it is during this process that DocFX identifies and flags the tilde-based links asInvalidCref
(invalid cross-reference). Additionally, you might encounterBadly formed CML comment
errors, which can further complicate the documentation generation process. - Next, run the command
docfx build doc/docfx.json
. This command builds the documentation website. During the build process, you will likely see a large number of warnings about duplicated files and not found UID (Unique Identifier) references, stemming from the incorrect link resolution.
Interestingly, when clicking on the flagged