-
Notifications
You must be signed in to change notification settings - Fork 43
Changes from statik to go:embed for example and extension templates #129
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b8c0ad2
Changes from statik to go:embed for examples serving
554af22
Adds RATIONALE.md
ba836c1
format
74b8fff
typos and restores chmod hack
854d5bf
typos
codefromthecrypt b5af0b9
less pasta
7db5fdd
Merge branch 'master' into goembed
codefromthecrypt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,3 @@ dist | |
/getenvoy | ||
/build/ | ||
.idea | ||
|
||
# code generated by `github.com/rakyll/statik` | ||
statik.go |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# Notable Rationale | ||
|
||
## go:embed for embedding example and init templates | ||
|
||
This project resolves templates into working examples or extensions via `getenvoy example add` and | ||
`getenvoy extension init`. Input [example](data/example/init/templates) and [extension](data/extension/init/templates) | ||
templates are embedded in the `getenvoy` binary for user convenience. | ||
|
||
We implement embedding with a Go 1.16+ directive `//go:embed templates/*`, which presents an `fs.FS` interface of the | ||
directory tree. Using this requires no extra build steps. Ex. `go build -o getenvoy ./cmd/getenvoy/main.go` works. | ||
|
||
However, there are some constraints to understand. It is accepted that while imperfect, this solution is an acceptable | ||
alternative to a custom solution. | ||
|
||
### Embedded files must be in a sub-path | ||
The go source file embedding content via `go:embed` must reference paths in the current directory tree. In other words, | ||
paths like `../../templates` are not allowed. | ||
|
||
This means we have to move the embedded directory tree where code refers to it, or make an accessor utility for each | ||
directory root. We use the latter pattern, specifically here: | ||
* [data/example/init/templates.go](data/example/init/templates.go) | ||
* [data/extension/init/templates.go](data/extension/init/templates.go) | ||
|
||
See https://pkg.go.dev/embed#hdr-Directives for more information. | ||
|
||
### Limitations of `go:embed` impact extension init templates | ||
`getenvoy extension init` creates a workspace directory for a given category and programming language. Some constraints | ||
of `go:embed` impact how these template directories are laid out, and the workarounds are file rename in basis. The | ||
impacts are noted below for information and future follow-up: | ||
|
||
#### `go:embed` doesn't traverse hidden directories, but Rust projects include a hidden directory | ||
Our Rust examples use [Cargo][https://doc.rust-lang.org/cargo/reference/config.html] as a build tool. This stores | ||
codefromthecrypt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
configuration in a hidden directory `.cargo`. As of Go 1.16, hidden directories are not yet supported with `go:embed`. | ||
See https://github.com/golang/go/issues/43854 | ||
|
||
#### `go:embed` stops traversing at module boundaries, and TinyGo examples look like sub-modules | ||
Go modules need to be built from the zip-file uploaded to the mirror. This implies `go:embed` must refer to the | ||
current module. https://github.com/golang/go/issues/45197 explains embedding stops traversing when it encounters a | ||
`go.mod` file, and there is no plan to change this. | ||
|
||
This presents a challenge with TinyGo templates, which except for parameterization like `module {{ .Extension.Name }}`, | ||
appear as normal go modules (ex `go.mod`) files. When `go:embed` encounters a `go.mod` file, it stops traversing. If we | ||
didn't work around this, no TinyGo project would end up in the embedded filesystem. | ||
|
||
The workaround is to rename the template `go.mod` to `go.mod_`, but this introduces a couple glitches: | ||
|
||
* TinyGo extension templates imports appear in the root project after running `go mod tidy` | ||
* As of the writing, there is only one github.com/tetratelabs/proxy-wasm-go-sdk | ||
* TinyGo extension templates can't be run from their directory without temporarily renaming `go.mod_` back to `go.mod` | ||
* To do this anyway, you'd need to fix any template variables like `module {{ .Extension.Name }}` | ||
|
||
### Former solution | ||
In the past, we embedded via [statik](https://github.com/rakyll/statik). This is a code generation solution which | ||
encodes files into internal variables. Entry-points use an `http.FileSystem` to access the embedded files. | ||
|
||
This solution worked well, except that it introduced build complexity. Code generation required an `init` phase in the | ||
`Makefile`, as well lint exclusions. This implied steps for developers to remember and CI to invoke. | ||
|
||
It also introduced maintenance and risk as the statik library stalled. This project ended up pinned to a personal fork | ||
in order to work around unmerged pull requests. | ||
``` | ||
replace github.com/rakyll/statik => github.com/yskopets/statik v0.1.8-0.20200501213002-c2d8dcc79889 | ||
``` | ||
|
||
Go 1.16's `go:embed` has limitations of its own, but brings with it shared understanding. While we may need workarounds | ||
to some issues, those issues are understood by the go community. `go:embed` does not require our maintenance, nor has | ||
any key person risk. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright 2021 Tetrate | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package init | ||
|
||
import ( | ||
"embed" | ||
"io/fs" | ||
) | ||
|
||
// templatesFs includes only the relative path of "templates". | ||
// | ||
// See RATIONALE.md for more information on embedding | ||
//go:embed templates/* | ||
var templatesFs embed.FS | ||
|
||
// GetTemplates returns the templates directory as a filesystem | ||
func GetTemplates() fs.FS { | ||
f, err := fs.Sub(templatesFs, "templates") | ||
if err != nil { | ||
panic(err) // unexpected or a typo | ||
} | ||
return f | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright 2021 Tetrate | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package init | ||
|
||
import ( | ||
"embed" | ||
"io/fs" | ||
) | ||
|
||
// templatesFs includes only the relative path of "templates". | ||
// | ||
// See RATIONALE.md for more information on embedding | ||
//go:embed templates/* | ||
var templatesFs embed.FS | ||
|
||
// GetTemplates returns the templates directory as a filesystem | ||
func GetTemplates() fs.FS { | ||
f, err := fs.Sub(templatesFs, "templates") | ||
if err != nil { | ||
panic(err) // unexpected or a typo | ||
} | ||
return f | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
# We rename .cargo to cargo See /RATIONALE.md for why | ||
.cargo | ||
Cargo.lock | ||
target/ |
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,4 @@ | ||
build/ | ||
|
||
# We rename go.mod to go.mod_ See /RATIONALE.md for why | ||
go.mod |
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.