Skip to content

Commit 21cd3aa

Browse files
fix: enhance PathManager to handle relative paths and set base directory (#11516)
1 parent 9e55f10 commit 21cd3aa

File tree

6 files changed

+64
-8
lines changed

6 files changed

+64
-8
lines changed

crates/rspack_fs/src/watcher/paths.rs

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{fmt::Debug, ops::Deref};
1+
use std::{fmt::Debug, ops::Deref, path::PathBuf};
22

33
use dashmap::{DashSet as HashSet, setref::multiple::RefMulti};
44
use rspack_error::Result;
@@ -110,6 +110,7 @@ impl<'a> PathAccessor<'a> {
110110
struct PathUpdater {
111111
pub added: Vec<ArcPath>,
112112
pub removed: Vec<ArcPath>,
113+
base_dir: PathBuf,
113114
}
114115

115116
impl<Added, Removed> From<(Added, Removed)> for PathUpdater
@@ -121,6 +122,7 @@ where
121122
Self {
122123
added: added.collect(),
123124
removed: removed.collect(),
125+
base_dir: std::env::current_dir().unwrap_or_default(),
124126
}
125127
}
126128
}
@@ -136,11 +138,25 @@ impl PathUpdater {
136138
continue; // Skip ignored paths
137139
}
138140

139-
watch_tracker.add(added);
141+
if added.is_absolute() {
142+
watch_tracker.add(added);
143+
continue;
144+
}
145+
146+
let added_absolute_path = self.base_dir.join(added.as_ref());
147+
148+
watch_tracker.add(ArcPath::from(added_absolute_path));
140149
}
141150

142151
for removed in removed_paths {
143-
watch_tracker.remove(removed);
152+
if removed.is_absolute() {
153+
watch_tracker.remove(removed);
154+
continue;
155+
}
156+
157+
let removed_absolute_path = self.base_dir.join(removed.as_ref());
158+
159+
watch_tracker.remove(ArcPath::from(removed_absolute_path));
144160
}
145161
Ok(())
146162
}
@@ -230,8 +246,6 @@ impl PathManager {
230246

231247
#[cfg(test)]
232248
mod tests {
233-
use std::path::PathBuf;
234-
235249
use rspack_paths::Utf8Path;
236250

237251
use super::*;
@@ -259,7 +273,11 @@ mod tests {
259273
let all = path_tracker.all;
260274

261275
assert_eq!(all.len(), 1);
262-
assert!(all.contains(&ArcPath::from(PathBuf::from("src/index.js"))));
276+
assert!(
277+
all
278+
.iter()
279+
.any(|p| p.to_string_lossy().contains("src/index.js"))
280+
)
263281
}
264282

265283
#[test]
@@ -290,7 +308,13 @@ mod tests {
290308

291309
all_paths.sort();
292310

293-
assert_eq!(all_paths, vec!["src", "src/index.js", "src/page/index.ts"]);
311+
assert_eq!(all_paths.len(), 3);
312+
313+
let should_exist_paths = vec!["src", "src/index.js", "src/page/index.ts"];
314+
315+
for path in should_exist_paths {
316+
assert!(all_paths.iter().any(|p| p.ends_with(path)));
317+
}
294318
}
295319

296320
#[test]
@@ -327,6 +351,12 @@ mod tests {
327351

328352
all_paths.sort();
329353

330-
assert_eq!(all_paths, vec!["src/", "src/index.js", "src/page/index.ts"]);
354+
assert_eq!(all_paths.len(), 3);
355+
356+
let should_exist_paths = vec!["src/", "src/index.js", "src/page/index.ts"];
357+
358+
for path in should_exist_paths {
359+
assert!(all_paths.iter().any(|p| p.ends_with(path)));
360+
}
331361
}
332362
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const a = 1;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log('111')
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const a = 2;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const path = require('node:path')
2+
3+
module.exports = function (source) {
4+
const depPath = path.resolve(this.context, '_module.js');
5+
const currentWorkDir = path.resolve('./');
6+
const relativePath = path.relative(currentWorkDir, depPath);
7+
this.addDependency(relativePath);
8+
return source;
9+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* @type {import('@rspack/core').Configuration}
3+
*/
4+
const config = {
5+
module: {
6+
rules: [
7+
{
8+
test: /index\.js$/,
9+
loader: require.resolve("./loader.js")
10+
}
11+
]
12+
}
13+
};
14+
module.exports = config;

0 commit comments

Comments
 (0)