# Buggy code
def compile_pillar(self, ext=True, pillar_dirs=None):
    """
    Render the pillar data and return
    """
    top, top_errors = self.get_top()
    if ext:
        if self.opts.get("ext_pillar_first", False):
            self.opts["pillar"], errors = self.ext_pillar({}, pillar_dirs)
            matches = self.top_matches(top)
            pillar, errors = self.render_pillar(matches, errors=errors)
            pillar = merge(
                pillar,
                self.opts["pillar"],
                self.merge_strategy,
                self.opts.get("renderer", "yaml"),
                self.opts.get("pillar_merge_lists", False),
            )
        else:
            matches = self.top_matches(top)
            pillar, errors = self.render_pillar(matches)
            pillar, errors = self.ext_pillar(pillar, pillar_dirs, errors=errors)
    else:
        matches = self.top_matches(top)
        pillar, errors = self.render_pillar(matches)
    errors.extend(top_errors)
    if self.opts.get("pillar_opts", False):
        mopts = dict(self.opts)
        if "grains" in mopts:
            mopts.pop("grains")
        # Restore the actual file_roots path. Issue 5449
        mopts["file_roots"] = self.actual_file_roots
        mopts["saltversion"] = __version__
        pillar["master"] = mopts
    if errors:
        for error in errors:
            log.critical("Pillar render error: {0}".format(error))
        pillar["_errors"] = errors

    if self.pillar_override and isinstance(self.pillar_override, dict):
        pillar = merge(
            pillar,
            self.pillar_override,
            self.merge_strategy,
            self.opts.get("renderer", "yaml"),
            self.opts.get("pillar_merge_lists", False),
        )

    return pillar


# Traceback (KeyError)
2016-04-24 07:31:51,215 [salt.minion      ][DEBUG   ][2175] compound_match: minion1 ? *
2016-04-24 07:31:51,215 [salt.minion      ][DEBUG   ][2175] compound_match minion1 ? "*" => "True"
2016-04-24 07:31:51,216 [salt.template    ][DEBUG   ][2175] compile template: /srv/pillar/common.sls
2016-04-24 07:31:51,219 [salt.pillar      ][CRITICAL][2175] Rendering SLS 'common' failed, render error:
Traceback (most recent call last):
File "/usr/lib/python2.7/site-packages/salt/utils/templates.py", line 178, in render_tmpl
output = render_str(tmplstr, context, tmplpath)
File "/usr/lib/python2.7/site-packages/salt/utils/templates.py", line 315, in render_jinja_tmpl
loader = salt.utils.jinja.SaltCacheLoader(opts, saltenv, pillar_rend=context.get('_pillar_rend', False))
File "/usr/lib/python2.7/site-packages/salt/utils/jinja.py", line 72, in __init__
self.searchpath = opts['file_roots'][saltenv]
KeyError: 'dev'

2016-04-24 07:31:51,219 [salt.pillar      ][CRITICAL][2175] Pillar render error: Rendering SLS 'common' failed. Please see master log for details.

# Fixed code
def compile_pillar(self, ext=True, pillar_dirs=None):
    """
    Render the pillar data and return
    """
    top, top_errors = self.get_top()
    if ext:
        if self.opts.get("ext_pillar_first", False):
            self.opts["pillar"], errors = self.ext_pillar({}, pillar_dirs)
            self.rend = salt.loader.render(self.opts, self.functions)
            matches = self.top_matches(top)
            pillar, errors = self.render_pillar(matches, errors=errors)
            pillar = merge(
                pillar,
                self.opts["pillar"],
                self.merge_strategy,
                self.opts.get("renderer", "yaml"),
                self.opts.get("pillar_merge_lists", False),
            )
        else:
            matches = self.top_matches(top)
            pillar, errors = self.render_pillar(matches)
            pillar, errors = self.ext_pillar(pillar, pillar_dirs, errors=errors)
    else:
        matches = self.top_matches(top)
        pillar, errors = self.render_pillar(matches)
    errors.extend(top_errors)
    if self.opts.get("pillar_opts", False):
        mopts = dict(self.opts)
        if "grains" in mopts:
            mopts.pop("grains")
        # Restore the actual file_roots path. Issue 5449
        mopts["file_roots"] = self.actual_file_roots
        mopts["saltversion"] = __version__
        pillar["master"] = mopts
    if errors:
        for error in errors:
            log.critical("Pillar render error: {0}".format(error))
        pillar["_errors"] = errors

    if self.pillar_override and isinstance(self.pillar_override, dict):
        pillar = merge(
            pillar,
            self.pillar_override,
            self.merge_strategy,
            self.opts.get("renderer", "yaml"),
            self.opts.get("pillar_merge_lists", False),
        )

    return pillar