Skip to content

Fix dotnet test crash on empty, invalid, or unreadable global.json#52386

Draft
Copilot wants to merge 6 commits intorelease/10.0.2xxfrom
copilot/fix-global-json-readability
Draft

Fix dotnet test crash on empty, invalid, or unreadable global.json#52386
Copilot wants to merge 6 commits intorelease/10.0.2xxfrom
copilot/fix-global-json-readability

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Jan 9, 2026

Fix dotnet test command failure when global.json is unreadable

  • Understand the codebase and issue
  • Add error handling in TestCommandDefinition.Create() for empty/invalid global.json
  • Add error handling for File.ReadAllText exceptions (catch all exceptions)
  • Add tests for empty, invalid, and unreadable global.json scenarios
  • Refactor tests to reduce code duplication
  • Run tests to validate the fix (32/32 tests pass)
  • Code review completed
  • Manual verification completed

Summary

Fixed crashes in dotnet test command when encountering:

  1. Empty global.json files
  2. Invalid JSON content in global.json
  3. Unreadable global.json files (any exception from File.ReadAllText)

All scenarios now default to VSTest, matching the behavior when no global.json exists. Uses broad exception handling for File.ReadAllText to catch all possible file access errors.

Original prompt

This section details on the original issue you should resolve

<issue_title>dotnet test command definition fails if global.json is unreadable</issue_title>
<issue_description>Create app and add empty global.json. Run dotnet --version.

System.TypeInitializationException: The type initializer for 'Microsoft.DotNet.Cli.Parser' threw an exception.
 ---> System.TypeInitializationException: The type initializer for 'Microsoft.DotNet.Cli.Commands.Test.TestCommandParser' threw an exception.
 ---> System.Text.Json.JsonException: The input does not contain any JSON tokens. Expected the input to start with a valid JSON token, when isFinalBlock is true. Path: $ | LineNumber: 0 | BytePositionInLine: 0.
 ---> System.Text.Json.JsonReaderException: The input does not contain any JSON tokens. Expected the input to start with a valid JSON token, when isFinalBlock is true. LineNumber: 0 | BytePositionInLine: 0.
   at System.Text.Json.ThrowHelper.ThrowJsonReaderException(Utf8JsonReader& json, ExceptionResource resource, Byte nextByte, ReadOnlySpan`1 bytes)
   at System.Text.Json.Utf8JsonReader.Read()
   at System.Text.Json.Serialization.JsonConverter`1.ReadCore(Utf8JsonReader& reader, T& value, JsonSerializerOptions options, ReadStack& state)
   --- End of inner exception stack trace ---
   at System.Text.Json.ThrowHelper.ReThrowWithPath(ReadStack& state, JsonReaderException ex)
   at System.Text.Json.Serialization.JsonConverter`1.ReadCore(Utf8JsonReader& reader, T& value, JsonSerializerOptions options, ReadStack& state)
   at System.Text.Json.Serialization.Metadata.JsonTypeInfo`1.Deserialize(Utf8JsonReader& reader, ReadStack& state)
   at System.Text.Json.JsonSerializer.ReadFromSpan[TValue](ReadOnlySpan`1 utf8Json, JsonTypeInfo`1 jsonTypeInfo, Nullable`1 actualByteCount)
   at System.Text.Json.JsonSerializer.ReadFromSpan[TValue](ReadOnlySpan`1 json, JsonTypeInfo`1 jsonTypeInfo)
   at System.Text.Json.JsonSerializer.Deserialize[TValue](String json, JsonSerializerOptions options)
   at Microsoft.DotNet.Cli.Commands.Test.TestCommandDefinition.Create() in E:\sdk2\src\Cli\dotnet\Commands\Test\TestCommandDefinition.cs:line 43
   at Microsoft.DotNet.Cli.Commands.Test.TestCommandParser.CreateCommand() in E:\sdk2\src\Cli\dotnet\Commands\Test\TestCommandParser.cs:line 20
   at Microsoft.DotNet.Cli.Commands.Test.TestCommandParser..cctor() in E:\sdk2\src\Cli\dotnet\Commands\Test\TestCommandParser.cs:line 11
   --- End of inner exception stack trace ---
   at Microsoft.DotNet.Cli.Commands.Test.TestCommandParser.GetCommand() in E:\sdk2\src\Cli\dotnet\Commands\Test\TestCommandParser.cs:line 15
   at Microsoft.DotNet.Cli.Parser..cctor() in E:\sdk2\src\Cli\dotnet\Parser.cs:line 59
   --- End of inner exception stack trace ---
   at Microsoft.DotNet.Cli.Parser.get_RootCommand() in E:\sdk2\src\Cli\dotnet\Parser.cs:line 241
   at Microsoft.DotNet.Cli.Parser.Parse(String[] args) in E:\sdk2\src\Cli\dotnet\Parser.cs:line 253
   at Microsoft.DotNet.Cli.Program.ProcessArgs(String[] args, TimeSpan startupTime) in E:\sdk2\src\Cli\dotnet\Program.cs:line 140
   at Microsoft.DotNet.Cli.Program.Main(String[] args) in E:\sdk2\src\Cli\dotnet\Program.cs:line 89
```</issue_description>

## Comments on the Issue (you are @copilot in this section)

<comments>
</comments>


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 2 commits January 9, 2026 22:35
Co-authored-by: tmat <41759+tmat@users.noreply.github.com>
Co-authored-by: tmat <41759+tmat@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix dotnet test command failure with unreadable global.json Fix dotnet test crash on empty or invalid global.json Jan 9, 2026
Copilot AI requested a review from tmat January 9, 2026 22:41
@@ -38,14 +38,23 @@ public static TestCommandDefinition Create()

string jsonText = File.ReadAllText(globalJsonPath);
Copy link
Copy Markdown
Member

@tmat tmat Jan 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot Also handle exception from File.ReadAllText

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot Also handle exception from File.ReadAllText

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added exception handling for File.ReadAllText to catch I/O errors and permission issues. Commit: 400138a

Copilot AI and others added 2 commits January 11, 2026 16:30
Co-authored-by: tmat <41759+tmat@users.noreply.github.com>
Co-authored-by: tmat <41759+tmat@users.noreply.github.com>
Copilot AI changed the title Fix dotnet test crash on empty or invalid global.json Fix dotnet test crash on empty, invalid, or unreadable global.json Jan 11, 2026
Copilot AI requested a review from tmat January 11, 2026 16:32
{
jsonText = File.ReadAllText(globalJsonPath);
}
catch (IOException)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot Catch all extensions here instead of explicit ones.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to catch all exceptions. Commit: 41f398a

Co-authored-by: tmat <41759+tmat@users.noreply.github.com>
Copilot AI requested a review from tmat January 11, 2026 19:17
@mmitche mmitche deleted the branch release/10.0.2xx January 17, 2026 00:38
@mmitche mmitche closed this Jan 17, 2026
@marcpopMSFT marcpopMSFT reopened this Jan 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants