Skip to content

Empty runtime config file produces exception #551

@tyhoff

Description

@tyhoff

Issue

When running Invoke with an empty runtime config file, the following exception is hit.

$ inv --list
Traceback (most recent call last):
  File "/Users/username/miniconda2/envs/env/bin/inv", line 11, in <module>
    sys.exit(program.run())
  File "/Users/username/miniconda2/envs/env/lib/python2.7/site-packages/invoke/program.py", line 313, in run
    self.create_config()
  File "/Users/username/miniconda2/envs/env/lib/python2.7/site-packages/invoke/program.py", line 243, in create_config
    self.config = self.config_class()
  File "/Users/username/miniconda2/envs/env/lib/python2.7/site-packages/invoke/config.py", line 641, in __init__
    self.merge()
  File "/Users/username/miniconda2/envs/env/lib/python2.7/site-packages/invoke/config.py", line 927, in merge
    self._merge_file('user', "Per-user")
  File "/Users/username/miniconda2/envs/env/lib/python2.7/site-packages/invoke/config.py", line 951, in _merge_file
    merge_dicts(self._config, data)
  File "/Users/username/miniconda2/envs/env/lib/python2.7/site-packages/invoke/config.py", line 1170, in merge_dicts
    for key, value in updates.items():
AttributeError: 'NoneType' object has no attribute 'items'

Desired Result

Passing in a valid, empty .yml file should not throw an exception.

Steps to reproduce

  1. Create a new directory
  2. Create a simple tasks.py file
from invoke import task, Collection, Config

@task
def hello(ctx):
    print("Hello {}".format(ctx.name))
  1. Create an empty ~/.invoke.yaml file
  2. Run invoke --list

Fix

I think it's reasonable to just return early if the dict being passed in is None.

diff --git a/invoke/config.py b/invoke/config.py
index 6d13ac08..84501133 100644
--- a/invoke/config.py
+++ b/invoke/config.py
@@ -1174,6 +1174,9 @@ def merge_dicts(base, updates):

     .. versionadded:: 1.0
     """
+    if not updates:
+        return base
+
     # TODO: for chrissakes just make it return instead of mutating?
     for key, value in updates.items():
         # Dict values whose keys also exist in 'base' -> recurse

Let me know if you need any more information or want me to throw up a commit!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions