Skip to content

Conversation

@mostlygeek
Copy link
Owner

@mostlygeek mostlygeek commented Jun 18, 2025

PR #162 refactored the default configuration code. This introduced a subtle bug where env became []string{} instead of the default of nil.

In golang, exec.Cmd.Env == nil means to use the "current process's environment". By setting it to []string{} as a default the Process's environment was emptied out which caused an array of strange and difficult to troubleshoot behaviour. See issues #168 and #169

This commit changes the behaviour to append model configured environment variables to the default list rather than replace them.

Summary by CodeRabbit

  • Bug Fixes

    • Improved handling of environment variables for processes to append configured variables without removing existing ones.
  • Tests

    • Added a test to verify correct setting of environment variables for processes.
    • Updated tests to accommodate environment variable handling changes and skip specific tests on Windows.

PR #162 refactored the default configuration code. This
introduced a subtle bug where `env` became `[]string{}` instead of the
default of `nil`.

In golang, `exec.Cmd.Env == nil` means to use the "current process's
environment". By setting it to `[]string{}` as a default the Process's
environment was emptied out which caused an array of strange and
difficult to troubleshoot behaviour. See issues #168 and #169

This commit changes the behaviour to append model configured environment
variables to the default list rather than replace them.
@coderabbitai
Copy link

coderabbitai bot commented Jun 18, 2025

Walkthrough

The changes update how environment variables are handled when starting a process, ensuring that custom variables are appended to the existing environment rather than replacing it. Logging is adjusted to reflect the new environment. Additionally, a new test verifies correct environment variable handling, and a Windows-specific test configuration is simplified.

Changes

File(s) Change Summary
proxy/process.go Modified environment variable handling in process startup; updated debug logging of command and env.
proxy/process_test.go Removed Windows-specific config from a test; added test for environment variable appending logic; skip test on Windows.

Sequence Diagram(s)

sequenceDiagram
    participant Test as TestProcess_EnvironmentSetCorrectly
    participant Proc1 as Process (default env)
    participant Proc2 as Process (env + 2 vars)

    Test->>Proc1: Start process with default env
    Proc1->>Proc1: Inherit default environment
    Test->>Proc2: Start process with additional env vars
    Proc2->>Proc2: Inherit default environment
    Proc2->>Proc2: Append 2 new env vars
    Test->>Test: Assert Proc2 env = Proc1 env + 2
Loading
sequenceDiagram
    participant Process
    participant OS

    Process->>OS: Get default environment (cmd.Environ())
    Process->>Process: Append config.Env to default environment
    Process->>Process: Set command environment
    Process->>Process: Log command and environment
    Process->>OS: Start process
Loading

📜 Recent review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 19be15d and 1153230.

📒 Files selected for processing (1)
  • proxy/process_test.go (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • proxy/process_test.go
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: run-tests
✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
proxy/process_test.go (1)

468-490: Good test coverage for the environment variable fix with minor improvements needed.

The test effectively validates the core fix by ensuring:

  1. Environments are not emptied (both processes have non-zero environment variables)
  2. Custom environment variables are properly appended (count difference verification)

However, there are a few areas for improvement:

Fix the typo on line 475:

-	// ensure the additiona variables are appended to the process' environment
+	// ensure the additional variables are appended to the process' environment

Consider enhancing the test to verify that specific environment variables are present:

+	// Verify that the custom environment variables are actually present
+	envMap := make(map[string]string)
+	for _, env := range process2.cmd.Environ() {
+		parts := strings.SplitN(env, "=", 2)
+		if len(parts) == 2 {
+			envMap[parts[0]] = parts[1]
+		}
+	}
+	assert.Equal(t, "1", envMap["TEST_ENV1"])
+	assert.Equal(t, "2", envMap["TEST_ENV2"])
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 9963ae1 and 19be15d.

📒 Files selected for processing (2)
  • proxy/process.go (1 hunks)
  • proxy/process_test.go (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: run-tests
🔇 Additional comments (2)
proxy/process.go (2)

196-196: Excellent fix for the environment variable bug!

This change correctly addresses the issue described in the PR objectives. By using append(p.cmd.Environ(), p.config.Env...) instead of directly assigning p.config.Env, the command now preserves the inherited environment while adding custom variables. This prevents the environment from being cleared when p.config.Env is an empty slice instead of nil.


204-204: Enhanced logging improves debugging capabilities.

Moving the debug log to after environment setup and including the environment variables in the output will help with troubleshooting environment-related issues. The timing of this log statement is now more accurate as it reflects the final state before command execution.

@mostlygeek mostlygeek merged commit 49035e2 into main Jun 18, 2025
3 checks passed
@mostlygeek mostlygeek deleted the fix-default-config-env-bug branch June 18, 2025 18:09
@repsac-by
Copy link

I think the fix won't be complete if we don't change stopCmd.Env similarly.

stopCmd.Env = p.config.Env

@mostlygeek
Copy link
Owner Author

That's a good catch. I'll update the code (again)

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.

3 participants