Repository: facebookresearch/hydra

Issue:
Overriding hydra defaults in job config is not working properly
hydra/hydra_logging override is not merged into defaults properly, and instead being appended to the end of the list.

config:
```
defaults:
- hydra/launcher: null
- hydra/hydra_logging: hydra_debug
- hydra/job_logging: disabled
```
Expected load history:
```
   [
        ("hydra.yaml", "pkg://hydra.conf", "hydra"),
        ("hydra/hydra_logging/hydra_debug.yaml", "pkg://hydra.conf", "hydra"),
        ("hydra/job_logging/disabled.yaml", "pkg://hydra.conf", "hydra"),
        ("hydra/sweeper/basic.yaml", "pkg://hydra.conf", "hydra"),
        ("hydra/output/default.yaml", "pkg://hydra.conf", "hydra"),
        ("overriding_logging_default.yaml", "tests/configs", "test"),
    ]
```
Actual:
```
[('hydra.yaml', 'pkg://hydra.conf', 'hydra'),
 ('hydra/hydra_logging/default.yaml', 'pkg://hydra.conf', 'hydra'),
 ('hydra/job_logging/disabled.yaml', 'pkg://hydra.conf', 'hydra'),
 ('hydra/sweeper/basic.yaml', 'pkg://hydra.conf', 'hydra'),
 ('hydra/output/default.yaml', 'pkg://hydra.conf', 'hydra'),
 ('overriding_logging_default.yaml', 'tests/configs', 'test'),
 ('hydra/hydra_logging/hydra_debug.yaml', 'pkg://hydra.conf', 'hydra')]
 
```


Discussion:

Resolving patch:
diff --git a/hydra/_internal/config_loader.py b/hydra/_internal/config_loader.py
--- a/hydra/_internal/config_loader.py
+++ b/hydra/_internal/config_loader.py
@@ -172,7 +172,7 @@ def get_key(d1):
             if isinstance(d, (dict, DictConfig)):
                 key = get_key(d)
                 key_to_idx[key] = idx
-        for d in merged_list:
+        for d in copy.deepcopy(merged_list):
             if isinstance(d, (dict, DictConfig)):
                 key = get_key(d)
                 if key in key_to_idx.keys():
@@ -406,16 +406,3 @@ def _validate_defaults(defaults):
             elif isinstance(default, str):
                 # single file to load
                 pass
-
-    @staticmethod
-    def _update_defaults(cfg, defaults_changes):
-        for default in cfg.defaults or []:
-            if isinstance(default, DictConfig):
-                for key in default.keys():
-                    if key != "optional":
-                        if key in defaults_changes:
-                            default[key] = defaults_changes[key]
-                            del defaults_changes[key]
-        # unmatched new defaults, put at end of list to be loaded normally
-        for key, value in defaults_changes.items():
-            cfg.defaults.append({key: value})


Test patch:
diff --git a/tests/configs/overriding_logging_default.yaml b/tests/configs/overriding_logging_default.yaml
new file mode 100644
--- /dev/null
+++ b/tests/configs/overriding_logging_default.yaml
@@ -0,0 +1,4 @@
+defaults:
+- hydra/launcher: null
+- hydra/hydra_logging: hydra_debug
+- hydra/job_logging: disabled
\ No newline at end of file
diff --git a/tests/test_conf_loader.py b/tests/test_conf_loader.py
--- a/tests/test_conf_loader.py
+++ b/tests/test_conf_loader.py
@@ -263,7 +263,7 @@ def test_resource_exists(module_name, resource_name):
     assert pkg_resources.resource_exists(module_name, resource_name) is True
 
 
-def test_override_hydra_config_group_from_config_file():
+def test_override_hydra_config_value_from_config_file():
     config_loader = ConfigLoader(
         config_search_path=create_search_path(["tests/configs"]),
         strict_cfg=False,
@@ -272,3 +272,21 @@ def test_override_hydra_config_group_from_config_file():
 
     cfg = config_loader.load_configuration(overrides=[])
     assert cfg.hydra.run.dir == "foo"
+
+
+def test_override_hydra_config_group_from_config_file():
+    config_loader = ConfigLoader(
+        config_search_path=create_search_path(["tests/configs"]),
+        strict_cfg=False,
+        config_file="overriding_logging_default.yaml",
+    )
+
+    config_loader.load_configuration(overrides=[])
+    assert config_loader.get_load_history() == [
+        ("hydra.yaml", "pkg://hydra.conf", "hydra"),
+        ("hydra/hydra_logging/hydra_debug.yaml", "pkg://hydra.conf", "hydra"),
+        ("hydra/job_logging/disabled.yaml", "pkg://hydra.conf", "hydra"),
+        ("hydra/sweeper/basic.yaml", "pkg://hydra.conf", "hydra"),
+        ("hydra/output/default.yaml", "pkg://hydra.conf", "hydra"),
+        ("overriding_logging_default.yaml", "tests/configs", "test"),
+    ]