Skip to content

FileSystemEventArgs.FullPath does not return a fully qualified path #62606

Description

@UnityAlex

Description

As per the documentation https://docs.microsoft.com/en-us/dotnet/api/system.io.filesystemeventargs.fullpath?view=net-6.0 the FullPath property should return a fully qualified path. However it appears the constructor for FileSystemEventArgs assumes the directory passed in is fully qualified: https://github.com/dotnet/runtime/blob/main/src/libraries/System.IO.FileSystem.Watcher/src/System/IO/FileSystemEventArgs.cs#L22

When used in conjunction with FileSystemWatcher it is possible for the path to be relative: https://github.com/dotnet/runtime/blob/main/src/libraries/System.IO.FileSystem.Watcher/src/System/IO/FileSystemWatcher.cs#L260

Reproduction Steps

Repro code:

using System;
using System.IO;

namespace MyNamespace
{
    class Program
    {
        static void Main()
        {
            using var watcher = new FileSystemWatcher(@"bar");

            watcher.NotifyFilter = NotifyFilters.LastWrite;

            watcher.Changed += OnChanged;

            watcher.Filter = "*.txt";
            watcher.IncludeSubdirectories = false;
            watcher.EnableRaisingEvents = true;

            Console.WriteLine("Press enter to exit.");
            Console.ReadLine();
        }

        private static void OnChanged(object sender, FileSystemEventArgs e)
        {
            if (e.ChangeType != WatcherChangeTypes.Changed)
            {
                return;
            }
            Console.WriteLine($"Changed: {e.FullPath}");
        }
    }
}
  1. I created the following dir structure on my machine:
    C:\foo\bar\footest.txt

  2. Set the working directory to be C:\foo

  3. Run the program and then modify footest.txt to trigger a changed event.

Expected behavior

Output should be:
Changed: C:\foo\bar\footest.txt

Actual behavior

Current output:
Changed: bar\footest.txt

Regression?

No response

Known Workarounds

Using Path.GetFullPath to fetch the fully qualified path

Console.WriteLine($"Changed: {Path.GetFullPath(e.FullPath)}");

Configuration

.Net 6.0
Windows 10 x64
After looking at the source I believe that this is not unique to Windows

Other information

No response

Activity

  1. ghost added
    untriaged
    on Dec 9, 2021
  2. adamsitnik commented on Dec 10, 2021

    @adamsitnik
    Member
  3. jozkee commented on Dec 17, 2021

    @jozkee
    Member

    This would require a breaking change on FileSystemEventArgs.FullPath which I'm inclined to accept given the name of the API.

    cc @ericstj @jeffhandley

  4. added this to the 7.0.0 milestone on Dec 17, 2021
  5. added
    breaking-change
    help wanted
    and removed
    untriaged
    on Dec 17, 2021
  6. slask commented on Dec 18, 2021

    @slask
    Contributor

    Hi @jozkee. Can I take this issue?

  7. jozkee commented on Dec 18, 2021

    @jozkee
    Member

    @slask Yes, let me assign you. Thanks!

  8. added 2 commits that reference this issue on Dec 21, 2021
    280021d
    21c655c
  9. ghost added
    in-pr
    on Dec 21, 2021
  10. 2 remaining items

  11. added 2 commits that reference this issue on Dec 30, 2021
    decdc2b
    f28bf24
  12. added 3 commits that reference this issue on Jan 17, 2022
    a025928
    5ff61d5
    aaf4b45
  13. slask commented on Jan 21, 2022

    @slask
    Contributor

    Linking docs issue dotnet/docs#27916

  14. added 2 commits that reference this issue on Feb 7, 2022
    646eeac
    c657af2
  15. added a commit that references this issue on Feb 7, 2022
    2a1b15d
  16. ghost removed
    in-pr
    on Feb 7, 2022
  17. ghost locked as resolved and limited conversation to collaborators on Mar 10, 2022
  18. jeffhandley commented on May 5, 2022

    @jeffhandley
    Member

    After fixing #68566 with #68582, which was addressing a regression caused by #63051, which fixed this issue, I've reconsidered this original issue further. I think we should revert both #68582 and #63051 and return FileSystemEventArgs and RenamedEventArgs back to their previous behavior and instead address this as a documentation issue.

    The regression that was reported involved using an empty directory name, and it was relying on FullPath being relative to the process's directory. From that regression report, it's easy to imagine scenarios where the non-rooted path nature of FullPath is utilized and perhaps even leveraged to combine that full path to a different root. I remember such code I wrote myself back in .NET Framework days that would be broken by this change.

    I'm going to submit a PR that reverts both #63051 and #68582, but leaves the additional tests in place, adapted to match the previous behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-System.IObreaking-changehelp wanted

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions