Skip to content

Commit 11a8ac8

Browse files
Geod24WebFreak001
authored andcommitted
Move environment read outside of DependencyResolver
Reading environment variables from random places in the code makes the flow harder to follow, and unittesting harder. This moves one environment reads to the `dub` module, which performs many of them already.
1 parent 3e70d33 commit 11a8ac8

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

source/dub/dependencyresolver.d

+28-6
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,32 @@ import std.string : format, lastIndexOf;
2929
can be defined in terms of a version range.
3030
*/
3131
class DependencyResolver(CONFIGS, CONFIG) {
32+
/// Maximum number of loop rounds to do
33+
protected ulong loop_limit;
34+
35+
/**
36+
* Construct an instance of this class
37+
*
38+
* Params:
39+
* limit = Maximum number of loop rounds to do
40+
*/
41+
public this (ulong limit) inout scope @safe pure nothrow @nogc
42+
{
43+
this.loop_limit = limit;
44+
}
45+
46+
/// Compatibility overload
47+
deprecated("Use the overload that accepts a `ulong limit` argument")
48+
public this () scope @safe
49+
{
50+
// Leave the possibility to opt-out from the loop limit
51+
import std.process : environment;
52+
if (environment.get("DUB_NO_RESOLVE_LIMIT") !is null)
53+
this(ulong.max);
54+
else
55+
this(1_000_000);
56+
}
57+
3258
/** Encapsulates a list of outgoing edges in the dependency graph.
3359
3460
A value of this type represents a single dependency with multiple
@@ -75,17 +101,13 @@ class DependencyResolver(CONFIGS, CONFIG) {
75101

76102
CONFIG[string] resolve(TreeNode root, bool throw_on_failure = true)
77103
{
78-
// Leave the possibility to opt-out from the loop limit
79-
import std.process : environment;
80-
bool no_loop_limit = environment.get("DUB_NO_RESOLVE_LIMIT") !is null;
81-
82104
auto rootbase = root.pack.basePackageName;
83105

84106
// build up the dependency graph, eliminating as many configurations/
85107
// versions as possible
86108
ResolveContext context;
87109
context.configs[rootbase] = [ResolveConfig(root.config, true)];
88-
ulong loop_counter = no_loop_limit ? ulong.max : 1_000_000;
110+
ulong loop_counter = this.loop_limit;
89111
constrain(root, context, loop_counter);
90112

91113
// remove any non-default optional dependencies
@@ -358,7 +380,7 @@ unittest {
358380

359381
static class TestResolver : DependencyResolver!(IntConfigs, IntConfig) {
360382
private TreeNodes[][string] m_children;
361-
this(TreeNodes[][string] children) { m_children = children; }
383+
this(TreeNodes[][string] children) { super(ulong.max); m_children = children; }
362384
protected override IntConfig[] getAllConfigs(string pack) {
363385
auto ret = appender!(IntConfig[]);
364386
foreach (p; m_children.byKey) {

source/dub/dub.d

+5
Original file line numberDiff line numberDiff line change
@@ -1457,6 +1457,11 @@ private class DependencyVersionResolver : DependencyResolver!(Dependency, Depend
14571457

14581458
this(Dub dub, UpgradeOptions options, Package root, SelectedVersions selected_versions)
14591459
{
1460+
if (environment.get("DUB_NO_RESOLVE_LIMIT") !is null)
1461+
super(ulong.max);
1462+
else
1463+
super(1_000_000);
1464+
14601465
m_dub = dub;
14611466
m_options = options;
14621467
m_rootPackage = root;

0 commit comments

Comments
 (0)