diff --git a/README.md b/README.md
index 1bf7498..bda112d 100644
--- a/README.md
+++ b/README.md
@@ -44,7 +44,7 @@ All options live in the `extra` section of the **root** `composer.json`. Every k
| `module-dir` | `modules` | Directory modules are installed into. |
| `module-exclude-dirs` | `[".github", ".git"]` | Directories removed from each module after install/update. |
| `module-update-strategy` | `merge` | `merge` preserves local edits; `overwrite` replaces entirely. |
-| `module-delete-on-remove` | `false` | Set to `true` to delete the module folder on `composer remove`. |
+| `module-delete-on-remove` | `true` while backups are on | Deletes the module folder on `composer remove`, after backing it up. `false` keeps it. |
| `module-backups` | `3` | Backups kept per module before an update or removal changes its folder. `0` turns them off. |
| `module-allow-symlink` | `false` | Set to `true` to allow a symlinked path repository outside the app. |
@@ -145,25 +145,25 @@ composer update vendor/example-module
The installer performs a clean install into the now-empty path.
-## Skipping Module Deletion on Remove
+## Removing a Module
-By default, running `composer remove` on a module **does not delete the module directory**. The package is removed from `composer.lock` and Composer's tracking, but the folder stays on disk for manual review.
+`composer remove` backs the module folder up to `storage/app/module-backups/` (see `module-backups`), then deletes it. A folder the installer doesn't own is never deleted: a symlink or a git checkout (`.git` folder or file) is refused, so a path repository's symlink or git folder stays. A path repository copied without its `.git` is backed up and deleted like any other. A folder another installed module still uses stays too.
-```
-- Skipping deletion of module directory (set module-delete-on-remove: true to enable): modules/example-module
-```
-
-To restore the old behaviour and delete the folder automatically:
+With `module-backups: 0` there is no backup, so the folder stays unless `module-delete-on-remove` is `true`. To always keep it:
```json
{
"extra": {
- "module-delete-on-remove": true
+ "module-delete-on-remove": false
}
}
```
-> **Why skip by default?** A `composer remove` followed by `composer install` in CI will not re-download an untracked folder — but it also will not delete your work. The conservative default prevents accidental data loss; opt in to deletion explicitly.
+```
+- Keeping the module directory (module-delete-on-remove is off); delete it to finish removing the module: modules/example-module
+```
+
+Delete the folder yourself then: an app with Saucebase core refuses to boot with a module folder Composer doesn't autoload.
## Safety
diff --git a/src/Installer.php b/src/Installer.php
index 88c9216..c02e96c 100644
--- a/src/Installer.php
+++ b/src/Installer.php
@@ -854,7 +854,9 @@ protected function shouldDeleteOnRemove(): bool
$extra = $this->composer->getPackage()->getExtra();
- return (bool) ($extra['module-delete-on-remove'] ?? false);
+ // The folder is backed up before it goes, so deleting is safe by default; with
+ // backups off it stays unless the app asks for deletion.
+ return (bool) ($extra['module-delete-on-remove'] ?? (int) ($extra['module-backups'] ?? self::DEFAULT_BACKUPS) > 0);
}
/**
@@ -878,7 +880,7 @@ public function uninstall(InstalledRepositoryInterface $repo, PackageInterface $
if (! $this->shouldDeleteOnRemove()) {
$installPath = $this->getInstallPath($package);
- $this->io->write(" - Skipping deletion of module directory (set module-delete-on-remove: true to enable): {$installPath}");
+ $this->io->write(" - Keeping the module directory (module-delete-on-remove is off); delete it to finish removing the module: {$installPath}");
if ($repo->hasPackage($package)) {
$repo->removePackage($package);
@@ -887,12 +889,34 @@ public function uninstall(InstalledRepositoryInterface $repo, PackageInterface $
return \React\Promise\resolve(null);
}
+ $sharedWith = $this->packageSharingFolder($repo, $package);
+ if ($sharedWith !== null) {
+ $this->io->write(" - Keeping the module directory, {$sharedWith} still uses it: {$this->getInstallPath($package)}");
+ $repo->removePackage($package);
+
+ return \React\Promise\resolve(null);
+ }
+
$this->assertOwnCopy($this->getInstallPath($package));
$this->backupModule($this->getInstallPath($package), $package);
return parent::uninstall($repo, $package);
}
+ /** Another installed module in this folder: modules of one name share it, whatever their vendor. */
+ protected function packageSharingFolder(InstalledRepositoryInterface $repo, PackageInterface $package): ?string
+ {
+ foreach ($repo->getPackages() as $other) {
+ if ($other->getName() !== $package->getName()
+ && $this->supports($other->getType())
+ && $this->getInstallPath($other) === $this->getInstallPath($package)) {
+ return $other->getPrettyName();
+ }
+ }
+
+ return null;
+ }
+
/**
* Refuses to change a module folder that isn't the app's own copy: a symlink, a git
* checkout, or anything resolving outside the modules directory. Every step that deletes
diff --git a/tests/ModuleInstallerTest.php b/tests/ModuleInstallerTest.php
index 3271fd9..6080910 100644
--- a/tests/ModuleInstallerTest.php
+++ b/tests/ModuleInstallerTest.php
@@ -1694,7 +1694,7 @@ public function test_uninstall_skips_folder_deletion_by_default_and_removes_from
$io = $this->createMock(IOInterface::class);
$io->expects($this->once())
->method('write')
- ->with($this->stringContains('Skipping deletion of module directory'));
+ ->with($this->stringContains('Keeping the module directory'));
$pkg = $this->createStub(PackageInterface::class);
$pkg->method('getPrettyName')->willReturn('saucebase/test-module');
@@ -1708,14 +1708,81 @@ public function test_uninstall_skips_folder_deletion_by_default_and_removes_from
$installer->uninstall($repo, $pkg);
}
- public function test_should_delete_on_remove_returns_false_when_flag_absent(): void
+ /** Two vendors' modules of one name install to one folder; removing one leaves it for the other. */
+ public function test_uninstall_keeps_a_folder_another_package_still_uses(): void
+ {
+ $io = $this->createMock(IOInterface::class);
+ $io->expects($this->once())
+ ->method('write')
+ ->with($this->stringContains('vendor-b/billing'));
+
+ $pkg = new Package('vendor-a/billing', '1.0.0.0', '1.0.0');
+ $pkg->setType('saucebase-module');
+ $other = new Package('vendor-b/billing', '1.0.0.0', '1.0.0');
+ $other->setType('saucebase-module');
+
+ $repo = $this->createMock(InstalledRepositoryInterface::class);
+ $repo->method('hasPackage')->willReturn(true);
+ $repo->method('getPackages')->willReturn([$pkg, $other]);
+ $repo->expects($this->once())->method('removePackage')->with($pkg);
+
+ $installer = $this->installerWithExtra([], $io);
+ $installer->forcePathRepository = false;
+ $installer->uninstall($repo, $pkg);
+ }
+
+ /** The folder is backed up before it goes, so removing it is the default. */
+ public function test_should_delete_on_remove_by_default(): void
+ {
+ $this->assertTrue($this->installerWithExtra([])->callShouldDeleteOnRemove());
+ }
+
+ /** No backup, no deletion: with backups off the folder stays unless asked for. */
+ public function test_should_keep_the_folder_when_backups_are_off(): void
+ {
+ $this->assertFalse($this->installerWithExtra(['module-backups' => 0])->callShouldDeleteOnRemove());
+ $this->assertTrue($this->installerWithExtra(['module-backups' => 0, 'module-delete-on-remove' => true])->callShouldDeleteOnRemove());
+ }
+
+ public function test_should_keep_the_folder_when_told_to(): void
+ {
+ $this->assertFalse($this->installerWithExtra(['module-delete-on-remove' => false])->callShouldDeleteOnRemove());
+ }
+
+ /** A module someone turned into a git checkout is refused, not deleted. */
+ public function test_uninstall_refuses_a_git_checkout(): void
+ {
+ $baseDir = sys_get_temp_dir().'/uninstall-git-'.uniqid('', true);
+ mkdir($baseDir.'/test-module/.git', 0755, true);
+ file_put_contents($baseDir.'/test-module/composer.json', '{}');
+
+ $installer = $this->makeSafetyInstaller($baseDir);
+ $installer->backupRoot = $baseDir.'/backups';
+ $pkg = new Package('saucebase/test-module', '1.0.0.0', '1.0.0');
+ $pkg->setDistType('zip');
+
+ try {
+ $repo = $this->createStub(InstalledRepositoryInterface::class);
+ $repo->method('getPackages')->willReturn([]);
+ $installer->uninstall($repo, $pkg);
+ $this->fail('A git checkout was not refused.');
+ } catch (ModuleInstallerException) {
+ $this->assertDirectoryExists($baseDir.'/test-module/.git');
+ $this->assertDirectoryDoesNotExist($baseDir.'/backups');
+ }
+
+ (new Filesystem)->remove($baseDir);
+ }
+
+ /** @param array $extra */
+ private function installerWithExtra(array $extra, ?IOInterface $io = null): TestableInstaller
{
$composer = $this->createStub(Composer::class);
$root = new RootPackage('root/app', '1.0.0.0', '1.0.0');
+ $root->setExtra($extra);
$composer->method('getPackage')->willReturn($root);
- $installer = new TestableInstaller(null, $composer);
- $this->assertFalse($installer->callShouldDeleteOnRemove());
+ return new TestableInstaller($io, $composer);
}
public function test_should_delete_on_remove_returns_true_when_flag_enabled(): void