VS Code debugger cannot "step" for .NET Console App - c#

I'm using VS code to write a simple .NET console application. Here's a look at my app's entry point:
Program.cs:
using System;
namespace MyApp
{
class Program
{
public static void Main(string[] args)
{
Console.WriteLine("test");
Console.WriteLine("another test");
}
}
}
I set a breakpoint on the line that reads, Console.WriteLine("test"), and start VS Code's debugger. The debugger highlights the line I've selected once it reaches that point in execution. Great. Then I press the "step over" button.
Expected behavior: test will be written to the debug console and the debugger will advance to the next line of code.
Actual behavior: test is not written to the debug console. Execution stops immediately and the console reads: The program '[3803] MyApp.dll' has exited with code 0 (0x0).
I re-launch my simple application without changing anything. The debugger once again stops at Console.WriteLine("test"). This time I press F5 to continue running. Again, nothing is written to the console and I am shown that the app has exited with code 0.
I re-launch the app, again. When the debugger stops at Console.WriteLine("test"), I un-set my breakpoint and press F5. test is written to the console, followed by another test, and the application finishes (with code 0, of course). I've found that the debugger will break at a later breakpoint, but I must un-set each breakpoint as it is reached (and then continue running / press F5)... otherwise the application stops running and I'm shown the same, simple exited with code 0 message. Un-setting a breakpoint makes no difference for "stepping over" / "stepping into" - those commands do not seem to work at all.
For what it's worth, I'm working on a MacBook running macOS Big Sur on an Apple M1 chip (I have no idea if this is useful information). Here is more detailed information about my workspace configuration:
MyApp.soln:
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.808.8
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MyApp", "MyApp\MyApp.csproj", "{D12824DE-83AF-4C7B-B5C2-C6CB1B9554C6}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|anycpu = Debug|anycpu
Release|anycpu = Release|anycpu
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{D12824DE-83AF-4C7B-B5C2-C6CB1B9554C6}.Debug|anycpu.ActiveCfg = Debug|anycpu
{D12824DE-83AF-4C7B-B5C2-C6CB1B9554C6}.Debug|anycpu.Build.0 = Debug|anycpu
{D12824DE-83AF-4C7B-B5C2-C6CB1B9554C6}.Release|anycpu.ActiveCfg = Release|anycpu
{D12824DE-83AF-4C7B-B5C2-C6CB1B9554C6}.Release|anycpu.Build.0 = Release|anycpu
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {8A94F55D-F55A-450C-A412-10554B4B7799}
EndGlobalSection
EndGlobal
MyApp.csproj:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
</Project>
launch.json:
{
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Launch (console)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceFolder}/MyApp/bin/Debug/net5.0/MyApp.dll",
"args": [],
"cwd": "${workspaceFolder}/MyApp/bin/Debug/net5.0",
"stopAtEntry": false,
"console": "internalConsole"
}
]
}
tasks.json:
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/MyApp/MyApp.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "publish",
"command": "dotnet",
"type": "process",
"args": [
"publish",
"${workspaceFolder}/MyApp/MyApp.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "watch",
"command": "dotnet",
"type": "process",
"args": [
"watch",
"run",
"${workspaceFolder}/MyApp/MyApp.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
}
]
}
How can I get VS Code's debugger to work as expected?

I believe that the .NET 5.0 SDK is not fully compatible with the new Apple M1 chip. I followed the process below on two Macs: one with an Intel chip and the other with an M1 chip:
Install the .NET 5.0 SDK.
Run dotnet new console to create a new console application.
Create a file, .vscode/launch.json, open it, and click the "Add Configuration" button. Select .NET: Launch .NET Core Console App. Update all placeholder-text in the file according to the structure of your project. You'll also need to create a .vscode/tasks.json file like the one that I included in my original post.
Attempt to debug the project in VS Code.
Running and debugging the application with this launch configuration works as expected on my Intel-chip-Mac, but not on my M1-chip-Mac. Looks like I'll be working from my old Mac until an M1-compatible .NET SDK is released.

Related

Debug C# NativeAOT Lambda Function

I created a .NET 7.0 NativeAOT Lambda function from the AWS Templates. I haven't changed it. I installed the Mock Lambda Test Tool. I believe I have to use the Executable Assembly page to test my function? I can't figure out how to test it though. Do I need to set up docker and sam to test it locally? Or do I just build my C# and run the tool? It's just supposed to take a string and convert it to upper case. I haven't been able to find a tutorial on how to do this...
I tried running the Mock Lambda Test Tool 7.0. I was expecting to send my function a string and it return an uppercase string.
I'm running VS Code on Linux. Here's 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 (console)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
//"program": "${workspaceFolder}/DocGenerator/bin/Debug/net7.0/linux-x64/bootstrap.dll",
"program": "${env:HOME}/.dotnet/tools/dotnet-lambda-test-tool-7.0",
"args": ["--port 5050"],
"cwd": "${workspaceFolder}/DocGenerator",
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
"console": "internalConsole",
"stopAtEntry": false
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach"
}
]
}
This is my aws-lambda-tools-default.json:
{
"Information": [
"This file provides default values for the deployment wizard inside Visual Studio and the AWS Lambda commands added to the .NET Core CLI.",
"To learn more about the Lambda commands with the .NET Core CLI execute the following command at the command line in the project root directory.",
"dotnet lambda help",
"All the command line options for the Lambda command can be specified in this file."
],
"profile": "default",
"region": "us-east-1",
"configuration": "Release",
"function-runtime": "provided.al2",
"function-memory-size": 256,
"function-timeout": 30,
"function-handler": "bootstrap",
"msbuild-parameters": "--self-contained true"
}
Please check the "launchSettings.json" file under properties folder
change the "executablePath" Path and "workingDirectory" and try to run and debug the code,
it should work. code here
"executablePath": "%USERPROFILE%\.dotnet\tools\dotnet-lambda-test-tool-7.0.exe",
"commandLineArgs": "--port 5050",
"workingDirectory": ".\bin\$(Configuration)\net7.0",

Why is my program not debugging on vscode (c#) like it used to? I am being asked to attach to a process

[Disclaimer: very new to coding]
I am learning to code and have so far covered some basic tutorials and can produce simple programs. These all used to run fine.
For some reason I can't explain, I am now running into an error every time I am trying to "start debugging".
For example, when trying to debug the default "Hello World!" code (as below),
using System;
namespace specialitySelector
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}
A drop down list appears (as below) and the program does not run! I have no idea why this is doing this :/.
Image of drop down list that appears and what I see
In my launch.json file I have this:
{
"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 (console)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/bin/Debug/net5.0/specialitySelector.dll",
"args": [],
"cwd": "${workspaceFolder}",
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
"console": "externalTerminal",
"stopAtEntry": false
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach"
}
]
}
What am I doing wrong and how do I fix this? I have even tried uninstalling and re-installing vscode with no result.
Thanks.

Can a Visual Studio Code Task generate a file and automatically open it in the same window

I have a C# task that generates a file within my VSCode workspace folder.
I would like the file to open automatically after it has been generated. Currently the best I have been able to do is print the full name of the file so the user can Ctrl+Click on the link.
Environment setup:
Open VSCode in an empty folder.
Create a new C# console app (type dotnet new console in the terminal).
Update Program.cs:
namespace VSCodeOpenGeneratedFile
{
using System;
using System.IO;
class Program
{
static void Main(string[] args)
{
File.WriteAllText("test.txt", "Hello world!");
Console.WriteLine(Path.Combine(Directory.GetCurrentDirectory(), "test.txt"));
}
}
}
Add .vscode/tasks.json:
{
"version": "2.0.0",
"tasks": [
{
"label": "Generate File",
"command": "dotnet run",
"type": "shell",
"problemMatcher": []
}
]
}
Run the task:
Ctrl+Shift+P
Select Tasks: Run Task
Select Generate File
In the TERMINAL you should see the file name, which you can Ctrl+Click to open.
How do I have the task automatically open this file instead?
I found that you can accomplish this with the command line arguments for VS code. Using the -r or --reuse-window argument, you can have a file open in your current VS code window.
I was able to do this after creating a dated markdown file with this task:
{
"label": "new note",
"type": "shell",
"command": "set -lx t (date +%Y-%m-%d-%H-%M-%S);echo \"# $t\" > ${workspaceFolder}/notes/$t.md;code -r ${workspaceFolder}/notes/$t.md"
}
This is on Linux with the fish shell, but you should be able to use a similar command for Windows or other shells.

Visual Studio Code: How to configure 'dotnet' not to show warnings when compiling

OK. I am in .NET Core and using Visual Studio Code on a Mac machine. Now, whenever I run my C# project then Visual Studio Code shows this annoying message:
The preLaunch task 'build' terminated with exit code 1
It shows me the option to click on a button called "Show problems". When I click on this button, it only shows the warning messages.
Now, if it was errors then it's OK to see this message. But the fact that it shows this message every time there are warnings (which to me is OK). That is very annoying. Is there a way I can configure Visual Studio Code to not show these messages on things like warnings?
In order to disable warnings, you should add -nowarn to args in the tasks.json file:
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/MyProject.csproj",
"-nowarn" // here
],
"problemMatcher": "$msCompile"
}
]
}
It might be a problem with your task.json file. Check the arguments of the file and make sure the build is pointing at the right location:
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/MyProject.csproj"
],
"problemMatcher": "$msCompile"
}
]
}

Simple dotnetcore app not running in VS Code

I tried to follow the instructions here: https://learn.microsoft.com/en-us/aspnet/core/tutorials/web-api-vsc
I did:
mkdir todoapi
cd todoapi
dotnet new webapi
When I opened Startup.cs in VS Code, I didn't get any warn message asking me the following:
Select Yes to the Warn message "Required assets to build and debug are missing from 'TodoApi'. Add them?"
Select Restore to the Info message "There are unresolved dependencies".
What is wrong with my local setup?
I just want to run this program from withi VS code.
If I run in debug mode I get this error:
The preLaunchTask 'build' terminated with exit code 1.
In the powershell terminal the command 'dotnet' doesn't work either. Should it?
Update
I re-started VS COde and now it picks up the command in terminal:
dotnet
Now when I run I get this:
launch: launch.json must be configured. Change 'program' to the path
to the executable file that you would like to debug.
My launch.json has:
"configurations": [
{
"name": ".NET Core Launch (console)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceRoot}/bin/Debug/<target-framework>/<project-name.dll>",
"args": [],
"cwd": "${workspaceRoot}",
"stopAtEntry": false,
"console": "internalConsole"
},
{
"name": ".NET Core Launch (web)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceRoot}/bin/Debug/<target-framework>/<project-name.dll>",
"args": [],
"cwd": "${workspaceRoot}",
"stopAtEntry": false,
"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"
}
},
Try:
dotnet restore //restore dependencies
dotnet build //build project
dotnet run //run project

Categories

Resources