Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions crates/decode/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use nickel_lang_core::{
program::Program,
};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::collections::{BTreeMap, HashMap};
use std::path::Path;

mod load;
Expand Down Expand Up @@ -431,8 +431,8 @@ pub(crate) fn patches_from_term(
) -> Result<EnvPatches, Error> {
let patch_rt = eval_if_closure(rt, program)?;

let mut dirs: Option<HashMap<String, PatchSetting>> = None;
let mut files: Option<HashMap<String, PatchSetting>> = None;
let mut dirs: Option<BTreeMap<String, PatchSetting>> = None;
let mut files: Option<BTreeMap<String, PatchSetting>> = None;

let Some(r) = record_data_from_val(&patch_rt) else {
return Err(Error::unexpected_type(
Expand Down Expand Up @@ -486,7 +486,7 @@ pub(crate) fn patches_from_term(
))
},
)
.collect::<Result<HashMap<_, _>, Error>>()?,
.collect::<Result<BTreeMap<_, _>, Error>>()?,
);
Ok(())
}
Expand Down Expand Up @@ -529,7 +529,7 @@ pub(crate) fn patches_from_term(
))
},
)
.collect::<Result<HashMap<_, _>, Error>>()?,
.collect::<Result<BTreeMap<_, _>, Error>>()?,
);
Ok(())
}
Expand Down
84 changes: 72 additions & 12 deletions crates/graph/src/env_setup.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::{
collections::{HashMap, HashSet},
collections::{BTreeMap, BTreeSet},
path::PathBuf,
};

Expand All @@ -12,9 +12,9 @@ use mfile::EnvPatches;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SetupForPackages {
/// Environment variables that need to be set, typically for an env_state_wiring entry.
pub env_vars: HashMap<String, String>,
pub env_vars: BTreeMap<String, String>,
/// Directories that should be created in `/state`, typically from an env_state_wiring entry.
pub state_dirs: HashSet<String>,
pub state_dirs: BTreeSet<String>,

/// Whether any package sets `needs.dns`.
pub needs_dns: bool,
Expand All @@ -28,7 +28,7 @@ pub struct SetupForPackages {
///
/// Consumed when a mapping can't be honoured, so the error can name the
/// package to go and look at rather than only the path it landed on.
pub fs_mapping_packages: HashMap<String, String>,
pub fs_mapping_packages: BTreeMap<String, String>,
}

impl SetupForPackages {
Expand All @@ -44,10 +44,10 @@ impl SetupForPackages {
i: I,
) -> Result<SetupForPackages, std::io::Error> {
let mut patch = EnvPatches::default();
let mut fs_mapping_packages: HashMap<String, String> = Default::default();
let mut fs_mapping_packages: BTreeMap<String, String> = Default::default();
let (mut needs_dns, mut needs_internet) = (false, false);
let mut env_vars: HashMap<String, String> = Default::default();
let mut state_dirs = HashSet::default();
let mut env_vars: BTreeMap<String, String> = Default::default();
let mut state_dirs = BTreeSet::default();

for dep in i.into_iter() {
let b = g.get(dep).unwrap();
Expand Down Expand Up @@ -184,10 +184,10 @@ mod tests {
needs_internet: false,
state_dirs: Default::default(),
fs_mappings: EnvPatches {
dir: HashMap::from_iter([("~/.claude".to_string(), PatchSetting::ReadWrite)]),
file: HashMap::new(),
dir: BTreeMap::from_iter([("~/.claude".to_string(), PatchSetting::ReadWrite)]),
file: BTreeMap::new(),
},
fs_mapping_packages: HashMap::from_iter([(
fs_mapping_packages: BTreeMap::from_iter([(
"~/.claude".to_string(),
"b1".to_string()
)]),
Expand Down Expand Up @@ -329,16 +329,76 @@ mod tests {
assert_eq!(
SetupForPackages::build(&g, [g.by_name("b1").unwrap()]).unwrap(),
SetupForPackages {
env_vars: HashMap::from_iter([
env_vars: BTreeMap::from_iter([
("GOCACHE".to_string(), "/state/gocache".to_string(),),
("GOMODCACHE".to_string(), "/state/gomodcache".to_string(),)
]),
needs_dns: false,
needs_internet: false,
state_dirs: HashSet::from_iter(["gocache".to_string(), "gomodcache".to_string()]),
state_dirs: BTreeSet::from_iter(["gocache".to_string(), "gomodcache".to_string()]),
fs_mappings: Default::default(),
fs_mapping_packages: Default::default(),
}
)
}

/// The same packages walked in opposite orders yield the same env-var,
/// fs-mapping and package-attribution key sequences and the same
/// state-dir set: the ordered collections take their order from their
/// keys, not from the walk, so the sandbox setup is stable run to run.
#[test]
fn build_is_walk_order_independent() {
let layer = Layer::new_for_test(
indoc! {
"
let {BuildSpec, ..} = import \"minimal.ncl\" in

let
b1 = {
name = \"b1\",
build_deps = [],
cmd = \"\",
attrs = {
env_state_wiring = [{ env_var = \"AVAR\", prefix = \"adir\" }],
env_dir_mappings = [{ read_only = false, path = \"~/a\", class = 'State }],
},
} | BuildSpec,
b2 = {
name = \"b2\",
build_deps = [],
cmd = \"\",
attrs = {
env_state_wiring = [{ env_var = \"BVAR\", prefix = \"bdir\" }],
env_dir_mappings = [{ read_only = false, path = \"~/b\", class = 'State }],
},
} | BuildSpec,
in
[b1, b2]
"
}
.to_string(),
)
.unwrap_or_else(|e| {
e.report_to_stderr();
panic!("spec parsing failed");
});

let g = Graph::new().ingest(layer).unwrap();
let (b1, b2) = (g.by_name("b1").unwrap(), g.by_name("b2").unwrap());

let forward = SetupForPackages::build(&g, [b1, b2]).unwrap();
let reverse = SetupForPackages::build(&g, [b2, b1]).unwrap();

let keys = |m: &BTreeMap<String, String>| m.keys().cloned().collect::<Vec<_>>();
assert_eq!(keys(&forward.env_vars), keys(&reverse.env_vars));
assert_eq!(
keys(&forward.fs_mapping_packages),
keys(&reverse.fs_mapping_packages)
);
assert_eq!(
forward.fs_mappings.dir.keys().collect::<Vec<_>>(),
reverse.fs_mappings.dir.keys().collect::<Vec<_>>()
);
assert_eq!(forward.state_dirs, reverse.state_dirs);
}
}
8 changes: 4 additions & 4 deletions crates/mctx/src/env.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::{
collections::{HashMap, HashSet},
collections::{BTreeMap, HashMap, HashSet},
fs::Permissions,
io::Write,
os::unix::{fs::PermissionsExt, net::UnixStream},
Expand Down Expand Up @@ -571,7 +571,7 @@ pub fn interpolate_task_strings(
/// Names whoever put a patch path into the environment: the package that
/// declared it, or — for a path that arrived through the task's own `patch`
/// table rather than a package attribute — the task itself.
fn declared_by(packages: &HashMap<String, String>, declared: &str, task: &str) -> String {
fn declared_by(packages: &BTreeMap<String, String>, declared: &str, task: &str) -> String {
match packages.get(declared) {
Some(p) => format!("package `{p}`"),
None => format!("task `{task}`"),
Expand Down Expand Up @@ -694,7 +694,7 @@ impl<'a> Env<'a> {
// the expanded form, so its "create mapped file" failures name a path
// nobody wrote down; this puts the package and its `~/`-rooted
// declaration back into the message.
let declarations: HashMap<String, &String> = fs_mapping_packages
let declarations: BTreeMap<String, &String> = fs_mapping_packages
.keys()
.filter_map(|declared| {
Some((
Expand Down Expand Up @@ -990,7 +990,7 @@ mod tests {
#[test]
fn declared_by_names_the_package_then_the_task() {
let packages =
HashMap::from_iter([("~/.claude.json".to_string(), "claude-code".to_string())]);
BTreeMap::from_iter([("~/.claude.json".to_string(), "claude-code".to_string())]);
assert_eq!(
declared_by(&packages, "~/.claude.json", "test"),
"package `claude-code`"
Expand Down
48 changes: 41 additions & 7 deletions crates/mfile/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,11 +232,11 @@ pub struct EnvPatches {
/// The list of directories to be patched into the environment. Keys are expected to be path strings,
/// either absolute paths are starting with the prefix `~/` to be rooted in the users home directory.
#[serde(default, alias = "dirs")]
pub dir: HashMap<String, PatchSetting>,
pub dir: BTreeMap<String, PatchSetting>,
/// The list of files to be patched into the environment. Keys are expected to be path strings,
/// either absolute paths are starting with the prefix `~/` to be rooted in the users home directory.
#[serde(default, alias = "files")]
pub file: HashMap<String, PatchSetting>,
pub file: BTreeMap<String, PatchSetting>,
}

/// Why a `~/`-rooted patch path had no home directory to expand against.
Expand Down Expand Up @@ -312,8 +312,6 @@ impl EnvPatches {
) -> Result<Vec<common::FsMapping>, PatchHomeError> {
let mut mappings = Vec::with_capacity(self.dir.len() + self.file.len());
for (is_file, entries) in [(false, &self.dir), (true, &self.file)] {
let mut entries: Vec<_> = entries.iter().collect();
entries.sort_by_key(|(path, _)| *path);
for (path, settings) in entries {
mappings.push(common::FsMapping {
host_path: Self::expand_home(path, home)?,
Expand Down Expand Up @@ -1422,7 +1420,7 @@ mod tests {
vars: HashMap::new(),
patch: EnvPatches {
dir: [("~/.claude".to_string(), PatchSetting::ReadWrite)].into(),
file: HashMap::new()
file: BTreeMap::new()
},
packages: vec!["base".to_string(), "go".to_string()],
action: TaskAction::exec_from_str("go test ./..."),
Expand Down Expand Up @@ -1545,7 +1543,7 @@ mod tests {
})
.unwrap();
let other = EnvPatches {
dir: HashMap::from_iter([
dir: BTreeMap::from_iter([
("~/.claude".to_string(), PatchSetting::ReadOnly),
("other".to_string(), PatchSetting::ReadOnly),
]),
Expand All @@ -1563,7 +1561,7 @@ mod tests {
("other".to_string(), PatchSetting::ReadOnly),
]
.into(),
file: HashMap::new()
file: BTreeMap::new()
}
);
}
Expand Down Expand Up @@ -1719,6 +1717,42 @@ mod tests {
);
}

/// Two [`EnvPatches`] holding the same entries inserted in opposite
/// orders lower to the same mapping sequence and serialize identically:
/// the order comes off the sorted map, not off insertion, so the sandbox
/// setup and any serialized patch table are stable from run to run.
#[test]
fn to_fs_mappings_and_serialization_are_insertion_order_independent() {
let mut forward = EnvPatches::default();
for k in ["~/a", "~/b", "~/c"] {
forward.dir.insert(k.to_string(), PatchSetting::ReadOnly);
}
for k in ["/x", "/y", "/z"] {
forward.file.insert(k.to_string(), PatchSetting::ReadWrite);
}

let mut reverse = EnvPatches::default();
for k in ["~/c", "~/b", "~/a"] {
reverse.dir.insert(k.to_string(), PatchSetting::ReadOnly);
}
for k in ["/z", "/y", "/x"] {
reverse.file.insert(k.to_string(), PatchSetting::ReadWrite);
}

let seq = |p: &EnvPatches| {
p.to_fs_mappings(Some(Path::new("/home/dev")))
.unwrap()
.into_iter()
.map(|m| m.host_path)
.collect::<Vec<_>>()
};
assert_eq!(seq(&forward), seq(&reverse));
assert_eq!(
toml::to_string(&forward).unwrap(),
toml::to_string(&reverse).unwrap()
);
}

#[test]
fn output_arch_defaults_to_none() {
let mf: File = toml::from_str(indoc! {
Expand Down
16 changes: 7 additions & 9 deletions crates/minimald/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
//! requests one at a time on the runtime, so every handler is a plain
//! `async fn` that `.await`s — no nested runtimes, no sync/async bridge writers.

use std::collections::{BTreeMap, HashMap, HashSet};
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
use std::fs::Permissions;
use std::io::Write;
use std::os::unix::fs::PermissionsExt;
Expand Down Expand Up @@ -379,9 +379,7 @@ impl RuntimeEnv {
/// resolved env-var value shaped `/state/<single-component>`.
/// Multi-component and empty prefixes are dropped — they can't be
/// distinguished from coincidental `/state/`-prefixed values.
fn state_dirs_from_env_vars(
env_vars: &HashMap<String, String>,
) -> std::collections::HashSet<String> {
fn state_dirs_from_env_vars(env_vars: &BTreeMap<String, String>) -> BTreeSet<String> {
env_vars
.values()
.filter_map(|v| v.strip_prefix("/state/"))
Expand Down Expand Up @@ -449,7 +447,7 @@ impl Env {
.map_err(std::io::Error::other)?;
(state_dirs, env_vars)
} else {
(std::collections::HashSet::<String>::new(), HashMap::new())
(BTreeSet::<String>::new(), BTreeMap::new())
};

if let Some(vars) = &args.env_vars {
Expand All @@ -471,7 +469,7 @@ impl Env {
// `deny` policy on some GOCACHE env var) also skips creating
// its state dir — no orphaned `/state` slot for a var the
// session doesn't actually have.
let state_dirs: std::collections::HashSet<String> = if args.include_package_attr_wiring {
let state_dirs: BTreeSet<String> = if args.include_package_attr_wiring {
legacy_state_dirs
} else {
state_dirs_from_env_vars(&pkg_env_vars)
Expand Down Expand Up @@ -2057,7 +2055,7 @@ mod tests {
/// it can be unit-tested directly.
#[test]
fn state_dirs_from_env_vars_extracts_single_component_prefixes() {
let env = HashMap::from([
let env = BTreeMap::from([
("GOCACHE".to_string(), "/state/gocache".to_string()),
("GOMODCACHE".to_string(), "/state/gomodcache".to_string()),
("HOME".to_string(), "/home/user".to_string()),
Expand All @@ -2073,7 +2071,7 @@ mod tests {
/// ignored — creating `state_base_dir/` itself is nonsense.
#[test]
fn state_dirs_from_env_vars_ignores_bare_state() {
let env = HashMap::from([("WEIRD".to_string(), "/state/".to_string())]);
let env = BTreeMap::from([("WEIRD".to_string(), "/state/".to_string())]);
assert!(state_dirs_from_env_vars(&env).is_empty());
}

Expand Down Expand Up @@ -2415,7 +2413,7 @@ mod tests {
/// diagnosing bad ones.
#[test]
fn state_dirs_from_env_vars_silently_drops_multi_component() {
let env = HashMap::from([("WEIRD".to_string(), "/state/foo/bar".to_string())]);
let env = BTreeMap::from([("WEIRD".to_string(), "/state/foo/bar".to_string())]);
assert!(state_dirs_from_env_vars(&env).is_empty());
}
}
4 changes: 2 additions & 2 deletions crates/minimald/src/sessions/composables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,7 @@ mod tests {
let env = |_: &str| Err(std::env::VarError::NotPresent);
let contribution = composable.contribute(&env).unwrap();

let composition_env_vars: std::collections::HashMap<String, String> = contribution
let composition_env_vars: std::collections::BTreeMap<String, String> = contribution
.vars()
.iter()
.map(|pv| (pv.var().name().to_string(), pv.var().value().to_string()))
Expand All @@ -660,7 +660,7 @@ mod tests {
// Composition's state-dir derivation (values shaped
// `/state/<prefix>` → `prefix`) matches what
// `SetupForPackages` extracted from `env_state_wiring`.
let composition_state_dirs: std::collections::HashSet<String> = composition_env_vars
let composition_state_dirs: std::collections::BTreeSet<String> = composition_env_vars
.values()
.filter_map(|v| v.strip_prefix("/state/"))
.filter(|p| !p.is_empty() && !p.contains('/'))
Expand Down
Loading