From 979049b2a60598d5ea274c9b19f12501fac79950 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Wed, 28 Jun 2023 09:52:20 -0400 Subject: [PATCH] Make lib iteration platform-specific (#5406) --- crates/ruff_python_resolver/src/python_platform.rs | 13 +++++++++++++ crates/ruff_python_resolver/src/search.rs | 2 +- ...lver__resolver__tests__airflow_third_party.snap | 14 +++++++------- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/crates/ruff_python_resolver/src/python_platform.rs b/crates/ruff_python_resolver/src/python_platform.rs index 8ee2600518..b82ebe256c 100644 --- a/crates/ruff_python_resolver/src/python_platform.rs +++ b/crates/ruff_python_resolver/src/python_platform.rs @@ -5,3 +5,16 @@ pub(crate) enum PythonPlatform { Linux, Windows, } + +impl PythonPlatform { + /// Returns the platform-specific library names. These are the candidate names for the top-level + /// subdirectory within a virtual environment that contains the `site-packages` directory + /// (with a `pythonX.Y` directory in-between). + pub(crate) fn lib_names(&self) -> &[&'static str] { + match self { + PythonPlatform::Darwin => &["lib"], + PythonPlatform::Linux => &["lib", "lib64"], + PythonPlatform::Windows => &["Lib"], + } + } +} diff --git a/crates/ruff_python_resolver/src/search.rs b/crates/ruff_python_resolver/src/search.rs index 57128e6458..01c6114ae4 100644 --- a/crates/ruff_python_resolver/src/search.rs +++ b/crates/ruff_python_resolver/src/search.rs @@ -140,7 +140,7 @@ fn find_python_search_paths(config: &Config, host: &Host) -> V if let Some(venv) = config.venv.as_ref() { let mut found_paths = vec![]; - for lib_name in ["lib", "Lib", "lib64"] { + for lib_name in host.python_platform().lib_names() { let lib_path = venv_path.join(venv).join(lib_name); if let Some(site_packages_path) = find_site_packages_path(&lib_path, None) { // Add paths from any `.pth` files in each of the `site-packages` directories. diff --git a/crates/ruff_python_resolver/src/snapshots/ruff_python_resolver__resolver__tests__airflow_third_party.snap b/crates/ruff_python_resolver/src/snapshots/ruff_python_resolver__resolver__tests__airflow_third_party.snap index 04c953624f..ed13cadb03 100644 --- a/crates/ruff_python_resolver/src/snapshots/ruff_python_resolver__resolver__tests__airflow_third_party.snap +++ b/crates/ruff_python_resolver/src/snapshots/ruff_python_resolver__resolver__tests__airflow_third_party.snap @@ -11,11 +11,11 @@ ImportResult { is_stub_package: false, import_type: ThirdParty, resolved_paths: [ - "./resources/test/airflow/venv/Lib/python3.11/site-packages/sqlalchemy/__init__.py", - "./resources/test/airflow/venv/Lib/python3.11/site-packages/sqlalchemy/orm/__init__.py", + "./resources/test/airflow/venv/lib/python3.11/site-packages/sqlalchemy/__init__.py", + "./resources/test/airflow/venv/lib/python3.11/site-packages/sqlalchemy/orm/__init__.py", ], search_path: Some( - "./resources/test/airflow/venv/Lib/python3.11/site-packages", + "./resources/test/airflow/venv/lib/python3.11/site-packages", ), is_stub_file: false, is_native_lib: false, @@ -27,21 +27,21 @@ ImportResult { is_stub_file: false, is_native_lib: false, name: "base", - path: "./resources/test/airflow/venv/Lib/python3.11/site-packages/sqlalchemy/orm/base.py", + path: "./resources/test/airflow/venv/lib/python3.11/site-packages/sqlalchemy/orm/base.py", py_typed: None, }, "dependency": ImplicitImport { is_stub_file: false, is_native_lib: false, name: "dependency", - path: "./resources/test/airflow/venv/Lib/python3.11/site-packages/sqlalchemy/orm/dependency.py", + path: "./resources/test/airflow/venv/lib/python3.11/site-packages/sqlalchemy/orm/dependency.py", py_typed: None, }, "query": ImplicitImport { is_stub_file: false, is_native_lib: false, name: "query", - path: "./resources/test/airflow/venv/Lib/python3.11/site-packages/sqlalchemy/orm/query.py", + path: "./resources/test/airflow/venv/lib/python3.11/site-packages/sqlalchemy/orm/query.py", py_typed: None, }, }, @@ -49,6 +49,6 @@ ImportResult { non_stub_import_result: None, py_typed_info: None, package_directory: Some( - "./resources/test/airflow/venv/Lib/python3.11/site-packages/sqlalchemy", + "./resources/test/airflow/venv/lib/python3.11/site-packages/sqlalchemy", ), }