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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
from directory children in a pruned iterative source walker, preserving sorted graph inputs.
- Fail closed on unreadable/disappearing included directory listings instead of inheriting Find's
silent listing-error skip. Excluded trees are still pruned without opening their descendants.
- Refuse ambiguous POSIX source paths containing a literal backslash rather than checking a
different file or silently passing. Ordinary Windows separators remain supported.
- Extend the existing extraction benchmark with allocation counts, full graph/evidence digests,
and excluded-tree workloads; retain generous CI timing ceilings.

Expand Down
3 changes: 3 additions & 0 deletions GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ ArchUnit.project_files('/workspace/my_app/Gemfile')
Source discovery skips symlinked files and directories, including project-local aliases. This
avoids duplicate file identities and unintentionally parsing linked source outside the project.
Direct file-information extraction also checks the canonical source path against the project root.
On POSIX a literal backslash is legal in a name, but cannot be represented as an ArchUnit file
identifier. Rename that project root, or rename or exclude the affected source. Discovery reports
an error rather than silently analyzing a different file.

### Project-wide exclusions

Expand Down
3 changes: 2 additions & 1 deletion lib/archunit/extraction/enumerate_source_files.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require_relative '../error/technical_error'
require_relative '../error/user_error'
require_relative 'arch_ignore'
require_relative 'source_identifier'
require_relative 'source_walker'
require_relative '../common/fluentapi/check_options'

Expand Down Expand Up @@ -109,7 +110,7 @@ def source_root(value)
root = Pathname.new(value).expand_path
raise UserError, 'project_root must be an existing directory' unless root.directory?

root.realpath
Pathname.new(SourceIdentifier.root(root.realpath))
rescue SystemCallError => e
raise TechnicalError, "could not enumerate Ruby source files: #{e.message}"
end
Expand Down
8 changes: 2 additions & 6 deletions lib/archunit/extraction/locate_project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require 'pathname'
require_relative '../error/technical_error'
require_relative '../error/user_error'
require_relative 'source_identifier'

module ArchUnit
# Ruby-specific project discovery and source extraction.
Expand All @@ -12,7 +13,7 @@ module Extraction
def locate_project(locator = nil, working_directory: Dir.pwd)
working_root = existing_directory(working_directory, :working_directory)
root = project_root(locator, working_root)
normalize_path(root.realpath)
SourceIdentifier.root(root.realpath)
rescue UserError, TechnicalError
raise
rescue SystemCallError => e
Expand Down Expand Up @@ -71,10 +72,5 @@ def pathname(value, attribute)
Pathname.new(value)
end
private_class_method :pathname

def normalize_path(path)
path.to_s.tr('\\', '/')
end
private_class_method :normalize_path
end
end
24 changes: 24 additions & 0 deletions lib/archunit/extraction/source_identifier.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

require_relative '../error/technical_error'

module ArchUnit
module Extraction
# A native path component must never be reinterpreted as two project components.
# @api private
module SourceIdentifier
def self.validate(path)
return path unless path.include?('\\')

raise TechnicalError, "cannot represent Ruby source path #{path.inspect}: " \
'literal backslash; rename the path or exclude the source'
end

def self.root(path)
native = path.to_s
File::ALT_SEPARATOR ? native.tr(File::ALT_SEPARATOR, '/') : validate(native)
end
end
private_constant :SourceIdentifier
end
end
4 changes: 2 additions & 2 deletions lib/archunit/extraction/source_walker.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require_relative 'source_pattern_matcher'
require_relative 'source_identifier'

module ArchUnit
module Extraction
Expand Down Expand Up @@ -53,15 +54,14 @@ def source_kind(path, name)
end

def child_identifier(prefix, name)
name = name.tr('\\', '/')
prefix.empty? ? name : "#{prefix}/#{name}"
end

def collect_path(kind, path, identifier, pending, files)
if kind == :directory
pending << [path, identifier]
else
files << identifier.freeze
files << SourceIdentifier.validate(identifier).freeze
end
end
end
Expand Down
10 changes: 10 additions & 0 deletions spec/extraction/locate_project_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ def normalized(path)
path.realpath.to_s.tr('\\', '/')
end

it 'does not substitute a different directory for a POSIX root containing a backslash' do
skip 'backslash is a native path separator' if File::ALT_SEPARATOR

original = create_file('one\\two/Gemfile').dirname
create_file('one/two/Gemfile')
expect { described_class.locate_project(original) }.to raise_error(ArchUnit::TechnicalError)
expect { described_class.enumerate_source_files(original) }
.to raise_error(ArchUnit::TechnicalError, /literal backslash/)
end

it 'uses an explicit directory even when it has no project marker' do
project = @temporary_root.join('explicit-project')
project.mkdir
Expand Down
22 changes: 22 additions & 0 deletions spec/extraction/source_walker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,28 @@ def legacy_excluded?(patterns, relative, name)
expect(ArchUnit::Extraction.enumerate_source_files(@root)).to eq(['lib/kept.rb'])
end

it 'never reinterprets a literal backslash in a directory entry as a file separator' do
name = 'actual\\name.rb'
allow(Dir).to receive(:children).with(@root.to_s, any_args).and_return([name])
source = instance_double(File::Stat, symlink?: false, directory?: false, file?: true)
allow(File).to receive(:lstat).and_call_original
allow(File).to receive(:lstat).with(File.join(@root, name)).and_return(source)
expect { ArchUnit::Extraction.enumerate_source_files(@root, exclude_patterns: ['actual/**']) }
.to raise_error(ArchUnit::TechnicalError, /literal backslash/)
expect(ArchUnit::Extraction.enumerate_source_files(@root, exclude_patterns: ['*.rb'])).to eq([])
end

it 'rejects two native POSIX paths that would otherwise identify the same Ruby file' do
skip 'backslash is a native path separator' if File::ALT_SEPARATOR

write_source('normal/nested/item.rb')
write_source('normal\\nested/item.rb')
expect { ArchUnit::Extraction.extract_graph(@root) }
.to raise_error(ArchUnit::TechnicalError, /literal backslash/)
expect(ArchUnit::Extraction.enumerate_source_files(@root, exclude_patterns: ['*nested']))
.to be_empty
end

[Errno::EACCES, Errno::ENOENT, Errno::ENOTDIR].each do |error|
it "fails closed when an included directory listing raises #{error}" do
write_source('lib/source.rb')
Expand Down