Repository: facebookresearch/hydra

Issue:
[Bug] Config search path for hydra apps
# 🐛 Bug

For packaged hydra apps, `pkg://` rather than `file://` should be used in the config search path. 

## To reproduce

Install `hydra_app` and run `hydra_app --info`.

## Stack trace/error message

```
Config search path
******************
| Provider | Search path        
--------------------------------------------------------------------------------------------
| hydra    | pkg://hydra.conf  
| main     | file:///Users/jm/Git/hydra/examples/advanced/hydra_app_example/hydra_app/conf 
| schema   | structured:// 
--------------------------------------------------------------------------------------------
```

## Expected Behavior

```
Config search path
******************
| Provider | Search path  
--------------------------------------------------------------------------------------------
| hydra    | pkg://hydra.conf
| main     | pkg://hydra_app.conf
| schema   | structured://
--------------------------------------------------------------------------------------------
```

## System information
- **Hydra Version** :  `master` at `dc8bed5a4d4a0324357f5baeae97962e759cdc6b`
- **Python version** :  `3.7.7`
- **Virtual environment type and version** : `conda`
- **Operating system** :  macOS

## Additional context

Using `pkg://` rather than `file://` would e.g. help with making configurations accessible across machines when using remote launchers where `file://`-based paths may differ.


Discussion:
how did you install the app? `pip install` or `pip install -e` ?
The output above was using `pip install .` from inside `examples/advanced/hydra_app_example/`. 

With `pip install -e .` I'd get `file://path_to_git_repo/examples/...`.
let me ask it in another way:
if you package a wheel, and pip install it, does it depend on the directory where you packaged it?

try this:
1. package a wheel.
2. move it to /tmp
3. rename the directory
4. install and run the wheel. 

Does it work or not?
It will use `file://` to point to the `site-packages` folder (no reference to `/tmp`). However, I would have expected usage of `pkg://` (as done for `hydra` itself). How could that be achieved?
I would have expected it too, but for some reason the logic detects this as a file run.
feel free to dig into the detection logic to see why. this is low pri for now because it seems to be working fine regardless.

Resolving patch:
diff --git a/hydra/_internal/utils.py b/hydra/_internal/utils.py
--- a/hydra/_internal/utils.py
+++ b/hydra/_internal/utils.py
@@ -43,10 +43,15 @@ def _get_module_name_override() -> Optional[str]:
 def detect_calling_file_or_module_from_task_function(
     task_function: Any,
 ) -> Tuple[Optional[str], Optional[str], str]:
-    mdl = _get_module_name_override()
+
+    mdl = task_function.__module__
+    override = _get_module_name_override()
+    if override is not None:
+        mdl = override
+
     calling_file: Optional[str]
     calling_module: Optional[str]
-    if mdl is not None:
+    if mdl not in (None, "__main__"):
         calling_file = None
         calling_module = mdl
     else:


Test patch:
diff --git a/examples/advanced/hydra_app_example/tests/test_installed_app.py b/examples/advanced/hydra_app_example/tests/test_installed_app.py
--- a/examples/advanced/hydra_app_example/tests/test_installed_app.py
+++ b/examples/advanced/hydra_app_example/tests/test_installed_app.py
@@ -1,4 +1,5 @@
 # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
+import re
 import subprocess
 import sys
 
@@ -30,6 +31,11 @@ def test_installed_run(self, tmpdir: str) -> None:
             )
         )
 
+    # test that installed app configs are loaded via pkg://hydra_app.conf
+    def test_installed_run_searchpath(self, tmpdir: str) -> None:
+        output = subprocess.check_output(["hydra_app", "--info"]).decode("utf-8")
+        assert re.search(re.escape("pkg://hydra_app.conf"), output) is not None
+
     @staticmethod
     def verify_output(result: bytes) -> None:
         assert str(result.decode("utf-8")).strip() == "Hello test_user, 10 + 20 = 30"