Search code examples
ruby-on-railsrubyrake

How do I find the source file for a rake task?


I know you can view all possible rake tasks by typing

rake -T

But I need to know what exactly a task does. From the output, how can I find a source file that actually has the task? For example, I'm trying to find the source for the db:schema:dump task.


Solution

  • Despite what others have said, you can programmatically get the source location of rake tasks in a rails application. To do this, just run something like the following in your code or from a console:

    # load all the tasks associated with the rails app
    Rails.application.load_tasks
    
    # get the source locations of actions called by a task
    task_name = 'db:schema:load' # fully scoped task name
    Rake.application[task_name].actions.map(&:source_location)
    

    This will return the source locations of any code that gets executed for this task. You can also use #prerequisites instead of #source_location to get a list of prerequisite task names (e.g. 'environment', etc).

    You can also list all tasks loaded using:

    Rake.application.tasks
    

    UPDATE: See Magne's good answer below. For versions of rake >= 0.9.0 you can use rake -W to show the source location of your rake tasks.