Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FileNotFoundError: No such file or directory (static) #1318

Closed
paulschreiber opened this issue Sep 17, 2020 · 13 comments · Fixed by #1319
Closed

FileNotFoundError: No such file or directory (static) #1318

paulschreiber opened this issue Sep 17, 2020 · 13 comments · Fixed by #1319
Labels

Comments

@paulschreiber
Copy link

Same as #1123.

I installed django-debug-toolbar. When I start my app, I get this error:
FileNotFoundError: [Errno 2] No such file or directory: '/Users/paul/.dev/police-records/static'

Here's the backtrace:

Internal Server Error: /
Traceback (most recent call last):
  File "/Users/paul/.dev/police-records/.direnv/python-3.8.5/lib/python3.8/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/Users/paul/.dev/police-records/.direnv/python-3.8.5/lib/python3.8/site-packages/debug_toolbar/middleware.py", line 92, in __call__
    panel.generate_stats(request, response)
  File "/Users/paul/.dev/police-records/.direnv/python-3.8.5/lib/python3.8/site-packages/debug_toolbar/panels/staticfiles.py", line 128, in generate_stats
    "staticfiles_finders": self.get_staticfiles_finders(),
  File "/Users/paul/.dev/police-records/.direnv/python-3.8.5/lib/python3.8/site-packages/debug_toolbar/panels/staticfiles.py", line 140, in get_staticfiles_finders
    for path, finder_storage in finder.list([]):
  File "/Users/paul/.dev/police-records/.direnv/python-3.8.5/lib/python3.8/site-packages/django/contrib/staticfiles/finders.py", line 130, in list
    for path in utils.get_files(storage, ignore_patterns):
  File "/Users/paul/.dev/police-records/.direnv/python-3.8.5/lib/python3.8/site-packages/django/contrib/staticfiles/utils.py", line 23, in get_files
    directories, files = storage.listdir(location)
  File "/Users/paul/.dev/police-records/.direnv/python-3.8.5/lib/python3.8/site-packages/django/core/files/storage.py", line 316, in listdir
    for entry in os.scandir(path):
FileNotFoundError: [Errno 2] No such file or directory: '/Users/paul/.dev/police-records/static'
@tim-schilling
Copy link
Contributor

Does that directory exist?

@paulschreiber
Copy link
Author

It didn't. Creating it didn't change the error.

@tim-schilling
Copy link
Contributor

Odd. Give collectstatic a go, but I doubt that'll fix it. Also please include your static files settings, version of django and version of the toolbar.

@paulschreiber
Copy link
Author

Versions:

Django==3.0.5
django-debug-toolbar==2.2

Settings:

STATIC_URL = '/static/'

STATICFILES_DIRS = [
	os.path.join(BASE_DIR, "static"),
	os.path.join(BASE_DIR, "case-manager", "public"),
	os.path.join(BASE_DIR, "case-intake", "public")
]

collectstatic initially failed because the directory didn't exist:

FileNotFoundError: [Errno 2] No such file or directory: '/Users/paul/.dev/police-records/static'

After mkdir static, I got this error:

django.core.exceptions.ImproperlyConfigured: You're using the staticfiles app without having set the STATIC_ROOT setting to a filesystem path.

I added this to settings:

STATIC_ROOT = os.path.join(BASE_DIR, "static")

I ran collectstatic again:

Found another file with the destination path 'index.html'. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.
Found another file with the destination path 'bundle.css'. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.
Found another file with the destination path 'bundle.js.map'. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.
Found another file with the destination path 'bundle.css.map'. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.
Found another file with the destination path 'bundle.js'. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.
Found another file with the destination path 'favicon.png'. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.
Found another file with the destination path 'images/loader.svg'. It will be ignored since only the first encountered file is collected. If this is not what you want, make sure every static file has a unique path.

214 static files copied to '/Users/paul/.dev/police-records/static'.

runserver now fails:

Traceback (most recent call last):
  File "/usr/local/Cellar/python@3.8/3.8.5/Frameworks/Python.framework/Versions/3.8/lib/python3.8/threading.py", line 932, in _bootstrap_inner
    self.run()
  File "/usr/local/Cellar/python@3.8/3.8.5/Frameworks/Python.framework/Versions/3.8/lib/python3.8/threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "/Users/paul/.dev/police-records/.direnv/python-3.8.5/lib/python3.8/site-packages/django/utils/autoreload.py", line 53, in wrapper
    fn(*args, **kwargs)
  File "/Users/paul/.dev/police-records/.direnv/python-3.8.5/lib/python3.8/site-packages/django/core/management/commands/runserver.py", line 117, in inner_run
    self.check(display_num_errors=True)
  File "/Users/paul/.dev/police-records/.direnv/python-3.8.5/lib/python3.8/site-packages/django/core/management/base.py", line 441, in check
    raise SystemCheckError(msg)
django.core.management.base.SystemCheckError: SystemCheckError: System check identified some issues:

ERRORS:
?: (staticfiles.E002) The STATICFILES_DIRS setting should not contain the STATIC_ROOT setting.

@matthiask
Copy link
Member

matthiask commented Sep 18, 2020

You shouldn't put non-existing folders in STATICFILES_DIRS, that's not supported by Django.

Also, since Django collects files from apps and from STATICFILES_DIRS into STATIC_ROOT, STATIC_ROOT shouldn't be in STATICFILES_DIRS. A folder shouldn't be a source and a target of static files at the same time.

@paulschreiber
Copy link
Author

Running mkdir static and setting STATIC_ROOT = os.path.join(BASE_DIR, "staticroot")fixed the problem.

Perhaps debug toolbar can do some sort of configuration sanity check and complain if:

  • a folder specified by STATICFILES_DIRS doesn't exist
  • STATIC_ROOT isn't defined
  • STATIC_ROOT is inside STATICFILES_DIRS

…or other similar errors that prevent debug toolbar from starting.

@matthiask
Copy link
Member

All of these checks make sense, but it seems to me they belong into Django itself. At first sight.

@paulschreiber
Copy link
Author

I agree in principle. However (a) My Django app ran fine with these items in their incorrect state and (b) it might be hard to (or take a long time to) convince the Django team to include this in a release.

If you don't think it's worth make changes to debug toolbar, hopefully this GitHub issue will help the next person solve the problem.

@matthiask
Copy link
Member

Well, there certainly is a django-debug-toolbar bug to be fixed – django-debug-toolbar should still work even if other applications are configured incorrectly (and mention that staticfiles couldn't be collected, in this case)

Maybe it's even worth it adding our own warnings in specific cases. This may be such a case. I'm torn.

So: Certainly +1 on fixing the crash, -0 on adding checks for other apps. +0 for adding a check if other apps would silently ignore their misconfiguration but django-debug-toolbar doesn't / cannot.

@tim-schilling
Copy link
Contributor

@matthiask for this particular case are you suggesting that we log a warning that the path specified didn't exist? Looking for clarification so I can write up a PR.

I agree that I'm hesitant to have the toolbar start acting as a config/project debugger as there's a lot of functionality that would need to be built for that. However, it could be argued that it's within the toolbar's domain.

@matthiask
Copy link
Member

matthiask commented Sep 18, 2020

@tim-schilling Thanks for the suggestion, that should work well!

I had in mind that we'd show a note about it in the toolbar itself, but that would probably be much more work without being more useful at all (resp. even less useful, in my case the probability is bigger that I see a warning in the runserver console than somewhere inside a panel)

tim-schilling added a commit to tim-schilling/django-debug-toolbar that referenced this issue Sep 18, 2020
If the django static files are misconfigured in some way or a
directory doesn't exist, the toolbar should still run, but
attempt to alert the user to the issue.

Fixes jazzband#1318
@valentijnscholten
Copy link

Just letting you know I am running into the same thing. Seems like having non-existing statis files dir is working fine with Django, they just get skipped. So might be better to log at debug level as otherwise the logs gets spammed with warnings?
The usecase I am in is that in some scenario's the static files get served by nginx, so they are not present in django. Also sometimes the local node_modules is used locally, but in production django relies on collectstatic.
Would of couse be cleaner to have different settings for every different scenario, but that's no always practical.

tim-schilling added a commit to tim-schilling/django-debug-toolbar that referenced this issue Sep 26, 2020
Adds support for toolbar panels to validate the configuration/
integration for their own purposes.

If the django static files are misconfigured in some way or a
directory doesn't exist, the toolbar should still run, but
attempt to alert the user to the issue.

Fixes jazzband#1318
tim-schilling added a commit to tim-schilling/django-debug-toolbar that referenced this issue Oct 1, 2020
Adds support for toolbar panels to validate the configuration/
integration for their own purposes.

If the django static files are misconfigured in some way or a
directory doesn't exist, the toolbar should still run, but
attempt to alert the user to the issue.

Fixes jazzband#1318
matthiask pushed a commit that referenced this issue Oct 2, 2020
…1319)

Adds support for toolbar panels to validate the configuration/
integration for their own purposes.

If the django static files are misconfigured in some way or a
directory doesn't exist, the toolbar should still run, but
attempt to alert the user to the issue.

Fixes #1318
@chriford
Copy link

chriford commented Aug 5, 2023

I fixed the FileNotFoundError:

  • it was caused by a wrong static folder path that I specified in my STATICFILES_DIRS,

Please Note: this error cannot be caused by a third party library because django picks only one global setting and so if you specify a global setting e.g STATICFILES_DIRS, it will override the one that would for instance be set in the third party package.

  • if you are sure that it's the third party library that's causing the error try commenting all the configs for that library; django-debug-toolbar, and then try collecting the static files once more. If the error is still popping up then you that you are messing up the static configurations somewhere and you could focus on just that and then if it works, you uncomment what you commented out.

HAPPY CODING!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
5 participants