Skip to content

Commit 02532a5

Browse files
committed
make DiskFileSystem::new not session dependent
1 parent f1e52d5 commit 02532a5

File tree

10 files changed

+45
-29
lines changed

10 files changed

+45
-29
lines changed

crates/next-api/src/project.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,9 @@ impl ProjectContainer {
223223
let project = self.project();
224224
let project_fs = project.project_fs().strongly_consistent().await?;
225225
if watch.enable {
226-
project_fs.start_watching_with_invalidation_reason(watch.poll_interval)?;
226+
project_fs
227+
.start_watching_with_invalidation_reason(watch.poll_interval)
228+
.await?;
227229
} else {
228230
project_fs.invalidate_with_reason();
229231
}
@@ -304,7 +306,9 @@ impl ProjectContainer {
304306
if !ReadRef::ptr_eq(&prev_project_fs, &project_fs) {
305307
if watch.enable {
306308
// TODO stop watching: prev_project_fs.stop_watching()?;
307-
project_fs.start_watching_with_invalidation_reason(watch.poll_interval)?;
309+
project_fs
310+
.start_watching_with_invalidation_reason(watch.poll_interval)
311+
.await?;
308312
} else {
309313
project_fs.invalidate_with_reason();
310314
}

turbopack/crates/node-file-trace/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ impl Args {
190190
async fn create_fs(name: &str, root: &str, watch: bool) -> Result<Vc<Box<dyn FileSystem>>> {
191191
let fs = DiskFileSystem::new(name.into(), root.into(), vec![]);
192192
if watch {
193-
fs.await?.start_watching(None)?;
193+
fs.await?.start_watching(None).await?;
194194
} else {
195195
fs.await?.invalidate_with_reason();
196196
}

turbopack/crates/turbo-tasks-fs/examples/hash_directory.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ async fn main() -> Result<()> {
3232
Box::pin(async {
3333
let root = current_dir().unwrap().to_str().unwrap().into();
3434
let disk_fs = DiskFileSystem::new("project".into(), root, vec![]);
35-
disk_fs.await?.start_watching(None)?;
35+
disk_fs.await?.start_watching(None).await?;
3636

3737
// Smart Pointer cast
3838
let fs: Vc<Box<dyn FileSystem>> = Vc::upcast(disk_fs);

turbopack/crates/turbo-tasks-fs/examples/hash_glob.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ async fn main() -> Result<()> {
2929
Box::pin(async {
3030
let root = current_dir().unwrap().to_str().unwrap().into();
3131
let disk_fs = DiskFileSystem::new("project".into(), root, vec![]);
32-
disk_fs.await?.start_watching(None)?;
32+
disk_fs.await?.start_watching(None).await?;
3333

3434
// Smart Pointer cast
3535
let fs: Vc<Box<dyn FileSystem>> = Vc::upcast(disk_fs);

turbopack/crates/turbo-tasks-fs/src/embed/dir.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub async fn directory_from_relative_path(
1212
path: RcStr,
1313
) -> Result<Vc<Box<dyn FileSystem>>> {
1414
let disk_fs = DiskFileSystem::new(name, path, vec![]);
15-
disk_fs.await?.start_watching(None)?;
15+
disk_fs.await?.start_watching(None).await?;
1616

1717
Ok(Vc::upcast(disk_fs))
1818
}

turbopack/crates/turbo-tasks-fs/src/embed/file.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub async fn content_from_relative_path(
2323
root_path.to_string_lossy().into(),
2424
vec![],
2525
);
26-
disk_fs.await?.start_watching(None)?;
26+
disk_fs.await?.start_watching(None).await?;
2727

2828
let fs_path = disk_fs.root().join(path.into());
2929
Ok(fs_path.read())

turbopack/crates/turbo-tasks-fs/src/invalidator_map.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,19 @@ pub struct InvalidatorMap {
1212
map: Mutex<HashMap<String, HashSet<Invalidator>>>,
1313
}
1414

15-
impl InvalidatorMap {
16-
pub fn new() -> Self {
15+
impl Default for InvalidatorMap {
16+
fn default() -> Self {
1717
Self {
1818
queue: ConcurrentQueue::unbounded(),
1919
map: Default::default(),
2020
}
2121
}
22+
}
23+
24+
impl InvalidatorMap {
25+
pub fn new() -> Self {
26+
Self::default()
27+
}
2228

2329
pub fn lock(&self) -> LockResult<MutexGuard<'_, HashMap<String, HashSet<Invalidator>>>> {
2430
let mut guard = self.map.lock()?;

turbopack/crates/turbo-tasks-fs/src/lib.rs

+23-17
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,9 @@ use std::{
2828
borrow::Cow,
2929
cmp::min,
3030
collections::HashSet,
31-
fmt::{
32-
Debug, Display, Formatter, Write as _, {self},
33-
},
31+
fmt::{self, Debug, Display, Formatter, Write as _},
3432
fs::FileType,
35-
io::{
36-
BufRead, ErrorKind, {self},
37-
},
33+
io::{self, BufRead, ErrorKind},
3834
mem::take,
3935
path::{Path, PathBuf, MAIN_SEPARATOR},
4036
sync::Arc,
@@ -62,8 +58,8 @@ use tokio::{
6258
};
6359
use tracing::Instrument;
6460
use turbo_tasks::{
65-
mark_session_dependent, trace::TraceRawVcs, Completion, Invalidator, RcStr, ReadRef,
66-
ResolvedVc, ValueToString, Vc,
61+
mark_session_dependent, mark_stateful, trace::TraceRawVcs, Completion, Invalidator, RcStr,
62+
ReadRef, ResolvedVc, ValueToString, Vc,
6763
};
6864
use turbo_tasks_hash::{
6965
hash_xxh3_hash128, hash_xxh3_hash64, DeterministicHash, DeterministicHasher,
@@ -207,8 +203,10 @@ pub struct DiskFileSystem {
207203
#[serde(skip)]
208204
mutex_map: MutexMap<PathBuf>,
209205
#[turbo_tasks(debug_ignore, trace_ignore)]
206+
#[serde(skip)]
210207
invalidator_map: Arc<InvalidatorMap>,
211208
#[turbo_tasks(debug_ignore, trace_ignore)]
209+
#[serde(skip)]
212210
dir_invalidator_map: Arc<InvalidatorMap>,
213211
/// Lock that makes invalidation atomic. It will keep a write lock during
214212
/// watcher invalidation and a read lock during other operations.
@@ -309,27 +307,37 @@ impl DiskFileSystem {
309307
});
310308
}
311309

312-
pub fn start_watching(&self, poll_interval: Option<Duration>) -> Result<()> {
313-
self.start_watching_internal(false, poll_interval)
310+
pub async fn start_watching(&self, poll_interval: Option<Duration>) -> Result<()> {
311+
self.start_watching_internal(false, poll_interval).await
314312
}
315313

316-
pub fn start_watching_with_invalidation_reason(
314+
pub async fn start_watching_with_invalidation_reason(
317315
&self,
318316
poll_interval: Option<Duration>,
319317
) -> Result<()> {
320-
self.start_watching_internal(true, poll_interval)
318+
self.start_watching_internal(true, poll_interval).await
321319
}
322320

323-
fn start_watching_internal(
321+
#[tracing::instrument(level = "info", name = "start filesystem watching", skip_all, fields(path = %self.root))]
322+
async fn start_watching_internal(
324323
&self,
325324
report_invalidation_reason: bool,
326325
poll_interval: Option<Duration>,
327326
) -> Result<()> {
328-
let _span = tracing::info_span!("start filesystem watching", path = &*self.root).entered();
329327
let invalidator_map = self.invalidator_map.clone();
330328
let dir_invalidator_map = self.dir_invalidator_map.clone();
331329
let root_path = self.root_path().to_path_buf();
332330

331+
// create the directory for the filesystem on disk, if it doesn't exist
332+
retry_future(|| {
333+
let path = root_path.as_path();
334+
fs::create_dir_all(&root_path).instrument(tracing::info_span!(
335+
"create root directory",
336+
path = display(path.display())
337+
))
338+
})
339+
.await?;
340+
333341
let report_invalidation_reason =
334342
report_invalidation_reason.then(|| (self.name.clone(), root_path.clone()));
335343
let invalidation_lock = self.invalidation_lock.clone();
@@ -419,9 +427,7 @@ impl DiskFileSystem {
419427
/// ignore specific subpaths from each.
420428
#[turbo_tasks::function]
421429
pub async fn new(name: RcStr, root: RcStr, ignored_subpaths: Vec<RcStr>) -> Result<Vc<Self>> {
422-
mark_session_dependent();
423-
// create the directory for the filesystem on disk, if it doesn't exist
424-
fs::create_dir_all(&root).await?;
430+
mark_stateful();
425431

426432
let instance = DiskFileSystem {
427433
name,

turbopack/crates/turbopack-cli/src/util.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,13 @@ pub fn normalize_entries(entries: &Option<Vec<String>>) -> Vec<RcStr> {
6262
#[turbo_tasks::function]
6363
pub async fn project_fs(project_dir: RcStr) -> Result<Vc<Box<dyn FileSystem>>> {
6464
let disk_fs = DiskFileSystem::new("project".into(), project_dir, vec![]);
65-
disk_fs.await?.start_watching(None)?;
65+
disk_fs.await?.start_watching(None).await?;
6666
Ok(Vc::upcast(disk_fs))
6767
}
6868

6969
#[turbo_tasks::function]
7070
pub async fn output_fs(project_dir: RcStr) -> Result<Vc<Box<dyn FileSystem>>> {
7171
let disk_fs = DiskFileSystem::new("output".into(), project_dir, vec![]);
72-
disk_fs.await?.start_watching(None)?;
72+
disk_fs.await?.start_watching(None).await?;
7373
Ok(Vc::upcast(disk_fs))
7474
}

turbopack/crates/turbopack/examples/turbopack.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ async fn main() -> Result<()> {
3434
Box::pin(async {
3535
let root: RcStr = current_dir().unwrap().to_str().unwrap().into();
3636
let disk_fs = DiskFileSystem::new(PROJECT_FILESYSTEM_NAME.into(), root, vec![]);
37-
disk_fs.await?.start_watching(None)?;
37+
disk_fs.await?.start_watching(None).await?;
3838

3939
// Smart Pointer cast
4040
let fs: Vc<Box<dyn FileSystem>> = Vc::upcast(disk_fs);

0 commit comments

Comments
 (0)