1use std::ffi::OsString;
15use std::path::{Path, PathBuf};
16use std::time::Duration;
17
18use serde_json::Value;
19
20use super::{Layer, Project, Runner, STOPPED, keep_var};
21use crate::Job;
22
23const LOAD_TIMEOUT: Duration = Duration::from_secs(900);
24const MAX_JSON: u64 = 4 << 20;
26
27#[derive(Debug, Clone, Copy, PartialEq, Eq)]
28pub enum Kind {
29 Pixi,
30 Mise,
31}
32
33#[derive(Debug, Clone)]
35pub struct JsonEnvTool {
36 kind: Kind,
37 program: PathBuf,
38 state: Option<PathBuf>,
40}
41
42impl JsonEnvTool {
43 pub fn new(kind: Kind, program: PathBuf, data_dir: Option<&Path>) -> Self {
45 let name = match kind {
46 Kind::Pixi => "pixi",
47 Kind::Mise => "mise",
48 };
49 Self { kind, program, state: data_dir.map(|d| d.join(name)) }
50 }
51
52 pub fn program(&self) -> &Path {
53 &self.program
54 }
55
56 fn vars(&self, ws: &Path) -> Vec<(String, OsString)> {
58 let Some(s) = &self.state else { return Vec::new() };
59 let mut v: Vec<(String, OsString)> = match self.kind {
60 Kind::Pixi => vec![("PIXI_CACHE_DIR".into(), s.join("cache").into()), ("RATTLER_CACHE_DIR".into(), s.join("cache").into())],
61 Kind::Mise => vec![("MISE_DATA_DIR".into(), s.join("data").into()), ("MISE_CACHE_DIR".into(), s.join("cache").into())],
62 };
63 if self.kind == Kind::Mise {
64 v.push(("MISE_TRUSTED_CONFIG_PATHS".into(), ws.into()));
65 v.push(("MISE_YES".into(), "1".into()));
66 }
67 v
68 }
69
70 fn run_tool(&self, args: &[&str], ws: &Path, run: &dyn Runner, capture: bool) -> Result<Option<String>, String> {
73 let mut write = vec![ws.to_path_buf()];
74 write.extend(self.state.clone());
75 let t = ToolRun { program: &self.program, name: self.label_cmd(), cwd: ws, write, read: Vec::new(), env: self.vars(ws) };
76 run_tool(&t, args, run, capture)
77 }
78
79 fn label_cmd(&self) -> &'static str {
80 match self.kind {
81 Kind::Pixi => "pixi",
82 Kind::Mise => "mise",
83 }
84 }
85}
86
87pub(crate) struct ToolRun<'a> {
89 pub program: &'a Path,
90 pub name: &'a str,
92 pub cwd: &'a Path,
93 pub write: Vec<PathBuf>,
95 pub read: Vec<PathBuf>,
97 pub env: Vec<(String, OsString)>,
98}
99
100pub(crate) fn run_tool(t: &ToolRun<'_>, args: &[&str], run: &dyn Runner, capture: bool) -> Result<Option<String>, String> {
103 let tmp = byob_sandbox::TempDir::new("byob-env-")?;
104 let out_file = tmp.path().join("env.json");
105 let mut argv: Vec<OsString> = vec!["-c".into(), if capture { r#"exec "$@" > "$BYOB_OUT""# } else { r#"exec "$@""# }.into(), "sh".into()];
107 argv.push(t.program.as_os_str().to_owned());
108 argv.extend(args.iter().map(OsString::from));
109 let mut job = Job::new("sh".into(), argv, LOAD_TIMEOUT);
110 job.network = true;
111 job.big_memory = true;
112 for w in &t.write {
113 std::fs::create_dir_all(w).map_err(|e| format!("can't create {}: {e}", w.display()))?;
114 job.layer.write.push(w.clone());
115 }
116 job.layer.read.extend(t.read.iter().cloned());
117 job.env.clone_from(&t.env);
118 job.env.push(("BYOB_OUT".into(), out_file.clone().into_os_string()));
119 let out = run.run(job, t.cwd, tmp.path())?;
120 if out.cancelled {
121 return Err(STOPPED.into());
122 }
123 if !out.success() {
124 let err = out.stderr.text();
125 return Err(format!("`{} {}` failed: {}", t.name, args.join(" "), err.trim()));
126 }
127 if !capture {
128 return Ok(None);
129 }
130 let size = std::fs::metadata(&out_file).map(|m| m.len()).unwrap_or(0);
131 if size > MAX_JSON {
132 return Err(format!("the environment is too large ({size} bytes)"));
133 }
134 std::fs::read_to_string(&out_file).map(Some).map_err(|e| format!("no environment from {}: {e}", t.name))
135}
136
137impl Project for JsonEnvTool {
138 fn id(&self) -> &str {
139 self.label_cmd()
140 }
141
142 fn label(&self) -> &str {
143 match self.kind {
144 Kind::Pixi => "pixi environment",
145 Kind::Mise => "mise tools",
146 }
147 }
148
149 fn file(&self, ws: &Path) -> Option<String> {
150 project_file(self.kind, ws).map(str::to_owned)
151 }
152
153 fn inputs(&self) -> Vec<String> {
154 let files: &[&str] = match self.kind {
155 Kind::Pixi => &["pixi.toml", "pixi.lock", "pyproject.toml"],
156 Kind::Mise => &["mise.toml", ".mise.toml", ".tool-versions", "mise.lock"],
157 };
158 files.iter().map(|f| (*f).to_owned()).collect()
159 }
160
161 fn load(&self, ws: &Path, _dir: &Path, run: &dyn Runner) -> Result<Layer, String> {
162 let text = match self.kind {
163 Kind::Pixi => self.run_tool(&["shell-hook", "--json"], ws, run, true)?,
164 Kind::Mise => {
165 self.run_tool(&["install"], ws, run, false)?;
166 self.run_tool(&["env", "--json"], ws, run, true)?
167 }
168 };
169 let host: Vec<PathBuf> = std::env::var_os("PATH").map(|p| std::env::split_paths(&p).collect()).unwrap_or_default();
170 let mut layer = parse_env(self.kind, text.as_deref().unwrap_or_default(), &host)?;
171 layer.read.extend(self.state.clone());
173 Ok(layer)
174 }
175}
176
177pub fn project_file(kind: Kind, ws: &Path) -> Option<&'static str> {
179 match kind {
180 Kind::Pixi => {
181 if ws.join("pixi.toml").is_file() {
182 return Some("pixi.toml");
183 }
184 let py = std::fs::read_to_string(ws.join("pyproject.toml")).ok()?;
185 py.lines().any(|l| l.trim_start().starts_with("[tool.pixi")).then_some("pyproject.toml")
186 }
187 Kind::Mise => ["mise.toml", ".mise.toml", ".tool-versions"].into_iter().find(|f| ws.join(f).is_file()),
188 }
189}
190
191pub fn parse_env(kind: Kind, text: &str, host: &[PathBuf]) -> Result<Layer, String> {
194 let v: Value = serde_json::from_str(text).map_err(|e| format!("bad environment JSON: {e}"))?;
195 let vars = match kind {
196 Kind::Pixi => v.get("environment_variables"),
197 Kind::Mise => Some(&v),
198 }
199 .and_then(Value::as_object)
200 .ok_or("the environment JSON has no variables")?;
201 let mut l = Layer::default();
202 for (k, val) in vars {
203 let Some(val) = val.as_str() else { continue };
204 if k == "PATH" {
205 for p in std::env::split_paths(val) {
206 if p.is_absolute() && !host.contains(&p) && !l.path.contains(&p) {
207 l.path.push(p);
208 }
209 }
210 } else if !keep_var(k) && !k.is_empty() && !k.contains(['=', '\0']) && !val.contains('\0') {
211 l.vars.push((k.clone(), val.to_owned()));
212 }
213 }
214 Ok(l)
215}
216
217#[cfg(test)]
218mod tests {
219 use super::*;
220
221 #[cfg(unix)]
222 #[test]
223 fn pixi_and_mise_json() {
224 let host = vec![PathBuf::from("/usr/bin"), PathBuf::from("/bin")];
225 let pixi = r#"{"environment_variables":{"CONDA_PREFIX":"/w/.pixi/envs/default","PIXI_PROJECT_NAME":"t","PATH":"/w/.pixi/envs/default/bin:/usr/bin:/bin","HOME":"/x"}}"#;
226 let l = parse_env(Kind::Pixi, pixi, &host).unwrap();
227 assert_eq!(l.path, vec![PathBuf::from("/w/.pixi/envs/default/bin")]);
228 assert_eq!(l.var("CONDA_PREFIX"), Some("/w/.pixi/envs/default"));
229 assert_eq!(l.var("HOME"), None);
230 let mise = r#"{"GREETING":"hi","PATH":"/d/installs/node/22/bin:/usr/bin","N":1}"#;
231 let l = parse_env(Kind::Mise, mise, &host).unwrap();
232 assert_eq!(l.path, vec![PathBuf::from("/d/installs/node/22/bin")]);
233 assert_eq!(l.vars, vec![("GREETING".to_owned(), "hi".to_owned())]);
234 assert!(parse_env(Kind::Pixi, "{}", &host).is_err() && parse_env(Kind::Mise, "[1]", &host).is_err() && parse_env(Kind::Mise, "x", &host).is_err());
235 }
236
237 #[test]
238 fn project_files() {
239 let d = tempfile::tempdir().unwrap();
240 assert_eq!(project_file(Kind::Pixi, d.path()), None);
241 assert_eq!(project_file(Kind::Mise, d.path()), None);
242 std::fs::write(d.path().join("pyproject.toml"), "[project]\nname = \"x\"\n").unwrap();
243 assert_eq!(project_file(Kind::Pixi, d.path()), None);
244 std::fs::write(d.path().join("pyproject.toml"), "[project]\nname = \"x\"\n\n[tool.pixi.workspace]\nchannels = []\n").unwrap();
245 assert_eq!(project_file(Kind::Pixi, d.path()), Some("pyproject.toml"));
246 std::fs::write(d.path().join("pixi.toml"), "").unwrap();
247 assert_eq!(project_file(Kind::Pixi, d.path()), Some("pixi.toml"));
248 std::fs::write(d.path().join(".tool-versions"), "nodejs 22\n").unwrap();
249 assert_eq!(project_file(Kind::Mise, d.path()), Some(".tool-versions"));
250 }
251}