From 74d02e9bf0dadaaa5872b97dfa629fdfae2f731b Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Sat, 22 Jun 2024 14:25:38 +0100 Subject: [PATCH 1/2] [red-knot] Use POSIX representations of paths when creating the typeshed zip file --- Cargo.lock | 1 + crates/red_knot_module_resolver/Cargo.toml | 2 ++ crates/red_knot_module_resolver/build.rs | 21 ++++++++++--------- .../red_knot_module_resolver/src/typeshed.rs | 4 +++- 4 files changed, 17 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1d1c0b1983b56..87c29169c4041 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1997,6 +1997,7 @@ version = "0.0.0" dependencies = [ "anyhow", "insta", + "path-slash", "ruff_db", "ruff_python_stdlib", "rustc-hash", diff --git a/crates/red_knot_module_resolver/Cargo.toml b/crates/red_knot_module_resolver/Cargo.toml index 2d2f256ab7d53..b53c4b56dfa86 100644 --- a/crates/red_knot_module_resolver/Cargo.toml +++ b/crates/red_knot_module_resolver/Cargo.toml @@ -21,12 +21,14 @@ tracing = { workspace = true } zip = { workspace = true } [build-dependencies] +path-slash = { workspace = true } walkdir = { workspace = true } zip = { workspace = true } [dev-dependencies] anyhow = { workspace = true } insta = { workspace = true } +path-slash = { workspace = true } tempfile = { workspace = true } [lints] diff --git a/crates/red_knot_module_resolver/build.rs b/crates/red_knot_module_resolver/build.rs index 91ddbde8027b7..15f67f3bbb63c 100644 --- a/crates/red_knot_module_resolver/build.rs +++ b/crates/red_knot_module_resolver/build.rs @@ -8,6 +8,7 @@ use std::fs::File; use std::path::Path; +use path_slash::PathExt; use zip::result::ZipResult; use zip::write::{FileOptions, ZipWriter}; use zip::CompressionMethod; @@ -28,25 +29,25 @@ fn zip_dir(directory_path: &str, writer: File) -> ZipResult { for entry in walkdir::WalkDir::new(directory_path) { let dir_entry = entry.unwrap(); - let relative_path = dir_entry.path(); - let name = relative_path + let absolute_path = dir_entry.path(); + let normalized_relative_path = absolute_path .strip_prefix(Path::new(directory_path)) .unwrap() - .to_str() + .to_slash() .expect("Unexpected non-utf8 typeshed path!"); // Write file or directory explicitly // Some unzip tools unzip files with directory paths correctly, some do not! - if relative_path.is_file() { - println!("adding file {relative_path:?} as {name:?} ..."); - zip.start_file(name, options)?; - let mut f = File::open(relative_path)?; + if absolute_path.is_file() { + println!("adding file {absolute_path:?} as {normalized_relative_path:?} ..."); + zip.start_file(normalized_relative_path, options)?; + let mut f = File::open(absolute_path)?; std::io::copy(&mut f, &mut zip).unwrap(); - } else if !name.is_empty() { + } else if !normalized_relative_path.is_empty() { // Only if not root! Avoids path spec / warning // and mapname conversion failed error on unzip - println!("adding dir {relative_path:?} as {name:?} ..."); - zip.add_directory(name, options)?; + println!("adding dir {absolute_path:?} as {normalized_relative_path:?} ..."); + zip.add_directory(normalized_relative_path, options)?; } } zip.finish() diff --git a/crates/red_knot_module_resolver/src/typeshed.rs b/crates/red_knot_module_resolver/src/typeshed.rs index bf7369e328bb7..bd8aeef7eef17 100644 --- a/crates/red_knot_module_resolver/src/typeshed.rs +++ b/crates/red_knot_module_resolver/src/typeshed.rs @@ -5,6 +5,8 @@ mod tests { use std::io::{self, Read}; use std::path::Path; + use path_slash::PathExt; + #[test] fn typeshed_zip_created_at_build_time() -> anyhow::Result<()> { // The file path here is hardcoded in this crate's `build.rs` script. @@ -16,7 +18,7 @@ mod tests { let path_to_functools = Path::new("stdlib").join("functools.pyi"); let mut functools_module_stub = typeshed_zip_archive - .by_name(path_to_functools.to_str().unwrap()) + .by_name(&path_to_functools.to_slash().unwrap()) .unwrap(); assert!(functools_module_stub.is_file()); From 4fc0d75ed448f0d95555fac8345d6f052411ba15 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Sat, 22 Jun 2024 15:06:50 +0100 Subject: [PATCH 2/2] Get rid of the dev dependency --- crates/red_knot_module_resolver/Cargo.toml | 1 - crates/red_knot_module_resolver/src/typeshed.rs | 6 +----- 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/crates/red_knot_module_resolver/Cargo.toml b/crates/red_knot_module_resolver/Cargo.toml index b53c4b56dfa86..7d107a907dfbe 100644 --- a/crates/red_knot_module_resolver/Cargo.toml +++ b/crates/red_knot_module_resolver/Cargo.toml @@ -28,7 +28,6 @@ zip = { workspace = true } [dev-dependencies] anyhow = { workspace = true } insta = { workspace = true } -path-slash = { workspace = true } tempfile = { workspace = true } [lints] diff --git a/crates/red_knot_module_resolver/src/typeshed.rs b/crates/red_knot_module_resolver/src/typeshed.rs index bd8aeef7eef17..7f00f71d97690 100644 --- a/crates/red_knot_module_resolver/src/typeshed.rs +++ b/crates/red_knot_module_resolver/src/typeshed.rs @@ -3,9 +3,6 @@ pub(crate) mod versions; #[cfg(test)] mod tests { use std::io::{self, Read}; - use std::path::Path; - - use path_slash::PathExt; #[test] fn typeshed_zip_created_at_build_time() -> anyhow::Result<()> { @@ -16,9 +13,8 @@ mod tests { let mut typeshed_zip_archive = zip::ZipArchive::new(io::Cursor::new(TYPESHED_ZIP_BYTES))?; - let path_to_functools = Path::new("stdlib").join("functools.pyi"); let mut functools_module_stub = typeshed_zip_archive - .by_name(&path_to_functools.to_slash().unwrap()) + .by_name("stdlib/functools.pyi") .unwrap(); assert!(functools_module_stub.is_file());