www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - dub.selections.json & optional dependencies: How's it work?

reply jmh530 <john.michael.hall gmail.com> writes:
I'm looking at the dub package format [1] about optional 
dependencies and it says:

"With this set to true, the dependency will only be used if 
explicitly selected in dub.selections.json. If omitted, this 
attribute defaults to false."

And it occurs to me that I don't know anything about how 
dub.selections.json works.

I would think dub optional dependencies work such that if there 
are no functions being called/compiled that import an optional 
dependency, then the dependency wouldn't be included. How is it 
different from `dmd -i`?

The dub.selections.json in some of my projects look like a 
listing of the dependencies and their versions. Above should 
imply that the optional dependency would only get included there 
if the import is actually used somewhere in the project. Is that 
correct?

[1] https://dub.pm/package-format-json.html
Feb 24 2023
parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
On 2/24/23 2:01 PM, jmh530 wrote:
 I'm looking at the dub package format [1] about optional dependencies 
 and it says:
 
 "With this set to true, the dependency will only be used if explicitly 
 selected in dub.selections.json. If omitted, this attribute defaults to 
 false."
 
 And it occurs to me that I don't know anything about how 
 dub.selections.json works.
Let's say you have dependency A, which has an *optional* dependency on B. In your project, if you depend on A, but don't explicitly depend on B, then A will be built without B as a dependency. If you have a dependency on A *and* B, then B's optional dependency will kick in, and participate in the selection of it's version. So for instance, you could depend on B version 1 or higher, and the optional dependency could be on v1.5 or higher, it will select the highest version of 1.5 dub.selections.json only applies to the primary project. So the decision on which versions of which dependencies to use is decided by the whole tree. -Steve
Feb 24 2023
parent jmh530 <john.michael.hall gmail.com> writes:
On Friday, 24 February 2023 at 19:37:41 UTC, Steven Schveighoffer 
wrote:
 On 2/24/23 2:01 PM, jmh530 wrote:
 I'm looking at the dub package format [1] about optional 
 dependencies and it says:
 
 "With this set to true, the dependency will only be used if 
 explicitly selected in dub.selections.json. If omitted, this 
 attribute defaults to false."
 
 And it occurs to me that I don't know anything about how 
 dub.selections.json works.
Let's say you have dependency A, which has an *optional* dependency on B. In your project, if you depend on A, but don't explicitly depend on B, then A will be built without B as a dependency. If you have a dependency on A *and* B, then B's optional dependency will kick in, and participate in the selection of it's version. So for instance, you could depend on B version 1 or higher, and the optional dependency could be on v1.5 or higher, it will select the highest version of 1.5 dub.selections.json only applies to the primary project. So the decision on which versions of which dependencies to use is decided by the whole tree. -Steve
Ok this makes sense. So it's all about dependencies downstream. For instance, if I list B as a dependency in my project but never use it in my own project, it will still compile in (so for instance if A's functionality differs depending on if B is included, then I will get that functionality even if I don't use B directly in my project).
Feb 24 2023