miracle2k / django-assets

Django webassets integration.
BSD 2-Clause "Simplified" License
89 stars 79 forks source link

Can't resolve Django environment in 404 contexts #11

Closed julen closed 11 years ago

julen commented 11 years ago

Hi,

Lately I'm receiving lots of BundleError exceptions because the underlying files django-assets is trying to read don't exist apparently (IOError is raised). The source files defined in the bundles do exist though, as well as the merged bundles.

For example, I have defined a bundle like this:

css_common = Bundle(
    'css/style.css', 'css/fancybox.css', 'css/tipsy.css',
    'css/markup.css', 'css/sprite.css', 'css/select2.css',
    filters='cssmin', output='css/common.min.css')
register('css_common', css_common)

And referenced it in the templates:

{% assets "css_common" %}                                                    
  <link rel="stylesheet" type="text/css" href="{{ ASSET_URL }}" />             
{% endassets %}

All works fine so far and I can even confirm it from a manage.py shell session:

In [1]: from myapp.assets import css_common

In [2]: css_common.urls()
Out[2]: ['/assets/css/common.min.css?5e87f161']

In [3]: css_common
Out[3]: <Bundle output=css/common.min.css, filters=[<webassets.filter.cssmin.CSSMin object at 0xa7b610c>], contents=('css/style.css', 'css/fancybox.css', 'css/tipsy.css', 'css/markup.css', 'css/sprite.css', 'css/select2.css')>

In [4]: from django_assets import env

In [5]: env.get_env()['css_common']
Out[5]: <Bundle output=css/common.min.css, filters=[<webassets.filter.cssmin.CSSMin object at 0xa7b610c>], contents=('css/style.css', 'css/fancybox.css', 'css/tipsy.css', 'css/markup.css', 'css/sprite.css', 'css/select2.css')>

On the other hand, from the tracebacks I can see the bundle is not being properly populated with the expected contents:

<Bundle output=None, filters=(), contents=(u'css_common',)>

After analyzing webassets' and django-assets' source code, I suspect the environment isn't being properly set and instead it treats the bundle identifier as a filename, therefore raising IOError at a later stage.

This seems to happen when visiting a non-existent URL and a 404 page is returned. Any relevant settings that might affect this are set to False, such as DEBUG, ASSETS_DEBUG, and ASSETS_AUTO_BUILD

miracle2k commented 11 years ago

Hm, I've never had any problem in a 404 context. Can you find out why get_env(), in that case, would not return an Environment that contains your "css_common" bundle?

julen commented 11 years ago

It might only be a coincidence that the error appeared in 404 environments. Due to the arbitrary nature of the issue and the given effects, I think this is a thread-safety issue as it's been newly-reported in issue #12 and miracle2k/webassets#217.