Skip to content

Include source file path in failed ParseResult when parsing via SourceRoot #4786#4874

Merged
jlerbsc merged 2 commits intojavaparser:masterfrom
JIN-RUI-LIU:sourcePath
Oct 21, 2025
Merged

Include source file path in failed ParseResult when parsing via SourceRoot #4786#4874
jlerbsc merged 2 commits intojavaparser:masterfrom
JIN-RUI-LIU:sourcePath

Conversation

@JIN-RUI-LIU
Copy link
Copy Markdown
Contributor

Fixes #4786.
Problem: Failed parses from SourceRoot.tryToParse() don’t indicate which file failed (ParseResult has Problems but no file path).
Change: ParseResult now carries an internal sourcePath set by SourceRoot; on failures, ParseResult.toString() includes “for ”. Successful parses still set CompilationUnit storage as before.
Tests: Added issues/Issue4786Test to reproduce the “file content is only ‘\u’” failure and assert diagnostics include the absolute file path.
Compatibility: Backward compatible.

@jlerbsc
Copy link
Copy Markdown
Collaborator

jlerbsc commented Oct 18, 2025

I am waiting for you to make the other changes before committing your work.

@JIN-RUI-LIU
Copy link
Copy Markdown
Contributor Author

I am waiting for you to make the other changes before committing your work.

Thank you very much for your guidance!
I’ve updated all the changes you suggested.
Could you please let me know if there’s anything else that needs improvement?

@jlerbsc
Copy link
Copy Markdown
Collaborator

jlerbsc commented Oct 18, 2025

Can you squash your commits to make it more readable? Thank you.

@JIN-RUI-LIU
Copy link
Copy Markdown
Contributor Author

Can you squash your commits to make it more readable? Thank you.

Thanks for the reminder! I’ve already squashed all the commits into one commit.

@jlerbsc
Copy link
Copy Markdown
Collaborator

jlerbsc commented Oct 18, 2025

Now what happens if parsing fails? The issue mainly concerned cases where parsing failed, because in the other case the path is already available via cu.getStorage().getPath().

@JIN-RUI-LIU
Copy link
Copy Markdown
Contributor Author

Now what happens if parsing fails? The issue mainly concerned cases where parsing failed, because in the other case the path is already available via cu.getStorage().getPath().

In my first attempt I added a test resource whose content was a single backslash-u ("\u"). The goal was to deterministically trigger a scanner failure so that ParseResult would be unsuccessful and result() would be empty (as issue #4786 ). This let me verify that getSourcePath is present on failed parses.
The figure below shows the case when parsing fails. We can see that the path
image

Based on your feedback, I removed the custom “\u” fixture and now use an existing Java file under the utils test resources to trigger the failure.

@codecov
Copy link
Copy Markdown

codecov bot commented Oct 19, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 58.333%. Comparing base (d1de37b) to head (9852c2b).
⚠️ Report is 6 commits behind head on master.

Additional details and impacted files

Impacted file tree graph

@@               Coverage Diff               @@
##              master     #4874       +/-   ##
===============================================
+ Coverage     58.330%   58.333%   +0.003%     
  Complexity      2535      2535               
===============================================
  Files            677       677               
  Lines          39038     39041        +3     
  Branches        7088      7089        +1     
===============================================
+ Hits           22771     22774        +3     
  Misses         13368     13368               
  Partials        2899      2899               
Flag Coverage Δ
AlsoSlowTests 58.333% <100.000%> (+0.003%) ⬆️
javaparser-core 58.333% <100.000%> (+0.003%) ⬆️
javaparser-symbol-solver 58.333% <100.000%> (+0.003%) ⬆️
jdk-10 57.898% <100.000%> (+0.003%) ⬆️
jdk-11 57.894% <100.000%> (+<0.001%) ⬆️
jdk-12 57.897% <100.000%> (+0.003%) ⬆️
jdk-13 57.897% <100.000%> (+0.003%) ⬆️
jdk-14 58.135% <100.000%> (+0.005%) ⬆️
jdk-15 58.135% <100.000%> (+0.003%) ⬆️
jdk-16 58.107% <100.000%> (+<0.001%) ⬆️
jdk-17 58.263% <100.000%> (+0.003%) ⬆️
jdk-18 58.263% <100.000%> (+0.003%) ⬆️
jdk-8 57.897% <100.000%> (+0.005%) ⬆️
jdk-9 57.892% <100.000%> (+<0.001%) ⬆️
macos-latest 58.325% <100.000%> (+0.003%) ⬆️
ubuntu-latest 58.320% <100.000%> (+0.003%) ⬆️
windows-latest 58.315% <100.000%> (+0.003%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
...rc/main/java/com/github/javaparser/JavaParser.java 87.804% <100.000%> (+0.150%) ⬆️
...c/main/java/com/github/javaparser/ParseResult.java 96.153% <100.000%> (+1.153%) ⬆️
...n/java/com/github/javaparser/utils/SourceRoot.java 58.673% <100.000%> (-0.827%) ⬇️

Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a191911...9852c2b. Read the comment docs.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@JIN-RUI-LIU JIN-RUI-LIU force-pushed the sourcePath branch 2 times, most recently from 64f317d to cb4a02e Compare October 19, 2025 10:41
- SourceRoot populates sourcePath for file-based parsing paths

- ParseResult adds getSourcePath(); toString includes the path on failure for better diagnostics

- Tests: SourceRootTest : getSourcePathTest and sourcePathToStringTest

- Formatting: run spotless to satisfy CI.
@JIN-RUI-LIU
Copy link
Copy Markdown
Contributor Author

JIN-RUI-LIU commented Oct 19, 2025

I added sourcePathToStringTest to cover ParseResult.toString() with and without sourcePath, improving branch coverage and guarding against regressions. And I ran Spotless locally.
image
image

@jlerbsc
Copy link
Copy Markdown
Collaborator

jlerbsc commented Oct 19, 2025

On second thought, I wonder if it wouldn't be better to set the source path in the JavaParser class at the point where methods take a file or path as a parameter.

@JIN-RUI-LIU
Copy link
Copy Markdown
Contributor Author

On second thought, I wonder if it wouldn't be better to set the source path in the JavaParser class at the point where methods take a file or path as a parameter.

Thanks for the suggestion. To confirm:
Do you mean that we should still generate a result even if parsing fails? (In this case, the result is null, and I would create a new one.) I'm not sure if that could have any side effects. If that’s what you intend, please let me know — I’ll create a new branch to handle it.

image

@JIN-RUI-LIU
Copy link
Copy Markdown
Contributor Author

JIN-RUI-LIU commented Oct 20, 2025

On second thought, I wonder if it wouldn't be better to set the source path in the JavaParser class at the point where methods take a file or path as a parameter.

I thought about it again — or do you mean setting the sourcePath here, so that only the files that failed to parse would have their sourcePath set? Please let me know if that’s what you mean, and I’ll give it a try.
image

@jlerbsc
Copy link
Copy Markdown
Collaborator

jlerbsc commented Oct 20, 2025

On second thought, I wonder if it wouldn't be better to set the source path in the JavaParser class at the point where methods take a file or path as a parameter.

Thanks for the suggestion. To confirm: Do you mean that we should still generate a result even if parsing fails? (In this case, the result is null, and I would create a new one.) I'm not sure if that could have any side effects. If that’s what you intend, please let me know — I’ll create a new branch to handle it.

image

In practice, ParseResult is instantiated even in the event of a parsing error.

@jlerbsc
Copy link
Copy Markdown
Collaborator

jlerbsc commented Oct 20, 2025

On second thought, I wonder if it wouldn't be better to set the source path in the JavaParser class at the point where methods take a file or path as a parameter.

I thought about it again — or do you mean setting the sourcePath here, so that only the files that failed to parse would have their sourcePath set? Please let me know if that’s what you mean, and I’ll give it a try. image

If the parsing runs correctly, the compilation unit already knows the path of the parsed file (storage). However, in the event of an error, there is no information because the compilation unit is not instantiated. So in this case, the path should be stored in the ParseResult that is returned.
Furthermore, JavaParser declares parsing methods for which the provider is instantiated. It is therefore preferable to use these methods in the SourceRoot class.

@JIN-RUI-LIU
Copy link
Copy Markdown
Contributor Author

On second thought, I wonder if it wouldn't be better to set the source path in the JavaParser class at the point where methods take a file or path as a parameter.

I thought about it again — or do you mean setting the sourcePath here, so that only the files that failed to parse would have their sourcePath set? Please let me know if that’s what you mean, and I’ll give it a try. image

If the parsing runs correctly, the compilation unit already knows the path of the parsed file (storage). However, in the event of an error, there is no information because the compilation unit is not instantiated. So in this case, the path should be stored in the ParseResult that is returned. Furthermore, JavaParser declares parsing methods for which the provider is instantiated. It is therefore preferable to use these methods in the SourceRoot class.

Thank you for your guidance; I’ll give it a try.

@jlerbsc
Copy link
Copy Markdown
Collaborator

jlerbsc commented Oct 20, 2025

Why did you close this PR? Just make sure to squash your commits and isolate improvements to other classes in a specific commit.

@JIN-RUI-LIU
Copy link
Copy Markdown
Contributor Author

JIN-RUI-LIU commented Oct 20, 2025

On second thought, I wonder if it wouldn't be better to set the source path in the JavaParser class at the point where methods take a file or path as a parameter.

Sorry to bother you again, but I’m a bit confused about this.
You suggested setting the source path in JavaParser.
However, SourceRoot.tryToParse() internally uses the Provider entry point:
image
in JavaParser.
Since the Provider doesn’t expose the file path, it seems there’s no way to retrieve it inside JavaParser.parse().
Please correct me if I’m missing something — thank you!

@JIN-RUI-LIU JIN-RUI-LIU reopened this Oct 20, 2025
@JIN-RUI-LIU
Copy link
Copy Markdown
Contributor Author

JIN-RUI-LIU commented Oct 20, 2025

Here is my attempt to move setSourcePath into JavaParser. Since SourceRoot.tryToParse() calls the overload that also instantiates the provider, we cannot retrieve the path at the JavaParser layer. That means we would need to switch the parse() overload used inside SourceRoot.tryToParse(), but this does not seem reliable.

Here is SourceRoot.tryToParse() . I’m a bit worried that replacing the .parse() call with another method might cause some unintended side effects.

image

Here is what I changed to setSourcePath in JavaParser, but this method is Deprecated.

image image

@jlerbsc
Copy link
Copy Markdown
Collaborator

jlerbsc commented Oct 21, 2025

The method that defines character encoding is deprecated because the input character encoding must be a configuration option so you can use the method that does not define the encoding.

…rser by changing the method in SourceRoot.tryToParse.
@jlerbsc jlerbsc merged commit 184b05a into javaparser:master Oct 21, 2025
35 checks passed
@jlerbsc jlerbsc added this to the next release milestone Oct 21, 2025
@jlerbsc jlerbsc added the PR: Fixed A PR that offers a fix or correction label Oct 21, 2025
@jlerbsc
Copy link
Copy Markdown
Collaborator

jlerbsc commented Oct 21, 2025

Thank you for your contribution.

@JIN-RUI-LIU
Copy link
Copy Markdown
Contributor Author

Thank you for your contribution.

Thank you for your guidance. I’ve learned a lot from this task.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

PR: Fixed A PR that offers a fix or correction

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Failed ParseResult needs reference to the source file

2 participants