-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New diagnostics infra #15326
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Ericson2314
merged 1 commit into
NixOS:master
from
obsidiansystems:no-abs-paths-in-eval
Feb 26, 2026
Merged
New diagnostics infra #15326
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| --- | ||
| synopsis: "New diagnostics infrastructure, with `lint-url-literals` and `lint-short-path-literals` settings" | ||
| prs: [15326] | ||
| issues: [10048, 10281] | ||
| --- | ||
|
|
||
| A new diagnostics infrastructure has been added for controlling language features that we are considering deprecating. | ||
|
|
||
| ## [`lint-url-literals`](@docroot@/command-ref/conf-file.md#conf-lint-url-literals) | ||
|
|
||
| The `no-url-literals` experimental feature has been stabilized and replaced with a new [`lint-url-literals`](@docroot@/command-ref/conf-file.md#conf-lint-url-literals) setting. | ||
|
|
||
| To migrate from the experimental feature, replace: | ||
| ``` | ||
| experimental-features = no-url-literals | ||
| ``` | ||
| with: | ||
| ``` | ||
| lint-url-literals = fatal | ||
| ``` | ||
|
|
||
| ## [`lint-short-path-literals`](@docroot@/command-ref/conf-file.md#conf-lint-short-path-literals) | ||
|
|
||
| The [`warn-short-path-literals`](@docroot@/command-ref/conf-file.md#conf-warn-short-path-literals) boolean setting has been deprecated and replaced with [`lint-short-path-literals`](@docroot@/command-ref/conf-file.md#conf-lint-short-path-literals). | ||
|
|
||
| To migrate, replace: | ||
| ``` | ||
| warn-short-path-literals = true | ||
| ``` | ||
| with: | ||
| ``` | ||
| lint-short-path-literals = warn | ||
| ``` | ||
|
|
||
| ## Setting values | ||
|
|
||
| Both settings accept three values: | ||
| - `ignore`: Allow the feature without any diagnostic (default) | ||
| - `warn`: Emit a warning when the feature is used | ||
| - `fatal`: Treat the feature as a parse error | ||
|
|
||
| In the future, we may change what the defaults are. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| #include "nix/expr/diagnose.hh" | ||
| #include "nix/util/configuration.hh" | ||
| #include "nix/util/config-impl.hh" | ||
| #include "nix/util/abstract-setting-to-json.hh" | ||
|
|
||
| #include <nlohmann/json.hpp> | ||
|
|
||
| namespace nix { | ||
|
|
||
| template<> | ||
| Diagnose BaseSetting<Diagnose>::parse(const std::string & str) const | ||
| { | ||
| if (str == "ignore") | ||
| return Diagnose::Ignore; | ||
| else if (str == "warn") | ||
| return Diagnose::Warn; | ||
| else if (str == "fatal") | ||
| return Diagnose::Fatal; | ||
| else | ||
| throw UsageError("option '%s' has invalid value '%s' (expected 'ignore', 'warn', or 'fatal')", name, str); | ||
| } | ||
|
|
||
| template<> | ||
| struct BaseSetting<Diagnose>::trait | ||
| { | ||
| static constexpr bool appendable = false; | ||
| }; | ||
|
|
||
| template<> | ||
| std::string BaseSetting<Diagnose>::to_string() const | ||
| { | ||
| switch (value) { | ||
| case Diagnose::Ignore: | ||
| return "ignore"; | ||
| case Diagnose::Warn: | ||
| return "warn"; | ||
| case Diagnose::Fatal: | ||
| return "fatal"; | ||
| default: | ||
| unreachable(); | ||
| } | ||
| } | ||
|
|
||
| NLOHMANN_JSON_SERIALIZE_ENUM( | ||
| Diagnose, | ||
| { | ||
| {Diagnose::Ignore, "ignore"}, | ||
| {Diagnose::Warn, "warn"}, | ||
| {Diagnose::Fatal, "fatal"}, | ||
| }); | ||
|
|
||
| /* Explicit instantiation of templates */ | ||
| template class BaseSetting<Diagnose>; | ||
|
|
||
| } // namespace nix |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| #pragma once | ||
| ///@file | ||
|
|
||
| #include <optional> | ||
|
|
||
| #include "nix/util/ansicolor.hh" | ||
| #include "nix/util/configuration.hh" | ||
| #include "nix/util/error.hh" | ||
| #include "nix/util/logging.hh" | ||
|
|
||
| namespace nix { | ||
|
|
||
| /** | ||
| * Diagnostic level for deprecated or non-portable language features. | ||
| */ | ||
| enum struct Diagnose { | ||
| /** | ||
| * Ignore the feature without any diagnostic. | ||
| */ | ||
| Ignore, | ||
| /** | ||
| * Warn when the feature is used, but allow it. | ||
| */ | ||
| Warn, | ||
| /** | ||
| * Treat the feature as a fatal error. | ||
| */ | ||
| Fatal, | ||
| }; | ||
|
|
||
| template<> | ||
| Diagnose BaseSetting<Diagnose>::parse(const std::string & str) const; | ||
|
|
||
| template<> | ||
| std::string BaseSetting<Diagnose>::to_string() const; | ||
|
|
||
| /** | ||
| * Check a diagnostic setting and either do nothing, log a warning, or throw an error. | ||
| * | ||
| * The setting name is automatically appended to the error message. | ||
| * | ||
| * @param setting The diagnostic setting to check | ||
| * @param mkError A function that takes a bool (true if fatal, false if warning) and | ||
| * returns an optional error to throw (or warn with). | ||
| * Only called if level is not `Ignore`. | ||
| * If the function returns `std::nullopt`, no diagnostic is emitted. | ||
| * | ||
| * @throws The error returned by mkError if level is `Fatal` and mkError returns a value | ||
| */ | ||
| template<typename F> | ||
| void diagnose(const Setting<Diagnose> & setting, F && mkError) | ||
| { | ||
| auto withError = [&](bool fatal, auto && handler) { | ||
| auto maybeError = mkError(fatal); | ||
| if (!maybeError) | ||
| return; | ||
| auto & info = maybeError->unsafeInfo(); | ||
| // Append the setting name to help users find the right setting | ||
| info.msg = HintFmt("%s (" ANSI_BOLD "%s" ANSI_NORMAL ")", Uncolored(info.msg.str()), setting.name); | ||
| maybeError->recalcWhat(); | ||
| handler(std::move(*maybeError)); | ||
| }; | ||
|
|
||
| switch (setting.get()) { | ||
| case Diagnose::Ignore: | ||
| return; | ||
Ericson2314 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| case Diagnose::Warn: | ||
| withError(false, [](auto && error) { logWarning(error.info()); }); | ||
| return; | ||
| case Diagnose::Fatal: | ||
| withError(true, [](auto && error) { throw std::move(error); }); | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| } // namespace nix | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good rename!
We could add
Errorand a diagnostic state later.class DiagnosticState { vector<Error>; warn(Error &&); saveError(Error &&); }or
class DiagnosticState { bool mustThrow; }and
throwingDiagnosticErrors :: (DiagnosticState -> IO r) -> IO rUh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed we should collect all fatal errors and then throw. But best saved for later.