Skip to content

CLI doesn't detect file-based apphost.cs when #:package Aspire.Hosting.AppHost has no version (Central Package Management) #22073

Closed

Description

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

A file-based AppHost that uses Central Package Management references the AppHost package without a version:

#:package Aspire.Hosting.AppHost
#:package Aspire.Hosting.Dotnet
// ...
var builder = DistributedApplication.CreateBuilder(args);

The version comes from Directory.Packages.props:

<PackageVersion Include="Aspire.Hosting.AppHost" Version="17.0.0-preview.1.26555.11" />

The Aspire CLI doesn't recognize this file as an AppHost. aspire run --apphost ./src/apphost.cs fails with:

❌ The --apphost option specified a project that does not exist.

aspire ls returns []. dotnet build ./src/apphost.cs evaluates and resolves the package correctly through Central Package Management.

Root cause: DotNetAppHostProject.IsValidSingleFileAppHost calls FallbackProjectParser.HasAppHostPackageDirective. That method uses PackageDirectiveRegex, which requires @version:

[GeneratedRegex(@"^[\t ]*#:package[\t ]+([a-zA-Z0-9\._]+)@([\d\.\-+a-zA-Z]+|\*)[\t ]*\r?$", RegexOptions.Multiline)]

A versionless #:package Aspire.Hosting.AppHost therefore never matches. This was introduced in #20867, which deprecated Aspire.AppHost.Sdk and added HasAppHostPackageDirective. Now that the SDK is obsolete, #:package Aspire.Hosting.AppHost is the recommended pattern, and CPM users can't specify a version in the directive.

Expected Behavior

#:package Aspire.Hosting.AppHost without @version should count as an AppHost directive, so the file is discovered and aspire run/aspire start work.

Steps To Reproduce

  1. Create Directory.Packages.props with <ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally> and <PackageVersion Include="Aspire.Hosting.AppHost" Version="17.0.0-preview.1.26555.11" />.
  2. Create apphost.cs:
    #:package Aspire.Hosting.AppHost
    var builder = DistributedApplication.CreateBuilder(args);
    builder.Build().Run();
  3. Run aspire ls --format Json. Result: [].
  4. Run aspire run --apphost apphost.cs. Result: The --apphost option specified a project that does not exist.

For comparison, a file containing #:package Aspire.Hosting.AppHost@17.0.0-preview.1.26555.11 or #:sdk Aspire.AppHost.Sdk@... is detected.

Workarounds and why they're poor

  • Adding @version to the #:package directive breaks restore with NU1008 under CPM.
  • Adding #:sdk Aspire.AppHost.Sdk@<version> restores detection but uses the now-obsolete SDK, which produces ASPIRE012.

Exceptions (if any)

Aspire.Cli.Projects.ProjectLocatorException: Project file does not exist.
   at Aspire.Cli.Projects.ProjectLocator.UseOrFindAppHostProjectFileCoreAsync(...)
   at Aspire.Cli.Projects.ProjectLocator.UseOrFindAppHostProjectFileAsync(...)
   at Aspire.Cli.Commands.RunCommand.ExecuteAsync(...)

Debug log excerpt:

[dbug] ProjectLocator: Checking candidate file ...\src\apphost.cs
[dbug] AppHostProjectFactory: Language detected: csharp for file: ...\src\apphost.cs
...
[fail] ProjectLocator: No AppHost project files found in directory ...\src

Anything else?

  • Aspire CLI: 17.0.0-preview.1.26555.11+ba68df274b71ed55aaf95c040295f5daee83dc95 (daily channel)
  • .NET SDK: 11.0.100-rc.2.26504.105
  • OS: Windows
  • Tooling: Aspire CLI
  • The regex is unchanged on main.
  • The same versionless form is probably also missed elsewhere PackageDirectiveRegex is used, for example when extracting package references in ParseCsAppHostFile.

Activity

  1. added this to the 17.0 milestone on Nov 10, 2026
  2. added a commit that references this issue on Nov 11, 2026
    f6d716e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions