From 4af162d4de5d60cef42e4707d821c1d6c0c99be0 Mon Sep 17 00:00:00 2001
From: Carlton Gibson <carlton.gibson@noumenal.es>
Date: Wed, 30 Jun 2021 14:15:25 +0200
Subject: [PATCH] Refs #32144 -- Made makemessages remove temporary files on
 preprocessing error.
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Co-authored-by: Anders Hovmöller <anders.hovmoller@dryft.se>
---
 django/core/management/commands/makemessages.py            | 5 +++++
 tests/i18n/commands/templates/template_0_with_no_error.tpl | 6 ++++++
 tests/i18n/test_extraction.py                              | 3 ++-
 3 files changed, 13 insertions(+), 1 deletion(-)
 create mode 100644 tests/i18n/commands/templates/template_0_with_no_error.tpl

diff --git a/django/core/management/commands/makemessages.py b/django/core/management/commands/makemessages.py
index 09346b4d27..6374ed0d11 100644
--- a/django/core/management/commands/makemessages.py
+++ b/django/core/management/commands/makemessages.py
@@ -527,6 +527,11 @@ class Command(BaseCommand):
                     )
                 )
                 continue
+            except BaseException:
+                # Cleanup before exit.
+                for build_file in build_files:
+                    build_file.cleanup()
+                raise
             build_files.append(build_file)
 
         if self.domain == 'djangojs':
diff --git a/tests/i18n/commands/templates/template_0_with_no_error.tpl b/tests/i18n/commands/templates/template_0_with_no_error.tpl
new file mode 100644
index 0000000000..8abd38e603
--- /dev/null
+++ b/tests/i18n/commands/templates/template_0_with_no_error.tpl
@@ -0,0 +1,6 @@
+{% load i18n %}
+{% blocktranslate %}Hello{% endblocktranslate %}
+
+This file has a name that should be lexicographically before
+'template_with_error.tpl' so that we can test the cleanup case
+of the first file being successful, but the second failing.
diff --git a/tests/i18n/test_extraction.py b/tests/i18n/test_extraction.py
index 3ba371f66c..124d35313f 100644
--- a/tests/i18n/test_extraction.py
+++ b/tests/i18n/test_extraction.py
@@ -226,8 +226,9 @@ class BasicExtractorTests(ExtractorTests):
         )
         with self.assertRaisesMessage(SyntaxError, msg):
             management.call_command('makemessages', locale=[LOCALE], extensions=['tpl'], verbosity=0)
-        # The temporary file was cleaned up
+        # The temporary files were cleaned up.
         self.assertFalse(os.path.exists('./templates/template_with_error.tpl.py'))
+        self.assertFalse(os.path.exists('./templates/template_0_with_no_error.tpl.py'))
 
     def test_unicode_decode_error(self):
         shutil.copyfile('./not_utf8.sample', './not_utf8.txt')