{"record":{"id":"722623f5809a8a35","repo":"nodejs/node","slug":"tried-to-select-from-an-empty-list-of-templates","errorCode":null,"errorMessage":"Tried to select from an empty list of templates.","messagePattern":"Tried to select from an empty list of templates\\.","errorType":"exception","errorClass":"TemplatesNotFound","httpStatus":null,"severity":"error","filePath":"tools/inspector_protocol/jinja2/environment.py","lineNumber":845,"sourceCode":"            return name\n        if parent is not None:\n            name = self.join_path(name, parent)\n        return self._load_template(name, self.make_globals(globals))\n\n    @internalcode\n    def select_template(self, names, parent=None, globals=None):\n        \"\"\"Works like :meth:`get_template` but tries a number of templates\n        before it fails.  If it cannot find any of the templates, it will\n        raise a :exc:`TemplatesNotFound` exception.\n\n        .. versionadded:: 2.3\n\n        .. versionchanged:: 2.4\n           If `names` contains a :class:`Template` object it is returned\n           from the function unchanged.\n        \"\"\"\n        if not names:\n            raise TemplatesNotFound(message=u'Tried to select from an empty list '\n                                            u'of templates.')\n        globals = self.make_globals(globals)\n        for name in names:\n            if isinstance(name, Template):\n                return name\n            if parent is not None:\n                name = self.join_path(name, parent)\n            try:\n                return self._load_template(name, globals)\n            except TemplateNotFound:\n                pass\n        raise TemplatesNotFound(names)\n\n    @internalcode\n    def get_or_select_template(self, template_name_or_list,\n                               parent=None, globals=None):\n        \"\"\"Does a typecheck and dispatches to :meth:`select_template`\n        if an iterable of template names is given, otherwise to","sourceCodeStart":827,"sourceCodeEnd":863,"githubUrl":"https://github.com/nodejs/node/blob/1b2de5e052fc0fb95fd7fb6846dcec4ade598e9e/tools/inspector_protocol/jinja2/environment.py#L827-L863","documentation":"Raised by Environment.select_template (jinja2 environment.py) as a TemplatesNotFound when the `names` argument is empty or falsy. select_template iterates a list of candidate template names and returns the first that loads; passing an empty list (or None) means there is nothing to try, which the method treats as an explicit error rather than silently returning.","triggerScenarios":"Calling `env.select_template([])`; passing a list that was built dynamically and ended up empty; passing None where a list was expected.","commonSituations":"Building the candidate list from configuration/user input that yielded zero entries; falling back through a chain of optional templates where all were filtered out upstream; defaulting a parameter to [] and not replacing it.","solutions":["Provide at least one fallback template name as the last element of the list.","Guard the call: `if names: env.select_template(names) else: <default behavior>`.","Ensure the list-building step always includes a guaranteed-to-exist default."],"exampleFix":"# before\nenv.select_template(candidates)  # candidates may be []\n# after\nif candidates:\n    tpl = env.select_template(candidates)\nelse:\n    tpl = env.get_template('default.html')","handlingStrategy":"validation","validationCode":"def safe_select(env, names, default=None):\n    if not names:\n        if default is None:\n            raise ValueError('no template names provided')\n        return env.get_template(default)\n    return env.select_template(names)","typeGuard":"def has_candidates(names) -> bool:\n    return bool(names)","tryCatchPattern":"from jinja2 import TemplatesNotFound\ntry:\n    tpl = env.select_template(names)\nexcept TemplatesNotFound as e:\n    if 'empty list' in str(e):\n        tpl = env.get_template('default.html')\n    else:\n        raise","preventionTips":["Always include a guaranteed default as the last candidate.","Guard select_template calls when the list is built dynamically.","Distinguish the empty-list error from the none-found error in handlers."],"tags":["jinja2","template","selection","validation"],"backgroundTag":null,"analyzedSha":"1b2de5e052fc0fb95fd7fb6846dcec4ade598e9e","analyzedAt":"2026-08-13T00:53:24.642Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}