65.9K
CodeProject is changing. Read more.
Home

How To Open, Build && Debug Multiple Projects in VS Code

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.91/5 (7 votes)

Jul 6, 2016

CPOL

3 min read

viewsIcon

34169

This article is going to describe the details of how to add multiple projects in VS Code, add dependencies between them and debug the main project.

Introduction

I have started working with ASP.NET Core, using Visual Studio Code( VS Code ), and the most surprising part was that everyone on the web said it doesn't support multiple projects in the same root folder and you ain't able to debug multiple projects and so on, I have searched and searched and searched, but at no success. however, these continuous searching makes me familiar with the structure of VS Code, and somehow I solved the problem. So I decided to share it with you guys. hope you enjoy.

Using the code

First of all, we need to make a root directory and applications directory, for our project, and adding our new dotnet core applications, so go to your desired path in Terminal( Command Prompt ) and follow the structure below : 

> mkdir mySampleRoot
> cd mySampleRoot
> mkdir FirstLibrary
> mkdir SecondProgram
> cd FirstLibrary
> dotnet new
> cd ..
> cd SecondProgram
> dotnet new
> cd ..
> code .

so far so good, too much scripts, from now on we can go to VS Code to maintain our projects. The last line of script from the box will run visual studio code, which should look similar to this one below : 

 

 

Well, there are two items in the .vscode folder that you should remove them, their names are :

  • browse.VC.db
  • browse.VC.db-wal

After that press Command(Ctrl in windows)+Shift+D to switch to debug window and press the setting icon to choose your environment, please select .NET Core, your screen will be as follows: 

After Launch.json file has been added

we need to make some changes to this file, we need to change program and cwd attributes, add /SecondProgram just after ${workspaceRoot}. It is worth to mention that the preLaunchTask will be run just before the current task, I will describe current task further, and that the arguments in the args attribute will be sent to the main method as arguments. After doing the changes the file should contain the following context:

 

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": ".NET Core Launch (console)",
            "type": "coreclr",
            "request": "launch",
            "preLaunchTask": "build",
            "program": "${workspaceRoot}/SecondProgram/bin/Debug/netcoreapp1.0/SecondProgram.dll",
            "args": [],
            "cwd": "${workspaceRoot}/SecondProgram",
            "stopAtEntry": false,
            "externalConsole": false
        }
    ]
}

 

 

 

Now, it's time to add the main task runner, task.json file, press Command(Ctrl)+Shift+P and type Configure Task Runner and press enter, then choose .NET Core. A task .json file will be added for you , change it to something like this : 

 

{
    "version": "0.1.0",
    "command": "dotnet",
    "isShellCommand": true,
    "args": [],
    "tasks": [
        {
            "taskName": "build_task",
            "suppressTaskName": true,
            "args": [
                "build",
                "SecondProgram"
            ],
            "isBuildCommand": true,
            "showOutput": "silent",
            "problemMatcher": "$msCompile"
        } , 
        {
            "taskName": "restore"
        } 
    ]
}

 

I have changed taskName and args, and I have also added suppressTaskName to the build_task and added a new task called restore. Well, it's time to say that current task mentioned above, here, can be one of the tasks defined in this task.json file, or any other task that you might optionally define, just like restore or build_task. The suppressTaskName set to true, ensures that the task name should not be sent as an argument to the dotnet command, if you set it to false then the task name will be sent as the only argument to the dotnet command, and the elements in the args attribute are for the dotnet command, not the main entry point of your application, so I could send build and project name, in this case, SecondProgram, as arguments for the dotnet shell command.

press Command(Ctrl)+P and type : task restore, then open the project.json file of the SecondProgram and just add a dependency to the FirstLibrary in its dependency part of the netcoreapp1.0, the file should contain texts like the following : 

 

{
  "version": "1.0.0-*",
  "buildOptions": {
    "debugType": "portable",
    "emitEntryPoint": true /* Set to false if you want a class library, or a project without entry point */
  },
  "dependencies": {},
  "frameworks": {
    "netcoreapp1.0": {
      "dependencies": {
        "Microsoft.NETCore.App": {
          "type": "platform",
          "version": "1.0.0"
        },
        "FirstLibrary": "1.0.0"
      },
      "imports": "dnxcore50"
    }
  }
}

 

 

** For the first application if you want to make it a class library, in its project.json file, change the emitEntryPoint to false.

 

** Note that the added dependency, FirstLibrary, should not contain type attribute.

 

Press Command(Ctrl)+P and type task build_task and press Enter, ignore any message and wait until all the projects have been built successfully, then press F5 and you are done.

 

** There is a switch project on the right-bottom of the Visual Studio Code( VS Code ), which is useful when you made any changes to the dependence project and you want the dependent project to become aware of those changes in the matter of IntelliSense.

 

I hope you guys find this article useful and it helps you on your journey with VS Code, I'm eager to hear about your experiences, please let me know if you have any problem.