We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Doc does not build
Am I the only one?

```
Exception occurred:
  File "/home/aa013911/work/nilearn/doc/sphinxext/sphinxgallery/docs_resolv.py", line 348, in embed_code_links
    gallery_dir = os.path.join(app.builder.srcdir, gallery_conf['gallery_dir'])
KeyError: 'gallery_dir'
The full traceback has been saved in /tmp/sphinx-err-sePU7r.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.
```


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `doc/sphinxext/sphinxgallery/__init__.py`
Content:
```
1 """Sphinx Gallery
2 """
3 import os
4 __version__ = '0.0.8'
5 
6 def path_static():
7     """Returns path to packaged static files"""
8     return os.path.abspath(os.path.dirname(__file__))+'/_static'
9
```

Path: `doc/sphinxext/sphinxgallery/gen_gallery.py`
Content:
```
1 # -*- coding: utf-8 -*-
2 # Author: Óscar Nájera
3 # License: 3-clause BSD
4 
5 import os
6 from sphinxgallery.gen_rst import generate_dir_rst
7 from sphinxgallery.docs_resolv import embed_code_links
8 
9 
10 def generate_gallery_rst(app):
11     """Starts the gallery configuration and recursively scans the examples
12     directory in order to populate the examples gallery
13     """
14     try:
15         plot_gallery = eval(app.builder.config.plot_gallery)
16     except TypeError:
17         plot_gallery = bool(app.builder.config.plot_gallery)
18 
19     if not plot_gallery:
20         return
21 
22     gallery_conf.update(app.config.sphinxgallery_conf)
23 
24     # this assures I can call the config in other places
25     app.config.sphinxgallery_conf = gallery_conf
26 
27     examples_dir = os.path.join(app.builder.srcdir, gallery_conf['examples_dir'])
28     gallery_dir = os.path.join(app.builder.srcdir, gallery_conf['gallery_dir'])
29     mod_examples_dir = os.path.join(app.builder.srcdir, gallery_conf['mod_example_dir'])
30 
31     for workdir in [examples_dir, gallery_dir, mod_examples_dir]:
32         if not os.path.exists(workdir):
33             os.makedirs(workdir)
34 
35     # we create an index.rst with all examples
36     fhindex = open(os.path.join(gallery_dir, 'index.rst'), 'w')
37     fhindex.write("""
38 
39 .. _examples-index:
40 
41 Gallery of Examples
42 ===================
43 
44 """)
45     # Here we don't use an os.walk, but we recurse only twice: flat is
46     # better than nested.
47     seen_backrefs = set()
48     generate_dir_rst('.', fhindex, examples_dir, gallery_dir, gallery_conf, plot_gallery, seen_backrefs)
49     for directory in sorted(os.listdir(examples_dir)):
50         if os.path.isdir(os.path.join(examples_dir, directory)):
51             generate_dir_rst(directory, fhindex, examples_dir, gallery_dir, gallery_conf, plot_gallery, seen_backrefs)
52     fhindex.flush()
53 
54 
55 gallery_conf = {
56     'examples_dir'   : '../examples',
57     'gallery_dir'    : 'auto_examples',
58     'mod_example_dir': 'modules/generated',
59     'doc_module'     : (),
60     'reference_url'  : {},
61 }
62 
63 def setup(app):
64     app.add_config_value('plot_gallery', True, 'html')
65     app.add_config_value('sphinxgallery_conf', gallery_conf, 'html')
66     app.add_stylesheet('gallery.css')
67 
68     app.connect('builder-inited', generate_gallery_rst)
69 
70     app.connect('build-finished', embed_code_links)
71 
72     # Sphinx hack: sphinx copies generated images to the build directory
73     #  each time the docs are made.  If the desired image name already
74     #  exists, it appends a digit to prevent overwrites.  The problem is,
75     #  the directory is never cleared.  This means that each time you build
76     #  the docs, the number of images in the directory grows.
77     #
78     # This question has been asked on the sphinx development list, but there
79     #  was no response: http://osdir.com/ml/sphinx-dev/2011-02/msg00123.html
80     #
81     # The following is a hack that prevents this behavior by clearing the
82     #  image build directory each time the docs are built.  If sphinx
83     #  changes their layout between versions, this will not work (though
84     #  it should probably not cause a crash).  Tested successfully
85     #  on Sphinx 1.0.7
86     build_image_dir = '_build/html/_images'
87     if os.path.exists(build_image_dir):
88         filelist = os.listdir(build_image_dir)
89         for filename in filelist:
90             if filename.endswith('png'):
91                 os.remove(os.path.join(build_image_dir, filename))
92 
93 def setup_module():
94     # HACK: Stop nosetests running setup() above
95     pass
96
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/doc/sphinxext/sphinxgallery/__init__.py b/doc/sphinxext/sphinxgallery/__init__.py
--- a/doc/sphinxext/sphinxgallery/__init__.py
+++ b/doc/sphinxext/sphinxgallery/__init__.py
@@ -1,7 +1,7 @@
 """Sphinx Gallery
 """
 import os
-__version__ = '0.0.8'
+__version__ = '0.0.9-dev'
 
 def path_static():
     """Returns path to packaged static files"""
diff --git a/doc/sphinxext/sphinxgallery/gen_gallery.py b/doc/sphinxext/sphinxgallery/gen_gallery.py
--- a/doc/sphinxext/sphinxgallery/gen_gallery.py
+++ b/doc/sphinxext/sphinxgallery/gen_gallery.py
@@ -16,14 +16,14 @@
     except TypeError:
         plot_gallery = bool(app.builder.config.plot_gallery)
 
-    if not plot_gallery:
-        return
-
     gallery_conf.update(app.config.sphinxgallery_conf)
 
     # this assures I can call the config in other places
     app.config.sphinxgallery_conf = gallery_conf
 
+    if not plot_gallery:
+        return
+
     examples_dir = os.path.join(app.builder.srcdir, gallery_conf['examples_dir'])
     gallery_dir = os.path.join(app.builder.srcdir, gallery_conf['gallery_dir'])
     mod_examples_dir = os.path.join(app.builder.srcdir, gallery_conf['mod_example_dir'])

{"golden_diff": "diff --git a/doc/sphinxext/sphinxgallery/__init__.py b/doc/sphinxext/sphinxgallery/__init__.py\n--- a/doc/sphinxext/sphinxgallery/__init__.py\n+++ b/doc/sphinxext/sphinxgallery/__init__.py\n@@ -1,7 +1,7 @@\n \"\"\"Sphinx Gallery\n \"\"\"\n import os\n-__version__ = '0.0.8'\n+__version__ = '0.0.9-dev'\n \n def path_static():\n     \"\"\"Returns path to packaged static files\"\"\"\ndiff --git a/doc/sphinxext/sphinxgallery/gen_gallery.py b/doc/sphinxext/sphinxgallery/gen_gallery.py\n--- a/doc/sphinxext/sphinxgallery/gen_gallery.py\n+++ b/doc/sphinxext/sphinxgallery/gen_gallery.py\n@@ -16,14 +16,14 @@\n     except TypeError:\n         plot_gallery = bool(app.builder.config.plot_gallery)\n \n-    if not plot_gallery:\n-        return\n-\n     gallery_conf.update(app.config.sphinxgallery_conf)\n \n     # this assures I can call the config in other places\n     app.config.sphinxgallery_conf = gallery_conf\n \n+    if not plot_gallery:\n+        return\n+\n     examples_dir = os.path.join(app.builder.srcdir, gallery_conf['examples_dir'])\n     gallery_dir = os.path.join(app.builder.srcdir, gallery_conf['gallery_dir'])\n     mod_examples_dir = os.path.join(app.builder.srcdir, gallery_conf['mod_example_dir'])\n", "issue": "Doc does not build\nAm I the only one?\n\n```\nException occurred:\n  File \"/home/aa013911/work/nilearn/doc/sphinxext/sphinxgallery/docs_resolv.py\", line 348, in embed_code_links\n    gallery_dir = os.path.join(app.builder.srcdir, gallery_conf['gallery_dir'])\nKeyError: 'gallery_dir'\nThe full traceback has been saved in /tmp/sphinx-err-sePU7r.log, if you want to report the issue to the developers.\nPlease also report this if it was a user error, so that a better error message can be provided next time.\n```\n\n", "before_files": [{"content": "\"\"\"Sphinx Gallery\n\"\"\"\nimport os\n__version__ = '0.0.8'\n\ndef path_static():\n    \"\"\"Returns path to packaged static files\"\"\"\n    return os.path.abspath(os.path.dirname(__file__))+'/_static'\n", "path": "doc/sphinxext/sphinxgallery/__init__.py"}, {"content": "# -*- coding: utf-8 -*-\n# Author: \u00d3scar N\u00e1jera\n# License: 3-clause BSD\n\nimport os\nfrom sphinxgallery.gen_rst import generate_dir_rst\nfrom sphinxgallery.docs_resolv import embed_code_links\n\n\ndef generate_gallery_rst(app):\n    \"\"\"Starts the gallery configuration and recursively scans the examples\n    directory in order to populate the examples gallery\n    \"\"\"\n    try:\n        plot_gallery = eval(app.builder.config.plot_gallery)\n    except TypeError:\n        plot_gallery = bool(app.builder.config.plot_gallery)\n\n    if not plot_gallery:\n        return\n\n    gallery_conf.update(app.config.sphinxgallery_conf)\n\n    # this assures I can call the config in other places\n    app.config.sphinxgallery_conf = gallery_conf\n\n    examples_dir = os.path.join(app.builder.srcdir, gallery_conf['examples_dir'])\n    gallery_dir = os.path.join(app.builder.srcdir, gallery_conf['gallery_dir'])\n    mod_examples_dir = os.path.join(app.builder.srcdir, gallery_conf['mod_example_dir'])\n\n    for workdir in [examples_dir, gallery_dir, mod_examples_dir]:\n        if not os.path.exists(workdir):\n            os.makedirs(workdir)\n\n    # we create an index.rst with all examples\n    fhindex = open(os.path.join(gallery_dir, 'index.rst'), 'w')\n    fhindex.write(\"\"\"\n\n.. _examples-index:\n\nGallery of Examples\n===================\n\n\"\"\")\n    # Here we don't use an os.walk, but we recurse only twice: flat is\n    # better than nested.\n    seen_backrefs = set()\n    generate_dir_rst('.', fhindex, examples_dir, gallery_dir, gallery_conf, plot_gallery, seen_backrefs)\n    for directory in sorted(os.listdir(examples_dir)):\n        if os.path.isdir(os.path.join(examples_dir, directory)):\n            generate_dir_rst(directory, fhindex, examples_dir, gallery_dir, gallery_conf, plot_gallery, seen_backrefs)\n    fhindex.flush()\n\n\ngallery_conf = {\n    'examples_dir'   : '../examples',\n    'gallery_dir'    : 'auto_examples',\n    'mod_example_dir': 'modules/generated',\n    'doc_module'     : (),\n    'reference_url'  : {},\n}\n\ndef setup(app):\n    app.add_config_value('plot_gallery', True, 'html')\n    app.add_config_value('sphinxgallery_conf', gallery_conf, 'html')\n    app.add_stylesheet('gallery.css')\n\n    app.connect('builder-inited', generate_gallery_rst)\n\n    app.connect('build-finished', embed_code_links)\n\n    # Sphinx hack: sphinx copies generated images to the build directory\n    #  each time the docs are made.  If the desired image name already\n    #  exists, it appends a digit to prevent overwrites.  The problem is,\n    #  the directory is never cleared.  This means that each time you build\n    #  the docs, the number of images in the directory grows.\n    #\n    # This question has been asked on the sphinx development list, but there\n    #  was no response: http://osdir.com/ml/sphinx-dev/2011-02/msg00123.html\n    #\n    # The following is a hack that prevents this behavior by clearing the\n    #  image build directory each time the docs are built.  If sphinx\n    #  changes their layout between versions, this will not work (though\n    #  it should probably not cause a crash).  Tested successfully\n    #  on Sphinx 1.0.7\n    build_image_dir = '_build/html/_images'\n    if os.path.exists(build_image_dir):\n        filelist = os.listdir(build_image_dir)\n        for filename in filelist:\n            if filename.endswith('png'):\n                os.remove(os.path.join(build_image_dir, filename))\n\ndef setup_module():\n    # HACK: Stop nosetests running setup() above\n    pass\n", "path": "doc/sphinxext/sphinxgallery/gen_gallery.py"}], "after_files": [{"content": "\"\"\"Sphinx Gallery\n\"\"\"\nimport os\n__version__ = '0.0.9-dev'\n\ndef path_static():\n    \"\"\"Returns path to packaged static files\"\"\"\n    return os.path.abspath(os.path.dirname(__file__))+'/_static'\n", "path": "doc/sphinxext/sphinxgallery/__init__.py"}, {"content": "# -*- coding: utf-8 -*-\n# Author: \u00d3scar N\u00e1jera\n# License: 3-clause BSD\n\nimport os\nfrom sphinxgallery.gen_rst import generate_dir_rst\nfrom sphinxgallery.docs_resolv import embed_code_links\n\n\ndef generate_gallery_rst(app):\n    \"\"\"Starts the gallery configuration and recursively scans the examples\n    directory in order to populate the examples gallery\n    \"\"\"\n    try:\n        plot_gallery = eval(app.builder.config.plot_gallery)\n    except TypeError:\n        plot_gallery = bool(app.builder.config.plot_gallery)\n\n    gallery_conf.update(app.config.sphinxgallery_conf)\n\n    # this assures I can call the config in other places\n    app.config.sphinxgallery_conf = gallery_conf\n\n    if not plot_gallery:\n        return\n\n    examples_dir = os.path.join(app.builder.srcdir, gallery_conf['examples_dir'])\n    gallery_dir = os.path.join(app.builder.srcdir, gallery_conf['gallery_dir'])\n    mod_examples_dir = os.path.join(app.builder.srcdir, gallery_conf['mod_example_dir'])\n\n    for workdir in [examples_dir, gallery_dir, mod_examples_dir]:\n        if not os.path.exists(workdir):\n            os.makedirs(workdir)\n\n    # we create an index.rst with all examples\n    fhindex = open(os.path.join(gallery_dir, 'index.rst'), 'w')\n    fhindex.write(\"\"\"\n\n.. _examples-index:\n\nGallery of Examples\n===================\n\n\"\"\")\n    # Here we don't use an os.walk, but we recurse only twice: flat is\n    # better than nested.\n    seen_backrefs = set()\n    generate_dir_rst('.', fhindex, examples_dir, gallery_dir, gallery_conf, plot_gallery, seen_backrefs)\n    for directory in sorted(os.listdir(examples_dir)):\n        if os.path.isdir(os.path.join(examples_dir, directory)):\n            generate_dir_rst(directory, fhindex, examples_dir, gallery_dir, gallery_conf, plot_gallery, seen_backrefs)\n    fhindex.flush()\n\n\ngallery_conf = {\n    'examples_dir'   : '../examples',\n    'gallery_dir'    : 'auto_examples',\n    'mod_example_dir': 'modules/generated',\n    'doc_module'     : (),\n    'reference_url'  : {},\n}\n\ndef setup(app):\n    app.add_config_value('plot_gallery', True, 'html')\n    app.add_config_value('sphinxgallery_conf', gallery_conf, 'html')\n    app.add_stylesheet('gallery.css')\n\n    app.connect('builder-inited', generate_gallery_rst)\n\n    app.connect('build-finished', embed_code_links)\n\n    # Sphinx hack: sphinx copies generated images to the build directory\n    #  each time the docs are made.  If the desired image name already\n    #  exists, it appends a digit to prevent overwrites.  The problem is,\n    #  the directory is never cleared.  This means that each time you build\n    #  the docs, the number of images in the directory grows.\n    #\n    # This question has been asked on the sphinx development list, but there\n    #  was no response: http://osdir.com/ml/sphinx-dev/2011-02/msg00123.html\n    #\n    # The following is a hack that prevents this behavior by clearing the\n    #  image build directory each time the docs are built.  If sphinx\n    #  changes their layout between versions, this will not work (though\n    #  it should probably not cause a crash).  Tested successfully\n    #  on Sphinx 1.0.7\n    build_image_dir = '_build/html/_images'\n    if os.path.exists(build_image_dir):\n        filelist = os.listdir(build_image_dir)\n        for filename in filelist:\n            if filename.endswith('png'):\n                os.remove(os.path.join(build_image_dir, filename))\n\ndef setup_module():\n    # HACK: Stop nosetests running setup() above\n    pass\n", "path": "doc/sphinxext/sphinxgallery/gen_gallery.py"}]}