1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-28 21:21:00 +01:00

Fix issues with std::filesystem::path settings

This commit is contained in:
John Ericson 2025-11-26 18:16:10 -05:00
parent 37cf990b41
commit 1e36f203e6
4 changed files with 34 additions and 12 deletions

View file

@ -433,10 +433,18 @@ std::string BaseSetting<StringMap>::to_string() const
[](const auto & kvpair) { return kvpair.first + "=" + kvpair.second; });
}
static Path parsePath(const AbstractSetting & s, const std::string & str)
{
if (str == "")
throw UsageError("setting '%s' is a path and paths cannot be empty", s.name);
else
return canonPath(str);
}
template<>
std::filesystem::path BaseSetting<std::filesystem::path>::parse(const std::string & str) const
{
return std::filesystem::path(str).lexically_normal();
return parsePath(*this, str);
}
template<>
@ -445,6 +453,22 @@ std::string BaseSetting<std::filesystem::path>::to_string() const
return value.string();
}
template<>
std::optional<std::filesystem::path>
BaseSetting<std::optional<std::filesystem::path>>::parse(const std::string & str) const
{
if (str == "")
return std::nullopt;
else
return parsePath(*this, str);
}
template<>
std::string BaseSetting<std::optional<std::filesystem::path>>::to_string() const
{
return value ? value->string() : "";
}
template class BaseSetting<int>;
template class BaseSetting<unsigned int>;
template class BaseSetting<long>;
@ -458,14 +482,7 @@ template class BaseSetting<StringSet>;
template class BaseSetting<StringMap>;
template class BaseSetting<std::set<ExperimentalFeature>>;
template class BaseSetting<std::filesystem::path>;
static Path parsePath(const AbstractSetting & s, const std::string & str)
{
if (str == "")
throw UsageError("setting '%s' is a path and paths cannot be empty", s.name);
else
return canonPath(str);
}
template class BaseSetting<std::optional<std::filesystem::path>>;
PathSetting::PathSetting(
Config * options,

View file

@ -5,6 +5,7 @@
#include "nix/util/types.hh"
#include "nix/util/os-string.hh"
#include "nix/util/json-non-null.hh"
namespace nix {
@ -53,4 +54,8 @@ std::optional<std::filesystem::path> maybePath(PathView path);
std::filesystem::path pathNG(PathView path);
template<>
struct json_avoids_null<std::filesystem::path> : std::true_type
{};
} // namespace nix

View file

@ -54,7 +54,7 @@ struct LoggerSettings : Config
expression evaluation errors.
)"};
Setting<std::filesystem::path> jsonLogPath{
Setting<std::optional<std::filesystem::path>> jsonLogPath{
this,
{},
"json-log-path",

View file

@ -367,10 +367,10 @@ std::unique_ptr<Logger> makeJSONLogger(const std::filesystem::path & path, bool
void applyJSONLogger()
{
if (!loggerSettings.jsonLogPath.get().empty()) {
if (auto & opt = loggerSettings.jsonLogPath.get()) {
try {
std::vector<std::unique_ptr<Logger>> loggers;
loggers.push_back(makeJSONLogger(loggerSettings.jsonLogPath.get(), false));
loggers.push_back(makeJSONLogger(*opt, false));
try {
logger = makeTeeLogger(std::move(logger), std::move(loggers));
} catch (...) {