fix(sandbox): derive staged scripts from Dockerfile to prevent drift#2509
fix(sandbox): derive staged scripts from Dockerfile to prevent drift#2509jason-ma-nv wants to merge 2 commits intomainfrom
Conversation
stageOptimizedSandboxBuildContext previously maintained an explicit allowlist of scripts/ files to copy into the Docker build context. Any COPY added to the Dockerfile without a matching entry in that list caused a silent 'file not found in build context' failure at sandbox creation time (see #2503). Replace the hardcoded list with a Dockerfile parser that extracts every 'COPY scripts/<src> <dest>' line (skipping --from= inter-stage copies) and copies the source into the build context at the same relative path. The Dockerfile is now the single source of truth; no manual sync needed. Add a regression test that verifies this property: it parses the same COPY lines and asserts each source exists in the staged context. Signed-off-by: jama <jama@nvidia.com> Made-with: Cursor
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Enterprise Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthroughParses the generated Dockerfile for Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
src/lib/sandbox-build-context.ts (1)
61-76: Consider adding error handling for missing source files.If a script referenced in the Dockerfile doesn't exist at
srcPath,fs.copyFileSyncwill throw a genericENOENTerror. A more descriptive error message would help developers diagnose the issue faster.Additionally, the regex pattern here differs from the test's pattern (line 66 handles flags like
--chown, but the test's pattern on line 24 of the test file does not). This inconsistency could cause the test to extract a different set of paths than the implementation copies, potentially missing regressions.Proposed improvement for error handling
const relSrc = match[1]; // e.g. "scripts/nemoclaw-start.sh" const srcPath = path.join(rootDir, relSrc); const destPath = path.join(buildCtx, relSrc); + if (!fs.existsSync(srcPath)) { + throw new Error( + `Dockerfile references "${relSrc}" but file not found at "${srcPath}". ` + + `Ensure the script exists or remove the COPY directive.` + ); + } fs.mkdirSync(path.dirname(destPath), { recursive: true }); fs.copyFileSync(srcPath, destPath);🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/lib/sandbox-build-context.ts` around lines 61 - 76, The code that extracts and copies script paths from stagedDockerfile can throw opaque ENOENT errors when a referenced source (srcPath) is missing and the regex copyPattern may not match the same forms as tests; update the implementation by (1) changing copyPattern to accept optional COPY flags like --chown and multiple flag forms (e.g. /^COPY(?:\s+--\w+[^\s]*)*\s+(scripts\/\S+)\s+\S/gm or align it exactly with the test's regex) so extraction matches tests, and (2) wrap the fs.copyFileSync call in a try/catch (or check fs.existsSync before copying) and throw/log a clearer error that includes the missing srcPath, the Dockerfile line or match text (match[0]) and context (stagedDockerfile/rootDir) to make ENOENT failures descriptive; reference symbols: dockerfileContent, copyPattern, match, srcPath, destPath, fs.copyFileSync.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@test/sandbox-build-context.test.ts`:
- Around line 22-29: The test's COPY extraction (variable copyPattern and the
loop that fills scriptSources from dockerfileContent) must be updated to match
the implementation's behavior: allow optional COPY flags like --chown/--chmod
and skip COPY lines that include --from= (inter-stage copies). Either replace
the hardcoded regex with the same pattern used by the implementation (extract
the pattern into a shared constant and import it into the test) or update the
test's copyPattern to accept leading flags and reject lines containing --from=
so the test no longer produces false positives/negatives.
---
Nitpick comments:
In `@src/lib/sandbox-build-context.ts`:
- Around line 61-76: The code that extracts and copies script paths from
stagedDockerfile can throw opaque ENOENT errors when a referenced source
(srcPath) is missing and the regex copyPattern may not match the same forms as
tests; update the implementation by (1) changing copyPattern to accept optional
COPY flags like --chown and multiple flag forms (e.g.
/^COPY(?:\s+--\w+[^\s]*)*\s+(scripts\/\S+)\s+\S/gm or align it exactly with the
test's regex) so extraction matches tests, and (2) wrap the fs.copyFileSync call
in a try/catch (or check fs.existsSync before copying) and throw/log a clearer
error that includes the missing srcPath, the Dockerfile line or match text
(match[0]) and context (stagedDockerfile/rootDir) to make ENOENT failures
descriptive; reference symbols: dockerfileContent, copyPattern, match, srcPath,
destPath, fs.copyFileSync.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Enterprise
Run ID: a1073aff-4f76-430c-9451-cb9d566bd6dc
📒 Files selected for processing (2)
src/lib/sandbox-build-context.tstest/sandbox-build-context.test.ts
Match optional COPY flags (--chown/--chmod) and skip --from= inter-stage copies, consistent with the implementation regex in sandbox-build-context.ts. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Summary
stageOptimizedSandboxBuildContextmaintained a hardcoded list ofscripts/files to copy into the Docker build context. AnyCOPY scripts/<file>added to the Dockerfile without a matching entry in that list caused a silent "file not found in build context" failure at sandbox creation time (see #2503). Replace the explicit allowlist with a Dockerfile parser so the two can never drift.Related Issue
Fixes #2503
Changes
src/lib/sandbox-build-context.ts: replace hardcodedscripts/file list with a loop that parses everyCOPY scripts/<src> <dest>line in the staged Dockerfile (skipping--from=inter-stage copies) and copies each source into the build context at the same relative path. The Dockerfile is now the single source of truth.test/sandbox-build-context.test.ts: add regression test that parses the sameCOPY scripts/lines from the Dockerfile and asserts each source path exists in the staged build context produced bystageOptimizedSandboxBuildContext.Type of Change
Verification
npx prek run --all-filespassesnpm testpassesmake docsbuilds without warnings (doc changes only)AI Disclosure
Signed-off-by: jama jama@nvidia.com
Made with Cursor
Summary by CodeRabbit
Refactor
Tests