markjprice / cs10dotnet6

Repository for the Packt Publishing book titled "C# 10 and .NET 6 - Modern Cross-Platform Development" by Mark J. Price
853 stars 373 forks source link

Not Grasping: Visual Studio Code runs the console app in project folder? #35

Open majew7 opened 2 years ago

majew7 commented 2 years ago

Hello Mark,

I just finished the Chapter 4 sub-section "Logging during development and runtime". So far the book is great, and I'm learning about all the new features in .NET-land since I've been away from .NET for some years.

Using Visual Studio 2022 on Mac, I set the appsettings.json file to copy to output directory: Copy If Newer. And during execution, the debugger allows me to confirm reading from appsettings.json as seen in variable TraceSwitch ts. Success!

However using Visual Studio Code for Mac, I did not have the same success. Regarding appsettings.json location, you had written:

"This is necessary because unlike Visual Studio Code, which runs the console app in the project folder, Visual Studio runs the console app in Instrumenting\bin\Debug\net6.0 or Instrumenting\bin\Release\net6.0."

I'm confused by this quote, because all my attempts in Visual Studio Code to confirm the reading of appsettings.json with ts suggest that this JSON file is not being read, because it's not found. Also, here is my launch.json snippet:

        {
            "name": ".NET Core Launch (console) - Instrumenting",
            "type": "coreclr",
            "request": "launch",
            "preLaunchTask": "build",
            "program": "${workspaceFolder}/Instrumenting/bin/Debug/net6.0/Instrumenting.dll",
            "args": [],
            "cwd": "${workspaceFolder}",
            "stopAtEntry": false,
            "console": "internalConsole"
        },

Q1) From your quote, can you help me understand what you mean when you write that, Visual Studio Code runs the console app in the project folder? So far I cannot confirm that this is the case, rather the console app runs from /bin/*/* is what I observe.

Q2) In Visual Studio Code how do we also copy appsettings.json to output directory upon build?

Thanks!

markjprice commented 2 years ago

At the bottom of page 159, in step 6, before the statement that sets the base directory, and a statement to output the current directory so that you can see where it is looking for appsettings.json, as shown in the following code:

Console.WriteLine(Directory.GetCurrentDirectory());

builder.SetBasePath(Directory.GetCurrentDirectory())
  .AddJsonFile("appsettings.json", 
   optional: true, reloadOnChange: true);

You should see that when running the console app using dotnet run, the current directory is the project folder Instrumenting. But if you were to run the console app using Visual Studio 2022, the current directory is Instrumenting\bin\*\net6.0.

majew7 commented 2 years ago

Thanks for replying @markjprice.

Scenario A: Command Line Yes, I added the additional Console.WriteLine() and upon executing dotnet run I see the running directory as: .../code/Other/Chapter04/Instrumenting.

Scenario B: Visual Studio 2022 for Mac w Debugger And yes, running the same code in Visual Studio with the debugger, I see the running directory is .../code/Other/Chapter04/Instrumenting/bin/Debug/net6.0.

Confirmed for both scenarios.


But the scenario, that this sub-section is highlighting, is VSC w debugger.

Scenario C: Visual Studio Code w Debugger When I tried this third scenario, I oddly saw this as the current directory: .../code/Other/Chapter04.

So I want back to my launch.json and saw that cwd was set like this:

...
"program": "${workspaceFolder}/Instrumenting/bin/Debug/net6.0/Instrumenting.dll",
"cwd": "${workspaceFolder}",
...

I changed it to this:

...
"program": "${workspaceFolder}/Instrumenting/bin/Debug/net6.0/Instrumenting.dll",
"cwd": "${workspaceFolder}/Instrumenting",
...

and then finally I saw VSC Debugger read from appsettings.json and set ts correctly. I had previously added the launch.json new json object entry myself because upon selecting the Instrumenting as the active OmniSharp project, I did not see a pop-up warning message saying that required assets are missing, and thus could not click Yes to add them. I believe I didn't see it, because it did not pop up at all.

My .vscode folder is here .../code/Other/Chapter04/.vscode/launch.json.

Q3) Is my .vscode folder where you expect to have been created? Thanks!

markjprice commented 2 years ago

Each project normally has its own .vscode folder, so it should have been .../code/Other/Chapter04/Instrumenting/.vscode/launch.json. If you miss the popup, you should then be able to select the bell icon to reveal it again. But for the past couple of months the C# extension had a bug that could have caused it not to correctly create the .vscode folder: https://github.com/markjprice/cs10dotnet6/issues/11

majew7 commented 2 years ago

Thanks for your response @markjprice. Ok, no, you do not expect .vscode folder to be at the workspace-level.

On my machine, the Required Assets Popup won't appear, if the .vscode folder already exists at the workspace level. However, when I delete the .../code/Other/Chapter04/.vscode/ folder, and select the next active project (ie. OmniSharp: Select Project), then the popup will reappear.

I will keep playing with VS Code and the C# Extension over the next week. Currently my hunch is that the C# Extension is incorrectly creating .vscode folders at the workspace-level, rather than the project-level.

My C# extension is v1.24.0, which I've uninstalled and installed a few times now.