Skip to content

Conversation

@xav-db
Copy link
Member

@xav-db xav-db commented Oct 7, 2025

Description

Related Issues

Closes #

Checklist when merging to main

  • No compiler warnings (if applicable)
  • Code is formatted with rustfmt
  • No useless or dead code (if applicable)
  • Code is easy to understand
  • Doc comments are used for all functions, enums, structs, and fields (where appropriate)
  • All tests pass
  • Performance has not regressed (assuming change was not to fix a bug)
  • Version number has been updated in helix-cli/Cargo.toml and helixdb/Cargo.toml

Additional Notes

Greptile Overview

Updated On: 2025-10-08 17:37:33 UTC

Summary

This PR implements environment variable handling for the Helix CLI, enabling users to specify custom `.env` files for their local instances. The feature adds comprehensive support for loading environment variables from both `.env` files and the shell environment, with proper precedence handling where shell variables take priority over file-based ones.

The implementation adds a new env_utils module that handles .env file parsing with support for quoted values, comments, and validation. The LocalInstanceConfig struct now includes an optional env_file field that allows users to specify custom environment files in their helix.toml configuration. The Docker integration has been enhanced to dynamically load these user-defined environment variables while filtering out HELIX_ prefixed variables to prevent conflicts with internal system variables.

Key components of the implementation:

  • Environment Variable Loading: The load_env_variables function in env_utils.rs provides robust parsing of .env files with proper error handling and user feedback
  • Configuration Integration: The env_file field is seamlessly integrated into the existing configuration system with proper serde defaults
  • Docker Integration: The DockerManager now dynamically generates environment variables for containers, combining built-in Helix variables with user-defined ones
  • Migration Support: The migration system properly handles the new configuration field to ensure backward compatibility

This change fits well with the existing codebase architecture, following established patterns for configuration management and Docker integration. The modular design keeps environment handling separate while integrating cleanly with existing systems.

Important Files Changed

Changed Files

| Filename | Score | Overview |
|----------|-------|
| helix-cli/Cargo.toml | 4/5 | Removed trailing newline - goes against standard conventions that files should end with newlines |
| helix-cli/src/commands/add.rs | 5/5 | Added proper initialization of env_file field to None for new local instances |
| helix-cli/src/main.rs | 5/5 | Added env_utils module declaration to integrate environment variable handling |
| helix-cli/src/commands/migrate.rs | 5/5 | Added env_file field initialization during v1 to v2 project migration |
| helix-cli/src/docker.rs | 4/5 | Enhanced DockerManager with dynamic environment variable loading - potential issues with error propagation |
| helix-cli/src/config.rs | 5/5 | Added optional env_file field to LocalInstanceConfig with proper serde defaults |
| helix-cli/src/commands/integrations/fly.rs | 5/5 | Fixed error handling for environment_variables method call with proper ? operator |
| helix-cli/src/env_utils.rs | 4/5 | New module implementing comprehensive environment variable handling - quote parsing logic could be more robust |

Sequence Diagram

sequenceDiagram
    participant User
    participant CLI as Helix CLI
    participant PM as ProjectManager
    participant Docker as DockerManager
    participant EnvUtils as env_utils
    participant FileSystem as File System

    User->>CLI: "helix init/add/build/push <instance>"
    CLI->>PM: "find_and_load(path)"
    PM->>FileSystem: "read helix.toml"
    FileSystem-->>PM: "config data"
    PM-->>CLI: "ProjectContext"

    CLI->>PM: "get_instance(instance_name)"
    PM-->>CLI: "InstanceInfo"

    alt Local Instance with env_file
        CLI->>Docker: "environment_variables(instance_name)"
        Docker->>PM: "get_instance(instance_name)"
        PM-->>Docker: "LocalInstanceConfig"
        Docker->>EnvUtils: "load_env_variables(env_file, project_root)"
        EnvUtils->>FileSystem: "read_to_string(env_file)"
        FileSystem-->>EnvUtils: "env file content"
        EnvUtils->>EnvUtils: "parse KEY=VALUE pairs"
        EnvUtils->>EnvUtils: "get shell environment vars"
        EnvUtils-->>Docker: "HashMap<String, String>"
        Docker->>Docker: "merge with HELIX_ variables"
        Docker-->>CLI: "Vec<String> env vars"
    end

    CLI->>Docker: "generate_docker_compose(instance, config)"
    Docker->>Docker: "environment_variables(instance)"
    Docker-->>CLI: "docker-compose.yml content"

    CLI->>FileSystem: "write docker-compose.yml"
    CLI->>Docker: "build_image(instance)"
    Docker->>FileSystem: "docker compose build"
Loading

@xav-db xav-db marked this pull request as draft October 7, 2025 13:12
Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

8 files reviewed, 3 comments

Edit Code Review Agent Settings | Greptile

}

Ok(vars)
} No newline at end of file
Copy link
Contributor

Choose a reason for hiding this comment

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

style: missing newline at end of file

Prompt To Fix With AI
This is a comment left during a code review.
Path: helix-cli/src/env_utils.rs
Line: 70:70

Comment:
**style:** missing newline at end of file

How can I resolve this? If you propose a fix, please make it concise.

Comment on lines +42 to +47
let value = if (value.starts_with('"') && value.ends_with('"'))
|| (value.starts_with('\'') && value.ends_with('\'')) {
&value[1..value.len() - 1]
} else {
value
};
Copy link
Contributor

Choose a reason for hiding this comment

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

logic: quote removal logic could fail on single-character quoted strings due to potential underflow in value.len() - 1

Suggested change
let value = if (value.starts_with('"') && value.ends_with('"'))
|| (value.starts_with('\'') && value.ends_with('\'')) {
&value[1..value.len() - 1]
} else {
value
};
let value = if value.len() >= 2 && ((value.starts_with('"') && value.ends_with('"'))
|| (value.starts_with('\'') && value.ends_with('\''))) {
&value[1..value.len() - 1]
} else {
value
};
Prompt To Fix With AI
This is a comment left during a code review.
Path: helix-cli/src/env_utils.rs
Line: 42:47

Comment:
**logic:** quote removal logic could fail on single-character quoted strings due to potential underflow in `value.len() - 1`

```suggestion
                    let value = if value.len() >= 2 && ((value.starts_with('"') && value.ends_with('"'))
                        || (value.starts_with('\'') && value.ends_with('\''))) {
                        &value[1..value.len() - 1]
                    } else {
                        value
                    };
```

How can I resolve this? If you propose a fix, please make it concise.


[[bin]]
name = "helix_test_env_parser"
path = "src/test_env_parser.rs"
Copy link
Contributor

Choose a reason for hiding this comment

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

style: Missing newline at end of file

Prompt To Fix With AI
This is a comment left during a code review.
Path: helix-cli/Cargo.toml
Line: 40:40

Comment:
**style:** Missing newline at end of file

How can I resolve this? If you propose a fix, please make it concise.

@xav-db xav-db changed the title implementing environment variable handling feat (cli): implementing environment variable handling Oct 7, 2025
@xav-db xav-db marked this pull request as ready for review October 7, 2025 16:43
@xav-db xav-db marked this pull request as draft October 7, 2025 16:43
Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

2 files reviewed, no comments

Edit Code Review Agent Settings | Greptile

@xav-db xav-db marked this pull request as ready for review October 8, 2025 17:36
Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

8 files reviewed, no comments

Edit Code Review Agent Settings | Greptile

@xav-db xav-db mentioned this pull request Oct 29, 2025
8 tasks
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.

2 participants