﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
36368	`makemessages` command runs `write_po_file()` multiple times for the same potfile	Michał Pokusa	Michał Pokusa	"This is both a Bug and a Cleanup/optimization issue.

In some cases, the `makemessages` management command will run `write_po_file()` multiple times per the same potfile and locale.

1. It happens because inside `handle()`, the `write_po_file()` method is called in a for loop for every `potfile`:
https://github.com/django/django/blob/main/django/core/management/commands/makemessages.py#L459

2. `potfiles` is a `list`, and that makes it possible for it to have duplicates, which is exactly the case when the bug described here happens.
Inside `build_potfiles()`, used for `potfiles` inside `handle()`, inside a for loop for every path in `self.locale_paths`, the returned list is appended.
https://github.com/django/django/blob/main/django/core/management/commands/makemessages.py#L513

3. The `self.locale_paths` is also a list, and it also can have duplicates, because e.g. when the `<project root>/locale` folder is present, it is being appended twice:
https://github.com/django/django/blob/main/django/core/management/commands/makemessages.py#L384

4. Later, it is possible that inside the `find_files()`, the same locale path is inserted to `self.locale_paths` once again, which makes it possible to have 2 duplicates or 3 in total of the same locale path.

I propose the solution to change `self.locale_paths` from `list` to `set`, and do the same for `potfiles` inside `handle()`, after doing that the problems seems to be fixed. This also shouldn't break anything, because it looks like this is how it is supposed to work, meaning ony one occurence of each value.

If the issue is accepted I would be happy to be assigned to it and make a PR.
"	Bug	closed	Core (Management commands)	5.2	Normal	fixed	makemessages write_po_file	Claude Paroz	Ready for checkin	1	0	0	0	0	0
