Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

Bug (Case 1344552) FileSystemWatcher inconsistencies after mono upgrade

Discussion in '2021.2 Beta' started by print_helloworld, Jun 23, 2021.

  1. print_helloworld

    print_helloworld

    Joined:
    Nov 14, 2016
    Posts:
    231
    A lot of the versions of Unity printed the full path instead of the name of the file when a file watcher was looking at a directory. Which according to the documentation by msdn it should print the file name instead. This was a consistent behaviour (though not correct), but it's a bit anxiety inducing since I'm not sure if its the same behaviour on linux and osx.

    After the mono upgrade in a16, the Name property continued to stay the same, but then the FullPath property started printing two full paths concatenated together. One was backslashed and the other wasnt (I was on windows so the forward slash path should be invalid).

    C:\GitHub\project\file.txtC:/GitHub/project/file.txt


    @bdovaz also posted a thread in the experimental forum about a specific FileSystemWatcher api missing lately if using IL2CPP with one of the api compatability levels, but ive tested and its missing in both of them at the moment (2021.2.0b1).

    Is this specific api managed/observed by Unity in any way? I'd assume "yes" since they have a fork of mono where they can adjust fix this behaviour. Btw the issue tracker has this filed for the Name bug and the FullPath bug as a note, I figure they're related enough to warrant the same bug ticket even though one existed for a long time and the other only just recently appeared, is this correct?
     
  2. mahdi_jeddi

    mahdi_jeddi

    Joined:
    Jul 18, 2016
    Posts:
    246
    I had a lot of problems with this API. Registering to its events was very costly after some Unity version and we had to move it to a thread. And after some time the events were firing multiple minutes after the edits were done to the files. I thought that the problems may have something to do with Windows Update and not Unity...
     
  3. ImpossibleRobert

    ImpossibleRobert

    Joined:
    Oct 10, 2013
    Posts:
    527
    I had the same effect @mahdi_jeddi and it was driving me nuts. Then I checked other assets in my project and realized on new asset had installed its own watcher, completely throwing my watcher off track. I just deleted their watcher and everything was back to normal.
     
  4. print_helloworld

    print_helloworld

    Joined:
    Nov 14, 2016
    Posts:
    231
    My setup is to take all of the 4 events that the watcher outputs directly into a List<> and process the list in a while loop inside update, its fairly fail safe in my past projects. Minute delays for the events sounds atrocious though, could be something on the OS level.
    Code (CSharp):
    1. private List<FileSystemWatcherArgs> fileEvents = new List<FileSystemWatcherArgs>();
    2.  
    3. private void OnEnable()
    4. {
    5.     FileSystemWatcher watcher = new FileSystemWatcher(Application.dataPath)
    6.     {
    7.         NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName,
    8.         EnableRaisingEvents = true,
    9.         IncludeSubdirectories = true
    10.     };
    11.  
    12.     watcher.Changed += (o, e) => fileEvents.Add(e);
    13.     watcher.Renamed += (o, e) => fileEvents.Add(e);
    14.     watcher.Deleted += (o, e) => fileEvents.Add(e);
    15.     watcher.Created += (o, e) => fileEvents.Add(e);
    16. }
    17.  
    18. private void Update()
    19. {
    20.     foreach (FileSystemWatcherArgs fileEvent in fileEvents)
    21.     {
    22.         //do something
    23.     }
    24.  
    25.     fileEvents.Clear();
    26. }
     
    mahdi_jeddi likes this.
  5. mahdi_jeddi

    mahdi_jeddi

    Joined:
    Jul 18, 2016
    Posts:
    246
    How did you do that? Searching for FileSystemWatcher in all script files?

    If it's a OS level problem, why Unity works fine when I add a new file to the assets folder? Do they use some other API for that? Maybe a bug in Mono implementation?
     
  6. ImpossibleRobert

    ImpossibleRobert

    Joined:
    Oct 10, 2013
    Posts:
    527
    Yes indeed, used Rider to find other usages of the API
     
  7. print_helloworld

    print_helloworld

    Joined:
    Nov 14, 2016
    Posts:
    231