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

Merge pull request #35848 from jasonmalinowski/add-editorconfig-support-to-msbuildworkspace

Add support for .editorconfig files to MSBuildWorkspace
......@@ -9,6 +9,7 @@ internal static class ItemNames
public const string Compile = nameof(Compile);
public const string CscCommandLineArgs = nameof(CscCommandLineArgs);
public const string DocFileItem = nameof(DocFileItem);
public const string EditorConfigFiles = nameof(EditorConfigFiles);
public const string Import = nameof(Import);
public const string ProjectReference = nameof(ProjectReference);
public const string Reference = nameof(Reference);
......
......@@ -361,7 +361,8 @@ private Task<ProjectInfo> CreateProjectInfoAsync(ProjectFileInfo projectFileInfo
var documents = CreateDocumentInfos(projectFileInfo.Documents, projectId, commandLineArgs.Encoding);
var additionalDocuments = CreateDocumentInfos(projectFileInfo.AdditionalDocuments, projectId, commandLineArgs.Encoding);
CheckForDuplicateDocuments(documents, additionalDocuments, projectPath, projectId);
var analyzerConfigDocuments = CreateDocumentInfos(projectFileInfo.AnalyzerConfigDocuments, projectId, commandLineArgs.Encoding);
CheckForDuplicateDocuments(documents.Concat(additionalDocuments).Concat(analyzerConfigDocuments), projectPath, projectId);
var analyzerReferences = ResolveAnalyzerReferences(commandLineArgs);
......@@ -385,7 +386,8 @@ private Task<ProjectInfo> CreateProjectInfoAsync(ProjectFileInfo projectFileInfo
additionalDocuments: additionalDocuments,
isSubmission: false,
hostObjectType: null)
.WithDefaultNamespace(projectFileInfo.DefaultNamespace);
.WithDefaultNamespace(projectFileInfo.DefaultNamespace)
.WithAnalyzerConfigDocuments(analyzerConfigDocuments);
});
}
......@@ -473,10 +475,10 @@ private static void GetDocumentNameAndFolders(string logicalPath, out string nam
}
}
private void CheckForDuplicateDocuments(ImmutableArray<DocumentInfo> documents, ImmutableArray<DocumentInfo> additionalDocuments, string projectFilePath, ProjectId projectId)
private void CheckForDuplicateDocuments(ImmutableArray<DocumentInfo> documents, string projectFilePath, ProjectId projectId)
{
var paths = new HashSet<string>(PathUtilities.Comparer);
foreach (var doc in documents.Concat(additionalDocuments))
foreach (var doc in documents)
{
if (paths.Contains(doc.FilePath))
{
......
......@@ -20,6 +20,9 @@ public static IEnumerable<MSB.Framework.ITaskItem> GetAnalyzers(this MSB.Executi
public static IEnumerable<MSB.Framework.ITaskItem> GetDocuments(this MSB.Execution.ProjectInstance executedProject)
=> executedProject.GetItems(ItemNames.Compile);
public static IEnumerable<MSB.Framework.ITaskItem> GetEditorConfigFiles(this MSB.Execution.ProjectInstance executedProject)
=> executedProject.GetItems(ItemNames.EditorConfigFiles);
public static IEnumerable<MSB.Framework.ITaskItem> GetMetadataReferences(this MSB.Execution.ProjectInstance executedProject)
=> executedProject.GetItems(ItemNames.ReferencePath);
......
......@@ -138,7 +138,11 @@ private ProjectFileInfo CreateProjectFileInfo(MSB.Execution.ProjectInstance proj
.ToImmutableArray();
var additionalDocs = project.GetAdditionalFiles()
.Select(MakeAdditionalDocumentFileInfo)
.Select(MakeNonSourceFileDocumentFileInfo)
.ToImmutableArray();
var analyzerConfigDocs = project.GetEditorConfigFiles()
.Select(MakeNonSourceFileDocumentFileInfo)
.ToImmutableArray();
return ProjectFileInfo.Create(
......@@ -151,6 +155,7 @@ private ProjectFileInfo CreateProjectFileInfo(MSB.Execution.ProjectInstance proj
commandLineArgs,
docs,
additionalDocs,
analyzerConfigDocs,
project.GetProjectReferences().ToImmutableArray(),
Log);
}
......@@ -187,7 +192,7 @@ private DocumentFileInfo MakeDocumentFileInfo(MSB.Framework.ITaskItem documentIt
return new DocumentFileInfo(filePath, logicalPath, isLinked, isGenerated, sourceCodeKind);
}
private DocumentFileInfo MakeAdditionalDocumentFileInfo(MSB.Framework.ITaskItem documentItem)
private DocumentFileInfo MakeNonSourceFileDocumentFileInfo(MSB.Framework.ITaskItem documentItem)
{
var filePath = GetDocumentFilePath(documentItem);
var logicalPath = GetDocumentLogicalPath(documentItem, _projectDirectory);
......
......@@ -69,6 +69,11 @@ internal sealed class ProjectFileInfo
/// </summary>
public ImmutableArray<DocumentFileInfo> AdditionalDocuments { get; }
/// <summary>
/// The analyzer config documents.
/// </summary>
public ImmutableArray<DocumentFileInfo> AnalyzerConfigDocuments { get; }
/// <summary>
/// References to other projects.
/// </summary>
......@@ -96,6 +101,7 @@ public override string ToString()
ImmutableArray<string> commandLineArgs,
ImmutableArray<DocumentFileInfo> documents,
ImmutableArray<DocumentFileInfo> additionalDocuments,
ImmutableArray<DocumentFileInfo> analyzerConfigDocuments,
ImmutableArray<ProjectFileReference> projectReferences,
DiagnosticLog log)
{
......@@ -111,6 +117,7 @@ public override string ToString()
this.CommandLineArgs = commandLineArgs;
this.Documents = documents;
this.AdditionalDocuments = additionalDocuments;
this.AnalyzerConfigDocuments = analyzerConfigDocuments;
this.ProjectReferences = projectReferences;
this.Log = log;
}
......@@ -125,6 +132,7 @@ public override string ToString()
ImmutableArray<string> commandLineArgs,
ImmutableArray<DocumentFileInfo> documents,
ImmutableArray<DocumentFileInfo> additionalDocuments,
ImmutableArray<DocumentFileInfo> analyzerConfigDocuments,
ImmutableArray<ProjectFileReference> projectReferences,
DiagnosticLog log)
=> new ProjectFileInfo(
......@@ -138,6 +146,7 @@ public override string ToString()
commandLineArgs,
documents,
additionalDocuments,
analyzerConfigDocuments,
projectReferences,
log);
......@@ -153,6 +162,7 @@ public static ProjectFileInfo CreateEmpty(string language, string filePath, Diag
commandLineArgs: ImmutableArray<string>.Empty,
documents: ImmutableArray<DocumentFileInfo>.Empty,
additionalDocuments: ImmutableArray<DocumentFileInfo>.Empty,
analyzerConfigDocuments: ImmutableArray<DocumentFileInfo>.Empty,
projectReferences: ImmutableArray<ProjectFileReference>.Empty,
log);
}
......
......@@ -3628,6 +3628,46 @@ public async Task TestDuplicateProjectReference()
}
}
[ConditionalFact(typeof(VisualStudioMSBuildInstalled), AlwaysSkip = "https://github.com/dotnet/core-eng/issues/6424"), Trait(Traits.Feature, Traits.Features.MSBuildWorkspace)]
public async Task TestEditorConfigDiscovery()
{
var files = GetSimpleCSharpSolutionFiles()
.WithFile(@"CSharpProject\CSharpProject.csproj", Resources.ProjectFiles.CSharp.WithDiscoverEditorConfigFiles)
.WithFile(".editorconfig", "root = true");
CreateFiles(files);
using (var workspace = CreateMSBuildWorkspace())
{
var projectFullPath = GetSolutionFileName(@"CSharpProject\CSharpProject.csproj");
var project = await workspace.OpenProjectAsync(projectFullPath);
var analyzerConfigDocument = Assert.Single(project.AnalyzerConfigDocuments);
Assert.Equal(".editorconfig", analyzerConfigDocument.Name);
var text = await analyzerConfigDocument.GetTextAsync();
Assert.Equal("root = true", text.ToString());
}
}
[ConditionalFact(typeof(VisualStudioMSBuildInstalled)), Trait(Traits.Feature, Traits.Features.MSBuildWorkspace)]
public async Task TestEditorConfigDiscoveryDisabled()
{
var files = GetSimpleCSharpSolutionFiles()
.WithFile(@"CSharpProject\CSharpProject.csproj", Resources.ProjectFiles.CSharp.WithDiscoverEditorConfigFiles)
.ReplaceFileElement(@"CSharpProject\CSharpProject.csproj", "DiscoverEditorConfigFiles", "false")
.WithFile(".editorconfig", "root = true");
CreateFiles(files);
using (var workspace = CreateMSBuildWorkspace())
{
var projectFullPath = GetSolutionFileName(@"CSharpProject\CSharpProject.csproj");
var project = await workspace.OpenProjectAsync(projectFullPath);
Assert.Empty(project.AnalyzerConfigDocuments);
}
}
private class InMemoryAssemblyLoader : IAnalyzerAssemblyLoader
{
public void AddDependencyLocation(string fullPath)
......
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. -->
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition="'$(Configuration)' == ''">Debug</Configuration>
<Platform Condition="'$(Platform)' == ''">AnyCPU</Platform>
<PlatformTarget>AnyCPU</PlatformTarget>
<ProjectGuid>{9705A8E6-C854-4FD3-8CEA-EDBDD54C7FD8}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>ConsoleApplication62</RootNamespace>
<AssemblyName>ConsoleApplication62</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<DiscoverEditorConfigFiles>true</DiscoverEditorConfigFiles>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|AnyCPU'">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|AnyCPU'">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.CSharp" />
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Data" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="System.Xml" />
<Reference Include="System.Xml.Linq" />
</ItemGroup>
<ItemGroup>
<Compile Include="CSharpClass.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
\ No newline at end of file
......@@ -149,6 +149,7 @@ public static class CSharp
public static string ReferencesPortableProject => GetText("ProjectFiles.CSharp.ReferencesPortableProject.csproj");
public static string Wildcards => GetText("ProjectFiles.CSharp.Wildcards.csproj");
public static string WithoutCSharpTargetsImported => GetText("ProjectFiles.CSharp.WithoutCSharpTargetsImported.csproj");
public static string WithDiscoverEditorConfigFiles => GetText("ProjectFiles.CSharp.WithDiscoverEditorConfigFiles.csproj");
public static string WithPrefer32Bit => GetText("ProjectFiles.CSharp.WithPrefer32Bit.csproj");
public static string WithLink => GetText("ProjectFiles.CSharp.WithLink.csproj");
public static string WithSystemNumerics => GetText("ProjectFiles.CSharp.WithSystemNumerics.csproj");
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册