While working on my .Net Core Series I noticed that Visual Studio was adding a global.json file to the root of my project, and liked to put src and test in there. I thought I would investigate what it actually does.

I noticed that if all my project folders were next to each other it didn't matter. So here is my experiment, and the results.

global.json

The default file I end up with in Visual Studio looks roughly like this:

{
  "projects": [ "src", "test" ],
  "sdk": {
    "version": "1.0.0-preview2-003121"
  }
}

Setup

To start with, I created a folder to work in:

mkdir globaltest
cd globaltest

This will be the root folder of my testing today.

Side-by-Side

For starters, I will create two folders side by side, inside a folder called src.

mkdir src
cd src

First a library:

mkdir mylib
cd mylib
dotnet new -t lib
dotnet restore
dotnet build

Next, a sibling app project:

cd ../
mkdir app1
cd app1
dotnet new

And to make sure they are linked, I add a project reference to the app's project.json file:

"mylib": {
    "target": "Project",
    "version": "*"
}

And test that it works

dotnet restore
dotnet build

And it does. Sibling project folders without a global.json file work successfully with project references.

Cousin projects

This time, I want to see what happens when there are two projects with an extra layer up between them. First back up to our root:

cd ../../

Now beside the original src folder we will create an other folder, and Inside it, a second app:

mkdir other
cd other
mkdir app2
cd app2
dotnet new

Again we add a project reference to the original lib project that is inside the src folder:

project.json

"mylib": {
    "target": "Project",
    "version": "*"
}

So now we have the following:

globaltest
|-- src
|    |-- mylib
|    |    |-- Library.cs
|    |    +-- project.json
|    +-- app1
|         |-- Program.cs
|         +-- project.json
+-- other
     +-- app2
          |-- Program.cs
          +-- project.json

For the moment of truth, we test if we can do a restore from app2:

dotnet restore

Fail.

error: Unable to resolve 'mylib' for '.NETCoreApp,Version=v1.0'.

Looks like we might need the global.json file for just this situation:

cd ../../
touch global.json

lets put the following in there:

{
  "projects": [ "src", "other" ],
  "sdk": {
    "version": "1.0.0-preview2-003121"
  }
}

And one more time now:

cd other/app2
dotnet restore
dotnet build

Success!

Conclusion

So why do when do we need a global.json? When our projects are spread out across different levels at different nesting. When can we do without? When the projects that need to reference each other are in folders right next to each other.

There are more complex scenarios I could have tried, but I'm happy to stop here. But please, keep testing to look for other cases that interest you, by all means.