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

Add --exit-code (-x) flag to enable carrying error codes from task cmds #755

Merged
merged 3 commits into from
Jun 11, 2022

Conversation

BrunoDelor
Copy link

Hello,

I recently found the need to get the return code that was returned from a failed command from a task's cmds. After investigating quickly it appeared to me that the task tool would always return with status code 1 when an error occurred during a task.

This PR proposes an additional command flag (--carry), false by default, that will carry the error code, if any, over to the calling shell.

@ghostsquad
Copy link
Contributor

Please add some tests to assert on this functionality.

@andreynering
Copy link
Member

Hi @BrunoDelor, thanks for your PR!

Given this would be a pretty small breaking change, perhaps we could even get rid of the flag and always return the right exit code? Do you guys have an opinion on this?

Another question: does other tools like Make does this as well?

@mgbowman
Copy link
Contributor

mgbowman commented Jun 2, 2022

Always returning the right exit code seems logical to me.

Looks like make only uses 0, 1 or 2

$ man make

EXIT STATUS
       GNU make exits with a status of zero if all makefiles were successfully parsed and no targets that were built failed.
       A status of one will be returned if the -q flag was used and make determines that a target needs to be rebuilt.
       A status of two will be returned if any errors were encountered.

@BrunoDelor
Copy link
Author

BrunoDelor commented Jun 2, 2022

Thank you for your comments

@ghostsquad
As this is mainly about what happens in the main I'm not entirely sure how to go about unit-testing it specifically.

If that's would be enough I can add a test that will show that the error code can be exposed through the new func (err *TaskRunError) ExitCode() int after being returned from a task command.

@andreynering
I agree with your comment about not having the flag at all and always returning the right error code.

It appears the question about Make is already answered.
This is why I thought about making it optional. I find a use for it, as I use my error code for decisions afterward, but considering it's not really a standard behavior I preferred not to enforce the change on everyone by default.

@BrunoDelor BrunoDelor force-pushed the feature/carry-error-code branch from a4cda9f to 58c7cc5 Compare June 2, 2022 14:44
@ghostsquad
Copy link
Contributor

I think maybe this flag could be renamed too. I've seen it described as exit code pass thru.

Heroku uses --exit-code as the flag and describes it as allowing the exit code of the sub process to "pass thru" the main/parent process.

https://github.com/heroku/heroku-run

I'm trying to recall specifically where else I've seen this.

@BrunoDelor
Copy link
Author

This sounds good to me, I'll push a change for -x --exit-code if this is agreed upon

Shorthand: -x
Longhand: --exit-code
@andreynering andreynering changed the title Add --carry flag to enable carrying error codes from task cmds Add --exit-code (-x) flag to enable carrying error codes from task cmds Jun 11, 2022
@andreynering
Copy link
Member

@BrunoDelor Thanks for contributing! ❤️

@andreynering andreynering merged commit 63aad1e into go-task:master Jun 11, 2022
@pd93 pd93 mentioned this pull request Apr 9, 2023
@pd93 pd93 added type: feature A new feature or functionality. and removed type: enhancement labels Dec 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: feature A new feature or functionality.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants