Skip to content

Commit 25d7e39

Browse files
JoshuaBattysdankel
andauthored
Only copy sway relevant files into LSP tmp memory (#5725)
## Description We were previously copying over every file in the users workspace. This meant that we were copying over a lot of unnecessary files. The language server only needs `.sw` files and `Forc.toml` and `Forc.lock` in temporary memory, so we now only copy over files that match this. --------- Co-authored-by: Sophie Dankel <47993817+sdankel@users.noreply.github.com>
1 parent b787d94 commit 25d7e39

File tree

1 file changed

+29
-8
lines changed

1 file changed

+29
-8
lines changed

sway-lsp/src/core/sync.rs

+29-8
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ use std::{
1818
time::Duration,
1919
};
2020
use sway_types::{SourceEngine, Span};
21+
use sway_utils::{
22+
constants::{LOCK_FILE_NAME, MANIFEST_FILE_NAME},
23+
SWAY_EXTENSION,
24+
};
2125
use tempfile::Builder;
2226
use tokio::task::JoinHandle;
2327

@@ -296,20 +300,37 @@ pub(crate) fn edit_manifest_dependency_paths(
296300
}
297301
}
298302

299-
/// Copy the contents of the current workspace folder into the target directory
303+
/// Copies only the specified files from the source directory to the target directory.
304+
/// This function targets files ending with `.sw`, and the specific files `Forc.toml` and `Forc.lock`.
305+
/// It returns `Ok(true)` if any relevant files were copied over, and `Ok(false)` if no such files were found.
300306
fn copy_dir_contents(
301307
src_dir: impl AsRef<Path>,
302308
target_dir: impl AsRef<Path>,
303-
) -> std::io::Result<()> {
304-
fs::create_dir_all(&target_dir)?;
305-
for entry in fs::read_dir(src_dir)? {
309+
) -> std::io::Result<bool> {
310+
let mut has_relevant_files = false;
311+
for entry in fs::read_dir(&src_dir)? {
306312
let entry = entry?;
313+
let path = entry.path();
307314
let ty = entry.file_type()?;
308315
if ty.is_dir() {
309-
copy_dir_contents(entry.path(), target_dir.as_ref().join(entry.file_name()))?;
310-
} else {
311-
fs::copy(entry.path(), target_dir.as_ref().join(entry.file_name()))?;
316+
// Recursively check the directory; if it has relevant files, create the target directory
317+
if copy_dir_contents(&path, target_dir.as_ref().join(entry.file_name()))? {
318+
has_relevant_files = true;
319+
}
320+
} else if let Some(file_name_os) = path.file_name() {
321+
if let Some(file_name) = file_name_os.to_str() {
322+
if file_name.ends_with(&format!(".{}", SWAY_EXTENSION))
323+
|| file_name == MANIFEST_FILE_NAME
324+
|| file_name == LOCK_FILE_NAME
325+
{
326+
if !has_relevant_files {
327+
fs::create_dir_all(&target_dir)?;
328+
has_relevant_files = true;
329+
}
330+
fs::copy(&path, target_dir.as_ref().join(file_name))?;
331+
}
332+
}
312333
}
313334
}
314-
Ok(())
335+
Ok(has_relevant_files)
315336
}

0 commit comments

Comments
 (0)