lepoco/wpfui · error · InvalidOperationException

Could not determine the working directory.

Error message

Could not determine the working directory.

What it means

This is in the Wpf.Ui.FontMapper code-generation tool (a console Program), not the runtime library. It derives its working directory from Assembly.GetExecutingAssembly().Location. Under single-file / trimmed publishing, Location returns an empty string, so Path.GetDirectoryName returns null and the tool throws InvalidOperationException because it has nowhere to write the generated SymbolRegular.cs/SymbolFilled.cs files.

Source

Thrown at src/Wpf.Ui.FontMapper/Program.cs:15

// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file, You can obtain one at https://opensource.org/licenses/MIT.
// Copyright (C) Leszek Pomianowski and WPF UI Contributors.
// All Rights Reserved.

using System.Net.Http;
using System.Net.Http.Json;
using Wpf.Ui.FontMapper;

Console.WriteLine("Fluent System Icons Mapper");
System.Diagnostics.Debug.WriteLine("INFO | Fluent System Icons Mapper", "Wpf.Ui.FontMapper");

var workingDirectory =
    Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)
    ?? throw new InvalidOperationException("Could not determine the working directory.");

var regularIcons = new FontSource(
    "SymbolRegular",
    "Represents a list of regular Fluent System Icons <c>v.{{FLUENT_SYSTEM_ICONS_VERSION}}</c>.\n<para>May be converted to <see langword=\"char\"/> using <c>GetGlyph()</c> or to <see langword=\"string\"/> using <c>GetString()</c></para>",
    @"https://raw.githubusercontent.com/microsoft/fluentui-system-icons/main/fonts/FluentSystemIcons-Regular.json",
    "generated\\SymbolRegular.cs"
);
var filledIcons = new FontSource(
    "SymbolFilled",
    "Represents a list of filled Fluent System Icons <c>v.{{FLUENT_SYSTEM_ICONS_VERSION}}</c>.\n<para>May be converted to <see langword=\"char\"/> using <c>GetGlyph()</c> or to <see langword=\"string\"/> using <c>GetString()</c></para>",
    @"https://raw.githubusercontent.com/microsoft/fluentui-system-icons/main/fonts/FluentSystemIcons-Filled.json",
    "generated\\SymbolFilled.cs"
);

Task<string> FetchVersion()
{
    // using var httpClient = new HttpClient();
    // httpClient.DefaultRequestHeaders.UserAgent.ParseAdd("Mozilla/5.0 (compatible; AcmeInc/1.0)");

View on GitHub (pinned to ffebacd610)

Solutions

  1. Run the tool from a normal (non-single-file) build output where Location is populated.
  2. If you must single-file publish, patch the tool to use AppContext.BaseDirectory instead of Assembly.Location.
  3. Run via 'dotnet run' against the project rather than a published single-file binary.

Example fix

// before
var workingDirectory =
    Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)
    ?? throw new InvalidOperationException("Could not determine the working directory.");

// after
var workingDirectory =
    Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)
    ?? AppContext.BaseDirectory;
// or unconditionally:
var workingDirectory = AppContext.BaseDirectory;
Defensive patterns

Strategy: validation

Validate before calling

var workingDirectory =
    Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)
    ?? AppContext.BaseDirectory;
if (!Directory.Exists(workingDirectory)) throw new InvalidOperationException("No working dir.");

Type guard

static bool HasWorkingDirectory()
    => !string.IsNullOrEmpty(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location))
       || Directory.Exists(AppContext.BaseDirectory);

Try / catch

try { /* run mapper */ }
catch (InvalidOperationException ex) when (ex.Message.Contains("working directory"))
{
    Console.Error.WriteLine("Run from a normal build output, not a single-file publish.");
}

Prevention

When it happens

Trigger: Running the FontMapper generator from a single-file publish, an AOT/trimmed executable, or any host where Assembly.Location is unavailable; running via certain test runners that shadow-copy with no Location.

Common situations: Publishing the FontMapper as a single-file exe; .NET trimming/NativeAOT; invoking the assembly through a loader that strips Location.


AI-assisted analysis of lepoco/wpfui@ffebacd610 (2026-08-13). Data as JSON: /api/errors/3ba4e4cf0221536c. Report an issue: GitHub.