From 875b9f9e4186dd3e3db978d71cf4543b4262086b Mon Sep 17 00:00:00 2001 From: Turiiya <34311583+ttytm@users.noreply.github.com> Date: Sun, 24 Mar 2024 19:36:09 +0100 Subject: [PATCH 1/4] feat: add version aware caching --- config/constants.v | 4 ++++ server/general.v | 48 ++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 44 insertions(+), 8 deletions(-) diff --git a/config/constants.v b/config/constants.v index 235615eb..ac9796af 100644 --- a/config/constants.v +++ b/config/constants.v @@ -31,6 +31,10 @@ pub const analyzer_global_config_path = os.join_path(analyzer_configs_path, anal // cache files for the analyzer. pub const analyzer_caches_path = os.join_path(os.cache_dir(), 'v-analyzer') +// analyzer_caches_path is the path to the directory containing the +// cache files for the analyzer. +pub const analyzer_caches_version_path = os.join_path(analyzer_caches_path, 'version.txt') + // analyzer_stubs_path is the path to the directory containing the // unpacked stub files for the analyzer. pub const analyzer_stubs_path = os.join_path(analyzer_configs_path, 'metadata') diff --git a/server/general.v b/server/general.v index 7b840962..5725c9ed 100644 --- a/server/general.v +++ b/server/general.v @@ -229,6 +229,7 @@ fn (mut ls LanguageServer) setup() { } if ls.cache_dir == '' { + // if custom cache dir is not set, use default ls.setup_cache_dir() } @@ -236,23 +237,54 @@ fn (mut ls LanguageServer) setup() { } fn (mut ls LanguageServer) setup_cache_dir() { - if !os.exists(config.analyzer_caches_path) { - os.mkdir_all(config.analyzer_caches_path) or { - ls.client.log_message('Failed to create analyzer caches directory: ${err}', - .error) + if os.exists(config.analyzer_caches_path) { + if os.exists(config.analyzer_caches_version_path) { + version := os.read_file(config.analyzer_caches_version_path) or { + ls.client.log_message('Failed to read caches version: ${err}', .error) + + loglib.with_fields({ + 'err': err.str() + }).error('Failed to read caches version') + '0' + } + + if version.trim_space() == metadata.build_commit { + return + } + } + + ls.client.log_message('Caches version mismatch, creating new cache', .info) + loglib.info('Caches version mismatch, creating new cache') + + os.rmdir_all(config.analyzer_caches_path) or { + ls.client.log_message('Failed to remove old caches: ${err}', .error) loglib.with_fields({ 'err': err.str() - }).error('Failed to create analyzer caches directory') - return + }).error('Failed to remove old caches') } } - // if custom cache dir is not set, use default ls.cache_dir = config.analyzer_caches_path - ls.client.log_message('Using "${ls.cache_dir}" as cache dir', .info) loglib.info('Using "${ls.cache_dir}" as cache dir') + + os.mkdir_all(config.analyzer_caches_path) or { + ls.client.log_message('Failed to create caches directory: ${err}', .error) + + loglib.with_fields({ + 'err': err.str() + }).error('Failed to create caches directory') + return + } + + os.write_file(config.analyzer_caches_version_path, metadata.build_commit) or { + ls.client.log_message('Failed to write caches version: ${err}', .error) + + loglib.with_fields({ + 'err': err.str() + }).error('Failed to write caches version') + } } fn (mut ls LanguageServer) find_config() string { From fee6f4eb887d18ab39eee08008047c509e288cd0 Mon Sep 17 00:00:00 2001 From: Turiiya <34311583+ttytm@users.noreply.github.com> Date: Sun, 24 Mar 2024 20:04:34 +0100 Subject: [PATCH 2/4] Revert "feat: add version aware caching" This reverts commit 240c5ec9e6d641158d228683b83a0937b0555097. --- config/constants.v | 4 ---- server/general.v | 48 ++++++++-------------------------------------- 2 files changed, 8 insertions(+), 44 deletions(-) diff --git a/config/constants.v b/config/constants.v index ac9796af..235615eb 100644 --- a/config/constants.v +++ b/config/constants.v @@ -31,10 +31,6 @@ pub const analyzer_global_config_path = os.join_path(analyzer_configs_path, anal // cache files for the analyzer. pub const analyzer_caches_path = os.join_path(os.cache_dir(), 'v-analyzer') -// analyzer_caches_path is the path to the directory containing the -// cache files for the analyzer. -pub const analyzer_caches_version_path = os.join_path(analyzer_caches_path, 'version.txt') - // analyzer_stubs_path is the path to the directory containing the // unpacked stub files for the analyzer. pub const analyzer_stubs_path = os.join_path(analyzer_configs_path, 'metadata') diff --git a/server/general.v b/server/general.v index 5725c9ed..7b840962 100644 --- a/server/general.v +++ b/server/general.v @@ -229,7 +229,6 @@ fn (mut ls LanguageServer) setup() { } if ls.cache_dir == '' { - // if custom cache dir is not set, use default ls.setup_cache_dir() } @@ -237,54 +236,23 @@ fn (mut ls LanguageServer) setup() { } fn (mut ls LanguageServer) setup_cache_dir() { - if os.exists(config.analyzer_caches_path) { - if os.exists(config.analyzer_caches_version_path) { - version := os.read_file(config.analyzer_caches_version_path) or { - ls.client.log_message('Failed to read caches version: ${err}', .error) - - loglib.with_fields({ - 'err': err.str() - }).error('Failed to read caches version') - '0' - } - - if version.trim_space() == metadata.build_commit { - return - } - } - - ls.client.log_message('Caches version mismatch, creating new cache', .info) - loglib.info('Caches version mismatch, creating new cache') - - os.rmdir_all(config.analyzer_caches_path) or { - ls.client.log_message('Failed to remove old caches: ${err}', .error) + if !os.exists(config.analyzer_caches_path) { + os.mkdir_all(config.analyzer_caches_path) or { + ls.client.log_message('Failed to create analyzer caches directory: ${err}', + .error) loglib.with_fields({ 'err': err.str() - }).error('Failed to remove old caches') + }).error('Failed to create analyzer caches directory') + return } } + // if custom cache dir is not set, use default ls.cache_dir = config.analyzer_caches_path + ls.client.log_message('Using "${ls.cache_dir}" as cache dir', .info) loglib.info('Using "${ls.cache_dir}" as cache dir') - - os.mkdir_all(config.analyzer_caches_path) or { - ls.client.log_message('Failed to create caches directory: ${err}', .error) - - loglib.with_fields({ - 'err': err.str() - }).error('Failed to create caches directory') - return - } - - os.write_file(config.analyzer_caches_version_path, metadata.build_commit) or { - ls.client.log_message('Failed to write caches version: ${err}', .error) - - loglib.with_fields({ - 'err': err.str() - }).error('Failed to write caches version') - } } fn (mut ls LanguageServer) find_config() string { From 8189abba6f9c59ef633fb727baaf7cd88834cc54 Mon Sep 17 00:00:00 2001 From: Turiiya <34311583+ttytm@users.noreply.github.com> Date: Sun, 24 Mar 2024 20:06:02 +0100 Subject: [PATCH 3/4] use cache key subdir, update after discord discussion https://discord.com/channels/592103645835821068/784039809459027979/1221532757583466677 --- config/constants.v | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/config/constants.v b/config/constants.v index 235615eb..b7e6d036 100644 --- a/config/constants.v +++ b/config/constants.v @@ -1,6 +1,7 @@ module config import os +import metadata // analyzer_name is the name of the analyzer. pub const analyzer_name = 'v-analyzer' @@ -29,7 +30,7 @@ pub const analyzer_global_config_path = os.join_path(analyzer_configs_path, anal // analyzer_caches_path is the path to the directory containing the // cache files for the analyzer. -pub const analyzer_caches_path = os.join_path(os.cache_dir(), 'v-analyzer') +pub const analyzer_caches_path = os.join_path(os.cache_dir(), 'v-analyzer', metadata.build_commit) // analyzer_stubs_path is the path to the directory containing the // unpacked stub files for the analyzer. From 532f38376239de8f1c93644c443526d35b004761 Mon Sep 17 00:00:00 2001 From: Turiiya <34311583+ttytm@users.noreply.github.com> Date: Sun, 24 Mar 2024 20:15:24 +0100 Subject: [PATCH 4/4] include last modification of executable in cache key --- config/constants.v | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/constants.v b/config/constants.v index b7e6d036..0d17a086 100644 --- a/config/constants.v +++ b/config/constants.v @@ -30,7 +30,7 @@ pub const analyzer_global_config_path = os.join_path(analyzer_configs_path, anal // analyzer_caches_path is the path to the directory containing the // cache files for the analyzer. -pub const analyzer_caches_path = os.join_path(os.cache_dir(), 'v-analyzer', metadata.build_commit) +pub const analyzer_caches_path = os.join_path(os.cache_dir(), 'v-analyzer', '${metadata.build_commit}_${os.file_last_mod_unix(os.executable())}') // analyzer_stubs_path is the path to the directory containing the // unpacked stub files for the analyzer.