itamae-kitchen / itamae

Configuration management tool inspired by Chef, but simpler and lightweight. Formerly known as Lightchef.
https://itamae.kitchen/
MIT License
1.12k stars 125 forks source link

`cwd` does not work in `local_ruby_block` #353

Closed mishina2228 closed 2 years ago

mishina2228 commented 2 years ago

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?

unasuke commented 2 years ago

@mishina2228 Thank you for reporting! I'll check that behavior in a few days...

unasuke commented 2 years ago

@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?

sue445 commented 2 years ago

@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.

unasuke commented 2 years ago

I found (maybe) the same issue on mitamae.

unasuke commented 2 years ago

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)

unasuke commented 2 years ago

I published v1.14.0