Skip to content

Commit

Permalink
Fix index_files being set False on Package Control updates
Browse files Browse the repository at this point in the history
This commit persists old user value of `index_files` settings on disk as
global variables/states are vanished during Package Control updates, causing
`index_files` not being reset afterwards.

It uses json serialization to be able to extend backup usage in future,
without loosing backward compatibility. As Package Control blocks all other
updates, if it is going to update itself, it is very unlikely other settings
stored by PackageDisabler need to be persisted for the purpose of surviving
Package Control update.

There may however be other use cases to do so, which are not within scope 
of this fix.
  • Loading branch information
deathaxe committed Jul 24, 2023
1 parent 3ce4cfa commit 1ab5c0e
Showing 1 changed file with 25 additions and 10 deletions.
35 changes: 25 additions & 10 deletions package_control/package_disabler.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
save_list_setting
)
from .show_error import show_error
from .sys_path import pc_cache_dir


class PackageDisabler:
Expand Down Expand Up @@ -162,8 +163,6 @@ class PackageDisabler:
}
"""

index_files = None

lock = RLock()
restore_id = 0

Expand Down Expand Up @@ -441,9 +440,15 @@ def backup_and_reset_settings(packages, backup):
# packages can be disabled/installed and re-enabled without indexer restarting
# for each one individually. Also we don't want to re-index while a syntax
# package is being disabled for upgrade - just once after upgrade is finished.
if backup and PackageDisabler.index_files is None:
PackageDisabler.index_files = settings.get('index_files')
settings.set('index_files', False)
if backup:
try:
# note: uses persistent cookie to survive PC updates.
with open(os.path.join(pc_cache_dir(), 'backup.json'), 'x', encoding='utf-8') as fobj:
json.dump({'index_files': settings.get('index_files')}, fobj)
except OSError:
pass
else:
settings.set('index_files', False)

# Backup and reset global theme(s)
for key, default_file in PackageDisabler.default_themes.items():
Expand Down Expand Up @@ -539,9 +544,21 @@ def restore_settings(restore_id):

try:
# restore indexing settings
if PackageDisabler.index_files is not None:
settings.set('index_files', PackageDisabler.index_files)
save_settings = True
fname = os.path.join(pc_cache_dir(), 'backup.json')
try:
with open(fname, 'r', encoding='utf-8') as fobj:
data = json.load(fobj)
index_files = data.get('index_files')
if isinstance(index_files, bool):
settings.set('index_files', index_files)
save_settings = True
except FileNotFoundError:
pass
finally:
try:
os.remove(fname)
except OSError:
pass

# restore global theme
all_missing_theme_packages = set()
Expand Down Expand Up @@ -624,8 +641,6 @@ def restore_settings(restore_id):
if save_settings:
sublime.save_settings(preferences_filename())

PackageDisabler.index_files = None

PackageDisabler.color_scheme_packages = {}
PackageDisabler.theme_packages = {}

Expand Down

0 comments on commit 1ab5c0e

Please sign in to comment.