Repository navigation
Polyglot AppHost Refactoring Summary - #13908
Conversation
- 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.
|
🚀 Dogfood this PR with:
curl -fsSL https://raw.githubusercontent.com/dotnet/aspire/main/eng/scripts/get-aspire-cli-pr.sh | bash -s -- 13908Or
iex "& { $(irm https://raw.githubusercontent.com/dotnet/aspire/main/eng/scripts/get-aspire-cli-pr.ps1) } 13908" |
…te export ID detection
| { | ||
| private const string LanguageId = "TypeScript"; | ||
| private const string LanguageDisplayName = "TypeScript (Node.js)"; | ||
| private static readonly string[] s_detectionPatterns = ["apphost.ts"]; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
TypeScriptAppHostProjectwithGuestAppHostProjectthat supports any guest language - Introduced
RuntimeSpecto define language execution configuration server-side - Added
ILanguageSupportinterface for scaffolding, detection, and runtime configuration - Implemented late-bound language resolution (from
settings.jsonor 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;
}
| 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); | ||
| } | ||
| } |
There was a problem hiding this comment.
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.
…Id and enhance TypeScript support
…ved argument handling
| // Create apphost.ts | ||
| files["apphost.ts"] = """ | ||
| // Aspire TypeScript AppHost | ||
| // For more information, see: https://aspire.dev |
There was a problem hiding this comment.
| // For more information, see: https://aspire.dev | |
| // For more information, see: https://aspire.dev/get-started/app-host/ |
…nd improve TypeScript support
…s and improved settings
|
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. |
…ename before checking file system
…terns and make AppHostFileName nullable
- 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.
…trieval in GuestAppHostProject
…c method from app host projects
…upport language detection
…erns and add unit tests for language discovery
…overy configuration
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"| ServerKey Concepts
RuntimeSpec
A data model that describes how to execute a guest language:
Language/DisplayName/CodeGenLanguageDetectionPatterns(e.g.,["apphost.ts"])InstallDependencies(e.g.,npm install)Execute/WatchExecute/PublishExecuteLate-Bound Language Resolution
The CLI no longer hardcodes TypeScript. Instead:
settings.jsonfor persistedlanguagefieldAdding 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"]ILanguageSupportandICodeGeneratorin a NuGet packageExperimental APIs
All public types in
Aspire.Hosting.Atsare marked[Experimental("ASPIREATS001")]:ILanguageSupport,ICodeGeneratorRuntimeSpec,CommandSpecAtsContext,AtsCapabilityInfo, etc.External consumers see a compiler warning; internal code suppresses via
Directory.Build.props.