improve Python environment activation (#118)

This commit is contained in:
Josh Thomas 2025-04-30 12:34:20 -05:00 committed by GitHub
parent 3b7ffe0b70
commit c09d6541ba
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 669 additions and 290 deletions

View file

@ -16,3 +16,6 @@ tokio = { workspace = true }
tower-lsp-server = { workspace = true }
percent-encoding = "2.3"
[build-dependencies]
pyo3-build-config = { workspace = true, features = ["resolve-config"] }

View file

@ -0,0 +1,45 @@
/// Build script to configure linking against the Python library.
///
/// This script is necessary when the crate's code, particularly tests
/// that interact with the Python interpreter via `pyo3` (e.g., using
/// `Python::with_gil`, importing modules, calling Python functions),
/// needs symbols from the Python C API at link time.
///
/// It uses `pyo3-build-config` to detect the Python installation and
/// prints the required `cargo:rustc-link-search` and `cargo:rustc-link-lib`
/// directives to Cargo, enabling the linker to find and link against the
/// appropriate Python library (e.g., libpythonX.Y.so).
///
/// It also adds an RPATH linker argument on Unix-like systems so the
/// resulting test executable can find the Python library at runtime.
///
/// Note: Each crate whose test target requires Python linking needs its
/// own `build.rs` with this logic.
fn main() {
println!("cargo:rerun-if-changed=build.rs");
// Set up #[cfg] flags first (useful for conditional compilation)
pyo3_build_config::use_pyo3_cfgs();
// Get the Python interpreter configuration directly
let config = pyo3_build_config::get();
// Add the library search path if available
if let Some(lib_dir) = &config.lib_dir {
println!("cargo:rustc-link-search=native={}", lib_dir);
// Add RPATH linker argument for Unix-like systems (Linux, macOS)
// This helps the test executable find the Python library at runtime.
#[cfg(not(windows))]
println!("cargo:rustc-link-arg=-Wl,-rpath,{}", lib_dir);
} else {
println!("cargo:warning=Python library directory not found in config.");
}
// Add the library link directive if available
if let Some(lib_name) = &config.lib_name {
println!("cargo:rustc-link-lib=dylib={}", lib_name);
} else {
println!("cargo:warning=Python library name not found in config.");
}
}

View file

@ -29,13 +29,12 @@ fn uri_to_pathbuf(uri: &Uri) -> Option<PathBuf> {
// Decode the percent-encoded path string
let decoded_path_cow = percent_decode_str(encoded_path_str).decode_utf8_lossy();
let path_str = decoded_path_cow.as_ref();
#[cfg(unix)]
let path_str = decoded_path_cow.as_ref();
#[cfg(windows)]
let path_str = {
// Remove leading '/' for paths like /C:/...
path_str.strip_prefix('/').unwrap_or(path_str)
};
// Remove leading '/' for paths like /C:/...
let path_str = path_str.strip_prefix('/').unwrap_or(path_str);
Some(PathBuf::from(path_str))
}