Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
majew7 opened this issue Jan 30, 2022 · 4 comments
Open

Comments

@majew7
Copy link

majew7 commented Jan 30, 2022

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
Copy link
Owner

markjprice commented Jan 30, 2022

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
Copy link
Author

majew7 commented Jan 30, 2022

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
Copy link
Owner

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: #11

@majew7
Copy link
Author

majew7 commented Feb 2, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants