Skip to content

πŸ™Œ OpenHands: AI-Driven Development

[bug] add list support in setting attributes from env variables (#8295)

rbrugaro3606ca87d558
diff --git a/openhands/core/config/utils.py b/openhands/core/config/utils.py
index 4882c0286c..f90525585d 100644
--- a/openhands/core/config/utils.py
+++ b/openhands/core/config/utils.py
@@ -89,8 +89,10 @@ def load_from_env(
# Attempt to cast the env var to type hinted in the dataclass
if field_type is bool:
cast_value = str(value).lower() in ['true', '1']
- # parse dicts like SANDBOX_RUNTIME_STARTUP_ENV_VARS
- elif get_origin(field_type) is dict:
+ # parse dicts and lists like SANDBOX_RUNTIME_STARTUP_ENV_VARS and SANDBOX_RUNTIME_EXTRA_BUILD_ARGS β”‚
+ elif (
+ get_origin(field_type) is dict or get_origin(field_type) is list
+ ):
cast_value = literal_eval(value)
else:
if field_type is not None:
diff --git a/tests/unit/test_config.py b/tests/unit/test_config.py
index 0f96b95c94..ef77396b60 100644
--- a/tests/unit/test_config.py
+++ b/tests/unit/test_config.py
@@ -344,6 +344,33 @@ user_id = 1001
assert default_config.sandbox.user_id == 1001
+def test_load_from_env_with_list(monkeypatch, default_config):
+ """Test loading list values from environment variables, particularly SANDBOX_RUNTIME_EXTRA_BUILD_ARGS."""
+ # Set the environment variable with a list-formatted string
+ monkeypatch.setenv(
+ 'SANDBOX_RUNTIME_EXTRA_BUILD_ARGS',
+ '['
+ + ' "--add-host=host.docker.internal:host-gateway",'
+ + ' "--build-arg=https_proxy=https://my-proxy:912",'
+ + ']',
+ )
+
+ # Load configuration from environment
+ load_from_env(default_config, os.environ)
+
+ # Verify that the list was correctly parsed
+ assert isinstance(default_config.sandbox.runtime_extra_build_args, list)
+ assert len(default_config.sandbox.runtime_extra_build_args) == 2
+ assert (
+ '--add-host=host.docker.internal:host-gateway'
+ in default_config.sandbox.runtime_extra_build_args
+ )
+ assert (
+ '--build-arg=https_proxy=https://my-proxy:912'
+ in default_config.sandbox.runtime_extra_build_args
+ )
+
+
def test_security_config_from_toml(default_config, temp_toml_file):
"""Test loading security specific configurations."""
with open(temp_toml_file, 'w', encoding='utf-8') as toml_file: