Repository: mkdocs/mkdocs

Issue:
error while building extra_templates
I am testing mkdocs and I get the following backtrace.

```
Traceback (most recent call last):
  File "/srv/mkdocs_head/bin/mkdocs", line 9, in <module>
    load_entry_point('mkdocs==0.14.0.dev', 'console_scripts', 'mkdocs')()
  File "/srv/mkdocs_head/local/lib/python2.7/site-packages/click/core.py", line 664, in __call__
    return self.main(*args, **kwargs)
  File "/srv/mkdocs_head/local/lib/python2.7/site-packages/click/core.py", line 644, in main
    rv = self.invoke(ctx)
  File "/srv/mkdocs_head/local/lib/python2.7/site-packages/click/core.py", line 991, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/srv/mkdocs_head/local/lib/python2.7/site-packages/click/core.py", line 837, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/srv/mkdocs_head/local/lib/python2.7/site-packages/click/core.py", line 464, in invoke
    return callback(*args, **kwargs)
  File "/home/zmousm/mkdocs/mkdocs/cli.py", line 134, in build_command
    ), clean_site_dir=clean)
  File "/home/zmousm/mkdocs/mkdocs/build.py", line 299, in build
    build_pages(config)
  File "/home/zmousm/mkdocs/mkdocs/build.py", line 252, in build_pages
    build_extra_templates(config['extra_templates'], config, site_navigation)
  File "/home/zmousm/mkdocs/mkdocs/build.py", line 230, in build_extra_templates
    output_content = template.render(context)
  File "/srv/mkdocs_head/local/lib/python2.7/site-packages/jinja2/environment.py", line 969, in render
    return self.environment.handle_exception(exc_info, True)
  File "/srv/mkdocs_head/local/lib/python2.7/site-packages/jinja2/environment.py", line 742, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "<template>", line 44, in top-level template code
TypeError: no loader for this environment specified
```

It appears to be related to `extra_templates`, even though I am not using any such thing in my config; I have actually removed any theme/theme_dir setting I had in the config and I still get this.

I can see `build_extra_templates()` was introduced in 26f2465; I can reproduce the same problem in every release after 0.12.2.

I am testing on a Debian wheezy system (python 2.7.3), in a newly created virtualenv where the following modules have been installed:

```
Jinja2==2.7.3
Markdown==2.6.2
MarkupSafe==0.23
PyYAML==3.11
backports.ssl-match-hostname==3.4.0.2
certifi==2015.04.28
click==4.0
distribute==0.6.24
livereload==2.4.0
-e git+git@github.com:zmousm/mkdocs.git@169e091bd86621dc616b05c8d7200d97cc37d0ac#egg=mkdocs-dev
six==1.9.0
tornado==4.2
```

Any ideas?



Discussion:
Well, I found the problem. I had `theme_dir` pointing to a subdirectory of `docs_dir`, so mkdocs indeed finds templates, which are there to override theme templates, however. Once I move `theme_dir outside`docs_dir`, the issue goes away.

I don't know if this backtrace should be expected though? The docs don't warn against this.

Yup, that's a fair point. I hadn't considered people putting theme_dir's within the docs_dirs. I think maybe we should make extra_templates an opt-in feature only. At the moment MkDocs will try to build any html or XML files it finds in the docs directory.


Resolving patch:
diff --git a/mkdocs/config/config_options.py b/mkdocs/config/config_options.py
--- a/mkdocs/config/config_options.py
+++ b/mkdocs/config/config_options.py
@@ -311,7 +311,7 @@ class Extras(OptionallyRequired):
     if not provided.
     """
 
-    def __init__(self, file_match, **kwargs):
+    def __init__(self, file_match=None, **kwargs):
         super(Extras, self).__init__(**kwargs)
         self.file_match = file_match
 
@@ -324,6 +324,10 @@ def run_validation(self, value):
                 "Expected a list, got {0}".format(type(value)))
 
     def walk_docs_dir(self, docs_dir):
+
+        if self.file_match is None:
+            raise StopIteration
+
         for (dirpath, _, filenames) in os.walk(docs_dir):
             for filename in sorted(filenames):
                 fullpath = os.path.join(dirpath, filename)
diff --git a/mkdocs/config/defaults.py b/mkdocs/config/defaults.py
--- a/mkdocs/config/defaults.py
+++ b/mkdocs/config/defaults.py
@@ -84,8 +84,7 @@
 
     # Similar to the above, but each template (HTML or XML) will be build with
     # Jinja2 and the global context.
-    ('extra_templates', config_options.Extras(
-        file_match=utils.is_template_file)),
+    ('extra_templates', config_options.Extras()),
 
     # Determine if the site should include the nav and next/prev elements.
     # Default, True if the site has more than one page, False otherwise.


Test patch: