Hacktoberfest 2026 : les issues que les mainteneurs ont marquées pour octobre, ouvertes et accessibles aux débutants. Parcourir les issues Hacktoberfest

import [filename] with task [filename] causes both to execute TWICE?

Ouverte
#228 7 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

Évaluation

Difficulté
4/5
Temps estimé
3-5 jours
Accessibilité débutants
35/100
Type d'issue
Bug
Clarté
Plutôt claire
Activité
À l'abandon
Stack technique
ruby
Domaine
build-system, cli

Piste de recherche

Reproduisez le comportement à l’aide des exemples Rakefile et foo.rb, puis suivez le point d’entrée de l’importation et le chemin d’exécution de la commande -T. Vérifiez comment les fichiers importés et les tâches portant le même nom sont planifiés, notamment lors de la liste des tâches, et ajoutez ou mettez à jour les tests afin que le nombre d’exécutions attendu et le comportement de -T soient explicites.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Description

I picked up Andrey Koleshko's Rake Task Management Essentials to start learning rake. The book covers the import statement in Chapter 1. After some tinkering, I think I figured out what he was trying to say, but noticed some differences between the behavior he describes in the book and what happens on my machine.

I've copied the relevant section below for reference, but I think the upshot is:

If you import a file, and also define a task with the same name as that file, then that task will be run before the file is imported. This feature is provided so that dependency files may be generated on-the-fly.

...The import statement may be used in any line of the Rakefile, and this doesn't apply to the loading process at all. The imported files will be loaded after the whole Rakefile is loaded. Its usage looks similar to the require statement and is shown in the following line of code:

import(filename)

Here, you are able to pass more than one file.

There is one more feature of the import method. If you pass the filenames to the import task, they are evaluated first, and this allows us to generate the dependent files on the fly. Look at the following Rakefile:

task 'dep.rb' do
  sh %Q{echo "puts 'Hello, from the dep.rb'" > dep.rb}
end

task :hello => 'dep.rb'

import 'dep.rb'

This example generates the dep.rb file on the file due to the import 'dep.rb' call that evaluates the 'dep.rb' task. The result of the hello task execution is shown as follows:

$ rake hello
echo "puts 'Hello, from the dep.rb'" > dep.rb
Hello, from the dep.rb

The problem is that on my machine, the aforementioned pre-import task runs twice:

# Rakefile
import 'foo.rb'

task 'foo.rb' do
  puts "I'm a little teacup,"
end
# foo.rb
puts "short and stout!"
$ rake foo.rb
I'm a little teacup,
short and stout!
I'm a little teacup,
short and stout!

At first, I thought this was happening because I was explicitly calling the foo.rb task, so maybe that was running once because I called it and again in response to the import statement — but then I tried running a different task altogether:

# Rakefile
import 'foo.rb'
    
task 'foo.rb' do
  puts "I'm a little teacup,"
end

task :bar

and I get the same duplicated output as before:

$ rake bar
I'm a little teacup,
short and stout!
I'm a little teacup,
short and stout!

In fact, it doesn't just spit out the same input twice; it actually runs twice, from start to finish:

# Rakefile
import 'foo.rb'
    
task 'foo.rb' do
  if !File.exist?('foo.rb')
    puts 'Where is foo.rb?'
    `echo "puts 'I\'m right here.'" > foo.rb`
  else
    puts 'Oh, I didn\'t see you there.'
    `echo "puts 'Sorry, I got held up in traffic.'" > foo.rb`
  end
end
$ rm foo.rb
$ rake foo.rb
Where is foo.rb?
I'm right here.
Oh, I didn't see you there.
Sorry, I got held up in traffic.

Is this the expected behavior, or does this constitute a bug in rake?


On a related note, the command-line flag -T is supposed to display a list of all the rake tasks that have descriptions, but when running rake -T against the above Rakefile, the import directive causes the corresponding task to run anyway:

$ rake -T
I'm a little teacup,
short and stout!
I'm a little teacup,
short and stout!

Is this the expected behavior, or does this constitute a bug in rake?


FWIW I'm on Ruby 2.4.0 / Rake 12.0.0

Langage dominant
Ruby
Étoiles
2.5k
Forks
651
Merge moyen
5 min
PR mergées (30 j)
4

Préparer son environnement

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Autres issues de ruby/rake

Toutes les issues de ruby/rake

Issues similaires

Plus d'issues Ruby

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.