How to continue debug VSCode extension when extension open new windows? - visual-studio-code

I press F5 to debug a VSCode extension and VSCode will open a new window that will contain the extension. The extension will open a new VSCode window to open a specific project. But this new VSCode window didn't contain the extension and I can't debug it anymore. What can I do? 😩

Not sure if you are looking for a specific extension. If you have already installed the debugger extension, Try to uninstall/reinstall.

Your automatically generated extension launch configuration should look like this:
{
"version": "0.2.0",
"configurations": [
{
"name": "Run Extension",
"type": "extensionHost",
"request": "launch",
"runtimeExecutable": "${execPath}",
"args": [
"${workspaceFolder}/../OneDrive/Test Bed", // add the path here
"--extensionDevelopmentPath=${workspaceFolder}"
]
},
{
"name": "Extension Tests",
"type": "extensionHost",
"request": "launch",
"runtimeExecutable": "${execPath}",
"args": [
"--extensionDevelopmentPath=${workspaceFolder}",
"--extensionTestsPath=${workspaceFolder}/test/suite/index"
]
}
]
}
You see in the Run Extension configuration that I added a path from the extension's ${workspaceFolder} up one level and then into my OneDrive folder to another folder named Test Bed. That Test Bed folder will be opened in the first ExtensionHost window that is opened when you F5 and you can debug the extension using that folder immediately.

Related

Open workspace when testing VS Code extension

I am writing a test suite for my VS Code extension using vscode-extension-tester, and my tests need to involve the opening of a workspace file. I need the workspace file to be opened as soon as the browser is launched, in order for my subsequent tests to pass.
I am able to open the workspace file as a separate test; however, I was wondering if there's any setting that allows the workspace to be opened before any test is launched. Is there a way to do this with vscode-extension-tester?
The way I handle this is through my launch configuration (launch.json).
Using an extensionHost type launch configuration you can pass a workspace file. This means that the code instance will open with the given workspace active whenever you execute it.
For example:
{
"version": "0.2.0",
"configurations": [
{
"name": "Run Extension",
"type": "extensionHost",
"request": "launch",
"args": [
"${workspaceFolder}/src/test/workspace/workspace.code-workspace",
"--extensionDevelopmentPath=${workspaceFolder}"
],
"outFiles": [
"${workspaceFolder}/dist/**/*.js"
],
"preLaunchTask": "${defaultBuildTask}"
},
{
"name": "Extension Tests",
"type": "extensionHost",
"request": "launch",
"args": [
"${workspaceFolder}/src/test/workspace/workspace.code-workspace",
"--extensionDevelopmentPath=${workspaceFolder}",
"--extensionTestsPath=${workspaceFolder}/out/test/suite/index"
],
"outFiles": [
"${workspaceFolder}/out/**/*.js",
"${workspaceFolder}/dist/**/*.js"
],
"preLaunchTask": "tasks: watch-tests"
}
]
}
Note the path to workspace.code-workspace that is specified as an arg to both of these launch configs.
You will likely already have launch configs like this for running and testing your extension if you used Yo Code to generate your extension at the start of development.
So in summary:
Create your workspace
Store the workspace file accordingly (for me it made sense to be within ./src/test)
Configure extensionHost type launch configs with a path to the workspace file given as an arg in the args array.

Current Working Directory on vscode extensionHost launch

I did some theme development in the past and wanted to update it. Unfortunately, I didn't save my previous launch.json and it's missing functionality to open files on f5.
Only option I find is cwd, but it isn't working.
{
"version": "0.2.0",
"configurations": [
{
"name": "Extension",
"type": "extensionHost",
"request": "launch",
"runtimeExecutabl5e": "${execPath}",
"args": [
"--extensionDevelopmentPath=${workspaceFolder}"
],
},
]
}
I am sure, I did use to open files inside vscode opened by launch task while developing a new theme. That's how I did testing for syntax highlighting (by opening different files on launch inside of the vscode with developed extension )...
What's changed and how to make it work again?
So... to open file or folder in vscode you need to provide it as last argument for args. In my case, launch.json will looks like this:
{
"version": "0.2.0",
"configurations": [
{
"name": "Extension",
"type": "extensionHost",
"request": "launch",
"args": [
"--extensionDevelopmentPath=${workspaceFolder}",
"--disable-extensions",
"${workspaceFolder}/examples"
],
},
]
}

How to debug Deno in VSCode

How do we configure .vscode/launch.json to debug Deno projects?
The IntelliSense the VSCode provides when I was in configurations didn't offer an option for Deno. Or is there an extension for this?
You need to attach the debugger, as per the deno manual.
Create .vscode/launch.json replacing <entry_point> with your actual script and then F5.
{
"version": "0.2.0",
"configurations": [
{
"name": "Deno",
"type": "node",
"request": "launch",
"cwd": "${workspaceFolder}",
"runtimeExecutable": "deno",
"runtimeArgs": ["run", "--inspect-brk", "-A", "<entry_point>"],
"port": 9229
}
]
}
It will stop at the breakpoints you set on VS Code, tried here and it worked fine.
About the VS Code plugin:
Official support in plugin is being worked on -
https://github.com/denoland/vscode_deno/issues/12
The official VS Code Deno extension comes with handy debug support starting with v2.3.0.
Screencast from the PR:
Fresh projects
You can already press F5 to debug the active file without launch.json (quite useful).
To auto-generate launch.json with a Deno entry: Press CTRL+Shift+D (Open debug view) → "create a launch.json file" → Deno
Add Deno entry in existing launch.json
Press Add Configuration... in opened launch.json (see screencast above).
F5 will now trigger the currently active debug launch action.
Launch active file
To debug the currently active file in case of an already configured launch.json, change:
{
"type": "pwa-node",
"program": "${file}", // change "program" value to "${file}"
// ...
},
Create debug selection shortcut
// Inside keybindings.json
{
"key": "ctrl+alt+d",
"command": "workbench.action.debug.selectandstart",
"args": "Start debug task"
},
The shortcut is called "Debug: Select and Start Debugging" - see also this related post.
Enable log output in Debug Console
To have log output shown in the debug console, I still needed to add "outputCapture": "std" to the config entry. More infos:
Where is stdout for VS Code?
Debug output doesn't appear in debug console without "outputCapture": "std" #41600
Related
https://code.visualstudio.com/docs/editor/debugging
https://code.visualstudio.com/docs/nodejs/nodejs-debugging
to debug current file, you can use below configuration :)
"outputCapture": "std" allows deno to print to VS code console
{
"version": "0.2.0",
"configurations": [
{
"name": "Deno",
"type": "node",
"request": "launch",
"cwd": "${workspaceFolder}",
"runtimeExecutable": "deno",
"runtimeArgs": ["run", "--inspect-brk", "-A", "${fileBasename}"],
"outputCapture": "std",
"port": 9229
}
]
}
P.S. just added to Evandro Pomatti's answer

How to disable "just my code" setting in VSCode debugger?

When starting my project in the debugger (C# .NET Core), it states it's debugging "just my code".
I want to also debug the libraries, and can't see a setting to disable this anywhere in VSCode.
Is it possible to disable?
Just adding "justMyCode": false to launch.json doesn't work. You need to add a separate config in launch.json like below. FYI each {} represents a config.
"configurations": [
{
.... # existing config
},
{
"name": "Debug Unit Test",
"type": "python",
"request": "test",
"justMyCode": false,
}
]
As pointed out in here
For this you need to change the launch.json file. Inside the launch.json file you have to set "justMyCode" to false.
As described here. (I was pointed to that link through this post on the Visual Studio Code site.)
VSCode 1.60 was complaining about the "request": "test" method suggested by others.
But I did have to add a new section below my existing configuration to get "justMyCode": false to work.
Here is what worked for me:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"args": [
"blah",
"whatever"
]
},
{
"name": "Python: Debug Unit Tests",
"type": "python",
"request": "launch",
"purpose": ["debug-test"],
"console": "integratedTerminal",
"justMyCode": false,
}
]
}
The purpose addition appears to be important.
I found the correct approach documented here: https://code.visualstudio.com/docs/python/testing#_debug-tests
If you're specifically debugging Python unit tests, adding "justMyCode": "false" to your normal config won't do, you'll need to add another in your launch.json with "request": "test":
{
"name": "Debug Unit Test",
"type": "python",
"request": "test",
"justMyCode": false,
},
Source: Github Microsoft/vscode-python Issue #7131
I added the "justMyCode": false" setting to launch.json and it still didn't stop at breakpoints in external library code. What was even more confusing: It did work for once and then suddenly it didn't anymore.
Then I found out: If you are in the "Run and Debug (SHIFT+CMD+D)"-tab and select your configuration there and click the green triangle / "Start Debugging (F5)" it works! However, if I click "Debug Python File" in the upper right corner it does not stop in external library code!
As of 2022, VS Code no longer seems to have an “Open launch.json” shortcut in its command palette, but it still prompts you to edit launch.json to set "justMyCode" to false. The solution that worked for me was:
create a directory called .vscode in the root of the repository
create a file called launch.json in the .vscode directory
put this text in the file:
{
"version": "0.2.0",
"configurations": [
{
"name": "Debug Unit Test",
"type": "python",
"request": "test",
"justMyCode": false,
}
]
}
There will be an error message under the word "test" saying that "test" is not a valid value, and that "attach" would be valid. However, it doesn't work for me if I change "test" to "attach". If the "version" field isn't there, it doesn't work and VS Code raises an error saying launch.json is missing a field.
In the documenentation of Visual Studio Code they have a section "Skipping uninteresting code".
VS Code Node.js debugging has a feature to avoid source code that you don't want to step through (AKA 'Just My Code').
This feature can be enabled with the skipFiles attribute in your launch configuration. skipFiles is an array of glob patterns for script paths to skip.
In your launch.json file you have to add (or any other file you want to skip):
"skipFiles": [
"${workspaceFolder}/node_modules/**/*.js",
"${workspaceFolder}/lib/**/*.js"
]
If you are using vscode on mac, press shift+command+p, search for Open'launch.json', open an editor you want, and add the following JSON object to the file :
{
"name": "Python: Debug Unit Tests",
"type": "python",
"request": "launch",
"purpose": ["debug-test"],
"console": "integratedTerminal",
"justMyCode": false,
}
Reopen your vscode and now you can put breakpoints on lines that are imported or you have not written.
I added in the configurations part as below:
"configurations": [
{
"name": "Python: Curent File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"justMyCode": false,
}
],
It took me a while to understand where the file needs to be and what exactly needs to be inside it. So here's what I've got for others to enjoy:
The launch.json file is not in the root of your project, it needs to be in .vscode/launch.json instead. And for new VSCode versions, once you open that file from that location, you can get warnings on issues in the file, and also automatically add sections to it.
For me, at the end the contents of the file is this:
{
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"justMyCode": false
},
]
}
This allows me to enter (F11) other libs when I run a custom code in debug mode for Python.
None of the fiddling with launch.json worked for me.
I had to tick the "Allow setting breakpoints in any files" box in the Settings:
Recently, even I faced this issue where the VS code was not taking the latest launch.json (one with 'justMycode: false'). So, I had to perform the following steps.
Instead of running a debugger from the Top right group menu of the editor, I ran it from below status bar as shown in the below picture
Click here to see the screenshot of the status bar
Once clicked on this option a pop-up appears asking which launch.json you want to run your debugging with, as shown below.
Click here to see the screenshot of the pop-up menu
You can click on launch.json here to edit the configuration file and now the debugger opens with the selected launch.json and 'justtMycode:false' setting will be applied.
I merged the previous answers and the below setting works for me (vscode 1.75.0).
"launch": {
"configurations": [
{
"name": "Debug Unit Test",
"type": "python",
"request": "launch",
"purpose": "debug-test",
"justMyCode": false,
"program": "${file}",
}
],
},

Can not change debug environment automatically on visual studio code with config

For example, my root workstation directory is /home/chain/Project. And I have two separate projects which is python and website. My launch.json goes:
{
"version": "0.2.0",
"configurations":
[
{
"name": "Python",
"type": "python",
"request": "launch",
"stopOnEntry": true,
"pythonPath": "${config:python.pythonPath}",
"program": "${workspaceRoot}/python_project_source/test.py",
"cwd": "${workspaceRoot}/python_project_source",
"env": {},
"envFile": "${workspaceRoot}/.env",
"debugOptions": [
"WaitOnAbnormalExit",
"WaitOnNormalExit",
"RedirectOutput"
]
},
{
"type": "chrome",
"request": "launch",
"name": "Launch Chrome against localhost",
"url": "${workspaceRoot}/website/test.html",
"webRoot": "${workspaceRoot}/website"
}
]
}
As expected when I select test.py and press 'F5' it can switch to python debug environment, and when I select test.html the Chrome will be opened.
The fact is, VS code only remember the environment I debugged last time rather than change it automatically. So the only thing I do now is adding some comments to one environment (/* */) when I need to compile the other.:(
Is there something wrong in my launch.json?
Automatically switching the debug environment based on the file (or file-type) is not a current feature of Visual Studio Code, I believe.
You'll have to manually switch the launch configuration depending on the type of debugging task you want to perform.
And, of course, you could consider writing a feature request: https://github.com/Microsoft/vscode