Skip to content

Commit

Permalink
Fixed an issue where errorCode was always 0 when startProcess did… (
Browse files Browse the repository at this point in the history
nim-lang#23992)

…n't use the `poEvalCommand` flag

https://forum.nim-lang.org/t/12310

Added a test case, tested on my fedora system.
  • Loading branch information
haoyu234 authored Aug 21, 2024
1 parent e38cbd3 commit 12b90d7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
11 changes: 7 additions & 4 deletions lib/pure/osproc.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1123,14 +1123,13 @@ elif not defined(useNimRtl):
var error: cint
let sizeRead = read(data.pErrorPipe[readIdx], addr error, sizeof(error))
if sizeRead == sizeof(error):
raiseOSError(osLastError(),
raiseOSError(OSErrorCode(error),
"Could not find command: '" & $data.sysCommand & "'. OS error: " & $strerror(error))

return pid

{.push stacktrace: off, profiler: off.}
proc startProcessFail(data: ptr StartProcessData) =
var error: cint = errno
proc startProcessFail(data: ptr StartProcessData, error: cint = errno) =
discard write(data.pErrorPipe[writeIdx], addr error, sizeof(error))
exitnow(1)

Expand Down Expand Up @@ -1167,7 +1166,11 @@ elif not defined(useNimRtl):
if (poUsePath in data.options):
when defined(uClibc) or defined(linux) or defined(haiku):
# uClibc environment (OpenWrt included) doesn't have the full execvpe
let exe = findExe(data.sysCommand)
var exe: string
try:
exe = findExe(data.sysCommand)
except OSError as e:
startProcessFail(data, e.errorCode)
discard execve(exe.cstring, data.sysArgs, data.sysEnv)
else:
# MacOSX doesn't have execvpe, so we need workaround.
Expand Down
18 changes: 18 additions & 0 deletions tests/osproc/tnoexe.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
discard """
output: '''true
true'''
"""

import std/osproc

const command = "lsaaa -lah"

try:
let process = startProcess(command, options = {poUsePath})
discard process.waitForExit()
except OSError as e:
echo e.errorCode != 0

let process = startProcess(command, options = {poUsePath, poEvalCommand})
let exitCode = process.waitForExit()
echo exitCode != 0

0 comments on commit 12b90d7

Please sign in to comment.