Skip to content

Commit

Permalink
Protect against cyclical references
Browse files Browse the repository at this point in the history
  • Loading branch information
dhh committed May 17, 2024
1 parent 624a3fd commit 5cff9da
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
18 changes: 9 additions & 9 deletions lib/propshaft/compiler/css_asset_urls.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ def compile(asset, input)
input.gsub(ASSET_URL_PATTERN) { asset_url resolve_path(asset.logical_path.dirname, $1), asset.logical_path, $2, $1 }
end

def referenced_by(asset)
Set.new.tap do |references|
asset.content.scan(ASSET_URL_PATTERN).each do |referenced_asset_url, _|
referenced_asset = load_path.find(resolve_path(asset.logical_path.dirname, referenced_asset_url))

unless references.include?(referenced_asset)
references << referenced_asset
references.merge referenced_by(referenced_asset)
end
def referenced_by(asset, references: Set.new)
asset.content.scan(ASSET_URL_PATTERN).each do |referenced_asset_url, _|
referenced_asset = load_path.find(resolve_path(asset.logical_path.dirname, referenced_asset_url))

unless references.include?(referenced_asset)
references << referenced_asset
references.merge referenced_by(referenced_asset, references: references)
end
end

references
end

private
Expand Down
2 changes: 1 addition & 1 deletion lib/propshaft/load_path.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def find(asset_name)
end

def find_referenced_by(asset)
compilers.referenced_by(asset)
compilers.referenced_by(asset).delete(self)
end

def assets(content_types: nil)
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/assets/first_path/dependent/c.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@import url('a.css')

p {
color: red;
}

0 comments on commit 5cff9da

Please sign in to comment.