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

tests: templ generate exits with 0 when there are errors #513

Closed
jraby opened this issue Feb 11, 2024 · 6 comments
Closed

tests: templ generate exits with 0 when there are errors #513

jraby opened this issue Feb 11, 2024 · 6 comments
Labels
cmd NeedsFix Needs implementing tests

Comments

@jraby
Copy link

jraby commented Feb 11, 2024

This is very similar to #278, when running templ generate on an invalid template, the command exits with 0:

Given the following templ file:

package main

templ hello(name string) {
        <div>Hello, { name }</div>
    <div
    ></div
}

Running generate yields this:

$ templ version
v0.2.543
$ templ generate; echo $?
Processing path: /home/jean/tmp/templ-exit-code
Generating production code: /home/jean/tmp/templ-exit-code
(✗) Error processing path: /home/jean/tmp/templ-exit-code/components.templ parsing error: <div>: expected end tag not present or invalid tag contents: line 5, col 5
(✗) Generated code for 1 templates with 1 errors in 333.805µs
0

There seem to be an issue in the generateProduction method, where the errors are seen and counted, but not returned:

func generateProduction(ctx context.Context, w io.Writer, args Arguments, opts []generator.GenerateOpt, p *proxy.Handler) error {
    fmt.Fprintln(w, "Generating production code:", args.Path)
    start := time.Now()

    changesFound, errs := processChanges(
        ctx, w, nil, nil,
        args.Path, args.GenerateSourceMapVisualisations,
        opts, args.WorkerCount, false, args.KeepOrphanedFiles)
    if len(errs) > 0 {
        if errors.Is(errs[0], context.Canceled) {
            return errs[0]
        }
        logError(w, "Error processing path: %v\n", errors.Join(errs...))
    }

    if changesFound > 0 {
        if len(errs) > 0 {   <<<<<< should return something with errs
            logError(w, "Generated code for %d templates with %d errors in %s\n", changesFound, len(errs), time.Since(start))
        } else {
            logSuccess(w, "Generated code for %d templates with %d errors in %s\n", changesFound, len(errs), time.Since(start))
        }
        if args.Command != "" {
            fmt.Fprintf(w, "Executing command: %s\n", args.Command)
            if _, err := run.Run(ctx, args.Path, args.Command); err != nil {
                fmt.Fprintf(w, "Error starting command: %v\n", err)
            }
        }
    }

    return nil   <<<  errs are lost :(
}
@a-h
Copy link
Owner

a-h commented Feb 11, 2024

Thanks for the report.

However, since the release you're using, that code has been completely rewritten from scratch, so the generateProduction function no longer exists in the codebase. The changes were made in #470 which adds a new fsnotify based watcher for --watch mode.

If you'd like to try the same operation with the latest CLI, by installing from source, e.g. go install github.com/a-h/templ/cmd/templ@8a1cd80d2e3880e55b6ae15e8245f8fec4e9b6cb, that would be great.

@jraby
Copy link
Author

jraby commented Feb 11, 2024

Hi, here's the result when using 8a1cd80:

$ ~/go/bin/templ generate
(!) templ version check: generator v0.2.562 is newer than templ version v0.2.543 found in go.mod file, consider running `go get -u github.com/a-h/templ` to upgrade
(✗) Error generating code [ file=/home/jean/tmp/templ-exit-code/test.templ error=/home/jean/tmp/templ-exit-code/test.templ parsing error: <div>: expected end tag not present or invalid tag contents: line 5, col 5 ]
(✗) Event handler failed [ error=failed to generate code for "/home/jean/tmp/templ-exit-code/test.templ": /home/jean/tmp/templ-exit-code/test.templ parsing error: <div>: expected end tag not present or invalid tag contents: line 5, col 5 ]
panic: send on closed channel

goroutine 12 [running]:
github.com/a-h/templ/cmd/templ/generatecmd.Generate.Run.func3.1({{0xc00002e420?, 0xc00005e788?}, 0x5e798?})
        /home/jean/go/pkg/mod/github.com/a-h/[email protected]/cmd/templ/generatecmd/cmd.go:173 +0x325
created by github.com/a-h/templ/cmd/templ/generatecmd.Generate.Run.func3 in goroutine 10
        /home/jean/go/pkg/mod/github.com/a-h/[email protected]/cmd/templ/generatecmd/cmd.go:166 +0x148

It definitely exits with a non zero code!

@a-h
Copy link
Owner

a-h commented Feb 11, 2024

Ha, yes, very much a fail. It looks like the errors channel is being closed too early (only in the case that the --watch flag is set).

I'm able to reproduce it:

0 2 /Users/adrian/testt/onclick % templ generate -v
(!) templ version check: generator v0.2.566 is newer than templ version v0.2.513 found in go.mod file, consider running `go get -u github.com/a-h/templ` to upgrade
(✓) Starting post-generation handler
(✓) Walking directory [ path=/Users/adrian/testt/onclick devMode=false ]
(✓) Starting event handler
(✓) Processing file [ file=/Users/adrian/testt/onclick/components/template_templ.go ]
(✓) Processing file [ file=/Users/adrian/testt/onclick/components/template.templ ]
(✓) Dev mode not enabled, process can finish early
(✓) Waiting for push handler to complete
(✓) Waiting for event handler to complete
(✗) Error generating code [ file=/Users/adrian/testt/onclick/components/template.templ error=/Users/adrian/testt/onclick/components/template.templ parsing error: <div>: expected end tag not present or invalid tag contents: line 8, col 5 ]
(✗) Event handler failed [ error=failed to generate code for "/Users/adrian/testt/onclick/components/template.templ": /Users/adrian/testt/onclick/components/template.templ parsing error: <div>: expected end tag not present or invalid tag contents: line 8, col 5 ]
panic: send on closed channel

goroutine 7 [running]:
github.com/a-h/templ/cmd/templ/generatecmd.Generate.Run.func3.1({{0x1400002e3c0?, 0x14000059670?}, 0x59680?})
        /Users/adrian/github.com/a-h/templ/cmd/templ/generatecmd/cmd.go:173 +0x240
created by github.com/a-h/templ/cmd/templ/generatecmd.Generate.Run.func3 in goroutine 21
        /Users/adrian/github.com/a-h/templ/cmd/templ/generatecmd/cmd.go:166 +0x108

@a-h a-h closed this as completed in 5821706 Feb 11, 2024
@a-h
Copy link
Owner

a-h commented Feb 11, 2024

I've taken a look at this area and made a few improvements. For example, I noticed that the logging showed you errors, but didn't show you if the error was cleared (by editing the file). So even though there weren't any more errors, the terminal output didn't show you that.

If you could give e78dbc1 a try out, that would be excellent.

@a-h a-h reopened this Feb 11, 2024
@jraby
Copy link
Author

jraby commented Feb 12, 2024

Looks like it is working now!

I'm not familiar with the codebase, but do you think it would warrant a test for that behavior? (exit != 0 on error?)

@a-h
Copy link
Owner

a-h commented Feb 12, 2024

Yeah, it would be worth a test. There's only a test on the happy path (successful generation) at the moment.

@joerdav joerdav changed the title templ generate exits with 0 when there are errors cmd: templ generate exits with 0 when there are errors Feb 12, 2024
@joerdav joerdav changed the title cmd: templ generate exits with 0 when there are errors tests: templ generate exits with 0 when there are errors Feb 12, 2024
@joerdav joerdav added cmd NeedsFix Needs implementing tests labels Feb 12, 2024
@a-h a-h closed this as completed in fdd8955 Feb 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cmd NeedsFix Needs implementing tests
Projects
None yet
Development

No branches or pull requests

3 participants