Visual Studio Code C# build Error On Ubuntu - c#

Last year I have successfully installed Visual Studio Code on Ubuntu 18.04 to develop with Python.
Yesterday I have installed ".NET Core for C# coding on Linux" so I can code C# with Visual Studio Code.
Workspace directory contents shown here: http://fortitudevolution.com/VisualStudioCode/images/Selection_999(3031).png
I am using this task.json file located parentdirectory/.vscode/tasks.json
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "clean",
"command": "dotnet clean",
"type": "shell",
"presentation": {
"reveal": "always"
},
"problemMatcher": "$msCompile"
},
{
"label": "build",
"command": "dotnet build",
"type": "shell",
"dependsOn": "clean",
"group": {
"kind": "build",
"isDefault": true
},
"presentation": {
"reveal": "always"
},
"problemMatcher": "$msCompile"
},
{
"label": "clean release",
"command": "dotnet clean --configuration Release",
"type": "shell",
"presentation": {
"reveal": "always"
},
"problemMatcher": "$msCompile"
},
{
"label": "publish",
"command": "dotnet publish --configuration Release",
"type": "shell",
"dependsOn": "clean release",
"presentation": {
"reveal": "always"
},
"problemMatcher": "$msCompile"
},
{
"label": "runFunctionsHost",
"type": "shell",
"dependsOn": "build",
"options": {
"cwd": "${workspaceFolder}/bin/Debug/netstandard2.0"
},
"command": "func host start",
"isBackground": true,
"presentation": {
"reveal": "always"
},
"problemMatcher": "$func-watch"
}
]
}
For the application code Program.cs shown in directory contents, I am getting this error when I run "Terminal -> Run Build Task":
Executing task in folder C#: dotnet clean
Microsoft (R) Build Engine version 16.9.0+57a23d249 for .NET Copyright
(C) Microsoft Corporation. All rights reserved.
MSBUILD : error MSB1003: Specify a project or solution file. The current working directory does not contain a project or solution file.
The terminal process "/bin/bash '-c', 'dotnet clean'" terminated with
exit code: 1.
Terminal will be reused by tasks, press any key to close it.
I would appreciate any help in resolving this.

For each command in the tasks array, add ${workspaceFolder}. So the first command for dotnet clean becomes dotnet clean ${workspaceFolder} and so on. This tells the dotnet command what directory to work from.

Related

VSCode .NET Core Debugging seems to ignore working directory

Recently, debugging has broken for my ASP.NET Core, C# solution.
VSCode 1.65.0 and C# extension v1.24.1
I get this message.
> Executing task in folder src: dotnet build /property:GenerateFullPaths=true /consoleloggerparameters:NoSummary <
Microsoft (R) Build Engine version 17.0.0+c9eb9dd64 for .NET
Copyright (C) Microsoft Corporation. All rights reserved.
MSBUILD : error MSB1011: Specify which project or solution file to use because this folder contains more than one project or solution file.
The terminal process "/bin/zsh '-c', 'dotnet build /property:GenerateFullPaths=true /consoleloggerparameters:NoSummary'" terminated with exit code: 1.
Terminal will be reused by tasks, press any key to close it.
The debugger configuration selector looks like this:
I am trying to run the ticked item, i.e. "Debug TZ Naked (src)" - note that TZ is a code for my app and "naked" means not-in-Docker.
The configuration looks like this:
{
"name": "Debug TZ Naked",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceFolder}/AcmeCorp.CoyoteApp.AspNetCoreMvc/bin/Debug/net6.0/AcmeCorp.CoyoteApp.AspNetCoreMvc.dll",
"args": [],
"cwd": "${workspaceFolder}/AcmeCorp.CoyoteApp.AspNetCoreMvc",
"stopAtEntry": false,
"internalConsoleOptions": "openOnSessionStart",
"launchBrowser": {
"enabled": true,
"args": "${auto-detect-url}",
"windows": {
"command": "cmd.exe",
"args": "/C start ${auto-detect-url}"
},
"osx": {
"command": "open",
"args": "https://localhost:44340"
},
"linux": {
"command": "xdg-open"
}
},
"env": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"sourceFileMap": {
"/Views": "${workspaceFolder}/Views"
}
},
Note that the cwd key is set to the correct value, as is relative to my workspace. I've not renamed any folders or moved my workspace for months.
I have a build task here:
{
"label": "build",
"type": "process",
"command": "dotnet",
"options": {
"cwd": "${workspaceFolder}/AcmeCorp.CoyoteApp.AspNetCoreMvc"
},
"args": [
"build",
"${workspaceFolder}/AcmeCorp.CoyoteApp.AspNetCoreMvc/AcmeCorp.CoyoteApp.AspNetCoreMvc.csproj"
],
"problemMatcher": "$msCompile"
},
I'm not sure why it's stopped working. I've restarted VSCode just in case, and I've switch the working directory of open terminals to no avail.

How can I configure Visual Studio Code to run/debug .NET (dotnet) Core from the Windows Subsystem for Linux (WSL)?

I've installed .NET Core 2.2 in the Windows Subsystem for Linux (WSL) and created a new project. I've also installed the C# extension for Visual Studio Code and the syntax highlighting and IntelliSense seems to be working.
However, when I try to use the debugger, things stop working. Here's a step by step of what I've tried to do to configure it.
Here's my launch.json file:
{
// 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": ".NET Core Launch (console)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceFolder}/bin/Debug/netcoreapp2.2/CodeCore.dll",
"args": [],
"cwd": "${workspaceFolder}",
"console": "internalConsole",
"stopAtEntry": false,
"internalConsoleOptions": "openOnSessionStart"
},
{
"name": ".NET Core Launch (web)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceFolder}/bin/Debug/netcoreapp2.2/CodeCore.dll",
"args": [],
"cwd": "${workspaceFolder}",
"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": "${workspaceFolder}/Views"
}
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach",
"processId": "${command:pickProcess}"
}
]
}
And my tasks.json file:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet build",
"type": "shell",
"group": "build",
"presentation": {
"reveal": "silent"
},
"problemMatcher": "$msCompile"
}
]
}
My directory structure:
But when I click the "Start Debugging" button I get the following error:
launch: program " does not exist
There is a great article on GitHub regarding the subject - Windows Subsystem for Linux.
To cut a long story short, you need to first verify your version after the Windows 10 Creators Update:
~$ cat /etc/os-release | grep -i version
VERSION="16.04.2 LTS (Xenial Xerus)"
VERSION_ID="16.04"
VERSION_CODENAME=xenial
Notice the following:
If you had upgraded to Windows Creators update and already had WSL
installed, you might still have Ubuntu 14 in the WSL. If the version
is 14, run the following commands in a cmd prompt to reinstall and
update WSL.
lxrun /uninstall /full
lxrun /install
Download the debugger:
sudo apt-get install unzip
curl -sSL https://aka.ms/getvsdbgsh | bash /dev/stdin -v latest -l ~/vsdbg
The debugger will be installed at ~/vsdbg/vsdbg. It is the debuggerPath.
Sample launch.json configuration for launch:
{
"name": ".NET Core WSL Launch",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "publish",
"program": "/mnt/c/temp/dotnetapps/wslApp/bin/publish/wslApp.dll",
"args": [],
"cwd": "/mnt/c/temp/dotnetapps/wslApp",
"stopAtEntry": false,
"console": "internalConsole",
"pipeTransport": {
"pipeCwd": "${workspaceRoot}",
"pipeProgram": "bash.exe",
"pipeArgs": [ "-c" ],
"debuggerPath": "~/vsdbg/vsdbg"
}
}
Please note:
/.vscode/launch.json: This provides an array of different configurations you can use to launch your application. There is a drop down in the Debug view for selecting which configuration is active.
<your-open-folder>/.vscode/tasks.json: This provides an array of different tasks, like building your application, that you can execute. Debug configurations can link to one of these tasks through the preLaunchTask property.
Sample 'publish' task for tasks.json (needed for launching):
{
"version": "2.0.0",
"tasks": [
...,
{
"label": "publish",
"command": "dotnet",
"type": "process",
"args": [
"publish",
"${workspaceFolder}/wslApp.csproj",
"-o",
"${workspaceFolder}/bin/publish"
]
}
]
}
Please Note:
preLaunchTask executes dotnet publish, which builds the project on Windows. Since coreclr is cross-platform, the binary can be executed on WSL without any extra work.
pipeProgram is set to bash.exe.
debuggerPath points to vsdbg, the coreclr debugger.
This will not support programs that want to read from the console.
Sample launch.json configuration for attach:
{
"name": ".NET Core WSL Attach",
"type": "coreclr",
"request": "attach",
"processId": "${command:pickRemoteProcess}",
"pipeTransport": {
"pipeCwd": "${workspaceRoot}",
"pipeProgram": "bash.exe",
"pipeArgs": [ "-c" ],
"debuggerPath": "~/vsdbg/vsdbg",
"quoteArgs": true
}
}
Please Note:
"processId": "${command:pickRemoteProcess}" lists the processes running on WSL using the pipe program.
quoteArgs will quote any arguments and debugger commands with spaces if set to true.
Use sourceFileMap to map sources if they are available in a
different location than where they were built. If you build your
project in Linux, make sure to add a map from the /mnt drive
letters. Example: "sourceFileMap": { "/mnt/c/": "c:\\" }
File and paths are case sensitive in Linux.

Release build in VS Code

When building my C# project, how can I switch to Release configuration in VS Code?
Right now I launch my application with Ctrl+F5 or Debug -> Start Without Debugging which also builds it, but this creates only a debug build at bin/Debug. There is no Build menu in VS Code.
Here's my tasks.json:
{
"version": "2.0.0",
"tasks": [
{
"taskName": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/dotnetcore-test.csproj"
],
"problemMatcher": "$msCompile"
}
]
}
edit the task.json like this:
{
"version": "2.0.0",
"tasks": [
{
"taskName": "build Debug",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/dotnetcore-test.csproj"
],
"problemMatcher": "$msCompile"
},
{
"taskName": "build Release",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/dotnetcore-test.csproj",
"-c",
"Release"
],
"problemMatcher": "$msCompile"
}
]
}
then when you press Ctrl+Shift+B the Command Palette will let you choose between Build Release and Build Debug
source:
https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-build?tabs=netcore2x
You can perform a release build via the terminal with:
dotnet build -c release
If you want to run in release, use:
dotnet run -c release
Source: https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-build
you can just do that without tasks.json , but with Launch.json
Here is a configuration I always use to build my C# projects by F5 :
Gist
Terminal -> Run Task... -> select the task name to run

VS Code: debug project .NET Core project that resides in a subfolder

After moving my .NET Core project into a subfolder in the solution I am no longer able to Debug it using VS Code.
If I move tasks.json and launch.json into the project folder and open VS Code there I am able to debug, but I would like to be able to open the entire solution in VS Code.
The error message I am getting is this:
MSBUILD : error MSB1003: Specify a project or solution file. The current working directory does not contain a project or solution file.
The error seems to state that my task runner can no longer find my project, so I tried adding this to tasks.json:
{
"version": "0.1.0",
"command": "dotnet",
"isShellCommand": true,
"args": [],
"tasks": [
{
"taskName": "build",
"args": [ ],
"isBuildCommand": true,
"showOutput": "silent",
"problemMatcher": "$msCompile"
}
],
// My addition
"options": {
"cwd": "${workspaceRoot}/myProjectFolder"
}
}
Doing this I am able to start the task, but I then get the following error:
Exception thrown: 'System.IO.FileNotFoundException' in
Microsoft.Extensions.Configuration.FileExtensions.dll
How do I configure the .NET Core debugger to be able to run a project that resides in a sub folder?
The problem is your tasks.json, you should copy your subfolder .vscode/tasks.json in the parent directory.
When you create your tasks.json from the parent directory, it make a tasks.json like as your tasks.json example.
But if you copy the tasks.json created in the subfolder It should be some like this:
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/<change to your subfolder>/<your project>.csproj"
],
"problemMatcher": "$msCompile"
}
]
}
So there is the .csproj file, it say in the error.
The reason for the FileNotFoundException pointed by mrtedweb, the program not finding the appsettings.json can be solved without changing the program code.
Just edit launch.json, modifying the cwd configuration to point to the project subfolder instead of the root workspace which is the default, from
cwd: ${workspaceRoot} to, ${workspaceRoot}/TheProject.
// C:\Solution\.vscode\launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Launch (console)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceRoot}/TheProject/bin/Debug/netcoreapp1.1/TheProject.dll",
"args": [],
"cwd": "${workspaceRoot}/TheProject",
"stopAtEntry": false,
"console": "internalConsole",
"env": {
"ASPNETCORE_ENVIRONMENT": "Development",
"ASPNETCORE_URLS": "http://localhost:5050"
}
}
]
}
With this launch.json, it's possible to run the application from VS Code opening it from a root solution folder with the project inside a subfolder.
C:\Solution>code .
And it can also be run from the commandline without changing the paths in the program from the project subfolder.
C:\Solution\TheProject>dotnet run TheProject.csproj
It can no longer locate the appsettings.json file since the project was moved to a subfolder. This can be easily fixed in the StartUp.cs file by adding the following variable to the constructor:
var filePath = Path.Combine(env.ContentRootPath, "<project folder name>", "appsettings.json");
Note: You will need to specify the value for <project folder name>.
Then update the line that reads
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
to read:
.AddJsonFile(filePath, optional: false, reloadOnChange: true)
Also, verify the launch.json file is targeting the proper program file.
"program": "${workspaceRoot}/<project name>/bin/Debug/netcoreapp1.1/<program name>.dll"
Note: You will need to specify the values for <project name>, and <program name>.

Could not find the preLaunch task 'build'

To configure Visual Studio Code to debug C# scripts on OSX, I followed through all the steps listed in the article below:
Debugging C# on OSX with Visual Studio Code
When I tried to debug the sample C# script, Visual Studio Code reported this error:
Could not find the preLaunch task 'build'
As a consequence, I could not inspect the variables defined in the script.
This is a copy of the launch.json file:
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch console application",
"type": "mono",
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceRoot}/Program.exe",
"args": [],
"cwd": "${workspaceRoot}",
"stopAtEntry": false
}
]
}
This is a copy of the tasks.json file:
{
"version": "0.1.0",
"command": "mcs",
"args": [
"-debug",
"Program.cs"
],
"showOutput": "silent",
"taskSelector": "/t:",
"tasks": [
{
"taskName": "exe",
"isBuildCommand": true,
"problemMatcher": "$msCompile"
}
]
}
How do I resolve this?
You can use the Visual Studio Code to solve it.
When you see the error message, click on the steps below
Configure Task
Create tasks.json file from template
NET Core Executes .NET Core build commands
The VSCode will create a file like it:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet build",
"type": "shell",
"group": "build",
"presentation": {
"reveal": "silent"
},
"problemMatcher": "$msCompile"
}
]
}
It's finished. The VSCode will build the project before run.
The error occurs because Visual Studio Code cannot find any task in the tasks.json with the taskName value set to 'build'.
The preLaunchTask property of the launch.json file defines the task that should be executed before the script is launched. From the question, Visual Studio Code has been configured to run the task build before launching the script:
preLaunchTask: 'build'
But there's no task named 'build' in the tasks.json file.
To fix this, you should change the value of the preLaunchTask property to 'exe', which is the build task that has been defined in the tasks.json file.
It seems like this will be different for every scenario.
For me what #Jeferson Tenorio worked, but it needed a few more steps so let's add them:
Click on Configure Task:
Create tasks.json file from template
.NET Core Executes .NET Core build commands
Go to your launch.json file, and under configurations/program you will find this:
${workspaceFolder}/bin/Debug/<insert-target-framework-here>/<insert-project-name-here>.dll
Simply replace <insert-target-framework-here> and <insert-project-name-here> with your target framework, in my case that would be netcoreapp2.0 and then your project name (if you haven't changed anything your project name should be the same as the folder where you created your project), it should look something like this:
"program": "${workspaceFolder}/bin/Debug/netcoreapp2.0/MyProject.dll"
I hope this helps.
Visual Studio Code: Could not find the preLaunchTask 'build'?
As suggested in the above answer, you need to have the launch items defined in launch.json file and not in blah.code-workspace file.
The latter doesn't read tasks defined in tasks.json but only those defined in the same .code-workspace file.
Bug reports:
https://github.com/microsoft/vscode/issues/136187
https://github.com/microsoft/vscode/issues/93149
On Linux, to get the build command to work, I needed to change the tasks.json file from:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet build",
"type": "shell",
"args": [
// Ask dotnet build to generate full paths for file names.
"/property:GenerateFullPaths=true",
// Do not generate summary otherwise it leads to duplicate errors in Problems panel
"/consoleloggerparameters:NoSummary"
],
"group": "build",
"presentation": {
"reveal": "silent"
},
"problemMatcher": "$msCompile"
}
]
}
to:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "shell",
"args": [
"build"
// Ask dotnet build to generate full paths for file names.
"/property:GenerateFullPaths=true",
// Do not generate summary otherwise it leads to duplicate errors in Problems panel
"/consoleloggerparameters:NoSummary"
],
"group": "build",
"presentation": {
"reveal": "silent"
},
"problemMatcher": "$msCompile"
}
]
}
the reason for this is the fact that Linux will treat the task generated by VSC as running command "dotnet build" instead of "dotnet" with the parameter of "build". Without the change you will receive "dotnet build: command not found" with exit code 127
For Ubuntu Linux 20.04 LTS (but may well be the same on other OS's) what got preLaunchTask working for me, was using both a local tasks.json and launch.json
So my folder structure (pre-build) is:
.vscode/launch.json
.vscode/tasks.json
dist
src/index.ts
package.json
tsconfig.json
My launch.json contains:
{
"configurations": [
{
"type": "node",
"request": "launch",
"name": "TS preLaunchTask-build",
"program": "${file}",
"preLaunchTask": "tsc: build",
"outFiles": ["${workspaceFolder}/dist/**/*.js"],
"skipFiles": [
"<node_internals>/**", "node_modules",
]
},
]
}
My tasks.json contains:
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"command": "echo hello yes working!",
"problemMatcher": [],
"label": "myTask"
},
{
"type": "typescript",
"tsconfig": "tsconfig.json",
"problemMatcher": ["$tsc"],
"group": "build",
"label": "tsc: build"
},
]
}
And my tsconfig.json contains:
{
"compilerOptions": {
"outDir": "./dist",
"sourceMap": true,
"target": "es5",
"module": "commonjs"
},
"include": [
"src/**/*"
]
}
Usage: whilst within the index.ts just hit F5 with a breakpoint set
I have also included another task, "myTask" you can change the preLaunchTask line in your launch.json to: "preLaunchTask": "myTask", (where it will output some text to console to verify preLaunchTask is now working)
That way, if you still have issues, you can see if the issue is in your tsconfig setup, or if it's a preTaskLaunch setup issue.
(I would have thought it should have resolved this itself, but apparently not at the current time of writing anyway - but does force the advantage (for me) of committing debug config to the repo on project basis rather than global config)

Categories