Potentially because when a plugin loads, the classes get created right away, but the API isn’t ready to do anything until plugin_loaded is called. When you cause the plugin to be reloaded it works because by that point the plugin API is ready.
It’s not that simple, I’m afraid. If the API wasn’t ready at the time sublime.load_settings was called, the return value would be None and the following line fail with an AttributeError since None has no attribute get.
This issues used to be a problem quite a while ago when the settings files weren’t loaded fast enough or so it seemed, so it would be an extension of the “API not ready” problem. I don’t think this was ever solved and instead it just slowly went away until it wasn’t seen frequently enough (or at all) anymore. I seem to remember @facelessuser also having problems with this.
It would be nice if this could be somewhat reliably reproduced and an issue filed on the github Issues repo because I strongly believe this to be a bug.
Ah, that’s good to know. I suspected that might be the case, but lacking a machine to check on I thought perhaps it would still return a settings object but without the ability to retreive a setting.
I can’t say I’ve run across this before, although I did notice some weirdness at one point with ApplicationCommand and how sublime.find_resources won’t find resources if invoked from it’s __init__ method. However in that case it just never finds resources even if you force reload the module after the API is ready so it may not be related.
This seems to be the same issue as: Core$1508 Sublime Text cannot call the plugins forwards as on_selection_modified(1) before the plugin goes through the forward plugin_loaded(0)
The Sublime Text API, is only ready after all plugins being loaded, which can take some time, if the user has too much plugins. Then until the API is ready, you cannot call it, that is why calling API on __init__ does not work when Sublime Text is starting. The __init__ is called right away when Sublime Text finds the plugin, but the API is only ready some time later, when plugin_loaded() is called.
In the forward plugin_loaded(), until your plugin’s plugin_loaded() forward has not being called by Sublime Text, you cannot attempt to use Sublime Text API. This is explained on the Sublime Text API documentation:
All are called from what appears to be the same thread ID, so presumably the console output shows you the output of the calls to test in the same order as they were made.
plugin_loaded gets called first, and the API is available, then the ApplicationCommand is created, but it can’t access the API, then the WindowCommand is created, and it can.
Next, with the plugin still loaded, save the file to cause it to be reloaded:
Same call order as before, only the WindowCommand is not instantiated again, which I guess makes sense because they’re only created for a new window. However, even now the ApplicationCommand cannot access the API.