-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
feat(code-mappings): Handle Java monorepo source roots in auto-derivation task #112655
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
619ca2e
feat(code-mappings): handle Java monorepo source roots
romtsn d131905
refactor(code-mappings): hide source root overrides behind frame info
romtsn bbb1549
fix(code-mappings): satisfy mypy source root resolver typing
romtsn b43aead
refactor(code-mappings): inline source root override resolver
romtsn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| from collections.abc import Sequence | ||
|
|
||
| SLASH = "/" | ||
| JAVA_SOURCE_ROOT_MARKERS = ("src/main/java/", "src/main/kotlin/") | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As per convo with @romtsn , these are to cover the majority of cases. |
||
|
|
||
|
|
||
| def get_java_source_set_root(source_path: str) -> str | None: | ||
| """Return the repo path through the Java/Kotlin source-set marker. | ||
|
|
||
| Example: | ||
| `module/src/main/java/io/sentry/Foo.java` -> `module/src/main/java/` | ||
| """ | ||
| for marker in JAVA_SOURCE_ROOT_MARKERS: | ||
| prefix, separator, _ = source_path.partition(marker) | ||
| if separator: | ||
| return f"{prefix}{separator}" | ||
|
|
||
| return None | ||
|
|
||
|
|
||
| def find_package_root_relative_to_source_set( | ||
| source_root: str, repo_files: Sequence[str] | ||
| ) -> str | None: | ||
| """Walk a source set until the directory tree stops being a single-child chain. | ||
|
|
||
| Examples: | ||
| `["module/src/main/java/io/sentry/graphql/Foo.java"]` with | ||
| `source_root="module/src/main/java/"` returns `io/sentry/graphql/`. | ||
|
|
||
| `["module/src/main/java/io/sentry/asyncprofiler/jfr/JfrParser.java", | ||
| "module/src/main/java/io/sentry/asyncprofiler/metrics/ProfileMetric.java"]` | ||
| with `source_root="module/src/main/java/"` returns `io/sentry/asyncprofiler/`. | ||
| """ | ||
| relative_paths = [ | ||
| file.removeprefix(source_root) for file in repo_files if file.startswith(source_root) | ||
| ] | ||
| if not relative_paths: | ||
| return None | ||
|
|
||
| package_root = "" | ||
| while True: | ||
| has_file = False | ||
| subdirs: set[str] = set() | ||
|
|
||
| for relative_path in relative_paths: | ||
| if package_root: | ||
| if not relative_path.startswith(package_root): | ||
| continue | ||
| remainder = relative_path[len(package_root) :] | ||
| else: | ||
| remainder = relative_path | ||
|
|
||
| if not remainder: | ||
| continue | ||
|
|
||
| if SLASH not in remainder: | ||
| has_file = True | ||
| break | ||
|
|
||
| subdirs.add(remainder.split(SLASH, 1)[0]) | ||
| if len(subdirs) > 1: | ||
| break | ||
|
|
||
| if has_file or len(subdirs) != 1: | ||
| return package_root | ||
|
|
||
| package_root = f"{package_root}{subdirs.pop()}{SLASH}" | ||
|
|
||
|
|
||
| def find_java_source_roots( | ||
| source_path: str, repo_files: Sequence[str] | None | ||
| ) -> tuple[str, str] | None: | ||
| """Return `(stack_root, source_root)` from a Java/Kotlin repo path. | ||
|
|
||
| Example: | ||
| `sentry-graphql-core/src/main/java/io/sentry/graphql/GraphQLFetcher.java` | ||
| becomes | ||
| `("io/sentry/graphql/", "sentry-graphql-core/src/main/java/io/sentry/graphql/")`. | ||
| """ | ||
| if not repo_files: | ||
| return None | ||
|
|
||
| if not (source_root := get_java_source_set_root(source_path)): | ||
| return None | ||
|
|
||
| if (package_root := find_package_root_relative_to_source_set(source_root, repo_files)) is None: | ||
| return None | ||
|
|
||
| return package_root, f"{source_root}{package_root}" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm glad seeing this file still being a good source for declaring the configuration of each platform 🦾