Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix dub dependency selection #359

Merged
merged 2 commits into from
Mar 2, 2024
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
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
6 changes: 4 additions & 2 deletions test/tc_dub/source/app.d
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ void main()
// if no dependencies are fetched
// or with all dependencies there a lot more
assert(dub.imports.length >= 2, dub.imports.to!string);
assert(dub.stringImports[0].endsWith("views")
|| dub.stringImports[0].endsWith("views/") || dub.stringImports[0].endsWith("views\\"));
assert(dub.stringImports[$ - 1].endsWith("views")
|| dub.stringImports[$ - 1].endsWith("views/")
|| dub.stringImports[$ - 1].endsWith("views\\"),
dub.stringImports.to!string ~ " doesn't end with `views`!");
assert(dub.fileImports.length > 10);
assert(dub.configurations.length == 2);
assert(dub.buildTypes.length);
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);
}
88 changes: 61 additions & 27 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 @@ -228,15 +225,9 @@ class DubComponent : ComponentWrapper

try
{
string[] failedPackages = null;
scope (exit)
_failedPackages = failedPackages;
string[string] configs = _dub.project.getPackageConfigs(settings.platform, settings.config);
foreach (pack; _dub.project.getTopologicalPackageList(true, null, configs))
failedPackages ~= pack.name;
failedPackages.sort!"a<b";

auto gen = new TargetDescriptionGenerator(_dub.project);
auto gen = new TargetInfoGenerator(_dub.project);
gen.generate(settings);

auto importPaths = appender!(string[]);
Expand All @@ -245,25 +236,25 @@ class DubComponent : ComponentWrapper
auto versions = appender!(string[]);
auto debugVersions = appender!(string[]);

foreach (target; gen.targetDescriptions)
bool failed;
foreach (pack; _dub.project.getTopologicalPackageList(true, null, configs))
{
// remove resolved dependencies from failedPackages
foreach (name; [target.rootPackage] ~ target.packages)
if (auto target = pack.name in gen.targets)
{
auto match = failedPackages.assumeSorted!"a<b".trisect(name);
foreach (i; 0 .. match[1].length)
failedPackages = failedPackages.remove(match[0].length);
importPaths ~= target.buildSettings.importPaths;
stringImportPaths ~= target.buildSettings.stringImportPaths;
sourceFiles ~= target.buildSettings.sourceFiles;
sourceFiles ~= target.buildSettings.importFiles; // what are these exactly?
versions ~= target.buildSettings.versions;
debugVersions ~= target.buildSettings.debugVersions;
}
else
{
failed = true;
}

importPaths ~= target.buildSettings.importPaths;
stringImportPaths ~= target.buildSettings.stringImportPaths;
sourceFiles ~= target.buildSettings.sourceFiles;
sourceFiles ~= target.buildSettings.importFiles; // what are these exactly?
versions ~= target.buildSettings.versions;
debugVersions ~= target.buildSettings.debugVersions;
}

string[] rootSourcePaths;
/* string[] rootSourcePaths;
if (auto rootTargetIndex = _dub.projectName in gen.targetDescriptionLookup)
{
auto rootTarget = gen.targetDescriptions[*rootTargetIndex];
Expand All @@ -281,14 +272,16 @@ class DubComponent : ComponentWrapper
rootSourcePaths ~= sourcePath;
}
}

_importPaths = rootSourcePaths ~ importPaths.data;
*/

_importPaths = importPaths.data;
_stringImportPaths = stringImportPaths.data;
_importFiles = sourceFiles.data;
_versions = versions.data;
_debugVersions = debugVersions.data;

return failedPackages.length == 0;
return !failed;
}
catch (Exception e)
{
Expand Down Expand Up @@ -319,6 +312,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 +383,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 +1014,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 Expand Up @@ -1043,3 +1062,18 @@ class ServedDub : Dub
return s;
}
}

class TargetInfoGenerator : ProjectGenerator
{
const(TargetInfo)[string] targets;

this(Project project)
{
super(project);
}

protected override void generateTargets(GeneratorSettings settings, in TargetInfo[string] targets)
{
this.targets = targets.dup;
}
}
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