File size: 32,752 Bytes
b2cad4f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 | diff --git a/crates/next-api/src/project.rs b/crates/next-api/src/project.rs
index d3dd463d0e..f74b426864 100644
--- a/crates/next-api/src/project.rs
+++ b/crates/next-api/src/project.rs
@@ -45,7 +45,7 @@ use turbo_tasks::{
};
use turbo_tasks_env::{EnvMap, ProcessEnv};
use turbo_tasks_fs::{
- DiskFileSystem, FileContent, FileSystem, FileSystemPath, VirtualFileSystem,
+ DiskFileSystem, DiskWatcherConfig, FileContent, FileSystem, FileSystemPath, VirtualFileSystem,
canonicalize_to_rcstr, invalidation,
};
use turbo_unix_path::join_path;
@@ -631,9 +631,7 @@ impl ProjectContainer {
.read_strongly_consistent()
.await?;
if watch.enable {
- project_fs
- .start_watching_with_invalidation_reason(watch.poll_interval)
- .await?;
+ project_fs.start_watching().await?;
} else {
project_fs.invalidate_with_reason(|path| invalidation::Initialize {
// this path is just used for display purposes
@@ -776,9 +774,7 @@ impl ProjectContainer {
if !ReadRef::ptr_eq(&prev_project_fs, &project_fs) {
if watch.enable {
// TODO stop watching: prev_project_fs.stop_watching()?;
- project_fs
- .start_watching_with_invalidation_reason(watch.poll_interval)
- .await?;
+ project_fs.start_watching().await?;
} else {
project_fs.invalidate_with_reason(|path| invalidation::Initialize {
// this path is just used for display purposes
@@ -1087,10 +1083,16 @@ impl Project {
.unwrap()
.into();
- Ok(DiskFileSystem::new_with_denied_paths(
+ Ok(DiskFileSystem::new_with_options(
PROJECT_FILESYSTEM_NAME,
*self.root_path,
vec![denied_path, denied_profiles_path],
+ DiskWatcherConfig {
+ recursive_mode: None,
+ poll_interval: self.watch.poll_interval,
+ // the dev server reports these to the user
+ report_invalidation_reason: true,
+ },
))
}
diff --git a/turbopack/crates/turbo-tasks-fs/examples/hash_directory.rs b/turbopack/crates/turbo-tasks-fs/examples/hash_directory.rs
index ec4a63534b..3b39edf0ab 100644
--- a/turbopack/crates/turbo-tasks-fs/examples/hash_directory.rs
+++ b/turbopack/crates/turbo-tasks-fs/examples/hash_directory.rs
@@ -29,7 +29,7 @@ async fn main() -> Result<()> {
Box::pin(async {
let root = RcStr::from(current_dir().unwrap().to_str().unwrap());
let disk_fs = DiskFileSystem::new(rcstr!("project"), Vc::cell(root));
- disk_fs.await?.start_watching(None).await?;
+ disk_fs.await?.start_watching().await?;
// Smart Pointer cast
let fs: Vc<Box<dyn FileSystem>> = Vc::upcast(disk_fs);
diff --git a/turbopack/crates/turbo-tasks-fs/examples/hash_glob.rs b/turbopack/crates/turbo-tasks-fs/examples/hash_glob.rs
index 3aa6492c2e..84cb1e0e38 100644
--- a/turbopack/crates/turbo-tasks-fs/examples/hash_glob.rs
+++ b/turbopack/crates/turbo-tasks-fs/examples/hash_glob.rs
@@ -30,7 +30,7 @@ async fn main() -> Result<()> {
Box::pin(async {
let root = RcStr::from(current_dir().unwrap().to_str().unwrap());
let disk_fs = DiskFileSystem::new(rcstr!("project"), Vc::cell(root));
- disk_fs.await?.start_watching(None).await?;
+ disk_fs.await?.start_watching().await?;
// Smart Pointer cast
let fs: Vc<Box<dyn FileSystem>> = Vc::upcast(disk_fs);
diff --git a/turbopack/crates/turbo-tasks-fs/src/disk.rs b/turbopack/crates/turbo-tasks-fs/src/disk.rs
index af3245e398..2133641cff 100644
--- a/turbopack/crates/turbo-tasks-fs/src/disk.rs
+++ b/turbopack/crates/turbo-tasks-fs/src/disk.rs
@@ -9,7 +9,6 @@ use std::{
mem::take,
path::{MAIN_SEPARATOR, Path, PathBuf},
sync::{Arc, LazyLock, Weak},
- time::Duration,
};
use anyhow::{Context, Result, anyhow, bail};
@@ -42,7 +41,7 @@ use crate::{
mutex_map::MutexMap,
path_map::OrderedPathMapExt,
retry::{can_retry, retry_blocking, retry_blocking_custom},
- watcher::DiskWatcher,
+ watcher::{DiskWatcher, DiskWatcherConfig},
};
/// Validate the path, returning the valid path, a modified-but-now-valid path, or bailing with an
@@ -445,11 +444,7 @@ impl DiskFileSystemInner {
}
#[tracing::instrument(level = "info", name = "start filesystem watching", skip_all, fields(path = %self.root))]
- async fn start_watching_internal(
- self: &Arc<Self>,
- report_invalidation_reason: bool,
- poll_interval: Option<Duration>,
- ) -> Result<()> {
+ async fn start_watching_internal(self: &Arc<Self>) -> Result<()> {
let root_path = self.root_path().to_path_buf();
// create the directory for the filesystem on disk, if it doesn't exist
@@ -458,8 +453,7 @@ impl DiskFileSystemInner {
.concurrency_limited(&self.write_semaphore)
.await?;
- DiskWatcher::start_watching(self.clone(), report_invalidation_reason, poll_interval)
- .await?;
+ DiskWatcher::start_watching(self.clone()).await?;
Ok(())
}
@@ -506,19 +500,8 @@ impl DiskFileSystem {
.invalidate_path_and_children_with_reason(paths, reason);
}
- pub async fn start_watching(&self, poll_interval: Option<Duration>) -> Result<()> {
- self.inner
- .start_watching_internal(false, poll_interval)
- .await
- }
-
- pub async fn start_watching_with_invalidation_reason(
- &self,
- poll_interval: Option<Duration>,
- ) -> Result<()> {
- self.inner
- .start_watching_internal(true, poll_interval)
- .await
+ pub async fn start_watching(&self) -> Result<()> {
+ self.inner.start_watching_internal().await
}
pub async fn stop_watching(&self) {
@@ -719,24 +702,32 @@ pub(crate) fn format_absolute_fs_path(path: &Path, name: &str, root_path: &Path)
}
impl DiskFileSystem {
- /// Create a new instance of `DiskFileSystem`. `name` is a display name for the filesystem. This
- /// should be unique. `root` is the [canonicalized][std::fs::canonicalize] root of the
- /// filesystem.
+ /// Create a new instance of `DiskFileSystem`.
+ ///
+ /// `name` is a display name for the filesystem. This should be unique. `root` is the
+ /// [canonicalized][std::fs::canonicalize] root of the filesystem.
///
/// This API does not canonicalize itself, as that requires IO operations (e.g. symlink
/// resolution) which should (ideally) not be cached.
pub fn new(name: RcStr, root: Vc<RcStr>) -> Vc<Self> {
- Self::new_internal(name, root, Vec::new())
+ Self::new_internal(name, root, Vec::new(), DiskWatcherConfig::default())
}
- /// Create a new instance of `DiskFileSystem`. This is the same as [`DiskFileSystem::new`], but
- /// with the option to mark certain paths as not allowed to be accessed or navigated to.
+ /// Create a new instance of `DiskFileSystem`.
+ ///
+ /// `name` is a display name for the filesystem. This should be unique. `root` is the
+ /// [canonicalized][std::fs::canonicalize] root of the filesystem.
+ ///
+ /// This API does not canonicalize itself, as that requires IO operations (e.g. symlink
+ /// resolution) which should (ideally) not be cached.
///
- /// `denied_paths` must be normalized unix-style paths, non-empty and relative to the fs root.
- pub fn new_with_denied_paths(
+ /// `denied_paths` is a list of paths that are not allowed to be accessed or navigated to. These
+ /// must be normalized unix-style paths, non-empty and relative to the fs root.
+ pub fn new_with_options(
name: RcStr,
root: Vc<RcStr>,
denied_paths: Vec<RcStr>,
+ watcher_config: DiskWatcherConfig,
) -> Vc<Self> {
for denied_path in &denied_paths {
debug_assert!(!denied_path.is_empty(), "denied_path must not be empty");
@@ -745,7 +736,7 @@ impl DiskFileSystem {
"denied_path must be normalized: {denied_path:?}"
);
}
- Self::new_internal(name, root, denied_paths)
+ Self::new_internal(name, root, denied_paths, watcher_config)
}
}
@@ -756,6 +747,7 @@ impl DiskFileSystem {
name: RcStr,
root: Vc<RcStr>,
denied_paths: Vec<RcStr>,
+ watcher_config: DiskWatcherConfig,
) -> Result<Vc<Self>> {
let root = root.owned().await?;
let instance = DiskFileSystem {
@@ -768,7 +760,7 @@ impl DiskFileSystem {
dir_invalidator_map: InvalidatorMap::new(),
read_semaphore: create_read_semaphore(),
write_semaphore: create_write_semaphore(),
- watcher: DiskWatcher::new(),
+ watcher: DiskWatcher::new(watcher_config),
denied_paths,
turbo_tasks: turbo_tasks_weak(),
tokio_handle: Handle::current(),
diff --git a/turbopack/crates/turbo-tasks-fs/src/embed/dir.rs b/turbopack/crates/turbo-tasks-fs/src/embed/dir.rs
index c291c95d4d..4143219277 100644
--- a/turbopack/crates/turbo-tasks-fs/src/embed/dir.rs
+++ b/turbopack/crates/turbo-tasks-fs/src/embed/dir.rs
@@ -19,7 +19,7 @@ pub async fn directory_from_relative_path(
// development and the root directory is unlikely to move.
let root = canonicalize_to_rcstr(Path::new(&*path))?;
let disk_fs = DiskFileSystem::new(name, Vc::cell(root));
- disk_fs.await?.start_watching(None).await?;
+ disk_fs.await?.start_watching().await?;
Ok(Vc::upcast(disk_fs))
}
diff --git a/turbopack/crates/turbo-tasks-fs/src/lib.rs b/turbopack/crates/turbo-tasks-fs/src/lib.rs
index efc0f4e1fe..d0773b250a 100644
--- a/turbopack/crates/turbo-tasks-fs/src/lib.rs
+++ b/turbopack/crates/turbo-tasks-fs/src/lib.rs
@@ -60,6 +60,7 @@ pub use crate::{
path::{FileSystemPath, FileSystemPathOption, RealPathResult, RealPathResultError, rebase},
read_glob::ReadGlobResult,
virtual_fs::VirtualFileSystem,
+ watcher::{DiskWatcherConfig, DiskWatcherRecursiveMode},
windows::to_verbatim_with_case_folded_disk,
};
diff --git a/turbopack/crates/turbo-tasks-fs/src/watcher/mod.rs b/turbopack/crates/turbo-tasks-fs/src/watcher/mod.rs
index 7c733aca0e..40f9940333 100644
--- a/turbopack/crates/turbo-tasks-fs/src/watcher/mod.rs
+++ b/turbopack/crates/turbo-tasks-fs/src/watcher/mod.rs
@@ -15,10 +15,15 @@ use std::{
};
use anyhow::{Context, Result};
-use bincode::{Decode, Encode};
+use bincode::{
+ Decode, Encode,
+ de::Decoder,
+ enc::Encoder,
+ error::{DecodeError, EncodeError},
+};
use bitflags::bitflags;
use notify::{
- Config, EventKind, PollWatcher, RecommendedWatcher, RecursiveMode, Watcher,
+ Config, EventKind, PollWatcher, RecommendedWatcher, Watcher,
event::{MetadataKind, ModifyKind, RenameMode},
};
use rustc_hash::{FxHashMap, FxHashSet};
@@ -26,8 +31,8 @@ use tokio::sync::{RwLock, RwLockWriteGuard};
use tracing::instrument;
use turbo_rcstr::RcStr;
use turbo_tasks::{
- FxIndexSet, InvalidationReason, InvalidationReasonKind, Invalidator, TurboTasksApi,
- spawn_thread, util::StaticOrArc,
+ FxIndexSet, InvalidationReason, InvalidationReasonKind, Invalidator, NonLocalValue, TaskInput,
+ TurboTasksApi, spawn_thread, trace::TraceRawVcs, util::StaticOrArc,
};
use crate::{
@@ -38,33 +43,109 @@ use crate::{
watcher::fs_api::DiskFileSystemWatcherApi,
};
-static WATCH_RECURSIVE_MODE: LazyLock<RecursiveMode> = LazyLock::new(|| {
- match env::var("TURBO_TASKS_FORCE_WATCH_MODE").as_deref() {
- Ok("recursive") => {
- return RecursiveMode::Recursive;
- }
- Ok("nonrecursive") => {
- return RecursiveMode::NonRecursive;
- }
+/// Overrides [`DiskWatcherConfig::recursive_mode`]. Users shouldn't need to set this, this is
+/// intended only for debugging purposes.
+static FORCED_WATCH_RECURSIVE_MODE: LazyLock<Option<DiskWatcherRecursiveMode>> = LazyLock::new(
+ || match env::var("TURBO_TASKS_FORCE_WATCH_MODE").as_deref() {
+ Ok("recursive") => Some(DiskWatcherRecursiveMode::Recursive),
+ Ok("nonrecursive") => Some(DiskWatcherRecursiveMode::NonRecursive),
Ok(_) => {
eprintln!(
"unsupported `TURBO_TASKS_FORCE_WATCH_MODE`, must be `recursive` or `nonrecursive`"
);
+ None
+ }
+ _ => None,
+ },
+);
+
+#[derive(
+ Clone, Copy, Debug, Default, Eq, PartialEq, Hash, TraceRawVcs, NonLocalValue, Encode, Decode,
+)]
+pub struct DiskWatcherConfig {
+ /// Whether to let the [`notify::Watcher`] recurse into subdirectories itself, or to track and
+ /// watch each directory we care about ourselves.
+ ///
+ /// [`None`] picks a default based on the platform and [`Self::poll_interval`], which is
+ /// normally what you want.
+ ///
+ /// The `TURBO_TASKS_FORCE_WATCH_MODE` environment variable will override this configuration
+ /// value (intended for debugging purposes).
+ pub recursive_mode: Option<DiskWatcherRecursiveMode>,
+ /// Poll the filesystem at this interval instead of using the platform's native file watching,
+ /// for cases where native watching doesn't work (e.g. some Docker setups). This is slow and
+ /// inefficient, it should only be used as a last resort.
+ ///
+ /// [`None`] disables polling, using the platform's native watcher ([`RecommendedWatcher`])
+ /// instead of [`PollWatcher`].
+ pub poll_interval: Option<Duration>,
+ /// Attach an [`InvalidationReason`] to every invalidation the watcher causes ([`WatchStart`],
+ /// [`WatchChange`], or [`InvalidateRescan`]), so that it can be reported to the user.
+ ///
+ /// This costs an extra allocation per invalidated path, so it's only worth enabling when
+ /// something actually consumes the reasons.
+ pub report_invalidation_reason: bool,
+}
+
+impl TaskInput for DiskWatcherConfig {
+ fn is_transient(&self) -> bool {
+ false
+ }
+}
+
+/// Equivalent to [`notify::RecursiveMode`], but implements traits needed by [`turbo_tasks`].
+///
+/// When using [`Self::Recursive`], [`notify::Watcher`] will recursively track all contents
+/// of the filesystem root. This should only be used on platforms with efficient recursive watcher
+/// implementations (i.e. macOS and Windows).
+///
+/// When using [`Self::NonRecursive`], we only track previously read files and their parent
+/// directories.
+#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, TraceRawVcs, NonLocalValue, Encode, Decode)]
+pub enum DiskWatcherRecursiveMode {
+ Recursive,
+ NonRecursive,
+}
+
+impl From<DiskWatcherRecursiveMode> for notify::RecursiveMode {
+ fn from(value: DiskWatcherRecursiveMode) -> Self {
+ match value {
+ DiskWatcherRecursiveMode::Recursive => notify::RecursiveMode::Recursive,
+ DiskWatcherRecursiveMode::NonRecursive => notify::RecursiveMode::NonRecursive,
}
- _ => {}
}
- if cfg!(any(target_os = "macos", target_os = "windows")) {
- // these platforms have efficient recursive watchers, it's best to track the entire
- // directory and filter events to the files we care about
- RecursiveMode::Recursive
- } else {
- // inotify on linux is non-recursive, so notify-rs's implementation is inefficient, it's
- // better for us to just track it ourselves and only watch the files we know we care about
+}
+
+impl DiskWatcherConfig {
+ /// Resolves [`Self::recursive_mode`], falling back to a default based on
+ /// [`Self::poll_interval`] and the platform.
+ fn resolve_recursive_mode(&self) -> DiskWatcherRecursiveMode {
+ // macOS and Windows have efficient recursive watchers, so it's best to track the entire
+ // directory and filter events to the files we care about. inotify on Linux is
+ // non-recursive, so notify-rs's implementation is inefficient; better for us to track it
+ // ourselves and only watch the directories we know we care about.
//
- // See: https://github.com/vercel/turborepo/pull/4100
- RecursiveMode::NonRecursive
+ // See: <https://github.com/vercel/turborepo/pull/4100>
+ let platform_has_efficient_recursive_watcher =
+ cfg!(any(target_os = "macos", target_os = "windows"));
+
+ // the env var is a debugging escape hatch, so it wins over everything else
+ if let Some(forced) = *FORCED_WATCH_RECURSIVE_MODE {
+ forced
+ } else if let Some(recursive_mode) = self.recursive_mode {
+ recursive_mode
+ } else if self.poll_interval.is_some() {
+ // `PollWatcher` implements recursive watching by walking the entire subtree on every
+ // poll, so watching the fs root recursively would stat every file in the project each
+ // interval. Watching non-recursively keeps each poll to the directories we've read.
+ DiskWatcherRecursiveMode::NonRecursive
+ } else if platform_has_efficient_recursive_watcher {
+ DiskWatcherRecursiveMode::Recursive
+ } else {
+ DiskWatcherRecursiveMode::NonRecursive
+ }
}
-});
+}
/// How long to extend an invalidation batch by when receiving new events, before flushing. This
/// reduces invalidations if the same file or directory is modified many times.
@@ -75,35 +156,45 @@ const BATCH_DELAY: Duration = Duration::from_millis(10);
#[cfg(not(target_os = "linux"))]
const BATCH_DELAY: Duration = Duration::from_millis(1);
-#[derive(Encode, Decode)]
pub(crate) struct DiskWatcher {
- #[bincode(skip)]
state: State,
+ config: DiskWatcherConfig,
+}
+
+/// Only [`Self::config`] is serialized: a decoded [`DiskWatcher`] is always stopped.
+impl Encode for DiskWatcher {
+ fn encode<E: Encoder>(&self, encoder: &mut E) -> Result<(), EncodeError> {
+ self.config.encode(encoder)
+ }
}
+impl<Ctx> Decode<Ctx> for DiskWatcher {
+ fn decode<D: Decoder<Context = Ctx>>(decoder: &mut D) -> Result<Self, DecodeError> {
+ Ok(Self::new(DiskWatcherConfig::decode(decoder)?))
+ }
+}
+bincode::impl_borrow_decode!(DiskWatcher);
+
enum State {
// Note: Information about if we're a recursive or non-recursive watcher must live outside the
- // `RwLock` to allow us to quickly bail out on calls to `ensure_watched`.
+ // `RwLock` to allow us to quickly bail out before calling functions in
+ // `non_recursive_helpers`.
Recursive(RwLock<RecursiveState>),
NonRecursive(RwLock<NonRecursiveState>),
}
-impl Default for State {
- fn default() -> Self {
- State::new_stopped()
- }
-}
-
enum StateWriteGuard<'a> {
Recursive(RwLockWriteGuard<'a, RecursiveState>),
NonRecursive(RwLockWriteGuard<'a, NonRecursiveState>),
}
impl State {
- fn new_stopped() -> Self {
- match *WATCH_RECURSIVE_MODE {
- RecursiveMode::Recursive => Self::Recursive(RwLock::new(RecursiveState::Stopped)),
- RecursiveMode::NonRecursive => {
+ fn new_stopped(recursive_mode: DiskWatcherRecursiveMode) -> Self {
+ match recursive_mode {
+ DiskWatcherRecursiveMode::Recursive => {
+ Self::Recursive(RwLock::new(RecursiveState::Stopped))
+ }
+ DiskWatcherRecursiveMode::NonRecursive => {
Self::NonRecursive(RwLock::new(NonRecursiveState::Stopped))
}
}
@@ -116,16 +207,16 @@ impl State {
}
}
- fn recursive_mode(&self) -> RecursiveMode {
+ fn recursive_mode(&self) -> DiskWatcherRecursiveMode {
match self {
- Self::Recursive(_) => RecursiveMode::Recursive,
- Self::NonRecursive(_) => RecursiveMode::NonRecursive,
+ Self::Recursive(_) => DiskWatcherRecursiveMode::Recursive,
+ Self::NonRecursive(_) => DiskWatcherRecursiveMode::NonRecursive,
}
}
}
-/// Used by when [`WATCH_RECURSIVE_MODE`] is [`RecursiveMode::Recursive`] (default on macOS and
-/// Windows).
+/// Used when [`DiskWatcherConfig::recursive_mode`] returns [`RecursiveMode::Recursive`] (default on
+/// macOS and Windows when not polling).
enum RecursiveState {
/// Used when [`DiskWatcher::start_watching`] hasn't been called yet or after
/// [`DiskWatcher::stop_watching`] is called.
@@ -136,7 +227,8 @@ enum RecursiveState {
},
}
-/// Used by when [`WATCH_RECURSIVE_MODE`] is [`RecursiveMode::NonRecursive`] (default on Linux).
+/// Used when [`DiskWatcherConfig::recursive_mode`] returns [`RecursiveMode::NonRecursive`] (default
+/// on Linux, and everywhere when polling).
enum NonRecursiveState {
/// Used when [`DiskWatcher::start_watching`] hasn't been called yet or after
/// [`DiskWatcher::stop_watching`] is called.
@@ -164,7 +256,7 @@ enum NotifyWatcher {
}
impl NotifyWatcher {
- fn watch(&mut self, path: &Path, recursive_mode: RecursiveMode) -> notify::Result<()> {
+ fn watch(&mut self, path: &Path, recursive_mode: notify::RecursiveMode) -> notify::Result<()> {
match self {
Self::Recommended(watcher) => watcher.watch(path, recursive_mode),
Self::Polling(watcher) => watcher.watch(path, recursive_mode),
@@ -291,7 +383,7 @@ mod non_recursive_helpers {
) -> Result<()> {
debug_assert_ne!(dir_path, root_path);
- match notify_watcher.watch(dir_path, RecursiveMode::NonRecursive) {
+ match notify_watcher.watch(dir_path, notify::RecursiveMode::NonRecursive) {
Ok(())
| Err(notify::Error {
// The path was probably deleted before we could process the event, but the parent
@@ -358,17 +450,14 @@ mod non_recursive_helpers {
}
impl DiskWatcher {
- pub fn new() -> Self {
+ pub fn new(config: DiskWatcherConfig) -> Self {
Self {
- state: State::new_stopped(),
+ state: State::new_stopped(config.resolve_recursive_mode()),
+ config,
}
}
- pub async fn start_watching<FsApi: DiskFileSystemWatcherApi>(
- fs: Arc<FsApi>,
- report_invalidation_reason: bool,
- poll_interval: Option<Duration>,
- ) -> Result<()> {
+ pub async fn start_watching<FsApi: DiskFileSystemWatcherApi>(fs: Arc<FsApi>) -> Result<()> {
let watcher: &Self = fs.watcher();
let state_guard = watcher.state.write().await;
@@ -392,7 +481,7 @@ impl DiskWatcher {
// turbo-tasks-fs
let config = config.with_follow_symlinks(false);
- let mut notify_watcher = if let Some(poll_interval) = poll_interval {
+ let mut notify_watcher = if let Some(poll_interval) = watcher.config.poll_interval {
let config = config.with_poll_interval(poll_interval);
NotifyWatcher::Polling(PollWatcher::new(tx, config)?)
} else {
@@ -402,17 +491,16 @@ impl DiskWatcher {
// TOCTOU: we must watch `root_path` before calling any invalidators and setting up the
// watchers in their associated functions
let root_path = fs.root_path();
- let recursive_mode = match state_guard {
- StateWriteGuard::Recursive(_) => RecursiveMode::Recursive,
- StateWriteGuard::NonRecursive(_) => RecursiveMode::NonRecursive,
- };
- notify_watcher.watch(root_path, recursive_mode)?;
+ notify_watcher.watch(
+ root_path,
+ notify::RecursiveMode::from(watcher.state.recursive_mode()),
+ )?;
// We need to invalidate all reads or writes that happened before watching. As a
// side-effect, this will call `ensure_watched` again, setting up any watchers needed.
//
// Best is to start_watching before starting to read
- if report_invalidation_reason {
+ if watcher.config.report_invalidation_reason {
let name = fs.name().clone();
fs.invalidate_all_with_reason(|path| WatchStart {
name: name.clone(),
@@ -425,7 +513,7 @@ impl DiskWatcher {
spawn_thread({
let fs = fs.clone();
- move || Self::watch_thread(fs, rx, report_invalidation_reason)
+ move || Self::watch_thread(fs, rx)
});
// Updating `self.state` is done last. If we panic while setting up the watcher, it'll
@@ -463,10 +551,13 @@ impl DiskWatcher {
fn watch_thread<FsApi: DiskFileSystemWatcherApi>(
fs: Arc<FsApi>,
rx: Receiver<notify::Result<notify::Event>>,
- report_invalidation_reason: bool,
) {
let watcher: &Self = fs.watcher();
- let mut batch = BatchedInvalidations::new(watcher.state.recursive_mode());
+ let report_invalidation_reason = watcher.config.report_invalidation_reason;
+ let mut batch = BatchedInvalidations::new(
+ watcher.state.recursive_mode(),
+ watcher.config.poll_interval.is_some(),
+ );
'outer: loop {
let mut deadline: Option<Instant> = None;
@@ -644,16 +735,47 @@ struct BatchedInvalidations {
paths: FxHashMap<Box<Path>, InvalidationFlags>,
/// See [`Self::new_paths`]). Stored as [`None`] in non-recursive mode.
new_paths: Option<FxHashSet<Box<Path>>>,
+ /// Whether events are coming from [`PollWatcher`] instead of [`RecommendedWatcher`], which
+ /// changes how a file content change is reported. See [`Self::is_content_change`].
+ polling: bool,
}
impl BatchedInvalidations {
- fn new(recursive_mode: RecursiveMode) -> Self {
+ fn new(recursive_mode: DiskWatcherRecursiveMode, polling: bool) -> Self {
Self {
paths: FxHashMap::default(),
new_paths: match recursive_mode {
- RecursiveMode::NonRecursive => Some(FxHashSet::default()),
- RecursiveMode::Recursive => None,
+ DiskWatcherRecursiveMode::NonRecursive => Some(FxHashSet::default()),
+ DiskWatcherRecursiveMode::Recursive => None,
},
+ polling,
+ }
+ }
+
+ /// Whether a [`ModifyKind::Metadata`] event means the file's *contents* changed.
+ ///
+ /// Some backends don't report content changes as [`ModifyKind::Data`] at all, so we have to
+ /// treat one specific metadata change per backend as a content change. Accepting these
+ /// unconditionally would mean invalidating on every `chmod`/`touch` on the backends that do
+ /// report `Data` properly.
+ fn is_content_change(&self, kind: MetadataKind) -> bool {
+ match kind {
+ // `PollWatcher` detects changes by comparing mtimes, so a content change surfaces as a
+ // write-time change. It only emits `Data` when `Config::with_compare_contents` is
+ // enabled, which we don't do because hashing every watched file is too expensive.
+ MetadataKind::WriteTime => self.polling,
+ // fsevents does not always emit `kFSEventStreamEventFlagItemModified` for a content
+ // change; sometimes it only emits `kFSEventStreamEventFlagItemInodeMetaMod`, which
+ // notify maps to `MetadataKind::Any`. This causes redundant invalidations, but it's the
+ // only way to reliably detect content changes there. Fix for PACK-2437.
+ //
+ // libuv does the same thing to trigger `UV_CHANGES`:
+ // https://github.com/libuv/libuv/commit/73cf3600d75a5884b890a1a94048b8f3f9c66876
+ //
+ // inotify and ReadDirectoryChangesW both report content changes on their own, so
+ // `MetadataKind::Any` there only ever means an actual attribute change.
+ MetadataKind::Any => cfg!(target_os = "macos"),
+ _ => false,
}
}
@@ -700,18 +822,14 @@ impl BatchedInvalidations {
return false;
}
match event.kind {
- // [NOTE] Observing `ModifyKind::Metadata(MetadataKind::Any)` is not a mistake, fix for
- // PACK-2437.
- // In here explicitly subscribes to the `ModifyKind::Data` which indicates file content
- // changes - in case of fsevents backend, this is `kFSEventStreamEventFlagItemModified`.
- // Also meanwhile we subscribe to `ModifyKind::Metadata` as well.
- // This is due to in some cases fsevents does not emit explicit
- // `kFSEventStreamEventFlagItemModified` kernel events, but only emits
- // `kFSEventStreamEventFlagItemInodeMetaMod`. While this could cause redundant
- // invalidation, it's the way to reliably detect file content changes.
- // ref other implementation, i.e libuv does same thing to trigger `UV_CHANGES`
- // https://github.com/libuv/libuv/commit/73cf3600d75a5884b890a1a94048b8f3f9c66876
- EventKind::Modify(ModifyKind::Data(_) | ModifyKind::Metadata(MetadataKind::Any)) => {
+ EventKind::Modify(ModifyKind::Data(_)) => {
+ for path in paths {
+ self.mark(path.into_boxed_path(), InvalidationFlags::PATH);
+ }
+ true
+ }
+ // Some backends (fsevents, polling) can report metadata events for file content changes
+ EventKind::Modify(ModifyKind::Metadata(kind)) if self.is_content_change(kind) => {
for path in paths {
self.mark(path.into_boxed_path(), InvalidationFlags::PATH);
}
diff --git a/turbopack/crates/turbo-tasks-fuzz/src/fs_watcher.rs b/turbopack/crates/turbo-tasks-fuzz/src/fs_watcher.rs
index 826ae5e4dd..212efda935 100644
--- a/turbopack/crates/turbo-tasks-fuzz/src/fs_watcher.rs
+++ b/turbopack/crates/turbo-tasks-fuzz/src/fs_watcher.rs
@@ -129,7 +129,7 @@ pub async fn run(args: FsWatcher) -> anyhow::Result<()> {
};
if !args.start_watching_late {
- project_fs.await?.start_watching(None).await?;
+ project_fs.await?.start_watching().await?;
}
let symlink_count = if args.symlinks.is_some() {
@@ -175,7 +175,7 @@ pub async fn run(args: FsWatcher) -> anyhow::Result<()> {
invalidations.0.lock().unwrap().clear();
if args.start_watching_late {
- project_fs.await?.start_watching(None).await?;
+ project_fs.await?.start_watching().await?;
}
let mut rand_buf = [0; 16];
diff --git a/turbopack/crates/turbopack-cli/src/util.rs b/turbopack/crates/turbopack-cli/src/util.rs
index d14b300dac..df54a1db37 100644
--- a/turbopack/crates/turbopack-cli/src/util.rs
+++ b/turbopack/crates/turbopack-cli/src/util.rs
@@ -4,7 +4,7 @@ use anyhow::{Context, Result};
use bincode::{Decode, Encode};
use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{Vc, trace::TraceRawVcs};
-use turbo_tasks_fs::{DiskFileSystem, FileSystem, canonicalize_to_rcstr};
+use turbo_tasks_fs::{DiskFileSystem, DiskWatcherConfig, FileSystem, canonicalize_to_rcstr};
#[turbo_tasks::task_input]
#[derive(Clone, Debug, Hash, PartialEq, Eq, TraceRawVcs, Encode, Decode)]
@@ -56,13 +56,14 @@ pub async fn project_fs(
watch: bool,
denied_root_path: RcStr,
) -> Result<Vc<Box<dyn FileSystem>>> {
- let disk_fs = DiskFileSystem::new_with_denied_paths(
+ let disk_fs = DiskFileSystem::new_with_options(
rcstr!("project"),
Vc::cell(project_dir),
vec![denied_root_path],
+ DiskWatcherConfig::default(),
);
if watch {
- disk_fs.await?.start_watching(None).await?;
+ disk_fs.await?.start_watching().await?;
}
Ok(Vc::upcast(disk_fs))
}
diff --git a/turbopack/crates/turbopack/examples/turbopack.rs b/turbopack/crates/turbopack/examples/turbopack.rs
index ad4209bab0..256e0f31f2 100644
--- a/turbopack/crates/turbopack/examples/turbopack.rs
+++ b/turbopack/crates/turbopack/examples/turbopack.rs
@@ -37,7 +37,7 @@ async fn main() -> Result<()> {
Box::pin(async {
let root: RcStr = current_dir().unwrap().to_str().unwrap().into();
let disk_fs = DiskFileSystem::new(PROJECT_FILESYSTEM_NAME, Vc::cell(root));
- disk_fs.await?.start_watching(None).await?;
+ disk_fs.await?.start_watching().await?;
// Smart Pointer cast
let fs: Vc<Box<dyn FileSystem>> = Vc::upcast(disk_fs);
|