evansd / whitenoise

Radically simplified static file serving for Python web apps
https://whitenoise.readthedocs.io
MIT License
2.55k stars 151 forks source link

Warning shown if STATIC_ROOT directory missing in development #191

Open edmorley opened 6 years ago

edmorley commented 6 years ago

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:

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).

evansd commented 6 years ago

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?

edmorley commented 6 years ago

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

urlsangel commented 6 years ago

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.

evansd commented 6 years ago

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

urlsangel commented 6 years ago

@evansd

Great stuff, thanks for your work on this! 👍

TheBitShepherd commented 3 years ago

@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?

kirankumbhar commented 3 years ago

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

michjnich commented 3 years ago

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

pawelad commented 2 years ago

Still here in 6.2.

banagale commented 1 year ago

I noticed this while running tests locally.

This can be worked around either by:

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