nim-lang / Nim

Nim is a statically typed compiled systems programming language. It combines successful concepts from mature languages like Python, Ada and Modula. Its design focuses on efficiency, expressiveness, and elegance (in that order of priority).
https://nim-lang.org
Other
16.55k stars 1.47k forks source link

compiler as a library: findNimStdLibCompileTime fails to find system.nim #12293

Closed mratsim closed 4 years ago

mratsim commented 5 years ago

When trying the compiler as library API, findNimStdLibCompileTime does not find system.nim when the compiler is installed through nimble.

Test case:

nimble install compiler

And then compile:

import compiler/[nimeval, llstream]

proc evalString(code: string, moduleName = "script.nim") =
  let stream = llStreamOpen(code)
  let std = findNimStdLibCompileTime()
  var intr = createInterpreter(moduleName, [std])
  intr.evalScript(stream)
  destroyInterpreter(intr)
  llStreamClose(stream)

let runtimeString = "echo \"Hello World\""
evalString(runtimeString)

Error:

/home/beta/Programming/Nim/Arraymancer/build/ceval.nim(13) ceval
/home/beta/Programming/Nim/Arraymancer/build/ceval.nim(5) evalString
/home/beta/.nimble/pkgs/compiler-1.0.0/compiler/nimeval.nim(98) findNimStdLibCompileTime
/home/beta/.choosenim/toolchains/nim-1.0.0/lib/system/assertions.nim(27) failedAssertImpl
/home/beta/.choosenim/toolchains/nim-1.0.0/lib/system/assertions.nim(20) raiseAssert
/home/beta/.choosenim/toolchains/nim-1.0.0/lib/system/fatal.nim(39) sysFatal
Error: unhandled exception: /home/beta/.nimble/pkgs/compiler-1.0.0/compiler/nimeval.nim(98, 12) `fileExists(result / "system.nim")` result:/home/beta/.nimble/pkgs/compiler-1.0.0/lib [AssertionError]

from: https://github.com/nim-lang/Nim/blob/af6539b0c3b6958251737d5bc140cbcc8652eb8b/compiler/nimeval.nim#L93-L98

Araq commented 5 years ago

findNimStdLibCompileTime is a bad idea. I wonder why we have it.

PMunch commented 4 years ago

Because findNimStdLib doesn't work with choosenim? It could try to run nim dump and find the folder from that, that would probably be the most reliable, or it could at least re-use whatever logic that calls. Right now it is less a findNimStdLib and more of a guessWhereNimStdLibMightBeDependingOnWhereTheCompilerBinaryIs..

Araq commented 4 years ago

The standard library is relative to nim.exe but it's a better idea to ship your own version of the required Nim files. Really, it is, I made the same mistake with NimEdit. The Nim stdlib evolves so fast that it gets outdated with the NimScript version that you ship your program with.

timotheecour commented 4 years ago

there are a few issues going on here: