diff --git a/src/python/pants/backend/tools/preamble/rules.py b/src/python/pants/backend/tools/preamble/rules.py index 1a70dfa42d3..ee088e54003 100644 --- a/src/python/pants/backend/tools/preamble/rules.py +++ b/src/python/pants/backend/tools/preamble/rules.py @@ -23,10 +23,20 @@ class PreambleRequest(FmtFilesRequest): @memoized def _template_checker_regex(template: str) -> re.Pattern: - maybe_shebang = r"(#!.*\n)?" + shebang = r"(?P#!.*\n)" + if re.match(shebang, template): + # If the template already contains a shebang, don't attempt to match one. + shebang = r"(?P)" + maybe_shebang = f"{shebang}?" + subbed = string.Template(template).safe_substitute(year=r"====YEAR====") raw_regex = re.escape(subbed).replace("====YEAR====", r"\d{4}") - return re.compile(maybe_shebang + raw_regex) + maybe_template = f"(?P