Mintplex-Labs/anything-llm · error · Error

Invalid folder name.

Error message

Invalid folder name.

What it means

Thrown by moveProcessedDocsToFolder() when normalizePath(folderName) returns an empty string. Because normalizePath already throws on ".."/"."/"/", reaching this line means the name consisted entirely of characters that path.normalize collapses to nothing (e.g. a single space, or only characters stripped upstream). It is the empty-folder-name guard for the document-storage layout that requires a real folder segment.

Source

Thrown at server/utils/files/index.js:711

 * Ensures a target folder exists under the documents storage path and moves
 * processed collector documents into it, updating each document's `location`
 * and `name` in-place. If the folder already exists, documents are merged
 * into it so repeated uploads to the same folder are idempotent.
 *
 * The folder must be a single path segment - see the note below on why.
 * @param {Array<{location: string, name: string}>} documents - documents returned by Collector.processDocument
 * @param {string} folderName - target folder name (e.g. "my-notes")
 * @param {string} basePath - base documents directory (overridable for testing)
 * @returns {string} the normalized folder name the documents were moved into
 * @throws {Error} if the folder name is empty, escapes basePath, or is nested
 */
function moveProcessedDocsToFolder(
  documents = [],
  folderName = "",
  basePath = documentsPath
) {
  const folder = normalizePath(folderName);
  if (!folder) throw new Error("Invalid folder name.");

  // Deliberate: document storage is exactly two segments (`folder/file.json`)
  // and docpath, the embedding pipeline and the vector cache all assume that
  // shape. A nested folder name would produce documents that the file picker
  // (which only enumerates one level below documentsPath) cannot see and that
  // cannot be embedded. /v1/document/upload/:folderName historically accepted
  // a URL-encoded separator here; that is now rejected.
  if (folder.includes("/") || folder.includes("\\"))
    throw new Error("Folder name cannot contain path separators.");

  const targetFolderPath = path.join(basePath, folder);
  if (!isWithin(path.resolve(basePath), path.resolve(targetFolderPath)))
    throw new Error("Invalid folder name.");
  if (!fs.existsSync(targetFolderPath))
    fs.mkdirSync(targetFolderPath, { recursive: true });

  for (const doc of documents) {
    const currentFolder = path.dirname(doc.location);

View on GitHub (pinned to 526360e320)

Solutions

  1. Require a non-empty folder name at the controller layer before calling moveProcessedDocsToFolder.
  2. Trim and length-check the incoming folderName (e.g. reject if !folderName.trim()) and return 400.
  3. If the folder is optional in your flow, default to a generated name (timestamp/uuid) before calling.
  4. Add a unit test asserting empty and whitespace inputs are rejected upstream.

Example fix

// before
await moveProcessedDocsToFolder(docs, folderName);

// after
const name = (folderName || "").trim();
if (!name) throw new UserError("folderName is required", 400);
await moveProcessedDocsToFolder(docs, name);
Defensive patterns

Strategy: validation

Validate before calling

const folder = (folderName || '').trim();
if (!folder) throw new UserError('folderName is required', 400);

Type guard

function isNonEmptyFolderName(v): v is string {
  return typeof v === 'string' && v.trim().length > 0;
}

Try / catch

try {
  await moveProcessedDocsToFolder(docs, folderName);
} catch (e) {
  if (e.message === 'Invalid folder name.') return res.status(400).json({ error: e.message });
  throw e;
}

Prevention

When it happens

Trigger: POST to a document-upload endpoint whose :folderName is "", " ", or whitespace-only; an automation pipeline that passes an undefined variable that stringifies to empty; a CLI tool that forgot to set the target folder argument.

Common situations: Replaying a collector run without a folder argument; a frontend form that allows submit on an empty folder field; a rename/move script iterating over a config map where one entry has no folder key.

Related errors


AI-assisted analysis of Mintplex-Labs/anything-llm@526360e320 (2026-08-13). Data as JSON: /api/errors/a74442917df16d0d. Report an issue: GitHub.