Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(code_mappings): Files that do not match the package name should not match #40892

Merged
merged 4 commits into from Nov 2, 2022

Conversation

armenzg
Copy link
Member

@armenzg armenzg commented Nov 2, 2022

For instance, raven/base.py should not think that the file apostello/views/base.py should be the related source file.

There's also some other minor tests added.

self.dir_path = ""
self.file_name = self.file_and_dir_path
else:
self.root = ""
self.dir_path = ""
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was a bug and it is now tested via test_frame_filename_* tests.

@@ -123,6 +125,16 @@ def _find_code_mapping(self, frame_filename: FrameFilename) -> Union[CodeMapping

return _code_mappings[0]

def _get_code_mapping_source_path(self, src_file: str, frame_filename: FrameFilename) -> str:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This adds the NotImplementedError functionality.

src/sentry/integrations/utils/code_mapping.py Outdated Show resolved Hide resolved
e.g. ssl.py -> raise NotImplemented
"""
if frame_filename.dir_path != "":
return src_file.rsplit(frame_filename.dir_path)[0].rstrip("/")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the original logic. See down below:

- source_path=matched_files[0].rsplit(frame_filename.dir_path)[0].rstrip("/"),

@@ -140,8 +152,9 @@ def _generate_code_mapping_from_tree(
CodeMapping(
repo=self.trees[repo_full_name].repo,
stacktrace_root=frame_filename.root, # sentry
# e.g. src/sentry/identity/oauth2.py -> src/sentry
source_path=matched_files[0].rsplit(frame_filename.dir_path)[0].rstrip("/"),
source_path=self._get_code_mapping_source_path(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using _get_code_mapping_source_path to raise NotImplementedError.
There's logic in _potential_match to prevent matching, however, it is safer to prevent issues during future refactorings.

assert cm == []

def test_no_support_for_toplevel_files(self):
file_name = "base.py"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want to test all the functions that could potential try to handle a single filename and make sure that they all would prevent trying to process it by mistakes.

This is to prevent that refactorings would allow for this case scenario.

# - "src/sentry_plugins/slack/client.py",
# - "src/sentry/integrations/slack/client.py",
"sentry_plugins/slack/client.py",
]
code_mappings = self.code_mapping_helper.generate_code_mappings(stacktraces)
assert code_mappings == [
CodeMapping(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fix to prevent false positives gives us this new match when before we wouldn't.

]

def test_no_stacktraces_to_process(self):
code_mappings = self.code_mapping_helper.generate_code_mappings([])
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want to make sure that an empty list would still work.

]

def test_no_stacktraces_to_process(self):
code_mappings = self.code_mapping_helper.generate_code_mappings([])
assert code_mappings == []

def test_more_than_one_match_works_when_code_mapping_excludes_other_match(self):
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test doesn't make sense but it may prevent some issues in the future.

assert code_mappings == self.expected_code_mappings
# Asserting lists with different order of elements would fail, thus, checking indexes
assert code_mappings[0] == self.expected_code_mappings[1]
assert code_mappings[1] == self.expected_code_mappings[0]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before it worked because the Reprocessing of stacktraces would allow matching sentry_plugins after the sentry code mappings would be generated.

We could make the call of generate_code_mappings to always return code mappings in the same order. I think this is fine.

@armenzg armenzg marked this pull request as ready for review November 2, 2022 15:01
@armenzg armenzg requested review from a team, snigdhas and asottile-sentry November 2, 2022 15:01
Co-authored-by: Snigdha Sharma <16563948+snigdhas@users.noreply.github.com>
@armenzg armenzg enabled auto-merge (squash) November 2, 2022 17:04
@armenzg armenzg merged commit 43b71c1 into master Nov 2, 2022
@armenzg armenzg deleted the armenzg/dcm/fix-bug branch November 2, 2022 18:25
@github-actions github-actions bot locked and limited conversation to collaborators Nov 18, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Scope: Backend Automatically applied to PRs that change backend components
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants