microsoft / vscode-dotnettools

This is a feedback repository to capture issues logged for the C# Dev Kit and related extensions from Visual Studio Code
Other
233 stars 13 forks source link

[BUG] Default C# targets don't execute in /bin/Debug/net8.0/ #1575

Open NogginBops opened 1 week ago

NogginBops commented 1 week ago

Describe the Issue

The default C# run targets don't correctly set the working directory of the spawned process to the bin/Debug/net8.0/ folder where the executable is located.

The causes problems when using relative paths to access files in the output directory, and it's the expected working directory when working with Visual Studio on windows.

It would be nice if it was possible to have a setting to get the Visual Studio behaviour instead of the current behaviour.

Steps To Reproduce

  1. Open a solution with C# projects.
  2. Create a run target using the following UI Image
  3. Run the project and print Directory.GetCurrentDirectory().

Expected Behavior

The same as VIsual Studio. The working directory should be bin/Debug/net8.0/.

Environment Information

Ubuntu 22.04 LTS C# devkit v1.12.37 (pre-release) C# (extension) v2.50.27 .NET Install Tool v2.2.2

> dotnet --info

.NET SDK:
 Version:           8.0.403
 Commit:            c64aa40a71
 Workload version:  8.0.400-manifests.18f19b92
 MSBuild version:   17.11.9+a69bbaaf5

Runtime Environment:
 OS Name:     ubuntu
 OS Version:  22.04
 OS Platform: Linux
 RID:         linux-x64
 Base Path:   /usr/share/dotnet/sdk/8.0.403/
WardenGnaw commented 1 week ago

@NogginBops I'm seeing that when starting debugging in Visual Studio, the default working directory is set to the directory of the csproj file. This is the same as what happens with VS Code debug launch.

If you want to switch the directory used, you can set workingDirectory in your launchSettings.json to the profile you are launching.

See https://code.visualstudio.com/docs/csharp/debugger-settings#_cwd

NogginBops commented 1 week ago

I'm seeing that when starting debugging in Visual Studio, the default working directory is set to the directory of the csproj file.

How are you getting this behaviour? I've been using Visual Studio for many years and in my mind it's common knowledge that the working directory when starting a project in VS is the directory where the final exe file is. And that and directories and files in your project file structure will not be accessible with relative paths unless you explicitly copy them to the output directory.

I can put together a small repro guide if needed but this has been the default behavior of VS for many years. At least in all the circles I've been in.

If you want to switch the directory used, you can set workingDirectory in your launchSettings.json to the profile you are launching.

The problem here is that there is no launch.json, I'm using the "built-in" run targets provided by the c# devkit and from what I can see the configuration of these targets are not part of any file in my project.

WardenGnaw commented 5 days ago

@NogginBops Ah, I made a mistake by testing a Web project instead of a Console project. For Console Projects it is the binary directory, for Web Project it sets it to the project directory.

The issue here is that the project system provides the directory that the debugger should start the target application in.


The suggestion below is a workaround for now as we work on resolving this issue.

If you want to switch the directory used, you can set workingDirectory in your launchSettings.json to the profile you are launching.

The problem here is that there is no launch.json, I'm using the "built-in" run targets provided by the c# devkit and from what I can see the configuration of these targets are not part of any file in my project.

Clarification here is the launchSettings.json not launch.json. This would be the file that exists under /Properties/launchSettings.json

With the contents as:

{
  "profiles": {
    "ConsoleApp": {
      "commandName": "Project",
      "workingDirectory": "C:\\src"
    }
  }
}

launchSettings.json is created/modified in VS if you add commandline arguments or modify the working directory.

NogginBops commented 5 days ago

Ok yeah I tried launchSettings.json and that seemed to work as a workaround.

Is there a way to define relative paths in the file? And is it possible to define different paths for Debug and Release?

LittleLittleCloud commented 1 day ago

You can also try override the OutDir property in your .csproj. This bug seems to be caused by a mixture use of forward/backward slash in OutDir.

  <PropertyGroup>
    <OutDir>bin/$(Configuration)/$(TargetFramework)</OutDir>
  </PropertyGroup>