Skip to content

Commit 653c090

Browse files
Use an empty vendored file system in Ruff (#13436)
## Summary This PR changes removes the typeshed stubs from the vendored file system shipped with ruff and instead ships an empty "typeshed". Making the typeshed files optional required extracting the typshed files into a new `ruff_vendored` crate. I do like this even if all our builds always include typeshed because it means `red_knot_python_semantic` contains less code that needs compiling. This also allows us to use deflate because the compression algorithm doesn't matter for an archive containing a single, empty file. ## Test Plan `cargo test` I verified with ` cargo tree -f "{p} {f}" -p <package> ` that: * red_knot_wasm: enables `deflate` compression * red_knot: enables `zstd` compression * `ruff`: uses stored I'm not quiet sure how to build the binary that maturin builds but comparing the release artifact size with `strip = true` shows a `1.5MB` size reduction --------- Co-authored-by: Charlie Marsh <[email protected]>
1 parent 8921fbb commit 653c090

File tree

602 files changed

+158
-126
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

602 files changed

+158
-126
lines changed

.github/workflows/sync_typeshed.yaml

+7-7
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@ jobs:
3737
- name: Sync typeshed
3838
id: sync
3939
run: |
40-
rm -rf ruff/crates/red_knot_python_semantic/vendor/typeshed
41-
mkdir ruff/crates/red_knot_python_semantic/vendor/typeshed
42-
cp typeshed/README.md ruff/crates/red_knot_python_semantic/vendor/typeshed
43-
cp typeshed/LICENSE ruff/crates/red_knot_python_semantic/vendor/typeshed
44-
cp -r typeshed/stdlib ruff/crates/red_knot_python_semantic/vendor/typeshed/stdlib
45-
rm -rf ruff/crates/red_knot_python_semantic/vendor/typeshed/stdlib/@tests
46-
git -C typeshed rev-parse HEAD > ruff/crates/red_knot_python_semantic/vendor/typeshed/source_commit.txt
40+
rm -rf ruff/crates/ruff_vendored/vendor/typeshed
41+
mkdir ruff/crates/ruff_vendored/vendor/typeshed
42+
cp typeshed/README.md ruff/crates/ruff_vendored/vendor/typeshed
43+
cp typeshed/LICENSE ruff/crates/ruff_vendored/vendor/typeshed
44+
cp -r typeshed/stdlib ruff/crates/ruff_vendored/vendor/typeshed/stdlib
45+
rm -rf ruff/crates/ruff_vendored/vendor/typeshed/stdlib/@tests
46+
git -C typeshed rev-parse HEAD > ruff/crates/ruff_vendored/vendor/typeshed/source_commit.txt
4747
- name: Commit the changes
4848
id: commit
4949
if: ${{ steps.sync.outcome == 'success' }}

.pre-commit-config.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ fail_fast: true
22

33
exclude: |
44
(?x)^(
5-
crates/red_knot_python_semantic/vendor/.*|
5+
crates/ruff_vendored/vendor/.*|
66
crates/red_knot_workspace/resources/.*|
77
crates/ruff_linter/resources/.*|
88
crates/ruff_linter/src/rules/.*/snapshots/.*|

Cargo.lock

+15-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+3-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ license = "MIT"
1414
[workspace.dependencies]
1515
ruff = { path = "crates/ruff" }
1616
ruff_cache = { path = "crates/ruff_cache" }
17-
ruff_db = { path = "crates/ruff_db" }
17+
ruff_db = { path = "crates/ruff_db", default-features = false }
1818
ruff_diagnostics = { path = "crates/ruff_diagnostics" }
1919
ruff_formatter = { path = "crates/ruff_formatter" }
2020
ruff_graph = { path = "crates/ruff_graph" }
@@ -34,11 +34,12 @@ ruff_python_trivia = { path = "crates/ruff_python_trivia" }
3434
ruff_server = { path = "crates/ruff_server" }
3535
ruff_source_file = { path = "crates/ruff_source_file" }
3636
ruff_text_size = { path = "crates/ruff_text_size" }
37+
ruff_vendored = { path = "crates/ruff_vendored" }
3738
ruff_workspace = { path = "crates/ruff_workspace" }
3839

3940
red_knot_python_semantic = { path = "crates/red_knot_python_semantic" }
4041
red_knot_server = { path = "crates/red_knot_server" }
41-
red_knot_workspace = { path = "crates/red_knot_workspace" }
42+
red_knot_workspace = { path = "crates/red_knot_workspace", default-features = false }
4243

4344
aho-corasick = { version = "1.1.3" }
4445
annotate-snippets = { version = "0.9.2", features = ["color"] }

crates/red_knot/Cargo.toml

+1-2
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@ license.workspace = true
1313

1414
[dependencies]
1515
red_knot_python_semantic = { workspace = true }
16-
red_knot_workspace = { workspace = true }
16+
red_knot_workspace = { workspace = true, features = ["zstd"] }
1717
red_knot_server = { workspace = true }
18-
1918
ruff_db = { workspace = true, features = ["os", "cache"] }
2019

2120
anyhow = { workspace = true }

crates/red_knot_python_semantic/Cargo.toml

+1-8
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ bitflags = { workspace = true }
2424
camino = { workspace = true }
2525
compact_str = { workspace = true }
2626
countme = { workspace = true }
27-
once_cell = { workspace = true }
2827
ordermap = { workspace = true }
2928
salsa = { workspace = true }
3029
thiserror = { workspace = true }
@@ -35,20 +34,14 @@ smallvec = { workspace = true }
3534
static_assertions = { workspace = true }
3635
test-case = { workspace = true }
3736

38-
[build-dependencies]
39-
path-slash = { workspace = true }
40-
walkdir = { workspace = true }
41-
zip = { workspace = true, features = ["zstd", "deflate"] }
42-
4337
[dev-dependencies]
4438
ruff_db = { workspace = true, features = ["os", "testing"] }
4539
ruff_python_parser = { workspace = true }
40+
ruff_vendored = { workspace = true }
4641

4742
anyhow = { workspace = true }
4843
insta = { workspace = true }
4944
tempfile = { workspace = true }
50-
walkdir = { workspace = true }
51-
zip = { workspace = true }
5245

5346
[lints]
5447
workspace = true

crates/red_knot_python_semantic/src/db.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ pub trait Db: SourceDb + Upcast<dyn SourceDb> {
1111
pub(crate) mod tests {
1212
use std::sync::Arc;
1313

14-
use crate::module_resolver::vendored_typeshed_stubs;
1514
use ruff_db::files::{File, Files};
1615
use ruff_db::system::{DbWithTestSystem, System, TestSystem};
1716
use ruff_db::vendored::VendoredFileSystem;
@@ -33,7 +32,7 @@ pub(crate) mod tests {
3332
Self {
3433
storage: salsa::Storage::default(),
3534
system: TestSystem::default(),
36-
vendored: vendored_typeshed_stubs().clone(),
35+
vendored: ruff_vendored::file_system().clone(),
3736
events: std::sync::Arc::default(),
3837
files: Files::default(),
3938
}

crates/red_knot_python_semantic/src/lib.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ use rustc_hash::FxHasher;
44

55
pub use db::Db;
66
pub use module_name::ModuleName;
7-
pub use module_resolver::{
8-
resolve_module, system_module_search_paths, vendored_typeshed_stubs, Module,
9-
};
7+
pub use module_resolver::{resolve_module, system_module_search_paths, Module};
108
pub use program::{Program, ProgramSettings, SearchPathSettings, SitePackages};
119
pub use python_version::PythonVersion;
1210
pub use semantic_model::{HasTy, SemanticModel};

crates/red_knot_python_semantic/src/module_resolver/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ pub use module::Module;
44
pub use resolver::resolve_module;
55
pub(crate) use resolver::{file_to_module, SearchPaths};
66
use ruff_db::system::SystemPath;
7-
pub use typeshed::vendored_typeshed_stubs;
87

98
use crate::module_resolver::resolver::search_paths;
109
use crate::Db;

crates/red_knot_python_semantic/src/module_resolver/typeshed/versions.rs renamed to crates/red_knot_python_semantic/src/module_resolver/typeshed.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,8 @@ mod tests {
390390
fn typeshed_versions_consistent_with_vendored_stubs() {
391391
let db = TestDb::new();
392392
let vendored_typeshed_versions = vendored_typeshed_versions(&db);
393-
let vendored_typeshed_dir = Path::new("vendor/typeshed").canonicalize().unwrap();
393+
let vendored_typeshed_dir =
394+
Path::new(env!("CARGO_MANIFEST_DIR")).join("../ruff_vendored/vendor/typeshed");
394395

395396
let mut empty_iterator = true;
396397

crates/red_knot_python_semantic/src/module_resolver/typeshed/mod.rs

-8
This file was deleted.

crates/red_knot_python_semantic/src/types.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -701,12 +701,12 @@ enum CallOutcome<'db> {
701701

702702
impl<'db> CallOutcome<'db> {
703703
/// Create a new `CallOutcome::Callable` with given return type.
704-
fn callable(return_ty: Type<'db>) -> CallOutcome {
704+
fn callable(return_ty: Type<'db>) -> CallOutcome<'db> {
705705
CallOutcome::Callable { return_ty }
706706
}
707707

708708
/// Create a new `CallOutcome::NotCallable` with given not-callable type.
709-
fn not_callable(not_callable_ty: Type<'db>) -> CallOutcome {
709+
fn not_callable(not_callable_ty: Type<'db>) -> CallOutcome<'db> {
710710
CallOutcome::NotCallable { not_callable_ty }
711711
}
712712

@@ -719,7 +719,10 @@ impl<'db> CallOutcome<'db> {
719719
}
720720

721721
/// Create a new `CallOutcome::Union` with given wrapped outcomes.
722-
fn union(called_ty: Type<'db>, outcomes: impl Into<Box<[CallOutcome<'db>]>>) -> CallOutcome {
722+
fn union(
723+
called_ty: Type<'db>,
724+
outcomes: impl Into<Box<[CallOutcome<'db>]>>,
725+
) -> CallOutcome<'db> {
723726
CallOutcome::Union {
724727
called_ty,
725728
outcomes: outcomes.into(),

crates/red_knot_wasm/Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ default = ["console_error_panic_hook"]
2020

2121
[dependencies]
2222
red_knot_python_semantic = { workspace = true }
23-
red_knot_workspace = { workspace = true }
23+
red_knot_workspace = { workspace = true, default-features = false, features = ["deflate"] }
2424

25-
ruff_db = { workspace = true }
25+
ruff_db = { workspace = true, features = [] }
2626
ruff_notebook = { workspace = true }
2727

2828
console_error_panic_hook = { workspace = true, optional = true }

crates/red_knot_workspace/Cargo.toml

+6
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ ruff_cache = { workspace = true }
1818
ruff_db = { workspace = true, features = ["os", "cache"] }
1919
ruff_python_ast = { workspace = true }
2020
ruff_text_size = { workspace = true }
21+
ruff_vendored = { workspace = true }
2122

2223
anyhow = { workspace = true }
2324
crossbeam = { workspace = true }
@@ -31,5 +32,10 @@ tracing = { workspace = true }
3132
ruff_db = { workspace = true, features = ["testing"] }
3233
tempfile = { workspace = true }
3334

35+
[features]
36+
default = ["zstd"]
37+
zstd = ["ruff_vendored/zstd"]
38+
deflate = ["ruff_vendored/deflate"]
39+
3440
[lints]
3541
workspace = true

crates/red_knot_workspace/src/db.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::sync::Arc;
44
use salsa::plumbing::ZalsaDatabase;
55
use salsa::{Cancelled, Event};
66

7-
use red_knot_python_semantic::{vendored_typeshed_stubs, Db as SemanticDb, Program};
7+
use red_knot_python_semantic::{Db as SemanticDb, Program};
88
use ruff_db::files::{File, Files};
99
use ruff_db::system::System;
1010
use ruff_db::vendored::VendoredFileSystem;
@@ -124,7 +124,7 @@ impl SemanticDb for RootDatabase {
124124
#[salsa::db]
125125
impl SourceDb for RootDatabase {
126126
fn vendored(&self) -> &VendoredFileSystem {
127-
vendored_typeshed_stubs()
127+
ruff_vendored::file_system()
128128
}
129129

130130
fn system(&self) -> &dyn System {
@@ -161,7 +161,7 @@ pub(crate) mod tests {
161161

162162
use salsa::Event;
163163

164-
use red_knot_python_semantic::{vendored_typeshed_stubs, Db as SemanticDb};
164+
use red_knot_python_semantic::Db as SemanticDb;
165165
use ruff_db::files::Files;
166166
use ruff_db::system::{DbWithTestSystem, System, TestSystem};
167167
use ruff_db::vendored::VendoredFileSystem;
@@ -183,7 +183,7 @@ pub(crate) mod tests {
183183
Self {
184184
storage: salsa::Storage::default(),
185185
system: TestSystem::default(),
186-
vendored: vendored_typeshed_stubs().clone(),
186+
vendored: ruff_vendored::file_system().clone(),
187187
files: Files::default(),
188188
events: Arc::default(),
189189
}

crates/ruff/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ default-run = "ruff"
1414

1515
[dependencies]
1616
ruff_cache = { workspace = true }
17-
ruff_db = { workspace = true }
17+
ruff_db = { workspace = true, default-features = false, features = ["os"] }
1818
ruff_diagnostics = { workspace = true }
1919
ruff_graph = { workspace = true, features = ["serde", "clap"] }
2020
ruff_linter = { workspace = true, features = ["clap"] }

crates/ruff_db/Cargo.toml

+2-4
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,17 @@ tracing = { workspace = true }
3333
tracing-subscriber = { workspace = true, optional = true }
3434
tracing-tree = { workspace = true, optional = true }
3535
rustc-hash = { workspace = true }
36-
37-
[target.'cfg(not(target_arch="wasm32"))'.dependencies]
38-
zip = { workspace = true, features = ["zstd"] }
36+
zip = { workspace = true }
3937

4038
[target.'cfg(target_arch="wasm32")'.dependencies]
4139
web-time = { version = "1.1.0" }
42-
zip = { workspace = true, features = ["deflate"] }
4340

4441
[dev-dependencies]
4542
insta = { workspace = true }
4643
tempfile = { workspace = true }
4744

4845
[features]
46+
default = ["os"]
4947
cache = ["ruff_cache"]
5048
os = ["ignore"]
5149
serde = ["dep:serde", "camino/serde1"]

crates/ruff_db/src/files.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,8 @@ mod tests {
503503
use crate::files::{system_path_to_file, vendored_path_to_file, FileError};
504504
use crate::system::DbWithTestSystem;
505505
use crate::tests::TestDb;
506-
use crate::vendored::tests::VendoredFileSystemBuilder;
506+
use crate::vendored::VendoredFileSystemBuilder;
507+
use zip::CompressionMethod;
507508

508509
#[test]
509510
fn system_existing_file() -> crate::system::Result<()> {
@@ -548,7 +549,7 @@ mod tests {
548549
fn stubbed_vendored_file() -> crate::system::Result<()> {
549550
let mut db = TestDb::new();
550551

551-
let mut vendored_builder = VendoredFileSystemBuilder::new();
552+
let mut vendored_builder = VendoredFileSystemBuilder::new(CompressionMethod::Stored);
552553
vendored_builder
553554
.add_file("test.pyi", "def foo() -> str")
554555
.unwrap();

crates/ruff_db/src/parsed.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,9 @@ mod tests {
7979
use crate::parsed::parsed_module;
8080
use crate::system::{DbWithTestSystem, SystemPath, SystemVirtualPath};
8181
use crate::tests::TestDb;
82-
use crate::vendored::{tests::VendoredFileSystemBuilder, VendoredPath};
82+
use crate::vendored::{VendoredFileSystemBuilder, VendoredPath};
8383
use crate::Db;
84+
use zip::CompressionMethod;
8485

8586
#[test]
8687
fn python_file() -> crate::system::Result<()> {
@@ -150,7 +151,7 @@ mod tests {
150151
fn vendored_file() {
151152
let mut db = TestDb::new();
152153

153-
let mut vendored_builder = VendoredFileSystemBuilder::new();
154+
let mut vendored_builder = VendoredFileSystemBuilder::new(CompressionMethod::Stored);
154155
vendored_builder
155156
.add_file(
156157
"path.pyi",

0 commit comments

Comments
 (0)