lib.fileset.gitTracked: Better error in pure eval

This commit is contained in:
Silvan Mosberger
2023-11-03 22:30:52 +01:00
parent d33f1a62f5
commit ada680bcfa
2 changed files with 49 additions and 18 deletions

View File

@@ -52,6 +52,7 @@ let
inherit (lib.trivial) inherit (lib.trivial)
isFunction isFunction
pipe pipe
inPureEvalMode
; ;
in { in {
@@ -628,7 +629,9 @@ in {
let let
fetchResult = builtins.fetchGit path; fetchResult = builtins.fetchGit path;
in in
if ! isPath path then if inPureEvalMode then
throw "lib.fileset.gitTracked: This function is currently not supported in pure evaluation mode, since it currently relies on `builtins.fetchGit`. See https://github.com/NixOS/nix/issues/9292."
else if ! isPath path then
throw "lib.fileset.gitTracked: Expected the argument to be a path, but it's a ${typeOf path} instead." throw "lib.fileset.gitTracked: Expected the argument to be a path, but it's a ${typeOf path} instead."
else if ! pathExists (path + "/.git") then else if ! pathExists (path + "/.git") then
throw "lib.fileset.gitTracked: Expected the argument (${toString path}) to point to a local working tree of a Git repository, but it's not." throw "lib.fileset.gitTracked: Expected the argument (${toString path}) to point to a local working tree of a Git repository, but it's not."
@@ -690,7 +693,9 @@ in {
${if recurseSubmodules then "submodules" else null} = true; ${if recurseSubmodules then "submodules" else null} = true;
}; };
in in
if ! isBool recurseSubmodules then if inPureEvalMode then
throw "lib.fileset.gitTrackedWith: This function is currently not supported in pure evaluation mode, since it currently relies on `builtins.fetchGit`. See https://github.com/NixOS/nix/issues/9292."
else if ! isBool recurseSubmodules then
throw "lib.fileset.gitTrackedWith: Expected the attribute `recurseSubmodules` of the first argument to be a boolean, but it's a ${typeOf recurseSubmodules} instead." throw "lib.fileset.gitTrackedWith: Expected the attribute `recurseSubmodules` of the first argument to be a boolean, but it's a ${typeOf recurseSubmodules} instead."
else if recurseSubmodules && versionOlder nixVersion _fetchGitSubmodulesMinver then else if recurseSubmodules && versionOlder nixVersion _fetchGitSubmodulesMinver then
throw "lib.fileset.gitTrackedWith: Setting the attribute `recurseSubmodules` to `true` is only supported for Nix version ${_fetchGitSubmodulesMinver} and after, but Nix version ${nixVersion} is used." throw "lib.fileset.gitTrackedWith: Setting the attribute `recurseSubmodules` to `true` is only supported for Nix version ${_fetchGitSubmodulesMinver} and after, but Nix version ${nixVersion} is used."

View File

@@ -43,15 +43,29 @@ crudeUnquoteJSON() {
cut -d \" -f2 cut -d \" -f2
} }
prefixExpression='let prefixExpression() {
lib = import <nixpkgs/lib>; echo 'let
internal = import <nixpkgs/lib/fileset/internal.nix> { lib =
inherit lib; (import <nixpkgs/lib>)
}; '
in if [[ "${1:-}" == "--simulate-pure-eval" ]]; then
with lib; echo '
with internal; .extend (final: prev: {
with lib.fileset;' trivial = prev.trivial // {
inPureEvalMode = true;
};
})'
fi
echo '
;
internal = import <nixpkgs/lib/fileset/internal.nix> {
inherit lib;
};
in
with lib;
with internal;
with lib.fileset;'
}
# Check that two nix expression successfully evaluate to the same value. # Check that two nix expression successfully evaluate to the same value.
# The expressions have `lib.fileset` in scope. # The expressions have `lib.fileset` in scope.
@@ -60,7 +74,7 @@ expectEqual() {
local actualExpr=$1 local actualExpr=$1
local expectedExpr=$2 local expectedExpr=$2
if actualResult=$(nix-instantiate --eval --strict --show-trace 2>"$tmp"/actualStderr \ if actualResult=$(nix-instantiate --eval --strict --show-trace 2>"$tmp"/actualStderr \
--expr "$prefixExpression ($actualExpr)"); then --expr "$(prefixExpression) ($actualExpr)"); then
actualExitCode=$? actualExitCode=$?
else else
actualExitCode=$? actualExitCode=$?
@@ -68,7 +82,7 @@ expectEqual() {
actualStderr=$(< "$tmp"/actualStderr) actualStderr=$(< "$tmp"/actualStderr)
if expectedResult=$(nix-instantiate --eval --strict --show-trace 2>"$tmp"/expectedStderr \ if expectedResult=$(nix-instantiate --eval --strict --show-trace 2>"$tmp"/expectedStderr \
--expr "$prefixExpression ($expectedExpr)"); then --expr "$(prefixExpression) ($expectedExpr)"); then
expectedExitCode=$? expectedExitCode=$?
else else
expectedExitCode=$? expectedExitCode=$?
@@ -96,7 +110,7 @@ expectEqual() {
expectStorePath() { expectStorePath() {
local expr=$1 local expr=$1
if ! result=$(nix-instantiate --eval --strict --json --read-write-mode --show-trace 2>"$tmp"/stderr \ if ! result=$(nix-instantiate --eval --strict --json --read-write-mode --show-trace 2>"$tmp"/stderr \
--expr "$prefixExpression ($expr)"); then --expr "$(prefixExpression) ($expr)"); then
cat "$tmp/stderr" >&2 cat "$tmp/stderr" >&2
die "$expr failed to evaluate, but it was expected to succeed" die "$expr failed to evaluate, but it was expected to succeed"
fi fi
@@ -109,10 +123,16 @@ expectStorePath() {
# The expression has `lib.fileset` in scope. # The expression has `lib.fileset` in scope.
# Usage: expectFailure NIX REGEX # Usage: expectFailure NIX REGEX
expectFailure() { expectFailure() {
if [[ "$1" == "--simulate-pure-eval" ]]; then
maybePure="--simulate-pure-eval"
shift
else
maybePure=""
fi
local expr=$1 local expr=$1
local expectedErrorRegex=$2 local expectedErrorRegex=$2
if result=$(nix-instantiate --eval --strict --read-write-mode --show-trace 2>"$tmp/stderr" \ if result=$(nix-instantiate --eval --strict --read-write-mode --show-trace 2>"$tmp/stderr" \
--expr "$prefixExpression $expr"); then --expr "$(prefixExpression $maybePure) $expr"); then
die "$expr evaluated successfully to $result, but it was expected to fail" die "$expr evaluated successfully to $result, but it was expected to fail"
fi fi
stderr=$(<"$tmp/stderr") stderr=$(<"$tmp/stderr")
@@ -129,12 +149,12 @@ expectTrace() {
local expectedTrace=$2 local expectedTrace=$2
nix-instantiate --eval --show-trace >/dev/null 2>"$tmp"/stderrTrace \ nix-instantiate --eval --show-trace >/dev/null 2>"$tmp"/stderrTrace \
--expr "$prefixExpression trace ($expr)" || true --expr "$(prefixExpression) trace ($expr)" || true
actualTrace=$(sed -n 's/^trace: //p' "$tmp/stderrTrace") actualTrace=$(sed -n 's/^trace: //p' "$tmp/stderrTrace")
nix-instantiate --eval --show-trace >/dev/null 2>"$tmp"/stderrTraceVal \ nix-instantiate --eval --show-trace >/dev/null 2>"$tmp"/stderrTraceVal \
--expr "$prefixExpression traceVal ($expr)" || true --expr "$(prefixExpression) traceVal ($expr)" || true
actualTraceVal=$(sed -n 's/^trace: //p' "$tmp/stderrTraceVal") actualTraceVal=$(sed -n 's/^trace: //p' "$tmp/stderrTraceVal")
@@ -1266,7 +1286,7 @@ expectFailure 'gitTrackedWith {} ./.' 'lib.fileset.gitTrackedWith: Expected the
expectFailure 'gitTrackedWith { recurseSubmodules = null; } ./.' 'lib.fileset.gitTrackedWith: Expected the attribute `recurseSubmodules` of the first argument to be a boolean, but it'\''s a null instead.' expectFailure 'gitTrackedWith { recurseSubmodules = null; } ./.' 'lib.fileset.gitTrackedWith: Expected the attribute `recurseSubmodules` of the first argument to be a boolean, but it'\''s a null instead.'
# recurseSubmodules = true is not supported on all Nix versions # recurseSubmodules = true is not supported on all Nix versions
if [[ "$(nix-instantiate --eval --expr "$prefixExpression (versionAtLeast builtins.nixVersion _fetchGitSubmodulesMinver)")" == true ]]; then if [[ "$(nix-instantiate --eval --expr "$(prefixExpression) (versionAtLeast builtins.nixVersion _fetchGitSubmodulesMinver)")" == true ]]; then
fetchGitSupportsSubmodules=1 fetchGitSupportsSubmodules=1
else else
fetchGitSupportsSubmodules= fetchGitSupportsSubmodules=
@@ -1336,6 +1356,12 @@ createGitRepo() {
git -C "$1" commit -q --allow-empty -m "Empty commit" git -C "$1" commit -q --allow-empty -m "Empty commit"
} }
# Check the error message for pure eval mode
createGitRepo .
expectFailure --simulate-pure-eval 'toSource { root = ./.; fileset = gitTracked ./.; }' 'lib.fileset.gitTracked: This function is currently not supported in pure evaluation mode, since it currently relies on `builtins.fetchGit`. See https://github.com/NixOS/nix/issues/9292.'
expectFailure --simulate-pure-eval 'toSource { root = ./.; fileset = gitTrackedWith {} ./.; }' 'lib.fileset.gitTrackedWith: This function is currently not supported in pure evaluation mode, since it currently relies on `builtins.fetchGit`. See https://github.com/NixOS/nix/issues/9292.'
rm -rf -- *
# Go through all stages of Git files # Go through all stages of Git files
# See https://www.git-scm.com/book/en/v2/Git-Basics-Recording-Changes-to-the-Repository # See https://www.git-scm.com/book/en/v2/Git-Basics-Recording-Changes-to-the-Repository