Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* SonarQube Java
* Copyright (C) SonarSource Sàrl
* mailto:info AT sonarsource DOT com
*
* You can redistribute and/or modify this program under the terms of
* the Sonar Source-Available License Version 1, as published by SonarSource Sàrl.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the Sonar Source-Available License for more details.
*
* You should have received a copy of the Sonar Source-Available License
* along with this program; if not, see https://sonarsource.com/license/ssal/
*/
package org.sonar.java.semsitter;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.Locale;
import java.util.Optional;

/**
* The SemSitter server shipped inside this plugin for the performance-validation runners, which are Linux x64 and have
* no server installed. It is used only while the performance-validation benchmark is running. Every other analysis
* keeps the default frontend unless an executable is configured.
*/
final class BundledServer {

static final String RESOURCE = "udg-java-server-x86_64-unknown-linux-musl";
static final String PERFORMANCE_VALIDATION_CONTEXT = "sonar.java.context";

private BundledServer() {
}

static Optional<Path> extract() {
if (!performanceValidation() || !linuxAmd64()) {
return Optional.empty();
}
try (InputStream in = BundledServer.class.getResourceAsStream(RESOURCE)) {
if (in == null) {
return Optional.empty();
}
Path destination = Path.of(System.getProperty("java.io.tmpdir"), RESOURCE);
Path partial = Path.of(destination.toString() + ".part");
Files.copy(in, partial, StandardCopyOption.REPLACE_EXISTING);
Files.move(partial, destination, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.ATOMIC_MOVE);
if (!destination.toFile().setExecutable(true)) {
return Optional.empty();
}
return Optional.of(destination);
} catch (IOException failure) {
return Optional.empty();
}
}

private static boolean performanceValidation() {
return PERFORMANCE_VALIDATION_CONTEXT.equals(System.getProperty("sensor.context.key"));
}

private static boolean linuxAmd64() {
String os = System.getProperty("os.name", "").toLowerCase(Locale.ROOT);
String arch = System.getProperty("os.arch", "").toLowerCase(Locale.ROOT);
return os.contains("linux") && ("amd64".equals(arch) || "x86_64".equals(arch));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@

/**
* Offers the SemSitter frontend when the analysis is allowed to use it. It is allowed when a SemSitter executable is
* known and either {@value #ENABLED_PROPERTY} says so or, when that property is not set, the LaunchDarkly flag
* {@value #FLAG_KEY} is on for the project. Anything that goes wrong while asking leaves the default frontend in use.
* known, from {@value #EXECUTABLE_PROPERTY}, {@value #EXECUTABLE_ENVIRONMENT} or the server bundled for the Linux
* performance-validation runners, and either {@value #ENABLED_PROPERTY} says so or, when that property is not set, the
* LaunchDarkly flag {@value #FLAG_KEY} is on for the project. Anything that goes wrong while asking leaves the flag
* default in use.
*/
public final class SemSitterParserProvider implements JParserProvider {

Expand All @@ -44,7 +46,7 @@
public static final String CLIENT_ID_PROPERTY = "sonar.java.semsitter.launchDarkly.clientSideId";
public static final String EXECUTABLE_ENVIRONMENT = "SEMSITTER_ANALYZER";
public static final String CLIENT_ID_ENVIRONMENT = "SONAR_JAVA_LAUNCHDARKLY_CLIENT_ID";
static final boolean FLAG_DEFAULT = false;
static final boolean FLAG_DEFAULT = true;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Quality: Failure log and Javadoc still say the default frontend is used

Now that FLAG_DEFAULT is true, isFlagOn returns true when LaunchDarkly can't be reached (line 90), so SemSitter is the frontend that runs. The warning on line 89 still says "the default frontend is used", which is the opposite of what happens. The class Javadoc (line 35) also still says "Anything that goes wrong while asking leaves the default frontend in use". Anyone reading the logs or the docs during a ruling or CI run will get the wrong idea about which parser handled the analysis. The test name without_property_nor_client_id_the_default_frontend_stays (test line 111) has the same problem, since with no client id the provider now turns SemSitter on. Fix: build the log message and Javadoc from FLAG_DEFAULT, or reword them to be neutral, and rename the test.

Make the warning match what actually happens, whatever the default value is:

LOG.warn("The SemSitter frontend flag could not be read, it defaults to {}: {}", FLAG_DEFAULT ? "on" : "off", e.getMessage());
  • Apply fix

Check the box to apply the fix or reply for a change | Was this helpful? React with 👍 / 👎


private static final Logger LOG = LoggerFactory.getLogger(SemSitterParserProvider.class);
private static final String PROJECT_KEY_PROPERTY = "sonar.projectKey";
Expand All @@ -54,17 +56,24 @@
private final UnaryOperator<String> environment;
@Nullable
private final DocumentTranslator translator;
private final Supplier<Optional<Path>> bundledServer;
private volatile boolean enabled;
private volatile Path executable;

public SemSitterParserProvider() {
this(LaunchDarklyFeatureFlags::connect, System::getenv, SemSitterTranslator::translate);
this(LaunchDarklyFeatureFlags::connect, System::getenv, SemSitterTranslator::translate, BundledServer::extract);
}

SemSitterParserProvider(FeatureFlags.Connector flags, UnaryOperator<String> environment, @Nullable DocumentTranslator translator) {
this(flags, environment, translator, Optional::empty);
}

SemSitterParserProvider(FeatureFlags.Connector flags, UnaryOperator<String> environment, @Nullable DocumentTranslator translator,
Supplier<Optional<Path>> bundledServer) {
this.flags = flags;
this.environment = environment;
this.translator = translator;
this.bundledServer = bundledServer;
}

@Override
Expand All @@ -73,9 +82,13 @@
executable = null;
Optional<String> path = firstNonBlank(configuration.get(EXECUTABLE_PROPERTY), Optional.ofNullable(environment.apply(EXECUTABLE_ENVIRONMENT)));
if (path.isEmpty()) {
executable = bundledServer.get().orElse(null);
} else {
executable = Path.of(path.get());
}
if (executable == null) {
return;
}
executable = Path.of(path.get());
enabled = configuration.getBoolean(ENABLED_PROPERTY).orElseGet(() -> isFlagOn(configuration));
if (enabled) {
LOG.info("The Java analyzer will use the SemSitter frontend.");
Expand All @@ -96,7 +109,7 @@
}
}

private static Optional<String> firstNonBlank(Optional<String> first, Optional<String> second) {

Check warning on line 112 in java-frontend-semsitter/src/main/java/org/sonar/java/semsitter/SemSitterParserProvider.java

View check run for this annotation

SonarQube-Next / SonarQube Code Analysis

Specify a "String" parameter instead.

[S3553] "Optional" should not be used for parameters See more on https://next.sonarqube.com/sonarqube/project/issues?id=org.sonarsource.java%3Ajava&pullRequest=6548&issues=d313623c-443b-44d7-a7fe-8a39a0cad8d0&open=d313623c-443b-44d7-a7fe-8a39a0cad8d0
return first.filter(value -> !value.isBlank()).or(() -> second.filter(value -> !value.isBlank()));
}

Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* SonarQube Java
* Copyright (C) SonarSource Sàrl
* mailto:info AT sonarsource DOT com
*
* You can redistribute and/or modify this program under the terms of
* the Sonar Source-Available License Version 1, as published by SonarSource Sàrl.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the Sonar Source-Available License for more details.
*
* You should have received a copy of the Sonar Source-Available License
* along with this program; if not, see https://sonarsource.com/license/ssal/
*/
package org.sonar.java.semsitter;

import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Optional;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assumptions.assumeTrue;

class BundledServerTest {

@TempDir
Path project;

@Test
void bundled_server_stays_unused_outside_performance_validation() {
System.clearProperty("sensor.context.key");

assertThat(BundledServer.extract()).isEmpty();
}

@Test
void bundled_linux_server_speaks_the_protocol() throws Exception {
System.setProperty("sensor.context.key", BundledServer.PERFORMANCE_VALIDATION_CONTEXT);
try {
Optional<Path> server = BundledServer.extract();
assumeTrue(server.isPresent());

try (SemSitterClient client = SemSitterClient.start(server.get(), project)) {
assertThat(client).isNotNull();
}
assertThat(Files.isExecutable(server.get())).isTrue();
} finally {
System.clearProperty("sensor.context.key");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.UnaryOperator;
import org.junit.jupiter.api.Test;
import org.sonar.api.config.Configuration;
Expand Down Expand Up @@ -156,13 +157,13 @@ void client_id_can_come_from_the_environment_and_the_project_may_be_unknown() {
}

@Test
void flag_service_failure_keeps_the_default_frontend() {
void flag_service_failure_gives_the_flag_its_default_value() {
SemSitterParserProvider provider = provider(Map.of());
connectionFailure = new IllegalStateException("offline");

provider.configure(settings(Map.of(SemSitterParserProvider.EXECUTABLE_PROPERTY, "/bin/semsitter", SemSitterParserProvider.CLIENT_ID_PROPERTY, "sdk")));

assertThat(provider.isEnabled()).isFalse();
assertThat(provider.isEnabled()).isEqualTo(SemSitterParserProvider.FLAG_DEFAULT);
}

@Test
Expand All @@ -176,6 +177,16 @@ void configuring_again_forgets_the_previous_analysis() {
assertThat(provider.isEnabled()).isFalse();
}

@Test
void bundled_server_enables_the_frontend_without_a_configured_executable() {
SemSitterParserProvider provider = new SemSitterParserProvider(connector, Map.<String, String>of()::get, translator, () -> Optional.of(Path.of("bundled-server")));

provider.configure(settings(Map.of()));

assertThat(provider.isEnabled()).isTrue();
assertThat(connections).isEmpty();
}

@Test
void created_parser_is_the_semsitter_parser_once_a_translator_is_available() {
SemSitterParserProvider provider = new SemSitterParserProvider(connector, Map.<String, String>of()::get, translator);
Expand Down
2 changes: 1 addition & 1 deletion sonar-java-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@
<configuration>
<rules>
<requireFilesSize>
<maxsize>24000000</maxsize>
<maxsize>50000000</maxsize>
<minsize>19000000</minsize>
<files>
<file>${project.build.directory}/${project.build.finalName}.jar</file>
Expand Down
Loading