Skip to content

Commit 2a7d271

Browse files
rchen152facebook-github-bot
authored andcommitted
Document recent changes to config finding
Summary: Documents: * the additional config-like files we now look for in upward search, * detection of flat and src project layouts, * the dump-config command. I struggled a bit to articulate what was going on with project roots and import roots. Happy to take suggestions for how to make this clearer. Reviewed By: migeed-z Differential Revision: D74289623 fbshipit-source-id: cf27339ffd6e85e880e6a8736d473788fa9495fd
1 parent 3628e2e commit 2a7d271

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-9
lines changed

website/docs/configuration.mdx

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,17 @@ correspond to different but useful ways we expect most people to interact with P
5252
## Configuration Finding
5353

5454
In both project checking mode and single-file checking mode (see [Type Checking Modes](#type-checking-modes)
55-
for more info), we may perform an upward file search to find a
56-
configuration file if one is not provided with `-c`/`--config`. We each directory
57-
from the 'start location' to the filesystem root, looking first for `pyrefly.toml` then `pyproject.toml`, in
58-
the same directory before looking at its parent.
55+
for more info), we attempt to find a *project root* from which to check each file, both for reading
56+
config options and for import resolution (see [Import Root](./import-resolution.mdx#import-root)). The
57+
project root is typically the directory containing the configuration file. More precisely:
58+
59+
1. If a configuration file is provided with `-c`/`--config`, we use its containing directory.
60+
1. Otherwise, we perform an upward file search from the 'start location' to the filesystem root,
61+
looking in each directory for any of the following files: `pyrefly.toml`, `pyproject.toml`,
62+
`setup.py`, `mypy.ini`, and `pyrightconfig.json`. If we find one, we use the current directory.
63+
64+
Note that only `pyrefly.toml` and `pyproject.toml` are parsed for config options, but we look for
65+
additional files that mark the root of a project to aid import resolution.
5966

6067
For project checking mode, the 'start location' is current working directory. For single-file checking mode,
6168
the start location is the directory containing each file to be type checked, and
@@ -131,12 +138,12 @@ type checked file is imported by another type checked file, we check all search
131138
determine how to import it.
132139

133140
- Type: list of directories specifying the root
134-
- Default: `["."]`
141+
- Default: [import root](./import-resolution.mdx#import-root)
135142
- Flag equivalent: `--search-path`
136143
- ENV equivalent: `PYREFLY_SEARCH_PATH`
137144
- Equivalent configs: `extraPaths` in Pyright, `mypy_path` in mypy
138145
- Notes:
139-
- We automatically append `"."` (the directory containing the
146+
- We automatically append an import root (typically the directory containing the
140147
configuration file) to the `search_roots` when type checking as a sensible
141148
default and last attempt at an import.
142149
- Libraries should not be listed here, since they may override `typeshed`

website/docs/import-resolution.mdx

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,8 @@ directory, and more dots (e.g. `..other.file`) will continue to walk upward.
2525
For absolute imports, Pyrefly uses the following import strategy:
2626

2727
1. Try to import from each entry in [`search_path`](./configuration.mdx#search_path) in the order they appear
28-
using the module finding strategy. a. NOTE: we append the config file's
29-
directory to `search_path` automatically when using a config file as a
30-
sensible last-resort for attempting an import.
28+
using the module finding strategy. a. NOTE: we append an [import root](#import-root) to `search_path`
29+
automatically when using a config file as a sensible last-resort for attempting an import.
3130
2. Try to import from `typeshed`.
3231
3. Try to find a stub package corresponding to the import we're trying to resolve in
3332
[`site_package_path`](./configuration.mdx#site_package_path).
@@ -43,6 +42,17 @@ See [Site Package Path Typing Rules](#site-package-path-typing-rules) for more i
4342
modules are valid imports from [`site_package_path`](./configuration.mdx#site_package_path), and
4443
how to override that behavior.
4544

45+
### Import Root
46+
47+
Pyrefly automatically appends an *import root* to the search path. This is the directory that
48+
importable Python packages live in, determined from the
49+
[project root](./configuration.mdx#configuration-finding) and layout.
50+
Pyrefly auto-detects import roots for two common project layouts:
51+
52+
* Src layout, assumed when there is a `src/` directory directly inside the project root. `src/` is
53+
the import root.
54+
* Flat layout, the default. The project root is the import root.
55+
4656
### Site Package Path Typing Rules
4757

4858
We respect typing rules as defined by the typing spec for
@@ -83,3 +93,9 @@ imports by first searching for a relevant `-stubs` package, then by looking at
8393
the non-stubs package's `.pyi` files, then falls back to a `.py` file. See
8494
[Absolute Imports](#absolute-imports) for details on when non-stubs packages
8595
are allowed to be used for types, and how you can override that behavior.
96+
97+
## Debugging Import Issues
98+
99+
Pyrefly has a `dump-config` command that dumps the import-related config options it is using for
100+
each file it is checking. To use it, simply replace `check` with `dump-config` in your
101+
command-line invocation.

0 commit comments

Comments
 (0)