Skip to content

Commit

Permalink
Merge pull request NixOS#8905 from hercules-ci/no-unknown-location
Browse files Browse the repository at this point in the history
Don't print unknown locations unless requested for dev purposes

(cherry picked from commit 3dd4475)
Change-Id: I04a91277d1d9d09f5c1bf4a28fc99f0702b161e5
  • Loading branch information
eldritch horrors committed Mar 4, 2024
1 parent c36ba79 commit 4517de0
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 35 deletions.
55 changes: 32 additions & 23 deletions src/libutil/error.cc
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,36 @@ static std::string indent(std::string_view indentFirst, std::string_view indentR
return res;
}

/**
* A development aid for finding missing positions, to improve error messages. Example use:
*
* NIX_DEVELOPER_SHOW_UNKNOWN_LOCATIONS=1 _NIX_TEST_ACCEPT=1 make tests/lang.sh.test
* git diff -U20 tests
*
*/
static bool printUnknownLocations = getEnv("_NIX_DEVELOPER_SHOW_UNKNOWN_LOCATIONS").has_value();

/**
* Print a position, if it is known.
*
* @return true if a position was printed.
*/
static bool printPosMaybe(std::ostream & oss, std::string_view indent, const std::shared_ptr<AbstractPos> & pos) {
bool hasPos = pos && *pos;
if (hasPos) {
oss << "\n" << indent << ANSI_BLUE << "at " ANSI_WARNING << *pos << ANSI_NORMAL << ":";

if (auto loc = pos->getCodeLines()) {
oss << "\n";
printCodeLines(oss, "", *pos, *loc);
oss << "\n";
}
} else if (printUnknownLocations) {
oss << "\n" << indent << ANSI_BLUE << "at " ANSI_RED << "UNKNOWN LOCATION" << ANSI_NORMAL << "\n";
}
return hasPos;
}

std::ostream & showErrorInfo(std::ostream & out, const ErrorInfo & einfo, bool showTrace)
{
std::string prefix;
Expand Down Expand Up @@ -203,8 +233,6 @@ std::ostream & showErrorInfo(std::ostream & out, const ErrorInfo & einfo, bool s

std::ostringstream oss;

auto noSource = ANSI_ITALIC " (source not available)" ANSI_NORMAL "\n";

/*
* Traces
* ------
Expand Down Expand Up @@ -320,34 +348,15 @@ std::ostream & showErrorInfo(std::ostream & out, const ErrorInfo & einfo, bool s

oss << "\n" << "" << trace.hint.str() << "\n";

if (trace.pos) {
if (printPosMaybe(oss, ellipsisIndent, trace.pos))
count++;

oss << "\n" << ellipsisIndent << ANSI_BLUE << "at " ANSI_WARNING << *trace.pos << ANSI_NORMAL << ":";

if (auto loc = trace.pos->getCodeLines()) {
oss << "\n";
printCodeLines(oss, "", *trace.pos, *loc);
oss << "\n";
} else
oss << noSource;
}
}
oss << "\n" << prefix;
}

oss << einfo.msg << "\n";

if (einfo.errPos) {
oss << "\n" << ANSI_BLUE << "at " ANSI_WARNING << *einfo.errPos << ANSI_NORMAL << ":";

if (auto loc = einfo.errPos->getCodeLines()) {
oss << "\n";
printCodeLines(oss, "", *einfo.errPos, *loc);
oss << "\n";
} else
oss << noSource;
}
printPosMaybe(oss, "", einfo.errPos);

auto suggestions = einfo.suggestions.trim();
if (!suggestions.suggestions.empty()) {
Expand Down
7 changes: 7 additions & 0 deletions src/libutil/error.hh
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ struct AbstractPos
uint32_t line = 0;
uint32_t column = 0;

/**
* An AbstractPos may be a "null object", representing an unknown position.
*
* Return true if this position is known.
*/
inline operator bool() const { return line != 0; };

/**
* Return the contents of the source file.
*/
Expand Down
2 changes: 0 additions & 2 deletions tests/functional/lang/eval-fail-fromTOML-timestamps.err.exp
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,3 @@ error:
2| key = "value"

error: while parsing a TOML string: Dates and times are not supported

at «none»:0: (source not available)
4 changes: 0 additions & 4 deletions tests/functional/lang/eval-fail-hashfile-missing.err.exp
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ error:

… while evaluating the first argument passed to builtins.toString

at «none»:0: (source not available)

… while calling the 'hashFile' builtin

at «none»:0: (source not available)

error: opening file '/pwd/lang/this-file-is-definitely-not-there-7392097': No such file or directory
2 changes: 0 additions & 2 deletions tests/functional/lang/eval-fail-set-override.err.exp
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
error:
… while evaluating the `__overrides` attribute

at «none»:0: (source not available)

error: value is an integer while a set was expected
2 changes: 0 additions & 2 deletions tests/functional/lang/eval-fail-substring.err.exp
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,3 @@ error:
2|

error: negative start position in 'substring'

at «none»:0: (source not available)
2 changes: 0 additions & 2 deletions tests/functional/lang/eval-fail-to-path.err.exp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,4 @@ error:

… while evaluating the first argument passed to builtins.toPath

at «none»:0: (source not available)

error: string 'foo/bar' doesn't represent an absolute path

0 comments on commit 4517de0

Please sign in to comment.