Skip to content
This repository was archived by the owner on Dec 29, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ racer = { version = "2.1.5", default-features = false }
rayon = "1"
rls-analysis = "0.16"
rls-blacklist = "0.1.2"
rls-data = { version = "0.18", features = ["serialize-serde"] }
rls-data = { version = "0.18", features = ["serialize-serde", "serialize-rustc"] }
rls-rustc = "0.5.0"
rls-span = { version = "0.4", features = ["serialize-serde"] }
rls-vfs = "0.4.6"
rustfmt-nightly = "0.99.4"
rustc-serialize = "0.3"
serde = "1.0"
serde_json = "1.0"
serde_derive = "1.0"
Expand Down
100 changes: 100 additions & 0 deletions src/build/external.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! Performs a build using a provided black-box build command, which ought to
//! return a list of save-analysis JSON files to be reloaded by the RLS.
//! Please note that since the command is ran externally (at a file/OS level)
//! this doesn't work with files that are not saved.

use std::io::BufRead;
use std::io::Read;
use std::fs::File;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};

use super::BuildResult;

use rls_data::Analysis;
use log::{log, trace};

/// Performs a build using an external command and interprets the results.
/// The command should output on stdout a list of save-analysis .json files
/// to be reloaded by the RLS.
/// Note: This is *very* experimental and preliminary - this can viewed as
/// an experimentation until a more complete solution emerges.
pub(super) fn build_with_external_cmd<S: AsRef<str>>(cmd_line: S, build_dir: PathBuf) -> BuildResult {
let cmd_line = cmd_line.as_ref();
let (cmd, args) = {
let mut words = cmd_line.split_whitespace();
let cmd = match words.next() {
Some(cmd) => cmd,
None => {
return BuildResult::Err("Specified build_command is empty".into(), None);
}
};
(cmd, words)
};
let spawned = Command::new(&cmd)
.args(args)
.current_dir(&build_dir)
.stdout(Stdio::piped())
.stderr(Stdio::null())
.spawn();

let child = match spawned {
Ok(child) => child,
Err(io) => {
let err_msg = format!("Couldn't execute: {} ({:?})", cmd_line, io.kind());
trace!("{}", err_msg);
return BuildResult::Err(err_msg, Some(cmd_line.to_owned()));
},
};

let reader = std::io::BufReader::new(child.stdout.unwrap());

let files = reader.lines().filter_map(|res| res.ok())
.map(PathBuf::from)
// Relative paths are relative to build command, not RLS itself (cwd may be different)
.map(|path| if !path.is_absolute() { build_dir.join(path) } else { path });

let analyses = match read_analysis_files(files) {
Ok(analyses) => analyses,
Err(cause) => {
let err_msg = format!("Couldn't read analysis data: {}", cause);
return BuildResult::Err(err_msg, Some(cmd_line.to_owned()));
}
};

BuildResult::Success(build_dir.clone(), vec![], analyses, false)
}

/// Reads and deserializes given save-analysis JSON files into corresponding
/// `rls_data::Analysis` for each file. If an error is encountered, a `String`
/// with the error message is returned.
fn read_analysis_files<I>(files: I) -> Result<Vec<Analysis>, String>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a better approach is to add another AnalysisLoader (see https://github.com/nrc/rls-analysis/blob/master/src/loader.rs) implementation to do the loading, that would keep the reading and deserialising of save-analysis encapsulated in rls-analysis rather than both there and the rls.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I originally didn't go with that, since it seems you can't incrementally load analysis with a different AnalysisLoader - we can only create a fresh one with a different loader.

I imagine we'd have to create new rls-analysis API that would allow us to incrementally load-once with a &dyn AnalysisLoader or allow us to replace the AnalysisHost with a new one, retaining current index, but with a different generic AnalysisLoader?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, that sounds like a problem with the AnalysisLoader API, could you file an issue with details to the rls-analysis crate please?

where
I: Iterator,
I::Item: AsRef<Path>
{
let mut analyses = Vec::new();

for path in files {
trace!("external::read_analysis_files: Attempt to read `{}`", path.as_ref().display());

let mut file = File::open(path).map_err(|e| e.to_string())?;
let mut contents = String::new();
file.read_to_string(&mut contents).map_err(|e| e.to_string())?;

let data = rustc_serialize::json::decode(&contents).map_err(|e| e.to_string())?;
analyses.push(data);
}

Ok(analyses)
}
7 changes: 7 additions & 0 deletions src/build/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub mod environment;
mod cargo;
mod rustc;
mod plan;
mod external;

use self::plan::{Plan as BuildPlan, WorkStatus};

Expand Down Expand Up @@ -530,6 +531,12 @@ impl Internals {
// do this so we can load changed code from the VFS, rather than from
// disk).

// Check if an override setting was provided and execute that, instead.
if let Some(ref cmd) = self.config.lock().unwrap().build_command {
let build_dir = self.compilation_cx.lock().unwrap().build_dir.clone();
return external::build_with_external_cmd(cmd, build_dir.unwrap());
}

// Don't hold this lock when we run Cargo.
let needs_to_run_cargo = self.compilation_cx.lock().unwrap().args.is_empty();

Expand Down
11 changes: 11 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,13 @@ pub struct Config {
/// Use provided rustfmt binary instead of the statically linked one.
/// (requires unstable features)
pub rustfmt_path: Option<String>,
/// EXPERIMENTAL (needs unstable features)
/// If set, executes a given program responsible for rebuilding save-analysis
/// to be loaded by the RLS. The program given should output a list of
/// resulting .json files on stdout.
///
/// Implies `build_on_save`: true.
pub build_command: Option<String>,
}

impl Default for Config {
Expand Down Expand Up @@ -189,6 +196,7 @@ impl Default for Config {
full_docs: Inferrable::Inferred(false),
show_hover_context: true,
rustfmt_path: None,
build_command: None,
};
result.normalise();
result
Expand Down Expand Up @@ -226,6 +234,9 @@ impl Config {
self.build_lib = Inferrable::Inferred(false);
self.cfg_test = false;
self.rustfmt_path = None;
self.build_command = None;
} else if self.build_command.is_some() {
self.build_on_save = true;
}
}

Expand Down