You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FileIndexer.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2022 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.scanner.scan.filesystem;
  21. import java.io.IOException;
  22. import java.nio.file.LinkOption;
  23. import java.nio.file.Path;
  24. import java.util.Arrays;
  25. import java.util.function.BooleanSupplier;
  26. import javax.annotation.Nullable;
  27. import org.apache.commons.io.FilenameUtils;
  28. import org.sonar.api.CoreProperties;
  29. import org.sonar.api.batch.fs.InputFile;
  30. import org.sonar.api.batch.fs.InputFile.Type;
  31. import org.sonar.api.batch.fs.InputFileFilter;
  32. import org.sonar.api.batch.fs.internal.DefaultIndexedFile;
  33. import org.sonar.api.batch.fs.internal.DefaultInputFile;
  34. import org.sonar.api.batch.fs.internal.DefaultInputModule;
  35. import org.sonar.api.batch.fs.internal.DefaultInputProject;
  36. import org.sonar.api.batch.fs.internal.SensorStrategy;
  37. import org.sonar.api.batch.scm.IgnoreCommand;
  38. import org.sonar.api.notifications.AnalysisWarnings;
  39. import org.sonar.api.utils.MessageException;
  40. import org.sonar.api.utils.log.Logger;
  41. import org.sonar.api.utils.log.Loggers;
  42. import org.sonar.scanner.issue.ignore.scanner.IssueExclusionsLoader;
  43. import org.sonar.scanner.repository.language.Language;
  44. import org.sonar.scanner.scan.ScanProperties;
  45. import org.sonar.scanner.util.ProgressReport;
  46. import static java.lang.String.format;
  47. /**
  48. * Index input files into {@link InputComponentStore}.
  49. */
  50. public class FileIndexer {
  51. private static final Logger LOG = Loggers.get(FileIndexer.class);
  52. private final AnalysisWarnings analysisWarnings;
  53. private final ScanProperties properties;
  54. private final InputFileFilter[] filters;
  55. private final ProjectExclusionFilters projectExclusionFilters;
  56. private final ProjectCoverageAndDuplicationExclusions projectCoverageAndDuplicationExclusions;
  57. private final IssueExclusionsLoader issueExclusionsLoader;
  58. private final MetadataGenerator metadataGenerator;
  59. private final DefaultInputProject project;
  60. private final ScannerComponentIdGenerator scannerComponentIdGenerator;
  61. private final InputComponentStore componentStore;
  62. private final SensorStrategy sensorStrategy;
  63. private final LanguageDetection langDetection;
  64. private boolean warnInclusionsAlreadyLogged;
  65. private boolean warnExclusionsAlreadyLogged;
  66. private boolean warnCoverageExclusionsAlreadyLogged;
  67. private boolean warnDuplicationExclusionsAlreadyLogged;
  68. public FileIndexer(DefaultInputProject project, ScannerComponentIdGenerator scannerComponentIdGenerator, InputComponentStore componentStore,
  69. ProjectExclusionFilters projectExclusionFilters, ProjectCoverageAndDuplicationExclusions projectCoverageAndDuplicationExclusions, IssueExclusionsLoader issueExclusionsLoader,
  70. MetadataGenerator metadataGenerator, SensorStrategy sensorStrategy, LanguageDetection languageDetection, AnalysisWarnings analysisWarnings, ScanProperties properties,
  71. InputFileFilter[] filters) {
  72. this.project = project;
  73. this.scannerComponentIdGenerator = scannerComponentIdGenerator;
  74. this.componentStore = componentStore;
  75. this.projectCoverageAndDuplicationExclusions = projectCoverageAndDuplicationExclusions;
  76. this.issueExclusionsLoader = issueExclusionsLoader;
  77. this.metadataGenerator = metadataGenerator;
  78. this.sensorStrategy = sensorStrategy;
  79. this.langDetection = languageDetection;
  80. this.analysisWarnings = analysisWarnings;
  81. this.properties = properties;
  82. this.filters = filters;
  83. this.projectExclusionFilters = projectExclusionFilters;
  84. }
  85. void indexFile(DefaultInputModule module, ModuleExclusionFilters moduleExclusionFilters, ModuleCoverageAndDuplicationExclusions moduleCoverageAndDuplicationExclusions,
  86. Path sourceFile, Type type, ProgressReport progressReport, ProjectFileIndexer.ExclusionCounter exclusionCounter, @Nullable IgnoreCommand ignoreCommand)
  87. throws IOException {
  88. // get case of real file without resolving link
  89. Path realAbsoluteFile = sourceFile.toRealPath(LinkOption.NOFOLLOW_LINKS).toAbsolutePath().normalize();
  90. Path projectRelativePath = project.getBaseDir().relativize(realAbsoluteFile);
  91. Path moduleRelativePath = module.getBaseDir().relativize(realAbsoluteFile);
  92. boolean included = evaluateInclusionsFilters(moduleExclusionFilters, realAbsoluteFile, projectRelativePath, moduleRelativePath, type);
  93. if (!included) {
  94. exclusionCounter.increaseByPatternsCount();
  95. return;
  96. }
  97. boolean excluded = evaluateExclusionsFilters(moduleExclusionFilters, realAbsoluteFile, projectRelativePath, moduleRelativePath, type);
  98. if (excluded) {
  99. exclusionCounter.increaseByPatternsCount();
  100. return;
  101. }
  102. if (!realAbsoluteFile.startsWith(project.getBaseDir())) {
  103. LOG.warn("File '{}' is ignored. It is not located in project basedir '{}'.", realAbsoluteFile.toAbsolutePath(), project.getBaseDir());
  104. return;
  105. }
  106. if (!realAbsoluteFile.startsWith(module.getBaseDir())) {
  107. LOG.warn("File '{}' is ignored. It is not located in module basedir '{}'.", realAbsoluteFile.toAbsolutePath(), module.getBaseDir());
  108. return;
  109. }
  110. Language language = langDetection.language(realAbsoluteFile, projectRelativePath);
  111. if (ignoreCommand != null && ignoreCommand.isIgnored(realAbsoluteFile)) {
  112. LOG.debug("File '{}' is excluded by the scm ignore settings.", realAbsoluteFile);
  113. exclusionCounter.increaseByScmCount();
  114. return;
  115. }
  116. DefaultIndexedFile indexedFile = new DefaultIndexedFile(realAbsoluteFile, project.key(),
  117. projectRelativePath.toString(),
  118. moduleRelativePath.toString(),
  119. type, language != null ? language.key() : null, scannerComponentIdGenerator.getAsInt(), sensorStrategy);
  120. DefaultInputFile inputFile = new DefaultInputFile(indexedFile, f -> metadataGenerator.setMetadata(module.key(), f, module.getEncoding()));
  121. if (language != null && language.isPublishAllFiles()) {
  122. inputFile.setPublished(true);
  123. }
  124. if (!accept(inputFile)) {
  125. return;
  126. }
  127. checkIfAlreadyIndexed(inputFile);
  128. componentStore.put(module.key(), inputFile);
  129. issueExclusionsLoader.addMulticriteriaPatterns(inputFile);
  130. String langStr = inputFile.language() != null ? format("with language '%s'", inputFile.language()) : "with no language";
  131. LOG.debug("'{}' indexed {}{}", projectRelativePath, type == Type.TEST ? "as test " : "", langStr);
  132. evaluateCoverageExclusions(moduleCoverageAndDuplicationExclusions, inputFile);
  133. evaluateDuplicationExclusions(moduleCoverageAndDuplicationExclusions, inputFile);
  134. if (properties.preloadFileMetadata()) {
  135. inputFile.checkMetadata();
  136. }
  137. int count = componentStore.inputFiles().size();
  138. progressReport.message(count + " " + pluralizeFiles(count) + " indexed... (last one was " + inputFile.getProjectRelativePath() + ")");
  139. }
  140. private boolean evaluateInclusionsFilters(ModuleExclusionFilters moduleExclusionFilters, Path realAbsoluteFile, Path projectRelativePath, Path moduleRelativePath,
  141. InputFile.Type type) {
  142. if (!Arrays.equals(moduleExclusionFilters.getInclusionsConfig(type), projectExclusionFilters.getInclusionsConfig(type))) {
  143. // Module specific configuration
  144. return moduleExclusionFilters.isIncluded(realAbsoluteFile, moduleRelativePath, type);
  145. }
  146. boolean includedByProjectConfiguration = projectExclusionFilters.isIncluded(realAbsoluteFile, projectRelativePath, type);
  147. if (includedByProjectConfiguration) {
  148. return true;
  149. } else if (moduleExclusionFilters.isIncluded(realAbsoluteFile, moduleRelativePath, type)) {
  150. warnOnce(
  151. type == Type.MAIN ? CoreProperties.PROJECT_INCLUSIONS_PROPERTY : CoreProperties.PROJECT_TEST_INCLUSIONS_PROPERTY,
  152. FilenameUtils.normalize(projectRelativePath.toString(), true), () -> warnInclusionsAlreadyLogged, () -> warnInclusionsAlreadyLogged = true);
  153. return true;
  154. }
  155. return false;
  156. }
  157. private boolean evaluateExclusionsFilters(ModuleExclusionFilters moduleExclusionFilters, Path realAbsoluteFile, Path projectRelativePath, Path moduleRelativePath,
  158. InputFile.Type type) {
  159. if (!Arrays.equals(moduleExclusionFilters.getExclusionsConfig(type), projectExclusionFilters.getExclusionsConfig(type))) {
  160. // Module specific configuration
  161. return moduleExclusionFilters.isExcluded(realAbsoluteFile, moduleRelativePath, type);
  162. }
  163. boolean includedByProjectConfiguration = projectExclusionFilters.isExcluded(realAbsoluteFile, projectRelativePath, type);
  164. if (includedByProjectConfiguration) {
  165. return true;
  166. } else if (moduleExclusionFilters.isExcluded(realAbsoluteFile, moduleRelativePath, type)) {
  167. warnOnce(
  168. type == Type.MAIN ? CoreProperties.PROJECT_EXCLUSIONS_PROPERTY : CoreProperties.PROJECT_TEST_EXCLUSIONS_PROPERTY,
  169. FilenameUtils.normalize(projectRelativePath.toString(), true), () -> warnExclusionsAlreadyLogged, () -> warnExclusionsAlreadyLogged = true);
  170. return true;
  171. }
  172. return false;
  173. }
  174. private void checkIfAlreadyIndexed(DefaultInputFile inputFile) {
  175. if (componentStore.inputFile(inputFile.getProjectRelativePath()) != null) {
  176. throw MessageException.of("File " + inputFile + " can't be indexed twice. Please check that inclusion/exclusion patterns produce "
  177. + "disjoint sets for main and test files");
  178. }
  179. }
  180. private void evaluateCoverageExclusions(ModuleCoverageAndDuplicationExclusions moduleCoverageAndDuplicationExclusions, DefaultInputFile inputFile) {
  181. boolean excludedForCoverage = isExcludedForCoverage(moduleCoverageAndDuplicationExclusions, inputFile);
  182. inputFile.setExcludedForCoverage(excludedForCoverage);
  183. if (excludedForCoverage) {
  184. LOG.debug("File {} excluded for coverage", inputFile);
  185. }
  186. }
  187. private boolean isExcludedForCoverage(ModuleCoverageAndDuplicationExclusions moduleCoverageAndDuplicationExclusions, DefaultInputFile inputFile) {
  188. if (!Arrays.equals(moduleCoverageAndDuplicationExclusions.getCoverageExclusionConfig(), projectCoverageAndDuplicationExclusions.getCoverageExclusionConfig())) {
  189. // Module specific configuration
  190. return moduleCoverageAndDuplicationExclusions.isExcludedForCoverage(inputFile);
  191. }
  192. boolean excludedByProjectConfiguration = projectCoverageAndDuplicationExclusions.isExcludedForCoverage(inputFile);
  193. if (excludedByProjectConfiguration) {
  194. return true;
  195. } else if (moduleCoverageAndDuplicationExclusions.isExcludedForCoverage(inputFile)) {
  196. warnOnce(CoreProperties.PROJECT_COVERAGE_EXCLUSIONS_PROPERTY, inputFile.getProjectRelativePath(), () -> warnCoverageExclusionsAlreadyLogged,
  197. () -> warnCoverageExclusionsAlreadyLogged = true);
  198. return true;
  199. }
  200. return false;
  201. }
  202. private void evaluateDuplicationExclusions(ModuleCoverageAndDuplicationExclusions moduleCoverageAndDuplicationExclusions, DefaultInputFile inputFile) {
  203. boolean excludedForDuplications = isExcludedForDuplications(moduleCoverageAndDuplicationExclusions, inputFile);
  204. inputFile.setExcludedForDuplication(excludedForDuplications);
  205. if (excludedForDuplications) {
  206. LOG.debug("File {} excluded for duplication", inputFile);
  207. }
  208. }
  209. private boolean isExcludedForDuplications(ModuleCoverageAndDuplicationExclusions moduleCoverageAndDuplicationExclusions, DefaultInputFile inputFile) {
  210. if (!Arrays.equals(moduleCoverageAndDuplicationExclusions.getDuplicationExclusionConfig(), projectCoverageAndDuplicationExclusions.getDuplicationExclusionConfig())) {
  211. // Module specific configuration
  212. return moduleCoverageAndDuplicationExclusions.isExcludedForDuplication(inputFile);
  213. }
  214. boolean excludedByProjectConfiguration = projectCoverageAndDuplicationExclusions.isExcludedForDuplication(inputFile);
  215. if (excludedByProjectConfiguration) {
  216. return true;
  217. } else if (moduleCoverageAndDuplicationExclusions.isExcludedForDuplication(inputFile)) {
  218. warnOnce(CoreProperties.CPD_EXCLUSIONS, inputFile.getProjectRelativePath(), () -> warnDuplicationExclusionsAlreadyLogged,
  219. () -> warnDuplicationExclusionsAlreadyLogged = true);
  220. return true;
  221. }
  222. return false;
  223. }
  224. private void warnOnce(String propKey, String filePath, BooleanSupplier alreadyLoggedGetter, Runnable markAsLogged) {
  225. if (!alreadyLoggedGetter.getAsBoolean()) {
  226. String msg = "Specifying module-relative paths at project level in the property '" + propKey + "' is deprecated. " +
  227. "To continue matching files like '" + filePath + "', update this property so that patterns refer to project-relative paths.";
  228. LOG.warn(msg);
  229. analysisWarnings.addUnique(msg);
  230. markAsLogged.run();
  231. }
  232. }
  233. private boolean accept(InputFile indexedFile) {
  234. // InputFileFilter extensions. Might trigger generation of metadata
  235. for (InputFileFilter filter : filters) {
  236. if (!filter.accept(indexedFile)) {
  237. LOG.debug("'{}' excluded by {}", indexedFile, filter.getClass().getName());
  238. return false;
  239. }
  240. }
  241. return true;
  242. }
  243. private static String pluralizeFiles(int count) {
  244. return count == 1 ? "file" : "files";
  245. }
  246. }