Skip to content

Commit 38a3341

Browse files
committed
Ignore ENOENT when reading file annotations
Errno::ENOENT is occuring intermittently for some users. This can happen even when the file seems to exist at the time. Since we were already returning quietly if a file did not exist, just try to read the file and rescue exceptions, returning silently. Fixes #98
1 parent 39bed6a commit 38a3341

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

lib/code_ownership/private/ownership_mappers/file_annotations.rb

+8-7
Original file line numberDiff line numberDiff line change
@@ -73,19 +73,20 @@ def update_cache(cache, files)
7373

7474
sig { params(filename: String).returns(T.nilable(CodeTeams::Team)) }
7575
def file_annotation_based_owner(filename)
76-
# If for a directory is named with an ownable extension, we need to skip
77-
# so File.foreach doesn't blow up below. This was needed because Cypress
78-
# screenshots are saved to a folder with the test suite filename.
79-
return if File.directory?(filename)
80-
return unless File.file?(filename)
81-
8276
# The annotation should be on line 1 but as of this comment
8377
# there's no linter installed to enforce that. We therefore check the
8478
# first line (the Ruby VM makes a single `read(1)` call for 8KB),
8579
# and if the annotation isn't in the first two lines we assume it
8680
# doesn't exist.
8781

88-
line1 = File.foreach(filename).first
82+
begin
83+
line1 = File.foreach(filename).first
84+
rescue Errno::EISDIR, Errno::ENOENT
85+
# Ignore files that don't exist to avoid an intermittent (harmless?) bug.
86+
# Ignoring directories is needed because Cypress
87+
# screenshots are saved to a folder with the test suite filename.
88+
return
89+
end
8990

9091
return if !line1
9192

spec/lib/code_ownership/private/ownership_mappers/file_annotations_spec.rb

+22
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,28 @@ module CodeOwnership
3737
end
3838

3939
describe '.for_file' do
40+
context 'path is a directory' do
41+
it 'returns nil' do
42+
write_configuration
43+
write_file('config/teams/bar.yml', <<~CONTENTS)
44+
name: Bar
45+
CONTENTS
46+
47+
expect(CodeOwnership.for_file('config/teams')).to be_nil
48+
end
49+
end
50+
51+
context 'path does not exist' do
52+
it 'returns nil' do
53+
write_configuration
54+
write_file('config/teams/bar.yml', <<~CONTENTS)
55+
name: Bar
56+
CONTENTS
57+
58+
expect(CodeOwnership.for_file('config/teams/foo.yml')).to be_nil
59+
end
60+
end
61+
4062
context 'ruby owned file' do
4163
before do
4264
write_configuration

0 commit comments

Comments
 (0)