aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMaximilian Bosch <maximilian@mbosch.me>2024-05-30 09:24:14 +0000
committerGerrit Code Review <gerrit@lix-systems>2024-05-30 09:24:14 +0000
commitce82067566a18fcd77ef1fe2f2575921fcceb665 (patch)
treebe89f481be4b0bd12898d0b0c72d5390de5198c5
parent2760818f062b6810da6766639c543aeb1db45522 (diff)
parent031d92411637904a8ed759dc80bc474213dd4fa9 (diff)
Merge "libutil/args: warn on unknown settings after parsing all flags" into main
-rw-r--r--doc/manual/change-authors.yml4
-rw-r--r--doc/manual/rl-next/fix-silent-unknown-options.md30
-rw-r--r--src/libutil/args.cc8
-rw-r--r--tests/functional/eval.sh4
-rw-r--r--tests/functional/misc.sh9
5 files changed, 48 insertions, 7 deletions
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 ]]