How can I pass arg "--remote-debugging-port=9229" to VS Code Extension Host in launch.json? - visual-studio-code

I want to lanuch VS Code with --remote-debugging-port.
I can do this with runTest.ts like below.
This works fine with puppereer.connect().
import { runTests } from '#vscode/test-electron';
async function main() {
try {
// The folder containing the Extension Manifest package.json
// Passed to `--extensionDevelopmentPath`
const extensionDevelopmentPath = path.resolve(__dirname, '../../');
// The path to the extension test runner script
// Passed to --extensionTestsPath
const extensionTestsPath = path.resolve(__dirname, './suite/index');
// Download VS Code, unzip it and run the integration test
await runTests({
extensionDevelopmentPath,
extensionTestsPath,
launchArgs: [
"--remote-debugging-port=9229",
]
});
} catch (err) {
console.error(err);
console.error('Failed to run tests');
process.exit(1);
}
}
main();
But when I try to do same thing with launch.json, it's failed.
When I start debug, this may cause FetchError: Failed to fetch browser webSocket URL from http://127.0.0.1:9229/json/version: request to http://127.0.0.1:9229/json/version failed, reason: connect ECONNREFUSED 127.0.0.1:9229
{
"version": "0.2.0",
"configurations": [
{
"name": "Run Extension Test",
"type": "extensionHost",
"request": "launch",
"args": [
"--extensionDevelopmentPath=${workspaceFolder}",
"--extensionTestsPath=${workspaceFolder}/out/test/suite/index",
],
"runtimeArgs": [
"--remote-debugging-port=9229",
],
"outFiles": [
"${workspaceFolder}/out/test/**/*.js"
],
"preLaunchTask": "test-compile"
},
]
}
How can I do this with launch.json?
Edit:
I update my launch.json and http://127.0.0.1:9229 is now opened.
{
"args": [
"--remote-debugging-port=9229",
"--extensionDevelopmentPath=${workspaceFolder}",
"--extensionTestsPath=${workspaceFolder}/out/test/suite/index",
],
"port": "9229",
}
But now, I dont get webSocketDebuggerUrl from result of fetch 127.0.0.1:9229. It only return this json.
{Browser: 'node.js/v14.16.0', Protocol-Version: '1.1'}

Looking at the code they have a specific location of the launchArgs
if runTests works you could try
{
"version": "0.2.0",
"configurations": [
{
"name": "Run Extension Test",
"type": "extensionHost",
"request": "launch",
"args": [
"--remote-debugging-port=9229",
"--extensionDevelopmentPath=${workspaceFolder}",
"--extensionTestsPath=${workspaceFolder}/out/test/suite/index",
],
"outFiles": [
"${workspaceFolder}/out/test/**/*.js"
],
"preLaunchTask": "test-compile"
},
]
}

Related

How to configure launch.json for NextJs and Typesctipt

I am trying to create a launch.json file for the following repo:
https://github.com/zakariamofaddel/shopify-nextjs-template
I have tried both the default VS Code node template and the NextJs launch file.
VS code default Node generated .vscode/launch.json file
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"skipFiles": [
"<node_internals>/**"
],
"program": "${workspaceFolder}\\server\\index.ts"
}
]
}
I am able to run yarn dev successfully and use console.log:
https://github.com/zakariamofaddel/shopify-nextjs-template/blob/38c700d8706818aa12d892b3f1193a969919e003/package.json#L9
I found the solution after adding the TypeScript Debugger extension in VS code.
{
"version": "0.2.0",
"configurations": [
{
"name": "Debug Localhost",
"type": "node",
"request": "launch",
"args": [
"${workspaceRoot}\\server\\server.ts"
],
"runtimeArgs": [
"-r",
"ts-node/register/transpile-only"
],
"cwd": "${workspaceRoot}",
"protocol": "inspector",
"internalConsoleOptions": "openOnSessionStart",
"env": {
"NODE_ENV": "development"
}
}
]
}

VS code showing warning when creating a new theme

I am trying to create a new VS Code theme, but whenever I try to run the debugger vs code shows a warning "You don't have an extension for debugging 'JSON with comments'. Should we find a 'JSON with comments' extension in the marketplace?'
My launch.json file:
{
"version": "0.2.0",
"configurations": [
{
"name": "Extension",
"type": "extensionHost",
"request": "launch",
"args": ["--extensionDevelopmentPath=${workspaceFolder}"]
}
]
}
My package.json file:
{
"name": "theme",
"displayName": "theme",
"description": "theme",
"version": "0.0.1",
"engines": {
"vscode": "^1.58.0"
},
"categories": [
"Themes"
],
"contributes": {
"themes": [
{
"label": "theme",
"uiTheme": "vs-dark",
"path": "./themes/theme-color-theme.json"
}
]
}
}
I tried finding the JSON with comments debugger in the marketplace, but couldn't find it. Does anyone has any idea what I am doing wrong?
When debugging a theme your "./.vscode/launch.json" should look like this:
// "./.vscode/launch.json"
{
"configurations": [
{
"args": ["--extensionDevelopmentPath=${workspaceFolder}"],
"name": "Launch Extension",
"request": "launch",
"type": "pwa-extensionHost"
}
]
}
"You only need four (4) settings total, which are displayed in the snippet above. Everything else is unnecessary junk when debugging a theme."
You need to do one more thing:
At the very top of your "./themes/theme-color-theme.json" file add the following lines:
// "./themes/theme-color-theme.json"
{
"name": "Your-themes-name",
"type": "dark",
"$schema": "vscode://schemas/color-theme",
"colors": {
// theme highlighting...
},
}
EDIT:
I unintentionally added "semanticHighlighting": true to the snippet above. That's something that isn't related to this question so I removed it from the example. Semantic Highlight is important, though, and you should read about it before choosing true or false, if you haven't read about it already.

Unable to read appsettings.json when debugging .net core webapi in vscode

When debugging in vscode I'm unable to read the appsettings.json in my Startup.cs file of my webapi project. They all return as empty/null.
My launch.json configuration for the webapi was generated by adding the .NET Core Launch (console) configuration. I also tried adding the .NET Core Launch (web) configuration but the same problem occurs.
The only thing I then had to change was the "program" setting to point to the correct dll in my project folder.
Here is my launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Launch (console)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceFolder}/SATestUtils.API/bin/Debug/netcoreapp3.1/SATestUtils.API.dll",
"args": [],
"cwd": "${workspaceFolder}",
"stopAtEntry": false,
"console": "internalConsole"
}
]
}
And here is my tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/SATestUtils.Api/SATestUtils.API.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "publish",
"command": "dotnet",
"type": "process",
"args": [
"publish",
"${workspaceFolder}/SATestUtils.Api/SATestUtils.API.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "watch",
"command": "dotnet",
"type": "process",
"args": [
"watch",
"run",
"${workspaceFolder}/SATestUtils.Api/SATestUtils.API.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
}
]
}
Here is my appsettings.json and appsettings.Development.json file...
{
"ConnectionStrings": {
"DefaultConnection": "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=SaTestUtils"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"StorageAccountName": "devstoreaccount1",
"AzureAD": {
"Instance": "https://login.microsoftonline.com",
"Domain": "[Domain]",
"TenantId": "[TenantId]",
"ClientId": "[ClientId]",
"Scope": "[Scope]"
},
"AllowedHosts": "*"
}
When I attempt to debug the following code in my services method in Startup.cs
var connectionString = Configuration["ConnectionStrings:DefaultConnection"];
Console.WriteLine($"Connection string = {connectionString}");
I simply get the following logged to the debug terminal
Connection string =
All the other settings in appsettings.json are also not returning. The correct settings are returned when using dotnet run.
What could be the issue here?
Note that I have a solution file in my working directory, then I have a folder called SATestUtils.API that contains the SATestUtils.API.csproj file for my webapi project.
I experienced the same case, for me, what worked was what has been commented before:
Change the value of 'cwd' property in launch.json to the value of the project directory.
{
...
"cwd": "${workspaceFolder}"
}
to
{
...
"cwd": "${workspaceFolder}/SATestUtils.API"
}
All the credits to Bemm...
Did you try to specify the asp net environment at the launch.json?
Something like this:

My Visual Code Debug Extension, doesn't execute my debugger

I have created a Visual Code Debugger Extension, and in my extension activate routine, I can tell it was activated when I debug a sample *.xyz file, as it writes out XYZ Debuger Activated to the console. The problem is, my debugger executable named debugger.exe (which is a console application written in C#) does not get executed.
When I close the *.xyz file, my breakpoint in the deactivate extension function gets hit. this leaves me to believe that for the most part, the extension is working somewhat, but not totally.
What am I doing wrong?
Here is my extension.ts file
'use strict';
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
import * as vscode from 'vscode';
import { WorkspaceFolder, DebugConfiguration, ProviderResult, CancellationToken } from 'vscode';
import * as Net from 'net';
// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
export function activate(context: vscode.ExtensionContext) {
// Use the console to output diagnostic information (console.log) and errors (console.error)
// This line of code will only be executed once when your extension is activated
console.log('XYZ-Debugger Activated');
}
// this method is called when your extension is deactivated
export function deactivate() {
console.log('XYZ-Debugger Deactivated');
}
And here is my package.json file:
{
"name": "xyz-debugger",
"displayName": "XYZ Debugger",
"description": "Test Debugger.",
"version": "0.0.1",
"publisher": "Ed_Starkey",
"engines": {
"vscode": "^1.24.0",
"node": "^6.3.0"
},
"categories": [
"Debuggers"
],
"dependencies": {
"vscode-debugprotocol": "^1.20.0",
"vscode-nls": "^2.0.2"
},
"activationEvents": [
"onDebug"
],
"main": "./out/extension",
"contributes": {
"breakpoints": [
{
"language": "xyz"
}
],
"debuggers": [{
"type": "XYZDebug",
"label": "XYZ Debug",
"windows": {
"program": "program": "./out/debugger.exe"
},
"languages": [
{
"id": "xyz",
"extensions": [
".xyz"
],
"aliases": [
"XYZ"
]
}],
"configurationAttributes": {
"launch": {
"required": [
"program"
],
"properties": {
"program": {
"type": "string",
"description": "The program to debug."
}
}
}
},
"initialConfigurations": [
{
"type": "xyz",
"request": "launch",
"name": "Launch Program",
"windows": {
"program": "./out/debugger.exe"
}
}
],
}]
},
"scripts": {
"vscode:prepublish": "npm run compile",
"compile": "tsc -p ./",
"watch": "tsc -watch -p ./",
"postinstall": "node ./node_modules/vscode/bin/install",
"test": "npm run compile && node ./node_modules/vscode/bin/test"
},
"devDependencies": {
"typescript": "^2.5.3",
"vscode": "^1.1.5",
"#types/node": "^7.0.43",
"vscode-debugadapter-testsupport":"^1.29.0"
}
}
And here is my launch.json:
// A launch configuration that compiles the extension and then opens it inside a new window
{
"version": "0.1.0",
"configurations": [
{
"name": "Launch XYZ Debug",
"type": "extensionHost",
"request": "launch",
"runtimeExecutable": "${execPath}",
"args": ["--extensionDevelopmentPath=${workspaceRoot}" ],
"stopOnEntry": false,
"sourceMaps": true,
"outFiles": [ "${workspaceRoot}/out/**/*.js" ],
"preLaunchTask": "npm: watch"
},
{
"name": "Launch Tests",
"type": "extensionHost",
"request": "launch",
"runtimeExecutable": "${execPath}",
"args": ["--extensionDevelopmentPath=${workspaceRoot}", "--extensionTestsPath=${workspaceRoot}/out/test" ],
"stopOnEntry": false,
"sourceMaps": true,
"outFiles": [ "${workspaceRoot}/out/test/**/*.js" ],
"preLaunchTask": "npm: watch"
}
]
}
You're not registering what you want VSCode to do in activate.
Something like this will do what you want:
let factory: XYZDebugAdapterDescriptorFactory
export function activate(context: vscode.ExtensionContext) {
const debugProvider = new XYZDebugConfigProvider();
factory = new XYZDebugAdapterDescriptorFactory();
context.subscriptions.push(
vscode.debug.registerDebugConfigurationProvider(
'xyz', debugProvider
)
);
context.subscriptions.push(
vscode.debug.onDidReceiveDebugSessionCustomEvent(
handleCustomEvent
)
);
context.subscriptions.push(vscode.debug.registerDebugAdapterDescriptorFactory('xyz', factory));
context.subscriptions.push(factory);
}
You'll then need to provide an adaptor for the config. This one will connect to a DAP server on the supplied port or launch xyz.exe
class XYZDebugConfigProvider implements vscode.DebugConfigurationProvider {
resolveDebugConfiguration(folder: WorkspaceFolder | undefined, config: DebugConfiguration, token?: CancellationToken): ProviderResult<DebugConfiguration> {
const editor = vscode.window.activeTextEditor;
const defaultConfig = vscode.extensions
.getExtension("YOUR EXTENSION NAME HERE")
.packageJSON
.contributes
.debuggers[0]
.initialConfigurations[0];
if (!config.request && editor.document.languageId === 'xyz') {
return defaultConfig;
} else if (!config.request) {
// Not trying to debug xyz?
return undefined;
}
config = {
...defaultConfig,
...config
}
config.execArgs = (config.args || [])
.concat(`-l${config.port}`);
return config;
}
}
class ElpsDebugAdapterDescriptorFactory implements vscode.DebugAdapterDescriptorFactory {
private outputchannel: vscode.OutputChannel
constructor() {
this.outputchannel = vscode.window.createOutputChannel(
'XYZ Debug Log'
);
}
createDebugAdapterDescriptor(session: DebugSession, executable: DebugAdapterExecutable | undefined): ProviderResult<DebugAdapterDescriptor> {
if (session.configuration.type !== "xyz") {
return undefined
}
if (session.configuration.request == "launch") {
const args = [...session.configuration.execArgs, session.configuration.program]
this.outputchannel.appendLine(`Starting XYZ with "${session.configuration.executable}" and args "${args.join(" ")}"`)
const debugee = spawn(session.configuration.executable, args)
debugee.stderr.on('data', (data) => {
this.outputchannel.appendLine(data.toString())
})
debugee.stdout.on('data', (data) => {
this.outputchannel.appendLine(data.toString())
})
debugee.on("close", (data) => {
this.outputchannel.appendLine(`Process closed with status ${data}`)
})
debugee.on("error", (data) => {
this.outputchannel.appendLine(`Error from XYZ: ${data.message}:\n${data.stack}`)
})
}
return new vscode.DebugAdapterServer(session.configuration.port)
}
dispose() {}
}
Lastly you'll need to add the rest of the config that you might want to use to your package.json and into launch.json in the debuggee - that part is very well covered in the docs for the example debugger https://code.visualstudio.com/api/extension-guides/debugger-extension
The above is adapted from a ELISP debugger I was writing and is in turn inspired by the excellent Perl 6 support at https://marketplace.visualstudio.com/items?itemName=mortenhenriksen.perl-debug

Pass arguments to command in visual studio code

Is it possible to run a command in a variable and pass parameters? Given that this command requires input arguments.
Example:
// in launch.json
{
"program": "${command:somecommand(foo=${someargument})}"
}
Create an input in your launch.json which calls your command, there you can pass your argument. You can then reference the input in your attach/launch config.
// launch.json
{
"version": "0.2.0",
"inputs": [
{
"id": "commandInput",
"type": "command",
"command": "somecommand",
"args": {
"foo": "${someargument}"
}
}
],
"configurations": [
{
"name": "Launch",
"type": "node",
"request": "launch",
"program": "${input:commandInput}"
}
]
}