Skip to content

Commit

Permalink
Do not allow for removal of subpackages
Browse files Browse the repository at this point in the history
Because it can mess up the system quite dramatically.
  • Loading branch information
Geod24 authored and PetarKirov committed Jan 22, 2024
1 parent 05e5883 commit 6a6ab4b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 20 deletions.
6 changes: 3 additions & 3 deletions source/dub/commandline.d
Original file line number Diff line number Diff line change
Expand Up @@ -2313,15 +2313,15 @@ class RemoveCommand : FetchRemoveCommand {
if (!m_version.empty) { // remove then --version removed
enforceUsage(!package_id.canFindVersionSplitter, "Double version spec not allowed.");
logWarn("The '--version' parameter was deprecated, use %s@%s. Please update your scripts.", package_id, m_version);
dub.remove(package_id, m_version, location);
dub.remove(PackageName(package_id), m_version, location);
} else {
const parts = UserPackageDesc.fromString(package_id);
const explicit = package_id.canFindVersionSplitter;
if (m_nonInteractive || explicit || parts.range != VersionRange.Any) {
const str = parts.range.matchesAny() ? "*" : parts.range.toString();
dub.remove(parts.name, str, location);
dub.remove(PackageName(parts.name), str, location);
} else {
dub.remove(package_id, location, &resolveVersion);
dub.remove(PackageName(package_id), location, &resolveVersion);
}
}
return 0;
Expand Down
45 changes: 28 additions & 17 deletions source/dub/dub.d
Original file line number Diff line number Diff line change
Expand Up @@ -1071,15 +1071,17 @@ class Dub {
is set to `RemoveVersionWildcard`.
Params:
package_id = Name of the package to be removed
name = Name of the package to be removed
location = Specifies the location to look for the given package
name/version.
resolve_version = Callback to select package version.
*/
void remove(string package_id, PlacementLocation location,
scope size_t delegate(in Package[] packages) resolve_version)
void remove(in PackageName name, PlacementLocation location,
scope size_t delegate(in Package[] packages) resolve_version)
{
enforce(!package_id.empty);
enforce(name.main.toString().length);
enforce(!name.sub.length, "Cannot remove subpackage %s, remove %s instead"
.format(name, name.main));
if (location == PlacementLocation.local) {
logInfo("To remove a locally placed package, make sure you don't have any data"
~ "\nleft in it's directory and then simply remove the whole directory.");
Expand All @@ -1089,16 +1091,13 @@ class Dub {
Package[] packages;

// Retrieve packages to be removed.
foreach(pack; m_packageManager.getPackageIterator(package_id))
foreach(pack; m_packageManager.getPackageIterator(name.toString()))
if (m_packageManager.isManagedPackage(pack))
packages ~= pack;

// Check validity of packages to be removed.
if(packages.empty) {
throw new Exception("Cannot find package to remove. ("
~ "id: '" ~ package_id ~ "', location: '" ~ to!string(location) ~ "'"
~ ")");
}
enforce(!packages.empty, "Cannot find package '%s' to remove at %s location"
.format(name, location.toString()));

// Sort package list in ascending version order
packages.sort!((a, b) => a.version_ < b.version_);
Expand All @@ -1114,12 +1113,19 @@ class Dub {
try {
remove(pack);
} catch (Exception e) {
logError("Failed to remove %s %s: %s", package_id, pack, e.msg);
logError("Failed to remove %s %s: %s", name, pack, e.msg);
logInfo("Continuing with other packages (if any).");
}
}
}

deprecated("Use `remove(PackageName, PlacementLocation, delegate)`")
void remove(string name, PlacementLocation location,
scope size_t delegate(in Package[] packages) resolve_version)
{
this.remove(PackageName(name), location, resolve_version);
}

/// Compatibility overload. Use the version without a `force_remove` argument instead.
deprecated("Use the overload without the 3rd argument (`force_remove`) instead")
void remove(string package_id, PlacementLocation location, bool force_remove,
Expand All @@ -1131,30 +1137,35 @@ class Dub {
/** Removes a specific version of a package.
Params:
package_id = Name of the package to be removed
name = Name of the package to be removed
version_ = Identifying a version or a wild card. If an empty string
is passed, the package will be removed from the location, if
there is only one version retrieved. This will throw an
exception, if there are multiple versions retrieved.
location = Specifies the location to look for the given package
name/version.
*/
void remove(string package_id, string version_, PlacementLocation location)
void remove(in PackageName name, string version_, PlacementLocation location)
{
remove(package_id, location, (in packages) {
remove(name, location, (in packages) {
if (version_ == RemoveVersionWildcard || version_.empty)
return packages.length;

foreach (i, p; packages) {
if (p.version_ == Version(version_))
return i;
}
throw new Exception("Cannot find package to remove. ("
~ "id: '" ~ package_id ~ "', version: '" ~ version_ ~ "', location: '" ~ to!string(location) ~ "'"
~ ")");
throw new Exception("Cannot find package '%s@%s' to remove at %s location"
.format(name, version_, location.toString()));
});
}

deprecated("Use `remove(PackageName, string, PlacementLocation)`")
void remove(string name, string version_, PlacementLocation location)
{
this.remove(PackageName(name), version_, location);
}

/// Compatibility overload. Use the version without a `force_remove` argument instead.
deprecated("Use the overload without force_remove instead")
void remove(string package_id, string version_, PlacementLocation location, bool force_remove)
Expand Down
1 change: 1 addition & 0 deletions source/dub/packagemanager.d
Original file line number Diff line number Diff line change
Expand Up @@ -885,6 +885,7 @@ symlink_exit:
{
logDebug("Remove %s, version %s, path '%s'", pack.name, pack.version_, pack.path);
enforce(!pack.path.empty, "Cannot remove package "~pack.name~" without a path.");
enforce(pack.parentPackage is null, "Cannot remove subpackage %s".format(pack.name));

// remove package from repositories' list
bool found = false;
Expand Down

0 comments on commit 6a6ab4b

Please sign in to comment.