From fed324cba445b123871a0a1261046dd248d40e29 Mon Sep 17 00:00:00 2001 From: roble Date: Tue, 3 Nov 2026 20:08:55 +0000 Subject: [PATCH 1/4] feat(installer): delete a removed module's folder by default when it is backed up (sc-1077) Co-Authored-By: Claude Opus 5.5 --- README.md | 20 +++++++------- src/Installer.php | 6 +++-- tests/ModuleInstallerTest.php | 50 ++++++++++++++++++++++++++++++++--- 3 files changed, 60 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 1bf7498..cbefb91 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, and a path repository's folder is left alone. -``` -- 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..72e8e2a 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); diff --git a/tests/ModuleInstallerTest.php b/tests/ModuleInstallerTest.php index 3271fd9..bb0ca3c 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,56 @@ 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 + /** 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 { + $installer->uninstall($this->createStub(InstalledRepositoryInterface::class), $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): 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(null, $composer); } public function test_should_delete_on_remove_returns_true_when_flag_enabled(): void From b849ee994f2cdae96b280ea6f76c307a90f5e057 Mon Sep 17 00:00:00 2001 From: roble Date: Tue, 3 Nov 2026 20:55:06 +0000 Subject: [PATCH 2/4] fix: keep a module folder another installed package still uses (sc-1077) Co-Authored-By: Claude Opus 5.5 --- README.md | 2 +- src/Installer.php | 22 ++++++++++++++++++++++ tests/ModuleInstallerTest.php | 27 +++++++++++++++++++++++++-- 3 files changed, 48 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index cbefb91..bda112d 100644 --- a/README.md +++ b/README.md @@ -147,7 +147,7 @@ The installer performs a clean install into the now-empty path. ## Removing a Module -`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, and a path repository's folder is left alone. +`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. With `module-backups: 0` there is no backup, so the folder stays unless `module-delete-on-remove` is `true`. To always keep it: diff --git a/src/Installer.php b/src/Installer.php index 72e8e2a..c02e96c 100644 --- a/src/Installer.php +++ b/src/Installer.php @@ -889,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 bb0ca3c..9837596 100644 --- a/tests/ModuleInstallerTest.php +++ b/tests/ModuleInstallerTest.php @@ -1708,6 +1708,29 @@ public function test_uninstall_skips_folder_deletion_by_default_and_removes_from $installer->uninstall($repo, $pkg); } + /** 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 { @@ -1750,14 +1773,14 @@ public function test_uninstall_refuses_a_git_checkout(): void } /** @param array $extra */ - private function installerWithExtra(array $extra): TestableInstaller + 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); - return new TestableInstaller(null, $composer); + return new TestableInstaller($io, $composer); } public function test_should_delete_on_remove_returns_true_when_flag_enabled(): void From 49983d277a49138c42c56dbb83da3393e7360c72 Mon Sep 17 00:00:00 2001 From: roble Date: Tue, 3 Nov 2026 20:55:32 +0000 Subject: [PATCH 3/4] test: give the uninstall mock its installed packages (sc-1077) Co-Authored-By: Claude Opus 5.5 --- tests/ModuleInstallerTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/ModuleInstallerTest.php b/tests/ModuleInstallerTest.php index 9837596..3c86a6d 100644 --- a/tests/ModuleInstallerTest.php +++ b/tests/ModuleInstallerTest.php @@ -1805,6 +1805,7 @@ public function test_uninstall_path_repository_guard_takes_precedence_over_delet $pkg->method('getPrettyName')->willReturn('saucebase/test-module'); $repo = $this->createMock(InstalledRepositoryInterface::class); + $repo->method('getPackages')->willReturn([]); $repo->method('hasPackage')->willReturn(true); $repo->expects($this->once())->method('removePackage')->with($pkg); From 7e5957ccb2a8cb3d06057b82b709347d2b29b268 Mon Sep 17 00:00:00 2001 From: roble Date: Tue, 3 Nov 2026 20:55:52 +0000 Subject: [PATCH 4/4] test: stub installed packages in the right uninstall test (sc-1077) Co-Authored-By: Claude Opus 5.5 --- tests/ModuleInstallerTest.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/ModuleInstallerTest.php b/tests/ModuleInstallerTest.php index 3c86a6d..6080910 100644 --- a/tests/ModuleInstallerTest.php +++ b/tests/ModuleInstallerTest.php @@ -1762,7 +1762,9 @@ public function test_uninstall_refuses_a_git_checkout(): void $pkg->setDistType('zip'); try { - $installer->uninstall($this->createStub(InstalledRepositoryInterface::class), $pkg); + $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'); @@ -1805,7 +1807,6 @@ public function test_uninstall_path_repository_guard_takes_precedence_over_delet $pkg->method('getPrettyName')->willReturn('saucebase/test-module'); $repo = $this->createMock(InstalledRepositoryInterface::class); - $repo->method('getPackages')->willReturn([]); $repo->method('hasPackage')->willReturn(true); $repo->expects($this->once())->method('removePackage')->with($pkg);