Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(scala): use scala-cli first to avoid timeout #5952

Merged
merged 1 commit into from
May 12, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
90 changes: 82 additions & 8 deletions src/modules/scala.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,17 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
}

fn get_scala_version(context: &Context) -> Option<String> {
let command = context.exec_cmd("scalac", &["-version"])?;
let scala_version_string = get_command_string_output(command);

parse_scala_version(&scala_version_string)
// try to get the version from scala-cli first as it is faster
// and return ONLY the version which save us from parsing the version string
context
.exec_cmd("scala-cli", &["version", "--scala"])
.filter(|out| !out.stdout.is_empty())
.map(|std_out_only| std_out_only.stdout.trim().to_string())
.or_else(|| {
let command = context.exec_cmd("scalac", &["-version"])?;
let scala_version_string = get_command_string_output(command);
parse_scala_version(&scala_version_string)
})
}

fn parse_scala_version(scala_version_string: &str) -> Option<String> {
Expand Down Expand Up @@ -111,7 +118,21 @@ mod tests {
let dir = tempfile::tempdir()?;
File::create(dir.path().join("Test.scala"))?.sync_all()?;
let actual = ModuleRenderer::new("scala").path(dir.path()).collect();
let expected = Some(format!("via {}", Color::Red.bold().paint("🆂 v2.13.5 ")));
let expected = Some(format!("via {}", Color::Red.bold().paint("🆂 v3.4.1 ")));
assert_eq!(expected, actual);
dir.close()
}

#[test]
fn folder_with_scala_file_using_scala_cli_only() -> io::Result<()> {
let dir = tempfile::tempdir()?;
File::create(dir.path().join("Test.scala"))?.sync_all()?;
let actual = ModuleRenderer::new("scala")
// for test purpose only real use case will have both in path
.cmd("scalac -version", None)
.path(dir.path())
.collect();
let expected = Some(format!("via {}", Color::Red.bold().paint("🆂 v3.4.1 ")));
assert_eq!(expected, actual);
dir.close()
}
Expand All @@ -121,6 +142,7 @@ mod tests {
let dir = tempfile::tempdir()?;
File::create(dir.path().join("Test.scala"))?.sync_all()?;
let actual = ModuleRenderer::new("scala")
.cmd("scala-cli version --scala", None)
.cmd("scalac -version", None)
.path(dir.path())
.collect();
Expand All @@ -134,7 +156,7 @@ mod tests {
let dir = tempfile::tempdir()?;
File::create(dir.path().join("build.sbt"))?.sync_all()?;
let actual = ModuleRenderer::new("scala").path(dir.path()).collect();
let expected = Some(format!("via {}", Color::Red.bold().paint("🆂 v2.13.5 ")));
let expected = Some(format!("via {}", Color::Red.bold().paint("🆂 v3.4.1 ")));
assert_eq!(expected, actual);
dir.close()
}
Expand All @@ -144,7 +166,7 @@ mod tests {
let dir = tempfile::tempdir()?;
File::create(dir.path().join(".scalaenv"))?.sync_all()?;
let actual = ModuleRenderer::new("scala").path(dir.path()).collect();
let expected = Some(format!("via {}", Color::Red.bold().paint("🆂 v2.13.5 ")));
let expected = Some(format!("via {}", Color::Red.bold().paint("🆂 v3.4.1 ")));
assert_eq!(expected, actual);
dir.close()
}
Expand All @@ -154,7 +176,7 @@ mod tests {
let dir = tempfile::tempdir()?;
File::create(dir.path().join(".sbtenv"))?.sync_all()?;
let actual = ModuleRenderer::new("scala").path(dir.path()).collect();
let expected = Some(format!("via {}", Color::Red.bold().paint("🆂 v2.13.5 ")));
let expected = Some(format!("via {}", Color::Red.bold().paint("🆂 v3.4.1 ")));
assert_eq!(expected, actual);
dir.close()
}
Expand All @@ -164,6 +186,58 @@ mod tests {
let dir = tempfile::tempdir()?;
fs::create_dir_all(dir.path().join(".metals"))?;
let actual = ModuleRenderer::new("scala").path(dir.path()).collect();
let expected = Some(format!("via {}", Color::Red.bold().paint("🆂 v3.4.1 ")));
assert_eq!(expected, actual);
dir.close()
}

#[test]
fn folder_with_sbt_file_without_scala_cli() -> io::Result<()> {
let dir = tempfile::tempdir()?;
File::create(dir.path().join("build.sbt"))?.sync_all()?;
let actual = ModuleRenderer::new("scala")
.cmd("scala-cli version --scala", None)
.path(dir.path())
.collect();
let expected = Some(format!("via {}", Color::Red.bold().paint("🆂 v2.13.5 ")));
assert_eq!(expected, actual);
dir.close()
}

#[test]
fn folder_with_scala_env_file_without_scala_cli() -> io::Result<()> {
let dir = tempfile::tempdir()?;
File::create(dir.path().join(".scalaenv"))?.sync_all()?;
let actual = ModuleRenderer::new("scala")
.cmd("scala-cli version --scala", None)
.path(dir.path())
.collect();
let expected = Some(format!("via {}", Color::Red.bold().paint("🆂 v2.13.5 ")));
assert_eq!(expected, actual);
dir.close()
}

#[test]
fn folder_with_sbt_env_file_without_scala_cli() -> io::Result<()> {
let dir = tempfile::tempdir()?;
File::create(dir.path().join(".sbtenv"))?.sync_all()?;
let actual = ModuleRenderer::new("scala")
.cmd("scala-cli version --scala", None)
.path(dir.path())
.collect();
let expected = Some(format!("via {}", Color::Red.bold().paint("🆂 v2.13.5 ")));
assert_eq!(expected, actual);
dir.close()
}

#[test]
fn folder_with_metals_dir_without_scala_cli() -> io::Result<()> {
let dir = tempfile::tempdir()?;
fs::create_dir_all(dir.path().join(".metals"))?;
let actual = ModuleRenderer::new("scala")
.cmd("scala-cli version --scala", None)
.path(dir.path())
.collect();
let expected = Some(format!("via {}", Color::Red.bold().paint("🆂 v2.13.5 ")));
assert_eq!(expected, actual);
dir.close()
Expand Down
4 changes: 4 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,10 @@ Elixir 1.10 (compiled with Erlang/OTP 22)\n",
stdout: String::from("OpenJDK 64-Bit Server VM (13.0.2+8) for bsd-amd64 JRE (13.0.2+8), built on Feb 6 2020 02:07:52 by \"brew\" with clang 4.2.1 Compatible Apple LLVM 11.0.0 (clang-1100.0.33.17)"),
stderr: String::default(),
}),
"scala-cli version --scala" => Some(CommandOutput {
stdout: String::from("3.4.1"),
stderr: String::default(),
}),
"scalac -version" => Some(CommandOutput {
stdout: String::from("Scala compiler version 2.13.5 -- Copyright 2002-2020, LAMP/EPFL and Lightbend, Inc."),
stderr: String::default(),
Expand Down