Skip to content

Commit 097aa04

Browse files
authored
[red-knot] Typeshed patching: use build.rs instead of workflow (#15370)
## Summary The symlink-approach in the typeshed-sync workflow caused some problems on Windows, even though it seemed to work fine in CI: #15138 (comment) Here, we rely on `build.rs` to patch typeshed instead, which allows us to get rid of the modifications in the workflow (thank you @MichaReiser for the idea). ## Test Plan - Made sure that changes to `knot_extensions.pyi` result in a recompile of `red_knot_vendored`.
1 parent f706c3f commit 097aa04

File tree

4 files changed

+15
-9
lines changed

4 files changed

+15
-9
lines changed

.github/workflows/sync_typeshed.yaml

-4
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,6 @@ jobs:
4646
cp -r typeshed/stdlib ruff/crates/red_knot_vendored/vendor/typeshed/stdlib
4747
rm -rf ruff/crates/red_knot_vendored/vendor/typeshed/stdlib/@tests
4848
git -C typeshed rev-parse HEAD > ruff/crates/red_knot_vendored/vendor/typeshed/source_commit.txt
49-
# Patch the typeshed stubs to include `knot_extensions`
50-
ln -s ../../../knot_extensions/knot_extensions.pyi ruff/crates/red_knot_vendored/vendor/typeshed/stdlib/
51-
echo "# Patch applied for red_knot:" >> ruff/crates/red_knot_vendored/vendor/typeshed/stdlib/VERSIONS
52-
echo "knot_extensions: 3.0-" >> ruff/crates/red_knot_vendored/vendor/typeshed/stdlib/VERSIONS
5349
- name: Commit the changes
5450
id: commit
5551
if: ${{ steps.sync.outcome == 'success' }}

crates/red_knot_vendored/build.rs

+15-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
//! in `crates/red_knot_vendored/vendor/typeshed` change.
77
88
use std::fs::File;
9+
use std::io::Write;
910
use std::path::Path;
1011

1112
use path_slash::PathExt;
@@ -14,6 +15,7 @@ use zip::write::{FileOptions, ZipWriter};
1415
use zip::CompressionMethod;
1516

1617
const TYPESHED_SOURCE_DIR: &str = "vendor/typeshed";
18+
const KNOT_EXTENSIONS_STUBS: &str = "knot_extensions/knot_extensions.pyi";
1719
const TYPESHED_ZIP_LOCATION: &str = "/zipped_typeshed.zip";
1820

1921
/// Recursively zip the contents of an entire directory.
@@ -55,21 +57,32 @@ fn zip_dir(directory_path: &str, writer: File) -> ZipResult<File> {
5557
// Some unzip tools unzip files with directory paths correctly, some do not!
5658
if absolute_path.is_file() {
5759
println!("adding file {absolute_path:?} as {normalized_relative_path:?} ...");
58-
zip.start_file(normalized_relative_path, options)?;
60+
zip.start_file(&*normalized_relative_path, options)?;
5961
let mut f = File::open(absolute_path)?;
6062
std::io::copy(&mut f, &mut zip).unwrap();
63+
64+
// Patch the VERSIONS file to make `knot_extensions` available
65+
if normalized_relative_path == "stdlib/VERSIONS" {
66+
writeln!(&mut zip, "knot_extensions: 3.0-")?;
67+
}
6168
} else if !normalized_relative_path.is_empty() {
6269
// Only if not root! Avoids path spec / warning
6370
// and mapname conversion failed error on unzip
6471
println!("adding dir {absolute_path:?} as {normalized_relative_path:?} ...");
6572
zip.add_directory(normalized_relative_path, options)?;
6673
}
6774
}
75+
76+
// Patch typeshed and add the stubs for the `knot_extensions` module
77+
println!("adding file {KNOT_EXTENSIONS_STUBS} as stdlib/knot_extensions.pyi ...");
78+
zip.start_file("stdlib/knot_extensions.pyi", options)?;
79+
let mut f = File::open(KNOT_EXTENSIONS_STUBS)?;
80+
std::io::copy(&mut f, &mut zip).unwrap();
81+
6882
zip.finish()
6983
}
7084

7185
fn main() {
72-
println!("cargo::rerun-if-changed={TYPESHED_SOURCE_DIR}");
7386
assert!(
7487
Path::new(TYPESHED_SOURCE_DIR).is_dir(),
7588
"Where is typeshed?"

crates/red_knot_vendored/vendor/typeshed/stdlib/VERSIONS

-2
Original file line numberDiff line numberDiff line change
@@ -341,5 +341,3 @@ zipfile._path: 3.12-
341341
zipimport: 3.0-
342342
zlib: 3.0-
343343
zoneinfo: 3.9-
344-
# Patch applied for red_knot:
345-
knot_extensions: 3.0-

crates/red_knot_vendored/vendor/typeshed/stdlib/knot_extensions.pyi

-1
This file was deleted.

0 commit comments

Comments
 (0)