From 49b6766332e7754cd8cc2ee1dd2ccc958b284e54 Mon Sep 17 00:00:00 2001 From: Alexander Romanov Date: Sun, 6 Apr 2025 22:52:46 +0300 Subject: [PATCH] libflake: add lock file path to invalid json error Previously, when lock file contained invalid JSON nix reported a parser error without specifying the file it came from. This change adds flake.lock file path to the error message to avoid confusion. (cherry picked from commit e3873aa1a0b1881f4380dd53ceb5dbd49c69e2c4) --- src/libflake/flake/lockfile.cc | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/libflake/flake/lockfile.cc b/src/libflake/flake/lockfile.cc index ba6f18c57..646516caf 100644 --- a/src/libflake/flake/lockfile.cc +++ b/src/libflake/flake/lockfile.cc @@ -108,8 +108,13 @@ LockFile::LockFile( const fetchers::Settings & fetchSettings, std::string_view contents, std::string_view path) { - auto json = nlohmann::json::parse(contents); - + auto json = [=] { + try { + return nlohmann::json::parse(contents); + } catch (const nlohmann::json::parse_error & e) { + throw Error("Could not parse '%s': %s", path, e.what()); + } + }(); auto version = json.value("version", 0); if (version < 5 || version > 7) throw Error("lock file '%s' has unsupported version %d", path, version);