Skip to content
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

Respect the disabling of a project system in services #1543

Merged
Merged
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Changelog
All changes to the project will be documented in this file.

## [1.33.1] - not released yet
* Fixed a bug where some internal services didn't respect the disabling of a project system ([#1543](https://github.com/OmniSharp/omnisharp-roslyn/pull/1543))

## [1.33.0] - 2019-07-01
* Added support for `.editorconfig` files to control formatting settings, analyzers, coding styles and naming conventions. The feature is currently opt-into and needs to be enabled using OmniSharp configuration ([#31](https://github.com/OmniSharp/omnisharp-roslyn/issues/31), PR: [#1526](https://github.com/OmniSharp/omnisharp-roslyn/pull/1526))
```JSON
Expand Down
3 changes: 3 additions & 0 deletions src/OmniSharp.Abstractions/Plugins/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public Plugin(ILogger<Plugin> logger, PluginConfig config)
public string Language { get; }
public IEnumerable<string> Extensions { get; }
public bool EnabledByDefault { get; } = true;
public bool Initialized { get; private set; }

public Task<TResponse> Handle<TRequest, TResponse>(string endpoint, TRequest request)
{
Expand Down Expand Up @@ -101,6 +102,8 @@ public void Dispose()

public void Initalize(IConfiguration configuration)
{
if (Initialized) return;
Initialized = true;
Task.Run(() => Run());
}

Expand Down
5 changes: 5 additions & 0 deletions src/OmniSharp.Abstractions/Services/IProjectSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ public interface IProjectSystem
IEnumerable<string> Extensions { get; }
bool EnabledByDefault { get; }

/// <summary>
/// Flag indicating that the project system has been sucessfully initialized.
/// </summary>
bool Initialized { get; }

/// <summary>
/// Initialize the project system.
/// </summary>
Expand Down
7 changes: 5 additions & 2 deletions src/OmniSharp.Cake/CakeProjectSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,12 @@ public class CakeProjectSystem : IProjectSystem
private readonly ILogger<CakeProjectSystem> _logger;
private readonly ConcurrentDictionary<string, ProjectInfo> _projects;
private readonly Lazy<CSharpCompilationOptions> _compilationOptions;

private CakeOptions _options;

public string Key { get; } = "Cake";
public string Language { get; } = Constants.LanguageNames.Cake;
public IEnumerable<string> Extensions { get; } = new[] { ".cake" };
public bool EnabledByDefault { get; } = true;
public bool Initialized { get; private set; }

[ImportingConstructor]
public CakeProjectSystem(
Expand Down Expand Up @@ -70,6 +69,8 @@ public CakeProjectSystem(

public void Initalize(IConfiguration configuration)
{
if (Initialized) return;

_options = new CakeOptions();
configuration.Bind(_options);

Expand Down Expand Up @@ -103,6 +104,8 @@ public void Initalize(IConfiguration configuration)

// Watch .cake files
_fileSystemWatcher.Watch(".cake", OnCakeFileChanged);

Initialized = true;
}

private void AddCakeFile(string cakeFilePath)
Expand Down
7 changes: 5 additions & 2 deletions src/OmniSharp.DotNet/DotNetProjectSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ namespace OmniSharp.DotNet
public class DotNetProjectSystem : IProjectSystem
{
private const string CompilationConfiguration = "Debug";

private readonly IOmniSharpEnvironment _environment;
private readonly OmniSharpWorkspace _workspace;
private readonly IDotNetCliService _dotNetCliService;
Expand All @@ -37,7 +36,6 @@ public class DotNetProjectSystem : IProjectSystem
private readonly IFileSystemWatcher _fileSystemWatcher;
private readonly ILogger _logger;
private readonly ProjectStatesCache _projectStates;

private DotNetWorkspace _workspaceContext;
private bool _enableRestorePackages;

Expand Down Expand Up @@ -66,6 +64,7 @@ public DotNetProjectSystem(
public string Language { get; } = LanguageNames.CSharp;
public IEnumerable<string> Extensions { get; } = new string[] { ".cs" };
public bool EnabledByDefault { get; } = false;
public bool Initialized { get; private set; }

Task<object> IProjectSystem.GetWorkspaceModelAsync(WorkspaceInformationRequest request)
{
Expand Down Expand Up @@ -103,6 +102,8 @@ Task<object> IProjectSystem.GetProjectModelAsync(string filePath)

public void Initalize(IConfiguration configuration)
{
if (Initialized) return;

_logger.LogInformation($"Initializing in {_environment.TargetDirectory}");

if (!bool.TryParse(configuration["enablePackageRestore"], out _enableRestorePackages))
Expand All @@ -115,6 +116,8 @@ public void Initalize(IConfiguration configuration)
_workspaceContext = new DotNetWorkspace(_environment.TargetDirectory);

Update(allowRestore: true);

Initialized = true;
}

public void Update(bool allowRestore)
Expand Down
2 changes: 1 addition & 1 deletion src/OmniSharp.Host/Endpoint/LanguagePredicateHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public LanguagePredicateHandler(IEnumerable<IProjectSystem> projectSystems)

public string GetLanguageForFilePath(string filePath)
{
foreach (var projectSystem in _projectSystems)
foreach (var projectSystem in _projectSystems.Where(project => project.Initialized))
{
if (projectSystem.Extensions.Any(extension => filePath.EndsWith(extension, StringComparison.OrdinalIgnoreCase)))
{
Expand Down
9 changes: 4 additions & 5 deletions src/OmniSharp.MSBuild/ProjectSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,16 @@ internal class ProjectSystem : IProjectSystem
private readonly ILogger _logger;
private readonly IAnalyzerAssemblyLoader _assemblyLoader;
private readonly ImmutableArray<IMSBuildEventSink> _eventSinks;
private readonly object _gate = new object();
private readonly Queue<ProjectFileInfo> _projectsToProcess;

private PackageDependencyChecker _packageDependencyChecker;
private ProjectManager _manager;
private ProjectLoader _loader;
private MSBuildOptions _options;
private string _solutionFileOrRootPath;

public string Key { get; } = "MsBuild";
public string Language { get; } = LanguageNames.CSharp;
public IEnumerable<string> Extensions { get; } = new[] { ".cs" };
public bool EnabledByDefault { get; } = true;
public bool Initialized { get; private set; }

[ImportingConstructor]
public ProjectSystem(
Expand Down Expand Up @@ -82,13 +79,14 @@ public ProjectSystem(
_fileSystemHelper = fileSystemHelper;
_loggerFactory = loggerFactory;
_eventSinks = eventSinks.ToImmutableArray();
_projectsToProcess = new Queue<ProjectFileInfo>();
_logger = loggerFactory.CreateLogger<ProjectSystem>();
_assemblyLoader = assemblyLoader;
}

public void Initalize(IConfiguration configuration)
{
if (Initialized) return;

_options = new MSBuildOptions();
ConfigurationBinder.Bind(configuration, _options);

Expand All @@ -105,6 +103,7 @@ public void Initalize(IConfiguration configuration)
_loader = new ProjectLoader(_options, _environment.TargetDirectory, _propertyOverrides, _loggerFactory, _sdksPathResolver);

_manager = new ProjectManager(_loggerFactory, _options, _eventEmitter, _fileSystemWatcher, _metadataFileReferenceCache, _packageDependencyChecker, _loader, _workspace, _assemblyLoader, _eventSinks);
Initialized = true;

if (_options.LoadProjectsOnDemand)
{
Expand Down
3 changes: 2 additions & 1 deletion src/OmniSharp.Roslyn/ProjectEventForwarder.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Composition;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using OmniSharp.Eventing;
Expand Down Expand Up @@ -85,7 +86,7 @@ private async Task<ProjectInformationResponse> GetProjectInformationAsync(string
{
var response = new ProjectInformationResponse();

foreach (var projectSystem in _projectSystems)
foreach (var projectSystem in _projectSystems.Where(project => project.Initialized))
{
var project = await projectSystem.GetProjectModelAsync(fileName);
if (project != null)
Expand Down
3 changes: 2 additions & 1 deletion src/OmniSharp.Roslyn/ProjectInformationService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Composition;
using System.Linq;
using System.Threading.Tasks;
using OmniSharp.Mef;
using OmniSharp.Models.ProjectInformation;
Expand All @@ -23,7 +24,7 @@ public async Task<ProjectInformationResponse> Handle(ProjectInformationRequest r
{
var response = new ProjectInformationResponse();

foreach (var projectSystem in _projectSystems)
foreach (var projectSystem in _projectSystems.Where(project => project.Initialized))
{
var project = await projectSystem.GetProjectModelAsync(request.FileName);
response.Add($"{projectSystem.Key}Project", project);
Expand Down
3 changes: 2 additions & 1 deletion src/OmniSharp.Roslyn/WorkspaceInformationService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Composition;
using System.Linq;
using System.Threading.Tasks;
using OmniSharp.Mef;
using OmniSharp.Models.WorkspaceInformation;
Expand All @@ -23,7 +24,7 @@ public async Task<WorkspaceInformationResponse> Handle(WorkspaceInformationReque
{
var response = new WorkspaceInformationResponse();

foreach (var projectSystem in _projectSystems)
foreach (var projectSystem in _projectSystems.Where(project => project.Initialized))
{
var workspaceModel = await projectSystem.GetWorkspaceModelAsync(request);
response.Add(projectSystem.Key, workspaceModel);
Expand Down
9 changes: 5 additions & 4 deletions src/OmniSharp.Script/ScriptProjectSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,13 @@ namespace OmniSharp.Script
public class ScriptProjectSystem : IProjectSystem
{
private const string CsxExtension = ".csx";

private readonly ConcurrentDictionary<string, ProjectInfo> _projects = new ConcurrentDictionary<string, ProjectInfo>();
private readonly ScriptContextProvider _scriptContextProvider;
private readonly OmniSharpWorkspace _workspace;
private readonly IOmniSharpEnvironment _env;
private readonly ILogger _logger;
private readonly IFileSystemWatcher _fileSystemWatcher;
private readonly ILoggerFactory _loggerFactory;
private readonly FileSystemHelper _fileSystemHelper;

private ScriptOptions _scriptOptions;
private Lazy<ScriptContext> _scriptContext;

Expand All @@ -41,7 +38,6 @@ public ScriptProjectSystem(OmniSharpWorkspace workspace, IOmniSharpEnvironment e
{
_workspace = workspace;
_env = env;
_loggerFactory = loggerFactory;
_fileSystemWatcher = fileSystemWatcher;
_fileSystemHelper = fileSystemHelper;
_logger = loggerFactory.CreateLogger<ScriptProjectSystem>();
Expand All @@ -52,9 +48,12 @@ public ScriptProjectSystem(OmniSharpWorkspace workspace, IOmniSharpEnvironment e
public string Language { get; } = LanguageNames.CSharp;
public IEnumerable<string> Extensions { get; } = new[] { CsxExtension };
public bool EnabledByDefault { get; } = true;
public bool Initialized { get; private set; }

public void Initalize(IConfiguration configuration)
{
if (Initialized) return;

_scriptOptions = new ScriptOptions();
ConfigurationBinder.Bind(configuration, _scriptOptions);

Expand Down Expand Up @@ -84,6 +83,8 @@ public void Initalize(IConfiguration configuration)

// Watch CSX files in order to add/remove them in workspace
_fileSystemWatcher.Watch(CsxExtension, OnCsxFileChanged);

Initialized = true;
}

private void OnCsxFileChanged(string filePath, FileChangeType changeType)
Expand Down
19 changes: 18 additions & 1 deletion tests/OmniSharp.Cake.Tests/CakeProjectSystemFacts.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.IO;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
Expand Down Expand Up @@ -71,6 +72,20 @@ public async Task AllProjectsShouldUseLatestLanguageVersion()
}
}

[Fact]
public async Task DoesntParticipateInWorkspaceInfoResponseWhenDisabled()
{
using (var testProject = await TestAssets.Instance.GetTestProjectAsync("CakeProject", shadowCopy: false))
using (var host = CreateOmniSharpHost(testProject.Directory, configurationData: new Dictionary<string, string>
{
["cake:enabled"] = "false"
}))
{
var workspaceInfo = await GetWorkspaceInfoAsync(host);
Assert.Null(workspaceInfo);
}
}

private static async Task<CakeContextModelCollection> GetWorkspaceInfoAsync(OmniSharpTestHost host)
{
var service = host.GetWorkspaceInformationService();
Expand All @@ -82,6 +97,8 @@ private static async Task<CakeContextModelCollection> GetWorkspaceInfoAsync(Omni

var response = await service.Handle(request);

if (!response.ContainsKey("Cake")) return null;

return (CakeContextModelCollection)response["Cake"];
}

Expand Down
1 change: 1 addition & 0 deletions tests/OmniSharp.Http.Tests/EndpointMiddlewareFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ class FakeProjectSystem : IProjectSystem
public string Language { get; } = LanguageNames.CSharp;
public IEnumerable<string> Extensions { get; } = new[] { ".cs" };
public bool EnabledByDefault { get; } = true;
public bool Initialized { get; } = true;

public Task<object> GetWorkspaceModelAsync(WorkspaceInformationRequest request)
{
Expand Down
15 changes: 15 additions & 0 deletions tests/OmniSharp.MSBuild.Tests/WorkspaceInformationTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
Expand Down Expand Up @@ -241,5 +242,19 @@ public async Task ProjectWithWildcardPackageReference()
Assert.Equal(3, project.SourceFiles.Count);
}
}

[Fact]
public async Task DoesntParticipateInWorkspaceInfoResponseWhenDisabled()
{
using (var testProject = await TestAssets.Instance.GetTestProjectAsync("ProjectAndSolution"))
using (var host = CreateOmniSharpHost(testProject.Directory, configurationData: new Dictionary<string, string>
{
["msbuild:enabled"] = "false"
}))
{
var workspaceInfo = await host.RequestMSBuildWorkspaceInfoAsync();
Assert.Null(workspaceInfo);
}
}
}
}
16 changes: 16 additions & 0 deletions tests/OmniSharp.Script.Tests/WorkspaceInformationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,20 @@ public async Task DotnetCoreScriptWithNuget()
}
}

[Fact]
public async Task DoesntParticipateInWorkspaceInfoResponseWhenDisabled()
{
using (var testProject = TestAssets.Instance.GetTestScript("SingleCsiScript"))
using (var host = CreateOmniSharpHost(testProject.Directory, configurationData: new Dictionary<string, string>
{
["script:enabled"] = "false"
}))
{
var workspaceInfo = await GetWorkspaceInfoAsync(host);
Assert.Null(workspaceInfo);
}
}

private string GetMsCorlibPath() => Assembly.Load(new AssemblyName("mscorlib"))?.Location;

private void VerifyCorLib(ScriptContextModel project, bool expected = true)
Expand All @@ -208,6 +222,8 @@ private static async Task<ScriptContextModelCollection> GetWorkspaceInfoAsync(Om

var response = await service.Handle(request);

if (!response.ContainsKey("Script")) return null;

return (ScriptContextModelCollection)response["Script"];
}
}
Expand Down
2 changes: 2 additions & 0 deletions tests/TestUtility/TestHostExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ public static async Task<MSBuildWorkspaceInfo> RequestMSBuildWorkspaceInfoAsync(

var response = await service.Handle(request);

if (!response.ContainsKey("MsBuild")) return null;

return (MSBuildWorkspaceInfo)response["MsBuild"];
}

Expand Down