Skip to content

Commit 87fed9a

Browse files
authored
[move-ide] Reset pre-compiled libs on manifest change (#17218)
## Description This PR adds clearing of pre-compiled dependencies for a given package if the manifest file of this package changes. ## Test plan Tested manually to verify that pre-compiled dependencies are re-computed on manifest file change
1 parent 57f41eb commit 87fed9a

File tree

3 files changed

+44
-16
lines changed

3 files changed

+44
-16
lines changed

crates/move-analyzer/src/bin/move-analyzer.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use lsp_types::{
1111
HoverProviderCapability, OneOf, SaveOptions, TextDocumentSyncCapability, TextDocumentSyncKind,
1212
TextDocumentSyncOptions, TypeDefinitionProviderCapability, WorkDoneProgressOptions,
1313
};
14-
use move_compiler::{command_line::compiler::FullyCompiledProgram, linters::LintLevel};
14+
use move_compiler::linters::LintLevel;
1515
use std::{
1616
collections::BTreeMap,
1717
path::PathBuf,
@@ -53,7 +53,7 @@ fn main() {
5353
let (connection, io_threads) = Connection::stdio();
5454
let symbols = Arc::new(Mutex::new(symbols::empty_symbols()));
5555
let pkg_deps = Arc::new(Mutex::new(
56-
BTreeMap::<PathBuf, Arc<FullyCompiledProgram>>::new(),
56+
BTreeMap::<PathBuf, symbols::PrecompiledPkgDeps>::new(),
5757
));
5858
let ide_files_root: VfsPath = MemoryFS::new().into();
5959
let context = Context {
@@ -260,7 +260,7 @@ fn on_request(
260260
context: &Context,
261261
request: &Request,
262262
ide_files_root: VfsPath,
263-
pkg_dependencies: Arc<Mutex<BTreeMap<PathBuf, Arc<FullyCompiledProgram>>>>,
263+
pkg_dependencies: Arc<Mutex<BTreeMap<PathBuf, symbols::PrecompiledPkgDeps>>>,
264264
shutdown_request_received: bool,
265265
) -> bool {
266266
if shutdown_request_received {

crates/move-analyzer/src/completion.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@
44

55
use crate::{
66
context::Context,
7-
symbols::{self, DefInfo, DefLoc, SymbolicatorRunner, Symbols},
7+
symbols::{self, DefInfo, DefLoc, PrecompiledPkgDeps, SymbolicatorRunner, Symbols},
88
};
99
use lsp_server::Request;
1010
use lsp_types::{
1111
CompletionItem, CompletionItemKind, CompletionParams, Documentation, InsertTextFormat, Position,
1212
};
1313
use move_command_line_common::files::FileHash;
1414
use move_compiler::{
15-
command_line::compiler::FullyCompiledProgram,
1615
editions::Edition,
1716
expansion::ast::Visibility,
1817
linters::LintLevel,
@@ -363,7 +362,7 @@ pub fn on_completion_request(
363362
context: &Context,
364363
request: &Request,
365364
ide_files_root: VfsPath,
366-
pkg_dependencies: Arc<Mutex<BTreeMap<PathBuf, Arc<FullyCompiledProgram>>>>,
365+
pkg_dependencies: Arc<Mutex<BTreeMap<PathBuf, PrecompiledPkgDeps>>>,
367366
) {
368367
eprintln!("handling completion request");
369368
let parameters = serde_json::from_value::<CompletionParams>(request.params.clone())

crates/move-analyzer/src/symbols.rs

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,16 @@ use move_symbol_pool::Symbol;
116116
/// go-to-references to the IDE.
117117
pub const DEFS_AND_REFS_SUPPORT: bool = true;
118118

119+
const MANIFEST_FILE_NAME: &str = "Move.toml";
120+
121+
#[derive(Clone)]
122+
pub struct PrecompiledPkgDeps {
123+
/// Hash of the manifest file for a given package
124+
manifest_hash: Option<FileHash>,
125+
/// Precompiled deps
126+
deps: Arc<FullyCompiledProgram>,
127+
}
128+
119129
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Copy)]
120130
/// Location of a definition's identifier
121131
pub struct DefLoc {
@@ -731,7 +741,7 @@ impl SymbolicatorRunner {
731741
pub fn new(
732742
ide_files_root: VfsPath,
733743
symbols: Arc<Mutex<Symbols>>,
734-
pkg_deps: Arc<Mutex<BTreeMap<PathBuf, Arc<FullyCompiledProgram>>>>,
744+
pkg_deps: Arc<Mutex<BTreeMap<PathBuf, PrecompiledPkgDeps>>>,
735745
sender: Sender<Result<BTreeMap<PathBuf, Vec<Diagnostic>>>>,
736746
lint: LintLevel,
737747
) -> Self {
@@ -851,7 +861,7 @@ impl SymbolicatorRunner {
851861
let mut current_path_opt = Some(starting_path);
852862
while current_path_opt.is_some() {
853863
let current_path = current_path_opt.unwrap();
854-
let manifest_path = current_path.join("Move.toml");
864+
let manifest_path = current_path.join(MANIFEST_FILE_NAME);
855865
if manifest_path.is_file() {
856866
return Some(current_path.to_path_buf());
857867
}
@@ -1051,7 +1061,7 @@ impl Symbols {
10511061
/// actually (re)computed and the diagnostics are returned, the old symbolic information should
10521062
/// be retained even if it's getting out-of-date.
10531063
pub fn get_symbols(
1054-
pkg_dependencies: Arc<Mutex<BTreeMap<PathBuf, Arc<FullyCompiledProgram>>>>,
1064+
pkg_dependencies: Arc<Mutex<BTreeMap<PathBuf, PrecompiledPkgDeps>>>,
10551065
ide_files_root: VfsPath,
10561066
pkg_path: &Path,
10571067
lint: LintLevel,
@@ -1077,6 +1087,19 @@ pub fn get_symbols(
10771087
VfsPath::new(PhysicalFS::new("/")),
10781088
]));
10791089

1090+
let manifest_file = overlay_fs_root
1091+
.join(pkg_path.to_string_lossy())
1092+
.and_then(|p| p.join(MANIFEST_FILE_NAME))
1093+
.and_then(|p| p.open_file());
1094+
1095+
let manifest_hash = if let Ok(mut f) = manifest_file {
1096+
let mut contents = String::new();
1097+
let _ = f.read_to_string(&mut contents);
1098+
Some(FileHash::new(&contents))
1099+
} else {
1100+
None
1101+
};
1102+
10801103
// get source files to be able to correlate positions (in terms of byte offsets) with actual
10811104
// file locations (in terms of line/column numbers)
10821105
let source_files = file_sources(&resolution_graph, overlay_fs_root.clone());
@@ -1120,18 +1143,24 @@ pub fn get_symbols(
11201143

11211144
let mut pkg_deps = pkg_dependencies.lock().unwrap();
11221145
let compiled_deps = match pkg_deps.get(pkg_path) {
1123-
Some(d) => {
1146+
Some(d) if manifest_hash.is_some() && manifest_hash == d.manifest_hash => {
11241147
eprintln!("found pre-compiled libs for {:?}", pkg_path);
1125-
Some(d.clone())
1148+
Some(d.deps.clone())
11261149
}
1127-
None => construct_pre_compiled_lib(src_deps, None, compiler_flags)
1150+
_ => construct_pre_compiled_lib(src_deps, None, compiler_flags)
11281151
.ok()
11291152
.and_then(|pprog_and_comments_res| pprog_and_comments_res.ok())
1130-
.map(|lib| {
1153+
.map(|libs| {
11311154
eprintln!("created pre-compiled libs for {:?}", pkg_path);
1132-
let res = Arc::new(lib);
1133-
pkg_deps.insert(pkg_path.to_path_buf(), res.clone());
1134-
res
1155+
let deps = Arc::new(libs);
1156+
pkg_deps.insert(
1157+
pkg_path.to_path_buf(),
1158+
PrecompiledPkgDeps {
1159+
manifest_hash,
1160+
deps: deps.clone(),
1161+
},
1162+
);
1163+
deps
11351164
}),
11361165
};
11371166
if compiled_deps.is_some() {

0 commit comments

Comments
 (0)