From 5950215f3a0c4790a0fa13b26d5040f60f3eab2c Mon Sep 17 00:00:00 2001 From: Tibor Blenessy Date: Thu, 11 Apr 2024 14:26:50 +0200 Subject: [PATCH] Skip file when there is a failure saving CPD token (#4660) --- .../javascript/bridge/AnalysisProcessor.java | 15 +++++++-------- .../javascript/bridge/AnalysisProcessorTest.java | 3 ++- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/sonar-plugin/sonar-javascript-plugin/src/main/java/org/sonar/plugins/javascript/bridge/AnalysisProcessor.java b/sonar-plugin/sonar-javascript-plugin/src/main/java/org/sonar/plugins/javascript/bridge/AnalysisProcessor.java index 2a15f8b6f70..e87d395dd5d 100644 --- a/sonar-plugin/sonar-javascript-plugin/src/main/java/org/sonar/plugins/javascript/bridge/AnalysisProcessor.java +++ b/sonar-plugin/sonar-javascript-plugin/src/main/java/org/sonar/plugins/javascript/bridge/AnalysisProcessor.java @@ -279,17 +279,16 @@ private void saveCpd(BridgeServer.CpdToken[] cpdTokens) { // even providing empty 'NewCpdTokens' will trigger duplication computation so skipping return; } - NewCpdTokens newCpdTokens = context.newCpdTokens().onFile(file); - for (BridgeServer.CpdToken cpdToken : cpdTokens) { - try { + try { + NewCpdTokens newCpdTokens = context.newCpdTokens().onFile(file); + for (BridgeServer.CpdToken cpdToken : cpdTokens) { newCpdTokens.addToken(cpdToken.location.toTextRange(file), cpdToken.image); - } catch (IllegalArgumentException e) { - LOG.warn("Failed to save CPD token in {} at {}", file.uri(), cpdToken.location); - LOG.warn("Exception cause", e); - // continue processing other tokens } + newCpdTokens.save(); + } catch (IllegalArgumentException e) { + LOG.warn("Failed to save CPD token in {}. File will not be analyzed for duplications.", file.uri()); + LOG.warn("Exception cause", e); } - newCpdTokens.save(); } void saveIssue(BridgeServer.Issue issue) { diff --git a/sonar-plugin/sonar-javascript-plugin/src/test/java/org/sonar/plugins/javascript/bridge/AnalysisProcessorTest.java b/sonar-plugin/sonar-javascript-plugin/src/test/java/org/sonar/plugins/javascript/bridge/AnalysisProcessorTest.java index 8e0782256ef..08c7dc2c12a 100644 --- a/sonar-plugin/sonar-javascript-plugin/src/test/java/org/sonar/plugins/javascript/bridge/AnalysisProcessorTest.java +++ b/sonar-plugin/sonar-javascript-plugin/src/test/java/org/sonar/plugins/javascript/bridge/AnalysisProcessorTest.java @@ -83,7 +83,8 @@ void should_not_fail_when_invalid_cpd() { cpd.location = new BridgeServer.Location(1, 2, 1, 1); // invalid range startCol > endCol response.cpdTokens = new BridgeServer.CpdToken[] { cpd }; processor.processResponse(context, mock(JsTsChecks.class), file, response); + assertThat(context.cpdTokens(file.key())).isNull(); assertThat(logTester.logs()) - .contains("Failed to save CPD token in " + file.uri() + " at 1:2-1:1"); + .contains("Failed to save CPD token in " + file.uri() + ". File will not be analyzed for duplications."); } }