Skip to content

Commit

Permalink
fix nim-lang#1994 gorge, staticExec now error at CT on exitCode !=0
Browse files Browse the repository at this point in the history
  • Loading branch information
timotheecour committed Jan 18, 2019
1 parent 214f48e commit 476f1e0
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 6 deletions.
5 changes: 4 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@
- two poorly documented and not used modules (`subexes`, `scgi`) were moved to
graveyard (they are available as Nimble packages)


- `gorge`, `staticExec` now give compile time error if exitCode is not 0 instead of
silently ignoring errors. See https://github.com/nim-lang/Nim/issues/1994#issuecomment-327904129
Use `--experimental:gorgeIgnoreExitCodeDeprecated` to get old behavior, or use
`gorgeEx` to get `tuple[output: string, exitCode: int]`.

#### Breaking changes in the compiler

Expand Down
2 changes: 2 additions & 0 deletions compiler/options.nim
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ type
forLoopMacros,
caseStmtMacros,
codeReordering,
# deprecated behavior `foo` is named: `fooDeprecated`
gorgeIgnoreExitCodeDeprecated,

SymbolFilesOption* = enum
disabledSf, writeOnlySf, readOnlySf, v2Sf
Expand Down
12 changes: 9 additions & 3 deletions compiler/vm.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1429,9 +1429,15 @@ proc rawExecute(c: PCtx, start: int, tos: PStackFrame): TFullReg =
let rd = c.code[pc].regA

createStr regs[ra]
regs[ra].node.strVal = opGorge(regs[rb].node.strVal,
regs[rc].node.strVal, regs[rd].node.strVal,
c.debug[pc], c.config)[0]
let cmd = regs[rb].node.strVal
let input = regs[rc].node.strVal
let ret = opGorge(cmd, input, regs[rd].node.strVal, c.debug[pc], c.config)
# note: can't be enabled locally via {.experimental: gorgeIgnoreExitCodeDeprecated.},
# as to be a global flag, unless VM starts supporting this (eg with `c.features`)
if gorgeIgnoreExitCodeDeprecated notin c.config.features:
if ret[1] != 0:
stackTrace(c, tos, pc, "gorge failed: " & $(exitCode: ret[1], cmd: cmd, input: input))
regs[ra].node.strVal = ret[0]
else:
globalError(c.config, c.debug[pc], "VM is not built with 'gorge' support")
of opcNError, opcNWarning, opcNHint:
Expand Down
7 changes: 5 additions & 2 deletions tests/vm/tvmops.nim
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import os
import math
import strutils

const nonexistant = "D20190116T211842"

template forceConst(a: untyped): untyped =
## Force evaluation at CT, useful for example here:
## `callFoo(forceConst(getBar1()), getBar2())`
Expand All @@ -22,7 +24,7 @@ static:
let ret = gorgeEx(nim & " --version")
doAssert ret.exitCode == 0
doAssert ret.output.contains "Nim Compiler"
let ret2 = gorgeEx(nim & " --unexistant")
let ret2 = gorgeEx(nim & " --" & nonexistant)
doAssert ret2.exitCode != 0
let output3 = gorge(nim & " --version")
doAssert output3.contains "Nim Compiler"
Expand All @@ -44,4 +46,5 @@ block:
# Check against bugs like #9176
doAssert getCurrentCompilerExe() == forceConst(getCurrentCompilerExe())
if false: #pending #9176
doAssert gorgeEx("unexistant") == forceConst(gorgeEx("unexistant"))
doAssert gorgeEx(nonexistant) == forceConst(gorgeEx(nonexistant))

15 changes: 15 additions & 0 deletions tests/vm/tvmops_fail.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
discard """
errormsg: '''gorge failed: (exitCode: 127, cmd: "D20190116T211842", input: "")'''
"""

# issue #1994

#[
This doesn't work as VM error isn't catchable
block:
static:
doAssertRaises(AssertionError):
]#

const nonexistant = "D20190116T211842"
const a = gorge(nonexistant)

0 comments on commit 476f1e0

Please sign in to comment.