Skip to content

fix(v2): prevent wails init in non-empty directory with -d flag - #4955

Merged
leaanthony merged 3 commits into
masterfrom
fix/v2-init-nonempty-check
Feb 4, 2026
Merged

leaanthony merged 3 commits into
masterfrom
fix/v2-init-nonempty-check

Conversation

@leaanthony

@leaanthony leaanthony commented Feb 3, 2026 •

Copy link
Copy Markdown
Member

Summary

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).

Changes

  • Added non-empty directory check in v2/pkg/templates/templates.go when -d flag is used

Test plan

  • wails init -n test in empty directory - should succeed
  • wails init -n test -d . in non-empty directory - should fail with error message
  • wails init -n test -d ./newdir where newdir doesn't exist - should succeed

Fixes #4940

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes

    • Install now permits initializing into an existing empty directory; attempting to initialize into a non-empty directory still returns an explicit error to prevent accidental data loss.
  • Tests

    • Added tests confirming install succeeds when target directory is empty and fails when it is non-empty.
  • Documentation

    • Changelog updated to note support for using the init flag with pre-existing empty directories.

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>
Copilot AI review requested due to automatic review settings February 3, 2026 20:32
@coderabbitai

coderabbitai Bot commented Feb 3, 2026 •

Copy link
Copy Markdown
Contributor

Caution

Review failed

The pull request is closed.

📝 Walkthrough

Walkthrough

Install 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

Cohort / File(s) Summary
Install Flow Safety Check
v2/pkg/templates/templates.go
Adjusted TargetDir validation: allow proceeding when an existing directory is empty; return an error when it is non-empty; still create the directory if it does not exist.
Tests: directory-empty behaviour
v2/pkg/templates/templates_test.go
Added tests: TestInstallFailsInNonEmptyDirectory (expects error for non-empty dir) and TestInstallSucceedsInEmptyDirectory (expects success for existing empty dir).
Changelog update
website/src/pages/changelog.mdx
Added note documenting that wails init now prevents initialization in non-empty directories when using -d, and allows initializing into existing empty directories.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

I’m a rabbit who checks every nook with care,
If the folder’s empty, I’ll hop right in there,
But if crumbs and clutter lie strewn on the floor,
I’ll sound the alarm and not open the door. 🐇✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 25.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and specifically describes the main change: preventing wails init in non-empty directories with the -d flag, which directly addresses the primary objective.
Description check ✅ Passed The description includes a clear summary of changes, references the linked issue (#4940), specifies the affected files, and provides a test plan covering the key scenarios.
Linked Issues check ✅ Passed The PR fully addresses issue #4940 by implementing the requested non-empty directory check to prevent accidental data loss when using the -d flag with wails init.
Out of Scope Changes check ✅ Passed All changes are directly scoped to implementing the non-empty directory check: core logic in templates.go, test coverage in templates_test.go, and changelog update. No extraneous changes detected.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/v2-init-nonempty-check

Important

Action Needed: IP Allowlist Update

If your organization protects your Git platform with IP whitelisting, please add the new CodeRabbit IP address to your allowlist:

  • ✨ 136.113.208.247/32 (new)
  • 34.170.211.100/32
  • 35.222.179.152/32

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 Install function when -d flag is used
  • Empty directories are still allowed with -d flag, but non-empty directories now return an error

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread v2/pkg/templates/templates.go
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Feb 3, 2026 •

Copy link
Copy Markdown

Deploying wails with  Cloudflare Pages  Cloudflare Pages

Latest commit: 0cd0ba6
Status:⚡️  Build in progress...

View logs

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>
@github-actions github-actions Bot added the Documentation Improvements or additions to documentation label Feb 3, 2026

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment thread website/src/pages/changelog.mdx Outdated
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
@leaanthony
leaanthony merged commit 718fd92 into master Feb 4, 2026
13 of 16 checks passed
@leaanthony
leaanthony deleted the fix/v2-init-nonempty-check branch February 4, 2026 10:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Documentation Improvements or additions to documentation templates v2-only

Projects

None yet

Development

Successfully merging this pull request may close these issues.

init command should check for existing project

2 participants