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

Templ CLI doesn't return a non-zero exit code on error #278

Closed
temos opened this issue Nov 3, 2023 · 1 comment
Closed

Templ CLI doesn't return a non-zero exit code on error #278

temos opened this issue Nov 3, 2023 · 1 comment

Comments

@temos
Copy link

temos commented Nov 3, 2023

Hello,

I've noticed that when chaining commands with the Templ CLI, it didn't matter whether the command succeeded or not.

In this example, given that the generation fails, I would expect templ generate to return a non-zero exit code, thus preventing the commands after it from executing.

templ generate && go run .

This, however, does not happen. I've taken a quick look at the code and it appears the non-zero status code is actually returned by the generateCmd function in cmd/templ/main.go, but it is ignored. The following is a snippet of the code:

func run(w io.Writer, args []string) (code int) {
	if len(args) < 2 {
		fmt.Fprint(w, usageText)
		return 0
	}
	switch args[1] {
	case "generate":
                //generateCmd returns the exit code, but it's ignored
		generateCmd(w, args[2:])
		return
...

Based on this, the solution might be just returning the exit code, but I'm unsure whether there is a reason for it being this way I'm not seeing:

func run(w io.Writer, args []string) (code int) {
	if len(args) < 2 {
		fmt.Fprint(w, usageText)
		return 0
	}
	switch args[1] {
	case "generate":
		return generateCmd(w, args[2:])
...

It would also be required to handle it in main which might look something like this:

// now
func main() {
	run(os.Stdout, os.Args)
}

// changed
func main() {
	exitCode := run(os.Stdout, os.Args)
	os.Exit(exitCode)
}

The generate subcommand is not the only one affected, it's just the one I tested. Based on the code in the run function, returned exit codes are ignored for all subcommands.

a-h added a commit that referenced this issue Nov 3, 2023
@a-h
Copy link
Owner

a-h commented Nov 3, 2023

Thanks for that! Nicely written up, much appreciated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants