Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@ManuelLR
Copy link
Contributor

@ManuelLR ManuelLR commented Nov 13, 2025

Fixes two environment variable precedence issues to restore correct behavior and align with Docker Compose standards.

Issues Fixed

  1. Regression from v1.63.0 - .env file variables incorrectly override YAML environment sections
  2. Docker Compose alignment - System/shell environment variables now override .env file values

The Problems

Problem 1: .env overriding YAML (Regression)

In v1.63.0 (PR #317), the .env loading refactor inadvertently changed the precedence order. The .env variables were appended last, giving them highest precedence instead of lowest.

Expected: System → .env → Global YAML → Local YAML
Broken: System → Global YAML → Local YAML → .env

Problem 2: .env overriding shell variables

Previously, .env file variables had higher precedence than shell environment variables, preventing runtime overrides.

Example:

# .env contains: PORT=3000
PORT=8080 process-compose up  # Should use 8080, was using 3000

The Fix

Reorders environment variable appends in getProcessEnvironment():

Correct precedence (lowest → highest):

  1. .env file variables (baseline defaults)
  2. System environment (shell overrides - matches docker-compose)
  3. Global YAML environment section
  4. Local process YAML environment section (highest)

This matches Docker Compose behavior where shell variables override .env files.

Testing

Closes #416

The environment variable precedence was broken in v1.63.0 (PR F1bonacc1#317) where
.env file variables were incorrectly given the highest precedence, overriding
YAML environment sections.

The correct precedence order should be (lowest to highest):
1. System environment (os.Environ)
2. .env file variables
3. Global YAML environment section
4. Local process YAML environment section

This fix reorders the environment variable appends in getProcessEnvironment()
to restore the correct behavior. The .env variables are now appended before
YAML sections, allowing explicit YAML configurations to override .env defaults
as originally intended.

Changes:
- Moved .env variable append block before YAML environment appends
- Added comprehensive regression tests (12 test cases)
- Preserved is_dotenv_disabled feature from PR F1bonacc1#317

Tests:
- All existing tests pass (247/247)
- New regression tests verify correct precedence with all 4 sources
- Manual verification with real-world configurations confirms fix

Fixes: F1bonacc1#416
Replace manual os.Setenv()/os.Unsetenv() calls with t.Setenv() which
automatically cleans up environment variables after tests, following
Go testing best practices and satisfying the usetesting linter rule.
@ManuelLR ManuelLR marked this pull request as draft November 14, 2025 12:06
Copy link
Owner

@F1bonacc1 F1bonacc1 left a comment

Choose a reason for hiding this comment

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

Great job!
Thank you!

@ManuelLR
Copy link
Contributor Author

ManuelLR commented Nov 14, 2025

Hey @F1bonacc1!

Thanks so much for reviewing and approving this! Really appreciate the quick turnaround.

I wanted to mention one thing I noticed while comparing with Docker Compose's behavior - not sure if it's intentional or worth addressing:

Shell Environment vs .env Precedence

I was looking at Docker Compose's environment variable precedence docs and noticed a difference in how shell variables vs .env files are handled.

Example scenario:

.env file contains:

PORT=3000

User runs with shell override:

PORT=8080 process-compose up

Current behavior: Uses 3000 (from .env)
Docker Compose behavior: Would use 8080 (shell wins)


In Docker Compose, shell/system environment variables have higher precedence than the .env file, but in process-compose it's the opposite (since .env is appended after os.Environ()).

Since the README mentions process-compose "tries to follow the docker-compose specifications," I wanted to check if this difference is intentional or if it would make sense to align it?

If you'd like me to fix this, it's a simple change - I can swap the order in getProcessEnvironment() so .env is appended before os.Environ() instead of after. Happy to push that update right now so everything is fixed in this PR at once.

Let me know what you prefer!

Thanks again for the great project!

@F1bonacc1
Copy link
Owner

I think that's a very good suggestion.
This approach follows closer the industry standard tool and also allows more flexibility to override env values without editing files.
Let's put everything in the same PR.
Thank you!

Shell/system environment variables now have higher precedence than .env
file variables, matching Docker Compose semantics where shell variables
can override .env defaults. This provides more flexibility for users to
override configuration at runtime without editing files.

Changes:
- Swapped append order: .env is now added before os.Environ()
- Added test case verifying system env overrides .env
- Updated precedence comments to reflect correct order

Precedence order (lowest to highest):
1. .env file (baseline defaults)
2. System environment (shell overrides)
3. Global YAML environment
4. Local process YAML environment (highest)
@ManuelLR ManuelLR changed the title fix: restore correct environment variable precedence order fix: restore environment variable precedence and align with docker-compose Nov 14, 2025
@ManuelLR ManuelLR requested a review from F1bonacc1 November 14, 2025 18:35
@ManuelLR ManuelLR marked this pull request as ready for review November 14, 2025 18:35
Copy link
Owner

@F1bonacc1 F1bonacc1 left a comment

Choose a reason for hiding this comment

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

Looks great!
There is only one small suggestion to one of the comments, to be better aligned with the precedence order.

@sonarqubecloud
Copy link

@F1bonacc1 F1bonacc1 merged commit e80119d into F1bonacc1:main Nov 14, 2025
5 checks passed
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.

[Regression] Environment variable precedence broken in v1.63.0 - .env overrides YAML

2 participants