@@ -501,11 +501,13 @@ int runDubCommandLine(string[] args)
501
501
return 1 ;
502
502
}
503
503
504
- // initialize the root package
505
- Dub dub = cmd.prepareDub(handler.options);
504
+ try {
505
+ // initialize the root package
506
+ Dub dub = cmd.prepareDub(handler.options);
506
507
507
- // execute the command
508
- try return cmd.execute(dub, remaining_args, command_args.appArgs);
508
+ // execute the command
509
+ return cmd.execute(dub, remaining_args, command_args.appArgs);
510
+ }
509
511
catch (UsageException e) {
510
512
// usage exceptions get thrown before any logging, so we are
511
513
// making the errors more narrow to better fit on small screens.
@@ -534,7 +536,7 @@ struct CommonOptions {
534
536
bool verbose, vverbose, quiet, vquiet, verror, version_;
535
537
bool help, annotate, bare;
536
538
string [] registry_urls;
537
- string root_path;
539
+ string root_path, recipeFile ;
538
540
enum Color { automatic, on, off }
539
541
Color colorMode = Color.automatic;
540
542
SkipPackageSuppliers skipRegistry = SkipPackageSuppliers.none;
@@ -568,6 +570,7 @@ struct CommonOptions {
568
570
{
569
571
args.getopt(" h|help" , &help, [" Display general or command specific help" ]);
570
572
args.getopt(" root" , &root_path, [" Path to operate in instead of the current working dir" ]);
573
+ args.getopt(" recipe" , &recipeFile, [" Loads a custom recipe path instead of dub.json/dub.sdl" ]);
571
574
args.getopt(" registry" , ®istry_urls, [
572
575
" Search the given registry URL first when resolving dependencies. Can be specified multiple times. Available registry types:" ,
573
576
" DUB: URL to DUB registry (default)" ,
@@ -836,11 +839,25 @@ class Command {
836
839
dub = new Dub(options.root_path, package_suppliers, options.skipRegistry);
837
840
dub.dryRun = options.annotate;
838
841
dub.defaultPlacementLocation = options.placementLocation;
839
-
842
+ dub.mainRecipePath = options.recipeFile;
840
843
// make the CWD package available so that for example sub packages can reference their
841
844
// parent package.
842
- try dub.packageManager.getOrLoadPackage(NativePath(options.root_path), NativePath.init, false , StrictMode.Warn);
843
- catch (Exception e) { logDiagnostic(" No valid package found in current working directory: %s" , e.msg); }
845
+ try dub.packageManager.getOrLoadPackage(NativePath(options.root_path), NativePath(options.recipeFile), false , StrictMode.Warn);
846
+ catch (Exception e) {
847
+ // by default we ignore CWD package load fails in prepareDUB, since
848
+ // they will fail again later when they are actually requested. This
849
+ // is done to provide custom options to the loading logic and should
850
+ // ideally be moved elsewhere. (This catch has been around since 10
851
+ // years when it was first introduced in _app.d_)
852
+ logDiagnostic(" No valid package found in current working directory: %s" , e.msg);
853
+
854
+ // for now, we work around not knowing if the package is needed or
855
+ // not, simply by trusting the user to only use `--recipe` when the
856
+ // recipe file actually exists, otherwise we throw the error.
857
+ bool loadMustSucceed = options.recipeFile.length > 0 ;
858
+ if (loadMustSucceed)
859
+ throw e;
860
+ }
844
861
845
862
return dub;
846
863
}
@@ -1168,6 +1185,7 @@ abstract class PackageBuildCommand : Command {
1168
1185
return true ;
1169
1186
}
1170
1187
1188
+
1171
1189
bool from_cwd = package_name.length == 0 || package_name.startsWith(" :" );
1172
1190
// load package in root_path to enable searching for sub packages
1173
1191
if (loadCwdPackage(dub, from_cwd)) {
@@ -1272,6 +1290,7 @@ class GenerateCommand : PackageBuildCommand {
1272
1290
if (! gensettings.config.length)
1273
1291
gensettings.config = m_defaultConfig;
1274
1292
gensettings.runArgs = app_args;
1293
+ gensettings.recipeName = dub.mainRecipePath;
1275
1294
// legacy compatibility, default working directory is always CWD
1276
1295
gensettings.overrideToolWorkingDirectory = getWorkingDirectory();
1277
1296
0 commit comments