Skip to content

using context with implicit vs explicit import #824

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

Closed
garrettladley opened this issue Jul 3, 2024 · 3 comments
Closed

using context with implicit vs explicit import #824

garrettladley opened this issue Jul 3, 2024 · 3 comments

Comments

@garrettladley
Copy link
Contributor

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
Copy link
Collaborator

joerdav commented Jul 3, 2024

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.

@joerdav joerdav closed this as not planned Won't fix, can't repro, duplicate, stale Jul 3, 2024
@garrettladley
Copy link
Contributor Author

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
Copy link
Collaborator

joerdav commented Jul 4, 2024

It was in the last release https://github.com/a-h/templ/releases/tag/v0.2.731 here's the commit: #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 :)

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