Display valid installable in InstallableDerivedPath::parse warning

The warning message should produce an installable name that can be
passed to `nix build`, `nix path-info`, etc. again. Since the CLI
expects that the .drv path and the output names are separated by
a caret, the warning message must also separate the .drv path and output
names with a caret.

However, `DerivedPath::Built.to_string()` uses an exclamation point as
the separator instead. This commit adds a `separator` argument to the
to_string method.

This changes the warning message from:
If this command is now failing try again with '/nix/store/foo.drv!*'
to:
If this command is now failing try again with '/nix/store/foo.drv^*'
This commit is contained in:
Raphael Robatsch 2023-04-13 16:22:45 +02:00
parent ef0b48377d
commit 9e8f209036
3 changed files with 10 additions and 9 deletions

View file

@ -47,7 +47,7 @@ InstallableDerivedPath InstallableDerivedPath::parse(
}; };
warn( warn(
"The interpretation of store paths arguments ending in `.drv` recently changed. If this command is now failing try again with '%s'", "The interpretation of store paths arguments ending in `.drv` recently changed. If this command is now failing try again with '%s'",
oldDerivedPath.to_string(*store)); oldDerivedPath.to_string(*store, '^'));
}; };
return DerivedPath::Opaque { return DerivedPath::Opaque {
.path = std::move(storePath), .path = std::move(storePath),

View file

@ -59,18 +59,19 @@ std::string DerivedPath::Opaque::to_string(const Store & store) const
return store.printStorePath(path); return store.printStorePath(path);
} }
std::string DerivedPath::Built::to_string(const Store & store) const std::string DerivedPath::Built::to_string(const Store & store, char separator) const
{ {
return store.printStorePath(drvPath) return store.printStorePath(drvPath)
+ "!" + separator
+ outputs.to_string(); + outputs.to_string();
} }
std::string DerivedPath::to_string(const Store & store) const std::string DerivedPath::to_string(const Store & store, char separator) const
{ {
return std::visit( return std::visit(overloaded {
[&](const auto & req) { return req.to_string(store); }, [&](const DerivedPath::Built & req) { return req.to_string(store, separator); },
this->raw()); [&](const DerivedPath::Opaque & req) { return req.to_string(store); },
}, this->raw());
} }

View file

@ -48,7 +48,7 @@ struct DerivedPathBuilt {
StorePath drvPath; StorePath drvPath;
OutputsSpec outputs; OutputsSpec outputs;
std::string to_string(const Store & store) const; std::string to_string(const Store & store, char separator = '!') const;
static DerivedPathBuilt parse(const Store & store, std::string_view, std::string_view); static DerivedPathBuilt parse(const Store & store, std::string_view, std::string_view);
nlohmann::json toJSON(ref<Store> store) const; nlohmann::json toJSON(ref<Store> store) const;
@ -81,7 +81,7 @@ struct DerivedPath : _DerivedPathRaw {
return static_cast<const Raw &>(*this); return static_cast<const Raw &>(*this);
} }
std::string to_string(const Store & store) const; std::string to_string(const Store & store, char separator = '!') const;
static DerivedPath parse(const Store & store, std::string_view); static DerivedPath parse(const Store & store, std::string_view);
}; };