diff --git a/source/dub/project.d b/source/dub/project.d index b38c2a6722..c7c097ee32 100644 --- a/source/dub/project.d +++ b/source/dub/project.d @@ -1519,11 +1519,23 @@ unittest assert(expandVars!expandVar("$${DUB_EXE:-dub}") == "${DUB_EXE:-dub}"); } -string expandEnvironmentVariables(string s) +/// Expands the variables in the input string with the same rules as command +/// variables inside custom dub commands. +/// +/// Params: +/// s = the input string where environment variables in form `$VAR` should be replaced +/// throwIfMissing = if true, throw an exception if the given variable is not found, +/// otherwise replace unknown variables with the empty string. +string expandEnvironmentVariables(string s, bool throwIfMissing = true) { import std.process : environment; - return expandVars!(v => environment.get(v))(s); + return expandVars!((v) { + auto ret = environment.get(v); + if (ret is null && throwIfMissing) + throw new Exception("Specified environment variable `$" ~ v ~ "` is not set"); + return ret; + })(s); } // Keep the following list up-to-date if adding more build settings variables.