Alert This post is over a year old, some of this information may be out of date.

#DevHack: check how your VSCode extension is running

post

When developing Visual Studio Code extensions, it might come in handy to know when running a production- or debug-build.

Typically in Node.js projects, we verify this by using the NODE_ENV environment variable. Unfortunately, this approach cannot be used as VS Code runs in a different instance, and environment variables are lost.

Solution

Luckily, there is a way to check how your extension is running. You can check this via the vscode extension API.

You can retrieve the mode your extension is started with via the context.extensionMode property.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
import { ExtensionContext, ExtensionMode } from 'vscode';

export function activate(context: ExtensionContext) {
  
  if (context.extensionMode === ExtensionMode.Development) {
    // Running in development mode
  } else if (context.extensionMode === ExtensionMode.Test) {
    // Running in test mode
  } else {
    // Running in production mode
  }
}

There are three modes:

  • Development
  • Production
  • Test

Hopefully, this #DevHack helps you to tweak your development flow when creating VS Code extensions.

Back to top