@@ -271,7 +271,8 @@ class PackageManager {
271
271
foreach (ovr; repo.overrides)
272
272
if (ovr.package_ == name.toString() && ovr.source.matches(ver)) {
273
273
Package pack = ovr.target.match! (
274
- (NativePath path) => getOrLoadPackage(path),
274
+ (NativePath path) => getOrLoadPackage(path, NativePath.init, false ,
275
+ StrictMode.Ignore, name),
275
276
(Version vers) => getPackage(name, vers, false ),
276
277
);
277
278
if (pack) return pack;
@@ -377,21 +378,45 @@ class PackageManager {
377
378
Params:
378
379
path = NativePath to the root directory of the package
379
380
recipe_path = Optional path to the recipe file of the package
380
- allow_sub_packages = Also return a sub package if it resides in the given folder
381
+ allow_sub_packages = Also return an already-loaded sub package if it resides in the given folder.
382
+ Ignored when specifying `name`, as a matching already-loaded sub package is always returned in that case.
381
383
mode = Whether to issue errors, warning, or ignore unknown keys in dub.json
384
+ name = Optional (sub-)package name if known in advance
382
385
383
386
Returns: The packages loaded from the given path
384
387
Throws: Throws an exception if no package can be loaded
385
388
*/
386
389
Package getOrLoadPackage (NativePath path, NativePath recipe_path = NativePath.init,
387
- bool allow_sub_packages = false , StrictMode mode = StrictMode.Ignore)
390
+ bool allow_sub_packages = false , StrictMode mode = StrictMode.Ignore,
391
+ PackageName name = PackageName.init)
388
392
{
389
393
path.endsWithSlash = true ;
390
- foreach (p; this .m_internal.fromPath)
391
- if (p.path == path && (! p.parentPackage || (allow_sub_packages && p.parentPackage.path != p.path)))
392
- return p;
394
+ const nameString = name.toString();
395
+
396
+ foreach (p; this .m_internal.fromPath) {
397
+ if (! nameString.empty) {
398
+ if (p.name == nameString && (p.path == path || p.basePackage.path == path))
399
+ return p;
400
+ } else {
401
+ if (p.path == path && (! p.parentPackage || (allow_sub_packages && p.parentPackage.path != p.path)))
402
+ return p;
403
+ }
404
+ }
405
+
393
406
auto pack = this .load(path, recipe_path, null , null , mode);
394
- addPackages(this .m_internal.fromPath, pack);
407
+ auto nameToResolve = PackageName(pack.name);
408
+
409
+ if (! nameString.empty) {
410
+ nameToResolve = PackageName(nameString);
411
+ const loadedName = PackageName(pack.name);
412
+ enforce(loadedName == nameToResolve || loadedName == nameToResolve.main,
413
+ " Package %s loaded from '%s' does not match expected name %s" .format(
414
+ loadedName, path.toNativeString(), nameToResolve));
415
+ }
416
+
417
+ pack = addPackagesAndResolveSubPackage(this .m_internal.fromPath, pack, nameToResolve);
418
+ enforce(pack ! is null , " No sub-package %s in parent package loaded from '%s'" .format(
419
+ nameToResolve, path.toNativeString()));
395
420
return pack;
396
421
}
397
422
0 commit comments