Skip to content

Pilot C# 15 union and closed on LinkResolution - #4179

Merged
Mpdreamz merged 3 commits into
mainfrom
feat/csharp15-union-closed
Nov 11, 2026
Merged

Mpdreamz merged 3 commits into
mainfrom
feat/csharp15-union-closed

Conversation

@Mpdreamz

@Mpdreamz Mpdreamz commented Sep 21, 2026 •

Copy link
Copy Markdown
Member

Introduces the LinkResolution union replacing the bool TryResolve(Action<string>, Uri, out Uri?) interface, and annotates four intentionally-finite hierarchies with closed. Call sites become exhaustive switches enforced by CS8509; the diagnostic side-channel is deleted entirely.

Affects: Cross-link resolution (Elastic.Documentation.Links), Markdown pipeline (Elastic.Markdown), code style guidance (AGENTS.md)

Prompt summary: Plan and apply C# 15 union and closed to docs-builder, starting with the quietest project in the repo as a pilot. Demonstrate both features together, fix real defects, and update the coding guidelines to prefer unions over ad-hoc bool-plus-out patterns going forward.

Why

ICrossLinkResolver.TryResolve carries an Action<string> errorEmitter side channel through five method signatures solely because the return type can't carry why resolution failed. Ten distinct failure modes collapse into the same false + emitter call, so callers can't distinguish a synthesised dev URL from a genuine success, and two call sites (AssemblerBuilder, CodexBuildService) discard the bool entirely and rely wholly on the lambda firing. Three of four real consumers immediately write an adapter lambda just to bridge the emitter into a local diagnostic sink.

closed on the four annotated hierarchies (DocumentationFile, RoleLeaf, AskAiEvent, TopNavRenderItem) removes the implicit possibility of unknown subtypes appearing at a match site without compile-time error.

What

LinkResolution union (new type)

src/Elastic.Documentation.Links/CrossLinks/LinkResolution.cs declares a ten-case union — LinkResolved, LinkSynthesized, and eight failure cases, including LinkFloatingPageNotFound for main's floating sections — each carrying exactly the payload its diagnostic needs. A ResolvedUri() extension method holds the single exhaustive switch, so CS8509 fires in one place if the union gains a new case. A ToDiagnosticMessage() extension replaces the inline format strings that were previously injected via the emitter.

ICrossLinkResolver interface redesigned

Resolve(Uri) → LinkResolution replaces TryResolve(Action<string>, Uri, out Uri?). The Action<string> parameter is deleted from five signatures. NoopCrossLinkResolver and StepCrossLinkResolver — byte-identical implementations in two assemblies — both collapse to return new LinkResolutionUnavailable(). LinkSynthesized makes a synthesised development URL distinguishable from a real LinkResolved for the first time.

Two silent discard sites fixed

AssemblerBuilder.cs and CodexBuildService.cs were _ = linkResolver.TryResolve(…) — the bool discarded, correctness resting solely on the lambda. Both now match on LinkResolution and emit an error when ResolvedUri() returns null.

closed annotations (behaviour-neutral)

DocumentationFile, RoleLeaf, AskAiEvent, and TopNavRenderItem gain closed; the redundant abstract keyword is removed from each. Wire format is unchanged — closed does not affect [JsonPolymorphic] serialization.

Code-style rule updated

AGENTS.md gains a Results bullet (a method with more than one outcome returns a union) and a Sealed hierarchies bullet (closed). TryGetX(key, out value) remains the documented exception for genuine keyed lookups where the out parameter carries no error information.

Verify

dotnet test tests/Elastic.Markdown.Tests/
dotnet test tests/Navigation.Tests/
./build.sh unit-test
./build.sh lint

Out of scope: Elastic.Changelog — highest union density in the repo but 301 file-touches in eight weeks; items are parked in the plan backlog until that area settles.

🤖 Generated with Claude Code

@Mpdreamz
Mpdreamz requested a review from a team as a code owner September 21, 2026 18:05
@Mpdreamz Mpdreamz added the chore label Sep 21, 2026
@Mpdreamz
Mpdreamz requested a review from technige September 21, 2026 18:05

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Requesting changes: this introduces regressions when cross-link resolution is intentionally unavailable (notably skip-cross-links/no-op resolver paths).


What is this? | From workflow: PR Review

Give us feedback! React with 🚀 if perfect, 👍 if helpful, 👎 if not.

link.SetData("isCrossLink", false);
}
else
processor.EmitError(link, resolution.ToDiagnosticMessage(uri));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[HIGH] skipCrossLinks mode now hard-fails ordinary cross-links

This branch emits an error whenever ResolvedUri() is null, but NoopCrossLinkResolver intentionally returns LinkResolutionUnavailable (for --skip-cross-links fast validation). That means valid `(redacted) links now produce hard errors in the exact mode that is supposed to skip cross-link validation.

A concrete failure path is IsolatedBuildService with SkipCrossLinks=true, which wires NoopCrossLinkResolver.Instance; any parsed cross-link now hits this error path.

Please preserve the skip behavior by treating LinkResolutionUnavailable as a non-error passthrough here, while still emitting diagnostics for true resolution failures (LinkNotInIndex, LinkAnchorNotFound, etc.).

var resolvedUri = resolution.ResolvedUri();
if (resolvedUri is null)
{
block.EmitError(resolution.ToDiagnosticMessage(uri));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[HIGH] Cross-link directive validation now errors when resolver is intentionally unavailable

resolver.Resolve(uri) returning LinkResolutionUnavailable is an expected state when cross-link resolution is intentionally disabled (for example fast validation builds using NoopCrossLinkResolver). This new unconditional error turns that opt-out mode into hard failures for directive links using cross-link schemes.

Please handle LinkResolutionUnavailable as a skip/no-op outcome in this path, and keep emitting errors for actual resolution problems.

@Mpdreamz Mpdreamz left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in 27a8e63 — added is LinkResolutionUnavailable early-return guards in DiagnosticLinkInlineParser, DirectiveLinkValidator, and DocumentationSetNavigation before the diagnostic path. All three sites now pass silently when the resolver is intentionally unavailable, and still emit errors for real resolution failures (LinkNotInIndex, LinkAnchorNotFound, etc.).

if (context.CrossLinkResolver.TryResolve(s => processor.EmitError(link, s), uri, out var resolvedUri))
var resolution = context.CrossLinkResolver.Resolve(uri);
if (resolution is LinkResolutionUnavailable)
return;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[LOW] Preserve empty-link-text diagnostics when cross-link resolution is disabled

This early return skips the empty-link-text check below, so in --skip-cross-links mode (where the resolver returns LinkResolutionUnavailable) markdown like `[]((index.md/redacted) no longer emits the existing "empty link text" error.

Before this change, unresolved/no-op resolution still allowed that structural validation to run; now the parser exits before reaching it. Please keep the LinkResolutionUnavailable fast path, but still run the empty-link-text diagnostic so skip mode only disables cross-link resolution/fetching, not this content-quality check.

Base automatically changed from chore/dotnet-11 to main November 11, 2026 07:01
Mpdreamz and others added 3 commits November 11, 2026 08:31
Replace the `bool TryResolve(Action<string>, Uri, out Uri?)` interface with a
`LinkResolution` union whose nine cases carry their own diagnostic payloads. The
`Action<string> errorEmitter` side channel is deleted from five signatures; a
`ResolvedUri()` extension method holds the single exhaustive switch so CS8509 enforces
completeness in one place rather than scattered across every call site.

Also fixes two call sites (`AssemblerBuilder`, `CodexBuildService`) that were discarding
the bool and relying solely on the emitter lambda firing — they now properly handle failure.
Adds `closed` to four intentionally-finite hierarchies: `DocumentationFile`, `RoleLeaf`,
`AskAiEvent` (wire-safe — `closed` does not change serialization representation), and
`TopNavRenderItem`. Updates CLAUDE.md with the union-first rule naming `TryGet` as the
documented exception.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
NoopCrossLinkResolver returns LinkResolutionUnavailable, which is the
intentional signal that cross-link resolution is disabled (--skip-cross-links,
isolated builds, live-reload initial state). The three call sites that
unconditionally emitted an error for any null ResolvedUri() were turning valid
cross-links into hard failures in exactly the modes meant to skip validation.

Guard each site with an early return on LinkResolutionUnavailable before
reaching the diagnostic path. Real resolution failures (LinkNotInIndex,
LinkAnchorNotFound, etc.) still emit errors as before.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
Main added floating-section link resolution while the pilot was open. It now
returns a LinkFloatingPageNotFound case with the same diagnostic text instead
of reporting through an error callback, and the C# authoring and markdown tests
call Resolve instead of TryResolve.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
@Mpdreamz
Mpdreamz force-pushed the feat/csharp15-union-closed branch from 27a8e63 to 198ff6c Compare November 11, 2026 07:34
Comment on lines +354 to +355
if (uri is null)
context.Collector.EmitError(context.ConfigurationPath.FullName, resolution.ToDiagnosticMessage(crossLinkUri));

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Severity: P3 (Low)

This error is filed against the codex configuration path, and it no longer includes the "An error occurred while resolving cross-link {path}" header that the old 3-arg EmitError added. Some ToDiagnosticMessage outputs don't identify the cross-link being resolved: for example, LinkAnchorNotFound gives only a lookup path and fragment without the scheme, and redirect failures such as LinkRedirectTargetMissing identify a target but not necessarily the source redirect key. If multiple redirect entries share a target/path, the operator may not be able to determine which entry failed. Preserve the old URI context in the diagnostic. AssemblerBuilder drops the same header.

Generated by Libra. Specialist: libra-complexity. React with 👍 or 👎 to give feedback on this comment.

Comment on lines 104 to +106
var path = ToTargetUrlPath((crossLinkUri.Host + '/' + crossLinkUri.AbsolutePath.TrimStart('/')).Trim('/'));
resolvedUri = uriResolver.Resolve(crossLinkUri, path);
return true;
var synthesizedUri = uriResolver.Resolve(crossLinkUri, path);
return new LinkSynthesized(synthesizedUri, crossLinkUri.Scheme);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Severity: P3 (Low)

Add resolver-level tests that assert the new result cases, especially LinkSynthesized versus LinkResolved and the synthesized repository payload. The migrated tests flatten results through ResolvedUri() and diagnostic strings, so returning LinkResolved here instead would leave them green and lose the verified-versus-synthesized distinction this API introduces.

Generated by Libra. React with 👍 or 👎 to give feedback on this comment.

@github-actions

Copy link
Copy Markdown
Contributor

@Mpdreamz
Mpdreamz merged commit 418c73d into main Nov 11, 2026
43 checks passed
@Mpdreamz
Mpdreamz deleted the feat/csharp15-union-closed branch November 11, 2026 07:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant