Skip to content

Commit

Permalink
fix(rust_common): fix compatibility with 2022-12-21 (#5482)
Browse files Browse the repository at this point in the history
  • Loading branch information
wcalandro committed Dec 29, 2022
1 parent 872432a commit 8d42287
Show file tree
Hide file tree
Showing 12 changed files with 458 additions and 471 deletions.
147 changes: 77 additions & 70 deletions Cargo.Bazel.lock

Large diffs are not rendered by default.

606 changes: 310 additions & 296 deletions cargo-bazel-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion external.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def _rule_dependencies():
rules_proto_dependencies()
py_repositories()
rules_rust_dependencies()
rust_register_toolchains(version = "nightly", iso_date = "2022-11-02", dev_components = True, include_rustc_srcs = True)
rust_register_toolchains(version = "nightly", iso_date = "2022-12-21", dev_components = True, include_rustc_srcs = True)
rust_proto_repositories(register_default_toolchain = False)
rust_analyzer_deps()
crate_universe_dependencies()
Expand Down
48 changes: 17 additions & 31 deletions kythe/rust/extractor/src/bin/extractor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ fn main() -> Result<()> {
let mut out_dir_inputs: Vec<String> = Vec::new();
if let Some(out_dir) = out_dir_var {
let absolute_out_dir = out_dir.replace("${pwd}", pwd.to_str().unwrap());
let glob_pattern = format!("{}/**/*", absolute_out_dir);
let glob_pattern = format!("{absolute_out_dir}/**/*");
for path in glob(&glob_pattern).unwrap().flatten() {
if path.is_file() {
out_dir_inputs.push(path.display().to_string());
Expand All @@ -91,23 +91,13 @@ fn main() -> Result<()> {
.get(0)
.ok_or_else(|| anyhow!("Failed to get output file from spawn info"))?;

// Get the name of the output file (without extension)
let output_file_name = PathBuf::from(build_output_path)
.with_extension("")
.file_name()
.ok_or_else(|| anyhow!("Failed to extract file name from build output path"))?
.to_str()
.ok_or_else(|| anyhow!("Failed to convert build output path file name to string"))?
.to_string();

// Create temporary directory and run the analysis
let tmp_dir = TempDir::new("rust_extractor")
.with_context(|| "Failed to make temporary directory".to_string())?;
let build_target_arguments: Vec<String> = spawn_info.get_argument().to_vec();
save_analysis::generate_save_analysis(
build_target_arguments.clone(),
PathBuf::from(tmp_dir.path()),
&output_file_name,
)?;

// Create the output kzip
Expand Down Expand Up @@ -153,7 +143,7 @@ fn main() -> Result<()> {
}

// Add save analysis to kzip
let save_analysis_path = analysis_path_string(&output_file_name, tmp_dir.path())?;
let save_analysis_path = analysis_path_string(tmp_dir.path())?;
let save_analysis_vname = create_vname(&mut vname_rules, &save_analysis_path, &default_corpus);
kzip_add_required_input(
&save_analysis_path,
Expand Down Expand Up @@ -187,7 +177,7 @@ fn main() -> Result<()> {
.with_context(|| "Failed to serialize IndexedCompilation to bytes".to_string())?;
let indexed_compilation_digest = sha256digest(&indexed_compilation_bytes);
kzip_add_file(
format!("root/pbunits/{}", indexed_compilation_digest),
format!("root/pbunits/{indexed_compilation_digest}"),
&indexed_compilation_bytes,
&mut kzip,
)?;
Expand Down Expand Up @@ -273,13 +263,13 @@ fn kzip_add_required_input(
required_inputs: &mut Vec<CompilationUnit_FileInput>,
) -> Result<()> {
let mut source_file = File::open(file_path_string)
.with_context(|| format!("Failed open file {:?}", file_path_string))?;
.with_context(|| format!("Failed open file {file_path_string}"))?;
let mut source_file_contents: Vec<u8> = Vec::new();
source_file
.read_to_end(&mut source_file_contents)
.with_context(|| format!("Failed read file {:?}", file_path_string))?;
.with_context(|| format!("Failed read file {file_path_string}"))?;
let digest = sha256digest(&source_file_contents);
kzip_add_file(format!("root/files/{}", digest), &source_file_contents, zip_writer)?;
kzip_add_file(format!("root/files/{digest}"), &source_file_contents, zip_writer)?;

// Generate FileInput and add it to the list of required inputs
let mut file_input = CompilationUnit_FileInput::new();
Expand All @@ -306,26 +296,22 @@ fn kzip_add_file(
) -> Result<()> {
zip_writer
.start_file(&file_name, FileOptions::default())
.with_context(|| format!("Failed create file in kzip: {:?}", file_name))?;
.with_context(|| format!("Failed create file in kzip: {file_name}"))?;
zip_writer
.write_all(file_bytes)
.with_context(|| format!("Failed write file contents to kzip: {:?}", file_name))?;
.with_context(|| format!("Failed write file contents to kzip: {file_name}"))?;
Ok(())
}

/// Find the path of the save_analysis file using the build target's
/// output file name and the temporary base directory
fn analysis_path_string(output_file_name: &str, temp_dir_path: &Path) -> Result<String> {
// The path should always be {tmp_dir}/save-analysis/{output_file_name}.json
let expected_path =
temp_dir_path.join("save-analysis").join(output_file_name).with_extension("json");
if expected_path.exists() {
expected_path
.to_str()
.ok_or_else(|| anyhow!("save_analysis file path is not valid UTF-8"))
.map(String::from)
} else {
Err(anyhow!("Failed to find save-analysis file in {:?}", temp_dir_path))
/// Find the path of the save_analysis file in the temporary directory
fn analysis_path_string(temp_dir_path: &Path) -> Result<String> {
let glob_pattern = format!("{}/save-analysis/*.json", temp_dir_path.display());
let mut glob_result = glob(&glob_pattern).context("Failed to glob for save_analysis file")?;
let first_path =
glob_result.next().ok_or_else(|| anyhow!("Failed to find save_analysis file"))?;
match first_path {
Ok(path) => Ok(path.display().to_string()),
Err(e) => Err(e.into()),
}
}

Expand Down
10 changes: 3 additions & 7 deletions kythe/rust/extractor/src/bin/save_analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,9 @@ use std::path::{Path, PathBuf};
///
/// * `arguments` - The Bazel arguments extracted from the extra action protobuf
/// * `output_dir` - The base directory to output the save_analysis
pub fn generate_save_analysis(
arguments: Vec<String>,
output_dir: PathBuf,
output_file_name: &str,
) -> Result<()> {
pub fn generate_save_analysis(arguments: Vec<String>, output_dir: PathBuf) -> Result<()> {
let rustc_arguments = generate_arguments(arguments, &output_dir)?;
kythe_rust_extractor::generate_analysis(rustc_arguments, output_dir, output_file_name)
kythe_rust_extractor::generate_analysis(rustc_arguments, output_dir)
.map_err(|_| anyhow!("Failed to generate save_analysis"))?;
Ok(())
}
Expand Down Expand Up @@ -59,7 +55,7 @@ fn generate_arguments(arguments: Vec<String>, output_dir: &Path) -> Result<Vec<S
.ok_or_else(|| anyhow!("Could not find the output directory argument: {:?}", arguments))?;
let outdir_str =
output_dir.to_str().ok_or_else(|| anyhow!("Couldn't convert temporary path to string"))?;
rustc_arguments[outdir_position] = format!("--out-dir={}", outdir_str);
rustc_arguments[outdir_position] = format!("--out-dir={outdir_str}");

// Check if there is a file containing environment variables
let env_file_position = arguments.iter().position(|arg| arg == "--env-file");
Expand Down
19 changes: 7 additions & 12 deletions kythe/rust/extractor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,14 @@ use std::path::PathBuf;
/// first element must be an empty string.
/// The save_analysis JSON output file will be located at
/// {output_dir}/save-analysis/{crate_name}.json
pub fn generate_analysis(
rustc_arguments: Vec<String>,
output_dir: PathBuf,
output_file_name: &str,
) -> Result<(), String> {
pub fn generate_analysis(rustc_arguments: Vec<String>, output_dir: PathBuf) -> Result<(), String> {
let first_arg =
rustc_arguments.get(0).ok_or_else(|| "Arguments vector should not be empty".to_string())?;
if first_arg != &"".to_string() {
return Err("The first argument must be an empty string".into());
}

let mut callback_shim = CallbackShim::new(output_dir, output_file_name.to_string());
let mut callback_shim = CallbackShim::new(output_dir);

rustc_driver::catch_fatal_errors(|| {
RunCompiler::new(&rustc_arguments, &mut callback_shim).run()
Expand All @@ -57,13 +53,12 @@ pub fn generate_analysis(
#[derive(Default)]
struct CallbackShim {
output_dir: PathBuf,
output_file_name: String,
}

impl CallbackShim {
/// Create a new CallbackShim that dumps save_analysis files to `output_dir`
pub fn new(output_dir: PathBuf, output_file_name: String) -> Self {
Self { output_dir, output_file_name }
pub fn new(output_dir: PathBuf) -> Self {
Self { output_dir }
}
}

Expand All @@ -79,7 +74,7 @@ impl Callbacks for CallbackShim {
queries: &'tcx Queries<'tcx>,
) -> Compilation {
let input = compiler.input();
let crate_name = queries.crate_name().unwrap().peek().clone();
let crate_name = *queries.crate_name().unwrap().peek();

// Configure the save_analysis to include full documentation.
// Normally this would be set using a `rls_data::config::Config` struct on the
Expand All @@ -98,10 +93,10 @@ impl Callbacks for CallbackShim {
queries.global_ctxt().unwrap().peek_mut().enter(|tcx| {
rustc_save_analysis::process_crate(
tcx,
&crate_name,
crate_name,
input,
None,
DumpHandler::new(Some(self.output_dir.as_path()), &self.output_file_name),
DumpHandler::new(Some(self.output_dir.as_path()), crate_name),
)
});

Expand Down
23 changes: 10 additions & 13 deletions kythe/rust/extractor/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,16 @@ fn main() -> Result<()> {
"--".to_string(),
"rustc".to_string(),
rust_test_source.clone(),
format!("-L{}", sysroot),
format!("-L{sysroot}"),
"--crate-name=test_crate".to_string(),
format!("--out-dir={}", temp_dir_str),
format!("--out-dir={temp_dir_str}"),
];
spawn_info.set_argument(protobuf::RepeatedField::from_vec(arguments.clone()));

let input_files: Vec<String> = vec![rust_test_source];
spawn_info.set_input_file(protobuf::RepeatedField::from_vec(input_files));

let output_key = format!("{}/libtest_crate-1234", temp_dir_str);
let output_key = format!("{temp_dir_str}/libtest_crate-1234");
let output_files: Vec<String> = vec![output_key.clone()];
spawn_info.set_output_file(protobuf::RepeatedField::from_vec(output_files));

Expand Down Expand Up @@ -150,10 +150,10 @@ fn correct_arguments_succeed(
let vnames_path = r.rlocation("io_kythe/external/io_kythe/kythe/data/vnames_config.json");

let extractor_path = std::env::var("EXTRACTOR_PATH").expect("Couldn't find extractor path");
let kzip_path_str = format!("{}/output.kzip", temp_dir_str);
let kzip_path_str = format!("{temp_dir_str}/output.kzip");
let exit_status = Command::new(extractor_path)
.arg(format!("--extra_action={}", extra_action_path_str))
.arg(format!("--output={}", kzip_path_str))
.arg(format!("--extra_action={extra_action_path_str}"))
.arg(format!("--output={kzip_path_str}"))
.arg(format!("--vnames_config={}", vnames_path.to_string_lossy()))
.status()
.unwrap();
Expand Down Expand Up @@ -231,8 +231,7 @@ fn correct_arguments_succeed(
let source_file_path = source_input.get_info().get_path();
assert!(
source_file_path.contains("kythe/rust/extractor/main.rs"),
"Path for the first required input doesn't contain \"kythe/rust/extractor/main.rs\": {}",
source_file_path
"Path for the first required input doesn't contain \"kythe/rust/extractor/main.rs\": {source_file_path}"
);
assert_eq!(
source_input.get_info().get_digest(),
Expand All @@ -246,8 +245,7 @@ fn correct_arguments_succeed(
let out_dir_input_path = out_dir_input.get_info().get_path();
assert!(
out_dir_input_path.contains("out_dir/input.rs"),
"Path for the second required input doesn't contain \"out_dir/input.rs\": {}",
out_dir_input_path
"Path for the second required input doesn't contain \"out_dir/input.rs\": {out_dir_input_path}"
);
assert_eq!(
out_dir_input.get_info().get_digest(),
Expand All @@ -260,8 +258,7 @@ fn correct_arguments_succeed(
let analysis_input = required_inputs.get(2).expect("Failed to get the third required input");
let analysis_path = analysis_input.get_info().get_path();
assert!(
analysis_path.contains("save-analysis/libtest_crate-1234.json"),
"Unexpected file path for save_analysis file: {}",
analysis_path
analysis_path.contains("save-analysis/test_crate.json"),
"Unexpected file path for save_analysis file: {analysis_path}"
);
}
15 changes: 8 additions & 7 deletions kythe/rust/extractor/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@

use kythe_rust_extractor::{generate_analysis, vname_util};
use std::env;
use std::fs::File;
use std::path::{Path, PathBuf};
use std::path::PathBuf;

use tempdir::TempDir;

Expand All @@ -24,7 +23,7 @@ fn empty_args_fails() {
let args: Vec<String> = Vec::new();
let temp_dir = TempDir::new("extractor_test").expect("Could not create temporary directory");
let analysis_directory = PathBuf::from(temp_dir.path());
let result = generate_analysis(args, analysis_directory, "lib");
let result = generate_analysis(args, analysis_directory);
assert_eq!(result.unwrap_err(), "Arguments vector should not be empty".to_string());
}

Expand All @@ -33,7 +32,7 @@ fn nonempty_string_first_fails() {
let args: Vec<String> = vec!["nonempty".to_string()];
let temp_dir = TempDir::new("extractor_test").expect("Could not create temporary directory");
let analysis_directory = PathBuf::from(temp_dir.path());
let result = generate_analysis(args, analysis_directory, "lib");
let result = generate_analysis(args, analysis_directory);
assert_eq!(result.unwrap_err(), "The first argument must be an empty string".to_string());
}

Expand All @@ -55,12 +54,14 @@ fn correct_arguments_succeed() {
format!("--out-dir={}", temp_dir.path().to_str().unwrap()),
];
let analysis_directory = PathBuf::from(temp_dir.path());
let result = generate_analysis(args, analysis_directory, "lib");
let result = generate_analysis(args, analysis_directory);
assert_eq!(result.unwrap(), (), "generate_analysis result wasn't void");

// Ensure the save_analysis file exists
let _ = File::open(Path::new(temp_dir.path()).join("save-analysis/lib.json"))
.expect("save_analysis did not exist in the expected path");
assert!(
temp_dir.path().join("save-analysis/test_crate.json").exists(),
"save_analysis did not exist in the expected path"
);
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion kythe/rust/indexer/src/bin/proxy/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ fn request_compilation_unit() -> Result<CompilationUnit> {
/// value, and the second sets the error message if the first argument is false.
fn send_done(ok: bool, msg: String) -> Result<()> {
let request = proxyrequests::done(ok, msg)?;
println!("{}", request);
println!("{request}");
io::stdout().flush().context("Failed to flush stdout")?;

// Grab the response, but we don't care what it is so just throw it away
Expand Down

0 comments on commit 8d42287

Please sign in to comment.