gobuffalo / buffalo

Rapid Web Development w/ Go
http://gobuffalo.io
MIT License
8.07k stars 573 forks source link

bug: Partials not working as described in documentation #2370

Closed armondressler closed 1 year ago

armondressler commented 1 year ago

Description

My goal is to use the partials feature as described in the documentation. Using buffalo v0.18.14

Description

When trying to use the partials as described in https://gobuffalo.io/documentation/frontend-layer/partials/, buffalo returns with a 500 and gives the following error:

users/new.html: line 2: could not call partial function: could not find template users/_form.html

As described in the documentation i added an action "users" (new), the templates/users directory contains the following files:

form.plush.html:

<form action="/users/" method="POST">
<!-- form content here  -->
<form>

new.plush.html:

<h1>Create New User</h1>
<%= partial("users/form.html") %>

Expected Behavior

users/new should render the partial instead of throwing an error.

Actual Behavior

users/new.html: line 2: could not call partial function: could not find template users/_form.html

To Reproduce

No response

Additional Context

The renderer seems to be working in general. The example from https://gobuffalo.io/pt/documentation/frontend-layer/rendering/#markdown works as intended.

fritzhu commented 1 year ago

Ditto here, same problem.

Environment: Windows 11 x64 Go 1.20.3 Buffalo 0.18.14

To reproduce

  1. Create and run test app
$ buffalo new test-app
$ cd test-app
$ buffalo generate resource Items name:string
$ buffalo pop create
$ buffalo pop migrate
$ buffalo dev
  1. Navigate to `http://localhost:3000/items/new

  2. Observe error as described in @armondressler's report above

saurori commented 1 year ago

I believe the root cause is the use of filepath.Join in the context of fs.FS with Windows using forward slash vs back slash. A similar issue was discussed here with the explanation:

The io/fs.FS package uses forward slashes for filenames, even on Windows. See https://golang.org/pkg/io/fs/#ValidPath:

"Note that paths are slash-separated on all systems, even Windows."

You should use path.Join, not filepath.Join for embed filenames.

Originally posted by @eliasnaur in https://github.com/golang/go/issues/44305#issuecomment-780111748

github-actions[bot] commented 1 year ago

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment. Otherwise, this will be closed in 7 days.

fsniper commented 1 year ago

This is related to how go:embed works. With go embed, files starting with "." and "_" are ignored. Which means, they are not available in the binary.

Check: https://pkg.go.dev/embed

To fix this, you can update the public/embed.go and apply this change:

-   //go:embed */* *
+  //go:embed all:*/* all:*
saurori commented 1 year ago

@fsniper that is not correct. See my above comment and the linked issues. It is a problem with using filepath.Join vs path.Join under Windows.

fsniper commented 1 year ago

@saurori I can't speak for windows, but I had to fix this on OSX just 2 days ago. And this was my solution.

fsniper commented 1 year ago

I suppose my case was totally unrelated. I used a public/partials directory which does not refer to any action.

saurori commented 1 year ago

@fsniper what you described with go:embed I also ran into on macOS (has to do with go:embed prior to go 1.18 and files starting with _). But I believe the issue in this thread and other linked issues has to do with back slash vs forward slash behavior on Windows when using filepath.Join.

fsniper commented 1 year ago

I am on go version go1.20.4 darwin/amd64. Perhaps there are 2 separate issues at play here.

fsniper commented 1 year ago

I checked the test case and it's working on my configuration. So my situation is unrelated.

github-actions[bot] commented 1 year ago

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment. Otherwise, this will be closed in 7 days.

github-actions[bot] commented 1 year ago

This issue was closed because it has been stalled for 30+7 days with no activity.