Skip to content

RecoverableText.RecoverAsync fails null check on non-Windows platforms #42178

@JoeRobich

Description

@JoeRobich

On Linux and MacOS platforms RecoverableText.RecoverAsync can fail a null check.

protected override async Task<SourceText> RecoverAsync(CancellationToken cancellationToken)
{
    Contract.ThrowIfNull(_storage);

    using (Logger.LogBlock(FunctionId.Workspace_Recoverable_RecoverTextAsync, _parent._filePath, cancellationToken))
    {
        return await _storage.ReadTextAsync(cancellationToken).ConfigureAwait(false);
    }
}
jorobich@jorobich-vm1:~/Source/format$ dotnet test
Test run for /home/jorobich/Source/format/artifacts/bin/dotnet-format.UnitTests/Debug/netcoreapp3.1/dotnet-format.UnitTests.dll(.NETCoreApp,Version=v3.1)
Microsoft (R) Test Execution Command Line Tool Version 16.3.0
Copyright (c) Microsoft Corporation.  All rights reserved.

Starting test execution, please wait...

A total of 1 test files matched the specified pattern.
[xUnit.net 00:00:05.88]     Microsoft.CodeAnalysis.Tools.Tests.CodeFormatterTests.NoFilesFormattedInFormattedProject [FAIL]                                                                                                                                                                                                                
  X Microsoft.CodeAnalysis.Tools.Tests.CodeFormatterTests.NoFilesFormattedInFormattedProject [182ms]                                                                                                                                                                                                                                       
  Error Message:
   System.AggregateException : One or more errors occurred. (Unexpected Null)
---- System.InvalidOperationException : Unexpected Null
  Stack Trace:
     at Microsoft.CodeAnalysis.Host.WeaklyCachedRecoverableValueSource`1.GetValueAsync(CancellationToken cancellationToken)
   at Microsoft.CodeAnalysis.RecoverableTextAndVersion.GetValueAsync(CancellationToken cancellationToken)
   at Microsoft.CodeAnalysis.TextDocumentState.GetTextAndVersionAsync(CancellationToken cancellationToken)
   at Microsoft.CodeAnalysis.TextDocumentState.GetTextAsync(CancellationToken cancellationToken)
   at Microsoft.CodeAnalysis.Tools.CodeFormatter.<>c__DisplayClass12_0.<<GetAnalyzerConfigSet>b__0>d.MoveNext() in /home/jorobich/Source/format/src/CodeFormatter.cs:line 382
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.CodeAnalysis.Tools.CodeFormatter.GetAnalyzerConfigSet(Project project, CancellationToken cancellationToken) in /home/jorobich/Source/format/src/CodeFormatter.cs:line 381
   at Microsoft.CodeAnalysis.Tools.CodeFormatter.GetAnalyzerConfigOptions(Document document, CancellationToken cancellationToken) in /home/jorobich/Source/format/src/CodeFormatter.cs:line 364
   at Microsoft.CodeAnalysis.Tools.CodeFormatter.GetDocumentAndOptions(Project project, DocumentId documentId, Matcher fileMatcher, CancellationToken cancellationToken) in /home/jorobich/Source/format/src/CodeFormatter.cs:line 328
   at Microsoft.CodeAnalysis.Tools.CodeFormatter.DetermineFormattableFiles(Solution solution, String projectPath, Matcher fileMatcher, ILogger logger, CancellationToken cancellationToken) in /home/jorobich/Source/format/src/CodeFormatter.cs:line 284
   at Microsoft.CodeAnalysis.Tools.CodeFormatter.FormatWorkspaceAsync(FormatOptions options, ILogger logger, CancellationToken cancellationToken) in /home/jorobich/Source/format/src/CodeFormatter.cs:line 65
   at Microsoft.CodeAnalysis.Tools.Tests.CodeFormatterTests.TestFormatWorkspaceAsync(String workspaceFilePath, IEnumerable`1 include, IEnumerable`1 exclude, Int32 expectedExitCode, Int32 expectedFilesFormatted, Int32 expectedFileCount) in /home/jorobich/Source/format/tests/CodeFormatterTests.cs:line 367
   at Microsoft.CodeAnalysis.Tools.Tests.CodeFormatterTests.NoFilesFormattedInFormattedProject() in /home/jorobich/Source/format/tests/CodeFormatterTests.cs:line 44
--- End of stack trace from previous location where exception was thrown ---
----- Inner Stack Trace -----
   at Roslyn.Utilities.Contract.Fail(String message)
   at Roslyn.Utilities.Contract.ThrowIfNull[T](T value, String message)
   at Microsoft.CodeAnalysis.RecoverableTextAndVersion.RecoverableText.RecoverAsync(CancellationToken cancellationToken)
                                                                                                                                                                                                                                                                                                                                           
Test Run Failed.
Total tests: 83
     Passed: 82
     Failed: 1
 Total time: 8.5459 Seconds

When debugging on Linux we see several PlatformNotSupportedExceptions are thrown, in particular, for System.IO.MemoryMappedFiles.
image

The TemporaryStorageService used by RecoverableText uses MemoryMappedFiles which explains this failure.

Metadata

Metadata

Labels

Area-IDEBugResolution-FixedThe bug has been fixed and/or the requested behavior has been implemented

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions