Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hardcoded sandbox experiment #1178

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
557 changes: 287 additions & 270 deletions Cargo.lock

Large diffs are not rendered by default.

28 changes: 15 additions & 13 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ futures = "0.3.31"
indicatif = "0.17.9"
console = { version = "0.15.10", features = ["windows-console-colors"] }
thiserror = "2.0.9"
tempfile = "3.14.0"
tempfile = "3.15.0"
chrono = "0.4.39"
sha1 = "0.10.6"
spdx = "0.10.7"
Expand Down Expand Up @@ -137,6 +137,7 @@ rattler_index = { version = "0.20.4", default-features = false }
rattler_networking = { version = "0.21.9", default-features = false }
rattler_redaction = { version = "0.1.5" }
rattler_repodata_gateway = { version = "0.21.29", default-features = false, features = ["gateway"] }
rattler_sandbox = { version = "0.1.0", default-features = false, features = ["tokio"] }
rattler_shell = { version = "0.22.12", default-features = false, features = ["sysinfo"] }
rattler_solve = { version = "1.3.1", default-features = false, features = ["resolvo", "serde"] }
rattler_virtual_packages = { version = "1.1.15", default-features = false }
Expand Down Expand Up @@ -175,15 +176,16 @@ pre-build = [
# this fork contains fixes for slow zip reading and large file writing
zip = { git = "https://github.com/wolfv/zip2", branch = "patched"}

# rattler = { path = "../rattler/crates/rattler" }
# rattler_cache = { path = "../rattler/crates/rattler_cache" }
# rattler_conda_types = { path = "../rattler/crates/rattler_conda_types" }
# rattler_digest = { path = "../rattler/crates/rattler_digest" }
# rattler_index = { path = "../rattler/crates/rattler_index" }
# rattler_networking = { path = "../rattler/crates/rattler_networking" }
# rattler_repodata_gateway = { path = "../rattler/crates/rattler_repodata_gateway" }
# rattler_shell = { path = "../rattler/crates/rattler_shell" }
# rattler_solve = { path = "../rattler/crates/rattler_solve" }
# rattler_redaction = { path = "../rattler/crates/rattler_redaction" }
# rattler_virtual_packages = { path = "../rattler/crates/rattler_virtual_packages" }
# rattler_package_streaming = { path = "../rattler/crates/rattler_package_streaming" }
rattler = { path = "../rattler/crates/rattler" }
rattler_sandbox = { path = "../rattler/crates/rattler_sandbox" }
rattler_cache = { path = "../rattler/crates/rattler_cache" }
rattler_conda_types = { path = "../rattler/crates/rattler_conda_types" }
rattler_digest = { path = "../rattler/crates/rattler_digest" }
rattler_index = { path = "../rattler/crates/rattler_index" }
rattler_networking = { path = "../rattler/crates/rattler_networking" }
rattler_repodata_gateway = { path = "../rattler/crates/rattler_repodata_gateway" }
rattler_shell = { path = "../rattler/crates/rattler_shell" }
rattler_solve = { path = "../rattler/crates/rattler_solve" }
rattler_redaction = { path = "../rattler/crates/rattler_redaction" }
rattler_virtual_packages = { path = "../rattler/crates/rattler_virtual_packages" }
rattler_package_streaming = { path = "../rattler/crates/rattler_package_streaming" }
1 change: 1 addition & 0 deletions src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ impl Output {
&self.build_configuration.directories.host_prefix,
Some(&self.build_configuration.directories.build_prefix),
Some(jinja),
None, // sandbox config
)
.await
.into_diagnostic()?;
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ pub async fn get_build_output(
),
store_recipe: !args.no_include_recipe,
force_colors: args.color_build_log && console::colors_enabled(),
sandbox_config: args.sandbox_arguments.clone().into(),
},
finalized_dependencies: None,
finalized_sources: None,
Expand Down
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ use tempfile::tempdir;

#[tokio::main]
async fn main() -> miette::Result<()> {
rattler_sandbox::init_sandbox();

let app = App::parse();
let log_handler = if !app.is_tui() {
Some(
Expand Down
10 changes: 10 additions & 0 deletions src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ use crate::{
parser::{Recipe, Source},
},
render::resolved_dependencies::FinalizedDependencies,
script::SandboxConfiguration,
system_tools::SystemTools,
tool_configuration,
utils::remove_dir_all_force,
Expand Down Expand Up @@ -346,6 +347,10 @@ pub struct BuildConfiguration {
/// build script or not
#[serde(skip_serializing, default = "default_true")]
pub force_colors: bool,

/// The configuration for the sandbox
#[serde(skip_serializing, default)]
pub sandbox_config: Option<SandboxConfiguration>,
}

impl BuildConfiguration {
Expand All @@ -354,6 +359,11 @@ impl BuildConfiguration {
self.target_platform != self.build_platform.platform
}

/// Retrieve the sandbox configuration for this output
pub fn sandbox_config(&self) -> Option<&SandboxConfiguration> {
self.sandbox_config.as_ref()
}

/// Construct a `SelectorConfig` from the given `BuildConfiguration`
pub fn selector_config(&self) -> SelectorConfig {
SelectorConfig {
Expand Down
5 changes: 5 additions & 0 deletions src/opt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use url::Url;
use crate::recipe_generator::GenerateRecipeOpts;
use crate::{
console_utils::{Color, LogStyle},
script::SandboxArguments,
tool_configuration::{SkipExisting, TestStrategy},
};

Expand Down Expand Up @@ -413,6 +414,10 @@ pub struct BuildOpts {
/// Extra metadata to include in about.json
#[arg(long, value_parser = parse_key_val)]
pub extra_meta: Option<Vec<(String, Value)>>,

#[allow(missing_docs)]
#[clap(flatten)]
pub sandbox_arguments: SandboxArguments,
}

fn is_dir(dir: &str) -> Result<PathBuf, String> {
Expand Down
27 changes: 22 additions & 5 deletions src/package_test/run_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ impl Tests {
})?;

script
.run_script(env_vars, tmp_dir.path(), cwd, environment, None, None)
.run_script(env_vars, tmp_dir.path(), cwd, environment, None, None, None)
.await
.map_err(|e| TestError::TestFailed(e.to_string()))?;
}
Expand All @@ -147,7 +147,7 @@ impl Tests {
};

script
.run_script(env_vars, tmp_dir.path(), cwd, environment, None, None)
.run_script(env_vars, tmp_dir.path(), cwd, environment, None, None, None)
.await
.map_err(|e| TestError::TestFailed(e.to_string()))?;
}
Expand Down Expand Up @@ -568,7 +568,15 @@ impl PythonTest {

let tmp_dir = tempfile::tempdir()?;
script
.run_script(Default::default(), tmp_dir.path(), path, prefix, None, None)
.run_script(
Default::default(),
tmp_dir.path(),
path,
prefix,
None,
None,
None,
)
.await
.map_err(|e| TestError::TestFailed(e.to_string()))?;

Expand All @@ -583,7 +591,7 @@ impl PythonTest {
..Script::default()
};
script
.run_script(Default::default(), path, path, prefix, None, None)
.run_script(Default::default(), path, path, prefix, None, None, None)
.await
.map_err(|e| TestError::TestFailed(e.to_string()))?;

Expand Down Expand Up @@ -648,7 +656,15 @@ impl PerlTest {

let tmp_dir = tempfile::tempdir()?;
script
.run_script(Default::default(), tmp_dir.path(), path, prefix, None, None)
.run_script(
Default::default(),
tmp_dir.path(),
path,
prefix,
None,
None,
None,
)
.await
.map_err(|e| TestError::TestFailed(e.to_string()))?;

Expand Down Expand Up @@ -756,6 +772,7 @@ impl CommandsTest {
&run_prefix,
build_prefix.as_ref(),
None,
None,
)
.await
.map_err(|e| TestError::TestFailed(e.to_string()))?;
Expand Down
1 change: 1 addition & 0 deletions src/script/interpreter/bash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ impl Interpreter for BashInterpreter {
&cmd_args,
&args.work_dir,
&args.replacements("$((var))"),
args.sandbox_config.as_ref(),
)
.await?;

Expand Down
1 change: 1 addition & 0 deletions src/script/interpreter/cmd_exe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ impl Interpreter for CmdExeInterpreter {
&cmd_args,
&args.work_dir,
&args.replacements("%((var))%"),
None,
)
.await?;

Expand Down
1 change: 1 addition & 0 deletions src/script/interpreter/nushell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ impl Interpreter for NuShellInterpreter {
&cmd_args,
&args.work_dir,
&args.replacements("$((var))"),
None,
)
.await?;

Expand Down
20 changes: 19 additions & 1 deletion src/script/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Module for running scripts in different interpreters.
mod interpreter;
mod sandbox;
pub use sandbox::{SandboxArguments, SandboxConfiguration};

use crate::script::interpreter::Interpreter;
use indexmap::IndexMap;
Expand Down Expand Up @@ -47,6 +49,9 @@ pub struct ExecutionArgs {

/// The working directory (`cwd`) in which the script should execute
pub work_dir: PathBuf,

/// The sandbox configuration to use for the script execution
pub sandbox_config: Option<SandboxConfiguration>,
}

impl ExecutionArgs {
Expand Down Expand Up @@ -226,6 +231,7 @@ impl Script {
run_prefix: &Path,
build_prefix: Option<&PathBuf>,
mut jinja_config: Option<Jinja<'_>>,
sandbox_config: Option<&SandboxConfiguration>,
) -> Result<(), std::io::Error> {
// TODO: This is a bit of an out and about way to determine whether or
// not nushell is available. It would be best to run the activation
Expand Down Expand Up @@ -318,6 +324,7 @@ impl Script {
run_prefix: run_prefix.to_owned(),
execution_platform: Platform::current(),
work_dir,
sandbox_config: sandbox_config.cloned(),
};

match interpreter {
Expand Down Expand Up @@ -387,6 +394,7 @@ impl Output {
&self.build_configuration.directories.host_prefix,
Some(&self.build_configuration.directories.build_prefix),
Some(jinja),
self.build_configuration.sandbox_config(),
)
.await?;

Expand All @@ -400,8 +408,18 @@ async fn run_process_with_replacements(
args: &[&str],
cwd: &Path,
replacements: &HashMap<String, String>,
sandbox_config: Option<&SandboxConfiguration>,
) -> Result<std::process::Output, std::io::Error> {
let mut command = tokio::process::Command::new(args[0]);
let mut command = if let Some(sandbox_config) = sandbox_config {
tracing::info!("Sandbox configuration: {}", sandbox_config);
rattler_sandbox::tokio::sandboxed_command(
args[0],
&sandbox_config.with_cwd(cwd).exceptions(),
)
} else {
tokio::process::Command::new(args[0])
};

command
.current_dir(cwd)
.args(&args[1..])
Expand Down
Loading