Skip to content

bug on catching exitcode for powershell commands #2073

@MrLixm

Description

@MrLixm

Hey there,
I was looking at the rez code to have a reference for interacting with power-shell from Python.

At some point I copied this snippet of code which allow to catch the lastexit code:

executor.command(
"if ((Test-Path variable:LASTEXITCODE) -and $LASTEXITCODE) {\n"
" exit $LASTEXITCODE\n"
"}\n"
"if (! $?) {\n"
" exit 1\n"
"}"
)

However on my side it was not working as expected. For example, if you call a command that don't exist before it, the code behave as it was successfull.

From what I found it seems that it's become in that case we should rely on $?, however at the time of inspection, it has already been overwritten by the Test-Path command we call just before. So it's always True.

I manage to fix it by storing it's value in a variable before calling Test-Path

$_op_success = $?
if ((Test-Path variable:LASTEXITCODE) -and $LASTEXITCODE) {
  exit $LASTEXITCODE
}
if (! $_op_success) {
  exit 1
}

Keep in mind that none of this was tested with rez so I might be mistaking.
Liam.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions