|
| 1 | +//! Minimal project file detection for MCP tools. |
| 2 | +//! |
| 3 | +//! Walks up from a start path looking for `pyproject.toml` or `pixi.toml`. |
| 4 | +//! Used to determine whether dep management should target a project file |
| 5 | +//! (via `pixi add` / `uv add`) or notebook inline metadata. |
| 6 | +
|
| 7 | +use std::path::{Path, PathBuf}; |
| 8 | + |
| 9 | +/// The type of project file detected. |
| 10 | +#[derive(Debug, Clone, PartialEq, Eq)] |
| 11 | +pub enum ProjectFileKind { |
| 12 | + PyprojectToml, |
| 13 | + PixiToml, |
| 14 | +} |
| 15 | + |
| 16 | +/// A detected project file with its path and kind. |
| 17 | +#[derive(Debug, Clone)] |
| 18 | +pub struct DetectedProjectFile { |
| 19 | + pub path: PathBuf, |
| 20 | + pub kind: ProjectFileKind, |
| 21 | +} |
| 22 | + |
| 23 | +impl DetectedProjectFile { |
| 24 | + /// The package manager name (for `detect_package_manager` compatibility). |
| 25 | + pub fn manager(&self) -> &'static str { |
| 26 | + match self.kind { |
| 27 | + ProjectFileKind::PyprojectToml => "uv", |
| 28 | + ProjectFileKind::PixiToml => "pixi", |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + /// The daemon env_source string for this project type. |
| 33 | + pub fn env_source(&self) -> &'static str { |
| 34 | + match self.kind { |
| 35 | + ProjectFileKind::PyprojectToml => "uv:pyproject", |
| 36 | + ProjectFileKind::PixiToml => "pixi:toml", |
| 37 | + } |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +/// Detect the nearest project file by walking up from `start_path`. |
| 42 | +/// |
| 43 | +/// Checks each directory for `pyproject.toml` and `pixi.toml`. |
| 44 | +/// A `pyproject.toml` with `[tool.pixi]` is treated as a pixi project. |
| 45 | +/// Stops at `.git` boundaries or the user's home directory. |
| 46 | +pub fn detect_project_file(start_path: &Path) -> Option<DetectedProjectFile> { |
| 47 | + let start_dir = if start_path.is_file() { |
| 48 | + start_path.parent()? |
| 49 | + } else { |
| 50 | + start_path |
| 51 | + }; |
| 52 | + |
| 53 | + let home_dir = dirs::home_dir(); |
| 54 | + let mut current = start_dir.to_path_buf(); |
| 55 | + |
| 56 | + loop { |
| 57 | + // Check pyproject.toml first (higher priority in tiebreaker) |
| 58 | + let pyproject = current.join("pyproject.toml"); |
| 59 | + if pyproject.exists() { |
| 60 | + // If it has [tool.pixi], treat as pixi project |
| 61 | + if has_pixi_section(&pyproject) { |
| 62 | + return Some(DetectedProjectFile { |
| 63 | + path: pyproject, |
| 64 | + kind: ProjectFileKind::PixiToml, |
| 65 | + }); |
| 66 | + } |
| 67 | + return Some(DetectedProjectFile { |
| 68 | + path: pyproject, |
| 69 | + kind: ProjectFileKind::PyprojectToml, |
| 70 | + }); |
| 71 | + } |
| 72 | + |
| 73 | + // Check pixi.toml |
| 74 | + let pixi = current.join("pixi.toml"); |
| 75 | + if pixi.exists() { |
| 76 | + return Some(DetectedProjectFile { |
| 77 | + path: pixi, |
| 78 | + kind: ProjectFileKind::PixiToml, |
| 79 | + }); |
| 80 | + } |
| 81 | + |
| 82 | + // Stop at home directory or git repo root |
| 83 | + if let Some(ref home) = home_dir { |
| 84 | + if current == *home { |
| 85 | + return None; |
| 86 | + } |
| 87 | + } |
| 88 | + if current.join(".git").exists() { |
| 89 | + return None; |
| 90 | + } |
| 91 | + |
| 92 | + match current.parent() { |
| 93 | + Some(parent) if parent != current => { |
| 94 | + current = parent.to_path_buf(); |
| 95 | + } |
| 96 | + _ => return None, |
| 97 | + } |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +/// Check if a pyproject.toml contains a `[tool.pixi]` section. |
| 102 | +fn has_pixi_section(path: &Path) -> bool { |
| 103 | + std::fs::read_to_string(path) |
| 104 | + .ok() |
| 105 | + .is_some_and(|content| content.contains("[tool.pixi]") || content.contains("[tool.pixi.")) |
| 106 | +} |
0 commit comments