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

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

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

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

    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.DebugName ?? "Unknown");
    }

    public static Exception CreateDocumentNotFoundException(string documentPath)
        => new InvalidOperationException(string.Format(WorkspaceExtensionsResources.The_solution_does_not_contain_the_specified_document, documentPath));

#if WORKSPACE
    public static Solution WithUpToDateSourceGeneratorDocuments(this Solution solution, IEnumerable<ProjectId> projectIds)
    {
        // If the solution is already in automatic mode, then SG documents are already always up to date.
        var configuration = solution.Services.GetRequiredService<IWorkspaceConfigurationService>().Options;
        if (configuration.SourceGeneratorExecution is SourceGeneratorExecutionPreference.Automatic)
            return solution;

        var projectIdToSourceGenerationVersion = ImmutableSortedDictionary.CreateBuilder<ProjectId, SourceGeneratorExecutionVersion>();

        foreach (var projectId in projectIds)
        {
            if (!projectIdToSourceGenerationVersion.ContainsKey(projectId))
            {
                var currentVersion = solution.GetSourceGeneratorExecutionVersion(projectId);
                projectIdToSourceGenerationVersion.Add(projectId, currentVersion.IncrementMinorVersion());
            }
        }

        return solution.UpdateSpecificSourceGeneratorExecutionVersions(
            new SourceGeneratorExecutionVersionMap(projectIdToSourceGenerationVersion.ToImmutable()));
    }
#endif


Review comment (bug):
@copilot this function should take `string? debugName` and should do `?? "Unknown"` here if it gets null.  Then all calllsites do not need to do that.

Code after:
    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);
    }

    public static Exception CreateDocumentNotFoundException(DocumentId? documentId)
#if WORKSPACE
        => CreateDocumentNotFoundException(documentId?.DebugName);
#else
        => CreateDocumentNotFoundException(documentId?.ToString());
#endif

    public static Exception CreateDocumentNotFoundException(string? debugName)
        => new InvalidOperationException(string.Format(WorkspaceExtensionsResources.The_solution_does_not_contain_the_specified_document, debugName ?? "Unknown"));

#if WORKSPACE
    public static Solution WithUpToDateSourceGeneratorDocuments(this Solution solution, IEnumerable<ProjectId> projectIds)
    {
        // If the solution is already in automatic mode, then SG documents are already always up to date.
        var configuration = solution.Services.GetRequiredService<IWorkspaceConfigurationService>().Options;
        if (configuration.SourceGeneratorExecution is SourceGeneratorExecutionPreference.Automatic)
            return solution;

        var projectIdToSourceGenerationVersion = ImmutableSortedDictionary.CreateBuilder<ProjectId, SourceGeneratorExecutionVersion>();

        foreach (var projectId in projectIds)
        {
            if (!projectIdToSourceGenerationVersion.ContainsKey(projectId))
            {
                var currentVersion = solution.GetSourceGeneratorExecutionVersion(projectId);
                projectIdToSourceGenerationVersion.Add(projectId, currentVersion.IncrementMinorVersion());
            }