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-ast` 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_ast.rs:22
use anyhow::Result;
use ruff_linter::source_kind::SourceKind;
use ruff_python_ast::{PySourceType, SourceType};
use ruff_python_parser::{ParseOptions, parse};
#[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 python_ast =
parse(source_kind.source_code(), ParseOptions::from(source_type))?.into_syntax();
println!("{python_ast:#?}");
Ok(())
}
View on GitHub (pinned to 672bb4edf0)
Solutions
- Copy or rename the file to a `.py` (or `.pyi` for stubs) extension and rerun
- If you need the AST of unusual inputs, pipe the source through a temporary .py file
Example fix
# before cargo dev print-ast snippet # after cp snippet /tmp/snippet.py && cargo dev print-ast /tmp/snippet.py
Defensive patterns
Strategy: validation
Validate before calling
case "$f" in *.py|*.pyi) cargo dev print-ast "$f" ;; *) echo "expected a .py or .pyi file: $f" >&2; exit 1 ;; esac
Prevention
- Save scratch snippets with a .py extension before inspecting them
- Remember .pyi selects stub parsing; use it deliberately for stub files
When it happens
Trigger: Running `cargo dev print-ast snippet.pyx`, `cargo dev print-ast script` (no extension), or any non-.py/.pyi path.
Common situations: Inspecting scratch files saved without extensions; .pyi-mislabeled stubs; test fixtures with custom suffixes such as .pyi.txt.
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/56b3fa2ef7894603.
Report an issue: GitHub.