Skip to content

Fix Out of memory with source gen tests #88266

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 1, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions src/libraries/Common/tests/SourceGenerators/RoslynTestUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,23 @@ namespace SourceGenerators.Tests
{
internal static class RoslynTestUtils
{
/// <summary>
/// Creates a canonical Roslyn workspace for testing.
/// </summary>
public static AdhocWorkspace CreateTestWorkspace()
{
AdhocWorkspace workspace = new AdhocWorkspace();
workspace.AddSolution(SolutionInfo.Create(SolutionId.CreateNewId(), VersionStamp.Create()));
return workspace;
}

/// <summary>
/// Creates a canonical Roslyn project for testing.
/// </summary>
/// <param name="references">Assembly references to include in the project.</param>
/// <param name="includeBaseReferences">Whether to include references to the BCL assemblies.</param>
public static Project CreateTestProject(
AdhocWorkspace workspace,
IEnumerable<Assembly>? references,
bool includeBaseReferences = true,
LanguageVersion langVersion = LanguageVersion.Preview)
Expand All @@ -50,8 +61,8 @@ public static Project CreateTestProject(
}
}

return new AdhocWorkspace()
.AddSolution(SolutionInfo.Create(SolutionId.CreateNewId(), VersionStamp.Create()))
return workspace
.CurrentSolution
.AddProject("Test", "test.dll", "C#")
.WithMetadataReferences(refs)
.WithCompilationOptions(new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary).WithNullableContextOptions(NullableContextOptions.Enable))
Expand Down Expand Up @@ -156,7 +167,8 @@ public static TextSpan MakeSpan(string text, int spanNum)
LanguageVersion langVersion = LanguageVersion.Preview,
CancellationToken cancellationToken = default)
{
Project proj = CreateTestProject(references, includeBaseReferences, langVersion);
using var workspace = CreateTestWorkspace();
Project proj = CreateTestProject(workspace, references, includeBaseReferences, langVersion);
proj = proj.WithDocuments(sources);
Assert.True(proj.Solution.Workspace.TryApplyChanges(proj.Solution));
Compilation? comp = await proj!.GetCompilationAsync(CancellationToken.None).ConfigureAwait(false);
Expand Down Expand Up @@ -191,7 +203,8 @@ public static async Task<IList<Diagnostic>> RunAnalyzer(
IEnumerable<Assembly> references,
IEnumerable<string> sources)
{
Project proj = CreateTestProject(references);
using var workspace = CreateTestWorkspace();
Project proj = CreateTestProject(workspace, references);

proj = proj.WithDocuments(sources);

Expand All @@ -215,7 +228,8 @@ public static async Task<IList<string>> RunAnalyzerAndFixer(
string? defaultNamespace = null,
string? extraFile = null)
{
Project proj = CreateTestProject(references);
using var workspace = CreateTestWorkspace();
Copy link
Member

Choose a reason for hiding this comment

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

For my education, OOM's were triggered by AdhocWorkspace instances not being disposed?

Copy link
Member Author

Choose a reason for hiding this comment

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

that is right

Project proj = CreateTestProject(workspace, references);

int count = sources.Count();
proj = proj.WithDocuments(sources, sourceNames);
Expand Down