Repository: spyder-ide/spyder

Issue:
Atomic writes generate temp files in Dropbox
<!--- **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! --->

### Issue Report Checklist

* [ x] Searched the [issues page](https://github.com/spyder-ide/spyder/issues?q=is%3Aissue) for similar reports
* [ x] Read the relevant sections of the [Spyder Troubleshooting Guide](https://github.com/spyder-ide/spyder/wiki/Troubleshooting-Guide-and-FAQ) and followed its advice
* [ x] Reproduced the issue after updating with ``conda update spyder`` (or ``pip``, if not using Anaconda)
* [ ] Could not reproduce inside ``jupyter qtconsole`` (if console-related)
* [ x] Tried basic troubleshooting (if a bug/error)
    * [x ] Restarted Spyder
    * [ x] Reset preferences with ``spyder --reset``
    * [ x] Reinstalled the latest version of [Anaconda](https://www.anaconda.com/download/)
    * [ x] Tried the other applicable steps from the Troubleshooting Guide
* [ x] Completed the **Problem Description**, **Steps to Reproduce** and **Version** sections below


## Problem Description

Spyder4 changes the auto-save recovery file location to the last directory a script was opened from or saved to.  Un-selecting the auto-save feature and restarting Spyder does not disable the auto-save file creation.


### What steps reproduce the problem?

1.  Reset Spyder Settings
2.  Disable autosave, restart Spyder
3.  Open a previously saved script -- example.py, from directory "usr/pyscript/"
4.  Run the script, and find a list of "File" saved under the diretory "usr/pyscript/", where "File" are the different auto-save versions of example.py
5.  When ceating a new script, the default "save as" directory is also "usr/pyscript/"

### What is the expected output? What do you see instead?

The autosave feature can be disabled correctly, or the autosave file to be saved in a specified directory.

## 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.1.3
* Python version: 3.7
* Qt version: 
* PyQt version: 
* Operating System name/version: 




Discussion:
A new auto-save file pops up immediately after running any unsaved script with runcell (ctrl + enter).  

By unchecking "save all files before running script" in the preferences, no new auto-save files are created at the time of running the cell.  HOWEVER, new auto-save files are created in "usr/pyscript/" immediately when I manually save the changes to "example.py".
Hi @rqli9,

Can you please send us a gif or screenshots of the behavior that you are having right now?
Hi @steff456 

I attached the screenshot of the auto-save files, which are variations of the versions of the highlighted ".py" file that I was actively working on and saved in the same folder.  The "auto-save" feature was unselected before all these files were created. It seems to me that these files are the auto-saves for recovery purposes created by spyder. 

![pyspyder](https://user-images.githubusercontent.com/67016991/84927543-9e8d5e00-b092-11ea-861e-058d129f4a2d.JPG)

Spyder autosaves files in the configuration directory, so those files were not created by the program. Can you please delete all the temp files in the directory and verify if they are created once you open the `NLTK_exercise.py` in Spyder?

As well I can see in your screenshot that you are using Dropbox, so maybe that is the cause of all those temp files.
Hello @steff456,

(1) I have manually checked all of these files.  They contain incremental versions of NLTK_exercise.py

(2) After deleting all of the existing files, new files are added to the directory once I begin editing and running code in NLTK_exercise.py.

(3)  I have been using Spyder3 in the same directory for an extended amount of time without issue.  This problem disappears after reverting back to a Spyder3 installation and opening the NLTK_exercise.py file in the same directory.  After going back to Spyder4, the problem appears again.

It seems there's a conflict with the combination of the auto-save function with dropbox. For now, please work with your files outside dropbox while we work on fixing this. 

Thanks for reporting!

@dalthviz can you please take a look on this one?
@steff456 

Thank you for working on this issue.  I hope it will get fixed soon.


@dalthviz, I think this is related to the way we perform atomic writes and how Dropbox saves (somehow) those temporary files we use in the background.
I do confirm I have this annoying issue as well, are there any ways of preventing these files from being saved (I cannot avoid working in dropbox)?
Hey @DominiqueMakowski, there are no ways to avoid this problem right now, sorry. 
I'm still affected by this issue, is this already being worked on?

I think this could be circumvented by not storing the tempfiles in the same directory (as long as it's on the same partition, this should be fine, right?), but in the tempfolder? or is there a reason why this is not done?
Hi @skjerns I think the idea you have is the right one :+1: In fact, I think is possible to change the dir where `atomic-writes` stores the temporary files by passing the `dir` kwarg ([which is used in Atomic Writes to hold the temp files](https://github.com/untitaker/python-atomicwrites/blob/5c39b454edf0176605d2b12ec60f83da8f82b958/atomicwrites/__init__.py#L178-L182)) with the path to the temp directory/a temp directory object here:

https://github.com/spyder-ide/spyder/blob/187c56a4909c7c5167f2c8a051033cd199bfa4c5/spyder/utils/encoding.py#L267-L268

I haven had time to work on a fix for this but maybe you want to try to implement the fix @skjerns ? Let us know :)
Good thinking @dalthviz! However, I would say that we should do that only if the directory you're trying to save to contains `Dropbox` in it. That's because it's the only directory our users have reported it gives problems with our atomic saving.
sure! I'll try that :)

And I'll also test it with other clients, such as NextCloud or OneDrive, as I would suspect that they could produce problems as well, however, as they are not so wide-spread it might not have been reported yet.
Thanks @skjerns !

Great! Thanks for your help @skjerns!

Resolving patch:
diff --git a/spyder/utils/encoding.py b/spyder/utils/encoding.py
--- a/spyder/utils/encoding.py
+++ b/spyder/utils/encoding.py
@@ -13,6 +13,7 @@
 
 # Standard library imports
 from codecs import BOM_UTF8, BOM_UTF16, BOM_UTF32
+import tempfile
 import locale
 import re
 import os
@@ -264,8 +265,13 @@ def write(text, filename, encoding='utf-8', mode='wb'):
             original_mode = 0o777 & ~umask
             creation = time.time()
         try:
+            # fixes issues with scripts in Dropbox leaving
+            # temporary files in the folder, see spyder-ide/spyder#13041
+            tempfolder = None
+            if 'dropbox' in absolute_filename.lower():
+                tempfolder = tempfile.gettempdir()
             with atomic_write(absolute_filename, overwrite=True,
-                              mode=mode) as textfile:
+                              mode=mode, dir=tempfolder) as textfile:
                 textfile.write(text)
         except OSError as error:
             # Some filesystems don't support the option to sync directories


Test patch: