Skip to content

Commit

Permalink
FileSystemLoader include paths in error (#1663)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidism authored Dec 20, 2024
2 parents b4b28ec + 227edfd commit 58a358f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ Unreleased
- Fix `copy`/`pickle` support for the internal ``missing`` object.
:issue:`2027`
- ``Environment.overlay(enable_async)`` is applied correctly. :pr:`2061`
- The error message from ``FileSystemLoader`` includes the paths that were
searched. :issue:`1661`


Version 3.1.4
Expand Down
7 changes: 6 additions & 1 deletion src/jinja2/loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,12 @@ def get_source(
if os.path.isfile(filename):
break
else:
raise TemplateNotFound(template)
plural = "path" if len(self.searchpath) == 1 else "paths"
paths_str = ", ".join(repr(p) for p in self.searchpath)
raise TemplateNotFound(
template,
f"{template!r} not found in search {plural}: {paths_str}",
)

with open(filename, encoding=self.encoding) as f:
contents = f.read()
Expand Down
18 changes: 18 additions & 0 deletions tests/test_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,24 @@ def test_filename_normpath(self):
t = e.get_template("foo/test.html")
assert t.filename == str(self.searchpath / "foo" / "test.html")

def test_error_includes_paths(self, env, filesystem_loader):
env.loader = filesystem_loader

with pytest.raises(TemplateNotFound) as info:
env.get_template("missing")

e_str = str(info.value)
assert e_str.startswith("'missing' not found in search path: ")

filesystem_loader.searchpath.append("other")

with pytest.raises(TemplateNotFound) as info:
env.get_template("missing")

e_str = str(info.value)
assert e_str.startswith("'missing' not found in search paths: ")
assert ", 'other'" in e_str


class TestModuleLoader:
archive = None
Expand Down

0 comments on commit 58a358f

Please sign in to comment.