Skip to main content

Why Knip reported Mastra's agent and tool files as unused

Updated 6 min readMarkdown
Why Knip reported Mastra's agent and tool files as unused

Imagine a cleanup tool telling you that your agents, tools and workflows are all unused files you could delete. They aren't. The app loads every one of them when it starts. The tool just couldn't see how.

The cleanup tool is Knip, which finds files, dependencies and exports that a JavaScript or TypeScript project no longer uses. The project was built with Mastra, a TypeScript framework for building agents and workflows. joealden asked for Mastra support in issue #2222, I wrote the plugin, and it was merged in webpro-nl/knip#2227.

How Knip decides a file is unused

Knip starts from a few entry files, the files where your program begins, such as src/index.ts. From each one it follows every import to the next file, then the imports in that file, and so on. Any file it never reaches is reported as unused.

Think of walking through a house from the front door and following the hallways. Every room you can reach is lived in. A room with no hallway leading to it looks abandoned, so Knip writes it down.

That works for most code. It goes wrong when a framework uses a side door: it loads files because of the folder they sit in, not because anything imports them.

Mastra loads files by folder

Mastra does exactly that. When you run mastra dev (its local development server) or mastra build, it looks inside src/mastra and picks files up by name and place. index.ts is the main entry. Files in workflows/ are workflows and files in tools/ are tools. Each folder under agents/ is a file based agent, with files like config.ts, instructions, memory.ts, its own tools/ and schedules/, and even nested subagents/.

Your own code never imports those files. Mastra finds them itself. So without a plugin, Knip saw a row of rooms with no hallway and listed them all as unused.

The issue asked for a plugin like Knip's existing ones for Flue and Eve, with src/mastra/index.ts as the main entry and, ideally, file based agents too.

Reading Mastra's own rules

I didn't want to guess the folder rules from the docs, so I read Mastra's discovery code, the part of its build tool that walks the folder and decides what to load.

Then I tried it on Mastra's example project, examples/agent. It contains a file based agent called weather-fs, and Knip had been reporting 7 of its files as unused. With the plugin, all 7 dropped out of the report.

The first version

A Knip plugin is a small file that says two things: when it should switch on, and which files count as entries. Mine switches on when a project depends on mastra, the command line package, because folder discovery only happens through that tool. A project that only uses the library @mastra/core connects everything with imports, which Knip already follows.

The entries are written as globs. A glob is a file pattern with wildcards: * matches any name inside one folder, and ** matches any depth of folders. My PR description also had one honest line: "Not included: the --dir CLI option".

What review changed

webpro, who maintains Knip, requested changes. The review confirmed that the patterns follow Mastra's discovery code and that all seven weather-fs files were no longer reported. Then came one request before merging: support two command line options, --dir and --tools, right away instead of leaving them for later.

--dir tells Mastra to use another folder instead of src/mastra, as in mastra dev --dir app/mastra. Every folder rule then applies there. webpro also pointed out a catch: mastra start --dir means something different, the folder holding the built output, so only dev and build should count.

--tools, or -t for short, takes a comma separated list of extra tool files, patterns or folders. Mastra bundles each one and loads them all from a tools.mjs file it writes during the build. So a tool listed there is really an entry file, along with anything it imports.

webpro linked the exact lines in Mastra's code for both, and suggested doing it the way Knip's React Email plugin handles its own folder option. So the list of patterns became a function of the folder:

const getProductionPatterns = (dir: string) => {
  const agentRoots = `${dir}/agents/*/*(subagents/*/)`;
  return [
    `${dir}/index.${extensions}`,
    `${dir}/workflows/*.${extensions}`,
    `${dir}/{,agents/*/}tools/**/*.${extensions}`,
    // trimmed: a few more files and the agent files
    `!${dir}/**/*.{test,spec}.${extensions}`,
    `!${dir}/**/__tests__/**`,
  ];
};

Here dir is the Mastra folder and extensions is {js,ts}. Each line matches a kind of file Mastra loads. agentRoots matches an agent folder and also any subagent folder inside it, so one pattern covers both. The two lines starting with ! do the opposite and leave files out: Mastra skips tests, so a forgotten test file should still show up as unused. With no options, the plugin calls this function with src/mastra.

Knip reads the scripts in your package.json. For each mastra command it finds, it calls a handler, trimmed here:

resolveInputs: (parsed, { cwd }) => {
  if (!discoveryCommands.has(parsed._[0])) return [];
  const inputs = [];
  if (parsed.dir) {
    for (const pattern of getProductionPatterns(parsed.dir)) inputs.push(toProductionEntry(pattern));
  }
  // trimmed: the --tools part
  return inputs;
},

discoveryCommands holds only dev and build, so a start command adds nothing. When --dir is there, the same patterns are built for that folder and added as entries. The trimmed part splits --tools on commas and adds each item as an entry; if an item is a folder, its index file is the entry.

Testing both directions

The test runs Knip on a small pretend Mastra project, which tests call a fixture. Its package.json has scripts such as mastra dev --dir app/mastra and mastra build -t lib/tools/*.ts,lib/extra-tools, plus a mastra start --dir .mastra/output that must add nothing.

Then it checks two sides. Files Mastra loads must not be reported: app/mastra/index.ts, a tool of an agent inside app/mastra and the file that tool imports, the tool files under lib/tools and a module one of them imports, and lib/extra-tools/index.ts. Files Mastra doesn't load must still be reported: a helper inside an agent folder that Mastra doesn't pick up, a test file next to a tool, and lib/unused.ts.

A plugin that marked every file as used would pass the first half and hide real leftovers.

webpro approved the update, noting that both options work as expected and that the fixtures cover them, and merged it on 28 October 2026. It shipped in Knip 6.44.0. Thanks, webpro!

What I'm keeping from this one

Read the framework's own discovery code, not only its docs. The patterns came straight from it, and that's exactly what review checked them against.

When an option lives in the same code you're already reading, it's probably part of the job. I had written --dir down as not included, and it was the first thing review asked for.

And a test for a tool that reports unused files needs both lists: what must leave the report, and what must stay in it.

Do you use Knip with a framework that loads files by folder? I'm curious which ones still trip it up.

About the author

  • I contribute to open source developer tools: Tauri, Biome, oxc, Julia, Quarkus, Grafana, Burn and a few more. I start from an issue I can reproduce and a test that fails, then fix it. Here I write up the ones worth telling: what broke, why, and how the fix got through review.

No comments yet. Be the first to comment.