Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bash scripts return wrong exit codes on Windows #2783

Closed
oleg-mastriukov opened this issue Mar 20, 2024 · 7 comments
Closed

Bash scripts return wrong exit codes on Windows #2783

oleg-mastriukov opened this issue Mar 20, 2024 · 7 comments
Labels
investigating We're actively investigating this issue validated Version information for this issue has been validated

Comments

@oleg-mastriukov
Copy link

Summary

I'm adding if-then statement in my bash script to check the result of the execution:

sf org create scratch --alias $ORGNAME --set-default --definition-file $DEFFILE --duration-days $DURATION
org_create_scratch_result=$?
if [ $org_create_scratch_result != 0 ];
then
    echo 'Error code: ' $org_create_scratch_result
    echo -e "$BRed Scratch not created. Shell script stopped $Color_Off"
    exit 1
fi

The problem is that even though a scratch org is successfully created, the org_create_scratch_result variable equals 1 instead of 0:
Screenshot_5

Steps To Reproduce

  1. create a valid project-scratch-def file
  2. create a bash script containing a code snippet mentioned above
  3. run the bash script
  4. examine results

Expected result

If a scratch org was successfully created with no errors, sf cli sets the $? variable to 0 after execution

Actual result

sf cli always sets the $? variable always to 1 after execution.

System Information

{
  "architecture": "win32-x64",
  "cliVersion": "@salesforce/cli/2.32.8",
  "nodeVersion": "node-v20.11.1",
  "osVersion": "Windows_NT 10.0.19045",
  "rootPath": "C:\\Users\\olegm\\AppData\\Local\\sf\\client\\2.32.8-aa146b8",
  "shell": "C:\\Program Files\\Git\\usr\\bin\\bash.exe",
  "pluginVersions": [
    "@oclif/plugin-autocomplete 3.0.12 (core)",
    "@oclif/plugin-commands 3.2.0 (core)",
    "@oclif/plugin-help 6.0.17 (core)",
    "@oclif/plugin-not-found 3.0.14 (core)",
    "@oclif/plugin-plugins 4.3.2 (core)",
    "@oclif/plugin-search 1.0.20 (core)",
    "@oclif/plugin-update 4.2.0 (core)",
    "@oclif/plugin-version 2.0.14 (core)",
    "@oclif/plugin-warn-if-update-available 3.0.13 (core)",
    "@oclif/plugin-which 3.1.3 (core)",
    "@salesforce/cli 2.32.8 (core)",
    "apex 3.0.26 (core)",
    "auth 3.3.21 (core)",
    "data 3.1.7 (core)",
    "deploy-retrieve 3.2.27 (core)",
    "dev 2.1.15 (user)",
    "info 3.0.28 (core)",
    "limits 3.1.13 (core)",
    "marketplace 1.0.26 (core)",
    "org 3.4.1 (core)",
    "packaging 2.1.11 (core)",
    "schema 3.1.7 (core)",
    "settings 2.0.31 (core)",
    "sobject 1.1.17 (core)",
    "source 3.1.18 (core)",
    "telemetry 3.1.15 (core)",
    "templates 56.0.21 (core)",
    "trust 3.3.16 (core)",
    "user 3.3.1 (core)"
  ]
}
@oleg-mastriukov oleg-mastriukov added the investigating We're actively investigating this issue label Mar 20, 2024
Copy link

Thank you for filing this issue. We appreciate your feedback and will review the issue as soon as possible. Remember, however, that GitHub isn't a mechanism for receiving support under any agreement or SLA. If you require immediate assistance, contact Salesforce Customer Support.

@github-actions github-actions bot added the validated Version information for this issue has been validated label Mar 20, 2024
@cristiand391
Copy link
Member

I can't repro on latest sf:

➜  dreamhouse-lwc git:(main) sf -v
@salesforce/cli/2.34.6 darwin-x64 node-v20.10.0

Screenshot 2024-03-20 at 15 53 47

I see you are using git-bash on windows which isn't fully supported, do you also get an exit code > 0 on a successful scratch org create on powershell?

@oleg-mastriukov
Copy link
Author

oleg-mastriukov commented Mar 22, 2024

Hey @cristiand391 thanks for your answer! Does this mean in my .sh scripts I can't somehow verify the command execution status?

@cristiand391
Copy link
Member

@oleg-mastriukov yeah, we've had a few reports like this when using git bash on windows, that's why officially support PowerShell on windows. Other native shells like Nushell work, but git-bash somehow doesn't play nice with sf.

@oleg-mastriukov
Copy link
Author

@cristiand391 thanks! I checked Nushell, but as I get it, it can't just run plain bash scripts so they need to be rewritten a little to match nu syntax. I don't think it's possible if other colleagues are working in the same repo and not having Nu installed.

To conclude, there's no way to use plain bash scripts running sf/sfdx commands and verifying their status with the $? system variable in powershell, right?

@cristiand391
Copy link
Member

@oleg-mastriukov correct. The only way I see to run bash scripts would be through WSL.

@oleg-mastriukov oleg-mastriukov changed the title sf org create scratch always return 1 exit code Bash scripts return wrong exit codes on Windows Mar 25, 2024
@oleg-mastriukov
Copy link
Author

@cristiand391 my local community helped me find a solution for the issue:

result=$(sf org create scratch --alias $ORGNAME --set-default --definition-file $DEFFILE --duration-days $DURATION --json | jq '' -r)
status=$(jq '.status' <<< $result)
if [ $status != 0 ];
then
    message=$(jq '.message' <<< $result)
    stack=$(jq '.stack' <<< $result)
    echo -e "Scratch not created. Shell script stopped"
    echo -e "Error message: ' $message"
    echo -e "Error stack: ' $stack"
    exit 1
fi

This bash script works on Windows and returns proper exit codes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
investigating We're actively investigating this issue validated Version information for this issue has been validated
Projects
None yet
Development

No branches or pull requests

2 participants