Daily Hack #day35 - dotnet global.json

Daily Hack #day35 - dotnet global.json

The global.json file is a configuration file used in .NET development to specify the version of the .NET SDK to use for building and running projects within a directory and its subdirectories. Here's a brief overview:

  1. Version Control: global.json is typically committed to version control alongside the project's source code. It ensures consistency in the development environment across different machines and team members by enforcing the use of a specific .NET SDK version.

  2. Directory Scope: Placed in the root directory of a project or solution, global.json affects the .NET SDK version used within that directory and its subdirectories. It allows projects within the same solution to use different .NET SDK versions if necessary.

  3. Syntax: The global.json file is a simple JSON file that contains a single property, sdk, specifying the desired .NET SDK version. For example:

{
  "sdk": {
    "version": "6.0.0"
  }
}

SDK commands include dotnet new and dotnet run. The .NET CLI must choose an SDK version for every dotnet command. It uses the latest SDK installed on the machine by default, even if:

  • The project targets an earlier version of the .NET runtime.
  • The latest version of the .NET SDK is a preview version.

You can take advantage of the latest SDK features and improvements while targeting earlier .NET runtime versions. You can target different runtime versions of .NET using the same SDK tools.

On rare occasions, you may need to use an earlier version of the SDK. You specify that version in a global.json file. The "use latest" policy means you only use global.json to specify a .NET SDK version earlier than the latest installed version.

The process for selecting an SDK version is:

  1. dotnet searches for a global.json file iteratively reverse-navigating the path upward from the current working directory.
  2. dotnet uses the SDK specified in the first global.json found.
  3. dotnet uses the latest installed SDK if no global.json is found.

Did you find this article valuable?

Support Cloud Tuned by becoming a sponsor. Any amount is appreciated!