Skip to content

Build failure when only deflate-flate2 crate feature is enabled #344

@anforowicz

Description

@anforowicz

Context

IIUC the zip crate supports using either zopfli or flate2 backend for compressing using the deflate algorithm/format. I assume that only one of those backends is required - I would like to avoid depending on both to minimize binary size, maintenance overhead, etc. The desire to minimize the dependency tree came up when discussing importing the zip crate into the Chromium project here.

Problem

Using the zip crate with only the deflate-flate2 feature enabled leads to build errors.

Repro steps:

$ cargo new zip-test
$ cd zip-test
$ vim Cargo.toml
$ cat Cargo.toml
[package]
name = "zip-test"
version = "0.1.0"
edition = "2024"

[dependencies.zip]
version = "2.6.1"
default-features = false
features = ["deflate-flate2"]

$ cargo build
error[E0433]: failed to resolve: use of unresolved module or unlinked crate `flate2`
  --> /usr/local/google/home/lukasza/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/zip-2.6.1/src/write.rs:33:5
   |
33 | use flate2::{write::DeflateEncoder, Compression};
   |     ^^^^^^ use of unresolved module or unlinked crate `flate2`
   |
   = help: if you wanted to use a crate named `flate2`, use `cargo add flate2` to add it to your `Cargo.toml`
...

Proposed fix

I assume that we just need to explicitly say in zip crate's Cargo.toml that the deflate-flate2 feature depends on the flate2 crate:

$ git diff
diff --git a/Cargo.toml b/Cargo.toml
index fae78e29..ed45ac1b 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -73,8 +73,8 @@ aes-crypto = ["aes", "constant_time_eq", "hmac", "pbkdf2", "sha1", "getrandom",
 chrono = ["chrono/default"]
 _deflate-any = []
 _all-features = [] # Detect when --all-features is used
-deflate = ["flate2/rust_backend", "deflate-zopfli", "deflate-flate2"]
-deflate-flate2 = ["_deflate-any"]
+deflate = ["deflate-zopfli", "deflate-flate2"]
+deflate-flate2 = ["_deflate-any", "flate2/rust_backend"]
 # DEPRECATED: previously enabled `flate2/miniz_oxide` which is equivalent to `flate2/rust_backend`
 deflate-miniz = ["deflate", "deflate-flate2"]
 deflate-zlib = ["flate2/zlib", "deflate-flate2"]

I don't understand why the deflate feature transitively enabled both deflate-zopfli and deflate-flate2, but this is somewhat secondary, since I can just use the deflate-flate2 feature explicitly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions