From 9a8519df87fe56f66792de63775f52a8c091fb1e Mon Sep 17 00:00:00 2001 From: dimgigov Date: Tue, 12 May 2026 11:28:12 +0300 Subject: [PATCH] feat(cli): add --lib-path flag and unify getLibPath logic - Add --lib-path global CLI flag (parsed before subcommand) - getLibPath() now checks CLI override first, then CLJNIM_LIB_PATH env, then current dir lib/, then app dir lib/ - nimCompile always includes compiler runtime lib path so cljnim_runtime is found even when --lib-path points to a project-specific directory - Sync repl.nim getLibPath() to match cljnim.nim (env var + current dir) --- src/cljnim.nim | 42 ++++++++++++++++++++++++++++++------------ src/repl.nim | 9 +++++++-- 2 files changed, 37 insertions(+), 14 deletions(-) diff --git a/src/cljnim.nim b/src/cljnim.nim index efbf7c4..a4e0cb1 100644 --- a/src/cljnim.nim +++ b/src/cljnim.nim @@ -2,8 +2,13 @@ import os, osproc, strutils, times import reader, emitter, types, repl, macros, deps, ai_assist, tui +var libPathOverride* = "" + proc getLibPath(): string = - # Check environment variable first + # CLI --lib-path takes highest precedence + if libPathOverride.len > 0 and dirExists(libPathOverride): + return libPathOverride + # Check environment variable let envPath = getEnv("CLJNIM_LIB_PATH", "") if envPath.len > 0 and dirExists(envPath): return envPath @@ -131,9 +136,13 @@ proc compileFileLib*(inputPath: string, outputPath: string, extraPaths: seq[stri proc nimCompile(nimPath: string, binPath: string, release: bool = false): tuple[exitCode: int, output: string] = let libPath = getLibPath() + let compilerLib = getAppDir() / "lib" var cmd = "nim c" if release: cmd.add(" -d:release") + # Always include compiler runtime lib so cljnim_runtime is found + if dirExists(compilerLib) and compilerLib != libPath: + cmd.add(" --path:" & quoteShell(compilerLib)) cmd.add(" --path:" & quoteShell(libPath)) cmd.add(" -o:" & quoteShell(binPath) & " " & quoteShell(nimPath)) echo "Compiling: ", cmd @@ -215,20 +224,29 @@ proc runFile*(inputPath: string) = proc main() = initBuiltinMacros() - let args = commandLineParams() - + var args = commandLineParams() + + # Strip leading global flags + if args.len >= 2 and args[0] == "--lib-path": + libPathOverride = args[1] + args = args[2..^1] + if args.len == 0: echo "Bara Lang — AI-First Compiler" echo "" echo "Usage:" - echo " cljnim compile [output.nim] Compile to Nim source" - echo " cljnim run Compile and run binary" - echo " cljnim read Parse and print AST" - echo " cljnim repl [--json] Start REPL (human or AI mode)" - echo " cljnim deps Resolve dependencies from deps.edn" - echo " cljnim -e '' Evaluate expression" - echo " cljnim ai '' Generate Bara Lang code with AI" - echo " cljnim tui Start interactive TUI" + echo " cljnim [--lib-path ] compile [output.nim]" + echo " cljnim [--lib-path ] run " + echo " cljnim [--lib-path ] read " + echo " cljnim repl [--json]" + echo " cljnim deps" + echo " cljnim -e ''" + echo " cljnim ai ''" + echo " cljnim tui" + echo "" + echo "Global Options:" + echo " --lib-path Additional Nim library search path" + echo " (also via CLJNIM_LIB_PATH env var)" echo "" echo "REPL Commands:" echo " :quit, :q Exit REPL" @@ -241,7 +259,7 @@ proc main() = echo " Input: {\"op\":\"eval\",\"form\":\"(+ 1 2)\"}" echo " Output: {\"status\":\"ok\",\"result\":{...},\"meta\":{...}}" quit(0) - + let cmd = args[0] # Handle -e flag diff --git a/src/repl.nim b/src/repl.nim index be45393..13b192d 100644 --- a/src/repl.nim +++ b/src/repl.nim @@ -16,13 +16,18 @@ type evalEnv*: Env proc getLibPath(): string = + # Check environment variable first + let envPath = getEnv("CLJNIM_LIB_PATH", "") + if envPath.len > 0 and dirExists(envPath): + return envPath + # Check current directory's lib first (project-local libs) + let candidate0 = getCurrentDir() / "lib" + if dirExists(candidate0): return candidate0 let appDir = getAppDir() let candidate1 = appDir / "lib" let candidate2 = appDir.parentDir / "lib" - let candidate3 = getCurrentDir() / "lib" if dirExists(candidate1): return candidate1 if dirExists(candidate2): return candidate2 - if dirExists(candidate3): return candidate3 return "lib" proc initReplState*(mode: ReplMode): ReplState =