Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Oct 23, 2025

Fix DockerComposeEnvironment Image Name Duplication

This PR fixes a regression where the DockerComposeEnvironment duplicates the image name in the .env file.

Root Cause

When onlyIfMissing=false is set for container image references (to always update the image name), the EnvFile.Add method didn't check if the key already exists and would add it again, causing duplicates.

Changes Made

  • Explored the repository and understood the issue
  • Updated EnvFile.Add method to upsert (update or insert) when onlyIfMissing=false
  • Added comprehensive tests to verify image name updates don't create duplicates
  • Built and tested all changes - all 56 tests pass
  • Ran security checks - no issues detected
  • Refactored to eliminate code duplication by extracting shared parsing logic

Technical Details

The fix modifies EnvFile.Add() to:

  1. When a key already exists and onlyIfMissing=false, find and update the existing line
  2. When a key already exists and onlyIfMissing=true, skip adding (existing behavior)
  3. When a key doesn't exist, add it normally (existing behavior)

The latest commit addresses code review feedback by extracting the duplicate parsing logic from the Load and Add methods into a shared TryParseKey helper method, ensuring consistent parsing behavior.

Files Changed

  • src/Aspire.Hosting.Docker/EnvFile.cs - Modified the Add method to handle upsert logic and extracted shared parsing logic
  • tests/Aspire.Hosting.Docker.Tests/EnvFileTests.cs - Added comprehensive unit tests for EnvFile behavior

Testing

  • All 56 existing Docker tests continue to pass
  • Added 6 new unit tests specifically for EnvFile upsert behavior
  • Verified that image names are updated correctly without duplication

Security Summary

No security vulnerabilities were detected in the changes.

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.

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
@github-actions
Copy link
Contributor

github-actions bot commented Oct 24, 2025

🚀 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"

@davidfowl davidfowl marked this pull request as ready for review October 30, 2025 04:52
Copilot AI review requested due to automatic review settings October 30, 2025 04:52
@davidfowl
Copy link
Member

@captainsafia if we're not finishing the docker compose change then we need to get this in.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds functionality to the EnvFile class to support updating existing environment variable keys when onlyIfMissing is set to false, enabling the update of container image names and other environment variables in Docker .env files.

Key Changes

  • Modified EnvFile.Add() method to update existing keys when onlyIfMissing=false instead of always skipping them
  • Added logic to locate and update the value of existing keys in the file while preserving file structure
  • Added comprehensive test coverage for the new update behavior and edge cases

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/Aspire.Hosting.Docker/EnvFile.cs Refactored the Add method to support updating existing keys with new values when onlyIfMissing=false
tests/Aspire.Hosting.Docker.Tests/EnvFileTests.cs Added new test file with six tests covering update behavior, duplicate prevention, and file loading scenarios

Comment on lines 46 to 62
for (int i = 0; i < _lines.Count; i++)
{
var trimmed = _lines[i].TrimStart();
if (!trimmed.StartsWith('#') && trimmed.Contains('='))
{
var eqIndex = trimmed.IndexOf('=');
if (eqIndex > 0)
{
var lineKey = trimmed[..eqIndex].Trim();
if (lineKey == key)
{
_lines[i] = value is not null ? $"{key}={value}" : $"{key}=";
return;
}
}
}
}
Copy link

Copilot AI Oct 30, 2025

Choose a reason for hiding this comment

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

The key-finding logic in lines 46-62 duplicates the parsing logic from the Load method (lines 19-31). Consider extracting this into a shared helper method to avoid code duplication and ensure consistent parsing behavior.

Copilot uses AI. Check for mistakes.
Copy link
Member

Choose a reason for hiding this comment

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

@copilot apply changes based on this feedback

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Refactored to extract the duplicate parsing logic into a shared TryParseKey helper method. This ensures consistent parsing behavior between the Load and Add methods. (f2a1504)

@captainsafia captainsafia merged commit 0a51ce5 into main Oct 31, 2025
309 of 321 checks passed
@captainsafia captainsafia deleted the copilot/fix-duplicate-image-name branch October 31, 2025 02:59
@dotnet-policy-service dotnet-policy-service bot added this to the 13.0 milestone Oct 31, 2025
@captainsafia
Copy link
Member

/backport to release/13.0

@github-actions
Copy link
Contributor

Started backporting to release/13.0: https://github.com/dotnet/aspire/actions/runs/18961330038

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DockerComposeEnvironment duplicates the image name

3 participants