Skip to content

Commit d927366

Browse files
committed
feat(cli): support multiple --upload flags on sandbox create (NVIDIA#1635)
Closes NVIDIA#1635 Signed-off-by: Philippe Martin <[email protected]>
1 parent e98ea3e commit d927366

6 files changed

Lines changed: 142 additions & 34 deletions

File tree

‎crates/openshell-cli/src/main.rs‎

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,7 +1182,7 @@ enum SandboxCommands {
11821182
/// `.gitignore` rules are applied by default; use `--no-git-ignore` to
11831183
/// upload everything.
11841184
#[arg(long, value_hint = ValueHint::AnyPath, help_heading = "UPLOAD FLAGS")]
1185-
upload: Option<String>,
1185+
upload: Vec<String>,
11861186

11871187
/// Disable `.gitignore` filtering for `--upload`.
11881188
#[arg(long, requires = "upload", help_heading = "UPLOAD FLAGS")]
@@ -2577,11 +2577,22 @@ async fn main() -> Result<()> {
25772577
labels_map.insert(parts[0].to_string(), parts[1].to_string());
25782578
}
25792579

2580-
// Parse --upload spec into (local_path, sandbox_path, git_ignore).
2581-
let upload_spec = upload.as_deref().map(|s| {
2582-
let (local, remote) = parse_upload_spec(s);
2583-
(local, remote, !no_git_ignore)
2584-
});
2580+
// Parse --upload specs into [(local_path, sandbox_path, git_ignore)].
2581+
let upload_specs: Vec<(String, Option<String>, bool)> = upload
2582+
.iter()
2583+
.map(|s| {
2584+
let (local, remote) = parse_upload_spec(s);
2585+
(local, remote, !no_git_ignore)
2586+
})
2587+
.collect();
2588+
2589+
// Validate all local paths before creating the sandbox so failures are
2590+
// fast and have no side effects.
2591+
for (local, _, _) in &upload_specs {
2592+
if !run::local_upload_path_exists(std::path::Path::new(local)) {
2593+
return Err(miette::miette!("local path does not exist: {}", local));
2594+
}
2595+
}
25852596

25862597
let editor = editor.map(Into::into);
25872598
let forward = forward
@@ -2598,7 +2609,7 @@ async fn main() -> Result<()> {
25982609
name.as_deref(),
25992610
from.as_deref(),
26002611
&ctx.name,
2601-
upload_spec.as_ref(),
2612+
&upload_specs,
26022613
keep,
26032614
gpu,
26042615
gpu_device.as_deref(),
@@ -3479,6 +3490,43 @@ mod tests {
34793490
assert_eq!(remote, None);
34803491
}
34813492

3493+
#[test]
3494+
fn sandbox_create_upload_flag_accepts_multiple_values() {
3495+
let result = Cli::try_parse_from([
3496+
"openshell",
3497+
"sandbox",
3498+
"create",
3499+
"--upload",
3500+
"./src:/workspace/src",
3501+
"--upload",
3502+
"./config:/workspace/config",
3503+
]);
3504+
assert!(
3505+
result.is_ok(),
3506+
"--upload should be repeatable, got: {:?}",
3507+
result.err()
3508+
);
3509+
}
3510+
3511+
#[test]
3512+
fn sandbox_create_upload_flag_accepts_zero_values() {
3513+
let result = Cli::try_parse_from(["openshell", "sandbox", "create"]);
3514+
assert!(
3515+
result.is_ok(),
3516+
"sandbox create with no --upload should succeed, got: {:?}",
3517+
result.err()
3518+
);
3519+
}
3520+
3521+
#[test]
3522+
fn sandbox_create_no_git_ignore_requires_upload() {
3523+
let result = Cli::try_parse_from(["openshell", "sandbox", "create", "--no-git-ignore"]);
3524+
assert!(
3525+
result.is_err(),
3526+
"--no-git-ignore without --upload should be rejected"
3527+
);
3528+
}
3529+
34823530
#[test]
34833531
fn resolve_sandbox_name_returns_explicit_name() {
34843532
// When a name is provided, it should be returned regardless of any

‎crates/openshell-cli/src/run.rs‎

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1679,7 +1679,7 @@ pub async fn sandbox_create(
16791679
name: Option<&str>,
16801680
from: Option<&str>,
16811681
gateway_name: &str,
1682-
upload: Option<&(String, Option<String>, bool)>,
1682+
uploads: &[(String, Option<String>, bool)],
16831683
keep: bool,
16841684
gpu: bool,
16851685
gpu_device: Option<&str>,
@@ -2045,18 +2045,30 @@ pub async fn sandbox_create(
20452045
drop(stream);
20462046
drop(client);
20472047

2048-
if let Some((local_path, sandbox_path, git_ignore)) = upload {
2048+
let upload_count = uploads.len();
2049+
for (idx, (local_path, sandbox_path, git_ignore)) in uploads.iter().enumerate() {
20492050
let dest = sandbox_path.as_deref();
20502051
let dest_display = dest.unwrap_or("~");
2051-
eprintln!(
2052-
" {} Uploading files to {dest_display}...",
2053-
"\u{2022}".dimmed(),
2054-
);
2052+
if upload_count > 1 {
2053+
eprintln!(
2054+
" {} Uploading [{}/{}] files to {dest_display}...",
2055+
"\u{2022}".dimmed(),
2056+
idx + 1,
2057+
upload_count,
2058+
);
2059+
} else {
2060+
eprintln!(
2061+
" {} Uploading files to {dest_display}...",
2062+
"\u{2022}".dimmed(),
2063+
);
2064+
}
20552065
let local = Path::new(local_path);
20562066
if !local_upload_path_exists(local) {
20572067
return Err(miette::miette!(
2058-
"local path does not exist: {}",
2059-
local.display()
2068+
"local path does not exist: {} (upload spec {} of {})",
2069+
local.display(),
2070+
idx + 1,
2071+
upload_count,
20602072
));
20612073
}
20622074

‎crates/openshell-cli/tests/sandbox_create_lifecycle_integration.rs‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -780,7 +780,7 @@ async fn sandbox_create_keeps_command_sessions_by_default() {
780780
Some("default-command"),
781781
None,
782782
"openshell",
783-
None,
783+
&[],
784784
true,
785785
false,
786786
None,
@@ -822,7 +822,7 @@ async fn sandbox_create_sends_cpu_and_memory_limits_only() {
822822
Some("resources"),
823823
None,
824824
"openshell",
825-
None,
825+
&[],
826826
true,
827827
false,
828828
None,
@@ -899,7 +899,7 @@ async fn sandbox_create_does_not_infer_command_providers_when_v2_enabled() {
899899
Some("v2-no-inferred-provider"),
900900
None,
901901
"openshell",
902-
None,
902+
&[],
903903
true,
904904
false,
905905
None,
@@ -956,7 +956,7 @@ async fn sandbox_create_returns_vm_error_without_waiting_for_timeout() {
956956
Some("vm-error"),
957957
None,
958958
"openshell",
959-
None,
959+
&[],
960960
true,
961961
false,
962962
None,
@@ -1009,7 +1009,7 @@ async fn sandbox_create_keeps_waiting_while_vm_progress_arrives() {
10091009
Some("vm-slow-progress"),
10101010
None,
10111011
"openshell",
1012-
None,
1012+
&[],
10131013
true,
10141014
false,
10151015
None,
@@ -1054,7 +1054,7 @@ async fn sandbox_create_times_out_when_only_logs_arrive() {
10541054
Some("vm-log-churn"),
10551055
None,
10561056
"openshell",
1057-
None,
1057+
&[],
10581058
true,
10591059
false,
10601060
None,
@@ -1095,7 +1095,7 @@ async fn sandbox_create_deletes_command_sessions_with_no_keep() {
10951095
Some("ephemeral-command"),
10961096
None,
10971097
"openshell",
1098-
None,
1098+
&[],
10991099
false,
11001100
false,
11011101
None,
@@ -1140,7 +1140,7 @@ async fn sandbox_create_deletes_shell_sessions_with_no_keep() {
11401140
Some("ephemeral-shell"),
11411141
None,
11421142
"openshell",
1143-
None,
1143+
&[],
11441144
false,
11451145
false,
11461146
None,
@@ -1185,7 +1185,7 @@ async fn sandbox_create_keeps_sandbox_with_hidden_keep_flag() {
11851185
Some("persistent-keep"),
11861186
None,
11871187
"openshell",
1188-
None,
1188+
&[],
11891189
true,
11901190
false,
11911191
None,
@@ -1230,7 +1230,7 @@ async fn sandbox_create_keeps_sandbox_with_forwarding() {
12301230
Some("persistent-forward"),
12311231
None,
12321232
"openshell",
1233-
None,
1233+
&[],
12341234
false,
12351235
false,
12361236
None,

‎docs/sandboxes/manage-sandboxes.mdx‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,12 @@ The CLI only allows sandbox-side sources that resolve inside the writable worksp
309309
310310
<Note>
311311
You can also upload files at creation time with the `--upload` flag on
312-
`openshell sandbox create`.
312+
`openshell sandbox create`. Pass `--upload` multiple times to upload
313+
several paths in a single command:
314+
315+
```shell
316+
openshell sandbox create --upload ./src:/workspace/src --upload ./config:/workspace/config -- claude
317+
```
313318
314319
</Note>
315320

‎e2e/rust/src/harness/sandbox.rs‎

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -216,16 +216,23 @@ impl SandboxGuard {
216216
upload_dest: &str,
217217
command: &[&str],
218218
) -> Result<Self, String> {
219-
let upload_spec = format!("{upload_local}:{upload_dest}");
219+
Self::create_with_uploads(&[(upload_local, upload_dest)], command).await
220+
}
220221

222+
/// Create a sandbox with multiple `--upload` specs.
223+
///
224+
/// Each element of `uploads` is a `(local_path, sandbox_dest)` pair.
225+
/// `--no-git-ignore` is applied to all uploads.
226+
pub async fn create_with_uploads(
227+
uploads: &[(&str, &str)],
228+
command: &[&str],
229+
) -> Result<Self, String> {
221230
let mut cmd = openshell_cmd();
222-
cmd.arg("sandbox")
223-
.arg("create")
224-
.arg("--upload")
225-
.arg(&upload_spec)
226-
.arg("--no-git-ignore")
227-
.arg("--")
228-
.args(command);
231+
cmd.arg("sandbox").arg("create");
232+
for (local, dest) in uploads {
233+
cmd.arg("--upload").arg(format!("{local}:{dest}"));
234+
}
235+
cmd.arg("--no-git-ignore").arg("--").args(command);
229236
cmd.stdout(Stdio::piped()).stderr(Stdio::piped());
230237

231238
let output = timeout(SANDBOX_READY_TIMEOUT, cmd.output())

‎e2e/rust/tests/upload_create.rs‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,42 @@ async fn create_with_upload_directory_preserves_source_basename() {
5252
guard.cleanup().await;
5353
}
5454

55+
/// Two `--upload` specs in a single `sandbox create` call should both land in
56+
/// the sandbox before the command runs.
57+
#[tokio::test]
58+
async fn create_with_multiple_uploads() {
59+
let tmpdir = tempfile::tempdir().expect("create tmpdir");
60+
61+
let dir_a = tmpdir.path().join("alpha");
62+
let dir_b = tmpdir.path().join("beta");
63+
fs::create_dir_all(&dir_a).expect("create alpha");
64+
fs::create_dir_all(&dir_b).expect("create beta");
65+
fs::write(dir_a.join("a.txt"), "content-alpha").expect("write a.txt");
66+
fs::write(dir_b.join("b.txt"), "content-beta").expect("write b.txt");
67+
68+
let spec_a = dir_a.to_str().expect("alpha path is UTF-8");
69+
let spec_b = dir_b.to_str().expect("beta path is UTF-8");
70+
71+
let mut guard = SandboxGuard::create_with_uploads(
72+
&[(spec_a, "/sandbox/alpha"), (spec_b, "/sandbox/beta")],
73+
&["sh", "-c", "cat /sandbox/alpha/alpha/a.txt /sandbox/beta/beta/b.txt"],
74+
)
75+
.await
76+
.expect("sandbox create with multiple --upload flags");
77+
78+
let clean = strip_ansi(&guard.create_output);
79+
assert!(
80+
clean.contains("content-alpha"),
81+
"expected alpha content in output:\n{clean}"
82+
);
83+
assert!(
84+
clean.contains("content-beta"),
85+
"expected beta content in output:\n{clean}"
86+
);
87+
88+
guard.cleanup().await;
89+
}
90+
5591
/// `--upload` with a single file (not a directory) should work.
5692
#[tokio::test]
5793
async fn create_with_upload_single_file() {

0 commit comments

Comments
 (0)