Opened 5 days ago

Last modified 7 hours ago

#36010 assigned Bug

compilemessages reports errors only once

Reported by: Balazs Endresz Owned by: amansharma612
Component: Internationalization Version: dev
Severity: Normal Keywords:
Cc: Balazs Endresz Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

When calling compilemessages for a locale that has invalid entries it prints out the errors correctly first.
But any subsequent calls just say that it's already compiled and up to date.
This is because msgfmt updates the last modified date of the mo file even if it's invalid, and then django will skip that locale afterwards:
https://github.com/django/django/blob/main/django/core/management/commands/compilemessages.py#L153

This can reproduced by calling compilemessages twice in django/tests/i18n/test_compilation.py:

class CompilationErrorHandling(MessageCompilationTests):
    def test_error_reported_by_msgfmt(self):
        # po file contains wrong po formatting.
        with self.assertRaises(CommandError):
            call_command("compilemessages", locale=["ja"], verbosity=0)

        # subsequent calls should still fail
        with self.assertRaises(CommandError):
            call_command("compilemessages", locale=["ja"], verbosity=0)  # currently this doesn't raise CommandError

One way of fixing this is perhaps doing the validation separately first, e.g. with mo_path = "NUL" if os.name == "nt" else "/dev/null" and then calling msgfmt again with the actual mo_path only if that passes.

Change History (4)

comment:1 by Claude Paroz, 5 days ago

Triage Stage: UnreviewedAccepted
Version: 5.1dev

Good catch! I think your suggested resolution strategy makes sense.

comment:2 by amansharma612, 5 days ago

Owner: set to amansharma612
Status: newassigned

comment:3 by amansharma612, 9 hours ago

I don't think that the error is due to msgfmt updating the last modified date, as I found that to not be the case when reproducing the bug.

The following is mentioned in the comments for is_writable

    # Known side effect: updating file access/modified time to current time if
    # it is writable.

Checking is_writable on 'mo_path modifies the date. Nonetheless, the suggested fix seems reasonable, or there could be a way to implement is_writable` without modifying the date.

Version 1, edited 9 hours ago by amansharma612 (previous) (next) (diff)

comment:4 by Claude Paroz, 7 hours ago

I cannot remember if I didn't use os.access(file_path, os.W_OK) on purpose or if I didn't know that method at the time. To be tested.

Note: See TracTickets for help on using tickets.
Back to Top