Skip to content

Commit 44a53b7

Browse files
committed
[red-knot] Fix file watching for new non-project files
1 parent bf2c9a4 commit 44a53b7

File tree

2 files changed

+54
-5
lines changed

2 files changed

+54
-5
lines changed

crates/red_knot/tests/file_watching.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,41 @@ fn new_ignored_file() -> anyhow::Result<()> {
462462
Ok(())
463463
}
464464

465+
#[test]
466+
fn new_non_project_file() -> anyhow::Result<()> {
467+
let mut case = setup_with_options([("bar.py", "")], |context| {
468+
Some(Options {
469+
environment: Some(EnvironmentOptions {
470+
extra_paths: Some(vec![RelativePathBuf::cli(
471+
context.join_root_path("site_packages"),
472+
)]),
473+
..EnvironmentOptions::default()
474+
}),
475+
..Options::default()
476+
})
477+
})?;
478+
479+
let bar_path = case.project_path("bar.py");
480+
let bar_file = case.system_file(&bar_path).unwrap();
481+
482+
assert_eq!(&case.collect_project_files(), &[bar_file]);
483+
484+
// Add a file to site packages
485+
let black_path = case.root_path().join("site_packages/black.py");
486+
std::fs::write(black_path.as_std_path(), "print('Hello')")?;
487+
488+
let changes = case.stop_watch(event_for_file("black.py"));
489+
490+
case.apply_changes(changes);
491+
492+
assert!(case.system_file(&black_path).is_ok());
493+
494+
// The file should not have been added to the project files
495+
assert_eq!(&case.collect_project_files(), &[bar_file]);
496+
497+
Ok(())
498+
}
499+
465500
#[test]
466501
fn changed_file() -> anyhow::Result<()> {
467502
let foo_source = "print('Hello, world!')";
@@ -1075,6 +1110,7 @@ fn hard_links_in_project() -> anyhow::Result<()> {
10751110

10761111
assert_eq!(source_text(case.db(), foo).as_str(), "print('Version 1')");
10771112
assert_eq!(source_text(case.db(), bar).as_str(), "print('Version 1')");
1113+
assert_eq!(case.collect_project_files(), &[bar, foo]);
10781114

10791115
// Write to the hard link target.
10801116
update_file(foo_path, "print('Version 2')").context("Failed to update foo.py")?;
@@ -1268,6 +1304,8 @@ mod unix {
12681304
Some(&*baz_project)
12691305
);
12701306

1307+
assert_eq!(case.collect_project_files(), &[baz.file()]);
1308+
12711309
let baz_original = case.root_path().join("bar/baz.py");
12721310

12731311
// Write to the symlink target.
@@ -1283,6 +1321,8 @@ mod unix {
12831321
"def baz(): print('Version 2')"
12841322
);
12851323

1324+
assert_eq!(case.collect_project_files(), &[baz.file()]);
1325+
12861326
// Write to the symlink source.
12871327
update_file(baz_project, "def baz(): print('Version 3')")
12881328
.context("Failed to update bar/baz.py")?;
@@ -1295,6 +1335,7 @@ mod unix {
12951335
source_text(case.db(), baz.file()).as_str(),
12961336
"def baz(): print('Version 3')"
12971337
);
1338+
assert_eq!(case.collect_project_files(), &[baz.file()]);
12981339

12991340
Ok(())
13001341
}
@@ -1354,6 +1395,8 @@ mod unix {
13541395
);
13551396
assert_eq!(baz.file().path(case.db()).as_system_path(), Some(&*bar_baz));
13561397

1398+
assert_eq!(case.collect_project_files(), &[patched_bar_baz_file]);
1399+
13571400
// Write to the symlink target.
13581401
update_file(&patched_bar_baz, "def baz(): print('Version 2')")
13591402
.context("Failed to update bar/baz.py")?;
@@ -1389,6 +1432,7 @@ mod unix {
13891432
bar_baz_text = bar_baz_text.as_str()
13901433
);
13911434

1435+
assert_eq!(case.collect_project_files(), &[patched_bar_baz_file]);
13921436
Ok(())
13931437
}
13941438

@@ -1469,6 +1513,8 @@ mod unix {
14691513
Some(&*baz_original)
14701514
);
14711515

1516+
assert_eq!(case.collect_project_files(), &[]);
1517+
14721518
// Write to the symlink target.
14731519
update_file(&baz_original, "def baz(): print('Version 2')")
14741520
.context("Failed to update bar/baz.py")?;
@@ -1494,6 +1540,8 @@ mod unix {
14941540
"def baz(): print('Version 2')"
14951541
);
14961542

1543+
assert_eq!(case.collect_project_files(), &[]);
1544+
14971545
Ok(())
14981546
}
14991547
}

crates/red_knot_project/src/db/changes.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -208,11 +208,12 @@ impl ProjectDatabase {
208208
return WalkState::Continue;
209209
}
210210

211-
if entry
212-
.path()
213-
.extension()
214-
.and_then(PySourceType::try_from_extension)
215-
.is_some()
211+
if entry.path().starts_with(&project_path)
212+
&& entry
213+
.path()
214+
.extension()
215+
.and_then(PySourceType::try_from_extension)
216+
.is_some()
216217
{
217218
let mut paths = added_paths.lock().unwrap();
218219

0 commit comments

Comments
 (0)