v.vcache: store the cache files in ~/.vmodules/.cache by default, allowing for user modules, that are named cache (fix #22459) (#22472)

This commit is contained in:
Delyan Angelov 2024-10-10 16:28:07 +03:00 committed by GitHub
parent a3f6fd5d2f
commit 469b56ef4e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 48 additions and 9 deletions

View File

@ -39,8 +39,9 @@ fn init_settings() VpmSettings {
}
if !is_ci && !is_dbg {
// Log by default:
os.mkdir_all(os.join_path(vmodules_path, 'cache'), mode: 0o700) or { panic(err) }
logger.set_output_path(os.join_path(vmodules_path, 'cache', 'vpm.log'))
cache_path := os.join_path(vmodules_path, '.cache')
os.mkdir_all(cache_path, mode: 0o700) or { panic(err) }
logger.set_output_path(os.join_path(cache_path, 'vpm.log'))
}
return VpmSettings{

View File

@ -14,7 +14,7 @@ const default_vpm_server_urls = ['https://vpm.vlang.io', 'https://vpm.url4e.com'
const vpm_server_urls = rand.shuffle_clone(default_vpm_server_urls) or { [] } // ensure that all queries are distributed fairly
const valid_vpm_commands = ['help', 'search', 'install', 'update', 'upgrade', 'outdated', 'list',
'remove', 'show']
const excluded_dirs = ['cache', 'vlib']
const excluded_dirs = ['.cache', 'vlib']
fn main() {
// This tool is intended to be launched by the v frontend,

View File

@ -34,7 +34,7 @@ fn (mut g Gen) gen_embed_file_init(mut node ast.ComptimeCall) {
if node.embed_file.compression_type == 'none' {
node.embed_file.bytes = file_bytes
} else {
cache_dir := os.join_path(os.vmodules_dir(), 'cache', 'embed_file')
cache_dir := os.join_path(os.vmodules_dir(), '.cache', 'embed_file')
cache_key := rand.ulid()
// cache_key := md5.hexhash(node.embed_file.apath)
if !os.exists(cache_dir) {

View File

@ -1,4 +1,4 @@
## this folder is the first one that will be put in vmodules.
## V uses that to put there its cache too.
## Just ignore it for now.
cache/
.cache/

View File

@ -2,6 +2,12 @@ import os
fn testsuite_begin() {
os.setenv('VCOLORS', 'never', true)
// TODO: remove this, when after vc/v.c *also* uses `_cache`, instead of `cache`:
old_cache_path := os.join_path(os.vmodules_dir(), 'cache')
dump(old_cache_path)
if os.exists(old_cache_path) {
os.rmdir_all(old_cache_path)!
}
}
fn vroot_path(relpath string) string {
@ -28,4 +34,7 @@ fn test_projects_should_run() {
res2 := vrun_ok('run', vroot_path('vlib/v/tests/testdata/modules_in_src/'))
assert res2.trim_space() == 'somemodule somemoduletwo'
res3 := vrun_ok('run', vroot_path('vlib/v/tests/testdata/module_named_cache/'))
assert res3.trim_space().ends_with('cache.a: 123')
}

View File

@ -0,0 +1,3 @@
module cache
pub const a = 123

View File

@ -0,0 +1,8 @@
module main
import cache
fn main() {
println('Hello World!')
dump(cache.a)
}

View File

@ -0,0 +1,7 @@
Module {
name: 'aa'
description: 'AA'
version: '0.0.0'
license: 'MIT'
dependencies: []
}

View File

@ -34,14 +34,25 @@ pub mut:
k2cpath map[string]string // key -> filesystem cache path for the object
}
pub fn new_cache_manager(opts []string) CacheManager {
mut vcache_basepath := os.getenv('VCACHE')
if vcache_basepath == '' {
vcache_basepath = os.join_path(os.vmodules_dir(), 'cache')
fn remove_old_cache_folder() {
// TODO: remove this after bootstrapping the new .cache location, i.e. after 2024-12-01
old_cache_folder := os.join_path(os.vmodules_dir(), 'cache')
if os.exists(old_cache_folder) {
old_readme_file := os.join_path(old_cache_folder, 'README.md')
if os.file_size(old_readme_file) == 254 {
os.rmdir_all(old_cache_folder) or {}
dlog(@FN, 'old_cache_folder: ${old_cache_folder}')
}
}
}
pub fn new_cache_manager(opts []string) CacheManager {
// use a path, that would not conflict with a user installable module. `import .cache` is not valid, => better than just `cache`:
vcache_basepath := os.getenv_opt('VCACHE') or { os.join_path(os.vmodules_dir(), '.cache') }
nlog(@FN, 'vcache_basepath: ${vcache_basepath}\n opts: ${opts}\n os.args: ${os.args.join(' ')}')
dlog(@FN, 'vcache_basepath: ${vcache_basepath} | opts:\n ${opts}')
if !os.is_dir(vcache_basepath) {
remove_old_cache_folder()
os.mkdir_all(vcache_basepath, mode: 0o700) or { panic(err) } // keep directory private
dlog(@FN, 'created folder:\n ${vcache_basepath}')
}