Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 853
severin Avatar asked Feb 05 '23 16:02

severin


2 Answers

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.

like image 126
Juan Pablo Avatar answered Feb 23 '23 01:02

Juan Pablo


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
like image 37
Germán Avatar answered Feb 23 '23 02:02

Germán