Skip to content

Turbocache miss reasoning#1499

Merged
AStaroverov merged 10 commits intomainfrom
feat/turbo-miss-cache-log
Mar 10, 2026
Merged

Turbocache miss reasoning#1499
AStaroverov merged 10 commits intomainfrom
feat/turbo-miss-cache-log

Conversation

@AStaroverov
Copy link
Copy Markdown
Collaborator

No description provided.

@AStaroverov AStaroverov changed the title Turbo miss reasoning Turbocache miss reasoning Mar 10, 2026
@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Mar 10, 2026

🦋 Changeset detected

Latest commit: ba0c532

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 0 packages

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Comment thread scripts/turbo-why.mjs Outdated
Comment thread scripts/turbo-why.mjs Outdated
Comment thread scripts/turbo-why.mjs Outdated
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces a powerful new diagnostic tool, turbo-why, designed to demystify Turbo cache misses by explaining the root causes and cascading rebuilds. By integrating this tool into existing build and test workflows, developers can gain immediate visibility into cache performance. Concurrently, the configuration for Turbo tasks has been streamlined to ensure more consistent and predictable caching behavior across the project.

Highlights

  • New Diagnostic Tool: Introduced a new turbo-why script to diagnose Turbo cache misses, providing detailed insights into why tasks are rebuilt and identifying root causes.
  • CI/Local Workflow Integration: Integrated the turbo-why diagnostic tool into core CI and local development scripts (ci:build, ci:test, etc.) to automatically analyze cache behavior during execution.
  • Turbo.json Configuration Simplification: Simplified and standardized turbo.json task inputs by consistently using $TURBO_DEFAULT$ for various tasks, removing redundant explicit input declarations for improved consistency.
Changelog
  • .changeset/ten-lamps-enter.md
    • Added a new changeset file.
  • package.json
    • Added a new turbo-why script.
    • Updated several CI/local scripts (ci:build, ci:build:local, ci:test, ci:test:local) to utilize the turbo-why script for cache analysis.
  • scripts/turbo-why.mjs
    • Created a new Node.js script that resolves package.json commands, executes turbo --dry-run=json, and analyzes the output to explain cache hits and misses.
  • turbo.json
    • Updated task definitions to simplify input configurations, primarily by replacing explicit input lists with $TURBO_DEFAULT$ for fmt, check, formatter:check, linter:check, types:check, build, test, and do-pack tasks.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@AStaroverov AStaroverov enabled auto-merge March 10, 2026 16:03
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a new script turbo-why to analyze and explain Turborepo cache misses, which is a great addition for debugging CI performance. The script is integrated into various CI jobs in package.json. The PR also includes optimizations in turbo.json to prevent cache invalidation from markdown file changes. My review focuses on the new script. I've found a logical issue in how it resolves script arguments which could lead to incorrect analysis, and a minor code cleanup. Overall, the script is well-structured and will be very useful once the issue is addressed.

Comment thread scripts/turbo-why.mjs
Comment on lines +57 to +113
*/
function resolveScript(scriptName) {
const pkgPath = join(process.cwd(), "package.json");
const pkg = JSON.parse(readFileSync(pkgPath, "utf-8"));
const scripts = pkg.scripts ?? {};

const env = {};
const seen = new Set();

let current = scriptName;
while (true) {
if (seen.has(current)) {
console.error(red(`Circular script reference: ${current}`));
process.exit(1);
}
seen.add(current);

const value = scripts[current];
if (!value) {
console.error(red(`Script "${current}" not found in package.json`));
process.exit(1);
}

// Tokenize the script value, collecting leading KEY=value env vars
const tokens = value.split(/\s+/);
let i = 0;
while (i < tokens.length && /^\w+=\S+$/.test(tokens[i])) {
const [k, ...rest] = tokens[i].split("=");
env[k] = rest.join("=");
i++;
}
const rest = tokens.slice(i);

// Case 1: turbo run <args>
if (rest[0] === "turbo" && rest[1] === "run") {
return { turboArgs: rest.slice(2), env };
}

// Case 2: pnpm <script> [-- extra-args] → follow the chain
if (rest[0] === "pnpm") {
const nextScript = rest[1];
// Collect extra args after `--` (e.g. pnpm test -- --force)
// but ignore `--` separator and args like --output-logs since those
// will be on the turbo level eventually
current = nextScript;
continue;
}

// Case 3: npx turbo run <args>
if (rest[0] === "npx" && rest[1] === "turbo" && rest[2] === "run") {
return { turboArgs: rest.slice(3), env };
}

console.error(red(`Script "${scriptName}" does not resolve to a turbo command.`));
console.error(dim(`Resolved to: ${value}`));
process.exit(1);
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

The current implementation of resolveScript doesn't handle arguments passed to pnpm scripts via --. This can lead to turbo-why analyzing a different command than what would be run, giving misleading results. For example, a script like pnpm test -- --force would be analyzed as if it were just pnpm test, losing the --force argument which is significant for turbo's caching behavior.

To fix this, you should collect these extra arguments and append them to the final turboArgs.

function resolveScript(scriptName) {
  const pkgPath = join(process.cwd(), 'package.json');
  const pkg = JSON.parse(readFileSync(pkgPath, 'utf-8'));
  const scripts = pkg.scripts ?? {};

  const env = {};
  const seen = new Set();
  const extraArgs = [];

  let current = scriptName;
  while (true) {
    if (seen.has(current)) {
      console.error(`${RED}Circular script reference: ${current}${RESET}`);
      process.exit(1);
    }
    seen.add(current);

    const value = scripts[current];
    if (!value) {
      console.error(`${RED}Script "${current}" not found in package.json${RESET}`);
      process.exit(1);
    }

    // Tokenize the script value, collecting leading KEY=value env vars
    const tokens = value.split(/\s+/);
    let i = 0;
    while (i < tokens.length && /^\w+=\S+$/.test(tokens[i])) {
      const [k, ...rest] = tokens[i].split('=');
      env[k] = rest.join('=');
      i++;
    }
    const rest = tokens.slice(i);

    // Case 1: turbo run <args>
    if (rest[0] === 'turbo' && rest[1] === 'run') {
      return { turboArgs: [...rest.slice(2), ...extraArgs], env };
    }

    // Case 2: pnpm <script> [-- extra-args]  →  follow the chain
    if (rest[0] === 'pnpm') {
      const nextScript = rest[1];
      const doubleDashIndex = rest.indexOf('--');
      if (doubleDashIndex !== -1) {
        // pnpm appends arguments after '--'. We use 'unshift' to add args from the
        // outer script to the end of the final command.
        extraArgs.unshift(...rest.slice(doubleDashIndex + 1));
      }
      current = nextScript;
      continue;
    }

    // Case 3: npx turbo run <args>
    if (rest[0] === 'npx' && rest[1] === 'turbo' && rest[2] === 'run') {
      return { turboArgs: [...rest.slice(3), ...extraArgs], env };
    }

    console.error(`${RED}Script "${scriptName}" does not resolve to a turbo command.${RESET}`);
    console.error(`${DIM}Resolved to: ${value}${RESET}`);
    process.exit(1);
  }
}

Comment thread scripts/turbo-why.mjs
// but dry-run reports them as MISS since there are no cached artifacts.
const realTasks = tasks.filter((t) => t.command !== "<NONEXISTENT>");
const skipped = tasks.length - realTasks.length;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

The variable byId is declared and initialized but is never used. It should be removed to improve code clarity.

@AStaroverov AStaroverov added this pull request to the merge queue Mar 10, 2026
Merged via the queue into main with commit 2404ed5 Mar 10, 2026
9 checks passed
@AStaroverov AStaroverov deleted the feat/turbo-miss-cache-log branch March 10, 2026 16:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants