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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prioritize CLI arguments over env vars when they conflict #5738

Merged
merged 5 commits into from
Feb 16, 2019

Conversation

BenMusch
Copy link
Contributor

@BenMusch BenMusch commented Feb 15, 2019

  • Have you followed the guidelines in our Contributing document?
  • Have you checked to ensure there aren't other open Pull Requests for the same change?
  • Have you added an explanation of what your changes do and why you'd like us to include them?
  • Have you written new tests for your changes? Here's an example.
  • Have you successfully run brew style with your changes locally? (Note: Brew style fails, but the failure is there to keep it consistent with other tests in the file. Happy to change it if y'all prefer)
  • Have you successfully run brew tests with your changes locally?

Addresses an issue mentioned here: #5736

(Caveat: The issue linked also points out a disconnect between the help text of the command and its actual behavior, which should probably be fixed separately)

I agree with that issue that CLI flags should take priority over environment variables when they conflict, and it's behavior I'm used to in other applications. This PR does that by selecting the CLI-passed argument when there are conflicting switches and all but one of the switches are specified via an env var.

Not 100% tied to this being the correct behavior -- just opening this in case y'all agree that it is

Also, I think there's also an opportunity for a richer API to resolve conflicts here, something along the lines of:

def switch(*names, description: nil, env: nil, required_for: nil, depends_on: nil, env_priority: 1, cli_priority: 2)

Then, you could specify the priority of an argument so that it would always take priority over another when they conflict, etc. But that seems like it may be more code than is worth maintaining

@@ -58,7 +59,7 @@ def switch(*names, description: nil, env: nil, required_for: nil, depends_on: ni
set_constraints(name, required_for: required_for, depends_on: depends_on)
end

enable_switch(*names) if !env.nil? && !ENV["HOMEBREW_#{env.to_s.upcase}"].nil?
enable_switch(*names, source: :env_var) if !env.nil? && !ENV["HOMEBREW_#{env.to_s.upcase}"].nil?
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would prefer to extract these env/cli values into constants somewhere, but not sure what the style for that is, or even if a simple boolean would be better here

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I understand what you mean? Also I'd maybe go for from: :env

Copy link
Contributor Author

@BenMusch BenMusch Feb 15, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this phrasing is clearer:

Would it be better to extract :env and :args to constants, to avoid having magic values floating around, or even just to change it to a boolean is_env_var? flag?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, still being stupid here! Can you give some examples of what values you think should maybe be constants?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

something like:

class Parser
  SWITCH_SOURCE_ENV_VAR = :env 
  SWITCH_SOURCE_CLI_ARG = :arg 

  ...

  def switch(...)
    ...
    enable_switch(*names, source: SWITCH_SOURCE_ENV_VAR)
  end
  
  ...

  def check_conflicts
    ...
    env_var_options = violations.select do |option|
      @switch_sources[option_to_name(option)] == SWITCH_SOURCE_ENV_VAR
    end
  end
end

Just to avoid having :env and :arg scattered around the code as magic values

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feels a bit overkill. I think the symbols are fine.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good. This is all set from my perspective, last thing would just be making sure that y'all are okay with the brew style failure, or if it would be better to update https://github.com/Homebrew/brew/pull/5738/files#diff-40e44d1232a302a388e451cc3a0a22f9R186 to expect(...).to be_switch_a, etc.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@BenMusch Need to have brew style be passing I'm afraid, thanks. brew style --fix may be able to do that for you automatically.

Copy link
Member

@MikeMcQuaid MikeMcQuaid left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work on this! I like the direction.

@@ -58,7 +59,7 @@ def switch(*names, description: nil, env: nil, required_for: nil, depends_on: ni
set_constraints(name, required_for: required_for, depends_on: depends_on)
end

enable_switch(*names) if !env.nil? && !ENV["HOMEBREW_#{env.to_s.upcase}"].nil?
enable_switch(*names, source: :env_var) if !env.nil? && !ENV["HOMEBREW_#{env.to_s.upcase}"].nil?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I understand what you mean? Also I'd maybe go for from: :env

@@ -172,12 +173,19 @@ def hide_from_man_page!

private

def enable_switch(*names)
def enable_switch(*names, source: :cli_arg)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might make the callers more obvious to make this a non-default parameter. Also, perhaps from: :args

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@BenMusch Thoughts on making this a non-default parameter?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fine with me, will push a change in an hour or two

@MikeMcQuaid
Copy link
Member

But that seems like it may be more code than is worth maintaining

Agreed.

I agree with that issue that CLI flags should take priority over environment variables when they conflict, and it's behavior I'm used to in other applications.

Also agreed 👍

@@ -51,14 +52,14 @@ def switch(*names, description: nil, env: nil, required_for: nil, depends_on: ni
end
process_option(*names, description)
@parser.on(*names, *wrap_option_desc(description)) do
enable_switch(*names)
enable_switch(*names, from: :args)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeh, much prefer this, thanks again!

@MikeMcQuaid MikeMcQuaid merged commit f3716fe into Homebrew:master Feb 16, 2019
@MikeMcQuaid
Copy link
Member

Thanks again @BenMusch, great work!

@@ -225,7 +233,13 @@ def check_conflicts

next if violations.count < 2

raise OptionConflictError, violations.map(&method(:name_to_option))
env_var_options = violations.select do |option|
@switch_sources[option_to_name(option)] == :env_var
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:env_var should be :env?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds right. Has this been fixed yet? I should also probably re-write the tests to catch this bug in the future

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not yet!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, the tests should ideally catch this failure

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good, will take care of this today

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, looks like the issue with the test is that stubbing env vars isn't working at all. My tests unfortunately were structured in a way that they passed regardless of that. For example, this fails:

allow(ENV).to receive(:[]).with("HOMEBREW_SWITCH_A").and_return("1")
expect(ENV["HOMEBREW_SWITCH_A"]).to eq("1") # actually is nil

Any idea how to fix this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found the issue, I had my stubs in the wrong order. Pushing a change now, sorry for the bugs!

@lock lock bot added the outdated PR was locked due to age label Mar 27, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Mar 27, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated PR was locked due to age
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants