Skip to content

Commit

Permalink
fix(repl): disable language server document preloading in the repl (d…
Browse files Browse the repository at this point in the history
…enoland#18543)

This was an oversight because the repl uses the language server under
the hood. Also, never preloads from a root directory.

Part of denoland#18538
  • Loading branch information
dsherret committed Apr 1, 2023
1 parent ae1ba2a commit bac8e4f
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 27 deletions.
20 changes: 20 additions & 0 deletions cli/lsp/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ pub enum TestingNotification {
Progress(testing_lsp_custom::TestRunProgressParams),
}

#[derive(Debug, Default, Copy, Clone, PartialEq, Eq)]
pub enum LspClientKind {
#[default]
CodeEditor,
Repl,
}

#[derive(Clone)]
pub struct Client(Arc<dyn ClientTrait>);

Expand All @@ -44,6 +51,10 @@ impl Client {
Self(Arc::new(ReplClient))
}

pub fn kind(&self) -> LspClientKind {
self.0.kind()
}

/// Gets additional methods that should only be called outside
/// the LSP's lock to prevent deadlocking scenarios.
pub fn when_outside_lsp_lock(&self) -> OutsideLockClient {
Expand Down Expand Up @@ -149,6 +160,7 @@ impl OutsideLockClient {

#[async_trait]
trait ClientTrait: Send + Sync {
fn kind(&self) -> LspClientKind;
async fn publish_diagnostics(
&self,
uri: lsp::Url,
Expand Down Expand Up @@ -177,6 +189,10 @@ struct TowerClient(tower_lsp::Client);

#[async_trait]
impl ClientTrait for TowerClient {
fn kind(&self) -> LspClientKind {
LspClientKind::CodeEditor
}

async fn publish_diagnostics(
&self,
uri: lsp::Url,
Expand Down Expand Up @@ -296,6 +312,10 @@ struct ReplClient;

#[async_trait]
impl ClientTrait for ReplClient {
fn kind(&self) -> LspClientKind {
LspClientKind::Repl
}

async fn publish_diagnostics(
&self,
_uri: lsp::Url,
Expand Down
2 changes: 1 addition & 1 deletion cli/lsp/completions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ mod tests {
source_fixtures: &[(&str, &str)],
location: &Path,
) -> Documents {
let mut documents = Documents::new(location);
let mut documents = Documents::new(location, Default::default());
for (specifier, source, version, language_id) in fixtures {
let specifier =
resolve_url(specifier).expect("failed to create specifier");
Expand Down
2 changes: 1 addition & 1 deletion cli/lsp/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1079,7 +1079,7 @@ mod tests {
location: &Path,
maybe_import_map: Option<(&str, &str)>,
) -> StateSnapshot {
let mut documents = Documents::new(location);
let mut documents = Documents::new(location, Default::default());
for (specifier, source, version, language_id) in fixtures {
let specifier =
resolve_url(specifier).expect("failed to create specifier");
Expand Down
96 changes: 73 additions & 23 deletions cli/lsp/documents.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.

use super::cache::calculate_fs_version;
use super::client::LspClientKind;
use super::text::LineIndex;
use super::tsc;
use super::tsc::AssetDocument;
Expand Down Expand Up @@ -815,6 +816,8 @@ pub struct Documents {
open_docs: HashMap<ModuleSpecifier, Document>,
/// Documents stored on the file system.
file_system_docs: Arc<Mutex<FileSystemDocuments>>,
/// Kind of the client that is using the documents.
lsp_client_kind: LspClientKind,
/// Hash of the config used for resolution. When the hash changes we update
/// dependencies.
resolver_config_hash: u64,
Expand All @@ -834,13 +837,14 @@ pub struct Documents {
}

impl Documents {
pub fn new(location: &Path) -> Self {
pub fn new(location: &Path, lsp_client_kind: LspClientKind) -> Self {
Self {
cache: HttpCache::new(location),
dirty: true,
dependents_map: Default::default(),
open_docs: HashMap::default(),
file_system_docs: Default::default(),
lsp_client_kind,
resolver_config_hash: 0,
imports: Default::default(),
resolver: CliGraphResolver::default(),
Expand Down Expand Up @@ -1248,33 +1252,50 @@ impl Documents {

// update the file system documents
let mut fs_docs = self.file_system_docs.lock();
let mut not_found_docs =
fs_docs.docs.keys().cloned().collect::<HashSet<_>>();
let open_docs = &mut self.open_docs;

for specifier in PreloadDocumentFinder::from_root_urls(&root_urls) {
// mark this document as having been found
not_found_docs.remove(&specifier);
match self.lsp_client_kind {
LspClientKind::CodeEditor => {
let mut not_found_docs =
fs_docs.docs.keys().cloned().collect::<HashSet<_>>();
let open_docs = &mut self.open_docs;

log::debug!("Preloading documents from root urls...");
for specifier in PreloadDocumentFinder::from_root_urls(&root_urls) {
// mark this document as having been found
not_found_docs.remove(&specifier);

if !open_docs.contains_key(&specifier)
&& !fs_docs.docs.contains_key(&specifier)
{
fs_docs.refresh_document(&self.cache, resolver, &specifier);
} else {
// update the existing entry to have the new resolver
if let Some(doc) = fs_docs.docs.get_mut(&specifier) {
if let Some(new_doc) = doc.maybe_with_new_resolver(resolver) {
*doc = new_doc;
}
}
}
}

if !open_docs.contains_key(&specifier)
&& !fs_docs.docs.contains_key(&specifier)
{
fs_docs.refresh_document(&self.cache, resolver, &specifier);
} else {
// update the existing entry to have the new resolver
if let Some(doc) = fs_docs.docs.get_mut(&specifier) {
// clean up and remove any documents that weren't found
for uri in not_found_docs {
fs_docs.docs.remove(&uri);
}
}
LspClientKind::Repl => {
// This log statement is used in the tests to ensure preloading doesn't
// happen, which is not useful in the repl and could be very expensive
// if the repl is launched from a directory with a lot of descendants.
log::debug!("Skipping document preload for repl.");

// for the repl, just update to use the new resolver
for doc in fs_docs.docs.values_mut() {
if let Some(new_doc) = doc.maybe_with_new_resolver(resolver) {
*doc = new_doc;
}
}
}
}

// clean up and remove any documents that weren't found
for uri in not_found_docs {
fs_docs.docs.remove(&uri);
}

fs_docs.dirty = true;
}

Expand Down Expand Up @@ -1516,6 +1537,14 @@ struct PreloadDocumentFinder {
}

impl PreloadDocumentFinder {
fn is_allowed_root_dir(dir_path: &Path) -> bool {
if dir_path.parent().is_none() {
// never search the root directory of a drive
return false;
}
true
}

pub fn from_root_urls(root_urls: &Vec<Url>) -> Self {
let mut finder = PreloadDocumentFinder {
pending_dirs: Default::default(),
Expand All @@ -1525,7 +1554,9 @@ impl PreloadDocumentFinder {
for root_url in root_urls {
if let Ok(path) = root_url.to_file_path() {
if path.is_dir() {
finder.pending_dirs.push(path);
if Self::is_allowed_root_dir(&path) {
finder.pending_dirs.push(path);
}
} else {
finder.pending_files.push(path);
}
Expand Down Expand Up @@ -1655,7 +1686,7 @@ mod tests {

fn setup(temp_dir: &TempDir) -> (Documents, PathBuf) {
let location = temp_dir.path().join("deps");
let documents = Documents::new(&location);
let documents = Documents::new(&location, Default::default());
(documents, location)
}

Expand Down Expand Up @@ -1905,4 +1936,23 @@ console.log(b, "hello deno");
]
);
}

#[test]
pub fn test_pre_load_document_finder_disallowed_dirs() {
if cfg!(windows) {
let paths =
PreloadDocumentFinder::from_root_urls(&vec![
Url::parse("file:https:///c:/").unwrap()
])
.collect::<Vec<_>>();
assert_eq!(paths, vec![]);
} else {
let paths =
PreloadDocumentFinder::from_root_urls(&vec![
Url::parse("file:https:///").unwrap()
])
.collect::<Vec<_>>();
assert_eq!(paths, vec![]);
}
}
}
2 changes: 1 addition & 1 deletion cli/lsp/language_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ impl Inner {
ModuleRegistry::new(&module_registries_location, http_client.clone())
.unwrap();
let location = dir.deps_folder_path();
let documents = Documents::new(&location);
let documents = Documents::new(&location, client.kind());
let deps_http_cache = HttpCache::new(&location);
let cache_metadata = cache::CacheMetadata::new(deps_http_cache.clone());
let performance = Arc::new(Performance::default());
Expand Down
2 changes: 1 addition & 1 deletion cli/lsp/tsc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3530,7 +3530,7 @@ mod tests {
fixtures: &[(&str, &str, i32, LanguageId)],
location: &Path,
) -> StateSnapshot {
let mut documents = Documents::new(location);
let mut documents = Documents::new(location, Default::default());
for (specifier, source, version, language_id) in fixtures {
let specifier =
resolve_url(specifier).expect("failed to create specifier");
Expand Down
14 changes: 14 additions & 0 deletions cli/tests/integration/repl_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use test_util::assert_contains;
use test_util::assert_ends_with;
use test_util::assert_not_contains;
use util::TempDir;
use util::TestContext;
use util::TestContextBuilder;

#[test]
Expand Down Expand Up @@ -957,3 +958,16 @@ fn package_json_uncached_no_error() {
console.expect("42")
});
}

#[test]
fn closed_file_pre_load_does_not_occur() {
TestContext::default()
.new_command()
.args_vec(["repl", "-A", "--log-level=debug"])
.with_pty(|console| {
assert_contains!(
console.all_output(),
"Skipping document preload for repl.",
);
});
}

0 comments on commit bac8e4f

Please sign in to comment.