Skip to content

Polyglot AppHost Refactoring Summary - #13908

Merged
David Fowler (davidfowl) merged 22 commits into
mainfrom
davidfowl/poly-init
Jan 15, 2026
Merged

David Fowler (davidfowl) merged 22 commits into
mainfrom
davidfowl/poly-init

Conversation

@davidfowl

@davidfowl David Fowler (davidfowl) commented Jan 14, 2026 •

Copy link
Copy Markdown
Collaborator

Polyglot AppHost Refactoring

Summary

Refactored the CLI's TypeScript-specific AppHost handling into a generic, data-driven architecture that supports any guest language. The CLI is now a "thin shell" that delegates all language-specific logic to the AppHost Server.

Architecture

flowchart TB
    subgraph CLI["Aspire CLI"]
        GuestProject["GuestAppHostProject<br/>(late-bound language)"]
        GuestRuntime["GuestRuntime<br/>(interprets RuntimeSpec)"]
        GuestProject --> GuestRuntime
    end

    subgraph Server["AppHost Server (.NET)"]
        ILang["ILanguageSupport<br/>• Scaffold<br/>• Detect<br/>• GetRuntimeSpec"]
        ICodeGen["ICodeGenerator<br/>• GenerateDistributedApp"]
    end

    subgraph Guest["Guest Process"]
        UserCode["apphost.ts / apphost.py"]
    end

    CLI -->|"JSON-RPC"| Server
    GuestRuntime -->|"npm/python/etc"| Guest
    Guest -->|"invokeCapability"| Server
Loading

Key Concepts

RuntimeSpec

A data model that describes how to execute a guest language:

  • Language / DisplayName / CodeGenLanguage
  • DetectionPatterns (e.g., ["apphost.ts"])
  • InstallDependencies (e.g., npm install)
  • Execute / WatchExecute / PublishExecute

Late-Bound Language Resolution

The CLI no longer hardcodes TypeScript. Instead:

  1. Check settings.json for persisted language field
  2. If not found, query server for all registered languages
  3. Match file patterns against directory contents
  4. Persist detected language for future runs

Adding a New Language

To add Python, Go, or any other language:

flowchart LR
    subgraph NuGet["New NuGet Package"]
        Lang["ILanguageSupport"]
        CodeGen["ICodeGenerator"]
    end

    NuGet -->|"discovered at runtime"| Server["AppHost Server"]
    Server -->|"RuntimeSpec"| CLI["CLI executes"]
Loading
  1. Server-side: Implement ILanguageSupport and ICodeGenerator in a NuGet package
  2. CLI-side: Nothing—the CLI discovers languages automatically via RPC

Experimental APIs

All public types in Aspire.Hosting.Ats are marked [Experimental("ASPIREATS001")]:

  • ILanguageSupport, ICodeGenerator
  • RuntimeSpec, CommandSpec
  • AtsContext, AtsCapabilityInfo, etc.

External consumers see a compiler warning; internal code suppresses via Directory.Build.props.

- Added GuestRuntime class to manage the execution of guest language processes based on RuntimeSpec.
- Implemented methods for installing dependencies, running AppHost processes, and publishing.
- Introduced command execution with environment variable support and output capturing.

feat: Create IAppHostRpcClient and IAppHostServerSession interfaces

- Defined IAppHostRpcClient for making JSON-RPC calls to the AppHost server with typed methods.
- Created IAppHostServerSession to represent a running AppHost server session, managing lifecycle and RPC access.

feat: Add ILanguageDiscovery interface for language metadata

- Introduced ILanguageDiscovery to discover available languages and their metadata.
- Implemented LanguageInfo record to encapsulate language details.

feat: Develop TypeScriptLanguageSupport for TypeScript AppHosts

- Implemented scaffolding, detection, and runtime configuration for TypeScript AppHosts.
- Created necessary scaffold files including apphost.ts and package.json.

feat: Implement LanguageService for JSON-RPC language operations

- Developed LanguageService to handle scaffolding and detection of AppHost projects.
- Integrated logging and error handling for RPC methods.

feat: Create LanguageSupportResolver for dynamic language support discovery

- Implemented LanguageSupportResolver to discover language support implementations from loaded assemblies.
- Enabled retrieval of all available language support.

feat: Define ILanguageSupport interface for language-specific operations

- Created ILanguageSupport interface to standardize scaffolding, detection, and runtime specification retrieval.

feat: Introduce LanguageModels for scaffolding requests and detection results

- Added ScaffoldRequest and DetectionResult classes to encapsulate data for scaffolding and detection operations.

feat: Define RuntimeSpec and CommandSpec for runtime execution configuration

- Created RuntimeSpec to specify execution configuration for languages.
- Introduced CommandSpec to define commands and their arguments for execution.
@github-actions

github-actions Bot commented Jan 14, 2026 •

Copy link
Copy Markdown
Contributor

🚀 Dogfood this PR with:

⚠️ WARNING: Do not do this without first carefully reviewing the code of this PR to satisfy yourself it is safe.

curl -fsSL https://raw.githubusercontent.com/dotnet/aspire/main/eng/scripts/get-aspire-cli-pr.sh | bash -s -- 13908

Or

  • Run remotely in PowerShell:
iex "& { $(irm https://raw.githubusercontent.com/dotnet/aspire/main/eng/scripts/get-aspire-cli-pr.ps1) } 13908"
@davidfowl David Fowler (davidfowl) changed the title Davidfowl/poly init Jan 14, 2026
{
private const string LanguageId = "TypeScript";
private const string LanguageDisplayName = "TypeScript (Node.js)";
private static readonly string[] s_detectionPatterns = ["apphost.ts"];

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.

I expect this to have more values in case two apps with two different language ids can have the same apphost.ts and each extra value filters the list of source language further. For instance next could be the configuration file (packages.json). Is that how it works?

@davidfowl
David Fowler (davidfowl) marked this pull request as ready for review January 14, 2026 17:21
Copilot AI review requested due to automatic review settings January 14, 2026 17:21

Copilot AI 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.

Pull request overview

This PR refactors the Aspire CLI from TypeScript-specific hardcoded logic to a generic, data-driven polyglot architecture. The CLI becomes a "thin shell" that delegates language-specific operations to the AppHost Server via JSON-RPC.

Changes:

  • Replaced TypeScriptAppHostProject with GuestAppHostProject that supports any guest language
  • Introduced RuntimeSpec to define language execution configuration server-side
  • Added ILanguageSupport interface for scaffolding, detection, and runtime configuration
  • Implemented late-bound language resolution (from settings.json or file detection)
  • Marked all ATS APIs as experimental with [Experimental("ASPIREATS001")]
  • Added 3 new analyzer diagnostics: ASPIRE011, ASPIRE012, ASPIRE013

Reviewed changes

Copilot reviewed 38 out of 38 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/Aspire.Hosting/Ats/*.cs New experimental API models: RuntimeSpec, ILanguageSupport, CommandSpec
src/Aspire.Hosting.RemoteHost/Language/*.cs Server-side language discovery and RPC service
src/Aspire.Hosting.CodeGeneration.TypeScript/TypeScriptLanguageSupport.cs TypeScript implementation of ILanguageSupport
src/Aspire.Cli/Projects/GuestAppHostProject.cs Refactored from TypeScriptAppHostProject to generic guest handler
src/Aspire.Cli/Projects/GuestRuntime.cs Data-driven runtime executor that interprets RuntimeSpec
src/Aspire.Cli/Projects/AppHostRpcClient.cs RPC client for server communication
src/Aspire.Cli/Projects/DefaultLanguageDiscovery.cs CLI-side language metadata provider
src/Aspire.Hosting.Analyzers/AspireExportAnalyzer.cs 3 new diagnostics for union validation and duplicate exports
tests/Aspire.Hosting.Analyzers.Tests/*.cs Comprehensive test coverage for new diagnostics
Directory.Build.props Suppress ASPIREATS001 warnings globally
Comments suppressed due to low confidence (1)

src/Aspire.Cli/Projects/GuestRuntime.cs:283

  • The QuoteIfNeeded method only checks for spaces and doesn't escape internal quotes or other shell metacharacters. An argument like file\" && rm -rf / would not be properly escaped. Consider using a more robust escaping mechanism or ProcessStartInfo.ArgumentList which handles escaping automatically.
    private static string QuoteIfNeeded(string arg)
    {
        if (arg.Contains(' ') && !arg.StartsWith('"'))
        {
            return $"\"{arg}\"";
        }
        return arg;
    }
Comment thread src/Aspire.Cli/Projects/GuestRuntime.cs Resolved
Comment on lines +28 to +39
SolutionTransforms =
{
(solution, projectId) =>
{
var project = solution.GetProject(projectId)!;
var compilationOptions = project.CompilationOptions!;
var specificDiagnosticOptions = compilationOptions.SpecificDiagnosticOptions
.Add("ASPIREATS001", ReportDiagnostic.Suppress);
compilationOptions = compilationOptions.WithSpecificDiagnosticOptions(specificDiagnosticOptions);
return solution.WithProjectCompilationOptions(projectId, compilationOptions);
}
}

Copilot AI Jan 14, 2026

Copy link

Choose a reason for hiding this comment

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

The null-forgiving operator (!) is used without null checks for project and CompilationOptions. While these are likely non-null in test scenarios, consider adding defensive null checks or using the null-conditional operator to make the code more robust.

Copilot uses AI. Check for mistakes.
Comment thread src/Aspire.Cli/Projects/GuestRuntime.cs Resolved
// Create apphost.ts
files["apphost.ts"] = """
// Aspire TypeScript AppHost
// For more information, see: https://aspire.dev

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
// For more information, see: https://aspire.dev
// For more information, see: https://aspire.dev/get-started/app-host/
@davidfowl

Copy link
Copy Markdown
Collaborator Author

Re Sébastien Ros (@sebastienros) question about detection patterns:

Yes, that is exactly how it works! The detection patterns are used as a filter chain. If two languages could both match apphost.ts, you would add additional patterns to disambiguate (e.g., package.json for Node.js vs some other config for a different runtime).

Currently TypeScript only needs [apphost.ts] since it is unique enough, but the architecture supports adding more patterns when needed. Each pattern narrows down the possible languages until only one matches.

- Updated NewCommand to utilize ILanguageDiscovery and IScaffoldingService for better language resolution and scaffolding.
- Modified AppHostProjectFactory to create projects based on resolved language information instead of file handling.
- Enhanced DefaultLanguageDiscovery to support language detection from files and added methods for retrieving language info by ID.
- Introduced IScaffoldingService and ScaffoldingService to manage scaffolding of new AppHost projects, including guest languages.
- Updated RunCommand to reflect changes in project handling.
- Refactored LanguageService to integrate with ILanguageDiscovery for improved language management.
- Adjusted ProjectLocator to utilize language detection patterns from ILanguageDiscovery.
- Added support for C# display name in KnownLanguageId.
- Removed deprecated methods and streamlined project and language handling across the codebase.
@davidfowl David Fowler (davidfowl) added the NO-MERGE The PR is not ready for merge yet (see discussion for detailed reasons) label Jan 15, 2026
@davidfowl David Fowler (davidfowl) removed the NO-MERGE The PR is not ready for merge yet (see discussion for detailed reasons) label Jan 15, 2026
@davidfowl
David Fowler (davidfowl) deleted the davidfowl/poly-init branch January 15, 2026 09:44
@dotnet-policy-service dotnet-policy-service Bot added this to the 13.2 milestone Jan 15, 2026
@github-actions github-actions Bot locked and limited conversation to collaborators Feb 15, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

4 participants