whitenoise icon indicating copy to clipboard operation
whitenoise copied to clipboard

Warning shown if STATIC_ROOT directory missing in development

Open edmorley opened this issue 6 years ago • 10 comments

Hi! :-)

STR:

  1. Update existing Django+WhiteNoise project from whitenoise 3.3.1 to 4.0.
  2. Don't run collectstatic (ie since developing locally), so the directory referenced by STATIC_ROOT doesn't yet exist
  3. With Django's DEBUG = True, run ./manage.py runserver

Expected:

No console warnings about a missing static files directory.

Actual:

Warning in the console:

...
Django version 1.11.15, using settings 'treeherder.config.settings'
Starting development server at http://0.0.0.0:8000/
Quit the server with CONTROL-C.
/usr/local/lib/python2.7/site-packages/whitenoise/base.py:97: UserWarning: No directory at: /app/treeherder/static/
  warnings.warn(u'No directory at: {}'.format(root))

Notes:

  • The warning was added to add_files() in 2a883f5cda0336dbefebef7bb918c3a2b167a70d.
  • We don't pre-create the STATIC_ROOT directory, since:
    • Django's collectstatic handles the creation of the directory automatically
    • To create the directory we'd either need to use the .gitkeep type approach (which then causes collectstatic to output misleading warnings/prompts about overwriting existing files) or else have additional local setup steps prior to running ./manage.py runserver.

A possible solution might be to have .add_files() take an optional ignore_missing=False argument, which is left as False everywhere apart from the Django middleware static root usage. eg:

if self.static_root:
    self.add_files(self.static_root, prefix=self.static_prefix, ignore_missing=self.autorefresh)

That said, should static_root even be added when run in DEBUG mode? The Django docs say not to place anything in the STATIC_ROOT directory directly, so it feels like there shouldn't be content there (though how many people go against that, I don't know).

edmorley avatar Aug 13 '18 15:08 edmorley

Thanks Ed, I hadn't considered this situation. I wonder if the correct behaviour would be just not to warn about missing directories if autorefresh is enabled. What do you think?

evansd avatar Aug 13 '18 15:08 evansd

Yeah I think skipping the warning entirely if autorefresh enabled, might be the simplest thing :-)

edmorley avatar Aug 16 '18 13:08 edmorley

Hi

I've just started a new project with Whitenoise 4.0 and have exactly this problem - will there be a release that removes this warning in local dev?

Thanks.

urlsangel avatar Sep 12 '18 08:09 urlsangel

@urlsangel Yep, the 4.1 release should be out soon which will include this fix

evansd avatar Sep 12 '18 08:09 evansd

@evansd

Great stuff, thanks for your work on this! 👍

urlsangel avatar Sep 12 '18 08:09 urlsangel

@evansd - I'm using version 5.2.0 and still seeing this warning, is there something that I need to set on my end to eliminate this?

TheBitShepherd avatar Jan 08 '21 18:01 TheBitShepherd

I can confirm what @Br4nd0R is saying. This issue is still there in 5.2.0

kirankumbhar avatar Feb 15 '21 06:02 kirankumbhar

Same here - 5.2.0 - I'm guessing a recent commit accidentally put it back in?

michjnich avatar Mar 24 '21 19:03 michjnich

Still here in 6.2.

pawelad avatar Jun 25 '22 15:06 pawelad

I noticed this while running tests locally.

This can be worked around either by:

  • Running ./manage.py collectstatic and having this dir exist before running tests.
  • Define a test runner which is referenced using the TEST_RUNNER setting, and in that runner add the setting: WHITENOISE_AUTOREFRESH = True

As for why this warning is being triggered even though it was though to have been fixed:

WhiteNoise.add_files() was changed late 2018 (after 4.1) from defining the root directory as:

root = root.rstrip(os.path.sep) + os.path.sep

to its current:

root = os.path.abspath(root)

The difference between these in 3.11 is the current version returns the path without the final forward slash.

That said, the test of existence, os.path.isdir(root) returns False for me either way (triggering a warning in each case).

The original commit merged in 4.1 seems to have included a test stub.

Currently, the test that should cover tests for the existence of a single warning.

I wonder if a warning is being issued but it isn't the one expected by the failed directory check

banagale avatar Dec 28 '22 06:12 banagale