Skip to content

Commit

Permalink
v.vcache: store the cache files in ~/.vmodules/.cache by default, a…
Browse files Browse the repository at this point in the history
…llowing for user modules, that are named `cache` (fix #22459) (#22472)
  • Loading branch information
spytheman authored Oct 10, 2024
1 parent a3f6fd5 commit 469b56e
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 9 deletions.
5 changes: 3 additions & 2 deletions cmd/tools/vpm/settings.v
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
2 changes: 1 addition & 1 deletion cmd/tools/vpm/vpm.v
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion vlib/v/gen/c/embed.v
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion vlib/v/tests/multiple_paths_in_vmodules/path1/.gitignore
Original file line number Diff line number Diff line change
@@ -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/
9 changes: 9 additions & 0 deletions vlib/v/tests/projects_that_should_compile_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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')
}
3 changes: 3 additions & 0 deletions vlib/v/tests/testdata/module_named_cache/src/cache/my_cache.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module cache

pub const a = 123
8 changes: 8 additions & 0 deletions vlib/v/tests/testdata/module_named_cache/src/main.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module main

import cache

fn main() {
println('Hello World!')
dump(cache.a)
}
7 changes: 7 additions & 0 deletions vlib/v/tests/testdata/module_named_cache/v.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Module {
name: 'aa'
description: 'AA'
version: '0.0.0'
license: 'MIT'
dependencies: []
}
19 changes: 15 additions & 4 deletions vlib/v/vcache/vcache.v
Original file line number Diff line number Diff line change
Expand Up @@ -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}')
}
Expand Down

0 comments on commit 469b56e

Please sign in to comment.