diff --git a/API.md b/API.md index 34ef8df..6e2b968 100644 --- a/API.md +++ b/API.md @@ -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. diff --git a/CHANGELOG.md b/CHANGELOG.md index 4cdbc21..cc67542 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/archunit/cli/baseline_identity.rb b/lib/archunit/cli/baseline_identity.rb index 569de55..cf0b353 100644 --- a/lib/archunit/cli/baseline_identity.rb +++ b/lib/archunit/cli/baseline_identity.rb @@ -2,6 +2,7 @@ require_relative '../testing/violation_factory' require_relative 'method_signature_payload' +require_relative 'named_call_payload' require 'json' module ArchUnit @@ -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) diff --git a/lib/archunit/cli/named_call_payload.rb b/lib/archunit/cli/named_call_payload.rb new file mode 100644 index 0000000..9d1cf61 --- /dev/null +++ b/lib/archunit/cli/named_call_payload.rb @@ -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 diff --git a/lib/archunit/cli/violation_payload.rb b/lib/archunit/cli/violation_payload.rb index e00701e..9aa5e14 100644 --- a/lib/archunit/cli/violation_payload.rb +++ b/lib/archunit/cli/violation_payload.rb @@ -2,6 +2,7 @@ require_relative '../testing/violation_factory' require_relative 'method_signature_payload' +require_relative 'named_call_payload' module ArchUnit module CLI @@ -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 diff --git a/lib/archunit/extraction/named_call_visitor.rb b/lib/archunit/extraction/named_call_visitor.rb index 91cbb77..8cd4d66 100644 --- a/lib/archunit/extraction/named_call_visitor.rb +++ b/lib/archunit/extraction/named_call_visitor.rb @@ -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 ) diff --git a/lib/archunit/files/fluentapi/named_call_condition.rb b/lib/archunit/files/fluentapi/named_call_condition.rb index f8a0384..54a5294 100644 --- a/lib/archunit/files/fluentapi/named_call_condition.rb +++ b/lib/archunit/files/fluentapi/named_call_condition.rb @@ -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) diff --git a/lib/archunit/testing/named_call_formatter.rb b/lib/archunit/testing/named_call_formatter.rb index d672b1e..c0a1ba6 100644 --- a/lib/archunit/testing/named_call_formatter.rb +++ b/lib/archunit/testing/named_call_formatter.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true require_relative 'test_violation' +require_relative '../common/identifier_text' module ArchUnit module Testing @@ -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 diff --git a/scripts/check_named_calls.rb b/scripts/check_named_calls.rb index 7ffe913..b957dbb 100644 --- a/scripts/check_named_calls.rb +++ b/scripts/check_named_calls.rb @@ -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.' diff --git a/spec/cli/named_calls_spec.rb b/spec/cli/named_calls_spec.rb index b1d05c5..6a25a3c 100644 --- a/spec/cli/named_calls_spec.rb +++ b/spec/cli/named_calls_spec.rb @@ -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 diff --git a/spec/files/fluentapi/named_call_condition_spec.rb b/spec/files/fluentapi/named_call_condition_spec.rb index d7d5692..79a18b7 100644 --- a/spec/files/fluentapi/named_call_condition_spec.rb +++ b/spec/files/fluentapi/named_call_condition_spec.rb @@ -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