From b7713da70e2f0faa98549f0777d3889c4f977878 Mon Sep 17 00:00:00 2001 From: Leo Date: Thu, 2 May 2024 07:40:38 -0400 Subject: [PATCH] Fixed an issue where go:embed directives would generate illegal directories (#3445) * Update staticanalysis.go and associated tests * Update changelog * Changed format of octal literal * Update changelog --- v2/internal/staticanalysis/staticanalysis.go | 35 +++++++++++-------- .../staticanalysis/staticanalysis_test.go | 7 +++- .../staticanalysis/test/standard/main.go | 5 ++- v2/pkg/commands/build/build.go | 21 ++++++----- website/src/pages/changelog.mdx | 1 + 5 files changed, 44 insertions(+), 25 deletions(-) diff --git a/v2/internal/staticanalysis/staticanalysis.go b/v2/internal/staticanalysis/staticanalysis.go index d5cc8c6e522..cde4366330b 100644 --- a/v2/internal/staticanalysis/staticanalysis.go +++ b/v2/internal/staticanalysis/staticanalysis.go @@ -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, - }) } } } diff --git a/v2/internal/staticanalysis/staticanalysis_test.go b/v2/internal/staticanalysis/staticanalysis_test.go index 17599676e01..77ad2fa6c04 100644 --- a/v2/internal/staticanalysis/staticanalysis_test.go +++ b/v2/internal/staticanalysis/staticanalysis_test.go @@ -1,8 +1,9 @@ package staticanalysis import ( - "github.com/stretchr/testify/require" "testing" + + "github.com/stretchr/testify/require" ) func TestGetEmbedDetails(t *testing.T) { @@ -25,6 +26,10 @@ func TestGetEmbedDetails(t *testing.T) { EmbedPath: "frontend/dist", All: true, }, + { + EmbedPath: "frontend/static", + All: false, + }, }, wantErr: false, }, diff --git a/v2/internal/staticanalysis/test/standard/main.go b/v2/internal/staticanalysis/test/standard/main.go index 3f735d6405f..2b6ab33b625 100644 --- a/v2/internal/staticanalysis/test/standard/main.go +++ b/v2/internal/staticanalysis/test/standard/main.go @@ -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() diff --git a/v2/pkg/commands/build/build.go b/v2/pkg/commands/build/build.go index 853943f8f1e..261f4c6d7ff 100644 --- a/v2/pkg/commands/build/build.go +++ b/v2/pkg/commands/build/build.go @@ -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() } } diff --git a/website/src/pages/changelog.mdx b/website/src/pages/changelog.mdx index f41b9a1bbe6..08de29fa198 100644 --- a/website/src/pages/changelog.mdx +++ b/website/src/pages/changelog.mdx @@ -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)