Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Aug 7, 2025

  • Initial plan - understand environment variable replacement issue
  • Fix bug in MergeImageConfig where env vars were appended instead of replaced
  • Add comprehensive tests for environment variable replacement behavior
  • Parse env var names correctly to enable proper replacement logic
  • Use strings.Cut instead of strings.Index for cleaner environment variable parsing

The MergeImageConfig function was incorrectly appending environment variables instead of replacing them when merging image configurations. This caused duplicate environment variables like PATH to appear in the final container image.

Problem

When a user specified environment variables in the image section of a dalec spec:

image:
  env:
    - PATH=/custom/bin:/usr/bin

And the base image already had PATH=/usr/bin:/bin, the result would contain both:

  • PATH=/usr/bin:/bin (from base image)
  • PATH=/custom/bin:/usr/bin (from spec)

This happened because the environment variable matching logic was comparing the full string ("PATH=/usr/bin:/bin") instead of just the variable name ("PATH").

Solution

Modified the MergeImageConfig function to:

  1. Parse environment variable names (the part before =) for proper matching using strings.Cut
  2. Replace existing environment variables when the same variable name is found
  3. Handle edge cases like environment variables without = signs

Example

Before the fix:

baseEnv := []string{"PATH=/usr/bin:/bin", "HOME=/root"}
specEnv := []string{"PATH=/custom/bin"}
// Result: ["PATH=/usr/bin:/bin", "HOME=/root", "PATH=/custom/bin"] ❌

After the fix:

baseEnv := []string{"PATH=/usr/bin:/bin", "HOME=/root"}
specEnv := []string{"PATH=/custom/bin"}
// Result: ["PATH=/custom/bin", "HOME=/root"] ✅

The fix includes comprehensive tests covering replacement scenarios, appending new variables, and handling edge cases. Updated to use strings.Cut for cleaner, more idiomatic string parsing.

Fixes #456.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

- Fix bug in MergeImageConfig where env vars were appended instead of replaced
- Add comprehensive tests for environment variable replacement behavior
- Parse env var names correctly to enable proper replacement logic

Co-authored-by: cpuguy83 <[email protected]>
Copilot AI changed the title [WIP] [BUG] Image config env var should be able to replace env var from base image config Fix environment variable replacement in image configuration Aug 7, 2025
Copilot AI requested a review from cpuguy83 August 7, 2025 23:08
Copilot finished work on behalf of cpuguy83 August 7, 2025 23:08
image.go Outdated
for i, env := range dst.Env {
envIdx[env] = i
// Extract the environment variable name (part before '=')
if eqIdx := strings.Index(env, "="); eqIdx != -1 {
Copy link
Collaborator

Choose a reason for hiding this comment

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

@copilot please use strings.Cut here and at line 122

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated to use strings.Cut at both locations for cleaner, more idiomatic string parsing. This replaces the strings.Index approach with a more direct way to split environment variable names from their values. Commit: 1866721

…sing

- Replace strings.Index with strings.Cut for cleaner code
- More idiomatic approach for splitting strings at delimiter
- Addresses review feedback from @cpuguy83

Co-authored-by: cpuguy83 <[email protected]>
Copilot AI requested a review from cpuguy83 September 5, 2025 23:57
Copilot finished work on behalf of cpuguy83 September 5, 2025 23:57
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.

[BUG] Image config env var should be able to replace env var from base image config

2 participants