Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warn on unknown settings when the first positional is an argument #10701

Merged
merged 2 commits into from
May 20, 2024

Conversation

cole-h
Copy link
Member

@cole-h cole-h commented May 14, 2024

Motivation

We should warn when unknown settings are passed on the command line.

Context

While debugging an unrelated issue, I noticed that one of the flags I was passing wasn't valid for that version, but Nix never warned me of this fact. When I tested a similar invocation with the legacy interface, I was immediately warned about this unknown setting.

Priorities and Process

Add 馃憤 to pull requests you find important.

The Nix maintainer team uses a GitHub project board to schedule and track reviews.

@cole-h cole-h requested a review from edolstra as a code owner May 14, 2024 19:29
@github-actions github-actions bot added the with-tests Issues related to testing. PRs with tests have some priority label May 14, 2024
@fricklerhandwerk
Copy link
Contributor

Does this also affect nix-build and all other stable commands?

@cole-h

This comment was marked as resolved.

@cole-h cole-h marked this pull request as draft May 14, 2024 21:09
@cole-h cole-h force-pushed the nix-command-warn-unknown-settings branch from 894bfdf to a53fd51 Compare May 15, 2024 18:32
@cole-h
Copy link
Member Author

cole-h commented May 15, 2024

OK, I updated the tests to test both clis and ensure that only one line of "unknown setting" is output.

Here's what I found while investigating this:

  • The warning on the old cli comes from the call to initialFlagsProcessed
  • Because the new cli uses subcommands (i.e. nix eval), eval is treated as an arg, which hits this branch before seeing any other options on the command line:

    nix/src/libutil/args.cc

    Lines 339 to 342 in bbe780b

    if (!argsSeen) {
    argsSeen = true;
    initialFlagsProcessed();
    }
  • If we unconditionally call initialFlagsProcessed after all the flags and args have been processed, the issue goes away and we are always (at least in every scenario I tested) warned about unknown args/flags.

You can actually reproduce this "silence" on the old cli with this:

$ nix-instantiate '{}' --max-jobs 0 --option foobar baz --expr

'{}' is an "arg", and the very first thing, so even though there's an invalid option specified later (foobar), this function doesn't care anymore because after parsing '{}', it has argsSeen == true, and will never attempt to call initialFlagsProcessed again.

@cole-h cole-h marked this pull request as ready for review May 15, 2024 19:11
@cole-h cole-h changed the title Warn on unknown settings when using nix-command Warn on unknown settings when the first positional is an argument May 15, 2024
@cole-h
Copy link
Member Author

cole-h commented May 15, 2024

Before:

$ nix-instantiate --option foobar baz --expr '{}'
warning: unknown setting 'foobar'
$ nix-instantiate '{}' --option foobar baz --expr
$ nix eval --expr '{}' --option foobar baz
{ }

After:

$ 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'
{ }

@cole-h cole-h force-pushed the nix-command-warn-unknown-settings branch from a53fd51 to 572fd4c Compare May 15, 2024 19:23
@cole-h cole-h force-pushed the nix-command-warn-unknown-settings branch from 572fd4c to 39a2696 Compare May 15, 2024 19:25
@roberth roberth merged commit e4be8ab into NixOS:master May 20, 2024
9 checks passed
@cole-h cole-h deleted the nix-command-warn-unknown-settings branch May 20, 2024 13:16
lf- pushed a commit to lix-project/lix that referenced this pull request May 30, 2024
Upstream change: NixOS/nix#10701

Change-Id: Icf271df57ec529dd8c64667d1ef9f6dbf02d33d3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation with-tests Issues related to testing. PRs with tests have some priority
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants