Skip to content

Commit ea3245b

Browse files
authored
[red-knot] Fix file watching for new non-project files (#16395)
1 parent 5925327 commit ea3245b

File tree

2 files changed

+49
-5
lines changed

2 files changed

+49
-5
lines changed

crates/red_knot/tests/file_watching.rs

Lines changed: 43 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")?;
@@ -1354,6 +1390,8 @@ mod unix {
13541390
);
13551391
assert_eq!(baz.file().path(case.db()).as_system_path(), Some(&*bar_baz));
13561392

1393+
assert_eq!(case.collect_project_files(), &[patched_bar_baz_file]);
1394+
13571395
// Write to the symlink target.
13581396
update_file(&patched_bar_baz, "def baz(): print('Version 2')")
13591397
.context("Failed to update bar/baz.py")?;
@@ -1389,6 +1427,7 @@ mod unix {
13891427
bar_baz_text = bar_baz_text.as_str()
13901428
);
13911429

1430+
assert_eq!(case.collect_project_files(), &[patched_bar_baz_file]);
13921431
Ok(())
13931432
}
13941433

@@ -1469,6 +1508,8 @@ mod unix {
14691508
Some(&*baz_original)
14701509
);
14711510

1511+
assert_eq!(case.collect_project_files(), &[]);
1512+
14721513
// Write to the symlink target.
14731514
update_file(&baz_original, "def baz(): print('Version 2')")
14741515
.context("Failed to update bar/baz.py")?;
@@ -1494,6 +1535,8 @@ mod unix {
14941535
"def baz(): print('Version 2')"
14951536
);
14961537

1538+
assert_eq!(case.collect_project_files(), &[]);
1539+
14971540
Ok(())
14981541
}
14991542
}

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)