{"record":{"id":"64c2f3383b41ba14","repo":"vitest-dev/vitest","slug":"shard-count-must-be-a-positive-number","errorCode":null,"errorMessage":"--shard <count> must be a positive number","messagePattern":"--shard <count> must be a positive number","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/vitest/src/node/config/resolveConfig.ts","lineNumber":360,"sourceCode":"  }\n\n  if (viteConfig.base !== '/') {\n    resolved.base = viteConfig.base\n  }\n\n  resolved.clearScreen = resolved.clearScreen ?? viteConfig.clearScreen ?? true\n\n  if (options.shard) {\n    if (resolved.watch) {\n      throw new Error('You cannot use --shard option with enabled watch')\n    }\n\n    const [indexString, countString] = options.shard.split('/')\n    const index = Math.abs(Number.parseInt(indexString, 10))\n    const count = Math.abs(Number.parseInt(countString, 10))\n\n    if (Number.isNaN(count) || count <= 0) {\n      throw new Error('--shard <count> must be a positive number')\n    }\n\n    if (Number.isNaN(index) || index <= 0 || index > count) {\n      throw new Error(\n        '--shard <index> must be a positive number less then <count>',\n      )\n    }\n\n    resolved.shard = { index, count }\n  }\n\n  if (resolved.standalone && !resolved.watch) {\n    throw new Error(`Vitest standalone mode requires --watch`)\n  }\n\n  if (resolved.mergeReports && resolved.watch) {\n    throw new Error(`Cannot merge reports with --watch enabled`)\n  }","sourceCodeStart":342,"sourceCodeEnd":378,"githubUrl":"https://github.com/vitest-dev/vitest/blob/1fa9837ec26533512fdcad8baebf249771bd340a/packages/vitest/src/node/config/resolveConfig.ts#L342-L378","documentation":"Vitest parses the `--shard <index>/<count>` string by splitting on `/` and integer-parsing both halves. The count (the denominator) must be a positive integer; this fires when it is missing, NaN, zero, or negative (the code applies `Math.abs`, so negative still resolves to its magnitude — NaN or 0 is the real trigger).","triggerScenarios":"Passing `--shard 2/`, `--shard 2/0`, `--shard 2/abc`, or `--shard 2` (no slash). Also `--shard 2/-3` resolves to count 3, so this specific error is really about NaN/0.","commonSituations":"CI matrix that builds the shard string from `CI_NODE_INDEX/CODEBUILD_NUM_SHARDS` style vars where the count var is empty; a typo'd shard literal.","solutions":["Format the shard as `<index>/<count>` with a positive integer count, e.g. `1/4`.","Verify the env vars supplying the denominator are set and numeric.","Echo the resolved shard string in your CI log before invoking vitest."],"exampleFix":"# before\nvitest --shard 2/\n# after\nvitest --shard 2/4","handlingStrategy":"validation","validationCode":"function parseShard(raw: string): { index: number, count: number } {\n  const [i, c] = raw.split('/')\n  const index = Number.parseInt(i ?? '', 10)\n  const count = Number.parseInt(c ?? '', 10)\n  if (!Number.isInteger(count) || count <= 0) {\n    throw new Error(`Invalid shard count in \"${raw}\"; expected <index>/<positive count>`)\n  }\n  if (!Number.isInteger(index) || index <= 0 || index > count) {\n    throw new Error(`Invalid shard index in \"${raw}\"`)\n  }\n  return { index, count }\n}","typeGuard":"function isValidShardString(raw: string): boolean {\n  const m = /^(\\d+)\\/(\\d+)$/.exec(raw)\n  if (!m) return false\n  const [, i, c] = m\n  return Number(i) > 0 && Number(c) > 0 && Number(i) <= Number(c)\n}","tryCatchPattern":null,"preventionTips":["Echo the resolved `--shard` value in CI logs before invoking vitest.","Validate shard env vars in a tiny pre-check step in the CI script."],"tags":["cli","shard","validation"],"backgroundTag":null,"analyzedSha":"1fa9837ec26533512fdcad8baebf249771bd340a","analyzedAt":"2026-08-11T16:11:39.638Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}