diff --git a/lib/raven/interfaces/stack_trace.rb b/lib/raven/interfaces/stack_trace.rb index 90b9bdc37..e42807153 100644 --- a/lib/raven/interfaces/stack_trace.rb +++ b/lib/raven/interfaces/stack_trace.rb @@ -35,16 +35,19 @@ class Frame < Interface def filename return nil if self.abs_path.nil? - prefix = $LOAD_PATH.select { |s| self.abs_path.start_with?(s.to_s) }.sort_by { |s| s.to_s.length }.last - if prefix.nil? && Raven.configuration.project_root - project_root = Raven.configuration.project_root.to_s - if self.abs_path.start_with?(project_root) - prefix = project_root - end + prefix = if project_root && self.abs_path.start_with?(project_root) + project_root + else + $LOAD_PATH.select { |s| self.abs_path.start_with?(s.to_s) }.sort_by { |s| s.to_s.length }.last end + prefix ? self.abs_path[prefix.to_s.chomp(File::SEPARATOR).length+1..-1] : self.abs_path end + def project_root + @project_root ||= Raven.configuration.project_root && Raven.configuration.project_root.to_s + end + def to_hash(*args) data = super(*args) data[:filename] = self.filename diff --git a/spec/raven/event_spec.rb b/spec/raven/event_spec.rb index fd0308cdc..01dc05fa9 100644 --- a/spec/raven/event_spec.rb +++ b/spec/raven/event_spec.rb @@ -368,7 +368,6 @@ class SubExc < BaseExc; end end context 'in a rails environment' do - before do rails = double('Rails') allow(rails).to receive(:root) { '/rails/root' } @@ -384,7 +383,7 @@ class SubExc < BaseExc; end allow(e).to receive(:backtrace).and_return([ "/rails/root/vendor/bundle/cache/other_gem.rb:10:in `public_method'", "vendor/bundle/some_gem.rb:10:in `a_method'", - "/rails/root/app/foobar:132:in `new_function'", + "/rails/root/app/models/user.rb:132:in `new_function'", "/gem/lib/path:87:in `a_function'", "/app/some/other/path:1412:in `other_function'", "test/some/other/path:1412:in `other_function'" @@ -401,13 +400,28 @@ class SubExc < BaseExc; end expect(frames[1][:in_app]).to eq(false) expect(frames[2][:filename]).to eq("/gem/lib/path") expect(frames[2][:in_app]).to eq(false) - expect(frames[3][:filename]).to eq("app/foobar") + expect(frames[3][:filename]).to eq("app/models/user.rb") expect(frames[3][:in_app]).to eq(true) expect(frames[4][:filename]).to eq("vendor/bundle/some_gem.rb") expect(frames[4][:in_app]).to eq(false) expect(frames[5][:filename]).to eq("vendor/bundle/cache/other_gem.rb") expect(frames[5][:in_app]).to eq(false) end + + context 'when a path under project_root is on the load path' do + before do + $LOAD_PATH << '/rails/root/app/models' + end + + after do + $LOAD_PATH.delete('/rails/root/app/models') + end + + it "doesn't remove any path information under project_root" do + frames = hash[:exception][:stacktrace][:frames] + expect(frames[3][:filename]).to eq("app/models/user.rb") + end + end end end