Skip to content

fix(cache): include subdir in CacheKey to prevent cross-platform cache collisions#2167

Open
ritankarsaha wants to merge 1 commit intoconda:mainfrom
ritankarsaha:fix/cache-key-subdir-collision
Open

fix(cache): include subdir in CacheKey to prevent cross-platform cache collisions#2167
ritankarsaha wants to merge 1 commit intoconda:mainfrom
ritankarsaha:fix/cache-key-subdir-collision

Conversation

@ritankarsaha
Copy link
Contributor

Description

Fixes the long-standing TODO in cache_key.rs about non-uniqueness of CacheKey across subdirectories.

Problem

CacheKey was keyed only on name + version + build_string. Two packages from different platforms
(e.g. linux-64 and osx-arm64) with identical coordinates would hash to the same cache entry. The
first one cached would be returned for both, silently installing the wrong architecture.

Solution

  • Added subdir: Option to CacheKey and BucketKey
  • From<&PackageRecord> now populates subdir from record.subdir
  • Display includes subdir in the directory name when no origin_hash is present:
    • Before: zlib-1.3.1-hb9d3cd8_2
    • After: zlib-1.3.1-hb9d3cd8_2-linux-64
  • When origin_hash is set (URL-based fetch), it takes precedence — the URL already encodes the
    subdir, so behavior is unchanged for that path
  • Added with_subdir() builder for keys created from CondaArchiveIdentifier (filename-only, no
    subdir info) where the caller knows the platform

Fixes #2166

How Has This Been Tested?

6 unit tests has been added to this properly

The critical test is test_cross_platform_keys_are_distinct — it directly exercises the bug scenario
(two packages from linux-64 vs osx-arm64 with identical coordinates) and asserts they produce different cache directory names.

To run just these tests:
pixi run -- cargo nextest run -p rattler_cache cache_key::tests

Checklist:

  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have added sufficient tests to cover my changes.

Breaking change

Cache directory names for PackageRecord-derived keys will include the subdir suffix. Existing
cached entries without the suffix will be treated as misses and re-fetched. This is the correct and
safe behavior.

…lisions

Signed-off-by: RITANKAR SAHA <ritankar.saha786@gmail.com>

 fix(cache): include subdir in CacheKey to prevent cross-platform collisions

Signed-off-by: RITANKAR SAHA <ritankar.saha786@gmail.com>

 fix(cache): include subdir in CacheKey to prevent cross-platform collisions

Signed-off-by: RITANKAR SAHA <ritankar.saha786@gmail.com>
@ritankarsaha ritankarsaha force-pushed the fix/cache-key-subdir-collision branch from 887d164 to 123de67 Compare March 5, 2026 06:32
@baszalmstra
Copy link
Collaborator

baszalmstra commented Mar 5, 2026

Did you just open the same pr twice with a different description?

Edit: nope it was two different people.

@ritankarsaha
Copy link
Contributor Author

Did you just open the same pr twice with a different description?

Ahh no, I squashed the 3 commits into one, to make it look cleaner, that's all !

@baszalmstra
Copy link
Collaborator

baszalmstra commented Mar 5, 2026

This is a duplicate of #2165, please see my comment there and figure out amongst yourselves which PR should be merged.

@ritankarsaha
Copy link
Contributor Author

This is a duplicate of #2165, please see my comment there and figure out amongst yourselves which PR should be merged.

Ahh I see the PR there, but I think you have left a comment there mentioning that #2165 is incomplete and doesn't fix the issue, also both addresses different issue tags.

@baszalmstra
Copy link
Collaborator

Both PRs suffer from the exact same issue, this one just has more unit tests. But both try to solve the same problem.

@ritankarsaha
Copy link
Contributor Author

Both PRs suffer from the exact same issue, this one just has more unit tests. But both try to solve the same problem.

Ahh lemme know, which one you planning to move on with, happy to be of help and use in both @baszalmstra

@baszalmstra
Copy link
Collaborator

I dont really care. Maybe you can discuss with the other author how you'd solve my comment?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CacheKey not unique across subdirectories — risk of cross-platform cache collisions

2 participants