Debugging Extensions
Debugging Visual Studio extensions requires running a second instance of VS - the Experimental Instance. This keeps your main VS installation clean while testing.
The Experimental Instance
When you press F5 to debug your extension, Visual Studio launches a separate instance with its own:
- Settings and configurations
- Installed extensions
- Window layouts
- Recent projects
This isolation means:
- Your extension won’t crash your main VS
- You can test different configurations
- You can easily reset to a clean state
Starting a Debug Session
- Set your VSIX project as the startup project
- Press F5 or go to Debug > Start Debugging
- A new VS instance opens with “(Experimental Instance)” in the title bar
- Your extension is automatically installed and loaded
The first launch of the Experimental Instance is slower as it initializes. Subsequent launches are faster.
Setting Breakpoints
Set breakpoints in your extension code just like any other project:
protected override async Task ExecuteAsync(OleMenuCmdEventArgs e)
{
// Set a breakpoint on the next line
var docView = await VS.Documents.GetActiveDocumentViewAsync();
if (docView?.TextView != null)
{
// Your code here
}
}
When you trigger your command in the Experimental Instance, the debugger breaks at your breakpoint.
Debug Output
Use the Output window to see diagnostic information:
protected override async Task ExecuteAsync(OleMenuCmdEventArgs e)
{
await VS.StatusBar.ShowMessageAsync("Command started...");
try
{
// Your code here
System.Diagnostics.Debug.WriteLine("Command executed successfully");
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"Error: {ex.Message}");
}
}
View output in View > Output and select “Debug” from the dropdown.
ActivityLog.xml
Visual Studio logs detailed information to ActivityLog.xml:
Location:
%APPDATA%\Microsoft\VisualStudio\17.0_<hash>Exp\ActivityLog.xml
To enable verbose logging:
- Start VS with
/Logcommand line argument - Or set
VSLOGENABLED=1environment variable
The ActivityLog is invaluable for diagnosing package loading failures and initialization errors.
Resetting the Experimental Instance
If the Experimental Instance gets corrupted or you want a fresh start:
Method 1: Visual Studio
- Close all VS instances
- Open Developer Command Prompt for VS 2022
- Run:
devenv /ResetSettings Exp
Method 2: Delete User Data
- Close all VS instances
- Delete the folder:
%LOCALAPPDATA%\Microsoft\VisualStudio\17.0_<hash>Exp
Method 3: Reset All
# From Developer Command Prompt
devenv /ResetSettings Exp
devenv /ClearCache Exp
Common Debugging Issues
Extension Not Loading
Check these common issues:
- Missing attributes - Ensure your package has
[PackageRegistration] - Async loading - Use
AllowsBackgroundLoading = true - Missing dependencies - Check your NuGet references
- MEF errors - Look in ActivityLog.xml for composition errors
Breakpoints Not Hit
- Symbols not loaded - Ensure PDB files are included
- Wrong instance - Make sure you’re debugging the right VS
- Background thread - Use
JoinableTaskFactory.SwitchToMainThreadAsync()
Package Load Failure
Check the error message in VS and ActivityLog.xml:
SetSite failed for package [YourPackageGuid]
Common causes:
- Exception in
InitializeAsync - Missing service dependency
- COM registration issue
Debug Configuration
Your .csproj should include debug settings:
<PropertyGroup Condition="'$(Configuration)' == 'Debug'">
<StartAction>Program</StartAction>
<StartProgram>$(DevEnvDir)devenv.exe</StartProgram>
<StartArguments>/rootsuffix Exp</StartArguments>
</PropertyGroup>
The /rootsuffix Exp argument launches the Experimental Instance.
Next Steps
Once your extension is working, learn about Packaging and Publishing to share it with others.