Repository: spyder-ide/spyder

Issue:
Failure checking out a branch and the opened file is in a different path
 This happened:
1. I had a file opened in Spyder
2. I git-checked out another branch with that file placed in a different path
3. Spyder told me to close the tab since the file couldn't be found
4. Spyder raised an internal problem

TRACE:
  self.refresh_plugin()   # To give focus to the plugin's widget
  File "/Users/manuel/anaconda/envs/sentisis3/lib/python3.6/site-packages/spyder/plugins/explorer.py", line 94, in refresh_plugin
    self.treewidget.update_history(new_path)
  File "/Users/manuel/anaconda/envs/sentisis3/lib/python3.6/site-packages/spyder/widgets/explorer.py", line 1124, in update_history
    directory = osp.abspath(to_text_string(directory))
  File "/Users/manuel/anaconda/envs/sentisis3/lib/python3.6/posixpath.py", line 374, in abspath
    cwd = os.getcwd()
FileNotFoundError: [Errno 2] No such file or directory



Discussion:
Thanks for reporting. We'll fix this in a future release.
@ccordoba12 Is this because what was previously the working directory doesn't exist on the other branch, and we just need to check for that?
Yes, I think so.
I can get the same error message without version control using the following steps:

1. Make a new directory
2. Change the Spyder working directory to this directory
3. Delete the directory outside Spyder
4. Click on the File Explorer tab to bring it on top (if it is already on top, then first click on another tab and then click on the File Explorer tab).
Ok, thanks a lot @jitseniesen!

@dalthviz, please work on this one.
Just to give here more info about the issue, I can't replicate it in Windows since when you try to delete the folder a dialog says it's open by another program.

![imagen](https://user-images.githubusercontent.com/16781833/43796662-803bd5e0-9a4a-11e8-95ad-8e3a1f9ac3f3.png)

However using Ubuntu (and probably in other linux distros), deleting permanently the directory (holding `shift + supr` or using `rm -rf`) the error shows up.

![imagen](https://user-images.githubusercontent.com/16781833/43797198-3ad2f216-9a4c-11e8-843c-ec35c65ad9d7.png)

@dalthviz You can't just do ``rm -rf`` in msys2 (Git-bash) or Cygwin?
Good catch @CAM-Gerlach that was something I didn't test. Although testing it, at least with Git-bash,it doesn't let you delete the directory:

![imagen](https://user-images.githubusercontent.com/16781833/43799965-942f944c-9a54-11e8-9789-42ba2fee8375.png)



Okay, thanks. Must ultimately be limited by the Win32 APIs MSYS2 is calling.

Resolving patch:
diff --git a/spyder/widgets/explorer.py b/spyder/widgets/explorer.py
--- a/spyder/widgets/explorer.py
+++ b/spyder/widgets/explorer.py
@@ -32,7 +32,7 @@
                             QMessageBox, QToolButton, QTreeView, QVBoxLayout,
                             QWidget)
 # Local imports
-from spyder.config.base import _
+from spyder.config.base import _, get_home_dir
 from spyder.py3compat import (str_lower, to_binary_string,
                               to_text_string)
 from spyder.utils import icon_manager as ima
@@ -1122,9 +1122,13 @@ def go_to_next_directory(self):
         
     def update_history(self, directory):
         """Update browse history"""
-        directory = osp.abspath(to_text_string(directory))
-        if directory in self.history:
-            self.histindex = self.history.index(directory)
+        try:
+            directory = osp.abspath(to_text_string(directory))
+            if directory in self.history:
+                self.histindex = self.history.index(directory)
+        except Exception:
+            user_directory = get_home_dir()
+            self.chdir(directory=user_directory, browsing_history=True)
         
     def chdir(self, directory=None, browsing_history=False):
         """Set directory as working directory"""


Test patch: