Repository: facebookresearch/hydra

Issue:
[Bug] Cannot use experiment pattern when using overrides in main config file
# 🐛 Bug
When using any override in main config, e.g. for color logging or sweeper:
```yaml
defaults:
    - override hydra/hydra_logging: colorlog
    - override hydra/job_logging: colorlog
```

Then the experiment configs can't be loaded:
```bash
python main.py +experiment=experiment_name
```
Stack trace:
```
In config.yaml: Override 'hydra/job_logging : colorlog' is defined before 'experiment: experiment_name'.
Overrides must be at the end of the defaults list
```

## System information
- **Hydra Version** :  1.1.0.dev5
- **Python version** : 3.8
- **Virtual environment type and version** : conda
- **Operating system** : Ubuntu20.04

## Additional context
I believe this issue didn't exist on 1.1.0.dev4.



Discussion:
Thanks for reporting.
For now, try this as a workaround:
```yaml
defaults:
    - experiment: null
    - override hydra/hydra_logging: colorlog
    - override hydra/job_logging: colorlog
```

Resolving patch:
diff --git a/hydra/_internal/defaults_list.py b/hydra/_internal/defaults_list.py
--- a/hydra/_internal/defaults_list.py
+++ b/hydra/_internal/defaults_list.py
@@ -464,7 +464,14 @@ def _create_defaults_tree_impl(
         self_added = _validate_self(containing_node=parent, defaults=defaults_list)
 
     if is_root_config:
-        defaults_list.extend(overrides.append_group_defaults)
+        # To ensure config overrides are last, insert the external overrides before the first config override.
+        insert_idx = len(defaults_list)
+        for idx, default in enumerate(defaults_list):
+            if default.is_override():
+                insert_idx = idx
+                break
+
+        defaults_list[insert_idx:insert_idx] = overrides.append_group_defaults
 
     _update_overrides(defaults_list, overrides, parent, interpolated_subtree)
 


Test patch:
diff --git a/tests/defaults_list/test_defaults_tree.py b/tests/defaults_list/test_defaults_tree.py
--- a/tests/defaults_list/test_defaults_tree.py
+++ b/tests/defaults_list/test_defaults_tree.py
@@ -2161,6 +2161,28 @@ def test_none_config_with_hydra(
             ),
             id="defaults_with_override_only",
         ),
+        param(
+            "defaults_with_override_only",
+            ["+group1=file1"],
+            DefaultsTreeNode(
+                node=VirtualRoot(),
+                children=[
+                    DefaultsTreeNode(
+                        node=ConfigDefault(path="hydra/config"),
+                        children=[
+                            GroupDefault(group="help", value="custom1"),
+                            GroupDefault(group="output", value="default"),
+                            ConfigDefault(path="_self_"),
+                        ],
+                    ),
+                    DefaultsTreeNode(
+                        node=ConfigDefault(path="defaults_with_override_only"),
+                        children=[GroupDefault(group="group1", value="file1")],
+                    ),
+                ],
+            ),
+            id="defaults_with_override_only",
+        ),
     ],
 )
 def test_defaults_with_overrides_only(