miracle2k / django-assets

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

BundleError: Bundle is not connected to an environment #39

Closed julen closed 10 years ago

julen commented 10 years ago

I wanted to use the fix for #12, but since no version of django-assets has been released in a while I had to install the git version, relying on webassets' git version as well (django-assets from git + webassets 0.9 doesn't work).

With these versions I end up with the following error:

Traceback:
File "/Users/julen/.virtualenvs/pootle/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  118.                         response = middleware_method(request, e)
File "/Users/julen/dev/translate/pootle/pootle/middleware/errorpages.py" in process_exception
  99.                                      RequestContext(request))
File "/Users/julen/.virtualenvs/pootle/lib/python2.7/site-packages/django/template/loader.py" in render_to_string
  169.         return t.render(context_instance)
File "/Users/julen/.virtualenvs/pootle/lib/python2.7/site-packages/django/template/base.py" in render
  140.             return self._render(context)
File "/Users/julen/.virtualenvs/pootle/lib/python2.7/site-packages/django/test/utils.py" in instrumented_test_render
  85.     return self.nodelist.render(context)
File "/Users/julen/.virtualenvs/pootle/lib/python2.7/site-packages/django/template/base.py" in render
  840.                 bit = self.render_node(node, context)
File "/Users/julen/.virtualenvs/pootle/lib/python2.7/site-packages/django/template/debug.py" in render_node
  78.             return node.render(context)
File "/Users/julen/.virtualenvs/pootle/lib/python2.7/site-packages/django/template/loader_tags.py" in render
  123.         return compiled_parent._render(context)
File "/Users/julen/.virtualenvs/pootle/lib/python2.7/site-packages/django/test/utils.py" in instrumented_test_render
  85.     return self.nodelist.render(context)
File "/Users/julen/.virtualenvs/pootle/lib/python2.7/site-packages/django/template/base.py" in render
  840.                 bit = self.render_node(node, context)
File "/Users/julen/.virtualenvs/pootle/lib/python2.7/site-packages/django/template/debug.py" in render_node
  78.             return node.render(context)
File "/Users/julen/.virtualenvs/pootle/lib/python2.7/site-packages/django/template/loader_tags.py" in render
  62.             result = block.nodelist.render(context)
File "/Users/julen/.virtualenvs/pootle/lib/python2.7/site-packages/django/template/base.py" in render
  840.                 bit = self.render_node(node, context)
File "/Users/julen/.virtualenvs/pootle/lib/python2.7/site-packages/django/template/debug.py" in render_node
  78.             return node.render(context)
File "/Users/julen/.virtualenvs/pootle/src/django-assets/django_assets/templatetags/assets.py" in render
  71.         for url in bundle.urls(env=get_env()):
File "/Users/julen/.virtualenvs/pootle/src/webassets/src/webassets/bundle.py" in urls
  781.         ctx = wrap(self.env, self)
File "/Users/julen/.virtualenvs/pootle/src/webassets/src/webassets/bundle.py" in _get_env
  368.             raise BundleError('Bundle is not connected to an environment')

Exception Type: BundleError at /
Exception Value: Bundle is not connected to an environment

Haven't seen anything related in the existing issues or in the docs. If I go back to django-assets 0.8 everything works fine.

mcfletch commented 10 years ago

Seeing the same issue here on a new Django project. I've tried both template-only and assets.py versions of the same bundle and always wind up with BundleErrors.

Haven't been able to track down why the environment isn't picked up. The urls method is called with an "env" parameter, but the url method seems to ignore that in favour of self.env. The "self" being called is what appears to be a null Bundle wrapped around the real bundle (that is, the real bundle is in self.contents[0]).

I've confirmed that the embedded bundle's env is being set in the package assets.py module, and during rendering of the tag the env on that instance is set (but not the wrapper bundle), but I haven't been able to track down where the un-registered wrapper Bundle is coming from.

mcfletch commented 10 years ago

Found the source of the un-bound bundle, it is the assets template tag, needs to bind the bundle before returning it:

    bundle = self.BundleClass(
        *[resolve_bundle(resolve_var(f)) for f in self.files],
        **{'output': resolve_var(self.output),
        'filters': resolve_var(self.filters),
        'debug': parse_debug_value(resolve_var(self.debug))})
    bundle.env = get_env()
    return bundle

but that produces the same error I was seeing when explicitly rebinding the environment, namely a type-error when calling the DjangoResolver.search_for_source, as it no longer matches the WebAssets interface/api (wrong number of arguments).

    return self.search_for_source(ctx, item) 

where the DjangoResolver is just looking for item.

mcfletch commented 10 years ago

Correction, with git head the search_for_source method has been fixed, so with the env-binding fix the asset chain works. Here's the diff:

diff --git a/django_assets/templatetags/assets.py b/django_assets/templatetags/assets.py index 249b51b..553d693 100644 --- a/django_assets/templatetags/assets.py +++ b/django_assets/templatetags/assets.py @@ -58,11 +58,13 @@ class AssetsNode(template.Node): except KeyError: return name