Skip to content

Commit b62fec3

Browse files
committed
Clear disabled python environments
ST lists older python environments' lib paths in `sys.path` which may likely cause import errors in case order is mixed up by plugins. To avoid any conflicts remove libraries and caches of all disabled/absent python environments. Note: This might hit people switching between ST revisions with py38 and py313.
1 parent 544079b commit b62fec3

File tree

4 files changed

+40
-0
lines changed

4 files changed

+40
-0
lines changed

Package Control.sublime-settings

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@
8989
// the Package Control.sublime-settings file was synced to this machine.
9090
"remove_orphaned": true,
9191

92+
// Remove libraries of disabled or absent plugin_hosts at startup.
93+
"remove_orphaned_enviornments": true,
94+
9295
// If incompatible packages should be automatically upgraded when ST starts.
9396
// If ST is upgraded or moved to other computers, installed packages may
9497
// become incompatible with either ST, OS or CPU architecture.

package_control/clear_directory.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ def clear_directory(directory, ignored_files=None, ignore_errors=True):
4040
If all of the files and folders were successfully deleted
4141
"""
4242

43+
if not os.path.isdir(directory):
44+
return True
45+
4346
# make sure not to lock directory by current working directory
4447
if sys_path.longpath(os.path.normcase(os.getcwd())).startswith(os.path.normcase(directory)):
4548
os.chdir(os.path.dirname(directory))

package_control/package_cleanup.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ def run(self):
6565
# Clear trash
6666
clear_directory(sys_path.trash_path())
6767

68+
# Cleanup disabled python environments
69+
self.cleanup_python_environments()
70+
6871
# Scan through packages and complete pending operations
6972
found_packages = self.cleanup_pending_packages()
7073

@@ -141,6 +144,36 @@ def run(self):
141144
if message:
142145
show_message('Sublime Text needs to be restarted %s.' % message)
143146

147+
def cleanup_python_environments(self):
148+
"""
149+
Remove library and cache folders of disabled or absent plugin_hosts.
150+
"""
151+
if not self.manager.settings.get('remove_orphaned_enviornments'):
152+
return
153+
154+
# actual library dir
155+
libdir = os.path.join(sys_path.data_path(), "Lib", "python")
156+
# portable cache dir
157+
cache1 = os.path.join(sys_path.cache_path(), "__pycache__", "install", "Data", "Lib", "python")
158+
# normal setup's cache dir
159+
cache2 = os.path.join(sys_path.cache_path(), "__pycache__", "data", "Lib", "python")
160+
161+
supported_versions = sys_path.python_versions()
162+
if "3.3" not in supported_versions:
163+
# Python folder is re-created at each startup, hence just clear it for now.
164+
clear_directory(libdir + "33")
165+
# Python 3.3 itself doesn't support nor create compiled cache modules,
166+
# ST's fallback mechanism might however have created some py38 or py313 cache files
167+
# in those directories, which need to be cleared out.
168+
delete_directory(cache1 + "33")
169+
delete_directory(cache2 + "33")
170+
171+
if "3.8" not in supported_versions:
172+
# if 3.8 is not supported it is not present, delete all folders
173+
delete_directory(libdir + "38")
174+
delete_directory(cache1 + "38")
175+
delete_directory(cache2 + "38")
176+
144177
def cleanup_pending_packages(self):
145178
"""
146179
Scan through packages and complete pending operations

package_control/package_manager.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ def __init__(self):
120120
'proxy_password',
121121
'proxy_username',
122122
'remove_orphaned',
123+
'remove_orphaned_enviornments',
123124
'renamed_packages',
124125
'repositories',
125126
'submit_url',

0 commit comments

Comments
 (0)