// Run Rust cargo tests properly in git worktrees with environment variable sourcing. Use when cargo test fails, when seeing authentication errors, when test results differ between runs, or before running pre-commit checks in worktrees. Essential for integration tests requiring LANGSMITH_API_KEY or ANTHROPIC_API_KEY.
| name | test-runner-worktree |
| description | Run Rust cargo tests properly in git worktrees with environment variable sourcing. Use when cargo test fails, when seeing authentication errors, when test results differ between runs, or before running pre-commit checks in worktrees. Essential for integration tests requiring LANGSMITH_API_KEY or ANTHROPIC_API_KEY. |
| scope | project |
Guide for running integration tests properly in both git worktrees and the main repository, with emphasis on environment variable sourcing and working directory context.
Before running ANY tests, pre-commit checks, or cargo commands in a worktree, ALWAYS source environment variables:
source /workspace/.devcontainer/.env
Why this matters:
LANGSMITH_API_KEY and other credentialscargo test, cargo clippy, etc.) will fail without proper environmentThis applies to:
cargo test - Any test commandcargo fmt && cargo check && cargo clippy && cargo test suitecargo clippy - Linting may trigger testscargo build - Build may run build scripts that need credentialsQuick Check:
# Verify environment variables are set
[ -n "$LANGSMITH_API_KEY" ] && echo "โ Ready to test" || echo "โ Run: source /workspace/.devcontainer/.env"
Quick Start for Pre-Commit Checks:
# One command to source and run full pre-commit suite
source /workspace/.devcontainer/.env && cargo fmt && cargo check --workspace --all-features && cargo clippy --workspace --all-features -- -D warnings && cargo test --workspace --all-features && cargo fmt --check
During development in git worktrees, running tests requires careful attention to:
/workspace/.devcontainer/.env before testingThis skill codifies the lessons learned from issues #186 and #232 to prevent future confusion and wasted time.
Problem: Repeatedly asking users for credentials when they're already available in the environment.
Solution: Check if environment variables are set BEFORE asking the user.
# โ
CORRECT: Check without exposing values
[ -n "$LANGSMITH_API_KEY" ] && echo "LANGSMITH_API_KEY is set" || echo "LANGSMITH_API_KEY not set"
[ -n "$ANTHROPIC_API_KEY" ] && echo "ANTHROPIC_API_KEY is set" || echo "ANTHROPIC_API_KEY not set"
If not set, source from devcontainer:
# Source devcontainer environment file
source /workspace/.devcontainer/.env
# Verify variables are now set
[ -n "$LANGSMITH_API_KEY" ] && echo "LANGSMITH_API_KEY is set" || echo "LANGSMITH_API_KEY not set"
Never expose actual values:
# โ WRONG: Exposes sensitive data
echo "LANGSMITH_API_KEY=$LANGSMITH_API_KEY"
# โ
CORRECT: Checks without exposing
[ -n "$LANGSMITH_API_KEY" ] && echo "Set" || echo "Not set"
Problem: Running tests from the wrong directory leads to unexpected failures and confusion.
Key Difference: Tests run from worktrees use the feature branch SDK, while tests from main workspace use the main branch SDK.
Example from #186:
/workspace (main branch) โ FAILED (old SDK without Queued enum)/workspace/wip/codekiln-186-test-helpers โ PASSED (new SDK with Queued from PR #185)Solution: Always verify working directory before running tests.
# โ
CORRECT: Verify and navigate to worktree
pwd
cd /workspace/wip/<worktree-name>
# Run tests from worktree
cargo test --test integration_test -- --ignored --nocapture
Different tests have different requirements and durations:
Unit Tests (No API Required):
# Fast (<1 second), no credentials needed
cargo test --test integration_deployment_workflow test_deployment_url_extraction -- --nocapture
Helper Tests (API Required, 1-5 seconds):
# Require credentials, fast feedback
cargo test --test integration_deployment_workflow test_list_github_integrations -- --ignored --nocapture
cargo test --test integration_deployment_workflow test_list_github_repositories -- --ignored --nocapture
cargo test --test integration_deployment_workflow test_find_integration_for_repo -- --ignored --nocapture
Full Workflow Tests (5-30 minutes):
# Long-running, creates real resources
cargo test --test integration_deployment_workflow test_deployment_workflow -- --ignored --nocapture
When working on a feature branch in a worktree, always run tests from the worktree directory.
Steps:
# Step 1: Navigate to worktree
cd /workspace/wip/<worktree-name>
# Step 2: Source environment variables (CRITICAL - do this first!)
source /workspace/.devcontainer/.env
# Step 3: Verify you're in the correct location
pwd
# Should output: /workspace/wip/<worktree-name>
# Step 4: Verify environment variables are loaded
[ -n "$LANGSMITH_API_KEY" ] && echo "โ LANGSMITH_API_KEY is set" || echo "โ LANGSMITH_API_KEY not set"
# Step 5: Run tests
# For unit tests (no API)
cargo test --test integration_deployment_workflow test_deployment_url_extraction -- --nocapture
# For helper tests (fast, require API)
cargo test --test integration_deployment_workflow test_list_github_integrations -- --ignored --nocapture
# For full workflow tests (slow, require API)
cargo test --test integration_deployment_workflow test_deployment_workflow -- --ignored --nocapture
Why this matters:
When testing against the main/release branch SDK, run from main workspace.
Steps:
# Step 1: Navigate to main workspace
cd /workspace
# Step 2: Verify branch
git branch --show-current
# Should output: main (or release/vX.Y.Z)
# Step 3: Check environment variables
[ -n "$LANGSMITH_API_KEY" ] && echo "LANGSMITH_API_KEY is set" || echo "LANGSMITH_API_KEY not set"
# Step 4: Run tests
cargo test --workspace --all-features
Before committing, run all checks from the worktree.
Steps:
# From worktree directory
cd /workspace/wip/<worktree-name>
# CRITICAL: Source environment variables first!
source /workspace/.devcontainer/.env
# Run pre-commit checklist
cargo fmt
cargo check --workspace --all-features
cargo clippy --workspace --all-features -- -D warnings
cargo test --workspace --all-features
cargo fmt --check
Why from worktree:
Wrong approach:
# Running pre-commit checks immediately in worktree without sourcing
cd /workspace/wip/<worktree-name>
cargo fmt && cargo check --workspace --all-features && cargo clippy --workspace --all-features -- -D warnings && cargo test --workspace --all-features && cargo fmt --check
# Tests will fail with "byte index out of bounds" or authentication errors
Symptom: Tests fail with cryptic errors like:
byte index 8 is out of bounds of \``thread 'main' panicked at cli/src/commands/prompt.rs:183:52Correct approach:
# ALWAYS source environment variables FIRST in worktrees
cd /workspace/wip/<worktree-name>
source /workspace/.devcontainer/.env
cargo fmt && cargo check --workspace --all-features && cargo clippy --workspace --all-features -- -D warnings && cargo test --workspace --all-features && cargo fmt --check
Why this happens:
LANGSMITH_API_KEYWrong approach:
# Don't ask user to provide credentials without checking first
echo "Please provide your LANGSMITH_API_KEY"
Correct approach:
# Check first
if [ -z "$LANGSMITH_API_KEY" ]; then
# Try sourcing from devcontainer
source /workspace/.devcontainer/.env
# If still not set, then ask
if [ -z "$LANGSMITH_API_KEY" ]; then
echo "LANGSMITH_API_KEY not found. Please set it in /workspace/.devcontainer/.env"
fi
fi
Wrong approach:
# Running from main workspace when working on feature branch
cd /workspace
cargo test --test integration_deployment_workflow -- --ignored
# May fail due to old SDK version in main branch
Correct approach:
# Always verify pwd first
pwd
cd /workspace/wip/codekiln-186-test-helpers
cargo test --test integration_deployment_workflow -- --ignored
Wrong approach:
# Never expose actual values
echo "LANGSMITH_API_KEY=$LANGSMITH_API_KEY"
echo "Using key: ${LANGSMITH_API_KEY}"
Correct approach:
# Only check if set, never show value
[ -n "$LANGSMITH_API_KEY" ] && echo "LANGSMITH_API_KEY is set" || echo "LANGSMITH_API_KEY not set"
Wrong approach:
# Running background tests from main workspace while developing in worktree
cd /workspace
cargo test --test integration_test -- --ignored &
# This will test old SDK from main, not your changes
Correct approach:
# Run from worktree to test your changes
cd /workspace/wip/<worktree-name>
cargo test --test integration_test -- --ignored
LangSmith API:
LANGSMITH_API_KEY - API key for LangSmith authenticationLANGSMITH_ORGANIZATION_ID - Organization ID (optional, auto-detected)Anthropic API:
ANTHROPIC_API_KEY - API key for Claude models# Check all required variables at once
echo "Checking required environment variables..."
[ -n "$LANGSMITH_API_KEY" ] && echo "โ LANGSMITH_API_KEY is set" || echo "โ LANGSMITH_API_KEY not set"
[ -n "$ANTHROPIC_API_KEY" ] && echo "โ ANTHROPIC_API_KEY is set" || echo "โ ANTHROPIC_API_KEY not set"
# Source from devcontainer if any are missing
if [ -z "$LANGSMITH_API_KEY" ] || [ -z "$ANTHROPIC_API_KEY" ]; then
echo "Sourcing from /workspace/.devcontainer/.env..."
source /workspace/.devcontainer/.env
# Check again
[ -n "$LANGSMITH_API_KEY" ] && echo "โ LANGSMITH_API_KEY is set" || echo "โ LANGSMITH_API_KEY not set"
[ -n "$ANTHROPIC_API_KEY" ] && echo "โ ANTHROPIC_API_KEY is set" || echo "โ ANTHROPIC_API_KEY not set"
fi
# Navigate to worktree
cd /workspace/wip/<worktree-name>
# Source environment (ALWAYS do this first!)
source /workspace/.devcontainer/.env
# Verify location
pwd
# Run specific test
cargo test --test <test_file> <test_name> -- --ignored --nocapture
# Navigate to worktree
cd /workspace/wip/<worktree-name>
# Source environment (ALWAYS do this first!)
source /workspace/.devcontainer/.env
# Run all integration tests (fast ones)
cargo test --test integration_deployment_workflow test_list_ -- --ignored --nocapture
# Navigate to worktree
cd /workspace/wip/<worktree-name>
# Source environment (ALWAYS do this first!)
source /workspace/.devcontainer/.env
# Run test in background
cargo test --test integration_deployment_workflow test_deployment_workflow -- --ignored --nocapture > test_output.log 2>&1 &
# Save PID
echo $! > test_pid.txt
# Monitor progress
tail -f test_output.log
Symptom: Tests fail immediately with authentication errors.
Diagnosis:
# Check if env vars are set
[ -n "$LANGSMITH_API_KEY" ] && echo "Set" || echo "Not set"
Solution:
# Source from devcontainer
source /workspace/.devcontainer/.env
# Verify
[ -n "$LANGSMITH_API_KEY" ] && echo "Set" || echo "Not set"
Symptom: Tests fail with compilation errors about missing types or enum variants.
Diagnosis:
# Check current location
pwd
# If output is /workspace, you're in main workspace (may have old SDK)
Solution:
# Navigate to worktree with feature branch
cd /workspace/wip/<worktree-name>
# Run tests from worktree
cargo test --test integration_test -- --ignored --nocapture
Symptom: Tests work locally in worktree but fail when pushed to CI.
Common Causes:
Diagnosis:
# Test from main workspace to simulate CI environment
cd /workspace
git checkout main
git pull origin main
cargo test --workspace --all-features
Solution:
Symptom: Running tests in background from main workspace shows failures, but running same test from worktree passes.
Cause: Background tests are using main branch SDK, not feature branch SDK.
Solution:
# Always run from worktree when testing feature branch
cd /workspace/wip/<worktree-name>
cargo test --test integration_test -- --ignored --nocapture
โ CORRECT:
# Check without exposing
[ -n "$LANGSMITH_API_KEY" ] && echo "Set" || echo "Not set"
# Use in commands without echoing
cargo test --test integration_test -- --ignored --nocapture
โ WRONG:
# Never do these
echo $LANGSMITH_API_KEY
echo "Key: $LANGSMITH_API_KEY"
env | grep LANGSMITH
Best practices:
/workspace/.devcontainer/.env (gitignored).env files<your-api-key>After creating a worktree, use this skill to run tests:
# Create worktree (from git-worktrees skill)
git worktree add -b codekiln/186-test-helpers wip/codekiln-186-test-helpers main
# Navigate and run tests (from this skill)
cd wip/codekiln-186-test-helpers
[ -n "$LANGSMITH_API_KEY" ] && echo "Set" || source /workspace/.devcontainer/.env
cargo test --test integration_test -- --ignored --nocapture
Before committing, run the pre-commit checklist from worktree:
cd /workspace/wip/<worktree-name>
cargo fmt && \
cargo check --workspace --all-features && \
cargo clippy --workspace --all-features -- -D warnings && \
cargo test --workspace --all-features && \
cargo fmt --check
[ -n "$LANGSMITH_API_KEY" ] && echo "โ LANGSMITH_API_KEY set" || (source /workspace/.devcontainer/.env && [ -n "$LANGSMITH_API_KEY" ] && echo "โ LANGSMITH_API_KEY set after source" || echo "โ LANGSMITH_API_KEY not found")
cd /workspace/wip/<worktree-name> && [ -n "$LANGSMITH_API_KEY" ] || source /workspace/.devcontainer/.env && cargo test --test integration_test -- --ignored --nocapture
# 1. Navigate to worktree
cd /workspace/wip/<worktree-name>
# 2. Source environment variables (CRITICAL FIRST STEP!)
source /workspace/.devcontainer/.env
# 3. Verify location
pwd
# 4. Run tests
cargo test --test integration_test -- --ignored --nocapture
@docs/dev/README.md "Pre-Commit Checklist" section@docs/dev/github-workflow.mdsource /workspace/.devcontainer/.env
cargo test, cargo clippy, cargo check, cargo build may all require environment variablespwd before running tests - Ensure you're in the correct worktreeThe #1 cause of test failures in worktrees is missing environment variables. Always source them first, especially before running pre-commit checks.
The #1 symptom: Cryptic panics like byte index 8 is out of bounds of \`` that disappear after sourcing environment variables.
These principles save time, prevent confusion, and improve security.