Skip to content

ConfigurationBuilder.AddJsonFile can't fire OnChange event using relative paths #111101

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -1021,5 +1021,31 @@ private sealed class MyOptions

public string XmlKey1 { get; set; }
}

private async Task WatchOverConfigJsonFileAndUpdateIt(string filePath)
{
var builder = new ConfigurationBuilder().AddJsonFile(filePath, true, true).Build();
bool reloaded = false;
ChangeToken.OnChange(builder.GetReloadToken, () =>
{
reloaded = true;
});
File.WriteAllText(filePath, "{\"Prop2\":\"Value2\"}");
await WaitForChange(
() => reloaded,
"on file change event handler did not get executed");
}

[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsWindows))]
public async Task OnChangeGetFiredForRelativeWindowsPath()
{
await WatchOverConfigJsonFileAndUpdateIt(".\\testFileToReload.json");
}

[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsLinux))]
public async Task OnChangeGetFiredForRelativeLinuxPath()
{
await WatchOverConfigJsonFileAndUpdateIt("./testFileToReload.json");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>$(NetCoreAppCurrent);$(NetFrameworkMinimum)</TargetFrameworks>
<EnableDefaultItems>true</EnableDefaultItems>
</PropertyGroup>

<ItemGroup>
<Compile Include="$(LibrariesProjectRoot)Microsoft.Extensions.Configuration\tests\Common\ConfigurationProviderExtensions.cs"
Link="Common\ConfigurationProviderExtensions.cs" />
<Compile Include="$(LibrariesProjectRoot)Microsoft.Extensions.Configuration\tests\Common\ConfigurationProviderExtensions.cs" Link="Common\ConfigurationProviderExtensions.cs" />
</ItemGroup>

<ItemGroup>
<Content Include="testFileToReload.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"Prop1": "Value1"
}
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,17 @@ private IChangeToken GetOrAddChangeToken(string pattern)
}
else
{
changeToken = GetOrAddFilePathChangeToken(pattern);
// get rid of \. in Windows and ./ in UNIX's at the start of path file
var filePath = RemoveRelativePathSegment(pattern);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a breaking change?

Copy link
Contributor Author

@pedrobsaila pedrobsaila Jan 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it breaks anything, it looks like a bug fix to me. Unless, there's someone relying on the fact that on change does not get fired for his files when specifying them with starting ./ which is highly improbable because he could just pass false to AddJsonFile if didn't want to subscribe to them

changeToken = GetOrAddFilePathChangeToken(filePath);
}

return changeToken;
}

private static string RemoveRelativePathSegment(string pattern) => pattern.StartsWith("./", StringComparison.Ordinal) ?
pattern.Substring(2) : pattern;

internal IChangeToken GetOrAddFilePathChangeToken(string filePath)
{
if (!_filePathTokenLookup.TryGetValue(filePath, out ChangeTokenInfo tokenInfo))
Expand Down
Loading