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

Don't fail for Razor: SymbolReference locations for @typeparam are misplaced #8424

Merged
merged 17 commits into from
Nov 30, 2023

Conversation

martin-strecker-sonarsource
Copy link
Contributor

@martin-strecker-sonarsource martin-strecker-sonarsource commented Nov 29, 2023

See #8417

The PR turns the hard failure into a log message.

@martin-strecker-sonarsource martin-strecker-sonarsource changed the title Symbol reference protobuf Don't fail for Razor: SymbolReference locations for @typeparam are misplaced Nov 29, 2023
@martin-strecker-sonarsource martin-strecker-sonarsource marked this pull request as ready for review November 30, 2023 09:20
}
} else if (declarationRange.get().overlap(referenceRange.get())) {
if (LOG.isDebugEnabled()) {
LOG.debug("The declaration token at {} overlaps with the referencing token {} in file {}", declarationRange.get(), referenceRange.get(), file.filename());
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The actual fix.

Copy link
Contributor

Choose a reason for hiding this comment

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

what is the actual fix here? It's hard for me to follow what the difference is from the diff

Copy link
Member

Choose a reason for hiding this comment

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

It's the condition from the if statement: declarationRange.get().overlap(referenceRange.get()). If we add the range directly, the API we use (symbol.newReference) will throw.
So this check ensures that we log a warning (in debug) and continue the import ignoring the second range that is overlaping.

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;

public class RazorImporterTestBase {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

New base class for all tests that interact with "src/test/resources/RazorProtobufImporter" files.

Comment on lines -78 to +73
Tuple.tuple(CoreMetrics.CLASSES, 1),
Tuple.tuple(CoreMetrics.STATEMENTS, 15),
Tuple.tuple(CoreMetrics.FUNCTIONS, 4),
Tuple.tuple(CoreMetrics.COMPLEXITY, 5),
Tuple.tuple(CoreMetrics.FUNCTIONS, 3),
Tuple.tuple(CoreMetrics.COMMENT_LINES, 0),
Tuple.tuple(CoreMetrics.COGNITIVE_COMPLEXITY, 1),
Tuple.tuple(CoreMetrics.NCLOC, 14));
Tuple.tuple(CoreMetrics.CLASSES, 0),
Tuple.tuple(CoreMetrics.NCLOC, 13),
Tuple.tuple(CoreMetrics.STATEMENTS, 6));
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Some values changed, likely because the pd files were created new, and the old ones didn't fit "Cases.razor" or because the old ones were created with .Net 7 SDK and the new ones with .Net 8 SDK.

var referenceRange = toTextRange(file, refTextRange);
if (referenceRange.isEmpty()) {
if (LOG.isDebugEnabled()) {
LOG.debug("The reported token was out of the range. File {}, Range {}", file.filename(), refTextRange);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We lost coverage for that line because in the new pb, there is not "out of range" anymore (either fixed in .Net 8 SDK or fixed somewhere else).

Choose a reason for hiding this comment

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

They fixed the tab problem and we used tabs to reproduce that out-of-range issue.

@martin-strecker-sonarsource martin-strecker-sonarsource added Type: Bug Exceptions and blocking issues during analysis. Area: SQ Plugin Java plugin related issues. labels Nov 30, 2023
Copy link

sonarcloud bot commented Nov 30, 2023

SonarCloud Quality Gate failed.    Quality Gate failed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

73.3% 73.3% Coverage
0.0% 0.0% Duplication

idea Catch issues before they fail your Quality Gate with our IDE extension sonarlint SonarLint

Copy link

sonarcloud bot commented Nov 30, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
No Duplication information No Duplication information

Copy link
Contributor

@sebastien-marichal sebastien-marichal left a comment

Choose a reason for hiding this comment

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

LGTM!

Roslyn version: 4.8.0.0Language version: CSharp12!Concurrent execution: enabled��File 'Microsoft.NET.Sdk.Razor.SourceGenerators\Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator\OverlapSymbolReferences_razor.g.cs' was recognized as razor generated��File 'Microsoft.NET.Sdk.Razor.SourceGenerators\Microsoft.NET.Sdk.Razor.SourceGenerators.RazorSourceGenerator\Cases_razor.g.cs' was recognized as razor generatedvrFile 'C:\Users\martin.strecker\Desktop\WebAss\obj\Debug\net8.0\WebAss.AssemblyInfo.cs' was recognized as generated��File 'C:\Users\martin.strecker\Desktop\WebAss\obj\Debug\net8.0\.NETCoreApp,Version=v8.0.AssemblyAttributes.cs' was recognized as generated
Copy link
Contributor

Choose a reason for hiding this comment

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

double check I understand well: did you recreate these PB files with .NET 8 @martin-strecker-sonarsource ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. I also added the csproj as part of the PR, so it is more easy to recreate the pb files (before, the csproj was missing):

@martin-strecker-sonarsource
Copy link
Contributor Author

Peach validation:
symbol references are still there when resolvable. See ariaPressedValue here:
https://peach.aws-prd.sonarsource.com/code?id=mudblazor&selected=mudblazor%3AMudBlazor%2FComponents%2FButton%2FMudToggleIconButton.razor

Other references work as well. See e.g. the Color property here:
https://peach.aws-prd.sonarsource.com/code?id=mudblazor&selected=mudblazor%3AMudBlazor%2FComponents%2FButton%2FMudFab.razor.cs

@martin-strecker-sonarsource
Copy link
Contributor Author

@andrei-epure-sonarsource

For the second link, Color is in a C# file, right?
It's not in a razor file. Or am I missing something?

It's just a type and I do not know where it is defined. It is highly likely defined either in the project or in the framework. I just wanted to show that symbol references work for the properties in that "normal" C# file:

Island_UyC4nuoEdW

@@ -0,0 +1,6 @@
namespace WebAss;

Choose a reason for hiding this comment

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

The first chars look strange. Is the encoding set correctly on the file?

Copy link
Member

@costin-zaharia-sonarsource costin-zaharia-sonarsource left a comment

Choose a reason for hiding this comment

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

LGTM!

Sorry, Martin, until now I only looked at the general picture which looked good.

I did not a complete review and I've noticed some small improvements. Up to you if you want to takle them or not.

.setMetadata(new FileMetadata(mock(AnalysisWarnings.class)).readMetadata(new FileReader(TEST_FILE)))
.build();
sensorContext.fileSystem().add(inputFile);
public void test_symbol_refs_get_imported_cases() {

Choose a reason for hiding this comment

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

pedantic: there is an empty line at the beginning of the tests that is not needed

public void test_symbol_refs_get_imported_overlapSymbolReferences() {

var inputFile = OverlapSymbolReferencesInputFile;
var sut = new SymbolRefsImporter(sensorContext, s -> Paths.get(s).getFileName().toString());

Choose a reason for hiding this comment

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

    var sut = new SymbolRefsImporter(sensorContext, RazorImporterTestBase::fileName);

Comment on lines +46 to 48
var sut = new SymbolRefsImporter(sensorContext, RazorImporterTestBase::fileName);
sut.accept(protobuf.toPath());
sut.save();

Choose a reason for hiding this comment

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

Creating the sut is duplicated (but slightly different), we should extract a function to create it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: SQ Plugin Java plugin related issues. Type: Bug Exceptions and blocking issues during analysis.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants