Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Oct 23, 2025

Problem

When publishing Docker Compose environments, the .env file would contain duplicate entries for container image names if the file was regenerated. This is a regression from the change that removed the hard-coded "latest" tag for container images.

For example, after multiple publishes, the .env file could contain:

# Container image name for project1
PROJECT1_IMAGE=project1:latest

# Container image name for project1
PROJECT1_IMAGE=project1:latest

Root Cause

The issue was in the EnvFile.Add() method in src/Aspire.Hosting.Docker/EnvFile.cs. When onlyIfMissing=false is set for container image references (line 193 of DockerComposePublishingContext.cs), the method would add the key again instead of updating the existing entry.

The original logic only checked if the key existed when onlyIfMissing=true:

if (onlyIfMissing && _keys.Contains(key))
{
    return;
}
// Always adds a new line, even if key exists
_lines.Add(value is not null ? $"{key}={value}" : $"{key}=");

Solution

Modified EnvFile.Add() to implement proper upsert logic:

  1. When key exists and onlyIfMissing=false: Find and update the existing line with the new value
  2. When key exists and onlyIfMissing=true: Skip adding (existing behavior)
  3. When key doesn't exist: Add it normally (existing behavior)

This ensures that container image names are properly updated without creating duplicate entries in the .env file.

Testing

  • Added comprehensive unit tests in EnvFileTests.cs covering all scenarios
  • All 56 Docker-related tests pass (50 existing + 6 new)
  • Verified that existing .env file handling behavior is preserved

Fixes the issue mentioned in

if (source is ContainerImageReference cir && cir.Resource.TryGetContainerImageName(out var imageName))
{
defaultValue = imageName;
onlyIfMissing = false; // Always update the image name if it changes
}
envFile.Add(key, defaultValue, description, onlyIfMissing);

Original prompt

This section details on the original issue you should resolve

<issue_title>DockerComposeEnvironment duplicates the image name</issue_title>
<issue_description>This is a regression from the last release to fix the fact that we no longer hard code "latest". This logic needs to upsert the existing image name instead

if (source is ContainerImageReference cir && cir.Resource.TryGetContainerImageName(out var imageName))
{
defaultValue = imageName;
onlyIfMissing = false; // Always update the image name if it changes
}
envFile.Add(key, defaultValue, description, onlyIfMissing);
</issue_description>

Comments on the Issue (you are @copilot in this section)

Fixes #12309


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Co-authored-by: davidfowl <95136+davidfowl@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix DockerComposeEnvironment to upsert image name Fix DockerComposeEnvironment duplicating image names in .env file Oct 23, 2025
Copilot AI requested a review from davidfowl October 23, 2025 15:16
Copilot finished work on behalf of davidfowl October 23, 2025 15:16
@github-actions
Copy link
Contributor

🚀 Dogfood this PR with:

⚠️ WARNING: Do not do this without first carefully reviewing the code of this PR to satisfy yourself it is safe.

curl -fsSL https://raw.githubusercontent.com/dotnet/aspire/main/eng/scripts/get-aspire-cli-pr.sh | bash -s -- 12310

Or

  • Run remotely in PowerShell:
iex "& { $(irm https://raw.githubusercontent.com/dotnet/aspire/main/eng/scripts/get-aspire-cli-pr.ps1) } 12310"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DockerComposeEnvironment duplicates the image name

2 participants