Skip to content

Conversation

chappjc
Copy link
Member

@chappjc chappjc commented Aug 28, 2022

v0.5.0 on Windows is broken when using embedded files because as per the embed pkg docs:

The path separator is a forward slash, even on Windows systems.

So doing filepath.Join, which is usually "the right thing", fails on Windows. Surprising to say the least. golang/go#45230 golang/go#44305 https://pkg.go.dev/io/fs#ValidPath

For an immediate patch.

@chappjc chappjc added this to the 0.5.1 milestone Aug 28, 2022
@chappjc chappjc marked this pull request as ready for review August 28, 2022 23:16
@chappjc chappjc mentioned this pull request Aug 28, 2022
@@ -46,7 +46,8 @@ func newTemplates(folder, lang string, embedded bool) *templates {
t.addErr = fmt.Errorf("no translation dictionary found for lang %q", lang)
return t
}
if embedded {
if embedded { // slashes even on Windows, see embed pkg docs
t.folder = filepath.ToSlash(t.folder)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be redundant with the changes in buildTemplates

@@ -60,6 +61,9 @@ func newTemplates(folder, lang string, embedded bool) *templates {

// filepath constructs the template path from the template ID.
func (t *templates) filepath(name string) string {
if t.embedded { // slashes even on Windows, see embed pkg docs
return t.folder + "/" + name + ".tmpl"
Copy link
Member Author

@chappjc chappjc Aug 29, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The general advice would be to use path.Join for embedded paths, instead of filepath.Join, but this is a very simple operation and path.Join is overcomplex for the job in this case.

@chappjc
Copy link
Member Author

chappjc commented Aug 29, 2022

Just tested on Windows 11 and it was alright there too.

@chappjc
Copy link
Member Author

chappjc commented Aug 29, 2022

One more review would be good. Ideally, another linux, plus a mac. Wanna double check everything this time so 0.5.1 is workable on all platforms.

@chappjc chappjc force-pushed the windows-embed-paths branch from c45dfb1 to 459a19c Compare August 29, 2022 19:22
This fixes the embedded file server on Windows. The embed and fs
packages user forward slashes as the path separator, even on
Windows, so we should not use filepath.Join when accessing files
in an embed.FS or fs.FS.

Also set Content-Length for embedded file responses.
@chappjc chappjc force-pushed the windows-embed-paths branch from 459a19c to 6e390c1 Compare August 29, 2022 19:48
@chappjc chappjc merged commit f1e21fe into decred:master Aug 29, 2022
@chappjc chappjc deleted the windows-embed-paths branch August 29, 2022 20:04
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

Successfully merging this pull request may close these issues.

2 participants