Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Fix EnvFile to preserve user-modified values when saving keys only
When SaveKeysOnly is called, it now checks if keys already exist on disk with non-empty values and preserves those values instead of overwriting them. This ensures that user modifications to .env files are not lost during subsequent publish operations.

Co-authored-by: captainsafia <[email protected]>
  • Loading branch information
Copilot and captainsafia committed Nov 4, 2025
commit 65b1889b922b007884f5b54735400fefe62b835a
25 changes: 24 additions & 1 deletion src/Aspire.Hosting.Docker/EnvFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,17 @@ public void Save(string path, bool includeValues)

private void SaveKeysOnly(string path)
{
// Load existing values from disk to preserve user modifications
var existingEntries = new Dictionary<string, string?>();
if (File.Exists(path))
{
var existingFile = Load(path);
foreach (var entry in existingFile._entries.Values)
{
existingEntries[entry.Key] = entry.Value;
}
}

var lines = new List<string>();

foreach (var entry in _entries.Values)
Expand All @@ -108,7 +119,19 @@ private void SaveKeysOnly(string path)
{
lines.Add($"# {entry.Comment}");
}
lines.Add($"{entry.Key}=");

// If the key exists on disk with a non-empty value, preserve it
// This ensures user-modified values are not overwritten when we save keys only
if (existingEntries.TryGetValue(entry.Key, out var existingValue) &&
!string.IsNullOrEmpty(existingValue))
{
lines.Add($"{entry.Key}={existingValue}");
}
else
{
lines.Add($"{entry.Key}=");
}

Copy link

Copilot AI Nov 4, 2025

Choose a reason for hiding this comment

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

Trailing whitespace on line 134. The blank line before lines.Add(string.Empty); contains only whitespace characters, which violates coding standards. Remove the trailing whitespace.

Suggested change

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed in eb5371f.

lines.Add(string.Empty);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Parameter param1
PARAM1=
PARAM1=changed

# Parameter param2
PARAM2=
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Parameter param1
PARAM1=
PARAM1=changed