Skip to content

Commit 4aef998

Browse files
GeekTrainerCopilot
andauthored
feat: add sandbox-npm-install skill to community collection (#989)
Add a reusable Agent Skill that installs npm packages in Docker sandbox environments where virtiofs-mounted workspaces cause native binary crashes (esbuild, lightningcss, rollup). The script installs on local ext4 and symlinks node_modules back into the workspace. - SKILL.md with spec-compliant frontmatter and documentation - scripts/install.sh with security hardening (no eval, readonly paths) - Updated docs/README.skills.md with new skill entry Co-authored-by: GeekTrainer <GeekTrainer@users.noreply.github.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent b1f3346 commit 4aef998

3 files changed

Lines changed: 274 additions & 0 deletions

File tree

docs/README.skills.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ See [CONTRIBUTING.md](../CONTRIBUTING.md#adding-skills) for guidelines on how to
209209
| [reviewing-oracle-to-postgres-migration](../skills/reviewing-oracle-to-postgres-migration/SKILL.md) | Identifies Oracle-to-PostgreSQL migration risks by cross-referencing code against known behavioral differences (empty strings, refcursors, type coercion, sorting, timestamps, concurrent transactions, etc.). Use when planning a database migration, reviewing migration artifacts, or validating that integration tests cover Oracle/PostgreSQL differences. | `references/REFERENCE.md`<br />`references/empty-strings-handling.md`<br />`references/no-data-found-exceptions.md`<br />`references/oracle-parentheses-from-clause.md`<br />`references/oracle-to-postgres-sorting.md`<br />`references/oracle-to-postgres-timestamp-timezone.md`<br />`references/oracle-to-postgres-to-char-numeric.md`<br />`references/oracle-to-postgres-type-coercion.md`<br />`references/postgres-concurrent-transactions.md`<br />`references/postgres-refcursor-handling.md` |
210210
| [ruby-mcp-server-generator](../skills/ruby-mcp-server-generator/SKILL.md) | Generate a complete Model Context Protocol server project in Ruby using the official MCP Ruby SDK gem. | None |
211211
| [rust-mcp-server-generator](../skills/rust-mcp-server-generator/SKILL.md) | Generate a complete Rust Model Context Protocol server project with tools, prompts, resources, and tests using the official rmcp SDK | None |
212+
| [sandbox-npm-install](../skills/sandbox-npm-install/SKILL.md) | Install npm packages in a Docker sandbox environment. Use this skill whenever you need to install, reinstall, or update node_modules inside a container where the workspace is mounted via virtiofs. Native binaries (esbuild, lightningcss, rollup) crash on virtiofs, so packages must be installed on the local ext4 filesystem and symlinked back. | `scripts/install.sh` |
212213
| [scaffolding-oracle-to-postgres-migration-test-project](../skills/scaffolding-oracle-to-postgres-migration-test-project/SKILL.md) | Scaffolds an xUnit integration test project for validating Oracle-to-PostgreSQL database migration behavior in .NET solutions. Creates the test project, transaction-rollback base class, and seed data manager. Use when setting up test infrastructure before writing migration integration tests, or when a test project is needed for Oracle-to-PostgreSQL validation. | None |
213214
| [scoutqa-test](../skills/scoutqa-test/SKILL.md) | This skill should be used when the user asks to "test this website", "run exploratory testing", "check for accessibility issues", "verify the login flow works", "find bugs on this page", or requests automated QA testing. Triggers on web application testing scenarios including smoke tests, accessibility audits, e-commerce flows, and user flow validation using ScoutQA CLI. Use this skill proactively after implementing web application features to verify they work correctly. | None |
214215
| [shuffle-json-data](../skills/shuffle-json-data/SKILL.md) | Shuffle repetitive JSON objects safely by validating schema consistency before randomising entries. | None |
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
---
2+
name: sandbox-npm-install
3+
description: 'Install npm packages in a Docker sandbox environment. Use this skill whenever you need to install, reinstall, or update node_modules inside a container where the workspace is mounted via virtiofs. Native binaries (esbuild, lightningcss, rollup) crash on virtiofs, so packages must be installed on the local ext4 filesystem and symlinked back.'
4+
---
5+
6+
# Sandbox npm Install
7+
8+
## When to Use This Skill
9+
10+
Use this skill whenever:
11+
- You need to install npm packages for the first time in a new sandbox session
12+
- `package.json` or `package-lock.json` has changed and you need to reinstall
13+
- You encounter native binary crashes with errors like `SIGILL`, `SIGSEGV`, `mmap`, or `unaligned sysNoHugePageOS`
14+
- The `node_modules` directory is missing or corrupted
15+
16+
## Prerequisites
17+
18+
- A Docker sandbox environment with a virtiofs-mounted workspace
19+
- Node.js and npm available in the container
20+
- A `package.json` file in the target workspace
21+
22+
## Background
23+
24+
Docker sandbox workspaces are typically mounted via **virtiofs** (file sync between the host and Linux VM). Native Go and Rust binaries (esbuild, lightningcss, rollup, etc.) crash with mmap alignment failures when executed from virtiofs on aarch64. The fix is to install on the container's local ext4 filesystem and symlink back into the workspace.
25+
26+
## Step-by-Step Installation
27+
28+
Run the bundled install script from the workspace root:
29+
30+
```bash
31+
bash scripts/install.sh
32+
```
33+
34+
### Common Options
35+
36+
| Option | Description |
37+
|---|---|
38+
| `--workspace <path>` | Path to directory containing `package.json` (auto-detected if omitted) |
39+
| `--playwright` | Also install Playwright Chromium browser for E2E testing |
40+
41+
### What the Script Does
42+
43+
1. Copies `package.json`, `package-lock.json`, and `.npmrc` (if present) to a local ext4 directory
44+
2. Runs `npm ci` (or `npm install` if no lockfile) on the local filesystem
45+
3. Symlinks `node_modules` back into the workspace
46+
4. Verifies known native binaries (esbuild, rollup, lightningcss, vite) if present
47+
5. Optionally installs Playwright browsers and system dependencies (uses `sudo` when available)
48+
49+
If verification fails, run the script again — crashes can be intermittent during initial setup.
50+
51+
## Post-Install Verification
52+
53+
After the script completes, verify your toolchain works. For example:
54+
55+
```bash
56+
npm test # Run project tests
57+
npm run build # Build the project
58+
npm run dev # Start dev server
59+
```
60+
61+
## Important Notes
62+
63+
- The local install directory (e.g., `/home/agent/project-deps`) is **container-local** and is NOT synced back to the host
64+
- The `node_modules` symlink appears as a broken link on the host — this is harmless since `node_modules` is typically gitignored
65+
- Running `npm ci` or `npm install` on the host naturally replaces the symlink with a real directory
66+
- After any `package.json` or `package-lock.json` change, re-run the install script
67+
- Do NOT run `npm ci` or `npm install` directly in the mounted workspace — native binaries will crash
68+
69+
## Troubleshooting
70+
71+
| Problem | Solution |
72+
|---|---|
73+
| `SIGILL` or `SIGSEGV` when running dev server | Re-run the install script; ensure you're not running `npm install` directly in the workspace |
74+
| `node_modules` not found after install | Check that the symlink exists: `ls -la node_modules` |
75+
| Permission errors during install | Ensure the local deps directory is writable by the current user |
76+
| Verification fails intermittently | Run the script again — native binary crashes can be non-deterministic on first load |
77+
78+
## Vite Compatibility
79+
80+
If your project uses Vite, you may need to allow the symlinked path in `server.fs.allow`. Add the symlink target's parent directory (e.g., `/home/agent/project-deps/`) to your Vite config so that Vite can serve files through the symlink.
Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
# Sandbox npm Install Script
5+
# Installs node_modules on local ext4 filesystem and symlinks into the workspace.
6+
# This avoids native binary crashes (esbuild, lightningcss, rollup) on virtiofs.
7+
8+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
9+
REPO_ROOT="$(cd "$SCRIPT_DIR/../../.." && pwd)"
10+
11+
# Local ext4 base directory where node_modules is installed to avoid virtiofs crashes.
12+
# Change this path if your sandbox uses a different local filesystem location.
13+
readonly DEPS_BASE="/home/agent/project-deps"
14+
WORKSPACE_CLIENT=""
15+
INSTALL_PLAYWRIGHT="false"
16+
17+
usage() {
18+
cat <<EOF
19+
Usage: $(basename "$0") [options]
20+
21+
Options:
22+
--workspace <path> Client workspace containing package.json
23+
--playwright Install Playwright Chromium browser
24+
--help Show this help message
25+
26+
Examples:
27+
bash scripts/install.sh
28+
bash scripts/install.sh --workspace app/client --playwright
29+
EOF
30+
}
31+
32+
while [[ $# -gt 0 ]]; do
33+
case "$1" in
34+
--workspace)
35+
if [[ -z "${2:-}" ]]; then
36+
echo "Error: --workspace requires a path argument"
37+
usage
38+
exit 1
39+
fi
40+
WORKSPACE_CLIENT="$2"
41+
shift 2
42+
;;
43+
--playwright)
44+
INSTALL_PLAYWRIGHT="true"
45+
shift
46+
;;
47+
--help|-h)
48+
usage
49+
exit 0
50+
;;
51+
*)
52+
echo "Unknown option: $1"
53+
usage
54+
exit 1
55+
;;
56+
esac
57+
done
58+
59+
if [[ -z "$WORKSPACE_CLIENT" ]]; then
60+
if [[ -f "$PWD/package.json" ]]; then
61+
WORKSPACE_CLIENT="$PWD"
62+
elif [[ -f "$REPO_ROOT/package.json" ]]; then
63+
WORKSPACE_CLIENT="$REPO_ROOT"
64+
fi
65+
fi
66+
67+
if [[ -n "$WORKSPACE_CLIENT" ]]; then
68+
WORKSPACE_CLIENT="$(cd "$WORKSPACE_CLIENT" 2>/dev/null && pwd || true)"
69+
fi
70+
71+
if [[ -z "$WORKSPACE_CLIENT" || ! -f "$WORKSPACE_CLIENT/package.json" ]]; then
72+
echo "Could not find a valid workspace client path containing package.json."
73+
echo "Use --workspace <path> to specify it explicitly."
74+
exit 1
75+
fi
76+
77+
echo "=== Sandbox npm Install ==="
78+
echo "Workspace: $WORKSPACE_CLIENT"
79+
80+
# Derive a unique subdirectory from the workspace path relative to the repo root.
81+
# e.g. /repo/apps/web -> apps-web, /repo -> <repo-basename>
82+
REL_PATH="${WORKSPACE_CLIENT#"$REPO_ROOT"}"
83+
REL_PATH="${REL_PATH#/}"
84+
if [[ -z "$REL_PATH" ]]; then
85+
REL_PATH="$(basename "$REPO_ROOT")"
86+
fi
87+
# Sanitize: replace path separators with hyphens
88+
DEPS_SUBDIR="${REL_PATH//\//-}"
89+
DEPS_DIR="${DEPS_BASE}/${DEPS_SUBDIR}"
90+
91+
echo "Deps dir: $DEPS_DIR"
92+
93+
# Step 1: Prepare local deps directory
94+
echo "→ Preparing $DEPS_DIR..."
95+
if [[ -z "$DEPS_DIR" || "$DEPS_DIR" != "${DEPS_BASE}/"* ]]; then
96+
echo "ERROR: DEPS_DIR ('$DEPS_DIR') is not under DEPS_BASE ('$DEPS_BASE'). Aborting."
97+
exit 1
98+
fi
99+
rm -rf "$DEPS_DIR"
100+
mkdir -p "$DEPS_DIR"
101+
chmod 700 "$DEPS_DIR"
102+
cp "$WORKSPACE_CLIENT/package.json" "$DEPS_DIR/"
103+
104+
# Copy .npmrc if present (needed for private registries / scoped packages)
105+
# Permissions restricted to owner-only since .npmrc may contain auth tokens
106+
if [[ -f "$WORKSPACE_CLIENT/.npmrc" ]]; then
107+
cp "$WORKSPACE_CLIENT/.npmrc" "$DEPS_DIR/"
108+
chmod 600 "$DEPS_DIR/.npmrc"
109+
fi
110+
111+
if [[ -f "$WORKSPACE_CLIENT/package-lock.json" ]]; then
112+
cp "$WORKSPACE_CLIENT/package-lock.json" "$DEPS_DIR/"
113+
INSTALL_CMD=(npm ci)
114+
else
115+
echo "! package-lock.json not found; falling back to npm install"
116+
INSTALL_CMD=(npm install)
117+
fi
118+
119+
# Step 2: Install on local ext4
120+
echo "→ Running ${INSTALL_CMD[*]} on local ext4..."
121+
cd "$DEPS_DIR" && "${INSTALL_CMD[@]}"
122+
123+
# Step 3: Symlink into workspace
124+
echo "→ Symlinking node_modules into workspace..."
125+
cd "$WORKSPACE_CLIENT"
126+
rm -rf node_modules
127+
ln -s "$DEPS_DIR/node_modules" node_modules
128+
129+
has_dep() {
130+
local dep="$1"
131+
node -e "
132+
const pkg=require(process.argv[1]);
133+
const deps={...(pkg.dependencies||{}),...(pkg.devDependencies||{}),...(pkg.optionalDependencies||{})};
134+
process.exit(deps[process.argv[2]] ? 0 : 1);
135+
" "$WORKSPACE_CLIENT/package.json" "$dep"
136+
}
137+
138+
verify_one() {
139+
local label="$1"
140+
shift
141+
if "$@" >/dev/null 2>&1; then
142+
echo "$label OK"
143+
return 0
144+
fi
145+
146+
echo "$label FAIL"
147+
return 1
148+
}
149+
150+
# Step 4: Verify native binaries when present in this project
151+
echo "→ Verifying native binaries..."
152+
FAIL=0
153+
154+
if has_dep esbuild; then
155+
verify_one "esbuild" node -e "require('esbuild').transform('const x: number = 1',{loader:'ts'}).catch(()=>process.exit(1))" || FAIL=1
156+
fi
157+
158+
if has_dep rollup; then
159+
verify_one "rollup" node -e "import('rollup').catch(()=>process.exit(1))" || FAIL=1
160+
fi
161+
162+
if has_dep lightningcss; then
163+
verify_one "lightningcss" node -e "try{require('lightningcss')}catch(_){process.exit(1)}" || FAIL=1
164+
fi
165+
166+
if has_dep vite; then
167+
verify_one "vite" node -e "import('vite').catch(()=>process.exit(1))" || FAIL=1
168+
fi
169+
170+
if [ "$FAIL" -ne 0 ]; then
171+
echo "✗ Binary verification failed. Try running the script again (crashes can be intermittent)."
172+
exit 1
173+
fi
174+
175+
# Step 5: Optionally install Playwright
176+
if [[ "$INSTALL_PLAYWRIGHT" == "true" ]]; then
177+
echo "→ Installing Playwright browsers..."
178+
if [[ "${EUID:-$(id -u)}" -eq 0 ]]; then
179+
npx playwright install --with-deps chromium
180+
elif command -v sudo &>/dev/null && sudo -n true 2>/dev/null; then
181+
# Non-root but passwordless sudo available — install browsers then system deps
182+
npx playwright install chromium
183+
sudo npx playwright install-deps chromium
184+
else
185+
npx playwright install chromium
186+
echo "⚠ System dependencies not installed (no root/sudo access)."
187+
echo " Playwright tests may fail. Run: sudo npx playwright install-deps chromium"
188+
fi
189+
fi
190+
191+
echo ""
192+
echo "=== ✓ Sandbox npm install complete ==="
193+
echo "Run 'npm run dev' to start the dev server."

0 commit comments

Comments
 (0)