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

exec in nimscript should have a way to check the result #6262

Closed dandevelo closed 5 years ago

dandevelo commented 7 years ago

I am trying to check for the result of the build step execution in order to run the app only if the build succeeded and run some custom code in case it didn't.

I have this code in a nimble task:

task debug, "Builds the debug version":
  echo("This is the debug build")
  try:
    exec "nim c -o:theapp main.nim"
    exec "./theapp"
  except Exception as foo:
    echo("Build failed. Can not run executable: " & foo.msg)

This fails with:

template/generic instantiation from here
SIGSEGV: Illegal storage access. (Attempt to read from nil?)

Is there another way to check for the exec result in nimscript?

kaushalmodi commented 5 years ago

As of devel build 2039dad2736909a9d2091b137eeab4293b508e12, try / except seems to work.

Below example:

# Create a new directory and save this snippet as config.nims.
# Then run `nim debug`.
task debug, "Builds the debug version":
  echo "This is the debug build"
  try:
    exec("./foo")
  except:
    echo "error happened"
  setCommand("nop")

outputs:

This is the debug build
sh: ./foo: No such file or directory
error happened

But if we try to get details about the exception it does not work.

So below:

# Create a new directory and save this snippet as config.nims.
# Then run `nim debug`.
task debug, "Builds the debug version":
  echo "This is the debug build"
  try:
    exec("./foo")
  except:
    echo "[Error] " & getCurrentException().name & ": " & getCurrentException().msg
  setCommand("nop")

results in this error:

config.nims(3, 6) template/generic instantiation of `task` from here
config.nims(8, 23) Error: undeclared identifier: 'getCurrentException'

So may be the issue should be retitled to:

getCurrentException() does not work in Nimscript

kaushalmodi commented 5 years ago

On the other hand, if getCurrentException() is designed to not get imported in Nimscripts, then this issue can be closed.

/cc @Araq

/cc @genotrance (copying you because I think https://github.com/nim-lang/Nim/issues/10240 was probably related to this?)

genotrance commented 5 years ago

You should be able to use gorgeEx() in nimscript.

Meanwhile, we can consider exposing getCurrentException().

kaushalmodi commented 5 years ago

You should be able to use gorgeEx() in nimscript.

Awesome! gorgeEx works.

Below code:

task debug, "Builds the debug version":
  echo "This is the debug build"
  for cmd in @["./foo", "ls"]:
    let
     (outStr, errCode) = gorgeEx(cmd)
    echo "cmd: " & cmd
    echo "output string: " & outStr
    echo "error code: " & $errCode
    echo ""
  setCommand("nop")

outputs:

This is the debug build
cmd: ./foo
output string: /bin/sh: ./foo: No such file or directory
error code: 127

cmd: ls
output string: config.nims
error code: 0

Meanwhile, we can consider exposing getCurrentException().

👍