Repository: iterative/dvc

Issue:
plots: code templates not found
# Bug Report

Since 2.8.2 my code base templates are not located as they used to be via `dvc plots show/diff -t <template_name>`.

As far as I can tell this is due to the path root being prefixed to the template name before matching

https://github.com/iterative/dvc/blob/e2d705fd76771c184f85a477f8d73aea40a96cab/dvc/repo/plots/template.py#L123-L129

At face value the fix may just be 

```python
templates = [
    file
    for root, _, files in os.walk(self.templates_dir)
    for file in files
]
```


Discussion:
Hi @mattlbeck ! There was a breaking change in https://github.com/iterative/dvc/pull/6550 but the docs have not been updated yet to document the new behavior 🙏 (you can find more info in the description of the P.R. though) .

Were you using custom templates or just the default included in DVC?
Also, could you share the output of `dvc doctor`? It is known that the templates are missing in binary installations (https://github.com/iterative/dvc/pull/6550#issuecomment-944194967)
```
DVC version: 2.8.2 (pip)
---------------------------------
Platform: Python 3.9.1 on macOS-10.16-x86_64-i386-64bit
Supports:
        webhdfs (fsspec = 2021.10.1),
        http (aiohttp = 3.7.4.post0, aiohttp-retry = 2.4.5),
        https (aiohttp = 3.7.4.post0, aiohttp-retry = 2.4.5),
        s3 (s3fs = 2021.10.1, boto3 = 1.17.106)
Cache types: reflink, hardlink, symlink
Cache directory: apfs on /dev/disk1s1s1
Caches: local
Remotes: s3
Workspace directory: apfs on /dev/disk1s1s1
Repo: dvc, git
```
I have a custom template I am using e.g. `.dvc/plots/mycustomplot.json`

The template is not found if I use `dvc plots show -t mycustomplot`, which was the correct old behaviour. It is found if I use the full path e.g. `dvc plots show -t .dvc/plots/mycustomplot.json`.

Is this the intended behaviour? It also breaks our current dvc.yaml as the templates are also specified there. I guess I could also fix this by moving the template to the working directory (repo root), but I would personally prefer to store custom plots in the `.dvc/plots` directory as this feels better organised.
@pared I don't think these changes were intended to be breaking, correct?
@mattlbeck no its not intended behaviour, let me look into that.

Resolving patch:
diff --git a/dvc/repo/plots/template.py b/dvc/repo/plots/template.py
--- a/dvc/repo/plots/template.py
+++ b/dvc/repo/plots/template.py
@@ -109,16 +109,16 @@ def templates_dir(self):
     @staticmethod
     def _find(templates, template_name):
         for template in templates:
-            if (
-                template_name == template
-                or template_name + ".json" == template
+            if template.endswith(template_name) or template.endswith(
+                template_name + ".json"
             ):
                 return template
         return None
 
     def _find_in_project(self, name: str) -> Optional["StrPath"]:
-        if os.path.exists(name):
-            return name
+        full_path = os.path.abspath(name)
+        if os.path.exists(full_path):
+            return full_path
 
         if os.path.exists(self.templates_dir):
             templates = [


Test patch:
diff --git a/tests/unit/render/test_vega.py b/tests/unit/render/test_vega.py
--- a/tests/unit/render/test_vega.py
+++ b/tests/unit/render/test_vega.py
@@ -451,3 +451,29 @@ def test_find_vega(tmp_dir, dvc):
         first(plot_content["layer"])["encoding"]["x"]["field"] == INDEX_FIELD
     )
     assert first(plot_content["layer"])["encoding"]["y"]["field"] == "y"
+
+
+@pytest.mark.parametrize(
+    "template_path, target_name",
+    [
+        (os.path.join(".dvc", "plots", "template.json"), "template"),
+        (os.path.join(".dvc", "plots", "template.json"), "template.json"),
+        (
+            os.path.join(".dvc", "plots", "subdir", "template.json"),
+            os.path.join("subdir", "template.json"),
+        ),
+        (
+            os.path.join(".dvc", "plots", "subdir", "template.json"),
+            os.path.join("subdir", "template"),
+        ),
+        ("template.json", "template.json"),
+    ],
+)
+def test_should_resolve_template(tmp_dir, dvc, template_path, target_name):
+    os.makedirs(os.path.abspath(os.path.dirname(template_path)), exist_ok=True)
+    with open(template_path, "w", encoding="utf-8") as fd:
+        fd.write("template_content")
+
+    assert dvc.plots.templates._find_in_project(
+        target_name
+    ) == os.path.abspath(template_path)