astral-sh/ruff · error
Could not determine source kind for file: {}
Error message
Could not determine source kind for file: {} What it means
`cargo dev print-tokens` loads the given file through SourceKind::from_path, which only recognizes `.py` and `.pyi` extensions; any other suffix (or no suffix) returns None and this error is raised.
Source
Thrown at crates/ruff_dev/src/print_tokens.rs:22
use anyhow::Result;
use ruff_linter::linter::parse_unchecked_source;
use ruff_linter::source_kind::SourceKind;
use ruff_python_ast::{PySourceType, PythonVersion, SourceType};
#[derive(clap::Args)]
pub(crate) struct Args {
/// Python file for which to generate the AST.
#[arg(required = true)]
file: PathBuf,
}
pub(crate) fn main(args: &Args) -> Result<()> {
let source_type = PySourceType::from(&args.file);
let source_kind = SourceKind::from_path(&args.file, SourceType::Python(source_type))?
.ok_or_else(|| {
anyhow::anyhow!(
"Could not determine source kind for file: {}",
args.file.display()
)
})?;
let parsed = parse_unchecked_source(&source_kind, source_type, PythonVersion::default());
for token in parsed.tokens() {
println!("{token:#?}");
}
Ok(())
}
View on GitHub (pinned to 672bb4edf0)
Solutions
- Copy or rename the file to `.py` (or `.pyi`) and rerun
- Feed the content via a temporary .py file when the original name must stay unchanged
Example fix
# before cargo dev print-tokens tokens # after cp tokens /tmp/tokens.py && cargo dev print-tokens /tmp/tokens.py
Defensive patterns
Strategy: validation
Validate before calling
case "$f" in *.py|*.pyi) cargo dev print-tokens "$f" ;; *) echo "expected a .py or .pyi file: $f" >&2; exit 1 ;; esac
Prevention
- Keep token-inspection inputs as .py files
- For extensionless fixtures, copy to /tmp/<name>.py first
When it happens
Trigger: Running `cargo dev print-tokens lexer.txt` or any path that does not end in .py/.pyi.
Common situations: Tokenizing scratch files without extensions or with renamed suffixes; fixtures saved as .python or .src.
Related errors
- Could not determine source kind for file: {}
- Unknown option: {key}
- The `--range` option is only supported when formatting a sin
- {flag} <reason> cannot contain newline characters
- Unsupported serialization format for statistics: {:?}
AI-assisted analysis of astral-sh/ruff@672bb4edf0 (2026-08-16).
Data as JSON: /api/errors/71ecc7553c5a1753.
Report an issue: GitHub.