Skip to content

Commit

Permalink
Don't eat my ARGV!
Browse files Browse the repository at this point in the history
Change the way that OptionParser.parse! is called, so that it doesn't
directly sink its teeth into ARGV.  This necessitated changing the way that
collect_command_line_tasks get the list of command-line arguments to examine
to get tasks and environment variable definitions, but that's a relatively
minor thing.
  • Loading branch information
mpalmer committed Dec 3, 2014
1 parent d2b6d7d commit 9532698
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
19 changes: 13 additions & 6 deletions lib/rake/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ def run
def init(app_name='rake')
standard_exception_handling do
@name = app_name
handle_options
collect_command_line_tasks
args = handle_options
collect_command_line_tasks(args)
end
end

Expand Down Expand Up @@ -616,7 +616,9 @@ def select_trace_output(options, trace_option, value) # :nodoc:
end
private :select_trace_output

# Read and handle the command line options.
# Read and handle the command line options. Returns the command line
# arguments that we didn't understand, which should (in theory) be just
# task names and env vars.
def handle_options # :nodoc:
options.rakelib = ['rakelib']
options.trace_output = $stderr
Expand All @@ -633,7 +635,7 @@ def handle_options # :nodoc:

standard_rake_options.each { |args| opts.on(*args) }
opts.environment('RAKEOPT')
end.parse!
end.parse(ARGV)
end

# Similar to the regular Ruby +require+ command, but will check
Expand Down Expand Up @@ -727,9 +729,14 @@ def standard_system_dir #:nodoc:
# Collect the list of tasks on the command line. If no tasks are
# given, return a list containing only the default task.
# Environmental assignments are processed at this time as well.
def collect_command_line_tasks # :nodoc:
#
# `args` is the list of arguments to peruse to get the list of tasks.
# It should be the command line that was given to rake, less any
# recognised command-line options, which OptionParser.parse will
# have taken care of already.
def collect_command_line_tasks(args) # :nodoc:
@top_level_tasks = []
ARGV.each do |arg|
args.each do |arg|
if arg =~ /^(\w+)=(.*)$/m
ENV[$1] = $2
else
Expand Down
4 changes: 2 additions & 2 deletions test/test_rake_application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -370,15 +370,15 @@ def test_building_imported_files_on_demand
# HACK no assertions
end

def test_handle_options_should_strip_options_from_argv
def test_handle_options_should_not_strip_options_from_argv
assert !@app.options.trace

valid_option = '--trace'
setup_command_line(valid_option)

@app.handle_options

assert !ARGV.include?(valid_option)
assert ARGV.include?(valid_option)
assert @app.options.trace
end

Expand Down
4 changes: 2 additions & 2 deletions test/test_rake_application_options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -457,8 +457,8 @@ def @app.exit(*args)
throw :system_exit, :exit
end
@app.instance_eval do
handle_options
collect_command_line_tasks
args = handle_options
collect_command_line_tasks(args)
end
@tasks = @app.top_level_tasks
@app.options
Expand Down

3 comments on commit 9532698

@bretweinraub
Copy link

Choose a reason for hiding this comment

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

Hmmm I sort of liked this functionality..... downstream code could process anything that rake didn't 'eat'.

What's the rationale .....

Also I wonder if I could get another ARRAY [like the old ARGS] where rake HAS done the eating....

@bretweinraub
Copy link

Choose a reason for hiding this comment

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

Wasn't this just reverted:

86af0ef

with the comment:

This reverts commit a36505d.

This caused issues with tools that used rake across fork boundaries.

Fixes rails/spring#366

Possibly also #4

@mpalmer
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The commit that was reverted was a previous attempt by @drbrain to fix the problem I originally reported. The rationale is to allow "downstream" code to see everything that was passed to rake. My specific use-case is to allow re-exec of rake commands in a different environment (specifically, one with bundler enabled), but anything that wanted to examine the full set of arguments that was passed to rake would also need this change.

I'll comment further on the specifics of your situation in jimweirich#277.

Please sign in to comment.