Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,11 @@ The `source_calls` analysis scope has no dependency counts. A baseline matches o
file, method name, receiver form and safe-navigation flag, not receiver identity or line number.
It accepts at most the previously recorded number of matching calls.

For non-ASCII method names, pass a String or Symbol in the source identifier's encoding. JSON
adds `name_encoding` and hexadecimal `name_bytes` alongside the display name for these calls.
Baselines created before the encoding fix for non-ASCII calls need review and regeneration:
the previous identities did not distinguish encodings. ASCII baselines are unaffected.

Named-call rules share a cached source inventory per project and effective source exclusions. They
don't change or reuse the dependency graph: a mixed graph and call suite has separate source passes.
Use `clear_cache: true` or `ArchUnit.clear_graph_cache` after source edits in a long-lived process.
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

- Preserve Ruby identifier encodings in named-call rules and baseline identities. Binary-encoded
calls no longer crash unrelated rules. Review and regenerate older non-ASCII named-call baseline
entries; existing ASCII fingerprints remain valid.

## 1.2.0 - 2026-11-28

Additive release over 1.1.0. Existing import-only checks and exhaustive cycle reporting remain
Expand Down
7 changes: 5 additions & 2 deletions lib/archunit/cli/baseline_identity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require_relative '../testing/violation_factory'
require_relative 'method_signature_payload'
require_relative 'named_call_payload'
require 'json'

module ArchUnit
Expand Down Expand Up @@ -56,8 +57,10 @@ def self.pattern(value)

def self.named_call(value)
call = value.call
{ 'file' => identifier(call.file), 'name' => call.name, 'receiver' => call.receiver.to_s,
'safe_navigation' => call.safe_navigation }
fields = NamedCallPayload.fields(call.name).transform_keys(&:to_s)
{ 'file' => identifier(call.file) }.merge(
fields, 'receiver' => call.receiver.to_s, 'safe_navigation' => call.safe_navigation
)
end

def self.method_signature(value)
Expand Down
23 changes: 23 additions & 0 deletions lib/archunit/cli/named_call_payload.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

require_relative '../common/identifier_text'

module ArchUnit
module CLI
# Lossless evidence and identity without changing the existing ASCII payload.
# @api private
module NamedCallPayload
def self.fields(name)
return { name: } if name.ascii_only?

# Tag every non-ASCII name, including UTF-8. Old baselines could not
# distinguish a UTF-8 identifier from the transcoding of another name.
{
name: Common::IdentifierText.utf8(name),
name_encoding: name.encoding.name,
name_bytes: name.unpack1('H*')
}
end
end
end
end
3 changes: 2 additions & 1 deletion lib/archunit/cli/violation_payload.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require_relative '../testing/violation_factory'
require_relative 'method_signature_payload'
require_relative 'named_call_payload'

module ArchUnit
module CLI
Expand All @@ -22,7 +23,7 @@ def self.add_evidence(result, violation)
when Files::Assertion::CycleViolation
result[:cycle_reporting] = 'witness' if violation.witness?
when Files::Assertion::NamedCallViolation
result[:call] = violation.call.to_h
result[:call] = violation.call.to_h.merge(NamedCallPayload.fields(violation.call.name))
when Classes::Assertion::MethodSignatureViolation
result[:method_signature] = MethodSignaturePayload.build(violation)
when Classes::Assertion::EmptyClassSelectionViolation
Expand Down
2 changes: 1 addition & 1 deletion lib/archunit/extraction/named_call_visitor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def visit_call_node(node)
def call_site(node)
location = node.message_loc
Common::Extraction::CallSite.new(
file: @file, name: node.name.to_s.encode(Encoding::UTF_8).freeze,
file: @file, name: node.name.to_s.freeze,
receiver: receiver_form(node.receiver), safe_navigation: node.safe_navigation?,
line: location.start_line, column: location.start_column + 1
)
Expand Down
9 changes: 6 additions & 3 deletions lib/archunit/files/fluentapi/named_call_condition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,16 @@ def immutable_names(names)
end

def method_name(value)
if (value.is_a?(String) || value.is_a?(Symbol)) && !value.to_s.strip.empty?
return value.to_s.encode(Encoding::UTF_8).freeze
end
name = value.to_s if value.is_a?(String) || value.is_a?(Symbol)
return name.dup.freeze if valid_name?(name)

raise ArgumentError, 'method names must be non-blank Strings or Symbols'
end

def valid_name?(name)
name&.encoding&.ascii_compatible? && name.valid_encoding? && !name.strip.empty?
end

def perform_check(options)
inventory = ArchUnit::Extraction.extract_named_calls(project_locator, options:)
paths = FileRuleSupport.selected_paths(inventory.keys, filters)
Expand Down
3 changes: 2 additions & 1 deletion lib/archunit/testing/named_call_formatter.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require_relative 'test_violation'
require_relative '../common/identifier_text'

module ArchUnit
module Testing
Expand All @@ -11,7 +12,7 @@ module NamedCallFormatter
def named_call(violation)
call = violation.call
TestViolation.new(
message: "Forbidden method call '#{call.name}'",
message: "Forbidden method call '#{Common::IdentifierText.utf8(call.name)}'",
details: "#{call.file}:#{call.line}:#{call.column} (#{call.receiver} receiver)."
)
end
Expand Down
2 changes: 2 additions & 0 deletions scripts/check_named_calls.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,7 @@ def check_named_call(root, expected_status:)
abort 'missing named-call evidence' unless evidence == ['delete_all', 1, 7]
file.write("store.read\nraise 'never execute target'")
check_named_call(root, expected_status: 0)
file.binwrite("# encoding: binary\nstore.r\xE9sumer\n".b)
check_named_call(root, expected_status: 0)
end
puts 'Named call rule passed the CLI red-to-green smoke without executing target code.'
53 changes: 53 additions & 0 deletions spec/cli/named_calls_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,57 @@ def check(command = 'check', *args)
'kind' => 'source_calls', 'status' => 'error'
)
end

it 'rejects accepted debt when the same spelling becomes a distinct Ruby identifier' do
name = "r\u00E9sumer"
@root.join('.archunit.rb').write(<<~'RUBY')
rule('read only') do
name = "r\u00E9sumer"
project_files.should_not.contain_calls_named([name, name.encode('ISO-8859-1')])
end
RUBY
@source.binwrite("store.#{name}\n")
expect(check('baseline', '--output', @debt.to_s).first).to eq(0)
expect(check('check', '--baseline', @debt.to_s).first).to eq(0)
@source.binwrite("# encoding: iso-8859-1\nstore.#{name}\n".encode('ISO-8859-1'))
code, report = check('check', '--baseline', @debt.to_s)
expect(code).to eq(1)
expect(report.fetch('baseline')).to include('accepted_count' => 0, 'new_count' => 1,
'stale_count' => 1)
expect(report.dig('rules', 0, 'violations', 0, 'call')).to include(
'name' => name, 'name_encoding' => 'ISO-8859-1', 'name_bytes' => '72e973756d6572'
)
end

it 'serializes a binary identifier without emitting invalid UTF-8' do
@root.join('.archunit.rb').write(<<~'RUBY')
rule('read only') { project_files.should_not.contain_calls_named("r\xE9sumer".b) }
RUBY
@source.binwrite("# encoding: binary\nstore.r\xE9sumer\n".b)
code, report = check
expect(code).to eq(1)
violation = report.dig('rules', 0, 'violations', 0)
expect(violation.fetch('message')).to include('r\xE9sumer')
expect(violation.fetch('call')).to include(
'name_encoding' => 'ASCII-8BIT', 'name_bytes' => '72e973756d6572'
)
end

it 'does not silently trust old non-ASCII baseline entries that lacked encoding identity' do
@root.join('.archunit.rb').write(<<~'RUBY')
rule('read only') { project_files.should_not.contain_calls_named("r\u00E9sumer") }
RUBY
@source.write("store.r\u00E9sumer")
expect(check('baseline', '--output', @debt.to_s).first).to eq(0)
old_document = JSON.parse(@debt.read)
entry = old_document.fetch('entries').first
entry['data'] = entry.fetch('data').except('name_encoding', 'name_bytes')
entry['fingerprint'] = ArchUnit::CLI::BaselineFingerprint.digest(entry)
@debt.write(JSON.generate(old_document))

code, report = check('check', '--baseline', @debt.to_s)
expect(code).to eq(1)
expect(report.fetch('baseline')).to include('accepted_count' => 0, 'new_count' => 1,
'stale_count' => 1)
end
end
19 changes: 19 additions & 0 deletions spec/files/fluentapi/named_call_condition_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,29 @@ def self.run(store)
expect(condition.names).to be_frozen
expect { rule([]) }.to raise_error(ArgumentError, /at least one/)
expect { rule(' ') }.to raise_error(ArgumentError, /method names/)
expect { rule("\xFF") }.to raise_error(ArgumentError, /method names/)
expect { rule('read', :bare) }.to raise_error(ArgumentError, /NamedCallOptions/)
expect { ArchUnit::NamedCallOptions.new(receiver: :self) }.to raise_error(ArgumentError)
end

it 'matches Ruby identifiers exactly across source encodings' do
utf8 = "r\u00E9sumer"
latin1 = utf8.encode('ISO-8859-1')
write('queries/utf8.rb', "store.#{utf8}")
source = "# encoding: iso-8859-1\nstore.#{utf8}".encode('ISO-8859-1')
@root.join('queries/latin1.rb').binwrite(source)
expect(rule(utf8).check.map { |hit| hit.call.file }).to eq(['queries/utf8.rb'])
expect(rule(latin1.to_sym).check.map { |hit| hit.call.file }).to eq(['queries/latin1.rb'])
end

it 'does not block unrelated checks on valid binary-encoded source' do
write('queries/source.rb', "# encoding: binary\nstore.r\xE9sumer\n".b)
expect(rule).to pass
result = rule("r\xE9sumer".b).check
expect(result.map { |hit| hit.call.name }).to eq(["r\xE9sumer".b])
expect(ArchUnit.format_violations(result)).to include('r\xE9sumer')
end

it 'reuses parsing across rules and refreshes facts with clear_cache or global clearing' do
write('queries/search.rb', 'store.delete_all')
expect(Prism).to receive(:parse).exactly(3).times.and_call_original
Expand Down