Skip to content

Commit

Permalink
specify erl path from cli
Browse files Browse the repository at this point in the history
Summary:
in buck env everything should be hermetic, so we can't rely on erl in the path.
this diff adds an option to specify path to `erl` binary for any command.
If the option is not specified `elp` will use `erl` from `$PATH`

this option will be used in D55965578

Reviewed By: alanz

Differential Revision: D56197691

fbshipit-source-id: 51f735d25e5006c558b7abdb80a0a53b7a0ea256
  • Loading branch information
perehonchuk authored and facebook-github-bot committed Apr 17, 2024
1 parent 80bdc5b commit 0d5865c
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 6 deletions.
2 changes: 2 additions & 0 deletions crates/elp/src/bin/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,8 @@ pub enum Command {
pub struct Args {
#[bpaf(argument("LOG_FILE"))]
pub log_file: Option<PathBuf>,
#[bpaf(argument("ERL"))]
pub erl: Option<PathBuf>,
pub no_log_buffering: bool,
#[bpaf(external(command))]
pub command: Command,
Expand Down
21 changes: 17 additions & 4 deletions crates/elp/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use elp_log::timeit;
use elp_log::FileLogger;
use elp_log::Logger;
use elp_project_model::eqwalizer_support;
use elp_project_model::otp::ERL;
use include_dir::include_dir;
use include_dir::Dir;
use lsp_server::Connection;
Expand Down Expand Up @@ -75,12 +76,24 @@ fn handle_res(result: Result<()>, stderr: &mut dyn Write) -> i32 {
}
}

fn try_main(cli: &mut dyn Cli, args: Args) -> Result<()> {
let logger = setup_logging(args.log_file, args.no_log_buffering)?;
fn setup_static(args: &Args) {
if let Err(err) = eqwalizer_support::setup_eqwalizer_support(&EQWALIZER_SUPPORT_DIR) {
log::warn!("Failed to setup eqwalizer_support: {}", err);
}
INIT.call_once(setup_thread_pool);
if let Some(erl) = &args.erl {
let path = fs::canonicalize(erl).expect("erl path should be valid");
let mut erl = ERL.write().unwrap();
*erl = path.to_string_lossy().to_string();
}
}

fn try_main(cli: &mut dyn Cli, args: Args) -> Result<()> {
let logger = setup_logging(&args.log_file, args.no_log_buffering)?;

INIT.call_once(|| {
setup_static(&args);
setup_thread_pool();
});
match args.command {
args::Command::RunServer(_) => run_server(logger)?,
args::Command::ParseAll(args) => erlang_service_cli::parse_all(&args, cli)?,
Expand Down Expand Up @@ -113,7 +126,7 @@ fn try_main(cli: &mut dyn Cli, args: Args) -> Result<()> {
Ok(())
}

fn setup_logging(log_file: Option<PathBuf>, no_buffering: bool) -> Result<Logger> {
fn setup_logging(log_file: &Option<PathBuf>, no_buffering: bool) -> Result<Logger> {
env::set_var("RUST_BACKTRACE", "short");

let log_file = match log_file {
Expand Down
3 changes: 2 additions & 1 deletion crates/elp/src/resources/test/help.stdout
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
Usage: [--log-file LOG_FILE] [--no-log-buffering] [COMMAND ...]
Usage: [--log-file LOG_FILE] [--erl ERL] [--no-log-buffering] [COMMAND ...]

Available options:
--log-file <LOG_FILE>
--erl <ERL>
--no-log-buffering
-h, --help Prints help information

Expand Down
9 changes: 8 additions & 1 deletion crates/project_model/src/otp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ use std::fs;
use std::path::Path;
use std::path::PathBuf;
use std::process::Command;
use std::sync::RwLock;

use anyhow::bail;
use anyhow::Result;
use elp_log::timeit;
use lazy_static::lazy_static;
use paths::AbsPathBuf;

use crate::ProjectAppData;
Expand All @@ -24,10 +26,15 @@ pub struct Otp {
pub lib_dir: AbsPathBuf,
}

lazy_static! {
pub static ref ERL: RwLock<String> = RwLock::new("erl".to_string());
}

impl Otp {
pub fn find_otp() -> Result<PathBuf> {
let _timer = timeit!("find otp");
let output = Command::new("erl")
let erl = ERL.read().unwrap();
let output = Command::new(&*erl)
.arg("-noshell")
.arg("-eval")
.arg("io:format('~s', [code:root_dir()])")
Expand Down

0 comments on commit 0d5865c

Please sign in to comment.