VSCode / csproj / tasks.json - Adding custom build step - visual-studio-code

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
}
}
]
}

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.

Configuring task.json and launch.json for C in vs code

I am a complete beginner with C and to VSCode. I am trying to configure the task and launch jsons but have no idea where to begin. I have tried googling the answers but I keep getting the same errors. I want to be able to step through the code line by line so I can see what it is doing.
I haven't changed the tasks.json from the original that VSCode sets. The launch.json I have changed by putting in the debugger path and the path of the executable. I have included the task.json, launch.json and the error that keeps popping up. Any help is appreciated.
tasks.json
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: gcc build active file",
"command": "/usr/bin/gcc",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
launch.json
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/scheduler.c",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceRoot}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "/usr/bin/gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Set Disassembly Flavor to Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
]
}
]
}
I've recently had the same issues. Here is the procedure that I follow, that seem to work:
Download and install msys2 (1)
Download MSYS2 from https://www.msys2.org/
Install folder is typically C:\msys64
Go to C:\msys64 and open mingw64.ini
Change ‘#MSYS2_PATH_TYPE=inherit’ to ‘MSYS2_PATH_TYPE=inherit’ i.e. enable PATH variable.
Add PATH environment variables (2)
Type “environment” in Windows search field, and select “Edit environment variables …”
Add path to gcc.exe and g++.exe:
— Add: “C:\msys64\mingw64\bin” to PATH variable.
Add path to VS Code to PATH variable as well:
— This is usually the folder:
C:\Users\<username>\AppData\Local\Programs\Microsoft VS Code\
Run “msys2 mingw64” and test access to compilers and debugger (3)
In Windows Search field, type “msys2” and select “msys2 mingw64” and run the following:
To install gcc, the Gnu C compiler: pacman -S mingw-w64-ucrt-x86_64-gcc
To install gdb, the Gnu GDB debugger:
pacman -S --needed base-devel mingw-w64-x86_64-toolchain
To test that you can run compilers and debugger, run:
gcc --version
g++ --version
gdb --version
Start development in VSC (4)
Open “msys mingw64” terminal and run:
cd <common projects folder>
mkdir <projname>
cd <projname>
Above changes current directory to a projects folder. I use C:\Users<username>\src\projects, i.e. this is my but you may want to use something else.
In the folder you can make a subfolder per C coding project you want to run. This is what “mkdir ” does.
You will need a little Unix Bash shell command skills here, here is an ultrashort summary:
cd – change directory to
mkdir – makes a new directory called
in the current folder.
cd .. – change directory one up.
_ cd ~ – changes directory to your home folder, I have C:\Users<username> where is my … username.
pwd – shows current directory.
Now, if you did above you are currently in the specific projects folder (you can verify this with pwd), and you should start VSC from there by typing:
code .
And accept Workspace trust.
Create a C source file (5)
Open folder view, click on + icon, and select ‘new file’, type “hello.c”, go into the file and add the contents:
include
int main() {
printf("Hello World\n");
return 0;
}
Configure VSC for building – tasks.json (6)
Press “ctrl+shift+B” to build target (or menu: -> Terminal -> Run Build Task… or press green play icon)
Select “C/C++: gcc.exe build and debug active file”
Now it tries to build, using an autogenerated tasks.json file, located in project-folder, in subfolder .vscode:
.vscode/tasks.json
An example of a tasks.json file is shown below:
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: gcc.exe build active file",
"command": "C:\apps\msys64\mingw64\bin\gcc.exe",
"args": [
"-fdiagnostics-color=always",
"-Wall",
"-g",
"${file}",
"-o",
"${fileDirname}\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
The important section is:
"command": "C:\msys64\mingw64\bin\gcc.exe",
"args": [
"-fdiagnostics-color=always",
"-Wall",
"-g",
"${workspaceFolder}/*.c", OR TODO "${file}"
"-o",
"${fileDirname}\${fileBasenameNoExtension}.exe"
],
“command” is the Gnu C compiler, gcc.exe and the full path to it.
“args” are arguments for the compiler:
-Wall means warn about everyting.
-g means compiler must prepare for debugging.
“${file}” is current file.
-o is output file which is specified in the next line with .exe extension.
Sometimes we have to build multi-file projects and it will break the default build-functionality in tasks.json in VSC. In some cases this can be solved by changing:
${file},
to
"${workspaceFolder}/*.c",
Configure VSC for Debugging – launch.json (7)
Go to source file hello.c, and set a break point,
Click left to the line numbers to set red circle.
Select play/bug icon
Select “Debug C/C++ File”
Choose “C/C++ gcc build and debug active file” from list of automatically detected compilers.
This will autogenerate a file, launch.json in the projects folder, in subfolder .vscode:
.vscode/launch.json
An example of a launch.json file is shown below:
{
"configurations": [
{
"name": "C/C++: gcc.exe build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\msys64\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
]
}
],
"preLaunchTask": "C/C++: gcc.exe build active file"
}
The most important parts are:
"program": "${fileDirname}\${fileBasenameNoExtension}.exe",
and
"miDebuggerPath": "C:\\msys64\\mingw64\\bin\\gdb.exe",
The “program” is the program generated when building the project. I.e. the output from running the task as specified in Tasks.json.
The miDebuggerPath is the path to gdb, the GNU gdb debugger.
If these does not match with your Tasks.json settings and your installation chances are slim to make it work.
Test it by pressing the Play/Bug icon button and notice if there are errors. If not you should be able to step through the code and watch variables etc.
Configure VSC for intellisense (8)
Install extension: “C/C++ extension for VS Code” by Microsoft.
Now this extension will assist when you type code. By example you can type for to get the basics of a for-loop. Similar for if, while etc. Cool!
Save work when creating new projects (9)
Instead of having to create the tasks.json and launch.json files for each project, I copy them to a templates folder, say:
C:\Users\username\src\templates\.vscode\
Copy the newly created tasks.json and launch.json files to the .vscode subfolder.
Now, say you want to create a new project, e.g. hello2, and you create a folder for it:
C:\Users\<username>\src\projects\hello2\
Go to the templates folder by
cd C:\Users<username>\src\templates
and copy the .json files to the new project by:
cp -Rp .vscode/ ../hello2/
And now the new project has the .json files.
Optional (10)
Later you may want to update msys2. To do this, open “msys2 mingw64” and type
pacman -Suy
Done (11)
Finally done … Not the easiest thing to get going, but I still think it is worth it.

How to set up tasks.json file in VSCODE to compile Fortran programs?

I want to set up VScode (OS: Windows 10) to create and then compile programs written in Fortran 90/95. I can do this by typing in the terminal : gfortran -o Example_exe Example.f90 and then ./Example_exe. I don't want to have to write these lines every time, so I tried to set up my tasks.json file to automate a build routine using gfortran as compiler.
I found this tutorial : https://titanwolf.org/Network/Articles/Article?AID=360e0bde-0507-4de4-960c-2eae8fa8c782#gsc.tab=0 but the tasks.json file given is unclear.
Can I have a tasks.json file setup to automate my build routine please ?
I have installed the following extensions : Modern Fortran, Fortran IntelliSense, Code Runner, Fortran Breakpoint Support
Yes you can. Assuming that you want to execute in debug mode, you should create a tasks.json and a launcher.json and place them in .vscode/ at the root of your workspace.
Assuming the following file structure and a debugging mode with GDB for the execution:
root/
.vscode/
code/
Example_exe
This is what your tasks.json should look like:
{
"version": "2.0.0",
"tasks": [
{
"label": "compile",
"type": "shell",
"command": "gfortran -o Example_exe Example.f90 -g",
"options": {
"cwd": "code/"
}
}
]
}
And then, the launch.json, which will identify tasks.json as a "preLaunchTask".
{
"version": "0.2.0",
"configurations":[
{
"name": "Run my example",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}\\code\\example_exe.exe",
"args": ["],
"stopAtEntry": false,
"cwd": "${workspaceFolder}\\code",
"miDebuggerPath": "gdb.exe",
"preLaunchTask": "compile",
}
]
}
Launch the debugger by pressing F5 (or in the Run menu).
If you don't want to run in debug mode, have a look at this issue.
Sources:
How to build and run C++ code in Visual Studio Code?
awesome tutorial (for ubuntu, working for me with VSCode-gfortran-Win10)
https://www.youtube.com/watch?v=Rj-kYb9nZ3g&ab_channel=LukasLamm.

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.

Why is dotnet run completely different than VS Code debug

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