itwanger/toBeBetterJavaer · error · Error

Markdown directory not found: ${target.dir}

Error message

Markdown directory not found: ${target.dir}

What it means

Thrown by syncTarget() in scripts/sync-sidebar.js when the Markdown directory for a target (from --dir or the built-in DEFAULT_TARGETS) does not exist after resolving against the repo root. It fires after the sidebar file check but before any sidebar.ts parsing, and aborts the whole run (not just that target).

Source

Thrown at scripts/sync-sidebar.js:181

    }
  } catch (error) {
    console.error(`sync-sidebar: ${error.message}`);
    process.exitCode = 1;
  }
}

function syncTarget(source, target) {
  const dir = path.resolve(ROOT_DIR, target.dir);
  const report = {
    route: target.route,
    dir: path.relative(ROOT_DIR, dir),
    added: [],
    removed: [],
    warnings: [],
  };

  if (!fs.existsSync(dir)) {
    throw new Error(`Markdown directory not found: ${target.dir}`);
  }

  const files = collectMarkdownSlugs(dir);
  const blockRange = findRouteArray(source, target.route);
  if (!blockRange) {
    throw new Error(`Route not found in sidebar.ts: ${target.route}`);
  }

  let block = source.slice(blockRange.start, blockRange.end + 1);
  const refs = collectRefs(block);
  const listedSlugs = new Set(refs.map((ref) => ref.slug));
  const fileSlugs = new Set(files.map((file) => file.slug));

  const staleSlugs = [...listedSlugs].filter((slug) => !fileSlugs.has(slug));
  if (staleSlugs.length > 0) {
    const removal = removeStaleStringEntries(block, staleSlugs);
    block = removal.block;
    report.removed = removal.removed;

View on GitHub (pinned to 6617f5fd0b)

Solutions

  1. Verify the directory: `ls docs/src/sidebar/itwanger/ai` and fix the --dir value
  2. Give --dir a repo-root-relative path (it is path.resolve(ROOT_DIR, dir))
  3. If the default target is wrong for your fork, always pass explicit --route/--dir for an existing section

Example fix

# before
node scripts/sync-sidebar.js --route=sidebar/itwanger/ai --dir=docs/src/sidebar/itwangr/ai

# after
node scripts/sync-sidebar.js --route=sidebar/itwanger/ai --dir=docs/src/sidebar/itwanger/ai
Defensive patterns

Strategy: validation

Validate before calling

const dir = path.resolve(REPO_ROOT, dirArg);
if (!fs.existsSync(dir) || !fs.statSync(dir).isDirectory()) { console.error(`Directory missing: ${dirArg} (resolves to ${dir})`); process.exit(2); }

Type guard

const isMarkdownDir = (p) => { const d = path.resolve(REPO_ROOT, p); return fs.existsSync(d) && fs.statSync(d).isDirectory(); };

Try / catch

catch (err) { if (err.message.startsWith("Markdown directory not found:")) { console.error("Fix --dir; must be a repo-root-relative directory"); process.exit(2); } throw err; }

Prevention

When it happens

Trigger: `--dir` with a typo'd or moved directory; a fork where docs/src/sidebar/itwanger/ai does not exist so even the default target fails; passing a file instead of a directory (collectMarkdownSlugs expects a directory to walk).

Common situations: Content reorganization moving sidebar content dirs; wrong repo-relative base; new clones of repos without the sidebar content subtree; case mismatch in directory names.

Related errors


AI-assisted analysis of itwanger/toBeBetterJavaer@6617f5fd0b (2026-08-14). Data as JSON: /api/errors/b967f6d1889a6603. Report an issue: GitHub.