haimgel / display-switch

Turn a $30 USB switch into a full-featured multi-monitor KVM switch
https://haim.dev/posts/2020-07-28-dual-monitor-kvm/
MIT License
2.86k stars 112 forks source link

`build.rs`'s `rerun-if-changed` directives don't expand `*.swift` #115

Closed Chris--B closed 1 year ago

Chris--B commented 1 year ago

I stumbled upon this from your wonderful blog post trying to link in Swift to my own project. It worked great, but my builds without a changed file were taking multiple seconds, which is weird. Digging into it, I found an issue in my build.rs.

In build.rs we have this line:

    println!("cargo:rerun-if-changed=mac_ddc/src/*.swift");

This looks like it globs all .swift files but does not. It is looking for the literal file *.swift, which should never exist. Since the file doesn't exist, it caused the build.rs to rerun every single build.

The fix is to list each Swift file directly. You could do this with a manual list, but I walk the directory and find the swift files instead. Here's my snippet:

    for entry in std::fs::read_dir("src/swift/").unwrap() {
        // I don't like using ".flatten()" to skip over Err results
        #![allow(clippy::manual_flatten)]
        if let Ok(e) = entry {
            if e.file_type().unwrap().is_file() {
                let p = e.path().canonicalize().unwrap();
                println!("cargo:rerun-if-changed={}", p);
            }
        }
    }

I include all files it finds, but you can filter them to restrict it just to .swift files.

The impact of this is pretty minor - an extra second or two to rebuild the crate if anything maybe changed. For clients of the crate, I wouldn't expect any impact. But when I used this for my own stuff, it really slows down iteration cycles (which I try to keep as tight as possible).

haimgel commented 1 year ago

Gah, that's a pretty rookie mistake! Thanks for pointing this out! I got rid of Swift parts of this project, so it's pure Rust now, but I should probably go and update that blog post. Thanks again!