Skip to content

Commit

Permalink
Set the location of the zip via environment variable
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexWaygood committed Jun 7, 2024
1 parent 6c7a33c commit c5ad832
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
6 changes: 6 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,9 @@ benchmark = "bench -p ruff_benchmark --bench linter --bench formatter --"
# See: https://github.com/astral-sh/ruff/issues/11503
[target.'cfg(all(target_env="msvc", target_os = "windows"))']
rustflags = ["-C", "target-feature=+crt-static"]

[env]
# Location for where to put the zip containing our vendored typeshed stubs
# that the red-knot crate creates at build time.
# The location is relative to the `OUT_DIR` environment variable that cargo sets.
RUFF_VENDORED_TYPESHED_ZIP = "/vendored_typeshed.zip"
6 changes: 4 additions & 2 deletions crates/red_knot/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use zip::write::{FileOptions, ZipWriter};
use zip::CompressionMethod;

const TYPESHED_SOURCE_DIR: &str = "vendor/typeshed";
const TYPESHED_ZIP_LOCATION: &str = "zipped_typeshed.zip";

/// Recursively zip the contents of an entire directory.
///
Expand Down Expand Up @@ -60,13 +59,16 @@ fn main() {
);
let out_dir = std::env::var("OUT_DIR").unwrap();

// This environment variable is set in `.cargo/config.toml`
let typeshed_destination = std::env::var("RUFF_VENDORED_TYPESHED_ZIP").unwrap();

// N.B. Deliberately using `format!()` instead of `Path::join()` here,
// so that we use `/` as a path separator on all platforms.
// That enables us to load the typeshed zip at compile time in `module.rs`
// (otherwise we'd have to dynamically determine the exact path to the typeshed zip
// based on the default path separator for the specific platform we're on,
// which can't be done at compile time.)
let zipped_typeshed_location = format!("{out_dir}/{TYPESHED_ZIP_LOCATION}");
let zipped_typeshed_location = format!("{out_dir}{typeshed_destination}");

let zipped_typeshed = File::create(zipped_typeshed_location).unwrap();
zip_dir(TYPESHED_SOURCE_DIR, zipped_typeshed).unwrap();
Expand Down
5 changes: 2 additions & 3 deletions crates/red_knot/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -928,10 +928,9 @@ mod tests {

#[test]
fn typeshed_zip_created_at_build_time() -> anyhow::Result<()> {
// The file path here is hardcoded in this crate's `build.rs` script.
// Luckily this crate will fail to build if this file isn't available at build time.
// The RUFF_VENDORED_TYPESHED_ZIP environment variable is set in `.cargo/config.toml`
const TYPESHED_ZIP_BYTES: &[u8] =
include_bytes!(concat!(env!("OUT_DIR"), "/zipped_typeshed.zip"));
include_bytes!(concat!(env!("OUT_DIR"), env!("RUFF_VENDORED_TYPESHED_ZIP")));
assert!(!TYPESHED_ZIP_BYTES.is_empty());
let mut typeshed_zip_archive = ZipArchive::new(Cursor::new(TYPESHED_ZIP_BYTES))?;

Expand Down

0 comments on commit c5ad832

Please sign in to comment.