[ANN] BetterFileWatching 1.0: recursive file watching with easy API

Hi! :waving_hand: I would like to announce BetterFileWatching.jl version 1.0!

watch_folder(f::Function, dir=".")

It is a file watching library (get notified when files change on disk) that can watch files and folders on all platforms. Unlike the FileWatching stdlib, this library supports:

  • Recursive folder watching: it can watch one folder, and all subfolders automatically
  • Watching contents of directory files: watch for edits, not just new and deleted files in a directory
  • Callback API: your function gets called on events. This means that handling events does not prevent receiving new events.

Version 1.0 has an even bigger CI test set that passes on Windows, Linux and MacOS. Version 1.0 is a complete rewrite. It’s now a pure Julia package with no binary dependencies and better performance.

Example

Simple example:

watch_folder(".") do event
    @info "Something changed!" event
end

CancellationToken

You can also watch a folder asynchronously, and stop it later with a CancellationTokens.jl token:

using BetterFileWatching, CancellationTokens

src = CancellationTokenSource()

watch_task = @async watch_folder(".", get_token(src)) do event
    @info "Something changed!" event
end

sleep(5)

# stop watching the folder
cancel(src)

Ignore function

There is also an ignore keyword to skip subtrees like .git. For example:

watch_folder(".", token; ignore = rel -> ".git" in split(rel, "/")) do event
    # ...
end

History (coded with AI)

This package was originally created by @Pangoraw and me in 2021, out of a need for recursive file watching in Julia, and we use it in PlutoSliderServer.jl. Version 0.1 used Deno wrapped in a JLL package to get their file watching API (which is very stable) in Julia. It worked fairly well, but the binary dependency and separate OS process was overkill.

I’m experimenting with AI coding recently, and I learned a technique from @quinnj and @pankgeorg to tell the robot to look at a well written implementation in another language, and translate that to Julia. So I used the implementation from Bun, written in Rust, and the AI wrote the whole package in one go. I designed the API spec and wrote most of the communication. I reviewed some code, but I don’t understand the file system APIs, so I cannot review this part. It’s not a lot of code, so an outside reviewer would be welcome!

The result is really good! It is now a pure Julia package with no binary dependency, and no separate process. It uses libuv on MacOS and Windows, and inotify with ccall on Linux. More details are in the repository.

The package is free and open source! I hope it’s useful to you :slight_smile:

9 Likes