Skip to content

Commit d940b24

Browse files
authored
[ty] Watch script dependencies in CLI watch mode (#28125)
## Summary This PR implements watching for script search paths. Before, changes to files in a script's search path were not picked up by ty, because the virtual environment is typically outside the `project_root` (at least when created by uv) and ty didn't explicitly watch the search paths for changes. This PR extends the CLI's watcher to also register script search paths as watched folders (while still deduplicating watched paths as before). The one caveat of registering script search paths is that we need to update the search paths after every edit because editing a script metadata block could change the script's search paths (e.g. when a user adds a new `tool.ty.environment` block). To make this as cheap as possible, computing the watch paths is a salsa query and we compute a `cache_key` that allows us to bail early (and extremely cheaply) if the search paths remain unchanged. I plan to make the same change for the LSP, but it's a bit more work because the LSP watcher simply watches the project folder. We never implemented the necessary dynamic registration to watch other search paths (e.g. extra paths outside the project etc). I plan to tackle this next. ## Test Plan Before, changes to a script's search path were not detected by ty: https://github.com/user-attachments/assets/06cfe9b8-fa74-4437-aeee-6f54b80d6a5a Now, changes are recognized and trigger a recheck of the script https://github.com/user-attachments/assets/2685261b-306d-4129-b238-a3eca2c88713
1 parent fe9f065 commit d940b24

4 files changed

Lines changed: 460 additions & 77 deletions

File tree

‎crates/ty/src/lib.rs‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -505,10 +505,11 @@ impl MainLoop {
505505
let scripts: Vec<_> = db.project().script_files(db).iter().collect();
506506
self.synchronize_scripts(db, &scripts);
507507
}
508-
509-
if let Some(watcher) = self.watcher.as_mut() {
510-
watcher.update(db);
511-
}
508+
}
509+
if !changes.is_empty()
510+
&& let Some(watcher) = self.watcher.as_mut()
511+
{
512+
watcher.update(db);
512513
}
513514
request_check(&check_sender);
514515
}

‎crates/ty/tests/file_watching.rs‎

Lines changed: 308 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,16 @@ impl TestCase {
189189
}
190190

191191
fn apply_changes(&mut self, changes: &[ChangeEvent]) -> ChangeResult {
192-
self.db.apply_changes(changes)
192+
let result = self.db.apply_changes(changes);
193+
194+
if !changes.is_empty()
195+
&& let Some(watcher) = &mut self.watcher
196+
{
197+
watcher.update(&mut self.db);
198+
assert!(!watcher.has_errored_paths());
199+
}
200+
201+
result
193202
}
194203

195204
fn update_options(&mut self, options: Options) -> anyhow::Result<()> {
@@ -206,11 +215,6 @@ impl TestCase {
206215
let changes = self.take_watch_changes(event_for_file("pyproject.toml"));
207216
self.apply_changes(&changes);
208217

209-
if let Some(watcher) = &mut self.watcher {
210-
watcher.update(&self.db);
211-
assert!(!watcher.has_errored_paths());
212-
}
213-
214218
Ok(())
215219
}
216220

@@ -506,7 +510,7 @@ where
506510
let watcher = directory_watcher(move |events| sender.send(events).unwrap())
507511
.with_context(|| "Failed to create directory watcher")?;
508512

509-
let watcher = ProjectWatcher::new(watcher, &db);
513+
let watcher = ProjectWatcher::new(watcher, &mut db);
510514
assert!(!watcher.has_errored_paths());
511515

512516
let test_case = TestCase {
@@ -1504,6 +1508,246 @@ fn remove_search_path() -> anyhow::Result<()> {
15041508
Ok(())
15051509
}
15061510

1511+
#[test]
1512+
fn script_dependency_changes() -> anyhow::Result<()> {
1513+
let mut case = setup(|context: &mut SetupContext| {
1514+
context.write_file("dependencies/dependency.py", "value = 1")?;
1515+
context.write_project_file(
1516+
"script.py",
1517+
r#"
1518+
# /// script
1519+
# requires-python = ">=3.12"
1520+
# [tool.ty.environment]
1521+
# extra-paths = ["../dependencies"]
1522+
# ///
1523+
from dependency import value
1524+
result: int = value
1525+
"#,
1526+
)
1527+
})?;
1528+
let dependency = case.root_path().join("dependencies/dependency.py");
1529+
assert!(case.db().check().is_empty());
1530+
1531+
update_file(&dependency, "value = 'wrong'")?;
1532+
let changes = case.stop_watch(event_for_file("dependency.py"));
1533+
case.apply_changes(&changes);
1534+
1535+
let diagnostics = case.db().check();
1536+
assert_eq!(diagnostics.len(), 1);
1537+
assert_eq!(diagnostics[0].id().as_str(), "invalid-assignment");
1538+
Ok(())
1539+
}
1540+
1541+
#[test]
1542+
fn shared_script_search_paths_remain_watched_until_unused() -> anyhow::Result<()> {
1543+
let mut case = setup(|context: &mut SetupContext| {
1544+
context.write_file("dependencies/dependency.py", "value = 1")?;
1545+
let script = r#"
1546+
# /// script
1547+
# requires-python = ">=3.12"
1548+
# [tool.ty.environment]
1549+
# extra-paths = ["../dependencies"]
1550+
# ///
1551+
"#;
1552+
context.write_project_file("first.py", script)?;
1553+
context.write_project_file("second.py", script)
1554+
})?;
1555+
let dependency = case.root_path().join("dependencies/dependency.py");
1556+
1557+
update_file(case.project_path("first.py"), "")?;
1558+
let changes = case.take_watch_changes(event_for_file("first.py"));
1559+
case.apply_changes(&changes);
1560+
1561+
update_file(&dependency, "value = 2")?;
1562+
let changes = case.take_watch_changes(event_for_file("dependency.py"));
1563+
assert!(
1564+
changes
1565+
.iter()
1566+
.any(|event| matches!(event, ChangeEvent::Changed { path, .. } if path == &dependency)),
1567+
"expected an edit while the search path is shared: {changes:?}"
1568+
);
1569+
1570+
update_file(case.project_path("second.py"), "")?;
1571+
let changes = case.take_watch_changes(event_for_file("second.py"));
1572+
case.apply_changes(&changes);
1573+
1574+
update_file(&dependency, "value = 3")?;
1575+
1576+
// Neither script uses this path now, so edits to it should no longer produce watch events.
1577+
let changes = case.try_stop_watch(event_for_file("dependency.py"), Duration::from_millis(100));
1578+
1579+
assert_eq!(changes, Err(vec![]));
1580+
Ok(())
1581+
}
1582+
1583+
#[test]
1584+
fn restoring_script_metadata_rewatches_search_path() -> anyhow::Result<()> {
1585+
let script = r#"
1586+
# /// script
1587+
# requires-python = ">=3.12"
1588+
# [tool.ty.environment]
1589+
# extra-paths = ["../dependencies"]
1590+
# ///
1591+
"#;
1592+
let mut case = setup(|context: &mut SetupContext| {
1593+
context.write_file("dependencies/dependency.py", "value = 1")?;
1594+
context.write_project_file("script.py", script)
1595+
})?;
1596+
let dependency = case.root_path().join("dependencies/dependency.py");
1597+
1598+
// Removing the script block removes its extra search path from the project.
1599+
update_file(case.project_path("script.py"), "")?;
1600+
let changes = case.take_watch_changes(event_for_file("script.py"));
1601+
case.apply_changes(&changes);
1602+
1603+
// Restoring the script re-registers its search path.
1604+
update_file(case.project_path("script.py"), script)?;
1605+
let changes = case.take_watch_changes(event_for_file("script.py"));
1606+
case.apply_changes(&changes);
1607+
1608+
update_file(&dependency, "value = 2")?;
1609+
let changes = case.stop_watch(event_for_file("dependency.py"));
1610+
assert!(
1611+
changes
1612+
.iter()
1613+
.any(|event| matches!(event, ChangeEvent::Changed { path, .. } if path == &dependency)),
1614+
"expected an edit after restoring the script's search path: {changes:?}"
1615+
);
1616+
Ok(())
1617+
}
1618+
1619+
#[test]
1620+
fn nested_search_path_picks_up_missed_dependency_edit() -> anyhow::Result<()> {
1621+
let mut case = setup(|context: &mut SetupContext| {
1622+
context.write_file("dependencies/pkg/dependency.py", "value = 1")?;
1623+
context.write_project_file(
1624+
"main.py",
1625+
r"
1626+
from pkg.dependency import value
1627+
result: int = value
1628+
",
1629+
)?;
1630+
context.write_project_file(
1631+
"pyproject.toml",
1632+
r#"
1633+
[tool.ty.environment]
1634+
extra-paths = ["../dependencies"]
1635+
"#,
1636+
)
1637+
})?;
1638+
let dependency = case.root_path().join("dependencies/pkg/dependency.py");
1639+
1640+
// The first check caches the dependency through the parent search path.
1641+
assert!(case.db().check().is_empty());
1642+
1643+
// Stop watching the parent before editing the dependency, so ty misses that file event.
1644+
update_file(case.project_path("pyproject.toml"), "[tool.ty]\n")?;
1645+
let changes = case.take_watch_changes(event_for_file("pyproject.toml"));
1646+
case.apply_changes(&changes);
1647+
1648+
update_file(&dependency, "value = 'wrong'")?;
1649+
1650+
// The new search path is nested inside the former watch. Registering it must refresh
1651+
// known files beneath it, even though that exact directory was never watched before.
1652+
update_file(
1653+
case.project_path("pyproject.toml"),
1654+
r#"
1655+
[tool.ty.environment]
1656+
extra-paths = ["../dependencies/pkg"]
1657+
"#,
1658+
)?;
1659+
let changes = case.take_watch_changes(event_for_file("pyproject.toml"));
1660+
case.apply_changes(&changes);
1661+
1662+
// Import relative to the new search path and check that ty sees the missed edit.
1663+
update_file(
1664+
case.project_path("main.py"),
1665+
r"
1666+
from dependency import value
1667+
result: int = value
1668+
",
1669+
)?;
1670+
let changes = case.take_watch_changes(event_for_file("main.py"));
1671+
case.apply_changes(&changes);
1672+
1673+
let diagnostics = case.db().check();
1674+
assert_eq!(diagnostics.len(), 1);
1675+
assert_eq!(diagnostics[0].id().as_str(), "invalid-assignment");
1676+
Ok(())
1677+
}
1678+
1679+
#[test]
1680+
fn pth_edit_while_unwatched_watches_new_search_path() -> anyhow::Result<()> {
1681+
let site_packages = if cfg!(windows) {
1682+
"venv/Lib/site-packages"
1683+
} else {
1684+
"venv/lib/python3.12/site-packages"
1685+
};
1686+
let project_config = r#"
1687+
[tool.ty.environment]
1688+
python = "../venv"
1689+
"#;
1690+
let mut case = setup(|context: &mut SetupContext| {
1691+
let python_home = context.join_root_path("base/bin");
1692+
context.write_file("base/bin/python", "")?;
1693+
context.write_file(
1694+
"venv/pyvenv.cfg",
1695+
&format!(
1696+
r"home = {python_home}
1697+
version = 3.12
1698+
"
1699+
),
1700+
)?;
1701+
context.write_file(
1702+
format!("{site_packages}/dependency.pth"),
1703+
context.join_root_path("old").as_str(),
1704+
)?;
1705+
context.write_file("old/dependency.py", "value = 1")?;
1706+
context.write_file("new/dependency.py", "value = 1")?;
1707+
context.write_project_file(
1708+
"main.py",
1709+
r"
1710+
from dependency import value
1711+
result: int = value
1712+
",
1713+
)?;
1714+
context.write_project_file("pyproject.toml", project_config)
1715+
})?;
1716+
1717+
// The initial import resolves through `old`, as named by `dependency.pth`.
1718+
assert!(case.db().check().is_empty());
1719+
1720+
// Stop watching the virtual environment before changing its `.pth` file.
1721+
update_file(case.project_path("pyproject.toml"), "[tool.ty]\n")?;
1722+
let changes = case.take_watch_changes(event_for_file("pyproject.toml"));
1723+
case.apply_changes(&changes);
1724+
1725+
// This edit has no watcher event, so the cached `.pth` contents still name `old`.
1726+
let new = case.root_path().join("new");
1727+
update_file(
1728+
case.root_path().join(site_packages).join("dependency.pth"),
1729+
new.as_str(),
1730+
)?;
1731+
1732+
// Restoring the environment initially sees the cached `old` path. Rewatching
1733+
// `site-packages` must refresh `.pth` and register `new` in the same update.
1734+
update_file(case.project_path("pyproject.toml"), project_config)?;
1735+
let changes = case.take_watch_changes(event_for_file("pyproject.toml"));
1736+
case.apply_changes(&changes);
1737+
1738+
// Receiving this edit proves that `new` is now watched.
1739+
let dependency = new.join("dependency.py");
1740+
update_file(&dependency, "value = 2")?;
1741+
let changes = case.stop_watch(event_for_file("dependency.py"));
1742+
assert!(
1743+
changes
1744+
.iter()
1745+
.any(|event| matches!(event, ChangeEvent::Changed { path, .. } if path == &dependency)),
1746+
"expected an edit at the new search path: {changes:?}"
1747+
);
1748+
Ok(())
1749+
}
1750+
15071751
#[test]
15081752
fn change_python_version_and_platform() -> anyhow::Result<()> {
15091753
let mut case = setup(|context: &mut SetupContext| {
@@ -2621,10 +2865,14 @@ mod uv_metadata {
26212865
use ruff_db::diagnostic::DiagnosticId;
26222866
use ruff_db::files::File;
26232867
use ruff_db::system::{OsSystem, System as _};
2868+
use ty_module_resolver::system_module_search_paths;
26242869
use ty_project::{Db, ScriptEnvironmentAvailability, UseUv, UvSyncChanges, uv_test_env_vars};
2870+
use ty_python_semantic::Db as _;
26252871
use ty_static::EnvVars;
26262872

2627-
use super::{SetupContext, TestCase, event_for_file, setup_with_system, update_file};
2873+
use super::{
2874+
ChangeEvent, SetupContext, TestCase, event_for_file, setup_with_system, update_file,
2875+
};
26282876

26292877
const MANIFEST: &str = r#"
26302878
[project]
@@ -2871,6 +3119,56 @@ mod uv_metadata {
28713119
Ok(())
28723120
}
28733121

3122+
#[test]
3123+
fn script_pth_adds_watched_search_path() -> anyhow::Result<()> {
3124+
let mut case = setup_uv(
3125+
UseUv::Scripts,
3126+
&[
3127+
(
3128+
"script.py",
3129+
r#"
3130+
# /// script
3131+
# dependencies = []
3132+
# requires-python = ">=3.12"
3133+
# ///
3134+
from dependency import value
3135+
result: int = value
3136+
"#,
3137+
),
3138+
("../dependencies/dependency.py", "value = 1"),
3139+
],
3140+
)?;
3141+
let db = case.db();
3142+
let script = case.system_file(case.project_path("script.py"))?;
3143+
let environment = db.program_file(script).program(db).resolver_environment(db);
3144+
let site_packages = system_module_search_paths(db, environment)
3145+
.find(|path| path.file_name() == Some("site-packages"))
3146+
.context("script environment has no site-packages")?
3147+
.to_path_buf();
3148+
let pth = site_packages.join("dependency.pth");
3149+
let diagnostics = case.db().check();
3150+
assert_eq!(diagnostics.len(), 1);
3151+
assert_eq!(diagnostics[0].id().as_str(), "unresolved-import");
3152+
3153+
let dependencies = case.root_path().join("dependencies");
3154+
std::fs::write(pth.as_std_path(), dependencies.as_str())?;
3155+
let changes = case.take_watch_changes(event_for_file("dependency.pth"));
3156+
case.apply_changes(&changes);
3157+
3158+
assert!(case.db().check().is_empty());
3159+
3160+
let dependency = dependencies.join("dependency.py");
3161+
update_file(&dependency, "value = 2")?;
3162+
let changes = case.stop_watch(event_for_file("dependency.py"));
3163+
assert!(
3164+
changes.iter().any(
3165+
|event| matches!(event, ChangeEvent::Changed { path, .. } if path == &dependency)
3166+
),
3167+
"expected an edit at the new search path: {changes:?}"
3168+
);
3169+
Ok(())
3170+
}
3171+
28743172
fn setup_uv(use_uv: UseUv, files: &[(&str, &str)]) -> anyhow::Result<TestCase> {
28753173
let uv = OsSystem::default().which("uv")?;
28763174
let mut case = setup_with_system(
@@ -2962,10 +3260,10 @@ mod uv_metadata {
29623260
changes.project = completed.project.or(changes.project);
29633261
}
29643262

2965-
if changes.project.is_some()
3263+
if !changes.is_empty()
29663264
&& let Some(watcher) = &mut case.watcher
29673265
{
2968-
watcher.update(&case.db);
3266+
watcher.update(&mut case.db);
29693267
assert!(!watcher.has_errored_paths());
29703268
}
29713269

‎crates/ty_project/src/uv/environments.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,7 @@ fn apply_sync_result(
733733
// previous metadata was cleared along with its virtual-environment path.
734734
//
735735
// FIXME: This is overbroad. A file watcher can tell us precisely what changed.
736+
// Remove this fallback once the language server also watches script environments.
736737
// Changes inside virtual environments should instead be watched and processed through `ProjectDatabase::apply_changes`.
737738
// Using a file watcher also ensures that virtual environment changes in
738739
// scripts without using uv are detected.

0 commit comments

Comments
 (0)