jackwener/OpenCLI · error · ArgumentError
crates crate name "${value}" is not a valid crates.io name
Error message
crates crate name "${value}" is not a valid crates.io name What it means
requireCrateName validates crate names against the crates.io naming rule: start with an ASCII letter, then 0-63 chars of letters/digits/underscore/hyphen (regex /^[A-Za-z][A-Za-z0-9_-]{0,63}$/). Values failing this pattern throw this ArgumentError with a hint about the allowed format.
Source
Thrown at clis/crates/utils.js:20
import { ArgumentError, CommandExecutionError, EmptyResultError } from '@jackwener/opencli/errors';
export const CRATES_BASE = 'https://crates.io';
const UA = 'opencli-crates-adapter (+https://github.com/jackwener/opencli)';
// crates.io crate names: 1-64 chars, ascii letters/digits/-_, must start with a letter.
const CRATE_NAME = /^[A-Za-z][A-Za-z0-9_-]{0,63}$/;
export function requireString(value, label) {
const s = String(value ?? '').trim();
if (!s) throw new ArgumentError(`crates ${label} cannot be empty`);
return s;
}
export function requireCrateName(value) {
const s = String(value ?? '').trim();
if (!s) throw new ArgumentError('crates crate name is required (e.g. "serde", "tokio")');
if (!CRATE_NAME.test(s)) {
throw new ArgumentError(
`crates crate name "${value}" is not a valid crates.io name`,
'Names start with an ASCII letter, then 0-63 chars of letters / digits / "_-".',
);
}
return s;
}
export function requireBoundedInt(value, defaultValue, maxValue, label = 'limit') {
const raw = value ?? defaultValue;
const n = typeof raw === 'number' ? raw : Number(raw);
if (!Number.isInteger(n) || n <= 0) {
throw new ArgumentError(`crates ${label} must be a positive integer`);
}
if (n > maxValue) {
throw new ArgumentError(`crates ${label} must be <= ${maxValue}`);
}
return n;
}View on GitHub (pinned to 49907e53dc)
Solutions
- Use only the bare crate name: letters/digits/_/- starting with a letter, max 64 chars.
- Strip URL prefixes and version suffixes: extract the last path segment and remove any '@...' part.
- Look the name up with `crates search` if unsure of the exact spelling.
- Pre-validate with the same regex before calling the API.
Example fix
// before
await cli.crates.crate({ name: 'https://crates.io/crates/serde' });
// after
await cli.crates.crate({ name: 'serde' }); Defensive patterns
Strategy: validation
Validate before calling
const CRATE_NAME = /^[A-Za-z][A-Za-z0-9_-]{0,63}$/;
function sanitizeCrateName(input) {
const s = String(input ?? '').trim();
const bare = s.split('/').pop()?.split('@')[0] ?? ''; // strip URL path and @version
if (!CRATE_NAME.test(bare)) throw new Error(`invalid crates.io name: ${input}`);
return bare;
} Type guard
function isValidCrateName(v) {
return typeof v === 'string' && /^[A-Za-z][A-Za-z0-9_-]{0,63}$/.test(v);
} Try / catch
try {
await cli.crates.crate({ name });
} catch (e) {
if (e instanceof ArgumentError && e.message.includes('not a valid crates.io name')) {
console.error('Names: ASCII letter first, then letters/digits/_/- (max 64 chars). No URLs or @versions.');
} else throw e;
} Prevention
- Strip URL prefixes and @version suffixes before passing a name.
- Reject names starting with digits/symbols early in your own input parsing.
- Copy bare names, not full crates.io URLs.
- Reuse the same regex the adapter uses for consistency.
When it happens
Trigger: Passing a name that starts with a digit or symbol ('2fa'), contains illegal characters ('serde json', 'serde/json', 'serde@1'), exceeds 64 chars, is a full URL ('https://crates.io/crates/serde'), or includes a version suffix ('serde@1.0.0').
Common situations: Pasting a crates.io URL as the name, appending @version like in npm/cargo CLI habits, names with spaces from copy/paste, or internal package names that were never valid on crates.io.
Related errors
- crates ${label} cannot be empty
- crates crate name is required (e.g. "serde", "tokio")
- crates ${label} must be a positive integer
- crates ${label} must be <= ${maxValue}
- packagist package "${value}" must be "<vendor>/<package>"
AI-assisted analysis of jackwener/OpenCLI@49907e53dc (2026-08-29).
Data as JSON: /api/errors/59facf8f74a9d16d.
Report an issue: GitHub.