diff --git a/.sampo/changesets/in-app-project-root-boundary.md b/.sampo/changesets/in-app-project-root-boundary.md new file mode 100644 index 000000000..811e91520 --- /dev/null +++ b/.sampo/changesets/in-app-project-root-boundary.md @@ -0,0 +1,5 @@ +--- +pypi/posthog: patch +--- + +`posthog.exception_utils`: only mark frames as `in_app` when their path is inside the project root directory, so sibling directories sharing the root's name as a prefix (e.g. `/work/app-shared` for root `/work/app`) are no longer treated as application code. diff --git a/posthog/exception_utils.py b/posthog/exception_utils.py index da0d1dbcd..066552246 100644 --- a/posthog/exception_utils.py +++ b/posthog/exception_utils.py @@ -1236,11 +1236,10 @@ def _is_in_project_root(abs_path, project_root): if project_root is None: return False - # check if path is in the project root - if abs_path.startswith(project_root): - return True - - return False + # check if path is in the project root, matching whole directory names so + # that sibling directories sharing a prefix (e.g. /app-shared) don't match + root = project_root.rstrip(os.sep) + return abs_path == root or abs_path.startswith(root + os.sep) def _truncate_by_bytes(string, max_bytes): diff --git a/posthog/test/test_exception_utils.py b/posthog/test/test_exception_utils.py index 063194618..625550e6b 100644 --- a/posthog/test/test_exception_utils.py +++ b/posthog/test/test_exception_utils.py @@ -178,6 +178,25 @@ def test_frame_classification_preserves_overrides_and_module_boundaries( assert frame.get("in_app") is expected +@pytest.mark.parametrize( + "project_root, abs_path, expected", + [ + ("/work/app", "/work/app/lib.py", True), + ("/work/app", "/work/app/pkg/mod.py", True), + ("/work/app/", "/work/app/lib.py", True), + ("/work/app", "/work/app-shared/lib.py", None), + ("/work/app", "/work/application/x.py", None), + ("/work/app/", "/work/app-shared/lib.py", None), + ], +) +def test_project_root_match_respects_directory_boundaries( + project_root, abs_path, expected +): + frame = {"abs_path": abs_path} + exception_utils.set_in_app_in_frames([frame], None, None, project_root) + assert frame.get("in_app") is expected + + def test_frame_classification_accepts_missing_options_and_empty_frames(): frame = {"module": "app", "abs_path": "/project/app.py"} exception_utils.set_in_app_in_frames([frame], None, None)