I am writing a VS Code extension which is only useful when a workspace is open. It relies specifically on ExtensionContext.storageUri
being defined (ie !== undefined
) and at least one folder being part of that workspace.
I want to avoid having to check the existence of a workspace and content each time I need to interact with it.
Looking at the available activation events I'm unsure on the best way to achieve what I need. Maybe using workspaceContains
for if any files exist?
"workspaceContains:**/*"
At the moment I am using onStartupFinished
, which appears to be emitted when a new workspace is opened (as opposed to only when VS Code is explicitly quit and reopened), which is great. But when a workspace isn't open it means that my extensions' activate()
function will have to handle this and leave the extension active without any functionality for the user to make use of. It doesn't feel right. Unless I throw an error of course, but I am not sure thats the right solution here either.
The activationEvent
pattern you suggested seems to work properly, and the extension is activated only for non-empty workspaces. However, starting at 1.74 release (Nov 2022), VS Code added a new feature for extension developers called Implicit Activation Events, which means that the extension will be activated even if you forgot to include some of the events you contributed, to the activationEvents
property. In the end, no matter if you didn't add some command to activationEvents
. When the user calls it, the extension will be activated, even on an empty workspace.
As an alternative, you could use When Clause Contexts to disable commands, but you still need to add some code/checks for this to work.
That being said, with the current API and with the latest Implicit Activation Events feature, I don't see how you could accomplish what you need. You will have to add some checking for empty workspaces on your extension.
Hope this helps