# Build-wrapper does not properly return error code of child script

**URL:** <https://community.sonarsource.com/t/build-wrapper-does-not-properly-return-error-code-of-child-script/38676>\
**Category:** SonarQube Server / Community Build\
**Tags:** build-wrapper, cfamily\
**Created:** [February 15, 2021, 10:32am UTC](https://community.sonarsource.com/t/build-wrapper-does-not-properly-return-error-code-of-child-script/38676 "2021-02-15T10:32:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![scm\_invn](https://avatars.discourse-cdn.com/v4/letter/s/f05b48/32.png) [@scm\_invn](https://community.sonarsource.com/u/scm_invn)\
**Post date:** [February 15, 2021, 10:32am UTC](https://community.sonarsource.com/t/build-wrapper-does-not-properly-return-error-code-of-child-script/38676/1 "2021-02-15T10:32:20Z")

</div>

Template for a good bug report, formatted with [Markdown](https://commonmark.org/help/):

- versions used

- ERRORLEVEL value of child script is not properly returned in following scenario:

- steps to reproduce

```auto
@echo off
if "%1" == "ifexitnob" (
    echo into if, exit without /b
    pushd subfolder
    exit 1
    popd
)
if "%1" == "ifexitb" (
    echo into if, exit /b
    pushd subfolder
    exit /b 1
    popd
)

```

put above script into a script.bat file, then call it from build-wrapper and print ERRORLEVEL.

```auto
build-wrapper-win-x86-64.exe --out-dir test script.bat ifexitnob
echo %ERRORLEVEL% --> Will show 1, OK

build-wrapper-win-x86-64.exe --out-dir test script.bat ifexitb
echo %ERRORLEVEL% --> Will show 0, KO

```

- potential workaround  
Do not use /b option of exit command, although it is common practice for Windows Batch scripting

---

<div class="post-metadata">

**Author:** ![mpaladin](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/mpaladin/32/118_2.png) [@mpaladin](https://community.sonarsource.com/u/mpaladin)\
**Post date:** [February 15, 2021, 4:03pm UTC](https://community.sonarsource.com/t/build-wrapper-does-not-properly-return-error-code-of-child-script/38676/2 "2021-02-15T16:03:02Z")

</div>

Hi @scm_invn ,

I’ve tried what you posted and it seems to work:

```auto
C:\cfam>build-wrapper-win-x86\build-wrapper-win-x86-64.exe --out-dir cfam-out script.bat ifexitb
into if, exit /b

C:\cfam>echo %ERRORLEVEL%
1

```

what version of the build-wrapper are you using? Could you please share the build-wrapper output dir that you named `test`? We had this bug and it has been fixed 4 years ago, see [CPP-1372](https://jira.sonarsource.com/browse/CPP-1372).

---

<div class="post-metadata">

**Author:** ![scm\_invn](https://avatars.discourse-cdn.com/v4/letter/s/f05b48/32.png) [@scm\_invn](https://community.sonarsource.com/u/scm_invn)\
**Post date:** [February 15, 2021, 5:06pm UTC](https://community.sonarsource.com/t/build-wrapper-does-not-properly-return-error-code-of-child-script/38676/3 "2021-02-15T17:06:49Z")

</div>

Sure, version of BW is 6.11.  
See attached output files, nothing relevant.  
[testnob\_build-wrapper.log](https://community.sonarsource.com/uploads/short-url/dCuFWX80nVBX4MWp7oBUfmflmFc.log) (966 Bytes)  
[testb\_build-wrapper.log](https://community.sonarsource.com/uploads/short-url/lwR4dmGKdMkgTGE1q2nsIlTEhli.log) (962 Bytes)

```auto
@echo off
mkdir subfolder 2> nul
pushd subfolder
if "%1" == "ifexitb" (
    echo into if, exit /b
    exit /b 1
    popd
)
if "%1" == "ifexitnob" (
    echo into if, exit without /b
    exit 1
    popd
)

```

Issue seems related to underlying cmd.exe call strange behavior.  
Probably above script can replicate the issue on your side.  
If it is the case, try removing the popd statement at line 7, and now it works fine…

```auto
C:\WINDOWS\system32\cmd.exe /c script.bat ifexitnob
echo %ERRORLEVEL% --> Will show 1, OK

C:\WINDOWS\system32\cmd.exe /c script.bat ifexitb
echo %ERRORLEVEL% --> Will show 0, KO

```

So it is probably not a build-wrapper issue

Edit: not reproducible on all our machines, so this is not an issue to spend much time on. Just applying the workaround.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/system/32/31083_2.png) [@system](https://community.sonarsource.com/u/system)\
**Post date:** [February 22, 2021, 5:06pm UTC](https://community.sonarsource.com/t/build-wrapper-does-not-properly-return-error-code-of-child-script/38676/4 "2021-02-22T17:06:55Z")

</div>

Closed on February 22, 2021, 5:06pm UTC

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
