Repository: dotnet/roslyn
File: src/Workspaces/SharedUtilitiesAndExtensions/Workspace/Core/Extensions/ISolutionExtensions.cs (C#)

Code before:
    /// <summary>
    /// Returns the <see cref="SourceGeneratedDocument"/> for the given <see cref="DocumentId"/> if it exists and has been generated.
    /// </summary>
    /// <remarks>
    /// This method is intended to be called on generated document that are "frozen", and hence there is a 100% guarantee that their content
    /// is available. If the document is not generated, or if it is not frozen, there is an inherent race condition that could cause this method
    /// to throw an exception at essentially random times.
    /// </remarks>
    public static SourceGeneratedDocument GetRequiredSourceGeneratedDocumentForAlreadyGeneratedId(this Solution solution, DocumentId documentId)
    {
        if (documentId is null)
            throw new ArgumentNullException(nameof(documentId));

        var project = solution.GetRequiredProject(documentId.ProjectId);
        var sourceGeneratedDocument = project.TryGetSourceGeneratedDocumentForAlreadyGeneratedId(documentId);
        if (sourceGeneratedDocument == null)
            throw CreateDocumentNotFoundException(documentId);

        return sourceGeneratedDocument;
    }

    public static ValueTask<Document> GetRequiredDocumentAsync(this Solution solution, DocumentId documentId, CancellationToken cancellationToken)
        => GetRequiredDocumentAsync(solution, documentId, includeSourceGenerated: false, cancellationToken);

    public static async ValueTask<Document> GetRequiredDocumentAsync(this Solution solution, DocumentId documentId, bool includeSourceGenerated, CancellationToken cancellationToken)
        => (await solution.GetDocumentAsync(documentId, includeSourceGenerated, cancellationToken).ConfigureAwait(false)) ?? throw CreateDocumentNotFoundException(documentId.DebugName);

    public static async ValueTask<TextDocument> GetRequiredTextDocumentAsync(this Solution solution, DocumentId documentId, CancellationToken cancellationToken = default)
        => (await solution.GetTextDocumentAsync(documentId, cancellationToken).ConfigureAwait(false)) ?? throw CreateDocumentNotFoundException(documentId);
#endif

    public static TextDocument GetRequiredAdditionalDocument(this Solution solution, DocumentId documentId)
        => solution.GetAdditionalDocument(documentId) ?? throw CreateDocumentNotFoundException(documentId);

    public static TextDocument GetRequiredAnalyzerConfigDocument(this Solution solution, DocumentId documentId)
        => solution.GetAnalyzerConfigDocument(documentId) ?? throw CreateDocumentNotFoundException(documentId);

    public static TextDocument GetRequiredTextDocument(this Solution solution, DocumentId documentId)
    {
        var document = solution.GetTextDocument(documentId);
        if (document != null)
            return document;

#if WORKSPACE
        if (documentId.IsSourceGenerated)
            throw new InvalidOperationException($"Use {nameof(GetRequiredTextDocumentAsync)} to get the {nameof(TextDocument)} for a `.{nameof(DocumentId.IsSourceGenerated)}=true` {nameof(DocumentId)}");
#endif

        throw CreateDocumentNotFoundException(documentId);
    }


Review comment (suggestion):
```suggestion
        => (await solution.GetDocumentAsync(documentId, includeSourceGenerated, cancellationToken).ConfigureAwait(false)) ?? throw CreateDocumentNotFoundException(documentId);
```

Code after:
    /// <summary>
    /// Returns the <see cref="SourceGeneratedDocument"/> for the given <see cref="DocumentId"/> if it exists and has been generated.
    /// </summary>
    /// <remarks>
    /// This method is intended to be called on generated document that are "frozen", and hence there is a 100% guarantee that their content
    /// is available. If the document is not generated, or if it is not frozen, there is an inherent race condition that could cause this method
    /// to throw an exception at essentially random times.
    /// </remarks>
    public static SourceGeneratedDocument GetRequiredSourceGeneratedDocumentForAlreadyGeneratedId(this Solution solution, DocumentId documentId)
    {
        if (documentId is null)
            throw new ArgumentNullException(nameof(documentId));

        var project = solution.GetRequiredProject(documentId.ProjectId);
        var sourceGeneratedDocument = project.TryGetSourceGeneratedDocumentForAlreadyGeneratedId(documentId);
        if (sourceGeneratedDocument == null)
            throw CreateDocumentNotFoundException(documentId);

        return sourceGeneratedDocument;
    }

    public static ValueTask<Document> GetRequiredDocumentAsync(this Solution solution, DocumentId documentId, CancellationToken cancellationToken)
        => GetRequiredDocumentAsync(solution, documentId, includeSourceGenerated: false, cancellationToken);

    public static async ValueTask<Document> GetRequiredDocumentAsync(this Solution solution, DocumentId documentId, bool includeSourceGenerated, CancellationToken cancellationToken)
        => (await solution.GetDocumentAsync(documentId, includeSourceGenerated, cancellationToken).ConfigureAwait(false)) ?? throw CreateDocumentNotFoundException(documentId);

    public static async ValueTask<TextDocument> GetRequiredTextDocumentAsync(this Solution solution, DocumentId documentId, CancellationToken cancellationToken = default)
        => (await solution.GetTextDocumentAsync(documentId, cancellationToken).ConfigureAwait(false)) ?? throw CreateDocumentNotFoundException(documentId);
#endif

    public static TextDocument GetRequiredAdditionalDocument(this Solution solution, DocumentId documentId)
        => solution.GetAdditionalDocument(documentId) ?? throw CreateDocumentNotFoundException(documentId);

    public static TextDocument GetRequiredAnalyzerConfigDocument(this Solution solution, DocumentId documentId)
        => solution.GetAnalyzerConfigDocument(documentId) ?? throw CreateDocumentNotFoundException(documentId);

    public static TextDocument GetRequiredTextDocument(this Solution solution, DocumentId documentId)
    {
        var document = solution.GetTextDocument(documentId);
        if (document != null)
            return document;

#if WORKSPACE
        if (documentId.IsSourceGenerated)
            throw new InvalidOperationException($"Use {nameof(GetRequiredTextDocumentAsync)} to get the {nameof(TextDocument)} for a `.{nameof(DocumentId.IsSourceGenerated)}=true` {nameof(DocumentId)}");
#endif

        throw CreateDocumentNotFoundException(documentId);
    }