diff --git a/cmd/tools/vpm/settings.v b/cmd/tools/vpm/settings.v index cdbcafbdac974f..80f89ecf00f831 100644 --- a/cmd/tools/vpm/settings.v +++ b/cmd/tools/vpm/settings.v @@ -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{ diff --git a/cmd/tools/vpm/vpm.v b/cmd/tools/vpm/vpm.v index 6a11e6580d949d..b74a9e495982a1 100644 --- a/cmd/tools/vpm/vpm.v +++ b/cmd/tools/vpm/vpm.v @@ -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, diff --git a/vlib/v/gen/c/embed.v b/vlib/v/gen/c/embed.v index f0205257c71293..890018d56f6866 100644 --- a/vlib/v/gen/c/embed.v +++ b/vlib/v/gen/c/embed.v @@ -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) { diff --git a/vlib/v/tests/multiple_paths_in_vmodules/path1/.gitignore b/vlib/v/tests/multiple_paths_in_vmodules/path1/.gitignore index 38977ffd211367..82ea626f6eae49 100644 --- a/vlib/v/tests/multiple_paths_in_vmodules/path1/.gitignore +++ b/vlib/v/tests/multiple_paths_in_vmodules/path1/.gitignore @@ -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/ diff --git a/vlib/v/tests/projects_that_should_compile_test.v b/vlib/v/tests/projects_that_should_compile_test.v index baf9780447cd4f..bd35b6de4d9b7c 100644 --- a/vlib/v/tests/projects_that_should_compile_test.v +++ b/vlib/v/tests/projects_that_should_compile_test.v @@ -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') } diff --git a/vlib/v/tests/testdata/module_named_cache/src/cache/my_cache.v b/vlib/v/tests/testdata/module_named_cache/src/cache/my_cache.v new file mode 100644 index 00000000000000..ff345da98bc913 --- /dev/null +++ b/vlib/v/tests/testdata/module_named_cache/src/cache/my_cache.v @@ -0,0 +1,3 @@ +module cache + +pub const a = 123 diff --git a/vlib/v/tests/testdata/module_named_cache/src/main.v b/vlib/v/tests/testdata/module_named_cache/src/main.v new file mode 100644 index 00000000000000..2c476aa1e7de58 --- /dev/null +++ b/vlib/v/tests/testdata/module_named_cache/src/main.v @@ -0,0 +1,8 @@ +module main + +import cache + +fn main() { + println('Hello World!') + dump(cache.a) +} diff --git a/vlib/v/tests/testdata/module_named_cache/v.mod b/vlib/v/tests/testdata/module_named_cache/v.mod new file mode 100644 index 00000000000000..7dd2a885c3b05e --- /dev/null +++ b/vlib/v/tests/testdata/module_named_cache/v.mod @@ -0,0 +1,7 @@ +Module { + name: 'aa' + description: 'AA' + version: '0.0.0' + license: 'MIT' + dependencies: [] +} diff --git a/vlib/v/vcache/vcache.v b/vlib/v/vcache/vcache.v index aaf021886ee34f..f0692c9cbdb121 100644 --- a/vlib/v/vcache/vcache.v +++ b/vlib/v/vcache/vcache.v @@ -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}') }