{"record":{"id":"482c72286dda5f7e","repo":"can1357/oh-my-pi","slug":"directive-invalid-directive","errorCode":null,"errorMessage":"{directive}: invalid directive","messagePattern":"(.+?): invalid directive","errorType":"error_code","errorClass":"StatError","httpStatus":null,"severity":"error","filePath":"crates/pi-builtins/src/stat.rs","lineNumber":140,"sourceCode":"-`%i`: file system ID in hex\n-`%l`: maximum length of filenames\n-`%n`: file name\n-`%s`: block size (for faster transfers)\n-`%S`: fundamental block size (for block counts)\n-`%t`: file system type in hex\n-`%T`: file system type in human readable form\n\nNOTE: your shell may have its own version of stat, which usually supersedes\nthe version described here.  Please refer to your shell's documentation\nfor details about the options it supports.\";\n\n\t#[derive(Debug, Error)]\n\tenum StatError {\n\t\t#[error(\"Invalid quoting style: {style}\")]\n\t\tInvalidQuotingStyle { style: String },\n\t\t#[error(\"missing operand\\nTry 'stat --help' for more information.\")]\n\t\tMissingOperand,\n\t\t#[error(\"{directive}: invalid directive\")]\n\t\tInvalidDirective { directive: String },\n\t\t#[error(\"cannot read table of mounted file systems: {error}\")]\n\t\t#[cfg_attr(not(unix), allow(dead_code, reason = \"mount tables are unix-only\"))]\n\t\tCannotReadFilesystem { error: String },\n\t\t#[error(\"using '-' to denote standard input does not work in file system mode\")]\n\t\t#[cfg_attr(not(unix), allow(dead_code, reason = \"stdin filesystem mode is unix-only\"))]\n\t\tStdinFilesystemMode,\n\t\t#[error(\"cannot read file system information for {file}: {error}\")]\n\t\tCannotReadFilesystemInfo { file: String, error: String },\n\t\t#[error(\"cannot stat {file}: {error}\")]\n\t\tCannotStat { file: String, error: String },\n\t}\n\n\tmod options {\n\t\tpub const DEREFERENCE: &str = \"dereference\";\n\t\tpub const FILE_SYSTEM: &str = \"file-system\";\n\t\tpub const FORMAT: &str = \"format\";\n\t\tpub const PRINTF: &str = \"printf\";","sourceCodeStart":122,"sourceCodeEnd":158,"githubUrl":"https://github.com/can1357/oh-my-pi/blob/969062200754ea02cfac922e5ebb8c608c079e15/crates/pi-builtins/src/stat.rs#L122-L158","documentation":"StatError::InvalidDirective is thrown when the --printf/--format string contains a format directive stat does not recognize. The full offending directive is echoed back so the user can locate it in their format string; this typically means a typo or a directive supported only by another stat implementation.","triggerScenarios":"Calling the stat builtin with a format containing an invalid %-directive, e.g. `stat --printf='%z\\n' file` where %z is not supported, or a malformed sequence like '%Q'.","commonSituations":"Copy-pasting format strings from GNU stat variants with extra directives; typos in directive letters; scripts migrated between macOS/BSD stat and GNU-style stat; hand-edited format templates.","solutions":["Check the directive letter against the stat builtin's documented directive list and fix or remove it.","Read the full message to identify which directive was rejected and correct the format string.","Test the format string incrementally (one directive at a time) to isolate the bad one.","Escape a literal percent as %% if a percent sign was intended rather than a directive."],"exampleFix":"// before\nstat --printf='%z\\n' file.txt   // unsupported directive\n// after\nstat --printf='%s\\n' file.txt   // use a supported directive","handlingStrategy":"validation","validationCode":"// allow-list of supported stat directives; validate the format before use\nconst SUPPORTED = new Set([\"n\", \"s\", \"F\", \"A\", \"a\", \"b\", \"c\", \"d\", \"f\", \"g\", \"i\", \"h\", \"m\", \"t\", \"u\", \"W\", \"X\", \"Y\", \"Z\", \"%\"]);\nconst directives = [...fmt.matchAll(/%(.)|%%/g)].map(m => m[1]);\nconst bad = directives.filter(d => !SUPPORTED.has(d));\nif (bad.length) throw new Error(`unsupported directives: ${bad.join(\"\")}`);","typeGuard":"const isSupportedDirective = (d: string): boolean => SUPPORTED_DIRECTIVES.has(d);","tryCatchPattern":"try {\n  await stat([\"--printf=\" + fmt, file]);\n} catch (err) {\n  const m = String(err).match(/^(.+): invalid directive$/);\n  if (m) {\n    const fixed = fmt.replace(m[1], \"\"); // strip the rejected directive and retry\n    return stat([\"--printf=\" + fixed, file]);\n  } else throw err;\n}","preventionTips":["Check directive letters against the stat builtin's documented set before deploying format strings.","Escape literal percents as %%.","Don't assume GNU-stat directives exist here; verify after porting scripts.","Test --printf formats in CI so unsupported directives fail before production."],"tags":["stat","format-string","argument-validation","usage-error"],"backgroundTag":"invalid-format-directive","analyzedSha":"969062200754ea02cfac922e5ebb8c608c079e15","analyzedAt":"2026-08-31T10:29:35.737Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}