Skip to content

Commit 11b6bf3

Browse files
committed
1 parent d2ec2fd commit 11b6bf3

1 file changed

Lines changed: 48 additions & 42 deletions

File tree

bin/lean-check

Lines changed: 48 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,51 @@
1-
#!/usr/bin/env bun
2-
/**
3-
* Fast Lean compile checker backed by the persistent REPL.
4-
*
5-
* Usage: lean-check <file.lean>
6-
*
7-
* When MATHCODE_LEAN_REPL=1, uses the persistent Lean LSP server for
8-
* sub-second compile checks. Falls back to `lake env lean` otherwise.
9-
*
10-
* Designed to be used by the agent-mode prover instead of raw
11-
* `lake env lean` for dramatically faster iteration.
12-
*/
13-
const file = process.argv[2]
14-
if (!file) {
15-
console.error('Usage: lean-check <file.lean>')
16-
process.exit(1)
17-
}
1+
#!/usr/bin/env bash
2+
# Fast Lean compile checker backed by the persistent REPL.
3+
#
4+
# Usage: lean-check <file.lean>
5+
#
6+
# Routes through `mathcode --compile-check` which uses the persistent
7+
# Lean REPL for sub-second checks. Falls back to `lake env lean` if
8+
# mathcode is not available.
9+
set -euo pipefail
1810

19-
const { resolve } = await import('node:path')
20-
const absFile = resolve(file)
11+
FILE="${1:-}"
12+
if [[ -z "$FILE" ]]; then
13+
echo "Usage: lean-check <file.lean>" >&2
14+
exit 1
15+
fi
2116

22-
try {
23-
const { getRepl } = await import('../src/autolean/leanRepl.js')
24-
const repl = await getRepl()
25-
if (repl) {
26-
const { ok, log } = await repl.compileFile(absFile)
27-
if (!ok) {
28-
console.error(log)
29-
process.exit(1)
30-
}
31-
console.log('ok')
32-
process.exit(0)
33-
}
34-
} catch {
35-
// REPL not available — fall through
36-
}
17+
# Resolve FILE to an absolute path relative to the caller's cwd, before any
18+
# downstream (e.g. bin/mathcode) changes directory. The -- delimiter keeps
19+
# dirname/basename from treating hyphen-prefixed filenames as options, and
20+
# ${ABS_DIR%/} strips a trailing slash so files in / don't become //foo.
21+
if [[ ! -e "$FILE" ]]; then
22+
echo "lean-check: file not found: $FILE" >&2
23+
exit 1
24+
fi
25+
ABS_DIR="$(cd -- "$(dirname -- "$FILE")" && pwd)"
26+
ABS_FILE="${ABS_DIR%/}/$(basename -- "$FILE")"
3727

38-
// Fallback: lake env lean
39-
const leanProjectDir = process.env.LEAN_PROJECT_DIR || resolve(process.cwd(), 'lean-workspace')
40-
const proc = Bun.spawn(['lake', 'env', 'lean', absFile], {
41-
cwd: leanProjectDir,
42-
stdout: 'inherit',
43-
stderr: 'inherit',
44-
})
45-
process.exit(await proc.exited)
28+
SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
29+
ROOT_DIR="$(cd -- "$SCRIPT_DIR/.." && pwd)"
30+
31+
# Try mathcode --compile-check (uses REPL, works in both source and binary mode)
32+
MATHCODE=""
33+
if [[ -x "$ROOT_DIR/mathcode" ]]; then
34+
MATHCODE="$ROOT_DIR/mathcode"
35+
elif [[ -x "$SCRIPT_DIR/mathcode" ]]; then
36+
MATHCODE="$SCRIPT_DIR/mathcode"
37+
fi
38+
39+
if [[ -n "$MATHCODE" ]]; then
40+
exec "$MATHCODE" --compile-check "$ABS_FILE"
41+
fi
42+
43+
# Source mode: try bun directly
44+
if command -v bun &>/dev/null && [[ -f "$ROOT_DIR/src/entrypoints/cli.tsx" ]]; then
45+
exec bun "$ROOT_DIR/src/entrypoints/cli.tsx" --compile-check "$ABS_FILE"
46+
fi
47+
48+
# Fallback: lake env lean (slow, no REPL). Must run from a Lake project dir.
49+
LEAN_PROJECT_DIR="${LEAN_PROJECT_DIR:-$ROOT_DIR/lean-workspace}"
50+
cd -- "$LEAN_PROJECT_DIR"
51+
exec lake env lean "$ABS_FILE"

0 commit comments

Comments
 (0)