Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions crates/ty/tests/cli/python_environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1727,3 +1727,52 @@ fn default_root_tests_package() -> anyhow::Result<()> {

Ok(())
}

#[test]
fn pythonpath_is_respected() -> anyhow::Result<()> {
let case = CliTest::with_files([
("src/bar/baz.py", "it = 42"),
(
"src/foo.py",
r#"
import baz
print(f"{baz.it}")
"#,
),
])?;

assert_cmd_snapshot!(case.command(),
@r#"
success: false
exit_code: 1
----- stdout -----
error[unresolved-import]: Cannot resolve imported module `baz`
--> src/foo.py:2:8
|
2 | import baz
| ^^^
3 | print(f"{baz.it}")
|
info: make sure your Python environment is properly configured: https://docs.astral.sh/ty/modules/#python-environment
info: rule `unresolved-import` is enabled by default

Found 1 diagnostic

----- stderr -----
WARN ty is pre-release software and not ready for production use. Expect to encounter bugs, missing features, and fatal errors.
"#);

assert_cmd_snapshot!(case.command()
.env("PYTHONPATH", case.root().join("src/bar")),
@r#"
success: true
exit_code: 0
----- stdout -----
All checks passed!

----- stderr -----
WARN ty is pre-release software and not ready for production use. Expect to encounter bugs, missing features, and fatal errors.
"#);

Ok(())
}
35 changes: 31 additions & 4 deletions crates/ty_project/src/metadata/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,14 +277,41 @@ impl Options {
roots
};

let settings = SearchPathSettings {
extra_paths: environment
// collect the existing site packages
let mut extra_paths: Vec<SystemPathBuf> = Vec::new();

// read all the paths off the PYTHONPATH environment variable, check
// they exist as a directory, and add them to the vec of extra_paths
// as they should be checked before site-packages just like python
// interpreter does
if let Ok(python_path) = system.env_var("PYTHONPATH") {
for path in python_path.split(':') {
let possible_path = SystemPath::absolute(path, system.current_directory());

if system.is_directory(&possible_path) {
tracing::debug!(
"Adding `{possible_path}` from the `PYTHONPATH` environment variable to `extra_paths`"
);
extra_paths.push(possible_path);
} else {
tracing::debug!(
"Skipping `{possible_path}` listed in `PYTHONPATH` because the path doesn't exist or isn't a directory"
);
}
}
}

extra_paths.extend(
environment
.extra_paths
.as_deref()
.unwrap_or_default()
.iter()
.map(|path| path.absolute(project_root, system))
.collect(),
.map(|path| path.absolute(project_root, system)),
);

let settings = SearchPathSettings {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AlexWaygood any thoughts on whether we should move this logic into SearchPaths::from_settings?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No strong opinion really!

extra_paths,
src_roots,
custom_typeshed: environment
.typeshed
Expand Down
Loading