Skip to content

Commit

Permalink
Local container support fix.
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchdenny committed Nov 25, 2024
1 parent ca5e4ec commit ce5666a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,18 @@ namespace Aspire.Hosting.Devcontainers;

internal class DevcontainerPortForwardingHelper
{
private const string MachineSettingsPath = "/home/vscode/.vscode-remote/data/Machine/settings.json";
private const string CodespaceSettingsPath = "/home/vscode/.vscode-remote/data/Machine/settings.json";
private const string LocalDevcontainerSettingsPath = "/home/vscode/.vscode-server/data/Machine/settings.json";
private const string PortAttributesFieldName = "remote.portsAttributes";

public static async Task SetPortAttributesAsync(int port, string protocol, string label, CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNullOrEmpty(protocol);
ArgumentNullException.ThrowIfNullOrEmpty(label);

var settingsContent = await File.ReadAllTextAsync(MachineSettingsPath, cancellationToken).ConfigureAwait(false);
var settingsPath = GetSettingsPath();

var settingsContent = await File.ReadAllTextAsync(settingsPath, cancellationToken).ConfigureAwait(false);
var settings = (JsonObject)JsonObject.Parse(settingsContent)!;

JsonObject? portsAttributes;
Expand Down Expand Up @@ -48,6 +51,25 @@ public static async Task SetPortAttributesAsync(int port, string protocol, strin
portAttributes["onAutoForward"] = "notify";

settingsContent = settings.ToString();
await File.WriteAllTextAsync(MachineSettingsPath, settingsContent, cancellationToken).ConfigureAwait(false);
await File.WriteAllTextAsync(settingsPath, settingsContent, cancellationToken).ConfigureAwait(false);

static string GetSettingsPath()
{
// For some reason the machine settings path is different between Codespaces and local Devcontainers
// so we probe for it here. We could use options to figure out which one to look for here but that
// would require taking a dependency on the options system here which seems like overkill.
if (File.Exists(CodespaceSettingsPath))
{
return CodespaceSettingsPath;
}
else if (File.Exists(LocalDevcontainerSettingsPath))
{
return LocalDevcontainerSettingsPath;
}
else
{
throw new DistributedApplicationException("Could not find a devcontainer settings file.");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using Microsoft.Extensions.Options;
using Xunit;
using Xunit.Abstractions;
using Aspire.Hosting.Devcontainers;

namespace Aspire.Hosting.Tests.Dashboard;

Expand Down Expand Up @@ -101,10 +102,13 @@ private static DashboardLifecycleHook CreateHook(
ResourceNotificationService resourceNotificationService,
IConfiguration configuration,
ILoggerFactory? loggerFactory = null,
IOptions<CodespacesOptions>? codespacesOptions = null
IOptions<CodespacesOptions>? codespacesOptions = null,
IOptions<DevcontainersOptions>? devcontainersOptions = null
)
{
codespacesOptions ??= Options.Create(new CodespacesOptions());
devcontainersOptions ??= Options.Create(new DevcontainersOptions());

var rewriter = new CodespacesUrlRewriter(codespacesOptions);

return new DashboardLifecycleHook(
Expand All @@ -118,7 +122,9 @@ private static DashboardLifecycleHook CreateHook(
loggerFactory ?? NullLoggerFactory.Instance,
new DcpNameGenerator(configuration, Options.Create(new DcpOptions())),
new TestHostApplicationLifetime(),
rewriter
rewriter,
codespacesOptions,
devcontainersOptions
);
}

Expand Down

0 comments on commit ce5666a

Please sign in to comment.