Skip to content

Commit

Permalink
Fixed an issue where go:embed directives would generate illegal direc…
Browse files Browse the repository at this point in the history
…tories (#3445)

* Update staticanalysis.go and associated tests

* Update changelog

* Changed format of octal literal

* Update changelog
  • Loading branch information
TwoNull authored May 2, 2024
1 parent b8dae7a commit b7713da
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 25 deletions.
35 changes: 21 additions & 14 deletions v2/internal/staticanalysis/staticanalysis.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,24 +52,31 @@ func GetEmbedDetailsForFile(file *ast.File, baseDir string) []*EmbedDetails {
for _, c := range comment.List {
if strings.HasPrefix(c.Text, "//go:embed") {
sl := strings.Split(c.Text, " ")
path := ""
all := false
if len(sl) == 1 {
continue
}
embedPath := strings.TrimSpace(sl[1])
switch true {
case strings.HasPrefix(embedPath, "all:"):
path = strings.TrimPrefix(embedPath, "all:")
all = true
default:
path = embedPath
// support for multiple paths in one comment
for _, arg := range sl[1:] {
embedPath := strings.TrimSpace(arg)
// ignores all pattern matching characters except escape sequence
if strings.Contains(embedPath, "*") || strings.Contains(embedPath, "?") || strings.Contains(embedPath, "[") {
continue
}
if strings.HasPrefix(embedPath, "all:") {
result = append(result, &EmbedDetails{
EmbedPath: strings.TrimPrefix(embedPath, "all:"),
All: true,
BaseDir: baseDir,
})
} else {
result = append(result, &EmbedDetails{
EmbedPath: embedPath,
All: false,
BaseDir: baseDir,
})
}

}
result = append(result, &EmbedDetails{
EmbedPath: path,
All: all,
BaseDir: baseDir,
})
}
}
}
Expand Down
7 changes: 6 additions & 1 deletion v2/internal/staticanalysis/staticanalysis_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package staticanalysis

import (
"github.com/stretchr/testify/require"
"testing"

"github.com/stretchr/testify/require"
)

func TestGetEmbedDetails(t *testing.T) {
Expand All @@ -25,6 +26,10 @@ func TestGetEmbedDetails(t *testing.T) {
EmbedPath: "frontend/dist",
All: true,
},
{
EmbedPath: "frontend/static",
All: false,
},
},
wantErr: false,
},
Expand Down
5 changes: 4 additions & 1 deletion v2/internal/staticanalysis/test/standard/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ import (
"github.com/wailsapp/wails/v2/pkg/options/assetserver"
)

//go:embed all:frontend/dist
//go:embed all:frontend/dist frontend/static
var assets embed.FS

//go:embed frontend/src/*.json
var srcjson embed.FS

func main() {
// Create an instance of the app structure
app := NewApp()
Expand Down
21 changes: 12 additions & 9 deletions v2/pkg/commands/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,16 +169,19 @@ func CreateEmbedDirectories(cwd string, buildOptions *Options) error {

for _, embedDetail := range embedDetails {
fullPath := embedDetail.GetFullPath()
if _, err := os.Stat(fullPath); os.IsNotExist(err) {
err := os.MkdirAll(fullPath, 0o755)
if err != nil {
return err
}
f, err := os.Create(filepath.Join(fullPath, "gitkeep"))
if err != nil {
return err
// assumes path is directory only if it has no extension
if filepath.Ext(fullPath) == "" {
if _, err := os.Stat(fullPath); os.IsNotExist(err) {
err := os.MkdirAll(fullPath, 0o755)
if err != nil {
return err
}
f, err := os.Create(filepath.Join(fullPath, "gitkeep"))
if err != nil {
return err
}
_ = f.Close()
}
_ = f.Close()
}
}

Expand Down
1 change: 1 addition & 0 deletions website/src/pages/changelog.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed

- Fixed an issue where embed directives with patterned paths would create illegal directories. Changed by [@twonull](https://github.com/twonull) in [PR](https://github.com/wailsapp/wails/pull/3445)
- Fixed an issue where certain calls to createFrom in TypeScript would fail. Changed by [@twonull](https://github.com/twonull) in [PR](https://github.com/wailsapp/wails/pull/3435)
- Fixed some typos in comments. Changed by [@reallylowest](https://github.com/reallylowest) in [PR](https://github.com/wailsapp/wails/pull/3357)
- Fixed an issue where the destination file was not properly closed after copying. Changed by [@testwill](https://github.com/testwill) in [PR](https://github.com/wailsapp/wails/pull/3384)
Expand Down

0 comments on commit b7713da

Please sign in to comment.