sf org create scratch always return 1 exit code
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:
Steps To Reproduce
- create a valid project-scratch-def file
- create a bash script containing a code snippet mentioned above
- run the bash script
- 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)"
]
}
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.
I can't repro on latest sf:
➜ dreamhouse-lwc git:(main) sf -v
@salesforce/cli/2.34.6 darwin-x64 node-v20.10.0
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?
Hey @cristiand391 thanks for your answer! Does this mean in my .sh scripts I can't somehow verify the command execution status?
@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.
@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?
@oleg-mastriukov correct. The only way I see to run bash scripts would be through WSL.
@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.