Repository: sphinx-doc/sphinx

Issue:
Failed to load last environment object if extension added
**Describe the bug**
Failed to load last environment object if extension added.

**To Reproduce**
Steps to reproduce the behavior:

1. Create a new project
2. Build HTML once
3. Add a some extension to conf.py
4. Build HTML again

```
Running Sphinx v2.2.0+
loading pickled environment... failed
failed: No such config value: autosummary_generate
building [mo]: targets for 0 po files that are out of date
building [html]: targets for 4 source files that are out of date
...
```

**Expected behavior**
Loading succeeded.

**Your project**
None

**Screenshots**
None

**Environment info**
- OS: Mac
- Python version: 3.7.3
- Sphinx version: 2.2.0 (dev)
- Sphinx extensions: sphinx.ext.autosummary


Discussion:
codecov[bot]: # [Codecov](https://codecov.io/gh/sphinx-doc/sphinx/pull/6532?src=pr&el=h1) Report
> Merging [#6532](https://codecov.io/gh/sphinx-doc/sphinx/pull/6532?src=pr&el=desc) into [2.0](https://codecov.io/gh/sphinx-doc/sphinx/commit/5731ca634e7d03d7cb5df70fe1b923a94af0c06d?src=pr&el=desc) will **increase** coverage by `<.01%`.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/sphinx-doc/sphinx/pull/6532/graphs/tree.svg?width=650&token=VFlFSELbkw&height=150&src=pr)](https://codecov.io/gh/sphinx-doc/sphinx/pull/6532?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##              2.0    #6532      +/-   ##
==========================================
+ Coverage   83.68%   83.69%   +<.01%     
==========================================
  Files         266      266              
  Lines       40538    40550      +12     
  Branches     5979     5979              
==========================================
+ Hits        33925    33937      +12     
  Misses       5291     5291              
  Partials     1322     1322
```


| [Impacted Files](https://codecov.io/gh/sphinx-doc/sphinx/pull/6532?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [tests/test\_environment.py](https://codecov.io/gh/sphinx-doc/sphinx/pull/6532/diff?src=pr&el=tree#diff-dGVzdHMvdGVzdF9lbnZpcm9ubWVudC5weQ==) | `100% <100%> (ø)` | :arrow_up: |
| [sphinx/environment/\_\_init\_\_.py](https://codecov.io/gh/sphinx-doc/sphinx/pull/6532/diff?src=pr&el=tree#diff-c3BoaW54L2Vudmlyb25tZW50L19faW5pdF9fLnB5) | `76.43% <100%> (ø)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/sphinx-doc/sphinx/pull/6532?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/sphinx-doc/sphinx/pull/6532?src=pr&el=footer). Last update [5731ca6...a758518](https://codecov.io/gh/sphinx-doc/sphinx/pull/6532?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/CHANGES b/CHANGES
index 7fe119d63..b264164db 100644
--- a/CHANGES
+++ b/CHANGES
@@ -48,6 +48,7 @@ Bugs fixed
 * #6507: autosummary: crashes without no autosummary_generate setting
 * #6511: LaTeX: autonumbered list can not be customized in LaTeX
   since Sphinx 1.8.0 (refs: #6533)
+* #6531: Failed to load last environment object when extension added
 
 Testing
 --------
diff --git a/sphinx/environment/__init__.py b/sphinx/environment/__init__.py
index af3b6cf1f..0fa966658 100644
--- a/sphinx/environment/__init__.py
+++ b/sphinx/environment/__init__.py
@@ -237,6 +237,8 @@ def _update_config(self, config):
         self.config_status = CONFIG_OK
         if self.config is None:
             self.config_status = CONFIG_NEW
+        elif self.config.extensions != config.extensions:
+            self.config_status = CONFIG_EXTENSIONS_CHANGED
         else:
             # check if a config value was changed that affects how
             # doctrees are read
@@ -245,11 +247,6 @@ def _update_config(self, config):
                     self.config_status = CONFIG_CHANGED
                     break
 
-            # this value is not covered by the above loop because it is handled
-            # specially by the config class
-            if self.config.extensions != config.extensions:
-                self.config_status = CONFIG_EXTENSIONS_CHANGED
-
         self.config = config
 
     def _update_settings(self, config):


Test patch:
diff --git a/tests/test_environment.py b/tests/test_environment.py
index 15562536f..1c6b49e64 100644
--- a/tests/test_environment.py
+++ b/tests/test_environment.py
@@ -11,9 +11,32 @@
 
 from sphinx.builders.html import StandaloneHTMLBuilder
 from sphinx.builders.latex import LaTeXBuilder
+from sphinx.environment import CONFIG_OK, CONFIG_CHANGED, CONFIG_EXTENSIONS_CHANGED, CONFIG_NEW
 from sphinx.testing.comparer import PathComparer
 
 
+@pytest.mark.sphinx('dummy', testroot='basic')
+def test_config_status(make_app, app_params):
+    args, kwargs = app_params
+
+    # clean build
+    app1 = make_app(*args, freshenv=True, **kwargs)
+    assert app1.env.config_status == CONFIG_NEW
+    app1.build()
+
+    # incremental build (no config changed)
+    app2 = make_app(*args, **kwargs)
+    assert app2.env.config_status == CONFIG_OK
+
+    # incremental build (config entry changed)
+    app3 = make_app(*args, confoverrides={'master_doc': 'content'}, **kwargs)
+    assert app3.env.config_status == CONFIG_CHANGED
+
+    # incremental build (extension changed)
+    app4 = make_app(*args, confoverrides={'extensions': ['sphinx.ext.autodoc']}, **kwargs)
+    assert app4.env.config_status == CONFIG_EXTENSIONS_CHANGED
+
+
 @pytest.mark.sphinx('dummy')
 def test_images(app):
     app.build()