Skip to content

Commit 8b7c300

Browse files
authored
Fix DependencyContext splitting on semi-colon (#87518)
* Fix DependencyContext splitting on semi-colon 5e67657 introduced a bug in DependencyContextPaths where the static array is not initialized before it is being used in the Create static method. This fix removes the static array since it is only used once. - Don't cache the semicolon array since it is only used once at startup - Skip test on netfx since it doesn't work.
1 parent 8042fac commit 8b7c300

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

src/libraries/Microsoft.Extensions.DependencyModel/src/DependencyContextPaths.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ internal sealed class DependencyContextPaths
2020

2121
public IEnumerable<string> NonApplicationPaths { get; }
2222

23-
private static readonly char[] s_semicolon = new[] { ';' };
24-
2523
public DependencyContextPaths(
2624
string? application,
2725
string? sharedRuntime,
@@ -42,7 +40,13 @@ private static DependencyContextPaths GetCurrent()
4240

4341
internal static DependencyContextPaths Create(string? depsFiles, string? sharedRuntime)
4442
{
45-
string[]? files = depsFiles?.Split(s_semicolon, StringSplitOptions.RemoveEmptyEntries);
43+
#if NETCOREAPP
44+
const char separator = ';';
45+
#else
46+
// This method is only executed once at startup. No need to cache the char[].
47+
char[] separator = { ';' };
48+
#endif
49+
string[]? files = depsFiles?.Split(separator, StringSplitOptions.RemoveEmptyEntries);
4650
string? application = files != null && files.Length > 0 ? files[0] : null;
4751

4852
string[]? nonApplicationPaths = files?

src/libraries/Microsoft.Extensions.DependencyModel/tests/DependencyContextTests.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,19 @@ public void MergeMergesRuntimeGraph()
276276
Subject.Fallbacks.Should().BeEquivalentTo("win7-x64", "win7-x86");
277277
}
278278

279+
[Fact]
280+
[SkipOnTargetFramework(TargetFrameworkMonikers.NetFramework, "GetEntryAssembly() returns null")]
281+
public void DefaultWorksCorrectly()
282+
{
283+
// only need to assert the context contains non-null properties.
284+
285+
var context = DependencyContext.Default;
286+
Assert.NotNull(context);
287+
Assert.NotNull(context.RuntimeGraph);
288+
Assert.NotNull(context.RuntimeLibraries);
289+
Assert.NotNull(context.Target);
290+
}
291+
279292
private TargetInfo CreateTargetInfo()
280293
{
281294
return new TargetInfo(

0 commit comments

Comments
 (0)