Why is dotnet run completely different than VS Code debug - visual-studio-code

I have started a few unmodified projects using dotnet new -t web. I have tried to restore using VS Code v 1.8.1 and dotnet restore. From the tutorials I have followed on dot.net and mva.microsoft.com there shouldn't be any difference in the projects at that point. I think that's right and I have not modified the project in question. When I run the same fresh project in the same browser using dotnet run then open a browser the left side of the image is the result as expected. When I try to run using F5 or pressing the run button on VS Code then the right side of the image is the result. All the practice I have had watching Elmo with my daughter has helped me come to the conclusion that one of these things is not like the other.
The proximate cause is that VS Code runs the site without a valid reference to necessary resources like the Bootstrap CSS file. Incorrectly, VS Code references Bootstrap at http://localhost:5000/lib/bootstrap/dist/css/bootstrap.css, while running the project with dotnet run is more fruitful and uses the following external URL: https://ajax.aspnetcdn.com/ajax/bootstrap/3.3.6/css/bootstrap.min.css. Is this a bug that will be fixed eventually and is there some documentation that would help fix this?
From the looks of the .vscode folder and the comment so far, the tasks.json file seems to be a key difference. Here is the file content:
{
"version": "0.1.0",
"command": "dotnet",
"isShellCommand": true,
"args": [],
"tasks": [
{
"taskName": "build",
"args": [
"${workspaceRoot}\\project.json"
],
"isBuildCommand": true,
"problemMatcher": "$msCompile"
}
]
}
Here is the launch.json file:
{
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Launch (web)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceRoot}\\bin\\Debug\\netcoreapp1.0\\Avalon.dll",
"args": [],
"cwd": "${workspaceRoot}",
"stopAtEntry": false,
"internalConsoleOptions": "openOnSessionStart",
"launchBrowser": {
"enabled": true,
"args": "${auto-detect-url}",
"windows": {
"command": "cmd.exe",
"args": "/C start ${auto-detect-url}"
},
"osx": {
"command": "open"
},
"linux": {
"command": "xdg-open"
}
},
"env": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"sourceFileMap": {
"/Views": "${workspaceRoot}/Views"
}
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach",
"processId": "${command.pickProcess}"
}
]

I did have the same issue.
The workaround is as follows:
Either run:
bower install
For this you need to install nodejs and git
Install Node.js from https://nodejs.org/
Install git from https://git-scm.com/
npm install -g bower
bower install
Or open _Layout.cshtml, and change line 9 to:
<link rel="stylesheet" href="https://ajax.aspnetcdn.com/ajax/bootstrap/3.3.6/css/bootstrap.css" />
Please have a look at the deeper explanation to this problem explained here:
https://github.com/dotnet/cli/issues/4076
Regards,
Thomas

Related

How should tasks.json be configured for building a dotnet core solution for debugging in VSCode when the entry project references a class library

I'm having some trouble building dotnet core solutions in VSCode for debugging when the entry project has a reference to another internal class library. I have the "C# for Visual Studio Code (powered by OmniSharp)" extension installed. I am running on Ubuntu 22.04 with the dotnet 6.0.403 sdk.
While I can write, build, and run programs referencing class libraries from the command line without issue, they fail to build when launched from VS Code's "Run and Debug" section.
I've attempted to work through a basic demo solution starting from scratch:
mkdir DebugCode && cd DebugCode
dotnet new sln
dotnet new console -o DebugCode.Console
dotnet new classlib -o DebugCode.Core
dotnet sln add DebugCode.Console/Debug.Console.csproj
dotnet sln add DebugCode.Core/DebugCode.Core.csproj
I put a simple SockDrawer.cs class in the DebugCode.Core project with the following code:
namespace DebugCode.Core;
public class SockDrawer
{
private IList<string> Socks { get; set; } = new List<string>();
public SockDrawer()
{
FillDrawer();
}
public void FillDrawer()
{
Socks.Add("Godzilla socks");
Socks.Add("Sasquatch socks");
Socks.Add("Blue Lightning socks");
Socks.Add("boring work socks");
}
public void ListSocks()
{
foreach(var socks in Socks)
{
Console.Write($"{socks}, ");
}
Console.Write(Environment.NewLine);
}
}
And then I reference that in Program.cs of the DebugCode.Console project.
using DebugCode.Core;
var sockdrawer = new SockDrawer();
sockdrawer.ListSocks();
I can build and execute the program from the root DebugCode directory with no issue
dotnet run --project DebugCode.Console
The trouble starts when I try to launch the build for debugging. The build fails when launching and building from the "Run and Debug" section. Using the default extension generated launch.json and tasks.json files with the content:
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Launch (console)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceFolder}/DebugCode.Console/bin/Debug/net6.0/DebugCode.Console.dll",
"args": [],
"cwd": "${workspaceFolder}/DebugCode.Console",
"console": "integratedTerminal", // I did manually update this line
"stopAtEntry": false
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach"
}
]
}
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/DebugCode.Console/DebugCode.Console.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "publish",
"command": "dotnet",
"type": "process",
"args": [
"publish",
"${workspaceFolder}/DebugCode.Console/DebugCode.Console.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "watch",
"command": "dotnet",
"type": "process",
"args": [
"watch",
"run",
"--project",
"${workspaceFolder}/DebugCode.Console/DebugCode.Console.csproj"
],
"problemMatcher": "$msCompile"
}
]
}
I get a file not found error referencing the dependent class library:
Executing task: dotnet build [redacted]/DebugCode/DebugCode.Console/DebugCode.Console.csproj /property:GenerateFullPaths=true /consoleloggerparameters:NoSummary
MSBuild version 17.3.2+561848881 for .NET
Determining projects to restore...
Restored [redacted]/DebugCode/DebugCode.Console/DebugCode.Console.csproj (in 62 ms).
1 of 2 projects are up-to-date for restore.
DebugCode.Core -> [redacted]/DebugCode/DebugCode.Core/bin/Debug/net6.0/DebugCode.Core.dll
CSC : error CS0006: Metadata file '[redacted]/DebugCode/DebugCode.Core/obj/Debug/net6.0/ref/DebugCode.Core.dll' could not be found [[redacted]/DebugCode/DebugCode.Console/DebugCode.Console.csproj]
* The terminal process "dotnet 'build', '[redacted]/DebugCode/DebugCode.Console/DebugCode.Console.csproj', '/property:GenerateFullPaths=true', '/consoleloggerparameters:NoSummary'" terminated with exit code: 1.
* Terminal will be reused by tasks, press any key to close it.
Based on this error, I've tried updating the build task to build the entire solution instead of just the console project.
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/DebugCode.sln",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
}
In this case, the build succeeds but debugging fails due to a missing DebugCode.Console.deps.json file:
[redacted].vscode/extensions/ms-dotnettools.csharp-1.25.2-linux-x64/.debugger/vsdbg --interpreter=vscode --connection=/tmp/CoreFxPipe_vsdbg-ui-e0801ed66b6a4fb8aa6f0c54a5fc2cc3
Cannot use file stream for [[redacted]/DebugCode/DebugCode.Console/bin/Debug/net6.0/DebugCode.Console.deps.json]: No such file or directory
A fatal error was encountered. The library 'libhostpolicy.so' required to execute the application was not found in '[redacted]/DebugCode.Console/bin/Debug/net6.0/'.
Failed to run as a self-contained app.
- The application was run as a self-contained app because '[redacted]/DebugCode/DebugCode.Console/bin/Debug/net6.0/DebugCode.Console.runtimeconfig.json' was not found.
- If this should be a framework-dependent app, add the '[redacted]/DebugCode/DebugCode.Console/bin/Debug/net6.0/DebugCode.Console.runtimeconfig.json' file and specify the appropriate framework.
Interestingly, if I build the solution myself manually using the dotnet build DebugCode.sln command from my DebugCode directory, the above referenced DebugCode.Console.deps.json file is generated. Then, when I comment out the "preLaunchTask": "build" line of launch.json, the debugger successfully launches from the "Run and Debug" section. I figure I must have something wrong in the 'build' task in tasks.json, but I'm failing to recognize the difference between the dotnet build command I'm using manually and what tasks.json needs.
I got the preLaunchTask to build and launch the solution successfully in the debugger by removing ${workspaceFolder} from the path to the solution file.
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"DebugCode.sln", // removed ${workspaceFolder}/
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
}
I don't exactly understand why that works, but at least I'm building and debugging.

Launching and debugging Blazor server and webasm on VS Code

I have tried run visual studio code, to build, launch and debug (using omnisharp and dotnet...) the blazor server and webasm project but It seems to only launch one project at a time even tho task.json has both of the projects? is there a way to launch both project at the same time? or to keep one executing while the other is launched in case this wasnt possible?
here is my launch.json
{
"version": "0.2.0",
"configurations":
[
{
// Use IntelliSense to find out which attributes exist for C# debugging
// Use hover for the description of the existing attributes
// For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md
"name": ".NET Core Launch (web)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/BlazorApp/bin/Debug/net5.0/BlazorApp.dll",
"args": [],
"cwd": "${workspaceFolder}/BlazorApp",
"stopAtEntry": false,
// Enable launching a web browser when ASP.NET Core starts. For more information: https://aka.ms/VSCode-CS-LaunchJson-WebBrowser
"serverReadyAction": {
"action": "openExternally",
"pattern": "\\bNow listening on:\\s+(https?://\\S+)"
},
"env": {
"ASPNETCORE_ENVIRONMENT": "Development",
"ASPNETCORE_URLS": "http://localhost:5010"
},
"sourceFileMap": {
"/Views": "${workspaceFolder}/Views"
}
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach"
},
{
"type": "blazorwasm",
"name": "Launch and Debug Blazor WebAssembly Application",
"request": "launch"
}
]
}
the answer is using compounds parameters in the launch.json
How to run multiple tasks in VS Code on build?
as for debugging I have yet to see how to get the js debugger to work on Chrome

Starlark debugger for Bazel in Visual Studio Code

i am new to Visual studio Code. I followed this tutorial to set up a Bazel build configuration in Visual studio code (I use Windows 10).
I created a simple task.json
{
"version": "2.0.0",
"tasks": [
{
"label": "Build Example (Debug)",
"type": "shell",
"command": "bazel build //main:hello-world -c dbg",
"windows": {
"command": "bazel build //main:hello-world --experimental_enable_runfiles -c dbg"
},
"osx": {
"command": "bazel build //main:hello-world -c dbg --spawn_strategy=standalone",
},
"group": {
"kind": "build",
"isDefault": true
},
}
]
}
and launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Example",
"type": "cppvsdbg",
"request": "launch",
"args": [],
"stopAtEntry": false,
"preLaunchTask": "Build Example (Debug)",
"cwd": "${workspaceFolder}/bazel-out/x64_windows-dbg/bin/example.exe.runfiles/__main__/",
"program": "${workspaceFolder}/bazel-out/x64_windows-dbg/bin/main/hello-world.exe",
"externalConsole": false,
"windows": {
"type": "cppdbg",
"type": "cppvsdbg",
"cwd": "${workspaceFolder}/bazel-out/x64_windows-dbg/bin",
"program": "${workspaceFolder}/bazel-out/x64_windows-dbg/bin/main/hello-world.exe",
},
},
}
]
}
In this way with run-> start debugging, I am able to debug and stop to breakpoins within the .cpp code of my project.
However, I read here, that is also possible to use the Starlark debugger to debug the .bzl files and Starlark rules.
According to the instructions in the same page I should be able to do this "by right-clicking a build target in the Bazel Build Targets view and selecting "Build Target with Starlark Debugger"". Unfortunately i can't see this option in my Bazel Build Targets view windows:
The Bazel Build Targets view is empty. and if i right click i can't see the "Build Target with Starlark Debugger" option. According to this link i should be able to see my targets listed below Bazel Build Targets view. I guess i am missing something in the configuration of the project or maybe some starlack extension?
Thanks for any help.
Bazel build targets does not work for me on windows either. When I run it, the extension outputs some error about bazel query. I haven't been on windows recently enough to remember the exact message, but I believe it is something along the lines of what is documented in this open issue.
Looks like there is a pull request open to solve it but no one has reviewed it yet. Best bet might be to weigh in over on either one of those after checking your extension output error log to see if it matches what is documented in there. Alternatively, you can check out the Clion with Bazel plugin, I have not tried that on windows yet though.

VSCode / csproj / tasks.json - Adding custom build step

I have a VSCode solution consisting a C# project. Part of the C# project is a static website that I build using webpack and npm run build would build the website.
I would like to include the website build as a custom build step within the CSPROJ file. How do I do it?
I tried this in the vscode tasks.json file:
{
"label": "website-build",
"command": "npm",
"args": ["run", "build"],
"options": {
"cwd": "${workspaceFolder}/website"
}
},
But I got the following error:
> Executing task: C:\Program Files\nodejs\npm run build <
The terminal process failed to launch: A native exception occurred during launch (Cannot create process, error code: 193).
Terminal will be reused by tasks, press any key to close it.
I am no expert but I believe this should work (this should be your tasks.json file... It will have also your other configurations):
(consider to have your package.json in your workingFolder/prova)
{
"version": "2.0.0",
"tasks": [
{
"type": "npm",
"script": "run build",
"path": "prova/",
"problemMatcher": [],
"label": "npm: run build",
"detail": "install dependencies from package",
"group": {
"kind": "build",
"isDefault": true
}
}
]
}

Debugging Perl with Visual Studio Code

I have just started with Perl today and installed ActivePerl 5.24.1 and everything went well. I was able to create my test program testPerl.pl with simple a print command and run it through console.
Now I wanted to use Visual Studio Code to run my Perl script, and so I opened the project folder [testPerl.pl location] with Visual Studio Code and tried to debug the code. I have installed the Perl-Debug extension in the editor and when I hit F5, Visual Studio Code asked me to Select Environment and I chose the Perl Debug option, which actually created the launch.json file for me with the below contents.
{
"version": "0.0.2",
"configurations": [
{
"type": "perl",
"request": "launch",
"exec": "perl",
"name": "Perl-Debug",
"root": "${workspaceRoot}/",
"program": "${workspaceRoot}/${command.AskForProgramName}",
"inc": [],
"stopOnEntry": true
}
]
}
I have kept default values as it, and when I hit F5 again, it asked me for a command with default value test.pl. It is because of ${command.AskForProgramName}, I assume. I entered my file name testPerl.pl in the command, but then nothing happens. It starts and ends without any print in console.
How can I actually configure this launch.json file or is there another way I need to do this?
I tried with a newer version of the plugin: Perl Debug version 0.2.0.
This works out of the box. The proposed configuration looks as follows:
{
// 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": [
{
"type": "perl",
"request": "launch",
"name": "Perl-Debug local",
"program": "${workspaceFolder}/${relativeFile}",
"exec": "perl",
"execArgs": [],
"root": "${workspaceRoot}/",
"inc": [],
"args": [],
"env": {},
"stopOnEntry": true
},
{
"type": "perl",
"request": "launch",
"name": "Perl-Debug remote",
"program": "${workspaceFolder}/${relativeFile}",
"root": "${workspaceRoot}/",
"stopOnEntry": true,
"port": 5000
}
]
}
Do note I tried this out on a Mac, with Visual Studio Code version 1.24.0.
I ran Visual Studio Code on a Mac and changed
"program": "${workspaceRoot}/${command.AskForProgramName}"
to
"program": "${file}"
to get the current file to debug.