{"record":{"id":"1527c8a457d4ed3f","repo":"bevyengine/bevy","slug":"failed-to-parse-index-as-integer","errorCode":null,"errorMessage":"failed to parse index as integer","messagePattern":"failed to parse index as integer","errorType":"exception","errorClass":"ParseError","httpStatus":null,"severity":"error","filePath":"crates/bevy_reflect/src/path/parse.rs","lineNumber":24,"sourceCode":"use thiserror::Error;\n\nuse super::{Access, ReflectPathError};\n\n/// An error that occurs when parsing reflect path strings.\n#[derive(Debug, PartialEq, Eq, Error)]\n#[error(transparent)]\npub struct ParseError<'a>(Error<'a>);\n\n/// A parse error for a path string.\n#[derive(Debug, PartialEq, Eq, Error)]\nenum Error<'a> {\n    #[error(\"expected an identifier, but reached end of path string\")]\n    NoIdent,\n\n    #[error(\"expected an identifier, got '{0}' instead\")]\n    ExpectedIdent(Token<'a>),\n\n    #[error(\"failed to parse index as integer\")]\n    InvalidIndex(#[from] ParseIntError),\n\n    #[error(\"a '[' wasn't closed, reached end of path string before finding a ']'\")]\n    Unclosed,\n\n    #[error(\"a '[' wasn't closed properly, got '{0}' instead\")]\n    BadClose(Token<'a>),\n\n    #[error(\"a ']' was found before an opening '['\")]\n    CloseBeforeOpen,\n}\n\npub(super) struct PathParser<'a> {\n    path: &'a str,\n    remaining: &'a [u8],\n}\n\nimpl<'a> PathParser<'a> {","sourceCodeStart":6,"sourceCodeEnd":42,"githubUrl":"https://github.com/bevyengine/bevy/blob/396ca727080776bd313bb892423b7d94e03b81b4/crates/bevy_reflect/src/path/parse.rs#L6-L42","documentation":"This nested ParseError variant InvalidIndex wraps a core ParseIntError: the text inside '[]' in a reflect path could not be parsed as an integer index (usize). It is raised by the path tokenizer when a bracketed segment is present but its contents are not a valid integer literal.","triggerScenarios":"Paths like \"items[-1]\" (negative index), \"items[1.0]\" or \"items[x]\" (non-integer), or \"items[99999999999999999999999]\" (overflow past usize::MAX) — each fails integer parsing.","commonSituations":"Users or tools expressing negative/backwards indexing, float indices, or variable names inside brackets; machine-generated paths that embed unvalidated numbers; 32/64-bit overflow from large external ids used as indices.","solutions":["Use a plain non-negative usize literal inside brackets (\"items[3]\").","Clamp/validate generated indices to the 0..len range and usize width before formatting them into paths.","For lookups by key rather than position, restructure to a map or use field access instead of bracket indices.","Log the failing path and regenerate it once the offending segment is fixed."],"exampleFix":"// before\nlet v = entity.path::<f32>(\"items[-1]\"); // InvalidIndex: invalid digit\n\n// after\nlet v = entity.path::<f32>(\"items[2]\");","handlingStrategy":"try-catch","validationCode":"fn bracket_indices_ok(p: &str) -> bool {\n    p.split(&['.', '[', ']']).filter(|s| !s.is_empty()).all(|s| s.parse::<usize>().is_ok() || s.chars().next().is_some_and(|c| c.is_alphabetic() || c == '_'))\n}","typeGuard":null,"tryCatchPattern":"match entity.reflect_path(path_str) {\n    Ok(el) => { /* ... */ }\n    Err(ReflectPathError::ParseError { offset, path, error }) => {\n        warn!(\"index in '{path}' at offset {offset} is not a usize: {error}\");\n    }\n    Err(other) => warn!(\"path failed: {other}\"),\n}","preventionTips":["Only put non-negative usize literals inside '[]'; negative, float, or name keys are invalid.","When generating paths from numbers, validate 0 <= idx <= usize::MAX and format with {} on a usize."],"tags":["rust","bevy","bevy-reflect","reflect-path","parse-error","invalid-index"],"backgroundTag":"invalid-path-syntax","analyzedSha":"396ca727080776bd313bb892423b7d94e03b81b4","analyzedAt":"2026-08-20T16:12:39.808Z","contentChangedAt":"2026-08-20T16:12:39.808Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}