# Buggy code
def apply_transforms(self):
    # type: () -> None
    if isinstance(self.document, nodes.document):
        if hasattr(self.document.settings, "env") and self.env:
            self.document.settings.env = self.env

        Transformer.apply_transforms(self)
    else:
        # wrap the target node by document node during transforming
        try:
            document = new_document("")
            if self.env:
                document.settings.env = self.env
            document += self.document
            self.document = document
            Transformer.apply_transforms(self)
        finally:
            self.document = self.document[0]


# Traceback (AttributeError)
# Sphinx version: 1.6.2+
# Python version: 3.6.1 (CPython)
# Docutils version: 0.13.1 release
# Jinja2 version: 2.9.6
# Last messages:
#   changes
#
#   examples
#
#   authors
#
#
#
#   resolving references...
#
# Loaded extensions:
#   alabaster (0.7.10) from C:\Project\sphinx-dev\sphinx\.venv\lib\site-packages\alabaster\__init__.py
#   sphinx.ext.autodoc (1.6.2+) from C:\Project\sphinx-dev\sphinx\sphinx\ext\autodoc.py
#   sphinx.ext.doctest (1.6.2+) from C:\Project\sphinx-dev\sphinx\sphinx\ext\doctest.py
#   sphinx.ext.todo (1.6.2+) from C:\Project\sphinx-dev\sphinx\sphinx\ext\todo.py
#   sphinx.ext.autosummary (1.6.2+) from C:\Project\sphinx-dev\sphinx\sphinx\ext\autosummary\__init__.py
#   sphinx.ext.extlinks (1.6.2+) from C:\Project\sphinx-dev\sphinx\sphinx\ext\extlinks.py
#   sphinx.ext.viewcode (1.6.2+) from C:\Project\sphinx-dev\sphinx\sphinx\ext\viewcode.py
Traceback (most recent call last):
File "C:\Project\sphinx-dev\sphinx\sphinx\cmdline.py", line 306, in main
app.build(opts.force_all, filenames)
File "C:\Project\sphinx-dev\sphinx\sphinx\application.py", line 338, in build
self.builder.build_update()
File "C:\Project\sphinx-dev\sphinx\sphinx\builders\__init__.py", line 323, in build_update
self.build(['__all__'], to_build)
File "C:\Project\sphinx-dev\sphinx\sphinx\builders\__init__.py", line 394, in build
self.write(docnames, list(updated_docnames), method)
File "C:\Project\sphinx-dev\sphinx\sphinx\builders\latex.py", line 140, in write
appendices=((docclass != 'howto') and self.config.latex_appendices or []))
File "C:\Project\sphinx-dev\sphinx\sphinx\builders\latex.py", line 190, in assemble_doctree
self.env.resolve_references(largetree, indexfile, self)
File "C:\Project\sphinx-dev\sphinx\sphinx\environment\__init__.py", line 943, in resolve_references
self.apply_post_transforms(doctree, fromdocname)
File "C:\Project\sphinx-dev\sphinx\sphinx\environment\__init__.py", line 955, in apply_post_transforms
transformer.apply_transforms()
File "C:\Project\sphinx-dev\sphinx\sphinx\transforms\__init__.py", line 91, in apply_transforms
Transformer.apply_transforms(self)
File "C:\Project\sphinx-dev\sphinx\.venv\lib\site-packages\docutils\transforms\__init__.py", line 171, in apply_transforms
transform.apply(**kwargs)
File "C:\Project\sphinx-dev\sphinx\sphinx\transforms\post_transforms\__init__.py", line 78, in apply
refdoc = node.get('refdoc', self.env.docname)
File "C:\Project\sphinx-dev\sphinx\sphinx\transforms\__init__.py", line 65, in env
return self.document.settings.env
AttributeError: 'Values' object has no attribute 'env'

# Fixed code
def apply_transforms(self):
    # type: () -> None
    if isinstance(self.document, nodes.document):
        if not hasattr(self.document.settings, "env") and self.env:
            self.document.settings.env = self.env

        Transformer.apply_transforms(self)
    else:
        # wrap the target node by document node during transforming
        try:
            document = new_document("")
            if self.env:
                document.settings.env = self.env
            document += self.document
            self.document = document
            Transformer.apply_transforms(self)
        finally:
            self.document = self.document[0]