Skip to content

Fix Dev Container / Codespaces support for non-default usernames #9538

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 16, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 23 additions & 8 deletions src/Aspire.Hosting/Devcontainers/DevcontainerSettingsWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,21 @@ namespace Aspire.Hosting.Devcontainers;

internal class DevcontainerSettingsWriter(ILogger<DevcontainerSettingsWriter> logger, IOptions<CodespacesOptions> codespaceOptions, IOptions<DevcontainersOptions> devcontainerOptions)
{
private const string CodespaceSettingsPath = "/home/vscode/.vscode-remote/data/Machine/settings.json";
private const string VSCodeServerPath = "/home/vscode/.vscode-server";
private const string VSCodeInsidersServerPath = "/home/vscode/.vscode-server-insiders";
// Define path segments that will be combined with the user's home directory
// These path segments are relative to the user's home directory
// instead of hardcoding to a specific user (e.g., "vscode")
private const string VscodeRemotePathSegment = ".vscode-remote/data/Machine/settings.json";
private const string VscodeServerPathSegment = ".vscode-server";
private const string VscodeInsidersServerPathSegment = ".vscode-server-insiders";
private const string LocalDevcontainerSettingsPath = "data/Machine/settings.json";
private const string PortAttributesFieldName = "remote.portsAttributes";
private const int WriteLockTimeoutMs = 2000;
private readonly SemaphoreSlim _writeLock = new SemaphoreSlim(1);

// Get the user's home directory using the Environment API
// This ensures we work with any username in devcontainers/codespaces
private static string GetUserHomeDirectory() =>
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);

private readonly List<(string Url, string Port, string Protocol, string Label, bool OpenBrowser)> _pendingPorts = [];

Expand Down Expand Up @@ -131,22 +139,29 @@ private async Task WriteSettingsAsync(CancellationToken cancellationToken = defa

IEnumerable<string> GetSettingsPaths()
{
// Get the current user's home directory
// This ensures we work with any username in the container, not just "vscode"
var userHomeDir = GetUserHomeDirectory();

// For some reason the machine settings path is different between Codespaces and local Devcontainers
// so we decide which one to use here based on the options.
if (codespaceOptions.Value.IsCodespace)
{
yield return CodespaceSettingsPath;
yield return Path.Combine(userHomeDir, VscodeRemotePathSegment);
}
else if (devcontainerOptions.Value.IsDevcontainer)
{
if (Directory.Exists(VSCodeServerPath))
var vscodeServerPath = Path.Combine(userHomeDir, VscodeServerPathSegment);
var vscodeInsidersServerPath = Path.Combine(userHomeDir, VscodeInsidersServerPathSegment);

if (Directory.Exists(vscodeServerPath))
{
yield return Path.Combine(VSCodeServerPath, LocalDevcontainerSettingsPath);
yield return Path.Combine(vscodeServerPath, LocalDevcontainerSettingsPath);
}

if (Directory.Exists(VSCodeInsidersServerPath))
if (Directory.Exists(vscodeInsidersServerPath))
{
yield return Path.Combine(VSCodeInsidersServerPath, LocalDevcontainerSettingsPath);
yield return Path.Combine(vscodeInsidersServerPath, LocalDevcontainerSettingsPath);
}
}
else
Expand Down
Loading