Skip to content

Commit

Permalink
Merge pull request #1772 from JoeRobich/use-roslyn-editorconfig
Browse files Browse the repository at this point in the history
Watch .editorconfig files in folders above the project folder.
  • Loading branch information
JoeRobich authored Apr 21, 2020
2 parents ff219be + 593f3ca commit e1ba268
Show file tree
Hide file tree
Showing 8 changed files with 149 additions and 2 deletions.
4 changes: 3 additions & 1 deletion build.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@
"ProjectWithDisabledAnalyzers2",
"ProjectWithAnalyzers",
"NetCore30Project",
"Net50Project"
"Net50Project",
"ProjectWithAnalyzersAndEditorConfig",
"ProjectWithParentEditorConfig"
],
"CakeTestAssets": [
"CakeProject"
Expand Down
17 changes: 17 additions & 0 deletions src/OmniSharp.MSBuild/ProjectManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
using OmniSharp.Utilities;
using System.Reflection;
using Microsoft.CodeAnalysis.Diagnostics;
using OmniSharp.Roslyn.EditorConfig;

namespace OmniSharp.MSBuild
{
Expand Down Expand Up @@ -379,10 +380,26 @@ private void WatchProjectFiles(ProjectFileInfo projectFileInfo)

if (_workspace.EditorConfigEnabled)
{
// Watch beneath the Project folder for changes to .editorconfig files.
_fileSystemWatcher.Watch(".editorconfig", (file, changeType) =>
{
QueueProjectUpdate(projectFileInfo.FilePath, allowAutoRestore: false, projectFileInfo.ProjectIdInfo);
});

// Watch in folders above the Project folder for changes to .editorconfig files.
var parentPath = Path.GetDirectoryName(projectFileInfo.FilePath);
while (parentPath != Path.GetPathRoot(parentPath))
{
if (!EditorConfigFinder.TryGetDirectoryPath(parentPath, out parentPath))
{
break;
}

_fileSystemWatcher.Watch(Path.Combine(parentPath, ".editorconfig"), (file, changeType) =>
{
QueueProjectUpdate(projectFileInfo.FilePath, allowAutoRestore: false, projectFileInfo.ProjectIdInfo);
});
}
}

if (projectFileInfo.RuleSet?.FilePath != null)
Expand Down
2 changes: 1 addition & 1 deletion src/OmniSharp.Roslyn/EditorConfig/EditorConfigFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public static ImmutableArray<string> GetEditorConfigPaths(string path)
return editorConfigPaths.ToImmutableArray();
}

private static bool TryGetDirectoryPath(string path, out string directoryPath)
public static bool TryGetDirectoryPath(string path, out string directoryPath)
{
try
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
root = true

[*.cs]
# IDE0005: Unnecessary using
dotnet_diagnostic.IDE0005.severity = error
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;

namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
<LangVersion>7.1</LangVersion>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26124.0
MinimumVisualStudioVersion = 15.0.26124.0
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Project", "Project\Project.csproj", "{ADE4E8E3-05F1-429F-966F-151F8E889485}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{ADE4E8E3-05F1-429F-966F-151F8E889485}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{ADE4E8E3-05F1-429F-966F-151F8E889485}.Debug|Any CPU.Build.0 = Debug|Any CPU
{ADE4E8E3-05F1-429F-966F-151F8E889485}.Debug|x64.ActiveCfg = Debug|Any CPU
{ADE4E8E3-05F1-429F-966F-151F8E889485}.Debug|x64.Build.0 = Debug|Any CPU
{ADE4E8E3-05F1-429F-966F-151F8E889485}.Debug|x86.ActiveCfg = Debug|Any CPU
{ADE4E8E3-05F1-429F-966F-151F8E889485}.Debug|x86.Build.0 = Debug|Any CPU
{ADE4E8E3-05F1-429F-966F-151F8E889485}.Release|Any CPU.ActiveCfg = Release|Any CPU
{ADE4E8E3-05F1-429F-966F-151F8E889485}.Release|Any CPU.Build.0 = Release|Any CPU
{ADE4E8E3-05F1-429F-966F-151F8E889485}.Release|x64.ActiveCfg = Release|Any CPU
{ADE4E8E3-05F1-429F-966F-151F8E889485}.Release|x64.Build.0 = Release|Any CPU
{ADE4E8E3-05F1-429F-966F-151F8E889485}.Release|x86.ActiveCfg = Release|Any CPU
{ADE4E8E3-05F1-429F-966F-151F8E889485}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
70 changes: 70 additions & 0 deletions tests/OmniSharp.MSBuild.Tests/ProjectWithAnalyzersTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,76 @@ public async Task WhenProjectEditorConfigIsChangedThenAnalyzerConfigurationUpdat
}
}

[Fact]
public async Task WhenProjectIsLoadedThenItContainsAnalyzerConfigurationFromParentFolder()
{
using (var testProject = await TestAssets.Instance.GetTestProjectAsync("ProjectWithParentEditorConfig"))
using (var host = CreateMSBuildTestHost(testProject.Directory, configurationData: TestHelpers.GetConfigurationDataWithAnalyzerConfig(roslynAnalyzersEnabled: true, editorConfigEnabled: true)))
{
var project = host.Workspace.CurrentSolution.Projects.Single();
var projectFolderPath = Path.GetDirectoryName(project.FilePath);
var projectParentFolderPath = Path.GetDirectoryName(projectFolderPath);

var analyzerConfigDocument = project.AnalyzerConfigDocuments.Single();
var editorConfigFolderPath = Path.GetDirectoryName(analyzerConfigDocument.FilePath);

Assert.Equal(projectParentFolderPath, editorConfigFolderPath);
}
}

[Fact]
public async Task WhenProjectIsLoadedThenItContainsAnalyzerConfigurationFromParentEditorConfig()
{
using (var testProject = await TestAssets.Instance.GetTestProjectAsync("ProjectWithParentEditorConfig"))
using (var host = CreateMSBuildTestHost(testProject.Directory, configurationData: TestHelpers.GetConfigurationDataWithAnalyzerConfig(roslynAnalyzersEnabled: true, editorConfigEnabled: true)))
{
var project = host.Workspace.CurrentSolution.Projects.Single();
var projectFolderPath = Path.GetDirectoryName(project.FilePath);

var diagnostics = await host.RequestCodeCheckAsync(Path.Combine(projectFolderPath, "Program.cs"));

Assert.NotEmpty(diagnostics.QuickFixes);

var quickFix = diagnostics.QuickFixes.OfType<DiagnosticLocation>().Single(x => x.Id == "IDE0005");
Assert.Equal("Error", quickFix.LogLevel);
}
}

[Fact]
public async Task WhenProjectParentEditorConfigIsChangedThenAnalyzerConfigurationUpdates()
{
var emitter = new ProjectLoadTestEventEmitter();

using (var testProject = await TestAssets.Instance.GetTestProjectAsync("ProjectWithParentEditorConfig"))
using (var host = CreateMSBuildTestHost(
testProject.Directory,
emitter.AsExportDescriptionProvider(LoggerFactory),
TestHelpers.GetConfigurationDataWithAnalyzerConfig(roslynAnalyzersEnabled: true, editorConfigEnabled: true)))
{
var initialProject = host.Workspace.CurrentSolution.Projects.Single();
var analyzerConfigDocument = initialProject.AnalyzerConfigDocuments.Single();

File.WriteAllText(analyzerConfigDocument.FilePath, @"
root = true
[*.cs]
# IDE0005: Unnecessary using
dotnet_diagnostic.IDE0005.severity = none
");

await NotifyFileChanged(host, analyzerConfigDocument.FilePath);

emitter.WaitForProjectUpdate();

var project = host.Workspace.CurrentSolution.Projects.Single();
var projectFolderPath = Path.GetDirectoryName(project.FilePath);
var diagnostics = await host.RequestCodeCheckAsync(Path.Combine(projectFolderPath, "Program.cs"));

Assert.NotEmpty(diagnostics.QuickFixes);
Assert.DoesNotContain(diagnostics.QuickFixes.OfType<DiagnosticLocation>(), x => x.Id == "IDE0005");
}
}

[Theory]
[InlineData("ProjectWithDisabledAnalyzers")]
[InlineData("ProjectWithDisabledAnalyzers2")]
Expand Down

0 comments on commit e1ba268

Please sign in to comment.