Skip to content

Commit be4777c

Browse files
[ty] Fix site-package error when multiple versions of pythons are installed in system path (#25769)
Co-authored-by: Micha Reiser <[email protected]>
1 parent 53f6ff7 commit be4777c

11 files changed

Lines changed: 713 additions & 188 deletions

File tree

‎Cargo.lock‎

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Cargo.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ salsa = { version = "0.27.0", default-features = false, features = [
163163
"salsa_unstable",
164164
"inventory",
165165
] }
166+
same-file = { version = "1.0.6" }
166167
schemars = { version = "1.0.4" }
167168
seahash = { version = "4.1.0" }
168169
serde = { version = "1.0.197", features = ["derive"] }

‎crates/ruff_db/Cargo.toml‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ pathdiff = { workspace = true }
3737
quick-junit = { workspace = true, optional = true }
3838
rustc-hash = { workspace = true }
3939
salsa = { workspace = true }
40+
same-file = { workspace = true, optional = true }
4041
schemars = { workspace = true, optional = true }
4142
serde = { workspace = true, optional = true }
4243
serde_json = { workspace = true, optional = true }
@@ -61,7 +62,7 @@ tempfile = { workspace = true }
6162
[features]
6263
cache = ["ruff_cache"]
6364
junit = ["dep:quick-junit"]
64-
os = ["ignore", "dep:etcetera", "dep:which"]
65+
os = ["ignore", "dep:etcetera", "dep:same-file", "dep:which"]
6566
serde = [
6667
"camino/serde1",
6768
"dep:serde",

‎crates/ruff_db/src/system.rs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ pub trait System: Debug + Sync + Send {
6565
/// See [dunce::canonicalize] for more information.
6666
fn canonicalize_path(&self, path: &SystemPath) -> Result<SystemPathBuf>;
6767

68+
/// Returns `true` if both paths refer to the same file.
69+
fn is_same_file(&self, first: &SystemPath, second: &SystemPath) -> Result<bool>;
70+
6871
/// Returns the source type for `path` if known or `None`.
6972
///
7073
/// The default is to always return `None`, assuming the system

‎crates/ruff_db/src/system/os.rs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ impl System for OsSystem {
9393
})
9494
}
9595

96+
fn is_same_file(&self, first: &SystemPath, second: &SystemPath) -> Result<bool> {
97+
same_file::is_same_file(first.as_std_path(), second.as_std_path())
98+
}
99+
96100
fn read_to_string(&self, path: &SystemPath) -> Result<String> {
97101
std::fs::read_to_string(path.as_std_path())
98102
}

‎crates/ruff_db/src/system/test.rs‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ impl System for TestSystem {
101101
self.system().canonicalize_path(path)
102102
}
103103

104+
fn is_same_file(&self, first: &SystemPath, second: &SystemPath) -> Result<bool> {
105+
self.system().is_same_file(first, second)
106+
}
107+
104108
fn read_to_string(&self, path: &SystemPath) -> Result<String> {
105109
self.system().read_to_string(path)
106110
}
@@ -360,6 +364,12 @@ impl System for InMemorySystem {
360364
self.memory_fs.canonicalize(path)
361365
}
362366

367+
fn is_same_file(&self, first: &SystemPath, second: &SystemPath) -> Result<bool> {
368+
// The in-memory file system does not support hard links, so canonical paths uniquely
369+
// identify files.
370+
Ok(self.canonicalize_path(first)? == self.canonicalize_path(second)?)
371+
}
372+
363373
fn read_to_string(&self, path: &SystemPath) -> Result<String> {
364374
self.memory_fs.read_to_string(path)
365375
}

‎crates/ty_server/src/system.rs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,10 @@ impl System for LSPSystem {
177177
self.native_system.canonicalize_path(path)
178178
}
179179

180+
fn is_same_file(&self, first: &SystemPath, second: &SystemPath) -> Result<bool> {
181+
self.native_system.is_same_file(first, second)
182+
}
183+
180184
fn path_exists_case_sensitive(&self, path: &SystemPath, prefix: &SystemPath) -> bool {
181185
self.native_system.path_exists_case_sensitive(path, prefix)
182186
}

‎crates/ty_site_packages/Cargo.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ tracing = { workspace = true }
3232

3333
[dev-dependencies]
3434
ruff_db = { workspace = true, features = ["testing", "os"] }
35+
tempfile = { workspace = true }
3536

3637
[lints]
3738
workspace = true

0 commit comments

Comments
 (0)