2023-10-25 07:43:36 +03:00
|
|
|
#include "users.hh"
|
2019-06-07 23:25:48 +03:00
|
|
|
#include "eval-cache.hh"
|
|
|
|
#include "sqlite.hh"
|
2019-06-07 23:38:39 +03:00
|
|
|
#include "eval.hh"
|
2020-04-20 14:13:52 +03:00
|
|
|
#include "eval-inline.hh"
|
2020-06-29 17:39:41 +03:00
|
|
|
#include "store-api.hh"
|
2024-08-05 19:34:05 +03:00
|
|
|
// Need specialization involving `SymbolStr` just in this one module.
|
|
|
|
#include "strings-inline.hh"
|
2019-06-07 23:25:48 +03:00
|
|
|
|
2020-04-20 14:13:52 +03:00
|
|
|
namespace nix::eval_cache {
|
2019-06-07 23:25:48 +03:00
|
|
|
|
2024-04-19 20:34:07 +03:00
|
|
|
CachedEvalError::CachedEvalError(ref<AttrCursor> cursor, Symbol attr)
|
|
|
|
: EvalError(cursor->root->state, "cached failure of attribute '%s'", cursor->getAttrPathStr(attr))
|
|
|
|
, cursor(cursor), attr(attr)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
void CachedEvalError::force()
|
|
|
|
{
|
|
|
|
auto & v = cursor->forceValue();
|
|
|
|
|
|
|
|
if (v.type() == nAttrs) {
|
|
|
|
auto a = v.attrs()->get(this->attr);
|
|
|
|
|
|
|
|
state.forceValue(*a->value, a->pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Shouldn't happen.
|
|
|
|
throw EvalError(state, "evaluation of cached failed attribute '%s' unexpectedly succeeded", cursor->getAttrPathStr(attr));
|
|
|
|
}
|
|
|
|
|
2019-06-07 23:25:48 +03:00
|
|
|
static const char * schema = R"sql(
|
|
|
|
create table if not exists Attributes (
|
2020-04-20 14:13:52 +03:00
|
|
|
parent integer not null,
|
|
|
|
name text,
|
|
|
|
type integer not null,
|
2019-06-07 23:25:48 +03:00
|
|
|
value text,
|
2020-06-29 20:08:37 +03:00
|
|
|
context text,
|
2020-04-20 14:13:52 +03:00
|
|
|
primary key (parent, name)
|
2019-06-07 23:25:48 +03:00
|
|
|
);
|
|
|
|
)sql";
|
|
|
|
|
2020-04-20 14:13:52 +03:00
|
|
|
struct AttrDb
|
2019-06-07 23:25:48 +03:00
|
|
|
{
|
2020-07-14 16:17:38 +03:00
|
|
|
std::atomic_bool failed{false};
|
|
|
|
|
2022-03-18 17:35:45 +02:00
|
|
|
const StoreDirConfig & cfg;
|
2022-03-12 02:28:00 +02:00
|
|
|
|
2020-04-20 14:13:52 +03:00
|
|
|
struct State
|
|
|
|
{
|
|
|
|
SQLite db;
|
|
|
|
SQLiteStmt insertAttribute;
|
2020-06-29 20:08:37 +03:00
|
|
|
SQLiteStmt insertAttributeWithContext;
|
2020-04-20 14:13:52 +03:00
|
|
|
SQLiteStmt queryAttribute;
|
|
|
|
SQLiteStmt queryAttributes;
|
|
|
|
std::unique_ptr<SQLiteTxn> txn;
|
|
|
|
};
|
|
|
|
|
|
|
|
std::unique_ptr<Sync<State>> _state;
|
|
|
|
|
2022-04-26 15:16:20 +03:00
|
|
|
SymbolTable & symbols;
|
|
|
|
|
|
|
|
AttrDb(
|
2022-03-18 17:35:45 +02:00
|
|
|
const StoreDirConfig & cfg,
|
2022-04-26 15:16:20 +03:00
|
|
|
const Hash & fingerprint,
|
|
|
|
SymbolTable & symbols)
|
2022-03-12 02:28:00 +02:00
|
|
|
: cfg(cfg)
|
|
|
|
, _state(std::make_unique<Sync<State>>())
|
2022-04-26 15:16:20 +03:00
|
|
|
, symbols(symbols)
|
2020-04-20 14:13:52 +03:00
|
|
|
{
|
|
|
|
auto state(_state->lock());
|
|
|
|
|
Use `std::set<StringContextElem>` not `PathSet` for string contexts
Motivation
`PathSet` is not correct because string contexts have other forms
(`Built` and `DrvDeep`) that are not rendered as plain store paths.
Instead of wrongly using `PathSet`, or "stringly typed" using
`StringSet`, use `std::std<StringContextElem>`.
-----
In support of this change, `NixStringContext` is now defined as
`std::std<StringContextElem>` not `std:vector<StringContextElem>`. The
old definition was just used by a `getContext` method which was only
used by the eval cache. It can be deleted altogether since the types are
now unified and the preexisting `copyContext` function already suffices.
Summarizing the previous paragraph:
Old:
- `value/context.hh`: `NixStringContext = std::vector<StringContextElem>`
- `value.hh`: `NixStringContext Value::getContext(...)`
- `value.hh`: `copyContext(...)`
New:
- `value/context.hh`: `NixStringContext = std::set<StringContextElem>`
- `value.hh`: `copyContext(...)`
----
The string representation of string context elements no longer contains
the store dir. The diff of `src/libexpr/tests/value/context.cc` should
make clear what the new representation is, so we recommend reviewing
that file first. This was done for two reasons:
Less API churn:
`Value::mkString` and friends did not take a `Store` before. But if
`NixStringContextElem::{parse, to_string}` *do* take a store (as they
did before), then we cannot have the `Value` functions use them (in
order to work with the fully-structured `NixStringContext`) without
adding that argument.
That would have been a lot of churn of threading the store, and this
diff is already large enough, so the easier and less invasive thing to
do was simply make the element `parse` and `to_string` functions not
take the `Store` reference, and the easiest way to do that was to simply
drop the store dir.
Space usage:
Dropping the `/nix/store/` (or similar) from the internal representation
will safe space in the heap of the Nix programming being interpreted. If
the heap contains many strings with non-trivial contexts, the saving
could add up to something significant.
----
The eval cache version is bumped.
The eval cache serialization uses `NixStringContextElem::{parse,
to_string}`, and since those functions are changed per the above, that
means the on-disk representation is also changed.
This is simply done by changing the name of the used for the eval cache
from `eval-cache-v4` to eval-cache-v5`.
----
To avoid some duplication `EvalCache::mkPathString` is added to abstract
over the simple case of turning a store path to a string with just that
string in the context.
Context
This PR picks up where #7543 left off. That one introduced the fully
structured `NixStringContextElem` data type, but kept `PathSet context`
as an awkward middle ground between internal `char[][]` interpreter heap
string contexts and `NixStringContext` fully parsed string contexts.
The infelicity of `PathSet context` was specifically called out during
Nix team group review, but it was agreeing that fixing it could be left
as future work. This is that future work.
A possible follow-up step would be to get rid of the `char[][]`
evaluator heap representation, too, but it is not yet clear how to do
that. To use `NixStringContextElem` there we would need to get the STL
containers to GC pointers in the GC build, and I am not sure how to do
that.
----
PR #7543 effectively is writing the inverse of a `mkPathString`,
`mkOutputString`, and one more such function for the `DrvDeep` case. I
would like that PR to have property tests ensuring it is actually the
inverse as expected.
This PR sets things up nicely so that reworking that PR to be in that
more elegant and better tested way is possible.
Co-authored-by: Théophane Hufschmitt <7226587+thufschmitt@users.noreply.github.com>
2023-01-29 03:31:10 +02:00
|
|
|
Path cacheDir = getCacheDir() + "/nix/eval-cache-v5";
|
2020-04-20 14:13:52 +03:00
|
|
|
createDirs(cacheDir);
|
|
|
|
|
2023-10-13 04:48:15 +03:00
|
|
|
Path dbPath = cacheDir + "/" + fingerprint.to_string(HashFormat::Base16, false) + ".sqlite";
|
2020-04-20 14:13:52 +03:00
|
|
|
|
|
|
|
state->db = SQLite(dbPath);
|
|
|
|
state->db.isCache();
|
|
|
|
state->db.exec(schema);
|
|
|
|
|
|
|
|
state->insertAttribute.create(state->db,
|
|
|
|
"insert or replace into Attributes(parent, name, type, value) values (?, ?, ?, ?)");
|
|
|
|
|
2020-06-29 20:08:37 +03:00
|
|
|
state->insertAttributeWithContext.create(state->db,
|
|
|
|
"insert or replace into Attributes(parent, name, type, value, context) values (?, ?, ?, ?, ?)");
|
|
|
|
|
2020-04-20 14:13:52 +03:00
|
|
|
state->queryAttribute.create(state->db,
|
2020-06-29 20:08:37 +03:00
|
|
|
"select rowid, type, value, context from Attributes where parent = ? and name = ?");
|
2020-04-20 14:13:52 +03:00
|
|
|
|
|
|
|
state->queryAttributes.create(state->db,
|
|
|
|
"select name from Attributes where parent = ?");
|
|
|
|
|
|
|
|
state->txn = std::make_unique<SQLiteTxn>(state->db);
|
|
|
|
}
|
|
|
|
|
|
|
|
~AttrDb()
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
auto state(_state->lock());
|
2024-07-12 19:45:35 +03:00
|
|
|
if (!failed && state->txn->active)
|
2020-07-14 16:17:38 +03:00
|
|
|
state->txn->commit();
|
2020-04-20 14:13:52 +03:00
|
|
|
state->txn.reset();
|
|
|
|
} catch (...) {
|
|
|
|
ignoreException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-14 16:17:38 +03:00
|
|
|
template<typename F>
|
|
|
|
AttrId doSQLite(F && fun)
|
|
|
|
{
|
|
|
|
if (failed) return 0;
|
|
|
|
try {
|
|
|
|
return fun();
|
|
|
|
} catch (SQLiteError &) {
|
|
|
|
ignoreException();
|
|
|
|
failed = true;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-20 14:13:52 +03:00
|
|
|
AttrId setAttrs(
|
|
|
|
AttrKey key,
|
|
|
|
const std::vector<Symbol> & attrs)
|
|
|
|
{
|
2020-07-14 16:17:38 +03:00
|
|
|
return doSQLite([&]()
|
|
|
|
{
|
|
|
|
auto state(_state->lock());
|
2020-04-20 14:13:52 +03:00
|
|
|
|
|
|
|
state->insertAttribute.use()
|
2020-07-14 16:17:38 +03:00
|
|
|
(key.first)
|
2022-04-26 15:16:20 +03:00
|
|
|
(symbols[key.second])
|
2020-07-14 16:17:38 +03:00
|
|
|
(AttrType::FullAttrs)
|
2020-04-20 14:13:52 +03:00
|
|
|
(0, false).exec();
|
|
|
|
|
2020-07-14 16:17:38 +03:00
|
|
|
AttrId rowId = state->db.getLastInsertedRowId();
|
|
|
|
assert(rowId);
|
|
|
|
|
|
|
|
for (auto & attr : attrs)
|
|
|
|
state->insertAttribute.use()
|
|
|
|
(rowId)
|
2022-04-22 22:45:39 +03:00
|
|
|
(symbols[attr])
|
2020-07-14 16:17:38 +03:00
|
|
|
(AttrType::Placeholder)
|
|
|
|
(0, false).exec();
|
|
|
|
|
|
|
|
return rowId;
|
|
|
|
});
|
2020-04-20 14:13:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
AttrId setString(
|
|
|
|
AttrKey key,
|
2020-06-29 20:08:37 +03:00
|
|
|
std::string_view s,
|
|
|
|
const char * * context = nullptr)
|
2020-04-20 14:13:52 +03:00
|
|
|
{
|
2020-07-14 16:17:38 +03:00
|
|
|
return doSQLite([&]()
|
|
|
|
{
|
|
|
|
auto state(_state->lock());
|
2020-04-20 14:13:52 +03:00
|
|
|
|
2020-07-14 16:17:38 +03:00
|
|
|
if (context) {
|
|
|
|
std::string ctx;
|
|
|
|
for (const char * * p = context; *p; ++p) {
|
|
|
|
if (p != context) ctx.push_back(' ');
|
|
|
|
ctx.append(*p);
|
|
|
|
}
|
|
|
|
state->insertAttributeWithContext.use()
|
|
|
|
(key.first)
|
2022-04-26 15:16:20 +03:00
|
|
|
(symbols[key.second])
|
2020-07-14 16:17:38 +03:00
|
|
|
(AttrType::String)
|
|
|
|
(s)
|
|
|
|
(ctx).exec();
|
|
|
|
} else {
|
|
|
|
state->insertAttribute.use()
|
|
|
|
(key.first)
|
2022-04-26 15:16:20 +03:00
|
|
|
(symbols[key.second])
|
2020-07-14 16:17:38 +03:00
|
|
|
(AttrType::String)
|
2020-06-29 20:08:37 +03:00
|
|
|
(s).exec();
|
2020-07-14 16:17:38 +03:00
|
|
|
}
|
2020-04-20 14:13:52 +03:00
|
|
|
|
2020-07-14 16:17:38 +03:00
|
|
|
return state->db.getLastInsertedRowId();
|
|
|
|
});
|
2020-04-20 14:13:52 +03:00
|
|
|
}
|
|
|
|
|
2020-04-27 17:29:26 +03:00
|
|
|
AttrId setBool(
|
|
|
|
AttrKey key,
|
|
|
|
bool b)
|
|
|
|
{
|
2020-07-14 16:17:38 +03:00
|
|
|
return doSQLite([&]()
|
|
|
|
{
|
|
|
|
auto state(_state->lock());
|
2020-04-27 17:29:26 +03:00
|
|
|
|
2020-07-14 16:17:38 +03:00
|
|
|
state->insertAttribute.use()
|
|
|
|
(key.first)
|
2022-04-26 15:16:20 +03:00
|
|
|
(symbols[key.second])
|
2020-07-14 16:17:38 +03:00
|
|
|
(AttrType::Bool)
|
|
|
|
(b ? 1 : 0).exec();
|
2020-04-27 17:29:26 +03:00
|
|
|
|
2020-07-14 16:17:38 +03:00
|
|
|
return state->db.getLastInsertedRowId();
|
|
|
|
});
|
2020-04-27 17:29:26 +03:00
|
|
|
}
|
|
|
|
|
2022-05-16 03:28:21 +03:00
|
|
|
AttrId setInt(
|
|
|
|
AttrKey key,
|
|
|
|
int n)
|
|
|
|
{
|
|
|
|
return doSQLite([&]()
|
|
|
|
{
|
|
|
|
auto state(_state->lock());
|
|
|
|
|
|
|
|
state->insertAttribute.use()
|
|
|
|
(key.first)
|
|
|
|
(symbols[key.second])
|
|
|
|
(AttrType::Int)
|
|
|
|
(n).exec();
|
|
|
|
|
|
|
|
return state->db.getLastInsertedRowId();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-04-20 17:39:47 +03:00
|
|
|
AttrId setListOfStrings(
|
|
|
|
AttrKey key,
|
|
|
|
const std::vector<std::string> & l)
|
|
|
|
{
|
|
|
|
return doSQLite([&]()
|
|
|
|
{
|
|
|
|
auto state(_state->lock());
|
|
|
|
|
|
|
|
state->insertAttribute.use()
|
|
|
|
(key.first)
|
|
|
|
(symbols[key.second])
|
|
|
|
(AttrType::ListOfStrings)
|
2024-07-12 23:09:27 +03:00
|
|
|
(dropEmptyInitThenConcatStringsSep("\t", l)).exec();
|
2022-04-20 17:39:47 +03:00
|
|
|
|
|
|
|
return state->db.getLastInsertedRowId();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-04-20 14:13:52 +03:00
|
|
|
AttrId setPlaceholder(AttrKey key)
|
|
|
|
{
|
2020-07-14 16:17:38 +03:00
|
|
|
return doSQLite([&]()
|
|
|
|
{
|
|
|
|
auto state(_state->lock());
|
2020-04-20 14:13:52 +03:00
|
|
|
|
2020-07-14 16:17:38 +03:00
|
|
|
state->insertAttribute.use()
|
|
|
|
(key.first)
|
2022-04-26 15:16:20 +03:00
|
|
|
(symbols[key.second])
|
2020-07-14 16:17:38 +03:00
|
|
|
(AttrType::Placeholder)
|
|
|
|
(0, false).exec();
|
2020-04-20 14:13:52 +03:00
|
|
|
|
2020-07-14 16:17:38 +03:00
|
|
|
return state->db.getLastInsertedRowId();
|
|
|
|
});
|
2020-04-20 14:13:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
AttrId setMissing(AttrKey key)
|
|
|
|
{
|
2020-07-14 16:17:38 +03:00
|
|
|
return doSQLite([&]()
|
|
|
|
{
|
|
|
|
auto state(_state->lock());
|
2020-04-20 14:13:52 +03:00
|
|
|
|
2020-07-14 16:17:38 +03:00
|
|
|
state->insertAttribute.use()
|
|
|
|
(key.first)
|
2022-04-26 15:16:20 +03:00
|
|
|
(symbols[key.second])
|
2020-07-14 16:17:38 +03:00
|
|
|
(AttrType::Missing)
|
|
|
|
(0, false).exec();
|
2020-04-20 14:13:52 +03:00
|
|
|
|
2020-07-14 16:17:38 +03:00
|
|
|
return state->db.getLastInsertedRowId();
|
|
|
|
});
|
2020-04-20 14:13:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
AttrId setMisc(AttrKey key)
|
|
|
|
{
|
2020-07-14 16:17:38 +03:00
|
|
|
return doSQLite([&]()
|
|
|
|
{
|
|
|
|
auto state(_state->lock());
|
2020-04-20 14:13:52 +03:00
|
|
|
|
2020-07-14 16:17:38 +03:00
|
|
|
state->insertAttribute.use()
|
|
|
|
(key.first)
|
2022-04-26 15:16:20 +03:00
|
|
|
(symbols[key.second])
|
2020-07-14 16:17:38 +03:00
|
|
|
(AttrType::Misc)
|
|
|
|
(0, false).exec();
|
2020-04-20 14:13:52 +03:00
|
|
|
|
2020-07-14 16:17:38 +03:00
|
|
|
return state->db.getLastInsertedRowId();
|
|
|
|
});
|
2020-04-20 14:13:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
AttrId setFailed(AttrKey key)
|
|
|
|
{
|
2020-07-14 16:17:38 +03:00
|
|
|
return doSQLite([&]()
|
|
|
|
{
|
|
|
|
auto state(_state->lock());
|
2020-04-20 14:13:52 +03:00
|
|
|
|
2020-07-14 16:17:38 +03:00
|
|
|
state->insertAttribute.use()
|
|
|
|
(key.first)
|
2022-04-26 15:16:20 +03:00
|
|
|
(symbols[key.second])
|
2020-07-14 16:17:38 +03:00
|
|
|
(AttrType::Failed)
|
|
|
|
(0, false).exec();
|
2020-04-20 14:13:52 +03:00
|
|
|
|
2020-07-14 16:17:38 +03:00
|
|
|
return state->db.getLastInsertedRowId();
|
|
|
|
});
|
2020-04-20 14:13:52 +03:00
|
|
|
}
|
|
|
|
|
2022-04-26 15:16:20 +03:00
|
|
|
std::optional<std::pair<AttrId, AttrValue>> getAttr(AttrKey key)
|
2020-04-20 14:13:52 +03:00
|
|
|
{
|
|
|
|
auto state(_state->lock());
|
|
|
|
|
2022-04-26 15:16:20 +03:00
|
|
|
auto queryAttribute(state->queryAttribute.use()(key.first)(symbols[key.second]));
|
2020-04-20 14:13:52 +03:00
|
|
|
if (!queryAttribute.next()) return {};
|
|
|
|
|
2022-06-23 22:11:08 +03:00
|
|
|
auto rowId = (AttrId) queryAttribute.getInt(0);
|
2020-04-20 14:13:52 +03:00
|
|
|
auto type = (AttrType) queryAttribute.getInt(1);
|
|
|
|
|
2020-04-27 17:29:26 +03:00
|
|
|
switch (type) {
|
|
|
|
case AttrType::Placeholder:
|
|
|
|
return {{rowId, placeholder_t()}};
|
|
|
|
case AttrType::FullAttrs: {
|
|
|
|
// FIXME: expensive, should separate this out.
|
|
|
|
std::vector<Symbol> attrs;
|
|
|
|
auto queryAttributes(state->queryAttributes.use()(rowId));
|
|
|
|
while (queryAttributes.next())
|
2022-04-22 22:45:39 +03:00
|
|
|
attrs.emplace_back(symbols.create(queryAttributes.getStr(0)));
|
2020-04-27 17:29:26 +03:00
|
|
|
return {{rowId, attrs}};
|
|
|
|
}
|
2020-06-29 20:08:37 +03:00
|
|
|
case AttrType::String: {
|
2021-03-10 04:20:32 +02:00
|
|
|
NixStringContext context;
|
2020-06-29 20:08:37 +03:00
|
|
|
if (!queryAttribute.isNull(3))
|
|
|
|
for (auto & s : tokenizeString<std::vector<std::string>>(queryAttribute.getStr(3), ";"))
|
Use `std::set<StringContextElem>` not `PathSet` for string contexts
Motivation
`PathSet` is not correct because string contexts have other forms
(`Built` and `DrvDeep`) that are not rendered as plain store paths.
Instead of wrongly using `PathSet`, or "stringly typed" using
`StringSet`, use `std::std<StringContextElem>`.
-----
In support of this change, `NixStringContext` is now defined as
`std::std<StringContextElem>` not `std:vector<StringContextElem>`. The
old definition was just used by a `getContext` method which was only
used by the eval cache. It can be deleted altogether since the types are
now unified and the preexisting `copyContext` function already suffices.
Summarizing the previous paragraph:
Old:
- `value/context.hh`: `NixStringContext = std::vector<StringContextElem>`
- `value.hh`: `NixStringContext Value::getContext(...)`
- `value.hh`: `copyContext(...)`
New:
- `value/context.hh`: `NixStringContext = std::set<StringContextElem>`
- `value.hh`: `copyContext(...)`
----
The string representation of string context elements no longer contains
the store dir. The diff of `src/libexpr/tests/value/context.cc` should
make clear what the new representation is, so we recommend reviewing
that file first. This was done for two reasons:
Less API churn:
`Value::mkString` and friends did not take a `Store` before. But if
`NixStringContextElem::{parse, to_string}` *do* take a store (as they
did before), then we cannot have the `Value` functions use them (in
order to work with the fully-structured `NixStringContext`) without
adding that argument.
That would have been a lot of churn of threading the store, and this
diff is already large enough, so the easier and less invasive thing to
do was simply make the element `parse` and `to_string` functions not
take the `Store` reference, and the easiest way to do that was to simply
drop the store dir.
Space usage:
Dropping the `/nix/store/` (or similar) from the internal representation
will safe space in the heap of the Nix programming being interpreted. If
the heap contains many strings with non-trivial contexts, the saving
could add up to something significant.
----
The eval cache version is bumped.
The eval cache serialization uses `NixStringContextElem::{parse,
to_string}`, and since those functions are changed per the above, that
means the on-disk representation is also changed.
This is simply done by changing the name of the used for the eval cache
from `eval-cache-v4` to eval-cache-v5`.
----
To avoid some duplication `EvalCache::mkPathString` is added to abstract
over the simple case of turning a store path to a string with just that
string in the context.
Context
This PR picks up where #7543 left off. That one introduced the fully
structured `NixStringContextElem` data type, but kept `PathSet context`
as an awkward middle ground between internal `char[][]` interpreter heap
string contexts and `NixStringContext` fully parsed string contexts.
The infelicity of `PathSet context` was specifically called out during
Nix team group review, but it was agreeing that fixing it could be left
as future work. This is that future work.
A possible follow-up step would be to get rid of the `char[][]`
evaluator heap representation, too, but it is not yet clear how to do
that. To use `NixStringContextElem` there we would need to get the STL
containers to GC pointers in the GC build, and I am not sure how to do
that.
----
PR #7543 effectively is writing the inverse of a `mkPathString`,
`mkOutputString`, and one more such function for the `DrvDeep` case. I
would like that PR to have property tests ensuring it is actually the
inverse as expected.
This PR sets things up nicely so that reworking that PR to be in that
more elegant and better tested way is possible.
Co-authored-by: Théophane Hufschmitt <7226587+thufschmitt@users.noreply.github.com>
2023-01-29 03:31:10 +02:00
|
|
|
context.insert(NixStringContextElem::parse(s));
|
2020-06-29 20:08:37 +03:00
|
|
|
return {{rowId, string_t{queryAttribute.getStr(2), context}}};
|
|
|
|
}
|
2020-04-27 17:29:26 +03:00
|
|
|
case AttrType::Bool:
|
|
|
|
return {{rowId, queryAttribute.getInt(2) != 0}};
|
2022-05-16 03:28:21 +03:00
|
|
|
case AttrType::Int:
|
language: cleanly ban integer overflows
This also bans various sneaking of negative numbers from the language
into unsuspecting builtins as was exposed while auditing the
consequences of changing the Nix language integer type to a newtype.
It's unlikely that this change comprehensively ensures correctness when
passing integers out of the Nix language and we should probably add a
checked-narrowing function or something similar, but that's out of scope
for the immediate change.
During the development of this I found a few fun facts about the
language:
- You could overflow integers by converting from unsigned JSON values.
- You could overflow unsigned integers by converting negative numbers
into them when going into Nix config, into fetchTree, and into flake
inputs.
The flake inputs and Nix config cannot actually be tested properly
since they both ban thunks, however, we put in checks anyway because
it's possible these could somehow be used to do such shenanigans some
other way.
Note that Lix has banned Nix language integer overflows since the very
first public beta, but threw a SIGILL about them because we run with
-fsanitize=signed-overflow -fsanitize-undefined-trap-on-error in
production builds. Since the Nix language uses signed integers, overflow
was simply undefined behaviour, and since we defined that to trap, it
did.
Trapping on it was a bad UX, but we didn't even entirely notice
that we had done this at all until it was reported as a bug a couple of
months later (which is, to be fair, that flag working as intended), and
it's got enough production time that, aside from code that is IMHO buggy
(and which is, in any case, not in nixpkgs) such as
https://git.lix.systems/lix-project/lix/issues/445, we don't think
anyone doing anything reasonable actually depends on wrapping overflow.
Even for weird use cases such as doing funny bit crimes, it doesn't make
sense IMO to have wrapping behaviour, since two's complement arithmetic
overflow behaviour is so *aggressively* not what you want for *any* kind
of mathematics/algorithms. The Nix language exists for package
management, a domain where bit crimes are already only dubiously in
scope to begin with, and it makes a lot more sense for that domain for
the integers to never lose precision, either by throwing errors if they
would, or by being arbitrary-precision.
Fixes: https://github.com/NixOS/nix/issues/10968
Original-CL: https://gerrit.lix.systems/c/lix/+/1596
Change-Id: I51f253840c4af2ea5422b8a420aa5fafbf8fae75
2024-07-12 17:22:34 +03:00
|
|
|
return {{rowId, int_t{NixInt{queryAttribute.getInt(2)}}}};
|
2022-04-20 17:39:47 +03:00
|
|
|
case AttrType::ListOfStrings:
|
|
|
|
return {{rowId, tokenizeString<std::vector<std::string>>(queryAttribute.getStr(2), "\t")}};
|
2020-04-27 17:29:26 +03:00
|
|
|
case AttrType::Missing:
|
|
|
|
return {{rowId, missing_t()}};
|
|
|
|
case AttrType::Misc:
|
|
|
|
return {{rowId, misc_t()}};
|
|
|
|
case AttrType::Failed:
|
|
|
|
return {{rowId, failed_t()}};
|
|
|
|
default:
|
|
|
|
throw Error("unexpected type in evaluation cache");
|
|
|
|
}
|
2020-04-20 14:13:52 +03:00
|
|
|
}
|
2019-06-07 23:25:48 +03:00
|
|
|
};
|
|
|
|
|
2022-04-26 15:16:20 +03:00
|
|
|
static std::shared_ptr<AttrDb> makeAttrDb(
|
2022-03-18 17:35:45 +02:00
|
|
|
const StoreDirConfig & cfg,
|
2022-04-26 15:16:20 +03:00
|
|
|
const Hash & fingerprint,
|
|
|
|
SymbolTable & symbols)
|
2020-07-14 16:17:38 +03:00
|
|
|
{
|
|
|
|
try {
|
2022-04-26 15:16:20 +03:00
|
|
|
return std::make_shared<AttrDb>(cfg, fingerprint, symbols);
|
2020-07-14 16:17:38 +03:00
|
|
|
} catch (SQLiteError &) {
|
|
|
|
ignoreException();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-20 14:13:52 +03:00
|
|
|
EvalCache::EvalCache(
|
2020-07-16 17:58:53 +03:00
|
|
|
std::optional<std::reference_wrapper<const Hash>> useCache,
|
2020-04-20 14:13:52 +03:00
|
|
|
EvalState & state,
|
|
|
|
RootLoader rootLoader)
|
2022-04-26 15:16:20 +03:00
|
|
|
: db(useCache ? makeAttrDb(*state.store, *useCache, state.symbols) : nullptr)
|
2020-04-20 14:13:52 +03:00
|
|
|
, state(state)
|
|
|
|
, rootLoader(rootLoader)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Value * EvalCache::getRootValue()
|
|
|
|
{
|
|
|
|
if (!value) {
|
|
|
|
debug("getting root value");
|
|
|
|
value = allocRootValue(rootLoader());
|
|
|
|
}
|
|
|
|
return *value;
|
|
|
|
}
|
|
|
|
|
2022-04-14 15:04:19 +03:00
|
|
|
ref<AttrCursor> EvalCache::getRoot()
|
2019-06-07 23:25:48 +03:00
|
|
|
{
|
2022-04-14 15:04:19 +03:00
|
|
|
return make_ref<AttrCursor>(ref(shared_from_this()), std::nullopt);
|
2020-04-20 14:13:52 +03:00
|
|
|
}
|
2019-06-07 23:25:48 +03:00
|
|
|
|
2020-04-20 14:13:52 +03:00
|
|
|
AttrCursor::AttrCursor(
|
|
|
|
ref<EvalCache> root,
|
|
|
|
Parent parent,
|
|
|
|
Value * value,
|
|
|
|
std::optional<std::pair<AttrId, AttrValue>> && cachedValue)
|
|
|
|
: root(root), parent(parent), cachedValue(std::move(cachedValue))
|
|
|
|
{
|
|
|
|
if (value)
|
|
|
|
_value = allocRootValue(value);
|
|
|
|
}
|
2019-06-07 23:25:48 +03:00
|
|
|
|
2020-04-20 14:13:52 +03:00
|
|
|
AttrKey AttrCursor::getKey()
|
|
|
|
{
|
|
|
|
if (!parent)
|
|
|
|
return {0, root->state.sEpsilon};
|
|
|
|
if (!parent->first->cachedValue) {
|
2022-04-26 15:16:20 +03:00
|
|
|
parent->first->cachedValue = root->db->getAttr(parent->first->getKey());
|
2020-04-20 14:13:52 +03:00
|
|
|
assert(parent->first->cachedValue);
|
|
|
|
}
|
|
|
|
return {parent->first->cachedValue->first, parent->second};
|
|
|
|
}
|
2019-06-07 23:25:48 +03:00
|
|
|
|
2020-04-20 14:13:52 +03:00
|
|
|
Value & AttrCursor::getValue()
|
|
|
|
{
|
|
|
|
if (!_value) {
|
|
|
|
if (parent) {
|
|
|
|
auto & vParent = parent->first->getValue();
|
2023-01-19 14:23:04 +02:00
|
|
|
root->state.forceAttrs(vParent, noPos, "while searching for an attribute");
|
2024-03-25 19:20:18 +02:00
|
|
|
auto attr = vParent.attrs()->get(parent->second);
|
2020-04-20 14:13:52 +03:00
|
|
|
if (!attr)
|
|
|
|
throw Error("attribute '%s' is unexpectedly missing", getAttrPathStr());
|
|
|
|
_value = allocRootValue(attr->value);
|
|
|
|
} else
|
|
|
|
_value = allocRootValue(root->getRootValue());
|
|
|
|
}
|
|
|
|
return **_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<Symbol> AttrCursor::getAttrPath() const
|
|
|
|
{
|
|
|
|
if (parent) {
|
|
|
|
auto attrPath = parent->first->getAttrPath();
|
|
|
|
attrPath.push_back(parent->second);
|
|
|
|
return attrPath;
|
|
|
|
} else
|
|
|
|
return {};
|
|
|
|
}
|
2019-06-07 23:25:48 +03:00
|
|
|
|
2020-04-20 14:13:52 +03:00
|
|
|
std::vector<Symbol> AttrCursor::getAttrPath(Symbol name) const
|
|
|
|
{
|
|
|
|
auto attrPath = getAttrPath();
|
|
|
|
attrPath.push_back(name);
|
|
|
|
return attrPath;
|
|
|
|
}
|
2019-06-07 23:25:48 +03:00
|
|
|
|
2020-04-20 14:13:52 +03:00
|
|
|
std::string AttrCursor::getAttrPathStr() const
|
|
|
|
{
|
2024-07-12 23:09:27 +03:00
|
|
|
return dropEmptyInitThenConcatStringsSep(".", root->state.symbols.resolve(getAttrPath()));
|
2019-06-07 23:25:48 +03:00
|
|
|
}
|
|
|
|
|
2020-04-20 14:13:52 +03:00
|
|
|
std::string AttrCursor::getAttrPathStr(Symbol name) const
|
|
|
|
{
|
2024-07-12 23:09:27 +03:00
|
|
|
return dropEmptyInitThenConcatStringsSep(".", root->state.symbols.resolve(getAttrPath(name)));
|
2020-04-20 14:13:52 +03:00
|
|
|
}
|
2019-06-07 23:25:48 +03:00
|
|
|
|
2020-04-20 14:13:52 +03:00
|
|
|
Value & AttrCursor::forceValue()
|
2019-06-07 23:25:48 +03:00
|
|
|
{
|
2022-04-20 17:39:47 +03:00
|
|
|
debug("evaluating uncached attribute '%s'", getAttrPathStr());
|
2019-06-07 23:38:39 +03:00
|
|
|
|
2020-04-20 14:13:52 +03:00
|
|
|
auto & v = getValue();
|
2019-06-07 23:25:48 +03:00
|
|
|
|
2020-04-20 14:13:52 +03:00
|
|
|
try {
|
2022-01-21 18:44:19 +02:00
|
|
|
root->state.forceValue(v, noPos);
|
2020-04-20 14:13:52 +03:00
|
|
|
} catch (EvalError &) {
|
|
|
|
debug("setting '%s' to failed", getAttrPathStr());
|
|
|
|
if (root->db)
|
|
|
|
cachedValue = {root->db->setFailed(getKey()), failed_t()};
|
|
|
|
throw;
|
|
|
|
}
|
2019-06-07 23:25:48 +03:00
|
|
|
|
2020-04-20 14:13:52 +03:00
|
|
|
if (root->db && (!cachedValue || std::get_if<placeholder_t>(&cachedValue->second))) {
|
2020-12-17 15:45:45 +02:00
|
|
|
if (v.type() == nString)
|
2023-09-26 04:30:41 +03:00
|
|
|
cachedValue = {root->db->setString(getKey(), v.c_str(), v.context()),
|
|
|
|
string_t{v.c_str(), {}}};
|
2023-04-06 14:15:50 +03:00
|
|
|
else if (v.type() == nPath) {
|
|
|
|
auto path = v.path().path;
|
|
|
|
cachedValue = {root->db->setString(getKey(), path.abs()), string_t{path.abs(), {}}};
|
|
|
|
}
|
2020-12-29 03:40:04 +02:00
|
|
|
else if (v.type() == nBool)
|
2024-03-25 19:20:18 +02:00
|
|
|
cachedValue = {root->db->setBool(getKey(), v.boolean()), v.boolean()};
|
2022-05-16 03:28:21 +03:00
|
|
|
else if (v.type() == nInt)
|
language: cleanly ban integer overflows
This also bans various sneaking of negative numbers from the language
into unsuspecting builtins as was exposed while auditing the
consequences of changing the Nix language integer type to a newtype.
It's unlikely that this change comprehensively ensures correctness when
passing integers out of the Nix language and we should probably add a
checked-narrowing function or something similar, but that's out of scope
for the immediate change.
During the development of this I found a few fun facts about the
language:
- You could overflow integers by converting from unsigned JSON values.
- You could overflow unsigned integers by converting negative numbers
into them when going into Nix config, into fetchTree, and into flake
inputs.
The flake inputs and Nix config cannot actually be tested properly
since they both ban thunks, however, we put in checks anyway because
it's possible these could somehow be used to do such shenanigans some
other way.
Note that Lix has banned Nix language integer overflows since the very
first public beta, but threw a SIGILL about them because we run with
-fsanitize=signed-overflow -fsanitize-undefined-trap-on-error in
production builds. Since the Nix language uses signed integers, overflow
was simply undefined behaviour, and since we defined that to trap, it
did.
Trapping on it was a bad UX, but we didn't even entirely notice
that we had done this at all until it was reported as a bug a couple of
months later (which is, to be fair, that flag working as intended), and
it's got enough production time that, aside from code that is IMHO buggy
(and which is, in any case, not in nixpkgs) such as
https://git.lix.systems/lix-project/lix/issues/445, we don't think
anyone doing anything reasonable actually depends on wrapping overflow.
Even for weird use cases such as doing funny bit crimes, it doesn't make
sense IMO to have wrapping behaviour, since two's complement arithmetic
overflow behaviour is so *aggressively* not what you want for *any* kind
of mathematics/algorithms. The Nix language exists for package
management, a domain where bit crimes are already only dubiously in
scope to begin with, and it makes a lot more sense for that domain for
the integers to never lose precision, either by throwing errors if they
would, or by being arbitrary-precision.
Fixes: https://github.com/NixOS/nix/issues/10968
Original-CL: https://gerrit.lix.systems/c/lix/+/1596
Change-Id: I51f253840c4af2ea5422b8a420aa5fafbf8fae75
2024-07-12 17:22:34 +03:00
|
|
|
cachedValue = {root->db->setInt(getKey(), v.integer().value), int_t{v.integer()}};
|
2020-12-17 15:45:45 +02:00
|
|
|
else if (v.type() == nAttrs)
|
2020-04-20 14:13:52 +03:00
|
|
|
; // FIXME: do something?
|
|
|
|
else
|
|
|
|
cachedValue = {root->db->setMisc(getKey()), misc_t()};
|
|
|
|
}
|
|
|
|
|
|
|
|
return v;
|
2019-06-07 23:25:48 +03:00
|
|
|
}
|
|
|
|
|
2022-03-04 10:44:00 +02:00
|
|
|
Suggestions AttrCursor::getSuggestionsForAttr(Symbol name)
|
|
|
|
{
|
|
|
|
auto attrNames = getAttrs();
|
|
|
|
std::set<std::string> strAttrNames;
|
|
|
|
for (auto & name : attrNames)
|
2024-06-06 17:33:41 +03:00
|
|
|
strAttrNames.insert(std::string(root->state.symbols[name]));
|
2022-03-04 10:44:00 +02:00
|
|
|
|
2022-04-22 22:45:39 +03:00
|
|
|
return Suggestions::bestMatches(strAttrNames, root->state.symbols[name]);
|
2022-03-04 10:44:00 +02:00
|
|
|
}
|
|
|
|
|
2024-04-19 20:34:07 +03:00
|
|
|
std::shared_ptr<AttrCursor> AttrCursor::maybeGetAttr(Symbol name)
|
2019-06-07 23:25:48 +03:00
|
|
|
{
|
2020-04-20 14:13:52 +03:00
|
|
|
if (root->db) {
|
|
|
|
if (!cachedValue)
|
2022-04-26 15:16:20 +03:00
|
|
|
cachedValue = root->db->getAttr(getKey());
|
2020-04-20 14:13:52 +03:00
|
|
|
|
|
|
|
if (cachedValue) {
|
|
|
|
if (auto attrs = std::get_if<std::vector<Symbol>>(&cachedValue->second)) {
|
|
|
|
for (auto & attr : *attrs)
|
|
|
|
if (attr == name)
|
2022-04-22 22:45:39 +03:00
|
|
|
return std::make_shared<AttrCursor>(root, std::make_pair(shared_from_this(), attr));
|
2020-04-20 14:13:52 +03:00
|
|
|
return nullptr;
|
|
|
|
} else if (std::get_if<placeholder_t>(&cachedValue->second)) {
|
2022-04-26 15:16:20 +03:00
|
|
|
auto attr = root->db->getAttr({cachedValue->first, name});
|
2020-04-20 14:13:52 +03:00
|
|
|
if (attr) {
|
|
|
|
if (std::get_if<missing_t>(&attr->second))
|
|
|
|
return nullptr;
|
2024-04-19 20:34:07 +03:00
|
|
|
else if (std::get_if<failed_t>(&attr->second))
|
|
|
|
throw CachedEvalError(ref(shared_from_this()), name);
|
|
|
|
else
|
2020-04-20 14:13:52 +03:00
|
|
|
return std::make_shared<AttrCursor>(root,
|
|
|
|
std::make_pair(shared_from_this(), name), nullptr, std::move(attr));
|
|
|
|
}
|
|
|
|
// Incomplete attrset, so need to fall thru and
|
|
|
|
// evaluate to see whether 'name' exists
|
|
|
|
} else
|
|
|
|
return nullptr;
|
libexpr: Support structured error classes
While preparing PRs like #9753, I've had to change error messages in
dozens of code paths. It would be nice if instead of
EvalError("expected 'boolean' but found '%1%'", showType(v))
we could write
TypeError(v, "boolean")
or similar. Then, changing the error message could be a mechanical
refactor with the compiler pointing out places the constructor needs to
be changed, rather than the error-prone process of grepping through the
codebase. Structured errors would also help prevent the "same" error
from having multiple slightly different messages, and could be a first
step towards error codes / an error index.
This PR reworks the exception infrastructure in `libexpr` to
support exception types with different constructor signatures than
`BaseError`. Actually refactoring the exceptions to use structured data
will come in a future PR (this one is big enough already, as it has to
touch every exception in `libexpr`).
The core design is in `eval-error.hh`. Generally, errors like this:
state.error("'%s' is not a string", getAttrPathStr())
.debugThrow<TypeError>()
are transformed like this:
state.error<TypeError>("'%s' is not a string", getAttrPathStr())
.debugThrow()
The type annotation has moved from `ErrorBuilder::debugThrow` to
`EvalState::error`.
2024-01-23 03:08:29 +02:00
|
|
|
//error<TypeError>("'%s' is not an attribute set", getAttrPathStr()).debugThrow();
|
2020-04-20 14:13:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto & v = forceValue();
|
|
|
|
|
2020-12-17 15:45:45 +02:00
|
|
|
if (v.type() != nAttrs)
|
2020-04-20 14:13:52 +03:00
|
|
|
return nullptr;
|
libexpr: Support structured error classes
While preparing PRs like #9753, I've had to change error messages in
dozens of code paths. It would be nice if instead of
EvalError("expected 'boolean' but found '%1%'", showType(v))
we could write
TypeError(v, "boolean")
or similar. Then, changing the error message could be a mechanical
refactor with the compiler pointing out places the constructor needs to
be changed, rather than the error-prone process of grepping through the
codebase. Structured errors would also help prevent the "same" error
from having multiple slightly different messages, and could be a first
step towards error codes / an error index.
This PR reworks the exception infrastructure in `libexpr` to
support exception types with different constructor signatures than
`BaseError`. Actually refactoring the exceptions to use structured data
will come in a future PR (this one is big enough already, as it has to
touch every exception in `libexpr`).
The core design is in `eval-error.hh`. Generally, errors like this:
state.error("'%s' is not a string", getAttrPathStr())
.debugThrow<TypeError>()
are transformed like this:
state.error<TypeError>("'%s' is not a string", getAttrPathStr())
.debugThrow()
The type annotation has moved from `ErrorBuilder::debugThrow` to
`EvalState::error`.
2024-01-23 03:08:29 +02:00
|
|
|
//error<TypeError>("'%s' is not an attribute set", getAttrPathStr()).debugThrow();
|
2020-04-20 14:13:52 +03:00
|
|
|
|
2024-03-25 19:20:18 +02:00
|
|
|
auto attr = v.attrs()->get(name);
|
2019-06-07 23:38:39 +03:00
|
|
|
|
2020-04-20 14:13:52 +03:00
|
|
|
if (!attr) {
|
|
|
|
if (root->db) {
|
|
|
|
if (!cachedValue)
|
|
|
|
cachedValue = {root->db->setPlaceholder(getKey()), placeholder_t()};
|
|
|
|
root->db->setMissing({cachedValue->first, name});
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
2019-06-07 23:25:48 +03:00
|
|
|
|
2020-04-20 14:13:52 +03:00
|
|
|
std::optional<std::pair<AttrId, AttrValue>> cachedValue2;
|
|
|
|
if (root->db) {
|
|
|
|
if (!cachedValue)
|
|
|
|
cachedValue = {root->db->setPlaceholder(getKey()), placeholder_t()};
|
|
|
|
cachedValue2 = {root->db->setPlaceholder({cachedValue->first, name}), placeholder_t()};
|
|
|
|
}
|
2019-06-07 23:25:48 +03:00
|
|
|
|
2022-03-04 10:44:00 +02:00
|
|
|
return make_ref<AttrCursor>(
|
2020-04-20 14:13:52 +03:00
|
|
|
root, std::make_pair(shared_from_this(), name), attr->value, std::move(cachedValue2));
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<AttrCursor> AttrCursor::maybeGetAttr(std::string_view name)
|
|
|
|
{
|
|
|
|
return maybeGetAttr(root->state.symbols.create(name));
|
|
|
|
}
|
|
|
|
|
2024-04-19 20:34:07 +03:00
|
|
|
ref<AttrCursor> AttrCursor::getAttr(Symbol name)
|
2020-04-20 14:13:52 +03:00
|
|
|
{
|
2024-04-19 20:34:07 +03:00
|
|
|
auto p = maybeGetAttr(name);
|
2020-04-20 14:13:52 +03:00
|
|
|
if (!p)
|
|
|
|
throw Error("attribute '%s' does not exist", getAttrPathStr(name));
|
2022-03-04 10:44:00 +02:00
|
|
|
return ref(p);
|
2020-04-20 14:13:52 +03:00
|
|
|
}
|
|
|
|
|
2022-03-04 10:44:00 +02:00
|
|
|
ref<AttrCursor> AttrCursor::getAttr(std::string_view name)
|
2020-04-20 14:13:52 +03:00
|
|
|
{
|
|
|
|
return getAttr(root->state.symbols.create(name));
|
|
|
|
}
|
|
|
|
|
2024-04-19 20:34:07 +03:00
|
|
|
OrSuggestions<ref<AttrCursor>> AttrCursor::findAlongAttrPath(const std::vector<Symbol> & attrPath)
|
2020-04-20 14:13:52 +03:00
|
|
|
{
|
|
|
|
auto res = shared_from_this();
|
|
|
|
for (auto & attr : attrPath) {
|
2024-04-19 20:34:07 +03:00
|
|
|
auto child = res->maybeGetAttr(attr);
|
2022-03-04 10:44:00 +02:00
|
|
|
if (!child) {
|
|
|
|
auto suggestions = res->getSuggestionsForAttr(attr);
|
|
|
|
return OrSuggestions<ref<AttrCursor>>::failed(suggestions);
|
|
|
|
}
|
|
|
|
res = child;
|
2020-04-20 14:13:52 +03:00
|
|
|
}
|
2022-03-04 10:44:00 +02:00
|
|
|
return ref(res);
|
2020-04-20 14:13:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string AttrCursor::getString()
|
|
|
|
{
|
|
|
|
if (root->db) {
|
|
|
|
if (!cachedValue)
|
2022-04-26 15:16:20 +03:00
|
|
|
cachedValue = root->db->getAttr(getKey());
|
2020-04-20 14:13:52 +03:00
|
|
|
if (cachedValue && !std::get_if<placeholder_t>(&cachedValue->second)) {
|
2020-06-29 20:08:37 +03:00
|
|
|
if (auto s = std::get_if<string_t>(&cachedValue->second)) {
|
2020-04-20 14:13:52 +03:00
|
|
|
debug("using cached string attribute '%s'", getAttrPathStr());
|
2020-06-29 20:08:37 +03:00
|
|
|
return s->first;
|
2020-04-20 14:13:52 +03:00
|
|
|
} else
|
libexpr: Support structured error classes
While preparing PRs like #9753, I've had to change error messages in
dozens of code paths. It would be nice if instead of
EvalError("expected 'boolean' but found '%1%'", showType(v))
we could write
TypeError(v, "boolean")
or similar. Then, changing the error message could be a mechanical
refactor with the compiler pointing out places the constructor needs to
be changed, rather than the error-prone process of grepping through the
codebase. Structured errors would also help prevent the "same" error
from having multiple slightly different messages, and could be a first
step towards error codes / an error index.
This PR reworks the exception infrastructure in `libexpr` to
support exception types with different constructor signatures than
`BaseError`. Actually refactoring the exceptions to use structured data
will come in a future PR (this one is big enough already, as it has to
touch every exception in `libexpr`).
The core design is in `eval-error.hh`. Generally, errors like this:
state.error("'%s' is not a string", getAttrPathStr())
.debugThrow<TypeError>()
are transformed like this:
state.error<TypeError>("'%s' is not a string", getAttrPathStr())
.debugThrow()
The type annotation has moved from `ErrorBuilder::debugThrow` to
`EvalState::error`.
2024-01-23 03:08:29 +02:00
|
|
|
root->state.error<TypeError>("'%s' is not a string", getAttrPathStr()).debugThrow();
|
2020-04-20 14:13:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto & v = forceValue();
|
|
|
|
|
2020-12-17 15:45:45 +02:00
|
|
|
if (v.type() != nString && v.type() != nPath)
|
2024-03-22 19:11:24 +02:00
|
|
|
root->state.error<TypeError>("'%s' is not a string but %s", getAttrPathStr(), showType(v)).debugThrow();
|
2020-04-20 14:13:52 +03:00
|
|
|
|
2023-09-26 04:30:41 +03:00
|
|
|
return v.type() == nString ? v.c_str() : v.path().to_string();
|
2020-04-20 14:13:52 +03:00
|
|
|
}
|
|
|
|
|
2020-06-29 20:08:37 +03:00
|
|
|
string_t AttrCursor::getStringWithContext()
|
|
|
|
{
|
|
|
|
if (root->db) {
|
|
|
|
if (!cachedValue)
|
2022-04-26 15:16:20 +03:00
|
|
|
cachedValue = root->db->getAttr(getKey());
|
2020-06-29 20:08:37 +03:00
|
|
|
if (cachedValue && !std::get_if<placeholder_t>(&cachedValue->second)) {
|
|
|
|
if (auto s = std::get_if<string_t>(&cachedValue->second)) {
|
2020-11-19 21:59:36 +02:00
|
|
|
bool valid = true;
|
|
|
|
for (auto & c : s->second) {
|
2023-01-03 18:44:59 +02:00
|
|
|
const StorePath & path = std::visit(overloaded {
|
|
|
|
[&](const NixStringContextElem::DrvDeep & d) -> const StorePath & {
|
|
|
|
return d.drvPath;
|
|
|
|
},
|
|
|
|
[&](const NixStringContextElem::Built & b) -> const StorePath & {
|
Make the Derived Path family of types inductive for dynamic derivations
We want to be able to write down `foo.drv^bar.drv^baz`:
`foo.drv^bar.drv` is the dynamic derivation (since it is itself a
derivation output, `bar.drv` from `foo.drv`).
To that end, we create `Single{Derivation,BuiltPath}` types, that are
very similar except instead of having multiple outputs (in a set or
map), they have a single one. This is for everything to the left of the
rightmost `^`.
`NixStringContextElem` has an analogous change, and now can reuse
`SingleDerivedPath` at the top level. In fact, if we ever get rid of
`DrvDeep`, `NixStringContextElem` could be replaced with
`SingleDerivedPath` entirely!
Important note: some JSON formats have changed.
We already can *produce* dynamic derivations, but we can't refer to them
directly. Today, we can merely express building or example at the top
imperatively over time by building `foo.drv^bar.drv`, and then with a
second nix invocation doing `<result-from-first>^baz`, but this is not
declarative. The ethos of Nix of being able to write down the full plan
everything you want to do, and then execute than plan with a single
command, and for that we need the new inductive form of these types.
Co-authored-by: Robert Hensing <roberth@users.noreply.github.com>
Co-authored-by: Valentin Gagarin <valentin.gagarin@tweag.io>
2023-01-16 00:39:04 +02:00
|
|
|
return b.drvPath->getBaseStorePath();
|
2023-01-03 18:44:59 +02:00
|
|
|
},
|
|
|
|
[&](const NixStringContextElem::Opaque & o) -> const StorePath & {
|
|
|
|
return o.path;
|
|
|
|
},
|
2023-08-16 19:29:23 +03:00
|
|
|
}, c.raw);
|
2023-01-03 18:44:59 +02:00
|
|
|
if (!root->state.store->isValidPath(path)) {
|
2020-11-19 21:59:36 +02:00
|
|
|
valid = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (valid) {
|
|
|
|
debug("using cached string attribute '%s'", getAttrPathStr());
|
|
|
|
return *s;
|
|
|
|
}
|
2020-06-29 20:08:37 +03:00
|
|
|
} else
|
libexpr: Support structured error classes
While preparing PRs like #9753, I've had to change error messages in
dozens of code paths. It would be nice if instead of
EvalError("expected 'boolean' but found '%1%'", showType(v))
we could write
TypeError(v, "boolean")
or similar. Then, changing the error message could be a mechanical
refactor with the compiler pointing out places the constructor needs to
be changed, rather than the error-prone process of grepping through the
codebase. Structured errors would also help prevent the "same" error
from having multiple slightly different messages, and could be a first
step towards error codes / an error index.
This PR reworks the exception infrastructure in `libexpr` to
support exception types with different constructor signatures than
`BaseError`. Actually refactoring the exceptions to use structured data
will come in a future PR (this one is big enough already, as it has to
touch every exception in `libexpr`).
The core design is in `eval-error.hh`. Generally, errors like this:
state.error("'%s' is not a string", getAttrPathStr())
.debugThrow<TypeError>()
are transformed like this:
state.error<TypeError>("'%s' is not a string", getAttrPathStr())
.debugThrow()
The type annotation has moved from `ErrorBuilder::debugThrow` to
`EvalState::error`.
2024-01-23 03:08:29 +02:00
|
|
|
root->state.error<TypeError>("'%s' is not a string", getAttrPathStr()).debugThrow();
|
2020-06-29 20:08:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto & v = forceValue();
|
|
|
|
|
Use `std::set<StringContextElem>` not `PathSet` for string contexts
Motivation
`PathSet` is not correct because string contexts have other forms
(`Built` and `DrvDeep`) that are not rendered as plain store paths.
Instead of wrongly using `PathSet`, or "stringly typed" using
`StringSet`, use `std::std<StringContextElem>`.
-----
In support of this change, `NixStringContext` is now defined as
`std::std<StringContextElem>` not `std:vector<StringContextElem>`. The
old definition was just used by a `getContext` method which was only
used by the eval cache. It can be deleted altogether since the types are
now unified and the preexisting `copyContext` function already suffices.
Summarizing the previous paragraph:
Old:
- `value/context.hh`: `NixStringContext = std::vector<StringContextElem>`
- `value.hh`: `NixStringContext Value::getContext(...)`
- `value.hh`: `copyContext(...)`
New:
- `value/context.hh`: `NixStringContext = std::set<StringContextElem>`
- `value.hh`: `copyContext(...)`
----
The string representation of string context elements no longer contains
the store dir. The diff of `src/libexpr/tests/value/context.cc` should
make clear what the new representation is, so we recommend reviewing
that file first. This was done for two reasons:
Less API churn:
`Value::mkString` and friends did not take a `Store` before. But if
`NixStringContextElem::{parse, to_string}` *do* take a store (as they
did before), then we cannot have the `Value` functions use them (in
order to work with the fully-structured `NixStringContext`) without
adding that argument.
That would have been a lot of churn of threading the store, and this
diff is already large enough, so the easier and less invasive thing to
do was simply make the element `parse` and `to_string` functions not
take the `Store` reference, and the easiest way to do that was to simply
drop the store dir.
Space usage:
Dropping the `/nix/store/` (or similar) from the internal representation
will safe space in the heap of the Nix programming being interpreted. If
the heap contains many strings with non-trivial contexts, the saving
could add up to something significant.
----
The eval cache version is bumped.
The eval cache serialization uses `NixStringContextElem::{parse,
to_string}`, and since those functions are changed per the above, that
means the on-disk representation is also changed.
This is simply done by changing the name of the used for the eval cache
from `eval-cache-v4` to eval-cache-v5`.
----
To avoid some duplication `EvalCache::mkPathString` is added to abstract
over the simple case of turning a store path to a string with just that
string in the context.
Context
This PR picks up where #7543 left off. That one introduced the fully
structured `NixStringContextElem` data type, but kept `PathSet context`
as an awkward middle ground between internal `char[][]` interpreter heap
string contexts and `NixStringContext` fully parsed string contexts.
The infelicity of `PathSet context` was specifically called out during
Nix team group review, but it was agreeing that fixing it could be left
as future work. This is that future work.
A possible follow-up step would be to get rid of the `char[][]`
evaluator heap representation, too, but it is not yet clear how to do
that. To use `NixStringContextElem` there we would need to get the STL
containers to GC pointers in the GC build, and I am not sure how to do
that.
----
PR #7543 effectively is writing the inverse of a `mkPathString`,
`mkOutputString`, and one more such function for the `DrvDeep` case. I
would like that PR to have property tests ensuring it is actually the
inverse as expected.
This PR sets things up nicely so that reworking that PR to be in that
more elegant and better tested way is possible.
Co-authored-by: Théophane Hufschmitt <7226587+thufschmitt@users.noreply.github.com>
2023-01-29 03:31:10 +02:00
|
|
|
if (v.type() == nString) {
|
|
|
|
NixStringContext context;
|
|
|
|
copyContext(v, context);
|
2023-09-26 04:30:41 +03:00
|
|
|
return {v.c_str(), std::move(context)};
|
2023-04-25 17:52:02 +03:00
|
|
|
}
|
|
|
|
else if (v.type() == nPath)
|
2023-04-06 14:15:50 +03:00
|
|
|
return {v.path().to_string(), {}};
|
2020-06-29 20:08:37 +03:00
|
|
|
else
|
2024-03-22 19:11:24 +02:00
|
|
|
root->state.error<TypeError>("'%s' is not a string but %s", getAttrPathStr(), showType(v)).debugThrow();
|
2020-06-29 20:08:37 +03:00
|
|
|
}
|
|
|
|
|
2020-04-27 17:29:26 +03:00
|
|
|
bool AttrCursor::getBool()
|
|
|
|
{
|
|
|
|
if (root->db) {
|
|
|
|
if (!cachedValue)
|
2022-04-26 15:16:20 +03:00
|
|
|
cachedValue = root->db->getAttr(getKey());
|
2020-04-27 17:29:26 +03:00
|
|
|
if (cachedValue && !std::get_if<placeholder_t>(&cachedValue->second)) {
|
|
|
|
if (auto b = std::get_if<bool>(&cachedValue->second)) {
|
|
|
|
debug("using cached Boolean attribute '%s'", getAttrPathStr());
|
|
|
|
return *b;
|
|
|
|
} else
|
libexpr: Support structured error classes
While preparing PRs like #9753, I've had to change error messages in
dozens of code paths. It would be nice if instead of
EvalError("expected 'boolean' but found '%1%'", showType(v))
we could write
TypeError(v, "boolean")
or similar. Then, changing the error message could be a mechanical
refactor with the compiler pointing out places the constructor needs to
be changed, rather than the error-prone process of grepping through the
codebase. Structured errors would also help prevent the "same" error
from having multiple slightly different messages, and could be a first
step towards error codes / an error index.
This PR reworks the exception infrastructure in `libexpr` to
support exception types with different constructor signatures than
`BaseError`. Actually refactoring the exceptions to use structured data
will come in a future PR (this one is big enough already, as it has to
touch every exception in `libexpr`).
The core design is in `eval-error.hh`. Generally, errors like this:
state.error("'%s' is not a string", getAttrPathStr())
.debugThrow<TypeError>()
are transformed like this:
state.error<TypeError>("'%s' is not a string", getAttrPathStr())
.debugThrow()
The type annotation has moved from `ErrorBuilder::debugThrow` to
`EvalState::error`.
2024-01-23 03:08:29 +02:00
|
|
|
root->state.error<TypeError>("'%s' is not a Boolean", getAttrPathStr()).debugThrow();
|
2020-04-27 17:29:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto & v = forceValue();
|
|
|
|
|
2020-12-17 15:45:45 +02:00
|
|
|
if (v.type() != nBool)
|
libexpr: Support structured error classes
While preparing PRs like #9753, I've had to change error messages in
dozens of code paths. It would be nice if instead of
EvalError("expected 'boolean' but found '%1%'", showType(v))
we could write
TypeError(v, "boolean")
or similar. Then, changing the error message could be a mechanical
refactor with the compiler pointing out places the constructor needs to
be changed, rather than the error-prone process of grepping through the
codebase. Structured errors would also help prevent the "same" error
from having multiple slightly different messages, and could be a first
step towards error codes / an error index.
This PR reworks the exception infrastructure in `libexpr` to
support exception types with different constructor signatures than
`BaseError`. Actually refactoring the exceptions to use structured data
will come in a future PR (this one is big enough already, as it has to
touch every exception in `libexpr`).
The core design is in `eval-error.hh`. Generally, errors like this:
state.error("'%s' is not a string", getAttrPathStr())
.debugThrow<TypeError>()
are transformed like this:
state.error<TypeError>("'%s' is not a string", getAttrPathStr())
.debugThrow()
The type annotation has moved from `ErrorBuilder::debugThrow` to
`EvalState::error`.
2024-01-23 03:08:29 +02:00
|
|
|
root->state.error<TypeError>("'%s' is not a Boolean", getAttrPathStr()).debugThrow();
|
2020-04-27 17:29:26 +03:00
|
|
|
|
2024-03-25 19:20:18 +02:00
|
|
|
return v.boolean();
|
2020-04-27 17:29:26 +03:00
|
|
|
}
|
|
|
|
|
2022-05-13 23:02:28 +03:00
|
|
|
NixInt AttrCursor::getInt()
|
|
|
|
{
|
|
|
|
if (root->db) {
|
|
|
|
if (!cachedValue)
|
|
|
|
cachedValue = root->db->getAttr(getKey());
|
|
|
|
if (cachedValue && !std::get_if<placeholder_t>(&cachedValue->second)) {
|
2022-05-16 16:17:35 +03:00
|
|
|
if (auto i = std::get_if<int_t>(&cachedValue->second)) {
|
2022-12-07 13:58:58 +02:00
|
|
|
debug("using cached integer attribute '%s'", getAttrPathStr());
|
2022-05-16 17:36:21 +03:00
|
|
|
return i->x;
|
2022-05-13 23:02:28 +03:00
|
|
|
} else
|
libexpr: Support structured error classes
While preparing PRs like #9753, I've had to change error messages in
dozens of code paths. It would be nice if instead of
EvalError("expected 'boolean' but found '%1%'", showType(v))
we could write
TypeError(v, "boolean")
or similar. Then, changing the error message could be a mechanical
refactor with the compiler pointing out places the constructor needs to
be changed, rather than the error-prone process of grepping through the
codebase. Structured errors would also help prevent the "same" error
from having multiple slightly different messages, and could be a first
step towards error codes / an error index.
This PR reworks the exception infrastructure in `libexpr` to
support exception types with different constructor signatures than
`BaseError`. Actually refactoring the exceptions to use structured data
will come in a future PR (this one is big enough already, as it has to
touch every exception in `libexpr`).
The core design is in `eval-error.hh`. Generally, errors like this:
state.error("'%s' is not a string", getAttrPathStr())
.debugThrow<TypeError>()
are transformed like this:
state.error<TypeError>("'%s' is not a string", getAttrPathStr())
.debugThrow()
The type annotation has moved from `ErrorBuilder::debugThrow` to
`EvalState::error`.
2024-01-23 03:08:29 +02:00
|
|
|
root->state.error<TypeError>("'%s' is not an integer", getAttrPathStr()).debugThrow();
|
2022-05-13 23:02:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto & v = forceValue();
|
|
|
|
|
|
|
|
if (v.type() != nInt)
|
libexpr: Support structured error classes
While preparing PRs like #9753, I've had to change error messages in
dozens of code paths. It would be nice if instead of
EvalError("expected 'boolean' but found '%1%'", showType(v))
we could write
TypeError(v, "boolean")
or similar. Then, changing the error message could be a mechanical
refactor with the compiler pointing out places the constructor needs to
be changed, rather than the error-prone process of grepping through the
codebase. Structured errors would also help prevent the "same" error
from having multiple slightly different messages, and could be a first
step towards error codes / an error index.
This PR reworks the exception infrastructure in `libexpr` to
support exception types with different constructor signatures than
`BaseError`. Actually refactoring the exceptions to use structured data
will come in a future PR (this one is big enough already, as it has to
touch every exception in `libexpr`).
The core design is in `eval-error.hh`. Generally, errors like this:
state.error("'%s' is not a string", getAttrPathStr())
.debugThrow<TypeError>()
are transformed like this:
state.error<TypeError>("'%s' is not a string", getAttrPathStr())
.debugThrow()
The type annotation has moved from `ErrorBuilder::debugThrow` to
`EvalState::error`.
2024-01-23 03:08:29 +02:00
|
|
|
root->state.error<TypeError>("'%s' is not an integer", getAttrPathStr()).debugThrow();
|
2022-05-13 23:02:28 +03:00
|
|
|
|
2024-03-25 19:20:18 +02:00
|
|
|
return v.integer();
|
2022-05-13 23:02:28 +03:00
|
|
|
}
|
|
|
|
|
2022-04-20 17:39:47 +03:00
|
|
|
std::vector<std::string> AttrCursor::getListOfStrings()
|
|
|
|
{
|
|
|
|
if (root->db) {
|
|
|
|
if (!cachedValue)
|
|
|
|
cachedValue = root->db->getAttr(getKey());
|
|
|
|
if (cachedValue && !std::get_if<placeholder_t>(&cachedValue->second)) {
|
|
|
|
if (auto l = std::get_if<std::vector<std::string>>(&cachedValue->second)) {
|
|
|
|
debug("using cached list of strings attribute '%s'", getAttrPathStr());
|
|
|
|
return *l;
|
|
|
|
} else
|
libexpr: Support structured error classes
While preparing PRs like #9753, I've had to change error messages in
dozens of code paths. It would be nice if instead of
EvalError("expected 'boolean' but found '%1%'", showType(v))
we could write
TypeError(v, "boolean")
or similar. Then, changing the error message could be a mechanical
refactor with the compiler pointing out places the constructor needs to
be changed, rather than the error-prone process of grepping through the
codebase. Structured errors would also help prevent the "same" error
from having multiple slightly different messages, and could be a first
step towards error codes / an error index.
This PR reworks the exception infrastructure in `libexpr` to
support exception types with different constructor signatures than
`BaseError`. Actually refactoring the exceptions to use structured data
will come in a future PR (this one is big enough already, as it has to
touch every exception in `libexpr`).
The core design is in `eval-error.hh`. Generally, errors like this:
state.error("'%s' is not a string", getAttrPathStr())
.debugThrow<TypeError>()
are transformed like this:
state.error<TypeError>("'%s' is not a string", getAttrPathStr())
.debugThrow()
The type annotation has moved from `ErrorBuilder::debugThrow` to
`EvalState::error`.
2024-01-23 03:08:29 +02:00
|
|
|
root->state.error<TypeError>("'%s' is not a list of strings", getAttrPathStr()).debugThrow();
|
2022-04-20 17:39:47 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
debug("evaluating uncached attribute '%s'", getAttrPathStr());
|
|
|
|
|
|
|
|
auto & v = getValue();
|
|
|
|
root->state.forceValue(v, noPos);
|
|
|
|
|
|
|
|
if (v.type() != nList)
|
libexpr: Support structured error classes
While preparing PRs like #9753, I've had to change error messages in
dozens of code paths. It would be nice if instead of
EvalError("expected 'boolean' but found '%1%'", showType(v))
we could write
TypeError(v, "boolean")
or similar. Then, changing the error message could be a mechanical
refactor with the compiler pointing out places the constructor needs to
be changed, rather than the error-prone process of grepping through the
codebase. Structured errors would also help prevent the "same" error
from having multiple slightly different messages, and could be a first
step towards error codes / an error index.
This PR reworks the exception infrastructure in `libexpr` to
support exception types with different constructor signatures than
`BaseError`. Actually refactoring the exceptions to use structured data
will come in a future PR (this one is big enough already, as it has to
touch every exception in `libexpr`).
The core design is in `eval-error.hh`. Generally, errors like this:
state.error("'%s' is not a string", getAttrPathStr())
.debugThrow<TypeError>()
are transformed like this:
state.error<TypeError>("'%s' is not a string", getAttrPathStr())
.debugThrow()
The type annotation has moved from `ErrorBuilder::debugThrow` to
`EvalState::error`.
2024-01-23 03:08:29 +02:00
|
|
|
root->state.error<TypeError>("'%s' is not a list", getAttrPathStr()).debugThrow();
|
2022-04-20 17:39:47 +03:00
|
|
|
|
|
|
|
std::vector<std::string> res;
|
|
|
|
|
|
|
|
for (auto & elem : v.listItems())
|
2023-01-19 14:23:04 +02:00
|
|
|
res.push_back(std::string(root->state.forceStringNoCtx(*elem, noPos, "while evaluating an attribute for caching")));
|
2022-04-20 17:39:47 +03:00
|
|
|
|
2022-05-04 22:13:49 +03:00
|
|
|
if (root->db)
|
|
|
|
cachedValue = {root->db->setListOfStrings(getKey(), res), res};
|
2022-04-20 17:39:47 +03:00
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2020-04-20 14:13:52 +03:00
|
|
|
std::vector<Symbol> AttrCursor::getAttrs()
|
|
|
|
{
|
|
|
|
if (root->db) {
|
|
|
|
if (!cachedValue)
|
2022-04-26 15:16:20 +03:00
|
|
|
cachedValue = root->db->getAttr(getKey());
|
2020-04-20 14:13:52 +03:00
|
|
|
if (cachedValue && !std::get_if<placeholder_t>(&cachedValue->second)) {
|
|
|
|
if (auto attrs = std::get_if<std::vector<Symbol>>(&cachedValue->second)) {
|
|
|
|
debug("using cached attrset attribute '%s'", getAttrPathStr());
|
|
|
|
return *attrs;
|
|
|
|
} else
|
libexpr: Support structured error classes
While preparing PRs like #9753, I've had to change error messages in
dozens of code paths. It would be nice if instead of
EvalError("expected 'boolean' but found '%1%'", showType(v))
we could write
TypeError(v, "boolean")
or similar. Then, changing the error message could be a mechanical
refactor with the compiler pointing out places the constructor needs to
be changed, rather than the error-prone process of grepping through the
codebase. Structured errors would also help prevent the "same" error
from having multiple slightly different messages, and could be a first
step towards error codes / an error index.
This PR reworks the exception infrastructure in `libexpr` to
support exception types with different constructor signatures than
`BaseError`. Actually refactoring the exceptions to use structured data
will come in a future PR (this one is big enough already, as it has to
touch every exception in `libexpr`).
The core design is in `eval-error.hh`. Generally, errors like this:
state.error("'%s' is not a string", getAttrPathStr())
.debugThrow<TypeError>()
are transformed like this:
state.error<TypeError>("'%s' is not a string", getAttrPathStr())
.debugThrow()
The type annotation has moved from `ErrorBuilder::debugThrow` to
`EvalState::error`.
2024-01-23 03:08:29 +02:00
|
|
|
root->state.error<TypeError>("'%s' is not an attribute set", getAttrPathStr()).debugThrow();
|
2020-04-20 14:13:52 +03:00
|
|
|
}
|
|
|
|
}
|
2019-06-07 23:25:48 +03:00
|
|
|
|
2020-04-20 14:13:52 +03:00
|
|
|
auto & v = forceValue();
|
2019-06-07 23:25:48 +03:00
|
|
|
|
2020-12-17 15:45:45 +02:00
|
|
|
if (v.type() != nAttrs)
|
libexpr: Support structured error classes
While preparing PRs like #9753, I've had to change error messages in
dozens of code paths. It would be nice if instead of
EvalError("expected 'boolean' but found '%1%'", showType(v))
we could write
TypeError(v, "boolean")
or similar. Then, changing the error message could be a mechanical
refactor with the compiler pointing out places the constructor needs to
be changed, rather than the error-prone process of grepping through the
codebase. Structured errors would also help prevent the "same" error
from having multiple slightly different messages, and could be a first
step towards error codes / an error index.
This PR reworks the exception infrastructure in `libexpr` to
support exception types with different constructor signatures than
`BaseError`. Actually refactoring the exceptions to use structured data
will come in a future PR (this one is big enough already, as it has to
touch every exception in `libexpr`).
The core design is in `eval-error.hh`. Generally, errors like this:
state.error("'%s' is not a string", getAttrPathStr())
.debugThrow<TypeError>()
are transformed like this:
state.error<TypeError>("'%s' is not a string", getAttrPathStr())
.debugThrow()
The type annotation has moved from `ErrorBuilder::debugThrow` to
`EvalState::error`.
2024-01-23 03:08:29 +02:00
|
|
|
root->state.error<TypeError>("'%s' is not an attribute set", getAttrPathStr()).debugThrow();
|
2019-06-07 23:25:48 +03:00
|
|
|
|
2020-04-20 14:13:52 +03:00
|
|
|
std::vector<Symbol> attrs;
|
2024-03-25 19:20:18 +02:00
|
|
|
for (auto & attr : *getValue().attrs())
|
2020-04-20 14:13:52 +03:00
|
|
|
attrs.push_back(attr.name);
|
2022-04-22 22:45:39 +03:00
|
|
|
std::sort(attrs.begin(), attrs.end(), [&](Symbol a, Symbol b) {
|
|
|
|
std::string_view sa = root->state.symbols[a], sb = root->state.symbols[b];
|
|
|
|
return sa < sb;
|
2020-04-20 14:13:52 +03:00
|
|
|
});
|
2019-06-07 23:25:48 +03:00
|
|
|
|
2020-04-20 14:13:52 +03:00
|
|
|
if (root->db)
|
|
|
|
cachedValue = {root->db->setAttrs(getKey(), attrs), attrs};
|
2019-06-07 23:25:48 +03:00
|
|
|
|
2020-04-20 14:13:52 +03:00
|
|
|
return attrs;
|
2019-06-07 23:25:48 +03:00
|
|
|
}
|
|
|
|
|
2020-04-20 14:13:52 +03:00
|
|
|
bool AttrCursor::isDerivation()
|
2019-06-07 23:25:48 +03:00
|
|
|
{
|
2020-04-20 14:13:52 +03:00
|
|
|
auto aType = maybeGetAttr("type");
|
|
|
|
return aType && aType->getString() == "derivation";
|
2019-06-07 23:25:48 +03:00
|
|
|
}
|
|
|
|
|
2020-06-29 17:39:41 +03:00
|
|
|
StorePath AttrCursor::forceDerivation()
|
|
|
|
{
|
2024-04-19 20:34:07 +03:00
|
|
|
auto aDrvPath = getAttr(root->state.sDrvPath);
|
2020-06-29 17:39:41 +03:00
|
|
|
auto drvPath = root->state.store->parseStorePath(aDrvPath->getString());
|
2024-05-22 19:35:44 +03:00
|
|
|
drvPath.requireDerivation();
|
2020-06-29 17:39:41 +03:00
|
|
|
if (!root->state.store->isValidPath(drvPath) && !settings.readOnlyMode) {
|
|
|
|
/* The eval cache contains 'drvPath', but the actual path has
|
|
|
|
been garbage-collected. So force it to be regenerated. */
|
|
|
|
aDrvPath->forceValue();
|
|
|
|
if (!root->state.store->isValidPath(drvPath))
|
|
|
|
throw Error("don't know how to recreate store derivation '%s'!",
|
|
|
|
root->state.store->printStorePath(drvPath));
|
|
|
|
}
|
|
|
|
return drvPath;
|
|
|
|
}
|
|
|
|
|
2019-06-07 23:25:48 +03:00
|
|
|
}
|