Repository: spyder-ide/spyder

Issue:
Editor extraneously reloads files when switching projects
<!--- **PLEASE READ:** When submitting here, please ensure you've completed the following checklist and checked the boxes to confirm. Issue reports without it may be closed. Thanks! --->

## Problem Description

When switching projects, the Editor seems to close and reopen the documents from the _first_ project, then switches to the new project and loads the documents for the new projects.


### What steps reproduce the problem?

1. Open a project with several files
2. Switch to another project
3. Observe the files from first project being closed and reopened, then the new project is loaded with its files.

### What is the expected output? What do you see instead?
When switching projects, the first project files should be closed and _not_ reopened.


## Versions
<!--- You can get this information from Help > About Spyder...
or (if Spyder won't launch) the "conda list" command
from the Anaconda Prompt/Terminal/command line. --->

* Spyder version: 4.3.0.dev0 1c4c84e72
* Python version: 3.9.1 64-bit
* Qt version: 5.9.7
* PyQt5 version: 5.9.2
* Operating System: Darwin 19.6.0

### Dependencies
<!--- Please go to the menu entry Help > Dependencies,
press the Copy to clipboard button and paste below --->
<details><summary>Dependencies</summary>

```
# Mandatory:
applaunchservices >=0.1.7       :  0.2.1 (OK)
atomicwrites >=1.2.0            :  1.4.0 (OK)
chardet >=2.0.0                 :  4.0.0 (OK)
cloudpickle >=0.5.0             :  1.6.0 (OK)
diff_match_patch >=20181111     :  20200713 (OK)
intervaltree >=3.0.2            :  3.1.0 (OK)
IPython >=7.6.0                 :  7.20.0 (OK)
jedi =0.17.2                    :  0.17.2 (OK)
jsonschema >=3.2.0              :  3.2.0 (OK)
keyring >=17.0.0                :  22.0.1 (OK)
nbconvert >=4.0                 :  6.0.7 (OK)
numpydoc >=0.6.0                :  1.1.0 (OK)
parso =0.7.0                    :  0.7.0 (OK)
pexpect >=4.4.0                 :  4.8.0 (OK)
pickleshare >=0.4               :  0.7.5 (OK)
psutil >=5.3                    :  5.8.0 (OK)
pygments >=2.0                  :  2.7.4 (OK)
pylint >=1.0                    :  2.6.0 (OK)
pyls >=0.36.2;<1.0.0            :  v4.2.1+130.g1c4c84e72 (OK)
pyls_black >=0.4.6              :  0.4.6 (OK)
pyls_spyder >=0.3.0             :  0.3.0 (OK)
qdarkstyle >=2.8                :  2.8.1 (OK)
qtawesome >=0.5.7               :  1.0.1 (OK)
qtconsole >=5.0.1               :  5.0.2 (OK)
qtpy >=1.5.0                    :  1.9.0 (OK)
rtree >=0.8.3                   :  0.9.4 (OK)
setuptools >=39.0.0             :  52.0.0.post20210125 (OK)
sphinx >=0.6.6                  :  3.4.3 (OK)
spyder_kernels >=1.10.1;<1.11.0 :  1.11.0.dev0 (OK)
textdistance >=4.2.0            :  4.2.0 (OK)
three_merge >=0.1.1             :  0.1.1 (OK)
watchdog >=0.10.3               :  1.0.2 (OK)
zmq >=17                        :  20.0.0 (OK)

# Optional:
cython >=0.21                   :  0.29.21 (OK)
matplotlib >=2.0.0              :  3.3.4 (OK)
numpy >=1.7                     :  1.19.2 (OK)
pandas >=1.1.1                  :  1.2.1 (OK)
scipy >=0.17.0                  :  1.6.0 (OK)
sympy >=0.7.3                   :  1.7.1 (OK)
```
</details>

![change projects](https://user-images.githubusercontent.com/9618975/108538475-ae99b500-7293-11eb-9a00-aeb01fd416ac.gif)



Discussion:
Thanks for reporting this @mrclary. I can successfully reproduce this bug in 4.3.0.dev0 (also MacOS). I also found another weird behaviour. When I open a project, it not only opens the files of the project but a bunch of files from Spyder's repo. Not sure if it is related.

<img width="1280" alt="Screenshot 2021-02-19 at 1 35 05 PM" src="https://user-images.githubusercontent.com/18587879/108546552-48bf2480-72b7-11eb-8a20-70a12f1c7e83.png">

@dalthviz  can you try reproducing this on Windows to see if it also happens there?
@juanis2112 I was able to reproduce this on Windows too:

![projects](https://user-images.githubusercontent.com/16781833/108548948-cf422900-72ec-11eb-8776-f19ea5866726.gif)

> When I open a project, it not only opens the files of the project but a bunch of files from Spyder's repo. Not sure if it is related.

I noticed this as well, but only when I bootstrap. It does not manifest with the macOS application. This may indicate an issue in `bootstrap.py`.


Resolving patch:
diff --git a/spyder/plugins/projects/plugin.py b/spyder/plugins/projects/plugin.py
--- a/spyder/plugins/projects/plugin.py
+++ b/spyder/plugins/projects/plugin.py
@@ -25,7 +25,6 @@
 from qtpy.QtWidgets import QInputDialog, QMessageBox
 
 # Local imports
-from spyder.api.config.decorators import on_conf_change
 from spyder.api.exceptions import SpyderAPIError
 from spyder.api.translations import get_translation
 from spyder.api.plugins import Plugins, SpyderDockablePlugin
@@ -101,18 +100,21 @@ class Projects(SpyderDockablePlugin):
 
     Parameters
     ----------
-    project_packages: object
-        Package to install. Currently not in use.
+    project_path: object
+        Loaded project path.
     """
 
-    sig_project_closed = Signal(object)
+    sig_project_closed = Signal((object,), (bool,))
     """
     This signal is emitted when a project is closed.
 
     Parameters
     ----------
-    project_packages: object
-        Package to install. Currently not in use.
+    project_path: object
+        Closed project path (signature 1).
+    close_project: bool
+        This is emitted only when closing a project but not when switching
+        between projects (signature 2).
     """
 
     sig_pythonpath_changed = Signal()
@@ -220,7 +222,7 @@ def register(self):
                                   update_kind=WorkspaceUpdateKind.DELETION,
                                   instance=self))
         if self.editor:
-            self.sig_project_closed.connect(
+            self.sig_project_closed[bool].connect(
                 lambda v: self.editor.setup_open_files())
         if outline_explorer:
             self.sig_project_closed.connect(
@@ -367,7 +369,6 @@ def opener(*args, **kwargs):
     def create_new_project(self):
         """Create new project."""
         self.unmaximize()
-        active_project = self.current_active_project
         dlg = ProjectDialog(self.get_widget(),
                             project_types=self.get_project_types())
         result = dlg.exec_()
@@ -376,19 +377,7 @@ def create_new_project(self):
         project_type = data.get("project_type", EmptyProject.ID)
 
         if result:
-            # A project was not open before
-            if active_project is None:
-                if self.get_conf('visible_if_project_open'):
-                    self.show_explorer()
-            else:
-                # We are switching projects.
-                # TODO: Don't emit sig_project_closed when we support
-                # multiple workspaces.
-                self.sig_project_closed.emit(active_project.root_path)
-
             self._create_project(root_path, project_type_id=project_type)
-            self.sig_pythonpath_changed.emit()
-            self.restart_consoles()
             dlg.close()
 
     def _create_project(self, root_path, project_type_id=EmptyProject.ID,
@@ -513,6 +502,7 @@ def close_project(self):
             self.setup_menu_actions()
 
             self.sig_project_closed.emit(path)
+            self.sig_project_closed[bool].emit(True)
             self.sig_pythonpath_changed.emit()
 
             # Hide pane.


Test patch:
diff --git a/spyder/plugins/projects/tests/test_plugin.py b/spyder/plugins/projects/tests/test_plugin.py
--- a/spyder/plugins/projects/tests/test_plugin.py
+++ b/spyder/plugins/projects/tests/test_plugin.py
@@ -466,11 +466,6 @@ def test_loaded_and_closed_signals(create_projects, tmpdir, mocker, qtbot):
             [projects.sig_project_loaded, projects.sig_project_closed]):
         projects.open_project(path=path2)
 
-    # Create new project with an active one. This must emit
-    # sig_project_closed!
-    with qtbot.waitSignal(projects.sig_project_closed):
-        projects.create_new_project()
-
 
 if __name__ == "__main__":
     pytest.main()