a-h / templ

A language for writing HTML user interfaces in Go.
https://templ.guide/
MIT License
7.14k stars 236 forks source link

using context with implicit vs explicit import #824

Closed garrettladley closed 1 day ago

garrettladley commented 2 days ago

this code that previously worked is now erroring on templ generate with reason path/to/file_templ.go:[line]:[col]: undefined: context

on my local i had been using v0.2.707, but, in my pipeline i was using a go install github.com/a-h/templ/cmd/templ@latest which at the time of writing this is v0.2.731

(yes probably my fault to not update my local/pin my pipeline version)

original simplified example code:

package views

// assume this is set in context earlier in the request
type contextKey byte
var nameContextKey contextKey = 0

templ Index() {
    <p>Hello { getName(ctx) }!</p>
}

func getName(ctx context.Context) string {
    if name, ok := ctx.Value(nameContextKey).(string); ok {
        return baseURL
    }
    return ""
}

in the fix, i simply import context. however, the LSP yells at me in the import statement saying "context" imported and not used and within Index it says context redeclared in this block.

package views

import "context"

// assume this is set in context earlier in the request
type contextKey byte
var nameContextKey contextKey = 0

templ Index() {
    <p>Hello { getName(ctx) }!</p>
}

func getName(ctx context.Context) string {
    if name, ok := ctx.Value(nameContextKey).(string); ok {
        return baseURL
    }
    return ""
}

i'm fine with this fix since my templ generate and subsequent pipeline steps work again, was just wondering if this change was intended or is a regression

joerdav commented 1 day ago

Hi @garrettladley thanks for the error report. This is expected as some of the imports being "magic" was causing a few issues and confusion, as well as making some logic a bit more complicated than it needed to be, so we have removed any standard library packages from being "magically" imported to templ files.

I expect that the reason you are seeing an error in the LSP is that the templ binary that is being used as an LSP is different to the one you are using for templ generate.

But in short yes, this is expected.

garrettladley commented 1 day ago

Hey @joerdav, thanks for getting back to me! Just for my understanding, when was this change made? I don't see anything mentioning context or imports in any of the recent changelogs. Also, I checked the docs before opening this issue and they didn't mention this change/new feature. The below linked pages about context still suggest using the implicit ctx. I'd be happy to raise a PR with the update.

templ.guide page md doc

joerdav commented 1 day ago

It was in the last release https://github.com/a-h/templ/releases/tag/v0.2.731 here's the commit: https://github.com/a-h/templ/pull/793

It's maybe not obvious the side effects of this change, but since the formatter should fix any issues we accepted the risk.

Absolutely happy to take a PR for clarifying the docs too :)