Closed Thom1729 closed 5 years ago
Awesome job. You mentioned that the current reloader code does not work in some edge cases, would you provide some insights?
Fallback may not be necessary, we could put a requirement of build 3189 on Package Control and show an error message to users who clone the repo directly but with build <3189.
You could also just ship an older build for older versions through package control's release channels (like for st2).
You could also just ship an older build for older versions through package control's release channels (like for st2).
I think it is what I 've said. Perhaps my english has broken.... nevermind
No, you just didn't mention to keep the old version and ship it for older at builds.
oh, you are right. I was thinking it is the same process....
Let's target it for ST4! 😄
@Thom1729 If you don't have anything to add, I am going to merge this.
PS: I have already pined the last tag for pre-3189 builds.
Sublime 3189 provides a new
sublime_plugin.load_module()
function. This allows us to load a module without using Sublime's reloading code, which may let us substantially simplify the reloading code. This PR is a first draft of what such a simplification may look like.The current reloading algorithm goes like this:
sublime_plugin.unload_module
).sys.modules
.sublime_plugin.reload_plugin
). This should reload the modules.Step 2 is not desirable by itself, but it's necessary because
sublime_plugin.reload_plugin
both reloads the given module and registers its contents with Sublime, and the reloading part works differently depending on whether the given module is insys.modules
. If it is, thenreload_plugin
will callunload_module
, which we already called in step 1. Therefore, it has proved necessary to manually remove items fromsys.modules
to invoke the second case inreload_plugin
. That case, in turn, callsimportlib.import_module
directly rather than using__import__
, which means that we have to temporarily add a new finder tosys.meta_path
to intercept the top-level plugin imports and actually reload modules.This PR uses the new
sublime_plugin.load_module
, which only registers the given module's contents with Sublime and does not attempt to unload the plugin contents or reload the module. (In essence,load_module
is the counterpart tounload_module
, whereasreload_plugin
is more likeunload_plugin
.) This allows the following algorithm:sublime_plugin.unload_module
).sublime_plugin.load_module
).Because
load_module
won't trigger module reloads, we can do it ourselves. The implementation in the PR does this by patching__import__
in a similar way toimporting_fromlist_aggresively
(and subsuming the latter). This ensures that modules are reloaded in the correct order without needing to modify eithersys.modules
orsys.meta_path
.The advantages of this approach are:
__import__
, but no longersys.modules
orsys.meta_path
.sublime_plugin
.Disadvantages may include:
load_module
for older versions of Sublime. (This is messy, but it won't break on us because old versions won't change.)This PR isn't ready; I need to do more testing and write the fallback code. The approach should be pretty stable, though, so I'd appreciate any comments or feedback.