Skip to content

FileConfigurationProvider watches all files starting an first existing path; /foo/appsettings.json will listen on all files on the file system #116713

Closed

Description

@simendsjo

Description

It seems FileConfigurationProvider has some "strange" behavior related to reloadOnChange. I haven't investigated properly, but I have a simple testcase to trigger it.

Looks like it traverse up the filesystem to the first existing folder, and then recursively inotify_add_watch everything it comes across.
So a file like "/doesntexist/appsettings.json" will add every file in the filesystem.

I tested on Linux (GNU Guix System) with net8 and net9.

ConfigurationBuilder()
  .AddJsonFile(path = "/doesntexist/appsettings.json", optional = true, reloadOnChange = true)
  .Build()
inotify_add_watch(55, "/", IN_MODIFY|IN_ATTRIB|IN_MOVED_FROM|IN_MOVED_TO|IN_CREATE|IN_DELETE|IN_ONLYDIR|IN_EXCL_UNLINK) = 1
...
lstat("/gnu", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
inotify_add_watch(55, "/gnu", IN_MODIFY|IN_ATTRIB|IN_MOVED_FROM|IN_MOVED_TO|IN_CREATE|IN_DELETE|IN_ONLYDIR|IN_DONT_FOLLOW|IN_EXCL_UNLINK) = 2
..
lstat("/gnu/store", {st_mode=S_IFDIR|S_ISVTX|0775, st_size=13279232, ...}) = 0
inotify_add_watch(55, "/gnu/store", IN_MODIFY|IN_ATTRIB|IN_MOVED_FROM|IN_MOVED_TO|IN_CREATE|IN_DELETE|IN_ONLYDIR|IN_DONT_FOLLOW|IN_EXCL_UNLINK) = 3
..
lstat("/gnu/store/hbq0irhwfnc0f0gbff8rs1zjq3ljx985-fluidsynth-2.3.1", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
inotify_add_watch(55, "/gnu/store/hbq0irhwfnc0f0gbff8rs1zjq3ljx985-fluidsynth-2.3.1", IN_MODIFY|IN_ATTRIB|IN_MOVED_FROM|IN_MOVED_TO|IN_CREATE|IN_DELETE|IN_ONLYDIR|IN_DONT_FOLLOW|IN_EXCL_UNLINK) = 4

Reproduction Steps

ConfigurationBuilder()
  .AddJsonFile(path = "/doesntexist/appsettings.json", optional = true, reloadOnChange = true)
  .Build()

Expected behavior

It doesn't find the optional file

Actual behavior

It adds inotify watch for every file in the file system

Regression?

No response

Known Workarounds

Check for non-existing paths before adding the json...?

Configuration

net8 and net9 on GNU Guix System (Linux). x86_64.

Other information

No response

Activity

  1. added
    untriaged
    on Jun 16, 2025
  2. added
    needs-area-label
    on Jun 16, 2025
  3. added and removed
    needs-area-label
    on Jun 17, 2025
  4. dotnet-policy-service commented on Jun 17, 2025

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @dotnet/area-extensions-configuration
    See info in area-owners.md if you want to be subscribed.

  5. dotnet-policy-service commented on Jun 17, 2025

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @dotnet/area-extensions-filesystem
    See info in area-owners.md if you want to be subscribed.

  6. simendsjo commented on Jun 17, 2025

    @simendsjo
    Author

    FileConfigurationSource has the following, which will traverse back to the first existing folder, in the case of /doesntexist/appsettings.json, /.
    It thus runs FileProvider = new PhysicalFileProvider("/");.

    This in turn creates a file system wather FileSystemWatcher(root), and probably with the recursive flag set.

    I guess it recurse down directories it doesn't find in order to detect that the missing directories is created?
    Nevertheless, this behavior is problematic when the first existing directory contains a lot of files recursively.

    public void ResolveFileProvider()
    {
        if (FileProvider == null &&
            !string.IsNullOrEmpty(Path) &&
            System.IO.Path.IsPathRooted(Path))
        {
            string? directory = System.IO.Path.GetDirectoryName(Path);
            string? pathToFile = System.IO.Path.GetFileName(Path);
            while (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory))
            {
                pathToFile = System.IO.Path.Combine(System.IO.Path.GetFileName(directory), pathToFile);
                directory = System.IO.Path.GetDirectoryName(directory);
            }
            if (Directory.Exists(directory))
            {
                FileProvider = new PhysicalFileProvider(directory);
                Path = pathToFile;
            }
        }
    }
  7. added this to the Future milestone on Jul 23, 2025
  8. removed
    untriaged
    on Jul 23, 2025
  9. self-assigned this
    on Mar 12, 2026
  10. modified the milestones: Future, 11.0.0 on Apr 1, 2026
  11. added a commit that references this issue on Apr 9, 2026
    b9ef488
  12. locked and limited conversation to collaborators on May 10, 2026
  13. added a commit that references this issue on Jul 15, 2026
    921edc7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions