Skip to content

Commit 3a4463e

Browse files
authored
fix(cli): fall back to regular upload when git filtering excludes all files (#1783)
* fix(cli): fall back to regular upload when git filtering excludes all files When uploading a directory whose contents are entirely excluded by .gitignore, git_sync_files returns an empty file list. The git-aware upload path silently accepted this and printed "Upload complete" without transferring any files. Now both call sites (direct CLI handler and sandbox_upload_plan) check for an empty file list and fall through to the regular, unfiltered upload path instead. Closes #1778 Signed-off-by: Russell Bryant <rbryant@nvidia.com> * test(e2e): add regression test for gitignored upload fallback (#1778) Reproduces the reported workflow: download a file from a sandbox into a local git repo where the target directory is gitignored, then re-upload that directory without --no-git-ignore. Before the fix the upload silently succeeded with zero files transferred. Signed-off-by: Russell Bryant <rbryant@nvidia.com> * fix(cli): warn on stderr when gitignore fallback triggers Address PR review: print a notice when .gitignore filtering excludes all files and the CLI falls back to unfiltered upload. Add a GitFilteredEmpty variant to SandboxUploadPlan so the caller can distinguish this case and emit a visible warning. Document the default gitignore filtering and fallback behavior in manage-sandboxes. * fix(cli): warn on stderr for direct upload gitignore fallback The SandboxCommands::Upload path in main.rs had the same silent fallback as sandbox_create. Restructure the git_sync_files check to emit the same .gitignore warning before falling back to unfiltered upload. Assert the warning in the E2E regression test. * docs(sandboxes): fix misleading upload fallback guidance Replace inaccurate "explicit file list" advice with actionable alternatives: --no-git-ignore, uploading outside the work tree, or git force-adding the files. * fix(cli): collapse nested ifs to satisfy clippy::collapsible_if * style(cli): apply rustfmt to main.rs --------- Signed-off-by: Russell Bryant <rbryant@nvidia.com>
1 parent 4025894 commit 3a4463e

4 files changed

Lines changed: 167 additions & 19 deletions

File tree

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

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2677,20 +2677,26 @@ async fn main() -> Result<()> {
26772677
let dest_display = sandbox_dest.unwrap_or("~");
26782678
eprintln!("Uploading {} -> sandbox:{}", local.display(), dest_display);
26792679
if !no_git_ignore && let Ok((base_dir, files)) = run::git_sync_files(local) {
2680-
run::sandbox_sync_up_files(
2681-
&ctx.endpoint,
2682-
&name,
2683-
&base_dir,
2684-
&files,
2685-
local,
2686-
sandbox_dest,
2687-
&tls,
2688-
)
2689-
.await?;
2690-
eprintln!("{} Upload complete", "✓".green().bold());
2691-
return Ok(());
2680+
if !files.is_empty() {
2681+
run::sandbox_sync_up_files(
2682+
&ctx.endpoint,
2683+
&name,
2684+
&base_dir,
2685+
&files,
2686+
local,
2687+
sandbox_dest,
2688+
&tls,
2689+
)
2690+
.await?;
2691+
eprintln!("{} Upload complete", "✓".green().bold());
2692+
return Ok(());
2693+
}
2694+
eprintln!(
2695+
"{} .gitignore filtering excluded all files in {}; uploading unfiltered",
2696+
"⚠".yellow().bold(),
2697+
local.display(),
2698+
);
26922699
}
2693-
// Fallback: upload without git filtering
26942700
run::sandbox_sync_up(&ctx.endpoint, &name, local, sandbox_dest, &tls).await?;
26952701
eprintln!("{} Upload complete", "✓".green().bold());
26962702
}

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ enum SandboxUploadPlan {
8383
files: Vec<String>,
8484
},
8585
Regular,
86+
GitFilteredEmpty,
8687
}
8788

8889
/// Convert a sandbox phase integer to a human-readable string.
@@ -2125,6 +2126,21 @@ pub async fn sandbox_create(
21252126
)
21262127
.await?;
21272128
}
2129+
SandboxUploadPlan::GitFilteredEmpty => {
2130+
eprintln!(
2131+
" {} .gitignore filtering excluded all files in {}; uploading unfiltered",
2132+
"⚠".yellow().bold(),
2133+
local.display(),
2134+
);
2135+
sandbox_sync_up(
2136+
&effective_server,
2137+
&sandbox_name,
2138+
local,
2139+
dest,
2140+
&effective_tls,
2141+
)
2142+
.await?;
2143+
}
21282144
SandboxUploadPlan::Regular => {
21292145
sandbox_sync_up(
21302146
&effective_server,
@@ -5804,6 +5820,9 @@ fn sandbox_upload_plan(local_path: &Path, git_ignore: bool) -> Result<SandboxUpl
58045820
&& !metadata.file_type().is_symlink()
58055821
&& let Ok((base_dir, files)) = git_sync_files(local_path)
58065822
{
5823+
if files.is_empty() {
5824+
return Ok(SandboxUploadPlan::GitFilteredEmpty);
5825+
}
58075826
return Ok(SandboxUploadPlan::GitAware { base_dir, files });
58085827
}
58095828

@@ -8364,6 +8383,25 @@ mod tests {
83648383
assert_eq!(plan, super::SandboxUploadPlan::Regular);
83658384
}
83668385

8386+
#[test]
8387+
fn sandbox_upload_plan_falls_back_when_all_files_gitignored() {
8388+
let tmpdir = tempfile::tempdir().expect("create tmpdir");
8389+
let repo = tmpdir.path().join("repo");
8390+
fs::create_dir_all(repo.join("runs")).expect("create repo");
8391+
init_git_repo(&repo);
8392+
fs::write(repo.join(".gitignore"), "runs/\n").expect("write .gitignore");
8393+
fs::write(repo.join("runs/test.json"), r#"{"key":"value"}"#).expect("write test.json");
8394+
8395+
let plan =
8396+
sandbox_upload_plan(&repo.join("runs"), true).expect("upload plan should succeed");
8397+
8398+
assert_eq!(
8399+
plan,
8400+
super::SandboxUploadPlan::GitFilteredEmpty,
8401+
"gitignored directory should fall back with GitFilteredEmpty"
8402+
);
8403+
}
8404+
83678405
#[test]
83688406
fn git_sync_files_ignores_inherited_git_env() {
83698407
let _lock = TEST_ENV_LOCK

‎docs/sandboxes/manage-sandboxes.mdx‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,13 @@ openshell sandbox create --upload ./src:/workspace/src --upload ./config:/worksp
358358
359359
</Note>
360360
361+
By default, uploads inside a Git repository respect `.gitignore` rules so that
362+
build artifacts, dependency caches, and other ignored files are not transferred.
363+
If `.gitignore` filtering excludes every file in the upload path, the CLI falls
364+
back to an unfiltered upload and prints a warning. Pass `--no-git-ignore` to
365+
opt into unfiltered uploads explicitly, upload a path outside the Git work
366+
tree, or force-add the intended files if they should remain Git-aware.
367+
361368
## Delete Sandboxes
362369
363370
Deleting a sandbox stops all processes, releases resources, and purges injected credentials.

‎e2e/rust/tests/sync.rs‎

Lines changed: 103 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,10 @@ async fn sandbox_file_upload_download_round_trip() {
7373
// Verify nested file.
7474
let nested = fs::read_to_string(download_dir.join("subdir/nested.txt"))
7575
.expect("read subdir/nested.txt after download");
76-
assert_eq!(nested, "nested-content", "subdir/nested.txt content mismatch");
76+
assert_eq!(
77+
nested, "nested-content",
78+
"subdir/nested.txt content mismatch"
79+
);
7780

7881
// ---------------------------------------------------------------
7982
// Step 4 — Large-file round-trip (~512 KiB) to exercise multi-chunk
@@ -112,7 +115,8 @@ async fn sandbox_file_upload_download_round_trip() {
112115
.await
113116
.expect("download large file");
114117

115-
let actual_data = fs::read(large_down.join("large.bin")).expect("read large.bin after download");
118+
let actual_data =
119+
fs::read(large_down.join("large.bin")).expect("read large.bin after download");
116120
let actual_hash = {
117121
let mut hasher = Sha256::new();
118122
hasher.update(&actual_data);
@@ -152,8 +156,8 @@ async fn sandbox_file_upload_download_round_trip() {
152156
.await
153157
.expect("download single file");
154158

155-
let single_content = fs::read_to_string(single_down.join("single.txt"))
156-
.expect("read single.txt after download");
159+
let single_content =
160+
fs::read_to_string(single_down.join("single.txt")).expect("read single.txt after download");
157161
assert_eq!(
158162
single_content, "single-file-payload",
159163
"single.txt content mismatch"
@@ -364,7 +368,11 @@ async fn sandbox_download_file_only() {
364368
.expect("sandbox create --keep");
365369

366370
guard
367-
.exec(&["sh", "-c", "printf greeting-payload > /sandbox/greeting.txt"])
371+
.exec(&[
372+
"sh",
373+
"-c",
374+
"printf greeting-payload > /sandbox/greeting.txt",
375+
])
368376
.await
369377
.expect("seed greeting.txt inside the sandbox");
370378

@@ -436,7 +444,9 @@ async fn sandbox_download_directory_only() {
436444
/// match the short markers individually instead.
437445
fn assert_resolves_outside_workspace(err: &str, label: &str) {
438446
assert!(
439-
err.contains("resolves to") && err.contains("outside the") && err.contains("sandbox workspace"),
447+
err.contains("resolves to")
448+
&& err.contains("outside the")
449+
&& err.contains("sandbox workspace"),
440450
"expected resolves-outside-workspace error for {label}, got: {err}"
441451
);
442452
}
@@ -543,3 +553,90 @@ async fn sandbox_download_handles_dash_leading_basename() {
543553

544554
guard.cleanup().await;
545555
}
556+
557+
/// Regression for #1778: uploading a directory whose contents are entirely
558+
/// excluded by `.gitignore` must still transfer the files instead of
559+
/// silently reporting success with zero bytes sent.
560+
///
561+
/// Reproduces the reported workflow: download files from a sandbox into a
562+
/// local git repo where the target directory is gitignored, then re-upload
563+
/// to the same sandbox at a different path.
564+
#[tokio::test]
565+
async fn upload_gitignored_directory_falls_back_to_unfiltered() {
566+
let mut guard =
567+
SandboxGuard::create_keep(&["sh", "-c", "echo Ready && sleep infinity"], "Ready")
568+
.await
569+
.expect("sandbox create --keep");
570+
571+
// Seed a file inside the sandbox so we have something to download.
572+
guard
573+
.exec(&[
574+
"sh",
575+
"-c",
576+
"mkdir -p /sandbox/runs && printf downloaded-payload > /sandbox/runs/test.json",
577+
])
578+
.await
579+
.expect("seed runs/test.json inside the sandbox");
580+
581+
// Download the file into a local git repo where `runs/` is gitignored.
582+
let tmpdir = tempfile::tempdir().expect("create tmpdir");
583+
let repo = tmpdir.path().join("repo");
584+
fs::create_dir_all(&repo).expect("create repo dir");
585+
586+
let git_init = tokio::process::Command::new("git")
587+
.args(["init"])
588+
.current_dir(&repo)
589+
.stdout(Stdio::null())
590+
.stderr(Stdio::null())
591+
.status()
592+
.await
593+
.expect("git init");
594+
assert!(git_init.success(), "git init should succeed");
595+
596+
fs::write(repo.join(".gitignore"), "runs/\n").expect("write .gitignore");
597+
598+
let download_dest = repo.join("runs");
599+
fs::create_dir_all(&download_dest).expect("create runs dir");
600+
let download_str = download_dest.to_str().expect("download path is UTF-8");
601+
602+
guard
603+
.download("/sandbox/runs/test.json", download_str)
604+
.await
605+
.expect("download runs/test.json");
606+
607+
let local_file = download_dest.join("test.json");
608+
assert!(local_file.exists(), "downloaded file should exist locally");
609+
610+
// Re-upload the gitignored directory (without --no-git-ignore).
611+
let upload_str = download_dest.to_str().expect("upload path is UTF-8");
612+
let upload_output = guard
613+
.upload_with_gitignore(upload_str, "/sandbox/reuploaded", &repo)
614+
.await
615+
.expect("upload gitignored directory");
616+
617+
assert!(
618+
upload_output.contains(".gitignore filtering excluded all files"),
619+
"expected fallback warning in upload output, got: {upload_output}"
620+
);
621+
622+
// Download the re-uploaded file and verify it arrived.
623+
let verify_dir = tmpdir.path().join("verify");
624+
fs::create_dir_all(&verify_dir).expect("create verify dir");
625+
let verify_str = verify_dir.to_str().expect("verify path is UTF-8");
626+
627+
guard
628+
.download("/sandbox/reuploaded", verify_str)
629+
.await
630+
.expect("download reuploaded directory");
631+
632+
// The upload wraps contents under the source basename, so the file
633+
// lands at verify/runs/test.json.
634+
let reuploaded = fs::read_to_string(verify_dir.join("runs/test.json"))
635+
.expect("reuploaded test.json should exist");
636+
assert_eq!(
637+
reuploaded, "downloaded-payload",
638+
"reuploaded file content mismatch — gitignored directory was not uploaded"
639+
);
640+
641+
guard.cleanup().await;
642+
}

0 commit comments

Comments
 (0)