From a01715f0eef1ba47e8f2bdec92b9726edf882c74 Mon Sep 17 00:00:00 2001 From: Carlo Cabrera <30379873+carlocab@users.noreply.github.com> Date: Sun, 10 Jul 2022 01:27:21 +0800 Subject: [PATCH] resource: support relative paths as `targets` of `#stage` `#stage` takes an optional `target` argument, which specifies the location in which to unpack the `resource`. Passing relative paths to `#stage`, as in resource("foo").stage "bar" does not work, because this calls `Pathname("bar").install` inside the temporary directory that the contents of the resource are unpacked to. This means that passing a relative path to `#stage` has the unintended effect of moving files around inside a temporary directory the formula has no access to. Let's fix that by keeping track of the original working directory before `#stage` was called and prepending this to `target` whenever `target` is a relative path. --- Library/Homebrew/resource.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Library/Homebrew/resource.rb b/Library/Homebrew/resource.rb index fd38ea8c8c..a8e5f712c3 100644 --- a/Library/Homebrew/resource.rb +++ b/Library/Homebrew/resource.rb @@ -121,6 +121,7 @@ class Resource # is a {ResourceStageContext}. # A target or a block must be given, but not both. def unpack(target = nil) + current_working_directory = Pathname.pwd mktemp(download_name) do |staging| downloader.stage do @source_modified_time = downloader.source_modified_time @@ -129,6 +130,7 @@ class Resource yield ResourceStageContext.new(self, staging) elsif target target = Pathname(target) + target = current_working_directory/target if target.relative? target.install Pathname.pwd.children end end