Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. |

Expand Down Expand Up @@ -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

Expand Down
28 changes: 26 additions & 2 deletions src/Installer.php
Original file line number Diff line number Diff line change
Expand Up @@ -854,7 +854,9 @@

$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);

Check warning on line 859 in src/Installer.php

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Split this 123 characters long line (which is greater than 120 authorized).

See more on https://sonarcloud.io/project/issues?id=saucebase-dev_module-installer&issues=AaGjCFzAU4eTBBawC_Nh&open=AaGjCFzAU4eTBBawC_Nh&pullRequest=27
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}

/**
Expand All @@ -878,7 +880,7 @@

if (! $this->shouldDeleteOnRemove()) {
$installPath = $this->getInstallPath($package);
$this->io->write(" - <info>Skipping deletion of module directory (set module-delete-on-remove: true to enable):</info> {$installPath}");
$this->io->write(" - <info>Keeping the module directory (module-delete-on-remove is off); delete it to finish removing the module:</info> {$installPath}");

Check warning on line 883 in src/Installer.php

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Split this 168 characters long line (which is greater than 120 authorized).

See more on https://sonarcloud.io/project/issues?id=saucebase-dev_module-installer&issues=AaGjCFzAU4eTBBawC_Ni&open=AaGjCFzAU4eTBBawC_Ni&pullRequest=27

if ($repo->hasPackage($package)) {
$repo->removePackage($package);
Expand All @@ -887,12 +889,34 @@
return \React\Promise\resolve(null);
}

$sharedWith = $this->packageSharingFolder($repo, $package);
if ($sharedWith !== null) {
$this->io->write(" - <info>Keeping the module directory, {$sharedWith} still uses it:</info> {$this->getInstallPath($package)}");

Check warning on line 894 in src/Installer.php

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Split this 142 characters long line (which is greater than 120 authorized).

See more on https://sonarcloud.io/project/issues?id=saucebase-dev_module-installer&issues=AaGjMg7eKaxp2EmXzRKB&open=AaGjMg7eKaxp2EmXzRKB&pullRequest=27
$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
Expand Down
75 changes: 71 additions & 4 deletions tests/ModuleInstallerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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<string, mixed> $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
Expand Down
Loading