-
Notifications
You must be signed in to change notification settings - Fork 416
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2140 from OmniSharp/bugfix/config-fail
Do not crash on startup when configuration is invalid
- Loading branch information
Showing
6 changed files
with
143 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,55 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.FileProviders; | ||
using OmniSharp.Internal; | ||
using OmniSharp.Utilities; | ||
|
||
namespace OmniSharp | ||
{ | ||
public class ConfigurationBuilder : IConfigurationBuilder | ||
public class ConfigurationBuilder | ||
{ | ||
private readonly IOmniSharpEnvironment _environment; | ||
private readonly IConfigurationBuilder _builder; | ||
|
||
public ConfigurationBuilder(IOmniSharpEnvironment environment) | ||
{ | ||
_environment = environment; | ||
_builder = new Microsoft.Extensions.Configuration.ConfigurationBuilder() | ||
.SetBasePath(AppContext.BaseDirectory); | ||
} | ||
|
||
public IConfigurationBuilder Add(IConfigurationSource source) | ||
public ConfigurationResult Build(Action<IConfigurationBuilder> additionalSetup = null) | ||
{ | ||
_builder.Add(source); | ||
return this; | ||
} | ||
|
||
public IConfigurationRoot Build() | ||
{ | ||
var configBuilder = new Microsoft.Extensions.Configuration.ConfigurationBuilder() | ||
.SetBasePath(AppContext.BaseDirectory) | ||
.AddEnvironmentVariables("OMNISHARP_"); | ||
|
||
if (_environment.AdditionalArguments?.Length > 0) | ||
try | ||
{ | ||
configBuilder.AddCommandLine(_environment.AdditionalArguments); | ||
var configBuilder = new Microsoft.Extensions.Configuration.ConfigurationBuilder() | ||
.SetBasePath(AppContext.BaseDirectory) | ||
.AddEnvironmentVariables("OMNISHARP_"); | ||
|
||
if (_environment.AdditionalArguments?.Length > 0) | ||
{ | ||
configBuilder.AddCommandLine(_environment.AdditionalArguments); | ||
} | ||
|
||
// Use the global omnisharp config if there's any in the shared path | ||
configBuilder.CreateAndAddGlobalOptionsFile(_environment); | ||
|
||
// Use the local omnisharp config if there's any in the root path | ||
configBuilder.AddJsonFile( | ||
new PhysicalFileProvider(_environment.TargetDirectory).WrapForPolling(), | ||
Constants.OptionsFile, | ||
optional: true, | ||
reloadOnChange: true); | ||
|
||
// bootstrap additional host configuration at the end | ||
additionalSetup?.Invoke(configBuilder); | ||
|
||
var config = configBuilder.Build(); | ||
return new ConfigurationResult(config); | ||
} | ||
catch (Exception ex) | ||
{ | ||
return new ConfigurationResult(ex); | ||
} | ||
|
||
// Use the global omnisharp config if there's any in the shared path | ||
configBuilder.CreateAndAddGlobalOptionsFile(_environment); | ||
|
||
// Use the local omnisharp config if there's any in the root path | ||
configBuilder.AddJsonFile( | ||
new PhysicalFileProvider(_environment.TargetDirectory).WrapForPolling(), | ||
Constants.OptionsFile, | ||
optional: true, | ||
reloadOnChange: true); | ||
|
||
return configBuilder.Build(); | ||
} | ||
|
||
public IDictionary<string, object> Properties => _builder.Properties; | ||
public IList<IConfigurationSource> Sources => _builder.Sources; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using System; | ||
using Microsoft.Extensions.Configuration; | ||
|
||
namespace OmniSharp | ||
{ | ||
public class ConfigurationResult | ||
{ | ||
public ConfigurationResult(IConfigurationRoot configuration) | ||
{ | ||
Configuration = configuration; | ||
} | ||
|
||
public ConfigurationResult(Exception exception) | ||
{ | ||
Exception = exception; | ||
Configuration = new ConfigurationRoot(Array.Empty<IConfigurationProvider>()); | ||
} | ||
|
||
public IConfigurationRoot Configuration { get; } | ||
|
||
public Exception Exception { get; } | ||
|
||
public bool HasError() => Exception != null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Microsoft.Extensions.Configuration; | ||
using OmniSharp.Services; | ||
using Xunit; | ||
|
||
namespace OmniSharp.Tests | ||
{ | ||
public class ConfigurationBuilderFacts | ||
{ | ||
[Fact] | ||
public void CustomConfigCanBeAdded() | ||
{ | ||
var env = new OmniSharpEnvironment(); | ||
var builder = new ConfigurationBuilder(env); | ||
var result = builder.Build(c => c.AddInMemoryCollection(new Dictionary<string, string> { { "key", "value" } })); | ||
|
||
Assert.False(result.HasError()); | ||
Assert.Equal("value", result.Configuration["key"]); | ||
} | ||
|
||
[Fact] | ||
public void EnvironmentVariableCanBeRead() | ||
{ | ||
var tempValue = Guid.NewGuid().ToString("d"); | ||
try | ||
{ | ||
Environment.SetEnvironmentVariable("OMNISHARP_testValue", tempValue); | ||
var env = new OmniSharpEnvironment(); | ||
var builder = new ConfigurationBuilder(env); | ||
var result = builder.Build(c => c.AddInMemoryCollection()); | ||
|
||
Assert.False(result.HasError()); | ||
Assert.Equal(tempValue, result.Configuration["testValue"]); | ||
} | ||
finally | ||
{ | ||
Environment.SetEnvironmentVariable("OMNISHARP_testValue", null); | ||
} | ||
} | ||
|
||
[Fact] | ||
public void FileArgsCanBeRead() | ||
{ | ||
var env = new OmniSharpEnvironment(additionalArguments: new string[] { "key:nestedKey=value" }); | ||
var builder = new ConfigurationBuilder(env); | ||
var result = builder.Build(); | ||
|
||
Assert.False(result.HasError()); | ||
Assert.Equal("value", result.Configuration["key:nestedKey"]); | ||
} | ||
|
||
[Fact] | ||
public void DoesNotCrashOnException() | ||
{ | ||
var env = new OmniSharpEnvironment(); | ||
var builder = new ConfigurationBuilder(env); | ||
var result = builder.Build(c => throw new Exception("bad thing happened")); | ||
|
||
Assert.True(result.HasError()); | ||
Assert.NotNull(result.Configuration); | ||
Assert.Empty(result.Configuration.AsEnumerable()); | ||
} | ||
} | ||
} |