Skip to content

Allow individual crates to specify server watching in rust-project.json #19907

Closed

Description

@Wilfred

Currently file watching is a global setting, e.g. using the following in VS Code's settings.json:

"rust-analyzer.files.watcher": "server",

However, we'd like the ability to specify file watching on a per-crate basis. This would be useful for projects built with buck, where generated code is written to ./buck-out/gen/abc123/__project/lib.rs. VS Code doesn't notify r-a when this file changes (even if the user has it open), so we get stale go-to-def and code completions when the generated file has changed on disk.

Previous discussions: #556 and #433 for file watchers in general, #16913 for file watching differences between operating systems, and #19387 mentions that VS Code doesn't send all notifications. I think there was an issue discussing VS Code not sending notifications in specific circumstances, but I can't find it.

@davidbarsky's view is (I think) that this is a reasonable addition, and I'm happy to put up a PR if y'all think this makes sense.

Activity

  1. added
    C-feature
    on Jun 2, 2025
  2. ChayimFriedman2 commented on Jun 3, 2025

    @ChayimFriedman2
    Contributor

    So this is a bug in VSCode, and you want to add this setting to workaround it? I don't object if the change is contained.

  3. Veykril commented on Jun 3, 2025

    @Veykril
    Member

    Why exactly is setting the global setting not an option?

  4. Wilfred commented on Jun 3, 2025

    @Wilfred
    ContributorAuthor

    My current repro looks like this:

    (1) Open myproj/main.rs, ./buck-out/generated/mod.rs, and the foo.thrift file that mod.rs is generated from.

    (2) Add a new field to a struct in foo.thrift and save, then make a change to myproj/main.rs and save. Observe that ./buck-out/generated/mod.rs is updated on disk (as a side effect of rust-analyzer.check.overrideCommand running a check build).

    (3) Observe that I'm not offered this new struct field in myproj/main.rs code completion, regardless of the value of rust-analyzer.files.watcher.

    (4) Switch to the ./buck-out/generated/mod.rs file in VS Code, switch back to myproj/main.rs, observe that code completion now knows about my new struct field.

    Overall it feels like generated code should probably have a separate watch mode rather than requiring a global setting (especially since "server" for watcher doesn't help). It's going to be modified outside of VS Code regularly.

  5. Veykril commented on Jun 4, 2025

    @Veykril
    Member

    Overall it feels like generated code should probably have a separate watch mode rather than requiring a global setting (especially since "server" for watcher doesn't help). It's going to be modified outside of VS Code regularly.

    I am confused, if changing it so "server" doesn't help, then how is what this issue proposes going to help? It sounds like neither client or server mode helps you here, so why would allowing this setting to be changed on a per crate basis make those modes suddenly work?

  6. Wilfred commented on Jun 4, 2025

    @Wilfred
    ContributorAuthor

    Sorry, I didn't have a great repro when I first wrote this issue. I was thinking of a rust-project per-crate setting that just made rust-analyzer aggressively poll or use inotify to pick up changes to the generated files.

    I will also trace through the "server" mode logic to see if it's just a limitation there that we can fix.

  7. Wilfred commented on Jun 23, 2025

    @Wilfred
    ContributorAuthor

    Some additional debugging notes:

    The server file watching mode requires your file to belong to the current workspace, i.e. is_workspace_member: true in JSON.

    Adding debug logging to the inotify logic, I think I'm getting a Remove event and nothing else:

    [crates/vfs-notify/src/lib.rs:112:41] &event = Event {
        kind: Remove(
            File,
        ),
        paths: [
            "/home/wilfred/scratch/buck-out/gen/abc123/__project/mod.rs",
        ],
        attr:tracker: None,
        attr:flag: None,
        attr:info: None,
        attr:source: None,
    }
    

    Possibly the code generator is treated as a deletion and a creation of the file, and r-a stops watching after the deletion. I'll dig further.

  8. Wilfred commented on Mar 6, 2026

    @Wilfred
    ContributorAuthor

    OK, I've had some time to investigate this and figured out what's going on.

    (1) If the file watcher gets a Remove event, we don't update the VFS (because the logic tries to find file metadata, which obviously doesn't work for a deleted file). I'll put a PR up for this.

    (2) The file watcher isn't recursive, so I can't use the parent directory of a crate. This is unfortunate for generated code when the directory is also recreated during code generation. I'll put up a PR for this too.

    Specifically I have mycrate/generated/lib.rs and the code generation removes and recreates the whole generated/ directory. notify can't watch generated/ when the whole directory is removed, it needs to watch mycrate/ instead.

    (3) VS Code ignores file changes that aren't done by the user (tested on a linux server). If mycrate/generated/lib.rs isn't open, rust-analyzer never notices when it changes. If mycrate/generated/lib.rs is open but changed externally, VS Code only tells r-a that the file is changed when the user switches to that tab.

    This means that I need server watching. I'll look at switching our users to server watching unconditionally (rather than per-crate), although I always prefer following upstream defaults as much as possible. It seems like server file watching is less used so might have more / different bugs.

  9. Wilfred commented on Apr 7, 2026

    @Wilfred
    ContributorAuthor

    Between #21770 and #21771 I don't think there's anything left to do here, everything is working in all the cases I've seen when using the server-side watching for all projects :)

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-feature

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions