Repository: dotnet/roslyn
File: src/LanguageServer/Microsoft.CodeAnalysis.LanguageServer.UnitTests/FileBasedProgramsWorkspaceTests.cs (C#)

Code before:

        // Get the document info once to kickoff the canonical project loading process
        _ = await GetRequiredLspWorkspaceAndDocumentAsync(nonFileUri, testLspServer).ConfigureAwait(false);
        await testLspServer.TestWorkspace.GetService<AsynchronousOperationListenerProvider>().GetWaiter(FeatureAttribute.Workspace).ExpeditedWaitAsync();

        // Verify the document is loaded in the canonical project.
        var (miscWorkspace, canonicalDocument) = await GetRequiredLspWorkspaceAndDocumentAsync(nonFileUri, testLspServer).ConfigureAwait(false);
        Assert.Equal(WorkspaceKind.MiscellaneousFiles, miscWorkspace.Kind);
        Assert.NotNull(canonicalDocument);
        // Should have the appropriate generated files now that we ran a design time build
        Assert.Contains(canonicalDocument.Project.Documents, d => d.Name == "Canonical.AssemblyInfo.cs");

        // File was saved to disk. Simulate this by opening the document under its new name and closing it under its old name.
        var fileUri = ProtocolConversions.CreateAbsoluteDocumentUri(@"C:\MyFile.cs");
        await testLspServer.OpenDocumentAsync(fileUri, """
            Console.WriteLine("Hello World");
            """).ConfigureAwait(false);

        await testLspServer.CloseDocumentAsync(nonFileUri).ConfigureAwait(false);

        // Issue a "textDocument/diagnostic" request for the closed document
        var diagnostics = await testLspServer.ExecuteRequestAsync<DocumentDiagnosticParams, SumType<FullDocumentDiagnosticReport, UnchangedDocumentDiagnosticReport>>(
                Methods.TextDocumentDiagnosticName,
                new DocumentDiagnosticParams() { TextDocument = new TextDocumentIdentifier { DocumentUri = nonFileUri } },
                CancellationToken.None).ConfigureAwait(false);
        Assert.Empty(((FullDocumentDiagnosticReport)diagnostics).Items);
    }
}


Review comment (suggestion):
There might be a more appropriate place for this test to live, happy to move it there if someone wants to point it out.

Code after:
            Console.WriteLine("Hello World");
            """, languageId: "csharp").ConfigureAwait(false);

        // Get the document info once to kickoff the canonical project loading process
        _ = await GetRequiredLspWorkspaceAndDocumentAsync(nonFileUri, testLspServer).ConfigureAwait(false);
        await testLspServer.TestWorkspace.GetService<AsynchronousOperationListenerProvider>().GetWaiter(FeatureAttribute.Workspace).ExpeditedWaitAsync();

        // Verify the document is loaded in the canonical project.
        var (miscWorkspace, canonicalDocument) = await GetRequiredLspWorkspaceAndDocumentAsync(nonFileUri, testLspServer).ConfigureAwait(false);
        Assert.Equal(WorkspaceKind.MiscellaneousFiles, miscWorkspace.Kind);
        Assert.NotNull(canonicalDocument);
        // Should have the appropriate generated files now that we ran a design time build
        Assert.Contains(canonicalDocument.Project.Documents, d => d.Name == "Canonical.AssemblyInfo.cs");

        // File was saved to disk. Simulate this by opening the document under its new name and closing it under its old name.
        var fileUri = ProtocolConversions.CreateAbsoluteDocumentUri(@"C:\MyFile.cs");
        await testLspServer.OpenDocumentAsync(fileUri, """
            Console.WriteLine("Hello World");
            """).ConfigureAwait(false);

        await testLspServer.CloseDocumentAsync(nonFileUri).ConfigureAwait(false);

        // Issue a "textDocument/diagnostic" request for the closed document
        var exception = await Assert.ThrowsAsync<RemoteInvocationException>(() =>
            testLspServer.ExecuteRequestAsync<DocumentDiagnosticParams, SumType<FullDocumentDiagnosticReport, UnchangedDocumentDiagnosticReport>>(
                Methods.TextDocumentDiagnosticName,
                new DocumentDiagnosticParams() { TextDocument = new TextDocumentIdentifier { DocumentUri = nonFileUri } },
                CancellationToken.None));
        Assert.Equal(RoslynLspErrorCodes.NonFatalRequestFailure, exception.ErrorCode);

        // Issue a "textDocument/hover" request for the closed document
        // At the time of authoring this test, the HoverHandler calls 'GetRequiredDocument'.
        // Demonstrate that instead of calling the handler (which would fail, due to the request not having any document),
        // we throw an exception with a known error code.
        exception = await Assert.ThrowsAsync<RemoteInvocationException>(() =>
            testLspServer.ExecuteRequestAsync<HoverParams, Hover>(
                Methods.TextDocumentHoverName,
                new HoverParams() { Position = new Position(0, 0), TextDocument = new TextDocumentIdentifier { DocumentUri = nonFileUri } },
                CancellationToken.None));
        Assert.Equal(RoslynLspErrorCodes.NonFatalRequestFailure, exception.ErrorCode);
    }
}