whitenoise icon indicating copy to clipboard operation
whitenoise copied to clipboard

[Question] Under what set of circumstances is WHITENOISE_MANIFEST_STRICT evaluated?

Open socketbox opened this issue 1 year ago • 6 comments

Python Version

3.10

Django Version

4.2

Package Version

6.6.0

Description

If I'm missing certain files in one of the directories that is sourced by the collectstatic process, the end result is an application image that falls over in the runtime environment with a ValueError that is raised at line 513 in Django's django/contrib/staticfiles/storage.py:

url = self.url(context)
File  "/home/pbsorg/.cache/pypoetry/virtualenvs/pbsorg-cRclfdpy-py3.10/lib/python3.10/site-packages/django/templatetags/static.py",  line 113, in url
return self.handle_simple(path)
File  "/home/pbsorg/.cache/pypoetry/virtualenvs/pbsorg-cRclfdpy-py3.10/lib/python3.10/site-packages/django/templatetags/static.py",  line 129, in handle_simple
return staticfiles_storage.url(path)
File  "/home/pbsorg/.cache/pypoetry/virtualenvs/pbsorg-cRclfdpy-py3.10/lib/python3.10/site-packages/django/contrib/staticfiles/storage.py",  line 203, in url
return self._url(self.stored_name, name, force)
File  "/home/pbsorg/.cache/pypoetry/virtualenvs/pbsorg-cRclfdpy-py3.10/lib/python3.10/site-packages/django/contrib/staticfiles/storage.py",  line 182, in _url
hashed_name = hashed_name_func(*args)
File  "/home/pbsorg/.cache/pypoetry/virtualenvs/pbsorg-cRclfdpy-py3.10/lib/python3.10/site-packages/django/contrib/staticfiles/storage.py",  line 513, in stored_name
raise ValueError(
ValueError: Missing staticfiles manifest entry for 'styles/shows-landing.css'

In an attempt to avoid this, I set WHITENOISE_MANIFEST_STRICT to False, because I see that, in the relevant Django code, that flag is checked, and, if True the ValueError that shows up in my stack trace is raised:

  def stored_name(self, name):
        parsed_name = urlsplit(unquote(name))
        clean_name = parsed_name.path.strip()
        hash_key = self.hash_key(clean_name)
        cache_name = self.hashed_files.get(hash_key)
        if cache_name is None:
            if self.manifest_strict:
                raise ValueError(
                    "Missing staticfiles manifest entry for '%s'" % clean_name
                )
            cache_name = self.clean_name(self.hashed_name(name))
        unparsed_name = list(parsed_name)
        unparsed_name[2] = cache_name
        # Special casing for a @font-face hack, like url(myfont.eot?#iefix")
        # http://www.fontspring.com/blog/the-new-bulletproof-font-face-syntax
        if "?#" in name and not unparsed_name[3]:
            unparsed_name[2] += "?"
        return urlunsplit(unparsed_name)

However, it's only checked if cache_name is None.

The explanation offered in Issue 289 doesn't really jibe with what I'm inferring from the code: there seems to be only one point in both the whitenoise and Django codebases where the manifest_strict flag is evaluated in branching logic. Not only that, but I don't see how it's possible that an incomplete (ie. missing a one-to-one mapping of filename to hashed filename) staticfiles.json manifest would be created if the files that are collected during the collectstatic process are all present.

In trying to understand why I'm still seeing this ValueError in stack traces despite having set WHITENOISE_MANIFEST_STRICT to false, I've questioned whether or not I've actually configured Whitenoise correctly. Here's my configuration:

STORAGES = {
    "default": {
        "BACKEND": "django.core.files.storage.FileSystemStorage",
    },
    "staticfiles": {
        "BACKEND": "whitenoise.storage.CompressedManifestStaticFilesStorage",
    },
}
STATIC_URL = "/static/"
WHITENOISE_MANIFEST_STRICT = "False"

Any advice or insight would be appreciated.

socketbox avatar Apr 24 '24 13:04 socketbox

Python is strongly typed and "False" is a truthy string.

socketbox avatar May 01 '24 23:05 socketbox

Thank you for returning to the issue and commenting with the problem. I hope you didn’t spend too long debugging it!

adamchainz avatar May 03 '24 20:05 adamchainz

Why did you reopen this? An accident?

adamchainz avatar May 06 '24 21:05 adamchainz

Why did you reopen this? An accident?

No, I wanted to ask you a question regarding the CompressedManifestStaticFilesStorage class: it seems that by setting WHITENOISE_MANIFEST_STRICT to False, I can successfully collectstatic even when I'm missing some static assets. However, later, when I deploy the application, a ValueError causes the application to fail completely in its deployed environment.

What's the intent behind this flag?

socketbox avatar May 06 '24 22:05 socketbox

Same issue, WHITENOISE_MANIFEST_STRICT=False doesn't work with CompressedManifestStaticFilesStorage.

Got:

ValueError: The file 'somefile.example' could not be found with <whitenoise.storage.CompressedManifestStaticFilesStorage object at 0xffffb70ade50>.

mike667 avatar Jun 04 '24 09:06 mike667