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

node-build compilation fallback #678

Closed
Closed
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
4 changes: 4 additions & 0 deletions src/commands/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ impl Command for Env {
"{}",
shell.set_env_var("FNM_MULTISHELL_PATH", multishell_path.to_str().unwrap())
);
println!(
"{}",
shell.set_env_var("FNM_FORCE_ARCH", &config.force_arch.to_string())
);
println!(
"{}",
shell.set_env_var(
Expand Down
27 changes: 25 additions & 2 deletions src/commands/install.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::alias::create_alias;
use crate::arch::get_safe_arch;
use crate::compile_node::compile_node_with_node_build;
use crate::config::FnmConfig;
use crate::downloader::{install_node_dist, Error as DownloaderError};
use crate::lts::LtsType;
Expand Down Expand Up @@ -28,14 +29,17 @@ impl Install {
Self {
version: Some(_),
lts: true,
..
} => Err(Error::TooManyVersionsProvided),
Self {
version: v,
lts: false,
..
} => Ok(v),
Self {
version: None,
lts: true,
..
} => Ok(Some(UserVersion::Full(Version::Lts(LtsType::Latest)))),
}
}
Expand Down Expand Up @@ -90,8 +94,12 @@ impl super::command::Command for Install {
}
};

// Automatically swap Apple Silicon to x64 arch for appropriate versions.
let safe_arch = get_safe_arch(&config.arch, &version);
let safe_arch = if config.force_arch {
&config.arch
} else {
// Automatically swap Apple Silicon to x64 arch for appropriate versions.
get_safe_arch(&config.arch, &version)
};

let version_str = format!("Node {}", &version);
outln!(
Expand All @@ -111,6 +119,16 @@ impl super::command::Command for Install {
Err(err @ DownloaderError::VersionAlreadyInstalled { .. }) => {
outln!(config, Error, "{} {}", "warning:".bold().yellow(), err);
}
Err(DownloaderError::VersionNotFound { version, arch }) if config.force_arch => {
outln!(
config,
Info,
"Version {} not found upstream for {}. Compiling locally.",
version.v_str().cyan(),
arch.to_string().cyan()
);
compile_node_with_node_build(config, &version)?;
}
other_err => other_err.map_err(|source| Error::DownloadError { source })?,
};

Expand Down Expand Up @@ -157,6 +175,11 @@ pub enum Error {
UninstallableVersion { version: Version },
#[error("Too many versions provided. Please don't use --lts with a version string.")]
TooManyVersionsProvided,
#[error(transparent)]
CompilationError {
#[from]
source: crate::compile_node::Error,
},
}

#[cfg(test)]
Expand Down
114 changes: 114 additions & 0 deletions src/compile_node.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
use crate::config::FnmConfig;
use crate::directory_portal::DirectoryPortal;
use crate::version::Version;
use std::io::{BufRead, BufReader};
use std::process::{Command, Stdio};
use thiserror::Error;

#[derive(Debug, Error)]
pub enum Error {
#[error("Can't spawn `node-build`. Do you have it installed? {source}")]
SpawningNodeBuild {
#[source]
source: std::io::Error,
},
#[error("Can't access `node-build` stdout. Please retry.")]
NodeBuildStdoutIsInaccessible,
#[error(
"`node-build` has failed.\n\n stdout:\n{}\n\n stderr: \n{}",
stdout,
stderr
)]
ExecutingNodeBuild { stdout: String, stderr: String },
#[error("Can't move built version to {target}. {source}")]
MovingBuiltVersion {
target: std::path::PathBuf,
#[source]
source: std::io::Error,
},
}

pub fn compile_node_with_node_build(config: &FnmConfig, version: &Version) -> Result<(), Error> {
let path = {
let mut path = version.installation_path(config);
path.pop();
path
};
let temp_installations_dir = tempfile::tempdir().unwrap();
Copy link
Owner Author

Choose a reason for hiding this comment

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

node-build does not support spaces in directories too. We need to find a solution for that 🥲
related:

Copy link
Owner Author

Choose a reason for hiding this comment

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

I mean, the solution is to have a dirs module (which we already have) and probably store stuff in .local/fnm or something unless XDG_ env vars are provided, but I want to make the migration fast 🥲

let portal = DirectoryPortal::new_in(&temp_installations_dir, &path);
let installation_path = portal.join("installation");

let portal_path_fixed = installation_path.to_str().unwrap().replace(' ', "\\ ");
let version_str = version.v_str();
let version_str = version_str.strip_prefix('v').unwrap();

log::debug!(
"Going to run `node-build` to compile node {} in {}",
version_str,
portal_path_fixed
);

let mut build_process = Command::new("node-build")
.arg(version_str)
.arg(portal_path_fixed)
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.stdin(Stdio::inherit())
.spawn()
.map_err(|source| Error::SpawningNodeBuild { source })?;

let stdout = build_process
.stdout
.take()
.ok_or(Error::NodeBuildStdoutIsInaccessible)?;

let stderr = build_process
.stderr
.take()
.ok_or(Error::NodeBuildStdoutIsInaccessible)?;

let stdout_reader = read_output("node-build stdout", stdout);
let stderr_reader = read_output("node-build stderr", stderr);

let [stdout_reader, stderr_reader] = [stdout_reader, stderr_reader].map(|thread| {
thread.join().map_err(|_source| Error::SpawningNodeBuild {
source: std::io::Error::from(std::io::ErrorKind::UnexpectedEof),
})
});

let exit_status = build_process
.wait()
.map_err(|source| Error::SpawningNodeBuild { source })?;

if !exit_status.success() {
return Err(Error::ExecutingNodeBuild {
stdout: stdout_reader.unwrap_or_else(|_| "[error reading]".to_string()),
stderr: stderr_reader.unwrap_or_else(|_| "[error reading]".to_string()),
});
}

portal
.teleport()
.map_err(|source| Error::MovingBuiltVersion {
target: path.clone(),
source,
})?;

Ok(())
}

fn read_output<Reader: 'static + std::io::Read + Sync + Send>(
tag: &'static str,
stream: Reader,
) -> std::thread::JoinHandle<String> {
std::thread::spawn(move || {
let mut buffer = String::new();
let bufread = BufReader::new(stream);
for line in bufread.lines().flatten() {
log::info!("[{}] {}", tag, line);
buffer.push_str(&line);
buffer.push('\n');
}
buffer
})
}
12 changes: 12 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,17 @@ pub struct FnmConfig {
)]
pub arch: Arch,

/// Build the Node.js using `node-build` if missing arch is detected.
/// This is relevant for M1 MacOS builds for Node.js versions < 16.
#[clap(
long,
env = "FNM_FORCE_ARCH",
global = true,
hide_env_values = true,
hide = true
)]
pub force_arch: bool,

/// A strategy for how to resolve the Node version. Used whenever `fnm use` or `fnm install` is
/// called without a version, or when `--use-on-cd` is configured on evaluation.
///
Expand All @@ -81,6 +92,7 @@ impl Default for FnmConfig {
log_level: LogLevel::Info,
arch: Arch::default(),
version_file_strategy: VersionFileStrategy::default(),
force_arch: false,
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ mod version_files;

#[macro_use]
mod log_level;
mod compile_node;
mod default_version;
mod directories;

Expand Down