Skip to content
Closed
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
203 changes: 203 additions & 0 deletions docs/docs/dotnet-api-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,209 @@ When the file extension is `.cs` or `.vb`, docfx uses the latest supported .NET
}
```

## Assemblies that share namespaces

A UID, the identifier docfx uses to address an API, is derived from the fully qualified name of the API
alone. When a single docfx project documents several assemblies that declare the same namespace, their
APIs therefore end up with the same UID: pages overwrite each other, `DuplicateUids` warnings are
reported, and every link to a shared namespace resolves to whichever assembly happened to win.

This is common when shipping platform or version specific packages that intentionally expose the same
API surface, for example `MyLib.Ef8` and `MyLib.Ef9`.

### Put the assembly in the UID

[`assemblyUids`](../reference/docfx-json-reference.md#assemblyuids) lists the assemblies whose APIs carry
the assembly they are declared in as a component of their UID, separated from the rest by `::`. It sits at
the top level of `docfx.json`, next to `metadata` rather than inside it, and covers every `src` entry of
the project:

```json
{
"assemblyUids": [ "MyLib", "MyLib.Ef8", "MyLib.Ef9" ],
"metadata": [
{ "src": [ "src/MyLib/MyLib.csproj" ], "dest": "api/core" },
{ "src": [ "src/MyLib.Ef8/MyLib.Ef8.csproj" ], "dest": "api/ef8" },
{ "src": [ "src/MyLib.Ef9/MyLib.Ef9.csproj" ], "dest": "api/ef9" }
]
}
```

`MyLib.Widget` becomes `MyLib::MyLib.Widget`, `MyLib.Ef8::MyLib.Widget` and `MyLib.Ef9::MyLib.Widget`, so
each keeps its own page, and `<see cref="..."/>` links and the table of contents follow. Everything
displayed keeps naming the namespace as it is declared — `MyLib` stays `MyLib`, not
`MyLib.Ef8.MyLib` — because the assembly is a component of the identity, not a namespace segment. How the
assembly itself is shown is up to [`assemblyLabel`](#showing-which-assembly-a-namespace-comes-from).

Give an assembly a shorter component by naming it explicitly, in which case the whole setting is an object
instead of an array:

```json
{
"assemblyUids": {
"MyLib": null,
"MyLib.Ef8": "Ef8",
"MyLib.Ef9": "Ef9"
}
}
```

`null` means the assembly's own name, so this yields `MyLib::MyLib.Widget` and `Ef8::MyLib.Widget`. A
component may contain letters, digits, underscores and dashes, separated by dots.

It is a project level setting because it has to be. An entry mints UIDs not only for the APIs it
documents but also for the APIs it *references*: the `MyLib.Ef8` entry emits reference UIDs for the
`MyLib` types that appear in its own signatures, and those must come out identical to the UIDs the
`MyLib` entry produced. If each entry carried its own list, an entry could not see the others' and those
references would come out unqualified, point at UIDs no page has, and **render as plain text with no link
and no warning**.

The practical consequence: **list every assembly whose APIs appear in another assembly's public
signatures**, not just the ones you want split up. Leaving out a referenced assembly does not fail the
build, it quietly drops links to it.

### Showing which assembly a namespace comes from

When two assemblies declare the same namespace, its pages need to say which one they belong to.
[`assemblyLabel`](../reference/docfx-json-reference.md#assemblylabel), set per `metadata` entry, decides
how:

| value | what a namespace looks like |
|---|---|
| `auto` (default) | `suffix` with `"namespaceLayout": "flattened"`, `none` with `"nested"` |
| `suffix` | `MyLib (MyLib.Ef8)` — the assembly is appended to the label and the page title |
| `none` | `MyLib` — the namespace alone |
| `page` | `MyLib`, with `Assembly: MyLib.Ef8.dll` named on the page, the way type pages do |

With `"namespaceLayout": "nested"` the namespaces of each qualified assembly are grouped under a node
naming that assembly, so the assembly is already visible and `auto` leaves the labels alone. That node has
no page of its own. A flattened layout has nothing else to tell two assemblies apart, so `auto` appends
the assembly to the label there.

> [!NOTE]
> With `"outputFormat": "apiPage"` or `"markdown"` this only affects the table of contents. Those formats
> title a page from the symbol itself, so `suffix` does not reach the title and `page` has nowhere to name
> the assembly.

Authored links name the UID, so they name the assembly and the namespace as they really are:

```md
<xref:MyLib.Ef8::MyLib.Widget>
@MyLib.Ef8::MyLib.Widget
[the Ef8 widget](xref:MyLib.Ef8::MyLib.Widget)
```

### When several entries build the same assembly name

Version specific packages are often *one* project built several times, sharing an `AssemblyName` and
differing only by target framework. Qualifying by assembly name cannot tell those apart, so those entries
use [`assemblyUidOverride`](../reference/docfx-json-reference.md#assemblyuidoverride), which names the
entry instead:

```json
{
"assemblyUids": [ "MyLib" ],
"metadata": [
{
"src": [ "src/MyLib/MyLib.csproj" ],
"dest": "api/core"
},
{
"src": [ "src/MyLib.Ef/MyLib.Ef.Ef8.csproj" ],
"dest": "api/ef8",
"assemblyUidOverride": "Ef8"
},
{
"src": [ "src/MyLib.Ef/MyLib.Ef.Ef9.csproj" ],
"dest": "api/ef9",
"assemblyUidOverride": "Ef9"
}
]
}
```

Both `MyLib.Ef` projects build `MyLib.Ef.dll`. `assemblyUidOverride` separates them, and `MyLib` stays in
`assemblyUids` because both of them reference it.

#### What the override does and does not fix

It fixes the collision *between the entries that declare it*: each gets its own pages, table of contents
entries and file names, and everything inside those pages — references, `<see cref="..."/>` links, member
anchors — is consistent with them.

It does not fix two things, both following from the fact that it names an entry rather than an assembly:

- **Links from other entries into those assemblies.** A fourth entry referencing `MyLib.Ef.dll` has no
way to know whether you meant `Ef8` or `Ef9`. Those references come out unqualified and lose their links,
with no warning. Fix this by
[nominating a default version](#nominate-a-default-version-for-links-from-elsewhere).
- **Two same-named assemblies inside *one* entry.** Every assembly an entry documents gets the same
component, so if one entry's `src` glob matches the same assembly built for several target frameworks,
those copies still collide and are reported as `Ignore duplicated member`. The fix there is to narrow
the glob to one target framework.

### Nominate a default version for links from elsewhere

If anything else in the project references an assembly that several entries document, one of those
versions has to be the target its links point at. Nominate it by giving the assembly that version's
component in `assemblyUids` — **in addition to** the `assemblyUidOverride` on each entry, not instead of
it:

```json
{
"assemblyUids": {
"MyLib": null,
"MyLib.Ef": "Ef9"
},
"metadata": [
{ "src": [ "src/MyLib/MyLib.csproj" ], "dest": "api/core" },
{ "src": [ "src/MyLib.Ef/MyLib.Ef.Ef8.csproj" ], "dest": "api/ef8", "assemblyUidOverride": "Ef8" },
{ "src": [ "src/MyLib.Ef/MyLib.Ef.Ef9.csproj" ], "dest": "api/ef9", "assemblyUidOverride": "Ef9" }
]
}
```

The two settings compose without any further option, because `assemblyUidOverride` only applies to the
assemblies its own entry documents:

| what is being generated | component used | why |
|---|---|---|
| the `api/ef8` pages | `Ef8` | that entry's `assemblyUidOverride` |
| the `api/ef9` pages | `Ef9` | that entry's `assemblyUidOverride` |
| a `MyLib.Ef` reference from any other entry | `Ef9` | `assemblyUids`, since no override applies |

So both versions still get their own complete set of pages, and `MyLib::MyLib` pages that mention
`MyLib.Ef` types link into `api/ef9`.

**Which version to nominate**: normally the newest one you support, since that is where you want readers
who arrive from an unrelated page to land. Nothing breaks if you pick another — the choice only decides
where these particular links go.

Two things to know about the nomination:

- **All such links go to the one version you nominate.** A reference cannot resolve to "whichever version
the referencing assembly was built against": by the time the UID is minted, the only thing
distinguishing the versions is which entry documented them, and a reference carries no record of that.
- **It does not affect hand-written `<xref>` links.** Those name a UID literally, so write the version you
mean — `<xref:Ef8::MyLib.Ef.Widget>` or `<xref:Ef9::MyLib.Ef.Widget>` — and the nomination is not
consulted.

In short:

| situation | option |
|---|---|
| the assembly has a name of its own | `assemblyUids` |
| the assembly is referenced by other assemblies in the project | `assemblyUids` |
| several entries build the same assembly name | `assemblyUidOverride` on each |
| ... and other entries reference it | also give it a component in `assemblyUids`, naming the default target |
| one entry matches the same assembly several times | narrow the `src` glob |

> [!NOTE]
> Enabling either option changes the UID, and therefore the output file name, of every API in the affected
> assemblies: `::` becomes `--` in file names, as `:` is not legal there. Update anything that refers to
> those UIDs by hand, such as `<xref>` links in markdown, overwrite files and external xref maps. Filter
> rules in [`filter`](#filter-apis) configs are unaffected: they keep matching the unqualified API surface.

## Customization Options

There are several options available for customizing .NET API pages that are tailored to your specific needs and preferences. To customize .NET API pages for DocFX, you can use the following options:
Expand Down
118 changes: 118 additions & 0 deletions docs/reference/docfx-json-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,54 @@ Overrides default log message severity level. Key is the log code, supported val
}
```

## `assemblyUids`

The assemblies whose APIs carry the assembly they are declared in as a component of their UID, separated
from the namespace qualified name by `::`. Use it when a single docfx project documents several assemblies
that share namespaces, which would otherwise produce colliding UIDs. Assemblies that are not listed are
left unchanged.

An array qualifies each assembly by its own name:

```json
{
"assemblyUids": [ "MyLib", "MyLib.Ef8" ],
"metadata": [
{ "src": [ "src/MyLib/MyLib.csproj" ], "dest": "api/core" },
{ "src": [ "src/MyLib.Ef8/MyLib.Ef8.csproj" ], "dest": "api/ef8" }
]
}
```

`MyLib.Widget` from `MyLib.dll` gets the UID `MyLib::MyLib.Widget` and the same type name from
`MyLib.Ef8.dll` gets `MyLib.Ef8::MyLib.Widget`, so each keeps its own page, TOC entry and cross
references. In the output file name the `::` becomes `--`, as `:` is not a legal file name character:
`MyLib--MyLib.Widget.html`.

An object names the component to use instead, where `null` means the assembly's own name:

```json
{
"assemblyUids": {
"MyLib": null,
"MyLib.Ef8": "Ef8"
}
}
```

A component may contain letters, digits, underscores and dashes, separated by dots, e.g. `MyLib.Tools`,
`my-lib` or `net8.0`. Unlike a namespace, it may start with a digit, and it is never displayed as part of
a namespace — see [`assemblyLabel`](#assemblylabel) for how the assembly is shown.

**It is a project level setting, not a per `metadata` entry one, and it has to be.** An entry mints UIDs
not only for the APIs it documents but also for the APIs it *references*, and those must come out
identical to the UIDs produced by the entry that documents them, or the links break — so every entry has
to agree on which assemblies are qualified. Declaring the same assembly twice with different components is
reported as an `InvalidAssemblyUid` warning and the first one wins.

Only affects the `docfx metadata` stage. See
[Assemblies that share namespaces](../docs/dotnet-api-docs.md#assemblies-that-share-namespaces).

## `build`

Configuration options that are applied for `docfx build` command:
Expand Down Expand Up @@ -480,6 +528,76 @@ Specifies whether explicit interface implementations are included in the generat

Specify the name to use for the global namespace. The default value is an empty string.

### `assemblyUidOverride`

The assembly component to use in the UIDs of the assemblies documented by this entry, instead of their
assembly name.

Use it **only** when several entries document assemblies that share an *assembly name* and so cannot be
told apart by name — most often per target version builds of one project, which share an `AssemblyName`
and differ only by target framework:

```json
{
"assemblyUids": [ "MyLib" ],
"metadata": [
{
"src": [ "src/MyLib/MyLib.csproj" ],
"dest": "api/core"
},
{
"src": [ "src/MyLib.Ef/MyLib.Ef.Ef8.csproj" ],
"dest": "api/ef8",
"assemblyUidOverride": "Ef8"
},
{
"src": [ "src/MyLib.Ef/MyLib.Ef.Ef9.csproj" ],
"dest": "api/ef9",
"assemblyUidOverride": "Ef9"
}
]
}
```

Both `MyLib.Ef` projects build `MyLib.Ef.dll`, so qualifying by assembly name cannot give them different
components.

The override has two limits, both following from the fact that it names an *entry* rather than an
assembly:

- **Other entries cannot see it.** An entry referencing `MyLib.Ef.dll` has no way to know whether you
meant `Ef8` or `Ef9`, so its references come out unqualified and silently render without a link. To give
those references somewhere to go, also give the assembly a component in
[`assemblyUids`](#assemblyuids), naming the version they should point at. The two settings compose,
because the override only applies to the assemblies its own entry documents — see
[Nominate a default version for links from
elsewhere](../docs/dotnet-api-docs.md#nominate-a-default-version-for-links-from-elsewhere).
- **It does not disambiguate within one entry.** All the assemblies an entry documents get the same
component, so if one entry compiles two assemblies with the same name — a glob matching several target
framework folders, for instance — they still collide.

See [Assemblies that share namespaces](../docs/dotnet-api-docs.md#assemblies-that-share-namespaces)
for a worked example.

### `assemblyLabel`

Specifies how the assembly of a namespace is shown, for assemblies listed in
[`assemblyUids`](#assemblyuids). Assemblies that are not listed there are unaffected.

| value | what a namespace looks like |
|---|---|
| `auto` (default) | `suffix` with `"namespaceLayout": "flattened"`, `none` with `"nested"` |
| `suffix` | `MyLib (MyLib.Ef8)` — the assembly is appended to the label and the page title |
| `none` | `MyLib` — the namespace alone |
| `page` | `MyLib`, with `Assembly: MyLib.Ef8.dll` named on the page, the way type pages do |

The assembly is never part of the namespace itself: a page always names the namespace as it is declared in
the source, whatever this is set to. Only the UID carries the assembly.

With `"namespaceLayout": "nested"` the namespaces of each qualified assembly are grouped under a node
naming that assembly, so `auto` leaves the labels alone there. A flattened layout has nothing else to tell
two assemblies apart, so `auto` appends the assembly.

## File Mappings

In the short-hand form, these filenames are resolved relative to the directory containing the `docfx.json` file:
Expand Down
30 changes: 30 additions & 0 deletions schemas/docfx.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,25 @@
},
"rules": {
"$ref": "#/$defs/ruleConfig"
},
"assemblyUids": {
"description": "The assemblies whose APIs carry the assembly they are declared in as a component of their UID, as in `MyLib.Tools::MyLib.Tools.Widget`, to disambiguate assemblies that share namespaces. Assemblies that are not listed are left unchanged. This is a project level setting because every metadata entry has to agree on which assemblies are qualified: an entry mints UIDs for the APIs it references as well as the ones it documents. Accepts an array of assembly names, each qualified by its own name, or an object naming the component to use instead.",
"oneOf": [
{
"type": "array",
"items": {
"type": "string",
"pattern": "^[A-Za-z0-9_\\-]+(\\.[A-Za-z0-9_\\-]+)*$"
}
},
{
"type": "object",
"additionalProperties": {
"type": ["string", "null"],
"pattern": "^[A-Za-z0-9_\\-]+(\\.[A-Za-z0-9_\\-]+)*$"
}
}
]
}
},
"$defs": {
Expand Down Expand Up @@ -630,6 +649,17 @@
"type": "string",
"description": "Specify the name to use for the global namespace."
},
"assemblyUidOverride": {
"type": "string",
"description": "The assembly component to use in the UIDs of the assemblies documented by this entry, instead of their assembly name. Needed only when several entries document assemblies that share an assembly name, such as per target version builds of one project, which the project level assemblyUids cannot tell apart. Because it is scoped to its own entry, other entries cannot see it, so an assembly that other entries reference belongs in assemblyUids instead.",
"pattern": "^[A-Za-z0-9_\\-]+(\\.[A-Za-z0-9_\\-]+)*$"
},
"assemblyLabel": {
"type": "string",
"enum": ["auto", "suffix", "none", "page"],
"default": "auto",
"description": "Specifies how the assembly of a namespace is shown, for assemblies listed in assemblyUids. `auto` appends the assembly to the namespace label in a flattened layout and shows the namespace on its own in a nested layout, where the per assembly root node already names it. `suffix` always appends it, `none` never does, and `page` names the assembly on the namespace page instead, the way type pages do."
},
"properties": {
"type": "object",
"additionalProperties": {
Expand Down
8 changes: 8 additions & 0 deletions src/Docfx.App/Config/DocfxConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,12 @@ class DocfxConfig
public BuildJsonConfig? build { get; init; }

public Dictionary<string, LogLevel>? rules { get; init; }

/// <summary>
/// The assemblies whose APIs carry their assembly in their UID, to disambiguate assemblies that share
/// namespaces. It lives here rather than inside a `metadata` entry because it applies to the whole
/// project: an entry mints UIDs for the APIs it references as well as the ones it documents, so all
/// entries have to agree on which assemblies are qualified.
/// </summary>
public AssemblyUidConfig? assemblyUids { get; init; }
}
Loading