From c66865dff1747ecd55915ef8c549dc1a088ff168 Mon Sep 17 00:00:00 2001 From: Nikolay Amiantov Date: Fri, 4 Mar 2016 13:47:19 +0300 Subject: [PATCH 1/2] builtins.readFile: Propagate path context Co-authored-by: Shea Levy --- src/libexpr/primops.cc | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc index d7382af6b..1d2a7d5d2 100644 --- a/src/libexpr/primops.cc +++ b/src/libexpr/primops.cc @@ -1470,7 +1470,11 @@ static void prim_readFile(EvalState & state, const Pos & pos, Value * * args, Va string s = readFile(path); if (s.find((char) 0) != string::npos) throw Error("the contents of the file '%1%' cannot be represented as a Nix string", path); - v.mkString(s); + auto refs = state.store->isInStore(path) ? + state.store->queryPathInfo(state.store->toStorePath(path).first)->references : + StorePathSet{}; + auto context = state.store->printStorePathSet(refs); + v.mkString(s, context); } static RegisterPrimOp primop_readFile({ From 120ca245d13708743a59e83da12ec90902636236 Mon Sep 17 00:00:00 2001 From: Nikolay Amiantov Date: Sat, 11 Nov 2017 01:51:34 +0300 Subject: [PATCH 2/2] Add test for readFile keeping context --- tests/local.mk | 3 ++- tests/readfile-context.builder.sh | 1 + tests/readfile-context.nix | 19 +++++++++++++++++++ tests/readfile-context.sh | 16 ++++++++++++++++ 4 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 tests/readfile-context.builder.sh create mode 100644 tests/readfile-context.nix create mode 100644 tests/readfile-context.sh diff --git a/tests/local.mk b/tests/local.mk index c22e779a6..d2a46f22b 100644 --- a/tests/local.mk +++ b/tests/local.mk @@ -59,7 +59,8 @@ nix_tests = \ ca/recursive.sh \ ca/concurrent-builds.sh \ ca/nix-copy.sh \ - eval-store.sh + eval-store.sh \ + readfile-context.sh # parallel.sh ifeq ($(HAVE_LIBCPUID), 1) diff --git a/tests/readfile-context.builder.sh b/tests/readfile-context.builder.sh new file mode 100644 index 000000000..7084a08cb --- /dev/null +++ b/tests/readfile-context.builder.sh @@ -0,0 +1 @@ +echo "$input" > $out diff --git a/tests/readfile-context.nix b/tests/readfile-context.nix new file mode 100644 index 000000000..600036a94 --- /dev/null +++ b/tests/readfile-context.nix @@ -0,0 +1,19 @@ +with import ./config.nix; + +let + + input = import ./simple.nix; + + dependent = mkDerivation { + name = "dependent"; + builder = ./readfile-context.builder.sh; + input = "${input}/hello"; + }; + + readDependent = mkDerivation { + name = "read-dependent"; + builder = ./readfile-context.builder.sh; + input = builtins.readFile dependent; + }; + +in readDependent diff --git a/tests/readfile-context.sh b/tests/readfile-context.sh new file mode 100644 index 000000000..31e70ddb1 --- /dev/null +++ b/tests/readfile-context.sh @@ -0,0 +1,16 @@ +source common.sh + +clearStore + +outPath=$(nix-build --no-out-link readfile-context.nix) + +# Set a GC root. +ln -s $outPath "$NIX_STATE_DIR"/gcroots/foo + +# Check that file exists. +[ "$(cat $(cat $outPath))" = "Hello World!" ] + +nix-collect-garbage + +# Check that file still exists. +[ "$(cat $(cat $outPath))" = "Hello World!" ]