未验证 提交 abad5e19 编写于 作者: J Jason Malinowski 提交者: GitHub

Merge pull request #35449 from jasonmalinowski/fix-compilations-not-holding-the-right-syntax-trees

Fix having the wrong syntax trees after adding an .editorconfig 
......@@ -1141,7 +1141,7 @@ public SolutionState AddDocuments(ImmutableArray<DocumentInfo> documentInfos)
{
return AddDocumentsToMultipleProjects(documentInfos,
(documentInfo, project) => project.CreateDocument(documentInfo, project.ParseOptions),
(project, documents) => (project.AddDocuments(documents), new CompilationTranslationAction.AddDocumentsAction(documents)));
(oldProject, documents) => (oldProject.AddDocuments(documents), new CompilationTranslationAction.AddDocumentsAction(documents)));
}
/// <summary>
......@@ -1225,7 +1225,11 @@ public SolutionState AddAnalyzerConfigDocuments(ImmutableArray<DocumentInfo> doc
// attached to them, so we'll just replace all syntax trees in that case.
return AddDocumentsToMultipleProjects(documentInfos,
(documentInfo, project) => new AnalyzerConfigDocumentState(documentInfo, _solutionServices),
(projectState, documents) => (projectState.AddAnalyzerConfigDocuments(documents), new CompilationTranslationAction.ReplaceAllSyntaxTreesAction(projectState)));
(oldProject, documents) =>
{
var newProject = oldProject.AddAnalyzerConfigDocuments(documents);
return (newProject, new CompilationTranslationAction.ReplaceAllSyntaxTreesAction(newProject));
});
}
public SolutionState RemoveAnalyzerConfigDocument(DocumentId documentId)
......
......@@ -1624,6 +1624,111 @@ public void TestUpdateDocumentsOrderExceptions()
Assert.Throws<ArgumentException>(() => solution = solution.WithProjectDocumentsOrder(pid, ImmutableList.CreateRange(new[] { did3, did2, did1 })));
}
[Fact, Trait(Traits.Feature, Traits.Features.Workspace)]
public async Task TestAddingEditorConfigFileWithDiagnosticSeverity()
{
var solution = CreateSolution();
var projectId = ProjectId.CreateNewId();
var sourceDocumentId = DocumentId.CreateNewId(projectId);
solution = solution.AddProject(projectId, "Test", "Test.dll", LanguageNames.CSharp);
solution = solution.AddDocument(sourceDocumentId, "Test.cs", "// Hello, world!", filePath: @"Z:\Test.cs");
var originalSyntaxTree = await solution.GetDocument(sourceDocumentId).GetSyntaxTreeAsync();
var originalCompilation = await solution.GetProject(projectId).GetCompilationAsync();
var editorConfigDocumentId = DocumentId.CreateNewId(projectId);
solution = solution.AddAnalyzerConfigDocuments(ImmutableArray.Create(
DocumentInfo.Create(
editorConfigDocumentId,
".editorconfig",
filePath: @"Z:\.editorconfig",
loader: TextLoader.From(TextAndVersion.Create(SourceText.From("[*.cs]\r\n\r\ndotnet_diagnostic.CA1234.severity = error"), VersionStamp.Default)))));
var newSyntaxTree = await solution.GetDocument(sourceDocumentId).GetSyntaxTreeAsync();
var newCompilation = await solution.GetProject(projectId).GetCompilationAsync();
Assert.NotSame(originalSyntaxTree, newSyntaxTree);
Assert.NotSame(originalCompilation, newCompilation);
Assert.True(newCompilation.ContainsSyntaxTree(newSyntaxTree));
Assert.Single(newSyntaxTree.DiagnosticOptions);
Assert.Equal(ReportDiagnostic.Error, newSyntaxTree.DiagnosticOptions["CA1234"]);
}
[Fact, Trait(Traits.Feature, Traits.Features.Workspace)]
public async Task TestAddingAndRemovingEditorConfigFileWithDiagnosticSeverity()
{
var solution = CreateSolution();
var projectId = ProjectId.CreateNewId();
var sourceDocumentId = DocumentId.CreateNewId(projectId);
solution = solution.AddProject(projectId, "Test", "Test.dll", LanguageNames.CSharp);
solution = solution.AddDocument(sourceDocumentId, "Test.cs", "// Hello, world!", filePath: @"Z:\Test.cs");
var editorConfigDocumentId = DocumentId.CreateNewId(projectId);
solution = solution.AddAnalyzerConfigDocuments(ImmutableArray.Create(
DocumentInfo.Create(
editorConfigDocumentId,
".editorconfig",
filePath: @"Z:\.editorconfig",
loader: TextLoader.From(TextAndVersion.Create(SourceText.From("[*.cs]\r\n\r\ndotnet_diagnostic.CA1234.severity = error"), VersionStamp.Default)))));
var syntaxTreeAfterAddingEditorConfig = await solution.GetDocument(sourceDocumentId).GetSyntaxTreeAsync();
Assert.Single(syntaxTreeAfterAddingEditorConfig.DiagnosticOptions);
Assert.Equal(ReportDiagnostic.Error, syntaxTreeAfterAddingEditorConfig.DiagnosticOptions["CA1234"]);
solution = solution.RemoveAnalyzerConfigDocument(editorConfigDocumentId);
var syntaxTreeAfterRemovingEditorConfig = await solution.GetDocument(sourceDocumentId).GetSyntaxTreeAsync();
Assert.Empty(syntaxTreeAfterRemovingEditorConfig.DiagnosticOptions);
var finalCompilation = await solution.GetProject(projectId).GetCompilationAsync();
Assert.True(finalCompilation.ContainsSyntaxTree(syntaxTreeAfterRemovingEditorConfig));
}
[Fact, Trait(Traits.Feature, Traits.Features.Workspace)]
public async Task TestChangingAnEditorConfigFile()
{
var solution = CreateSolution();
var projectId = ProjectId.CreateNewId();
var sourceDocumentId = DocumentId.CreateNewId(projectId);
solution = solution.AddProject(projectId, "Test", "Test.dll", LanguageNames.CSharp);
solution = solution.AddDocument(sourceDocumentId, "Test.cs", "// Hello, world!", filePath: @"Z:\Test.cs");
var editorConfigDocumentId = DocumentId.CreateNewId(projectId);
solution = solution.AddAnalyzerConfigDocuments(ImmutableArray.Create(
DocumentInfo.Create(
editorConfigDocumentId,
".editorconfig",
filePath: @"Z:\.editorconfig",
loader: TextLoader.From(TextAndVersion.Create(SourceText.From("[*.cs]\r\n\r\ndotnet_diagnostic.CA1234.severity = error"), VersionStamp.Default)))));
var syntaxTreeBeforeEditorConfigChange = await solution.GetDocument(sourceDocumentId).GetSyntaxTreeAsync();
Assert.Single(syntaxTreeBeforeEditorConfigChange.DiagnosticOptions);
Assert.Equal(ReportDiagnostic.Error, syntaxTreeBeforeEditorConfigChange.DiagnosticOptions["CA1234"]);
solution = solution.WithAnalyzerConfigDocumentTextLoader(
editorConfigDocumentId,
TextLoader.From(TextAndVersion.Create(SourceText.From("[*.cs]\r\n\r\ndotnet_diagnostic.CA6789.severity = error"), VersionStamp.Default)),
PreservationMode.PreserveValue);
var syntaxTreeAfterEditorConfigChange = await solution.GetDocument(sourceDocumentId).GetSyntaxTreeAsync();
Assert.Single(syntaxTreeAfterEditorConfigChange.DiagnosticOptions);
Assert.Equal(ReportDiagnostic.Error, syntaxTreeAfterEditorConfigChange.DiagnosticOptions["CA6789"]);
var finalCompilation = await solution.GetProject(projectId).GetCompilationAsync();
Assert.True(finalCompilation.ContainsSyntaxTree(syntaxTreeAfterEditorConfigChange));
}
private static void GetMultipleProjects(
out Project csBrokenProject,
out Project vbNormalProject,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册