Detect imports in src layouts by default (#12848)

## Summary

Occasionally, we receive bug reports that imports in `src` directories
aren't correctly detected. The root of the problem is that we default to
`src = ["."]`, so users have to set `src = ["src"]` explicitly. This PR
extends the default to cover _both_ of them: `src = [".", "src"]`.

Closes https://github.com/astral-sh/ruff/issues/12454.

## Test Plan

I replicated the structure described in
https://github.com/astral-sh/ruff/issues/12453, and verified that the
imports were considered sorted, but that adding `src = ["."]` showed an
error.
This commit is contained in:
Charlie Marsh 2024-08-14 10:54:45 -04:00 committed by Micha Reiser
parent 33512a4249
commit 15aa5a6d57
8 changed files with 33 additions and 31 deletions

View file

@ -323,33 +323,37 @@ pub struct Options {
/// The directories to consider when resolving first- vs. third-party
/// imports.
///
/// As an example: given a Python package structure like:
/// When omitted, the `src` directory will typically default to including both:
///
/// 1. The directory containing the nearest `pyproject.toml`, `ruff.toml`, or `.ruff.toml` file (the "project root").
/// 2. The `"src"` subdirectory of the project root.
///
/// These defaults ensure that uv supports both flat layouts and `src` layouts out-of-the-box.
/// (If a configuration file is explicitly provided (e.g., via the `--config` command-line
/// flag), the current working directory will be considered the project root.)
///
/// As an example, consider an alternative project structure, like:
///
/// ```text
/// my_project
/// ├── pyproject.toml
/// └── src
/// └── lib
/// └── my_package
/// ├── __init__.py
/// ├── foo.py
/// └── bar.py
/// ```
///
/// The `./src` directory should be included in the `src` option
/// (e.g., `src = ["src"]`), such that when resolving imports,
/// `my_package.foo` is considered a first-party import.
///
/// When omitted, the `src` directory will typically default to the
/// directory containing the nearest `pyproject.toml`, `ruff.toml`, or
/// `.ruff.toml` file (the "project root"), unless a configuration file
/// is explicitly provided (e.g., via the `--config` command-line flag).
/// In this case, the `./lib` directory should be included in the `src` option
/// (e.g., `src = ["lib"]`), such that when resolving imports, `my_package.foo`
/// is considered first-party.
///
/// This field supports globs. For example, if you have a series of Python
/// packages in a `python_modules` directory, `src = ["python_modules/*"]`
/// would expand to incorporate all of the packages in that directory. User
/// home directory and environment variables will also be expanded.
/// would expand to incorporate all packages in that directory. User home
/// directory and environment variables will also be expanded.
#[option(
default = r#"["."]"#,
default = r#"[".", "src"]"#,
value_type = "list[str]",
example = r#"
# Allow imports relative to the "src" and "test" directories.