-
Notifications
You must be signed in to change notification settings - Fork 802
Description
Summary
When using Aspire's --localhost-tld option (which creates URLs like https://myapp.dev.localhost:17000), the terminal URL detection in GitHub Codespaces fails to automatically forward ports. Users must manually forward ports through the Codespaces UI.
Reproduction Steps
- Create a new Aspire project in a GitHub Codespace with the localhost TLD option enabled:
aspire new --localhost-tld
- Run the project:
dotnet run --project MyApp.AppHost
- Observe the terminal output displays URLs like:
Now listening on: https://myapp-apphost.dev.localhost:17000 Login to the dashboard at https://myapp-apphost.dev.localhost:17000/login?t=... - Expected: Codespaces should auto-detect the localhost URL and offer to forward the port
- Actual: No port forwarding is triggered; the URL is not clickable/accessible
Root Cause
The CodespacesResourceUrlRewriterService only rewrites URLs when the host is exactly "localhost":
// src/Aspire.Hosting/Devcontainers/Codespaces/CodespacesResourceUrlRewriterService.cs:36
if (!originalUrlSnapshot.IsInternal && (uri.Scheme == "http" || uri.Scheme == "https") && uri.Host == "localhost")When --localhost-tld is enabled, the dashboard URL becomes https://myproject-apphost.dev.localhost:17000. This does not match the uri.Host == "localhost" condition because uri.Host is myproject-apphost.dev.localhost, not localhost.
As a result:
- The Codespaces URL rewriter skips these URLs
- The terminal outputs
*.dev.localhostURLs unchanged - Codespaces' terminal URL detection doesn't recognize these as localhost URLs
- Port forwarding is not triggered automatically
Proposed Fix
Use the existing EndpointHostHelpers.IsLocalhostOrLocalhostTld() helper method which already handles all localhost variants:
// Before (line 36):
if (!originalUrlSnapshot.IsInternal && (uri.Scheme == "http" || uri.Scheme == "https") && uri.Host == "localhost")
// After:
if (!originalUrlSnapshot.IsInternal && (uri.Scheme == "http" || uri.Scheme == "https")
&& EndpointHostHelpers.IsLocalhostOrLocalhostTld(uri))This is a minimal, single-line change that leverages existing infrastructure.
Affected Files
src/Aspire.Hosting/Devcontainers/Codespaces/CodespacesResourceUrlRewriterService.cs(primary)- Possibly
src/Aspire.Hosting/Devcontainers/Codespaces/CodespacesUrlRewriter.csif called from other paths
Alternative Considerations
- Auto-disable
--localhost-tldin Codespaces: Detect the environment and warn or fallback to regular localhost URLs - Documentation: Document that
--localhost-tlddoesn't work optimally in Codespaces (not ideal UX) - Hybrid display: Show both Codespaces URL and original
.dev.localhostURL in terminal
Environment
- Aspire version: main branch / .NET 10
- Environment: GitHub Codespaces
- Feature:
--localhost-tldtemplate option
Additional Context
- The
.localhostTLD is an IETF reserved TLD (RFC 2606) that always resolves to127.0.0.1 - Browsers and OS handle
.localhostspecially, but port forwarding tools (including Codespaces) often only look for exactlocalhostmatches - The helper method
EndpointHostHelpers.IsLocalhostOrLocalhostTld()already exists in the codebase for this exact purpose