Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't the ServiceStack Razor FileSystemWatcher work on Mono + Mac OS X?

ServiceStack's new support for Razor v2 uses a FileSystemWatcher to detect changes to tracked view files and mark them as invalid so they'll be recompiled at the next request.

This is great for debugging as it lets you edit your views and not rebuild/restart your project.

On Mono (currently running 3.0.10) on my Mac OS X (Mountain Lion) there is apparently a Mono bug where the FileSystemWatcher doesn't raise Changed events for file changes. Furthermore, it also doesn't raise any events for files in a subdirectory, even if IncludeSubdirectories is set to true.

like image 483
Aaron Lerch Avatar asked Dec 06 '25 14:12

Aaron Lerch


1 Answers

After investigating and testing various things out, I found several older bug reports against Mono about failing FileSystemWatcher functionality.

The workaround to the problem is found in the Mono source: https://github.com/mono/mono/blob/master/mcs/class/System/System.IO/FileSystemWatcher.cs

string managed = Environment.GetEnvironmentVariable ("MONO_MANAGED_WATCHER");
...
if (String.Compare (managed, "disabled", true) == 0)
    NullFileWatcher.GetInstance (out watcher);
else
    DefaultWatcher.GetInstance (out watcher);

If you set the environment variable MONO_MANAGED_WATCHER to anything (I set it to "enabled") then it will use the DefaultWatcher which is a managed implementation, and it works on Mac OS X.

So during my application startup, I added:

Environment.SetEnvironmentVariable("MONO_MANAGED_WATCHER", "enabled");

and voila, my Razor views are recompiled after I save a new version. :)

like image 52
Aaron Lerch Avatar answered Dec 08 '25 08:12

Aaron Lerch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!