npm / cacache

npm's content-addressable cache
Other
280 stars 31 forks source link

[BUG] `rm.all` doesn't delete anything on Windows #165

Closed peterhirn closed 1 year ago

peterhirn commented 1 year ago

Is there an existing issue for this?

Current Behavior

import cacache from "cacache"

const cachePath = "./cache"
const key = "test-key"

await cacache.put(cachePath, key, "10293801983029384")

const cached = await cacache.get(cachePath, key)
console.log(`Data: ${cached.data}`)

await cacache.rm.all(cachePath)

const cached2 = await cacache.get(cachePath, key)
console.log(`Data after rm.all: ${cached2.data}`)

Output

Data: 10293801983029384
Data after rm.all: 10293801983029384

After rm.all cache keys still exist and nothing is delete from the filesystem.

Expected Behavior

Exception: NotFoundError: No cache entry for test-key found in ./cache

Works as expected in version 16.1.3

Steps To Reproduce

No response

Environment

peterhirn commented 1 year ago

The problem is lib/util/glob.js

https://www.npmjs.com/package/glob#windows

The pattern argument is cache\*(content-*|index-*) on windows, cache/*(content-*|index-*) on linux.

Maybe globify should be something like this

const globify = (pattern) => pattern.replace(/\\+|\/{2,}/g, '/')

But I really don't understand why double forward slashes are replaced at all.

supita commented 1 year ago

I'm having an issue where the stats from cacache.verify are not properly reported on Windows because the files are not detected.

The root of the problem is the same: globify function is not replacing to forward-slashes as glob library is expecting to handle Windows paths.

I found that before globify was at lib/verify.js, the replacement of the forward-slashes was made correctly.

const globify = pattern => pattern.split('\\').join('/')

But when created the file lib/util/glob.js and moved globify there, it was changed to

const globify = pattern => pattern.split('//').join('/')

which is causing problems when glob is used for obtaining stats and cleanup in the case of verify

wraithgar commented 1 year ago

Thank you @supita https://github.com/npm/cacache/pull/209