fix(v2): prevent wails init in non-empty directory with -d flag - #4955
Conversation
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>
|
Caution Review failedThe pull request is closed. 📝 WalkthroughWalkthroughInstall now permits a pre-existing empty TargetDir: if TargetDir exists and is empty, Install proceeds; if it exists and is non-empty, Install returns an explicit error. Creation of TargetDir still happens when it did not previously exist. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches🧪 Generate unit tests (beta)
Important Action Needed: IP Allowlist UpdateIf your organization protects your Git platform with IP whitelisting, please add the new CodeRabbit IP address to your allowlist:
Reviews will stop working after February 8, 2026 if the new IP is not added to your allowlist. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Pull request overview
This PR adds a safety check to prevent accidental data loss when using wails init with the -d flag in non-empty directories. The change addresses issue #4940 where a user accidentally deleted their .git directory by running wails init -n project -d . in an existing project directory.
Changes:
- Added non-empty directory validation in the
Installfunction when-dflag is used - Empty directories are still allowed with
-dflag, but non-empty directories now return an error
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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>
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In `@website/src/pages/changelog.mdx`:
- Line 19: Update the changelog sentence in website/src/pages/changelog.mdx to
state that wails init no longer allows initialization in non-empty directories
when using the -d flag by replacing the current line that reads "Fixed `wails
init` allowing initialization in non-empty directories when using `-d` flag,
preventing accidental data loss" with clearer wording such as "Prevented `wails
init` from initializing in non-empty directories when using the `-d` flag to
avoid accidental data loss" so it reflects prevention rather than allowance.
🧹 Nitpick comments (1)
v2/pkg/templates/templates_test.go (1)
56-79: Assert the pre‑existing file survives the failed install.This test only checks the error message. To validate the “prevent data loss” requirement, also assert the original file still exists after the failure.
Proposed test enhancement
- // Create a file in the directory to make it non-empty - err = os.WriteFile(filepath.Join(tempDir, "existing-file.txt"), []byte("test"), 0644) + // Create a file in the directory to make it non-empty + existingFile := filepath.Join(tempDir, "existing-file.txt") + err = os.WriteFile(existingFile, []byte("test"), 0644) is2.NoErr(err) @@ _, _, err = Install(options) is2.True(err != nil) // Should fail is2.True(err.Error() == "cannot initialise project in non-empty directory: "+tempDir) + _, statErr := os.Stat(existingFile) + is2.NoErr(statErr) // Ensure nothing was removed
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Summary
When using
-dto specify a target directory,wails initnow checks if the directory is non-empty and errors if so. This prevents accidental data loss (e.g., overwriting.gitdirectories).Changes
v2/pkg/templates/templates.gowhen-dflag is usedTest plan
wails init -n testin empty directory - should succeedwails init -n test -d .in non-empty directory - should fail with error messagewails init -n test -d ./newdirwherenewdirdoesn't exist - should succeedFixes #4940
🤖 Generated with Claude Code
Summary by CodeRabbit
Bug Fixes
Tests
Documentation