Merge pull request #7667 from dramforever/flake-search-attr

Better error message for nix search when attr is not found
This commit is contained in:
Théophane Hufschmitt 2023-02-01 15:56:22 +01:00 committed by GitHub
commit e32c5c2c77
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 43 deletions

View file

@ -379,10 +379,9 @@ Installable::getCursors(EvalState & state)
ref<eval_cache::AttrCursor> ref<eval_cache::AttrCursor>
Installable::getCursor(EvalState & state) Installable::getCursor(EvalState & state)
{ {
auto cursors = getCursors(state); /* Although getCursors should return at least one element, in case it doesn't,
if (cursors.empty()) bound check to avoid an undefined behavior for vector[0] */
throw Error("cannot find flake attribute '%s'", what()); return getCursors(state).at(0);
return cursors[0];
} }
static StorePath getDeriver( static StorePath getDeriver(
@ -696,46 +695,28 @@ InstallableFlake::getCursors(EvalState & state)
std::vector<ref<eval_cache::AttrCursor>> res; std::vector<ref<eval_cache::AttrCursor>> res;
for (auto & attrPath : getActualAttrPaths()) {
auto attr = root->findAlongAttrPath(parseAttrPath(state, attrPath));
if (attr) res.push_back(ref(*attr));
}
return res;
}
ref<eval_cache::AttrCursor> InstallableFlake::getCursor(EvalState & state)
{
auto lockedFlake = getLockedFlake();
auto cache = openEvalCache(state, lockedFlake);
auto root = cache->getRoot();
Suggestions suggestions; Suggestions suggestions;
auto attrPaths = getActualAttrPaths(); auto attrPaths = getActualAttrPaths();
for (auto & attrPath : attrPaths) { for (auto & attrPath : attrPaths) {
debug("trying flake output attribute '%s'", attrPath); debug("trying flake output attribute '%s'", attrPath);
auto attrOrSuggestions = root->findAlongAttrPath( auto attr = root->findAlongAttrPath(parseAttrPath(state, attrPath));
parseAttrPath(state, attrPath), if (attr) {
true res.push_back(ref(*attr));
); } else {
suggestions += attr.getSuggestions();
if (!attrOrSuggestions) {
suggestions += attrOrSuggestions.getSuggestions();
continue;
} }
return *attrOrSuggestions;
} }
throw Error( if (res.size() == 0)
suggestions, throw Error(
"flake '%s' does not provide attribute %s", suggestions,
flakeRef, "flake '%s' does not provide attribute %s",
showAttrPaths(attrPaths)); flakeRef,
showAttrPaths(attrPaths));
return res;
} }
std::shared_ptr<flake::LockedFlake> InstallableFlake::getLockedFlake() const std::shared_ptr<flake::LockedFlake> InstallableFlake::getLockedFlake() const

View file

@ -103,9 +103,13 @@ struct Installable
return {}; return {};
} }
/* Get a cursor to each value this Installable could refer to. However
if none exists, throw exception instead of returning empty vector. */
virtual std::vector<ref<eval_cache::AttrCursor>> virtual std::vector<ref<eval_cache::AttrCursor>>
getCursors(EvalState & state); getCursors(EvalState & state);
/* Get the first and most preferred cursor this Installable could refer
to, or throw an exception if none exists. */
virtual ref<eval_cache::AttrCursor> virtual ref<eval_cache::AttrCursor>
getCursor(EvalState & state); getCursor(EvalState & state);
@ -193,15 +197,11 @@ struct InstallableFlake : InstallableValue
std::pair<Value *, PosIdx> toValue(EvalState & state) override; std::pair<Value *, PosIdx> toValue(EvalState & state) override;
/* Get a cursor to every attrpath in getActualAttrPaths() that /* Get a cursor to every attrpath in getActualAttrPaths()
exists. */ that exists. However if none exists, throw an exception. */
std::vector<ref<eval_cache::AttrCursor>> std::vector<ref<eval_cache::AttrCursor>>
getCursors(EvalState & state) override; getCursors(EvalState & state) override;
/* Get a cursor to the first attrpath in getActualAttrPaths() that
exists, or throw an exception with suggestions if none exists. */
ref<eval_cache::AttrCursor> getCursor(EvalState & state) override;
std::shared_ptr<flake::LockedFlake> getLockedFlake() const; std::shared_ptr<flake::LockedFlake> getLockedFlake() const;
FlakeRef nixpkgsFlakeRef() const override; FlakeRef nixpkgsFlakeRef() const override;

View file

@ -56,8 +56,8 @@ struct CmdSearch : InstallableCommand, MixJSON
Strings getDefaultFlakeAttrPaths() override Strings getDefaultFlakeAttrPaths() override
{ {
return { return {
"packages." + settings.thisSystem.get() + ".", "packages." + settings.thisSystem.get(),
"legacyPackages." + settings.thisSystem.get() + "." "legacyPackages." + settings.thisSystem.get()
}; };
} }