`cwd` does not work in `local_ruby_block`
mishina2228 opened this issue · comments
It seems that cwd
does not work in local_ruby_block
.
Minimal reproduce code:
# /tmp/foo/sample.rb
local_ruby_block 'foo' do
cwd '/tmp'
block do
system('pwd')
end
end
$ itamae version
Itamae v1.13.0
$ pwd
/home
$ itamae local /tmp/foo/sample.rb
INFO : Starting Itamae...
INFO : Recipe: /tmp/foo/sample.rb
/home # Expected: /tmp
The wiki says that cwd
can be specified.
https://github.com/itamae-kitchen/itamae/wiki/local_ruby_block-resource
Is this a bug or did I use it incorrectly?
@mishina2228 Thank you for reporting! I'll check that behavior in a few days...
@mishina2228 Sorry for the late reply.
I'm not deeply investigate yet, the root cause is evaluate "local_ruby_block" by itamae's process, so this working directory is not changed, it's the reason why "cwd" attribute is not work correctly.
This patch fixes the problem but I'm worried about this change might cause a large breaking change...
diff --git a/lib/itamae/resource/local_ruby_block.rb b/lib/itamae/resource/local_ruby_block.rb
index 54c9342..ca880d3 100644
--- a/lib/itamae/resource/local_ruby_block.rb
+++ b/lib/itamae/resource/local_ruby_block.rb
@@ -5,7 +5,9 @@ module Itamae
define_attribute :block, type: Proc
def action_run(options)
- attributes.block.call
+ Dir.chdir(attributes[:cwd]) do
+ attributes.block.call
+ end
end
end
end
@sue445 How do you think?
@unasuke Thank you for your research.
I think I am good to go as long as all existing tests pass.
If you're worried, you could release the gem in rc
version and see what happens.
I found (maybe) the same issue on mitamae.
I couldn't find public itamae recipes that use "local_ruby_block" with "cwd".
https://github.com/search?q=language%3Aruby+local_ruby_block&type=code
So I thought the change is breaking, but not huge impact. (Although I will make rc release)