From 031d92411637904a8ed759dc80bc474213dd4fa9 Mon Sep 17 00:00:00 2001 From: Cole Helbling Date: Tue, 14 May 2024 12:13:40 -0700 Subject: [PATCH] libutil/args: warn on unknown settings after parsing all flags Upstream change: https://github.com/NixOS/nix/pull/10701 Change-Id: Icf271df57ec529dd8c64667d1ef9f6dbf02d33d3 --- doc/manual/change-authors.yml | 4 +++ .../rl-next/fix-silent-unknown-options.md | 30 +++++++++++++++++++ src/libutil/args.cc | 8 +---- tests/functional/eval.sh | 4 +++ tests/functional/misc.sh | 9 ++++++ 5 files changed, 48 insertions(+), 7 deletions(-) create mode 100644 doc/manual/rl-next/fix-silent-unknown-options.md diff --git a/doc/manual/change-authors.yml b/doc/manual/change-authors.yml index 98a135397..c56f588ca 100644 --- a/doc/manual/change-authors.yml +++ b/doc/manual/change-authors.yml @@ -36,6 +36,10 @@ artemist: display_name: Artemis Tosini forgejo: artemist +cole-h: + display_name: Cole Helbling + github: cole-h + edolstra: display_name: Eelco Dolstra github: edolstra diff --git a/doc/manual/rl-next/fix-silent-unknown-options.md b/doc/manual/rl-next/fix-silent-unknown-options.md new file mode 100644 index 000000000..679cc26e8 --- /dev/null +++ b/doc/manual/rl-next/fix-silent-unknown-options.md @@ -0,0 +1,30 @@ +--- +synopsis: Warn on unknown settings anywhere in the command line +prs: 10701 +credits: [cole-h] +category: Improvements +--- + +All `nix` commands will now properly warn when an unknown option is specified anywhere in the command line. + +Before: + +```console +$ nix-instantiate --option foobar baz --expr '{}' +warning: unknown setting 'foobar' +$ nix-instantiate '{}' --option foobar baz --expr +$ nix eval --expr '{}' --option foobar baz +{ } +``` + +After: + +```console +$ nix-instantiate --option foobar baz --expr '{}' +warning: unknown setting 'foobar' +$ nix-instantiate '{}' --option foobar baz --expr +warning: unknown setting 'foobar' +$ nix eval --expr '{}' --option foobar baz +warning: unknown setting 'foobar' +{ } +``` diff --git a/src/libutil/args.cc b/src/libutil/args.cc index 4983e49af..655b3e82f 100644 --- a/src/libutil/args.cc +++ b/src/libutil/args.cc @@ -93,7 +93,6 @@ void RootArgs::parseCmdline(const Strings & _cmdline) verbosity = lvlError; } - bool argsSeen = false; for (auto pos = cmdline.begin(); pos != cmdline.end(); ) { auto arg = *pos; @@ -122,10 +121,6 @@ void RootArgs::parseCmdline(const Strings & _cmdline) throw UsageError("unrecognised flag '%1%'", arg); } else { - if (!argsSeen) { - argsSeen = true; - initialFlagsProcessed(); - } pos = rewriteArgs(cmdline, pos); pendingArgs.push_back(*pos++); if (processArgs(pendingArgs, false)) @@ -135,8 +130,7 @@ void RootArgs::parseCmdline(const Strings & _cmdline) processArgs(pendingArgs, true); - if (!argsSeen) - initialFlagsProcessed(); + initialFlagsProcessed(); /* Now that we are done parsing, make sure that any experimental * feature required by the flags is enabled */ diff --git a/tests/functional/eval.sh b/tests/functional/eval.sh index 2b34caddb..9c125b569 100644 --- a/tests/functional/eval.sh +++ b/tests/functional/eval.sh @@ -51,3 +51,7 @@ mkdir -p $TEST_ROOT/xyzzy $TEST_ROOT/foo ln -sfn ../xyzzy $TEST_ROOT/foo/bar printf 123 > $TEST_ROOT/xyzzy/default.nix [[ $(nix eval --impure --expr "import $TEST_ROOT/foo/bar") = 123 ]] + +# Test that unknown settings are warned about +out="$(expectStderr 0 nix eval --option foobar baz --expr '""' --raw)" +[[ "$(echo "$out" | grep foobar | wc -l)" = 1 ]] diff --git a/tests/functional/misc.sh b/tests/functional/misc.sh index af96d20bd..d4379b7ce 100644 --- a/tests/functional/misc.sh +++ b/tests/functional/misc.sh @@ -30,3 +30,12 @@ expectStderr 1 nix-instantiate --eval -E '[]' -A 'x' | grepQuiet "should be a se expectStderr 1 nix-instantiate --eval -E '{}' -A '1' | grepQuiet "should be a list" expectStderr 1 nix-instantiate --eval -E '{}' -A '.' | grepQuiet "empty attribute name" expectStderr 1 nix-instantiate --eval -E '[]' -A '1' | grepQuiet "out of range" + +# Unknown setting warning +# NOTE(cole-h): behavior is different depending on the order, which is why we test an unknown option +# before and after the `'{}'`! +out="$(expectStderr 0 nix-instantiate --option foobar baz --expr '{}')" +[[ "$(echo "$out" | grep foobar | wc -l)" = 1 ]] + +out="$(expectStderr 0 nix-instantiate '{}' --option foobar baz --expr )" +[[ "$(echo "$out" | grep foobar | wc -l)" = 1 ]]