Path: redb-route/redb.Route/Core/RouteResourceResolver.cs Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route/Core/RouteResourceResolver.cs ---- using redb.Route.Abstractions; namespace redb.Route.Core; /// /// Default : probes, in order, the reference as an absolute /// path, (when set), , and the /// current working directory. The working directory is deliberately LAST: it preserves the /// pre-resolver behaviour (a bare File.Exists) as the final fallback rather than the rule. /// public sealed class RouteResourceResolver : IRouteResourceResolver { /// The shared instance used when no resolver is registered on the context. public static RouteResourceResolver Default { get; } = new(); /// Optional search base probed first for relative references (e.g. a package root). public string? ResourceRoot { get; init; } /// public string? Resolve(string reference) { ArgumentException.ThrowIfNullOrWhiteSpace(reference); return Candidates(reference).FirstOrDefault(File.Exists); } /// public string DescribeSearch(string reference) => string.Join(", ", Candidates(reference)); private IEnumerable Candidates(string reference) { if (Path.IsPathRooted(reference)) { yield return reference; yield break; } if (!string.IsNullOrWhiteSpace(ResourceRoot)) yield return Path.GetFullPath(Path.Combine(ResourceRoot, reference)); yield return Path.GetFullPath(Path.Combine(AppContext.BaseDirectory, reference)); yield return Path.GetFullPath(reference); // current working directory — last on purpose } } /// Shared resolve-or-throw used by the components that read files from endpoint URIs. public static class ResourceResolution { /// /// Resolves through the context's registered /// (or ) and /// returns the absolute path. Throws naming every probed /// location when the resource does not exist. /// public static string Resolve(IRouteContext? context, string reference, string what) { var resolver = context?.GetService() ?? RouteResourceResolver.Default; var resolved = resolver.Resolve(reference); if (resolved is null) throw new FileNotFoundException( $"{what} not found: '{reference}'. Searched: {resolver.DescribeSearch(reference)}."); return resolved; } /// /// Pins a that names a file by a relative path to the file the context /// can actually see: the registered first (a package keeps /// its resources where only the resolver knows), then , which is /// what the consuming package was configured with. Inline text, an embedded resource and an /// absolute path are returned untouched. /// /// The returned source names the resolved file, so a compile cache keyed by it distinguishes two /// packages that ship the same relative name — a hot-reloaded module must not render the previous /// version's text. /// /// /// Route context whose resolver is consulted; null falls back to the default one. /// The source as the route author wrote it. /// The package's own base directory, probed after the resolver. /// Noun for the error message, e.g. "Template". /// Neither place holds the file; the message names both. public static TextSource Locate(IRouteContext? context, TextSource source, string baseDirectory, string what) { ArgumentNullException.ThrowIfNull(source); if (source.FilePath is not { } reference || Path.IsPathRooted(reference)) return source; var resolver = context?.GetService() ?? RouteResourceResolver.Default; if (resolver.Resolve(reference) is { } resolved) return TextSource.Located(reference, resolved); // Not known to the resolver: the package's own base directory is the second place, and the // only one the pre-resolver behaviour ever used. var fromBase = Path.GetFullPath(Path.Combine(baseDirectory, reference)); if (System.IO.File.Exists(fromBase)) return TextSource.Located(reference, fromBase); // Both places in one line, each once: the default resolver already probes the process base // directory, which for most packages is the base directory as well. var searched = resolver.DescribeSearch(reference) .Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries) .Append(fromBase) .Distinct(StringComparer.OrdinalIgnoreCase); throw new FileNotFoundException( $"{what} not found: '{reference}'. Searched: {string.Join(", ", searched)}.", fromBase); } }