Skip to content

Commit 718fd92

Browse files
leaanthonyclaudecoderabbitai[bot]
authored
fix(v2): prevent wails init in non-empty directory with -d flag (#4955)
* fix(v2): prevent wails init in non-empty directory with -d flag When using -d to specify a target directory, wails init now checks if the directory is non-empty and errors if so. This prevents accidental data loss (e.g., overwriting .git directories). Fixes #4940 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * test(v2): add tests for init non-empty directory check Add tests to verify: - Install fails when target directory is non-empty - Install succeeds when target directory is empty Also update changelog with the fix. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * Apply suggestions from code review Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --------- Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com> Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
1 parent 01b661f commit 718fd92

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed

v2/pkg/templates/templates.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,16 @@ func Install(options *Options) (bool, *Template, error) {
186186
return false, nil, err
187187
}
188188
options.TargetDir = targetDir
189-
if !fs.DirExists(options.TargetDir) {
189+
if fs.DirExists(options.TargetDir) {
190+
// Check if directory is non-empty
191+
entries, err := os.ReadDir(options.TargetDir)
192+
if err != nil {
193+
return false, nil, err
194+
}
195+
if len(entries) > 0 {
196+
return false, nil, fmt.Errorf("cannot initialise project in non-empty directory: %s", options.TargetDir)
197+
}
198+
} else {
190199
err := fs.Mkdir(options.TargetDir)
191200
if err != nil {
192201
return false, nil, err

v2/pkg/templates/templates_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,48 @@ func TestInstall(t *testing.T) {
5252
is2.NoErr(err)
5353

5454
}
55+
56+
func TestInstallFailsInNonEmptyDirectory(t *testing.T) {
57+
is2 := is.New(t)
58+
59+
// Create a temp directory with a file in it
60+
tempDir, err := os.MkdirTemp("", "wails-test-nonempty-*")
61+
is2.NoErr(err)
62+
defer func() {
63+
_ = os.RemoveAll(tempDir)
64+
}()
65+
66+
// Create a file in the directory to make it non-empty
67+
err = os.WriteFile(filepath.Join(tempDir, "existing-file.txt"), []byte("test"), 0644)
68+
is2.NoErr(err)
69+
70+
options := &Options{
71+
ProjectName: "test",
72+
TemplateName: "vanilla",
73+
TargetDir: tempDir,
74+
}
75+
76+
_, _, err = Install(options)
77+
is2.True(err != nil) // Should fail
78+
is2.True(err.Error() == "cannot initialise project in non-empty directory: "+tempDir)
79+
}
80+
81+
func TestInstallSucceedsInEmptyDirectory(t *testing.T) {
82+
is2 := is.New(t)
83+
84+
// Create an empty temp directory
85+
tempDir, err := os.MkdirTemp("", "wails-test-empty-*")
86+
is2.NoErr(err)
87+
defer func() {
88+
_ = os.RemoveAll(tempDir)
89+
}()
90+
91+
options := &Options{
92+
ProjectName: "test",
93+
TemplateName: "vanilla",
94+
TargetDir: tempDir,
95+
}
96+
97+
_, _, err = Install(options)
98+
is2.NoErr(err) // Should succeed in empty directory
99+
}

website/src/pages/changelog.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1616

1717
### Fixed
1818

19+
- Fixed `wails init` to prevent initialization in non-empty directories when using the `-d` flag, avoiding accidental data loss [`#4940`](https://github.com/wailsapp/wails/issues/4940) by `@leaanthony`
1920
- Fixed missing `EventsOffAll` in runtime templates for all frontend frameworks [#4883](https://github.com/wailsapp/wails/pull/4883) by @narcilee7
2021
- Fixed Linux crash on panic in JS-bound Go methods due to WebKit overriding signal handlers [#3965](https://github.com/wailsapp/wails/issues/3965) by @leaanthony
2122
- Fixed code block range in "How Does It Work?" documentation [#4884](https://github.com/wailsapp/wails/pull/4884) by @msal4

0 commit comments

Comments
 (0)