@@ -245,6 +245,12 @@ const ONE_WEEK = 7 * 24 * 3600 * 1000;
245245 * Otherwise, it will remove everything that does not match any string in the
246246 * `keepPrefix` set.
247247 * The matching strips and trailing `-$hash` suffix.
248+ *
249+ * Cargo's newer `build-dir` layout (rust-lang/cargo#17258) nests the hash as
250+ * a subdirectory instead of appending it, so entries there are bare package
251+ * names with no suffix to strip. Check for an exact match first so those
252+ * names (which may themselves contain hyphens) aren't mistaken for a
253+ * `<name>-<hash>` entry from the old layout and truncated incorrectly.
248254 */
249255async function rmExcept ( dirName : string , keepPrefix : Set < string > , checkTimestamp = false ) {
250256 const dir = await fs . promises . opendir ( dirName ) ;
@@ -262,14 +268,21 @@ async function rmExcept(dirName: string, keepPrefix: Set<string>, checkTimestamp
262268
263269 let name = dirent . name ;
264270
265- // strip the trailing hash
266- const idx = name . lastIndexOf ( "-" ) ;
267- if ( idx !== - 1 ) {
268- name = name . slice ( 0 , idx ) ;
269- }
271+ // in Cargo's V1 layout, all packages are suffixed by their hash.
272+ // in V2, all package hashes are subdirectories instead.
273+ // Check both possible naming standards for packages and accept either.
270274
275+ // v2 package format
271276 if ( ! keepPrefix . has ( name ) ) {
272- await rm ( dir . path , dirent ) ;
277+ // now check for v1 package format
278+ // strip the trailing hash
279+ const idx = name . lastIndexOf ( "-" ) ;
280+ if ( idx !== - 1 ) {
281+ name = name . slice ( 0 , idx ) ;
282+ }
283+ if ( ! keepPrefix . has ( name ) ) {
284+ await rm ( dir . path , dirent ) ;
285+ }
273286 }
274287 }
275288}
0 commit comments