Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion compose/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,10 @@ def resolve_environment(service_dict, environment=None):
"""
env = {}
for env_file in service_dict.get('env_file', []):
env.update(env_vars_from_file(env_file))
try:
env.update(env_vars_from_file(env_file))
except ConfigurationError as exc:
log.warn('{} - ignoring'.format(exc.msg))
Copy link

@nilbus nilbus Sep 19, 2017

Choose a reason for hiding this comment

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

Though unlikely, a missing file isn't the only reason a ConfigurationError could be thrown. When the path exists but is not a file (e.g. a directory), a ConfigurationError will also be thrown. In that case, a blocking error seems more appropriate than a passive warning.

Instead of a try/except, perhaps we should check file existence directly.

if os.path.exists(env_file):
  env.update(env_vars_from_file(env_file))
else:
  log.warn('env_file {} does not exist - ignoring'.format(env_file))

Choose a reason for hiding this comment

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

If @akalipetis makes this change, would this be merge-able?

Copy link

@AlJohri AlJohri Mar 8, 2018

Choose a reason for hiding this comment

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

EDIT: removed. sorry missed to read the thread.


env.update(parse_environment(service_dict.get('environment')))
return dict(resolve_env_var(k, v, environment) for k, v in six.iteritems(env))
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/config/config_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2936,13 +2936,13 @@ def test_resolve_environment_with_multiple_env_files(self):
)

def test_resolve_environment_nonexistent_file(self):
with pytest.raises(ConfigurationError) as exc:
with mock.patch('compose.config.config.log') as mock_logging:
config.load(build_config_details(
{'foo': {'image': 'example', 'env_file': 'nonexistent.env'}},
working_dir='tests/fixtures/env'))

assert 'Couldn\'t find env file' in exc.exconly()
assert 'nonexistent.env' in exc.exconly()
assert 'Couldn\'t find env file' in mock_logging.warn.call_args[0][0]
assert 'nonexistent.env' in mock_logging.warn.call_args[0][0]

@mock.patch.dict(os.environ)
def test_resolve_environment_from_env_file_with_empty_values(self):
Expand Down