how to launch vscode live server as a task in launch.json - visual-studio-code

I need to have vscode to launch vscode live server as a run configuration in launch.json, to be able to include its launch with other run configurations in a compound launch.
I have tried the "Launch Extension" of type "pwa-extensionHost" with several different arguments and runtimeexecutables, for example
{
"name": "Launch Extension",
"request": "launch",
"type": "pwa-extensionHost",
"runtimeExecutable": "liveserver"
}
and
{
"name": "Launch Extension",
"request": "launch",
"type": "pwa-extensionHost",
"args": [
"--extensionDevelopmentPath=${workspaceFolder}",
"liveserver"
]
}
but couldn't get it run.

Related

How do I get new HTML files to execute in VS Code?

I have recently started learning WebDev using Colt Steele Udemy course. I've created few HTML files and whenever I try to execute any of them only the first created .html file opens in Chrome. How do I get the other files to open in the browser. Eg: If I create 3 files i.e a.html, b.html & c.html only a.html opens in Chrome even if b/c is open in the editor. This happens with both debugging or run without debugging. My VS Code version is 1.65.2. Please advice.
The launch.json code is:
"version": "0.2.0",
"configurations": [
{
"type": "pwa-chrome",
"request": "launch",
"name": "Open a.html",
"file": "e:\\\\Colt Steele\\HTML the essentials\\a.html"
}
The launch configuration you're showing is telling VSCode what to do when running the project. You can change the launch configuration to open a different file by default:
{
"type": "pwa-chrome",
"request": "launch",
"name": "Open b.html",
"file": "e:\\\\Colt Steele\\HTML the essentials\\b.html"
}
Or perhaps add multiple launch configurations, which you can select when debugging/running:
{
"type": "pwa-chrome",
"request": "launch",
"name": "Open a.html",
"file": "e:\\\\Colt Steele\\HTML the essentials\\a.html"
},
{
"type": "pwa-chrome",
"request": "launch",
"name": "Open b.html",
"file": "e:\\\\Colt Steele\\HTML the essentials\\b.html"
}
In general any given "application" would have a single default page as the entry point, and from there you can navigate through the application to use the other pages. So you might have a single index.html as the launch file, and in whatever you're building there would be links to other files as needed for the application.
But if what you're building for whatever reason truly has different "entry points" then you can just set up your build configurations based on that and select which configuration to use when running the application.
Alternatively (and I haven't tested this), you should be able to tell it to launch "the current file" by using a variable called file in the launch config:
{
"type": "pwa-chrome",
"request": "launch",
"name": "Open Current File",
"program": "${file}"
}

How to launch swagger url through vscode?

I am trying to launch swagger UI in browser using dotnet run command. Here is what I have tried in launch.json file. Expected outcome is that it should launch browser at the specified url and open swagger UI automatically. FYI I am using dotnet5 and created new web api project that already has swagger baked in. It doesn't launch the browser.
If you are using dotnet run command then its a known limitation that it doesn't launch a browser.
Please refer to this link for confirmation
While it doesn't launch it, it will still have your project running just means you manually have to type it in browser or use one of the workarounds listed in that issue.
Use compounds launch settings
"configurations": [
{
"name": ".NET Core Launch (console)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceFolder}/src/WebApi/bin/Debug/net7.0/YourWebDll.dll",
"args": [],
"cwd": "${workspaceFolder}",
"stopAtEntry": false,
"console": "internalConsole"
},
{
"name": "Launch Chrome",
"request": "launch",
"type": "chrome",
"url": "https://localhost:5001/swagger/index.html",
"webRoot": "${workspaceFolder}"
}
],
"compounds": [
{
"name": ".Net Core & Chrome",
"configurations": [".NET Core Launch (console)","Launch Chrome"]
}
]
This will run your app and launch chrome at the URL you want.

How do I switch language in Visual Studio Code?

I am playing with a few programming languages within Visual Studio Code. My initial language is Python, and then I added Go in there within their own respective folders. When I run the Go file, it tries to interpret with Python, which obviously would fail.
Could I have different languages within the same project and separate them by different interpreter?
How do I point one folder to run with Python, the other to run with Go?
Thanks.
I've gotten it to work. The Configuration settings control what you are building. It is set within the launch.json file, and you can have different languages with the same project and run them in different interpreter/compiler.
This is what I have in my launch.json.
"version": "0.2.0",
"configurations": [
{
"name": "GO: Launch",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${fileDirname}",
"env": {},
"args": []
},
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal"
}
]

Why can't vscode find `preLaunchTask`?

I am using vscode to debug a typescript program. Below is the configuration. It has a preLaunchTask to use the task to build the code before debugging. when I run debugger, it prompts an error dialog Could not find the task 'build'. It fails to find the build task which is already defined in tasks section. How can I make it work in vscode?
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceFolder}/node/src/index.ts",
"skipFiles": [
"<node_internals>/**"
],
"preLaunchTask": "build",
"outFiles": [
"${workspaceFolder}/node/dist/**/*.js"
]
},
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal"
}
],
"tasks": [
{
"label": "build",
"type": "npm",
"script": "yarn build"
}
]
}
The custom tasks should be in a separate tasks.json file, not in the same launch.json file.
From the docs on launch.json attributes:
preLaunchTask - to launch a task before the start of a debug
session, set this attribute to the name of a task specified in
tasks.json (in the workspace's .vscode folder).
See the VS Code tutorial on creating Tasks Auto-Detection and Custom Tasks.

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