SonarSource/sonarqube · error · NotFoundException

No cache for given branch or pull request

Error message

No cache for given branch or pull request

What it means

ScannerCacheWs GetAction streams the scanner cache for a branch or pull request; when cache.get(branchDto.getUuid()) returns null there is no cached report for that branch/PR, so a NotFoundException is thrown. The branch exists but the compute engine never stored a cache entry for it.

Source

Thrown at server/sonar-webserver-webapi/src/main/java/org/sonar/server/scannercache/ws/GetAction.java:96

    action.createParam(BRANCH)
      .setDescription("Branch key. If not provided, main branch will be used.")
      .setExampleValue(KEY_BRANCH_EXAMPLE_001)
      .setRequired(false);
  }

  @Override
  public void handle(Request request, Response response) throws Exception {
    String projectKey = request.mandatoryParam(PROJECT);
    String branchKey = request.param(BRANCH);

    try (DbSession dbSession = dbClient.openSession(false)) {
      ProjectDto project = componentFinder.getProjectByKey(dbSession, projectKey);
      checkPermission(project);
      BranchDto branchDto = componentFinder.getBranchOrPullRequest(dbSession, project, branchKey, null);

      try (DbInputStream dbInputStream = cache.get(branchDto.getUuid())) {
        if (dbInputStream == null) {
          throw new NotFoundException("No cache for given branch or pull request");
        }

        boolean compressed = requestedCompressedData(request);
        try (OutputStream output = response.stream().output()) {
          if (compressed) {
            response.setHeader("Content-Encoding", "gzip");
            // data is stored compressed
            IOUtils.copy(dbInputStream, output);
          } else {
            try (InputStream uncompressedInput = new GZIPInputStream(dbInputStream)) {
              IOUtils.copy(uncompressedInput, output);
            }
          }
        }
      }
    }
  }

View on GitHub (pinned to 184c821202)

Solutions

  1. Ensure at least one successful analysis of that branch/PR has completed before requesting the cache
  2. Re-check the branch key spelling; a typo may resolve to a different, unanalyzed branch
  3. Retry later if an analysis is still running (the cache is written during report processing)
  4. Handle 404 gracefully in CI: fall back to a full (cold) analysis instead of failing the pipeline

Example fix

// before
curl api/analysis_cache/get?project=my_project&branch=feature-x  # 404 before first analysis
// after (CI)
analyze branch feature-x once; then
curl api/analysis_cache/get?project=my_project&branch=feature-x
Defensive patterns

Strategy: fallback

Validate before calling

// check the branch has been analyzed before fetching cache
const branches = await get(`api/project_branches/list?project=${projectKey}`);
const b = branches.branches.find(x => x.name === branchKey);
if (!b || !b.analysisDate) console.warn("Branch not analyzed yet; cache will be missing");

Type guard

function isCacheAvailable(branch) {
  return Boolean(branch && branch.analysisDate);
}

Try / catch

try {
  return await getStream(`api/analysis_cache/get?project=${p}&branch=${b}`);
} catch (err) {
  if (err.status === 404 && String(err.message).includes("No cache for given branch")) {
    return runFullColdAnalysis(); // fallback
  }
  throw err;
}

Prevention

When it happens

Trigger: Calling api/analysis_cache/get with branch/pullRequest parameters whose UUID has no entry in the scanner cache table, e.g. before the first analysis completed or after cache eviction.

Common situations: Requesting the cache for a brand-new branch that has never been analyzed; CI downloading the cache before the previous analysis finished; cache purged by retention/cleanup jobs; wrong branch name mapping to a fresh branch UUID.

Understand the failure class

Background: 'Could not be found', 'does not exist', 'not found in database': the resource-not-found family when an ID, slug, key, or URI lookup comes back empty — this error's family across 20 libraries.

Related errors


AI-assisted analysis of SonarSource/sonarqube@184c821202 (2026-09-09). Data as JSON: /api/errors/5175c092b39d3492. Report an issue: GitHub.