-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path.envrc
More file actions
140 lines (117 loc) · 4.71 KB
/
Copy path.envrc
File metadata and controls
140 lines (117 loc) · 4.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/bin/bash
# Create interceptor directory
mkdir -p .direnv/bin
# Python interceptor
cat > .direnv/bin/python <<'PYTHON_EOF'
#!/bin/bash
# Intercept bare python calls and redirect to uv
ROOT=$(git rev-parse --show-toplevel 2>/dev/null || echo ".")
CURRENT=$(pwd)
if [ -f "pyproject.toml" ] && [ "$CURRENT" != "$ROOT" ]; then
echo "Warning: Running python from subdirectory with pyproject.toml ($CURRENT)" >&2
echo " May cause errors or create local .venv/. To avoid: cd $ROOT && uv run python" >&2
fi
echo "→ Redirecting to: uv run python" >&2
# Remove .direnv/bin from PATH to prevent recursion
PATH=$(echo "$PATH" | tr ':' '\n' | grep -v '.direnv/bin' | tr '\n' ':' | sed 's/:$//')
exec uv run python "$@"
PYTHON_EOF
# Python3 interceptor (same behavior)
cat > .direnv/bin/python3 <<'PYTHON3_EOF'
#!/bin/bash
# Intercept bare python3 calls and redirect to uv
ROOT=$(git rev-parse --show-toplevel 2>/dev/null || echo ".")
CURRENT=$(pwd)
if [ -f "pyproject.toml" ] && [ "$CURRENT" != "$ROOT" ]; then
echo "Warning: Running python3 from subdirectory with pyproject.toml ($CURRENT)" >&2
echo " May cause errors or create local .venv/. To avoid: cd $ROOT && uv run python" >&2
fi
echo "→ Redirecting to: uv run python" >&2
# Remove .direnv/bin from PATH to prevent recursion
PATH=$(echo "$PATH" | tr ':' '\n' | grep -v '.direnv/bin' | tr '\n' ':' | sed 's/:$//')
exec uv run python "$@"
PYTHON3_EOF
# pip interceptor (prevent accidental bare pip)
cat > .direnv/bin/pip <<'PIP_EOF'
#!/bin/bash
# Block bare pip, show helpful error
ROOT=$(git rev-parse --show-toplevel 2>/dev/null || echo ".")
CURRENT=$(pwd)
if [ -f "pyproject.toml" ] && [ "$CURRENT" != "$ROOT" ]; then
echo "Warning: Running pip from subdirectory with pyproject.toml ($CURRENT)" >&2
echo " May cause errors or create local .venv/. To avoid: cd $ROOT && uv pip ..." >&2
echo "" >&2
fi
echo "✗ Bare 'pip' detected!" >&2
echo "" >&2
echo "Instead of 'pip install <package>', use:" >&2
echo " uv add <package>" >&2
echo "" >&2
echo "To sync lockfile:" >&2
echo " uv pip sync requirements-dev.lock" >&2
exit 1
PIP_EOF
# pytest interceptor
cat > .direnv/bin/pytest <<'PYTEST_EOF'
#!/bin/bash
# Intercept bare pytest calls and redirect to uv
ROOT=$(git rev-parse --show-toplevel 2>/dev/null || echo ".")
CURRENT=$(pwd)
if [ -f "pyproject.toml" ] && [ "$CURRENT" != "$ROOT" ]; then
echo "Warning: Running pytest from subdirectory with pyproject.toml ($CURRENT)" >&2
echo " May cause errors or create local .venv/. To avoid: cd $ROOT && uv run pytest" >&2
fi
# Remove .direnv/bin from PATH to prevent recursion
PATH=$(echo "$PATH" | tr ':' '\n' | grep -v '.direnv/bin' | tr '\n' ':' | sed 's/:$//')
exec uv run pytest "$@"
PYTEST_EOF
# ruff interceptor
cat > .direnv/bin/ruff <<'RUFF_EOF'
#!/bin/bash
# Intercept bare ruff calls and redirect to uv
ROOT=$(git rev-parse --show-toplevel 2>/dev/null || echo ".")
CURRENT=$(pwd)
if [ -f "pyproject.toml" ] && [ "$CURRENT" != "$ROOT" ]; then
echo "Warning: Running ruff from subdirectory with pyproject.toml ($CURRENT)" >&2
echo " May cause errors or create local .venv/. To avoid: cd $ROOT && uv run ruff" >&2
fi
# Remove .direnv/bin from PATH to prevent recursion
PATH=$(echo "$PATH" | tr ':' '\n' | grep -v '.direnv/bin' | tr '\n' ':' | sed 's/:$//')
exec uv run ruff "$@"
RUFF_EOF
# mypy interceptor
cat > .direnv/bin/mypy <<'MYPY_EOF'
#!/bin/bash
# Intercept bare mypy calls and redirect to uv
ROOT=$(git rev-parse --show-toplevel 2>/dev/null || echo ".")
CURRENT=$(pwd)
if [ -f "pyproject.toml" ] && [ "$CURRENT" != "$ROOT" ]; then
echo "Warning: Running mypy from subdirectory with pyproject.toml ($CURRENT)" >&2
echo " May cause errors or create local .venv/. To avoid: cd $ROOT && uv run mypy" >&2
fi
# Remove .direnv/bin from PATH to prevent recursion
PATH=$(echo "$PATH" | tr ':' '\n' | grep -v '.direnv/bin' | tr '\n' ':' | sed 's/:$//')
exec uv run mypy "$@"
MYPY_EOF
# Make executables
chmod +x .direnv/bin/python
chmod +x .direnv/bin/python3
chmod +x .direnv/bin/pip
chmod +x .direnv/bin/pytest
chmod +x .direnv/bin/ruff
chmod +x .direnv/bin/mypy
# Add to PATH (highest priority)
PATH_add .direnv/bin
# Auto-add Node bin to PATH based on .nvmrc
# (avoids calling nvm as command, which doesn't work in direnv's restricted context)
if [ -f .nvmrc ]; then
NODE_VERSION=$(cat .nvmrc)
# Find matching Node version directory (handles "24" matching "v24.11.0")
if [ -d "$HOME/.nvm/versions/node" ]; then
NODE_PATH=$(find "$HOME/.nvm/versions/node" -maxdepth 1 -name "v${NODE_VERSION}*" | head -1)
if [ -n "$NODE_PATH" ] && [ -d "$NODE_PATH/bin" ]; then
PATH_add "$NODE_PATH/bin"
fi
fi
fi
echo "✓ direnv loaded: Python tools intercepted (python, pip, pytest, ruff, mypy), Node version managed"