# Buggy code
def _apply_defaults_overrides(overrides, defaults):
    consumed = []
    key_to_idx = {}
    for idx, d in enumerate(defaults):
        if isinstance(d, DictConfig):
            key = next(iter(d.keys()))
            key_to_idx[key] = idx
    for override in copy.deepcopy(overrides):
        key, value = override.split("=")
        if key in key_to_idx:
            # Do not add sweep configs into defaults, those will be added to the sweep config
            # after the list is broken into items
            if "," not in value:
                if value == "null":
                    del defaults[key_to_idx[key]]
                else:
                    defaults[key_to_idx[key]][key] = value
                overrides.remove(override)
                consumed.append(override)
    return consumed


# Traceback (KeyError)
(tutorial_env) dasneha@devfair0254:~/tutorials/hydra$ python my_app.py -m exp=1,2
Traceback (most recent call last):
File "my_app.py", line 7, in <module>
my_app()
File "/private/home/dasneha/.conda/envs/tutorial_env/lib/python3.7/site-packages/hydra/main.py", line 25, in decorated_main
strict,
File "/private/home/dasneha/.conda/envs/tutorial_env/lib/python3.7/site-packages/hydra/_internal/utils.py", line 52, in run_hydra
hydra.multirun(overrides=args.overrides)
File "/private/home/dasneha/.conda/envs/tutorial_env/lib/python3.7/site-packages/hydra/_internal/hydra.py", line 113, in multirun
cfg = self._load_config(overrides)
File "/private/home/dasneha/.conda/envs/tutorial_env/lib/python3.7/site-packages/hydra/_internal/hydra.py", line 259, in _load_config
cfg = self.config_loader.load_configuration(overrides)
File "/private/home/dasneha/.conda/envs/tutorial_env/lib/python3.7/site-packages/hydra/_internal/config_loader.py", line 66, in load_configuration
cfg.merge_with_dotlist(remaining_overrides)
File "/private/home/dasneha/.conda/envs/tutorial_env/lib/python3.7/site-packages/omegaconf/config.py", line 240, in merge_with_dotlist
self.update(key, value)
File "/private/home/dasneha/.conda/envs/tutorial_env/lib/python3.7/site-packages/omegaconf/config.py", line 260, in update
root[last] = value
File "/private/home/dasneha/.conda/envs/tutorial_env/lib/python3.7/site-packages/omegaconf/dictconfig.py", line 28, in __setitem__
raise KeyError("Accessing unknown key in a struct : {}".format(self.get_full_key(key)))
KeyError: 'Accessing unknown key in a struct : exp'

# Fixed code
def _apply_defaults_overrides(overrides, defaults):
    consumed = []
    key_to_idx = {}
    for idx, d in enumerate(defaults):
        if isinstance(d, DictConfig):
            key = next(iter(d.keys()))
            key_to_idx[key] = idx
    for override in copy.deepcopy(overrides):
        key, value = override.split("=")
        if key in key_to_idx:
            # Do not add multirun configs into defaults, those will be added to the multirun config
            # after the list is broken into items
            if "," not in value:
                if value == "null":
                    del defaults[key_to_idx[key]]
                else:
                    defaults[key_to_idx[key]][key] = value
            overrides.remove(override)
            consumed.append(override)
    return consumed