Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

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

未关闭
#228 7 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
35/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
停滞
技术栈
ruby
领域
build-system, cli

调研方向

使用 Rakefile 和 foo.rb 示例复现该行为,然后跟踪 import 入口点和 -T 命令路径。检查导入的文件和同名任务是如何进行调度的,包括任务列表,并添加或更新测试,使预期的执行次数和 -T 行为明确无误。

由索引模型根据 Issue 内容生成。

描述

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

主要语言
Ruby
星标
2.5k
派生
650
平均合并
6 分钟
30 天内合并 PR
3

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

ruby/rake 的其他 Issue

查看 ruby/rake 的全部 Issue

相似的 Issue

更多 Ruby Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。