diff --git a/docs/ROADMAP.md b/docs/ROADMAP.md index 92a0e8a..4e585a2 100644 --- a/docs/ROADMAP.md +++ b/docs/ROADMAP.md @@ -79,10 +79,8 @@ ## Known Issues - `->>` threading macro with nested `map`/`reduce` requires proper macro expansion context -- REPL definitions are re-compiled from scratch on each evaluation (slow but correct — interpreter path handles most cases in <0.1ms now) -- ffi/interop examples fail — `sin` symbol not found (Nim module import issue, pre-existing) ## Recent Bug Fixes (2026-05-08) +- Fixed: ffi/interop examples — temp files now use isolated subdirectories to avoid shadowing Nim stdlib modules (e.g., `math.nim` shadowing `import math`) - Fixed: `quot`/`rem` — added `cljQuot`/`cljRem` to runtime + interpreter support -- Fixed: `rem` — interpreter and compiled path both now handle `(rem n d)` - Cleanup: removed unused `processAgentActions` proc, unused `sequtils` import in repl.nim diff --git a/src/cljnim.nim b/src/cljnim.nim index 5fcada9..521f615 100644 --- a/src/cljnim.nim +++ b/src/cljnim.nim @@ -111,11 +111,17 @@ proc nimCompile(nimPath: string, binPath: string, release: bool = false): int = echo "Compiling: ", cmd execCmd(cmd) +proc makeTempDir(): string = + let pid = getCurrentProcessId() + let ts = epochTime().int + result = getTempDir() / "cljnim_build_" & $pid & "_" & $ts + createDir(result) + proc runFile*(inputPath: string) = - let tmpDir = getTempDir() let baseName = inputPath.splitFile.name - let nimPath = tmpDir / baseName & "_generated.nim" - let binPath = tmpDir / baseName & "_generated" + let buildDir = makeTempDir() + let nimPath = buildDir / baseName & ".nim" + let binPath = buildDir / baseName # Resolve project dependencies let inputDir = inputPath.parentDir @@ -124,8 +130,9 @@ proc runFile*(inputPath: string) = # Check nimcache for cached output let projectDir = inputPath.parentDir let cacheDir = projectDir / "nimcache" - let cacheNimPath = cacheDir / baseName & ".nim" - let cacheBinPath = cacheDir / baseName + let cacheSubDir = cacheDir / baseName + let cacheNimPath = cacheSubDir / baseName & ".nim" + let cacheBinPath = cacheSubDir / baseName var useCache = false if fileExists(cacheNimPath) and fileExists(cacheBinPath): @@ -140,6 +147,7 @@ proc runFile*(inputPath: string) = if compileResult != 0: stderr.writeLine("Compilation failed (cached)") quit(1) + try: removeDir(buildDir) except: discard let runResult = execCmd(cacheBinPath) quit(runResult) @@ -148,11 +156,12 @@ proc runFile*(inputPath: string) = let compileResult = nimCompile(nimPath, binPath) if compileResult != 0: stderr.writeLine("Compilation failed") + try: removeDir(buildDir) except: discard quit(1) # Save to cache try: - createDir(cacheDir) + createDir(cacheSubDir) copyFile(nimPath, cacheNimPath) copyFile(binPath, cacheBinPath) echo "Cached: ", cacheNimPath @@ -160,6 +169,7 @@ proc runFile*(inputPath: string) = discard let runResult = execCmd(binPath) + try: removeDir(buildDir) except: discard if runResult != 0: stderr.writeLine("Execution failed") quit(1) @@ -199,17 +209,19 @@ proc main() = stderr.writeLine("Error: Missing expression after -e") quit(1) let code = args[1] - let tmpDir = getTempDir() - let nimPath = tmpDir / "cljnim_expr.nim" - let binPath = tmpDir / "cljnim_expr" + let buildDir = makeTempDir() + let nimPath = buildDir / "expr.nim" + let binPath = buildDir / "expr" let forms = reader.readAll(code) let nimCode = emitter.emitProgram(forms) writeFile(nimPath, nimCode) let compileResult = nimCompile(nimPath, binPath) if compileResult != 0: stderr.writeLine("Compilation failed") + try: removeDir(buildDir) except: discard quit(1) let runResult = execCmd(binPath) + try: removeDir(buildDir) except: discard quit(runResult) case cmd