Code Monkey home page Code Monkey logo

Comments (9)

adasauce avatar adasauce commented on July 17, 2024 1

oh, I think I missed something re-reading this thread. If I check in the generated go code from when templ was running in watch mode it's going to make reference to this function.

I had checked it in from a previous issue I was reading mentioning it was idiomatic to commit the generated code so that people who don't have the templ built tools installed ahead of time can build the application without worrying about that step of the build chain.

from templ.

IcelandicIcecream avatar IcelandicIcecream commented on July 17, 2024

Adding to this:
Here's the error I'm getting:
2024-02-09T17:08:46Z ERR server/server.go:23 > 200 | GET: '/' -- templ: failed to cache strings: templ: failed to open /app/views/layout/base_templ.txt: open /app/views/layout/base_templ.txt: no such file or directory pid=1

EDIT: I had to port over my views folder for the .txt files to be there in my dockerfile. I don't know if this is intentional. But perhaps this can be added in the documentation for projects with components in nested folders?

from templ.

joerdav avatar joerdav commented on July 17, 2024

Before the build step in Docker you need to run temple generate it's copying over your --watch mode templates.

from templ.

IcelandicIcecream avatar IcelandicIcecream commented on July 17, 2024

Before the build step in Docker you need to run temple generate it's copying over your --watch mode templates.

Oh I see. So I would need to build and install templ in my docker image, and then templ generate to get the txt files during my build stage? Is this the best way? seems a bit excessive haha Also, for some reason, when I do templ generate.. It doesn't generate the txt files anymore. Only templ generate --watch does and it deletes the txt file when I stop it

from templ.

joerdav avatar joerdav commented on July 17, 2024

The txt files are only relevant for dev mode. When you run templ generate on it's own you get the production templates. That's why I'd recommend a templ generate in your build process, just incase you forgot to do a prod build.

from templ.

alehechka avatar alehechka commented on July 17, 2024

So I would need to build and install templ in my docker image, and then templ generate to get the txt files during my build stage? Is this the best way? seems a bit excessive

I feel like it's better to do it this way imo. When I personally use templ I gitignore all of the Go code that templ generates (all *_templ.go files) and the docker image runs templ generate everytime so it always has the most up-to-date code. This also will fail the docker build if templ throws an error during generation which is better because it ensures the templ code I pushed is still valid.

Here's an snippet from one of my projects:

# BASE IMAGES
FROM --platform=$BUILDPLATFORM golang:1.21 as go-base

# GENERATE TEMPL

FROM go-base as templ-builder

WORKDIR /app

COPY go.mod .

# install templ version that go.mod is using
RUN go install github.com/a-h/templ/cmd/templ@$(go list -m -f '{{ .Version }}' github.com/a-h/templ)

COPY web/templ web/templ

RUN templ generate --path=web/templ

from templ.

IcelandicIcecream avatar IcelandicIcecream commented on July 17, 2024

So I would need to build and install templ in my docker image, and then templ generate to get the txt files during my build stage? Is this the best way? seems a bit excessive

I feel like it's better to do it this way imo. When I personally use templ I gitignore all of the Go code that templ generates (all *_templ.go files) and the docker image runs templ generate everytime so it always has the most up-to-date code. This also will fail the docker build if templ throws an error during generation which is better because it ensures the templ code I pushed is still valid.

Here's an snippet from one of my projects:

# BASE IMAGES
FROM --platform=$BUILDPLATFORM golang:1.21 as go-base

# GENERATE TEMPL

FROM go-base as templ-builder

WORKDIR /app

COPY go.mod .

# install templ version that go.mod is using
RUN go install github.com/a-h/templ/cmd/templ@$(go list -m -f '{{ .Version }}' github.com/a-h/templ)

COPY web/templ web/templ

RUN templ generate --path=web/templ

Awesome thanks for this, I'll use it as a reference for mine. Having thought about it, it does make sense to do it on build to simplify the CI/CD process

from templ.

adasauce avatar adasauce commented on July 17, 2024

Just a little inquiry on this, the error failing explicitly says "failed to cache strings", and it looks like that's being generated by WriteWatchModeString

// WriteWatchModeString is used when rendering templates in development mode.
// the generator would have written non-go code to the _templ.txt file, which
// is then read by this function and written to the output.

based on the documentation, this shouldn't be getting called at all, right? this isn't dev mode -- it's likely production.

is the documentation misleading, or is this function getting called accidentally?

especially given an app is probably running in a hardened container, it shouldn't be writing anything out to the filesystem (and in many cases doesn't have permission to at all).

from templ.

andersryanc avatar andersryanc commented on July 17, 2024

What is the purpose of the text files when developing locally? Seems like it adds more complications than benefit. For instance, even if you choose to gitignore them AND also decide to take the idiomatic approach to track the generated go files, it would be very easy during development to stage and commit all your files to git (in the middle of your work) without first stopping the templ generate --watch command. This would then lead to a problem state where the generated go files you committed are referencing the txt files you chose to ignore...

In my opinion @alehechka has a good point to ignore them both and run the generate command in the CI/CD (or manual build) process. It's not as convenient as @adasauce points out, but if someone is building and running the app for development, they will probably need to install templ cli at some point anyways right?

Lastly, I think it would be helpful to update the docs on the website to discuss gitignore and deployment best practices more clearly. I ended up on this issue because I could not find a mention of if the generated text files should be tracked or not in the repository. I looked at the docker example also and noticed that the generated go files are tracked and that is also does not have a .gitignore file at all.

from templ.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.