Skip to content

Commit

Permalink
Fix dub dependency selection
Browse files Browse the repository at this point in the history
  • Loading branch information
WebFreak001 committed Feb 13, 2024
1 parent 59be1bc commit 02bb374
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 58 deletions.
72 changes: 31 additions & 41 deletions source/served/commands/dub.d
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,11 @@ void onDidSaveDubRecipe(DidSaveTextDocumentParams params)
return;
}

rpc.window.runOrMessage(backend.get!DubComponent(workspaceRoot)
.selectAndDownloadMissing(), MessageType.warning, translate!"d.ext.dubUpgradeFail");
rpc.window.runOrMessage({
DubComponent dub = backend.get!DubComponent(workspaceRoot);
dub.updateImportPaths(true);
dub.selectAndDownloadMissing();
}(), MessageType.warning, translate!"d.ext.dubUpgradeFail");
}
else
{
Expand All @@ -285,7 +288,7 @@ void onDidSaveDubRecipe(DidSaveTextDocumentParams params)

setTimeout({
const successfulUpdate = rpc.window.runOrMessage(backend.get!DubComponent(workspaceRoot)
.updateImportPaths(true), MessageType.warning, translate!"d.ext.dubImportFail");
.updateImportPaths(false), MessageType.warning, translate!"d.ext.dubImportFail");
if (successfulUpdate)
{
rpc.window.runOrMessage(updateImports(UpdateImportsParams(false)),
Expand All @@ -312,7 +315,7 @@ void onDidSaveDubRecipe(DidSaveTextDocumentParams params)
if (backend.attach(backend.getInstance(workspaceRoot), "dub", err))
{
rpc.window.runOrMessage(backend.get!DubComponent(workspaceRoot)
.updateImportPaths(true), MessageType.warning,
.updateImportPaths(false), MessageType.warning,
translate!"d.ext.dubRecipeMaybeBroken");
error(err);
}
Expand All @@ -329,62 +332,49 @@ DubDependency[] listDependencies(ListDependenciesParams params)
if (!instance.has!DubComponent)
return ret;

auto allDeps = instance.get!DubComponent.dependencies;
auto failed = instance.get!DubComponent.failedPackages;
auto dub = instance.get!DubComponent;
auto failed = dub.failedPackages;
if (!params.packageName.length)
{
auto deps = instance.get!DubComponent.rootDependencies;
auto deps = dub.rootDependencies;
foreach (dep; deps)
{
DubDependency r;
r.name = dep;
r.failed = failed.canFind(dep);
r.root = true;
foreach (other; allDeps)
if (other.name == dep)
{
r.version_ = other.ver;
r.path = other.path;
r.description = other.description;
r.homepage = other.homepage;
r.authors = other.authors;
r.copyright = other.copyright;
r.license = other.license;
r.subPackages = other.subPackages.map!"a.name".array;
r.hasDependencies = other.dependencies.length > 0;
break;
}
auto other = dub.getPackageInfo(dep);
r.version_ = other.ver;
r.path = other.path;
r.description = other.description;
r.homepage = other.homepage;
r.authors = other.authors;
r.copyright = other.copyright;
r.license = other.license;
r.subPackages = other.subPackages.map!"a.name".array;
r.hasDependencies = other.dependencies.length > 0;
ret ~= r;
}
}
else
{
string[string] aa;
foreach (other; allDeps)
if (other.name == params.packageName)
{
aa = other.dependencies;
break;
}
auto info = dub.getPackageInfo(params.packageName);
string[string] aa = info.dependencies;
foreach (name, ver; aa)
{
DubDependency r;
r.name = name;
r.failed = failed.canFind(name);
r.version_ = ver;
foreach (other; allDeps)
if (other.name == name)
{
r.path = other.path;
r.description = other.description;
r.homepage = other.homepage;
r.authors = other.authors;
r.copyright = other.copyright;
r.license = other.license;
r.subPackages = other.subPackages.map!"a.name".array;
r.hasDependencies = other.dependencies.length > 0;
break;
}
auto other = dub.getPackageInfo(name);
r.path = other.path;
r.description = other.description;
r.homepage = other.homepage;
r.authors = other.authors;
r.copyright = other.copyright;
r.license = other.license;
r.subPackages = other.subPackages.map!"a.name".array;
r.hasDependencies = other.dependencies.length > 0;
ret ~= r;
}
}
Expand Down
37 changes: 30 additions & 7 deletions test/tc_dub_dependencies/source/app.d
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,53 @@ import workspaced.com.dub;

void main()
{
{
import std.logger;
globalLogLevel = LogLevel.trace;
static if (__VERSION__ < 2101)
sharedLog = new FileLogger(stderr);
else
sharedLog = (() @trusted => cast(shared) new FileLogger(stderr))();
}

string dir = buildNormalizedPath(fs.getcwd, "project");

fs.write(buildPath(dir, "dub.sdl"), "name \"project\"\n");

scope backend = new WorkspaceD();
backend.addInstance(dir);
backend.register!DubComponent;

import dub.internal.logging : LogLevel, setLogLevel;
setLogLevel(LogLevel.debug_);

version (Posix)
{
if (fs.exists(expandTilde(`~/.dub/packages/gitcompatibledubpackage-1.0.4/`)))
fs.rmdirRecurse(expandTilde(`~/.dub/packages/gitcompatibledubpackage-1.0.4/`));
if (fs.exists(expandTilde(`~/.dub/packages/gitcompatibledubpackage/1.0.4/`)))
fs.rmdirRecurse(expandTilde(`~/.dub/packages/gitcompatibledubpackage/1.0.4/`));
}

auto dub = backend.get!DubComponent(dir);

dub.upgradeAndSelectAll();
assert(dub.dependencies.length == 0);
assert(dub.rootDependencies.length == 0);
dub.selectAndDownloadMissing();
assert(dub.rootDependencies.length == 0);

fs.write(buildPath(dir, "dub.sdl"), "name \"project\"\ndependency \"gitcompatibledubpackage\" version=\"1.0.4\"\n");

assert(dub.dependencies.length == 0);
dub.updateImportPaths();
assert(dub.dependencies.length == 1);
assert(dub.dependencies[0].name == "gitcompatibledubpackage");
assert(dub.missingDependencies.length == 1);
assert(dub.rootDependencies.length == 1);
dub.selectAndDownloadMissing();
assert(dub.missingDependencies.length == 0);
assert(dub.rootDependencies.length == 1);
assert(dub.rootDependencies[0] == "gitcompatibledubpackage");

fs.write(buildPath(dir, "dub.sdl"), "name \"project\"\n");

assert(dub.dependencies.length == 1);
assert(dub.rootDependencies.length == 1);
dub.updateImportPaths();
assert(dub.dependencies.length == 0);
assert(dub.rootDependencies.length == 0);
}
31 changes: 27 additions & 4 deletions workspace-d/source/workspaced/com/dub.d
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,7 @@ class DubComponent : ComponentWrapper

static void registered()
{
static if (__traits(compiles, { import dub.internal.logging; }))
import dub.internal.logging;
else
import dub.internal.vibecompat.core.log;
import dub.internal.logging;

setLogLevel(LogLevel.none);
}
Expand Down Expand Up @@ -319,6 +316,9 @@ class DubComponent : ComponentWrapper
void upgrade(UpgradeOptions options)
{
_dub.upgrade(options);
trace("dependencies after upgrade(", options,
"): found: ", rootDependencies,
"\nmissing:", missingDependencies);
optionalifyRoot();
}

Expand Down Expand Up @@ -387,13 +387,35 @@ class DubComponent : ComponentWrapper

/// Lists all dependencies. This will go through all dependencies and contain the dependencies of dependencies. You need to create a tree structure from this yourself.
/// Returns: `[{dependencies: string[string], ver: string, name: string}]`
deprecated("this API is broken without dub.selections.json - use rootDependencies, selectedVersions and getPackageInfo instead to operate on IDE selections")
auto dependencies() @property const
{
validateConfiguration();

return listDependencies(_dub.project);
}

/// Lists all selected dependencies.
Dependency[string] selectedVersions() @property const
{
validateConfiguration();

Dependency[string] ret;
foreach (key; _dub.project.selections.selectedPackages)
ret[key] = _dub.project.selections.getSelectedVersion(key);
return ret;
}

/// Gets the dependency information about a package.
/// Returns `DubPackageInfo.init` if it's not found.
DubPackageInfo getPackageInfo(string pkgName) @property const
{
auto pkg = _dub.project.getDependency(pkgName, true);
if (!pkg)
return DubPackageInfo.init;
return getInfo(pkg);
}

/// Lists dependencies of the root package. This can be used as a base to create a tree structure.
string[] rootDependencies() @property const
{
Expand Down Expand Up @@ -996,6 +1018,7 @@ DubPackageInfo getInfo(in Package dep)
return info;
}

deprecated("This API doesn't take into account local / IDE selections")
auto listDependencies(scope const Project project)
{
auto deps = project.dependencies;
Expand Down
9 changes: 3 additions & 6 deletions workspace-d/source/workspaced/com/snippets/dependencies.d
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,9 @@ class DependencyBasedSnippetProvider : SnippetProvider
string id = typeid(this).name;
auto dub = instance.get!DubComponent;
return typeof(return).async(delegate() {
string[] deps;
foreach (dep; dub.dependencies)
{
deps ~= dep.name;
deps ~= dep.dependencies.keys;
}
// TODO: this should use dependencies, not selections, but this
// regressed: https://github.com/dlang/dub/issues/2853
string[] deps = dub.selectedVersions.keys;
Snippet[] ret;
foreach (k, v; snippets)
{
Expand Down

0 comments on commit 02bb374

Please sign in to comment.