Skip to content

watched file is duplicated in callback in certain cases #55

Closed

Description

@tomjemmett

I have encountered an interesting case where watcher is sometimes firing the callback and reporting the same file has been changed more than once. Is this intended behaviour? I think this occurs when a file changes more than once before R is able to fire the callback. An example:

tf <- "/tmp/test"

w <- watcher::watcher(
  tf,
  \(file) {
    cat(
      "length:", length(file), "\n"
    )
  }
)

w$start()

then, in a separate terminal, if I run

echo 1 >> /tmp/test

Watcher reports "length: 1".

if I run

echo 2 >> /tmp/test; echo 3 >> /tmp/test

Watcher now reports "length: 2".

This has caused me issues because I was watching a json file, and essentially having a callback which ran jsonlite::read_json(file), this leads to file erroring with invalid 'description' argument error. my solution is to simply take the first item for file, which is fine for my particular use case.

Activity

  1. shikokuchuo commented on Oct 12, 2026

    @shikokuchuo
    Member

    It's possible to get duplicates as e.g. a single write can generate multiple filesystem events at source (Created/Updated).

    However, we only ever return the paths to R and no additional information about the event, hence it's arguably not useful to have duplicated paths, and creates extra work in your case above.

    We can solve this by making the events consolidated ('bubbled') before the callback fires, so the paths reported by a single callback are always unique.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions