# Buggy code
def apply_post_transforms(self, doctree, docname):
    # type: (nodes.Node, unicode) -> None
    """Apply all post-transforms."""
    try:
        # set env.docname during applying post-transforms
        self.temp_data["docname"] = docname
        if hasattr(doctree, "settings"):
            doctree.settings.env = self

        transformer = SphinxTransformer(doctree)
        transformer.add_transforms(self.app.post_transforms)
        transformer.apply_transforms()
    finally:
        self.temp_data.clear()

    # allow custom references to be resolved
    self.app.emit("doctree-resolved", doctree, docname)


# Traceback (AttributeError)
Running Sphinx v1.7+
Generating grammar tables from /usr/home/cg/workspace/sphinx/sphinx/pycode/Grammar-py3.txt
loading pickled environment... failed: build environment version not current
loading intersphinx inventory from http://docs.python.org/objects.inv...
intersphinx inventory has moved: http://docs.python.org/objects.inv -> https://docs.python.org/2/objects.inv
building [mo]: targets for 0 po files that are out of date
building [html]: targets for 36 source files that are out of date
updating environment: 36 added, 0 changed, 0 removed
reading sources... [  2%] changelog
reading sources... [  5%] configure
reading sources... [  8%] faq
reading sources... [ 11%] feedback
reading sources... [ 13%] hacking
reading sources... [ 16%] index
reading sources... [ 19%] install
reading sources... [ 22%] license
reading sources... [ 25%] man
reading sources... [ 27%] news
reading sources... [ 30%] news/30c3
reading sources... [ 33%] news/31c3
reading sources... [ 36%] news/callfortesting
reading sources... [ 38%] news/khal01
reading sources... [ 41%] news/khal011
reading sources... [ 44%] news/khal02
reading sources... [ 47%] news/khal03
reading sources... [ 50%] news/khal031
reading sources... [ 52%] news/khal04
reading sources... [ 55%] news/khal05
reading sources... [ 58%] news/khal06
reading sources... [ 61%] news/khal07
reading sources... [ 63%] news/khal071
reading sources... [ 66%] news/khal08
reading sources... [ 69%] news/khal081
reading sources... [ 72%] news/khal082
reading sources... [ 75%] news/khal083
reading sources... [ 77%] news/khal084
reading sources... [ 80%] news/khal09
reading sources... [ 83%] news/khal091
reading sources... [ 86%] news/khal092
reading sources... [ 88%] news/khal093
reading sources... [ 91%] news/khal094
reading sources... [ 94%] news/khal095
reading sources... [ 97%] standards
reading sources... [100%] usage

looking for now-outdated files... none found
pickling environment... done
checking consistency... /usr/home/cg/workspace/khal/doc/source/man.rst: WARNING: document isn't included in any toctree
done
preparing documents... done
writing output... [  2%] changelog
writing output... [  5%] configure
writing output... [  8%] faq
writing output... [ 11%] feedback
writing output... [ 13%] hacking
writing output... [ 16%] index
writing output... [ 19%] install
writing output... [ 22%] license
writing output... [ 25%] man
writing output... [ 27%] news
/usr/home/cg/workspace/khal/doc/source/configure.rst:16: WARNING: unknown option: -c
path/to/config

Traceback (most recent call last):
File "/usr/home/cg/workspace/sphinx/sphinx/cmdline.py", line 306, in main
app.build(opts.force_all, filenames)
File "/usr/home/cg/workspace/sphinx/sphinx/application.py", line 329, in build
self.builder.build_update()
File "/usr/home/cg/workspace/sphinx/sphinx/builders/__init__.py", line 325, in build_update
'out of date' % len(to_build))
File "/usr/home/cg/workspace/sphinx/sphinx/builders/__init__.py", line 391, in build
self.write(docnames, list(updated_docnames), method)
File "/usr/home/cg/workspace/sphinx/sphinx/builders/__init__.py", line 428, in write
self._write_serial(sorted(docnames))
File "/usr/home/cg/workspace/sphinx/sphinx/builders/__init__.py", line 435, in _write_serial
doctree = self.env.get_and_resolve_doctree(docname, self)
File "/usr/home/cg/workspace/sphinx/sphinx/environment/__init__.py", line 880, in get_and_resolve_doctree
self.apply_post_transforms(doctree, docname)
File "/usr/home/cg/workspace/sphinx/sphinx/environment/__init__.py", line 932, in apply_post_transforms
self.app.emit('doctree-resolved', doctree, docname)
File "/usr/home/cg/workspace/sphinx/sphinx/application.py", line 435, in emit
return self.events.emit(event, self, *args)
File "/usr/home/cg/workspace/sphinx/sphinx/events.py", line 79, in emit
results.append(callback(*args))
File "/usr/home/cg/workspace/sphinxcontrib-newsfeed/sphinxcontrib/newsfeed.py", line 264, in process_feed
env.resolve_references(section_node, fromdocname, app.builder)
File "/usr/home/cg/workspace/sphinx/sphinx/environment/__init__.py", line 914, in resolve_references
self.apply_post_transforms(doctree, fromdocname)
File "/usr/home/cg/workspace/sphinx/sphinx/environment/__init__.py", line 927, in apply_post_transforms
transformer.apply_transforms()
File "/usr/home/cg/workspace/sphinx/sphinx/transforms/__init__.py", line 97, in apply_transforms
Transformer.apply_transforms(self)
File "/home/cg/.virtualenvs/khal/lib/python3.5/site-packages/docutils/transforms/__init__.py", line 171, in apply_transforms
transform.apply(**kwargs)
File "/usr/home/cg/workspace/sphinx/sphinx/transforms/post_transforms/__init__.py", line 78, in apply
refdoc = node.get('refdoc', self.env.docname)
File "/usr/home/cg/workspace/sphinx/sphinx/transforms/__init__.py", line 65, in env
return self.document.settings.env
AttributeError: 'Values' object has no attribute 'env'

Exception occurred:
File "/usr/home/cg/workspace/sphinx/sphinx/transforms/__init__.py", line 65, in env
return self.document.settings.env
AttributeError: 'Values' object has no attribute 'env'
The full traceback has been saved in /tmp/sphinx-err-sqvq9h7o.log, if you want to report the issue to the developers.
Please also report this if it was a user error, so that a better error message can be provided next time.
A bug report can be filed in the tracker at <https://github.com/sphinx-doc/sphinx/issues>. Thanks!

# Fixed code
def apply_post_transforms(self, doctree, docname):
    # type: (nodes.Node, unicode) -> None
    """Apply all post-transforms."""
    try:
        # set env.docname during applying post-transforms
        self.temp_data["docname"] = docname

        transformer = SphinxTransformer(doctree)
        transformer.set_environment(self)
        transformer.add_transforms(self.app.post_transforms)
        transformer.apply_transforms()
    finally:
        self.temp_data.clear()

    # allow custom references to be resolved
    self.app.emit("doctree-resolved", doctree, docname)