Skip to main content

lux_cli/
shell.rs

1use clap::Args;
2use lux_lib::{
3    config::Config, lua_installation::LuaInstallation, lua_version::LuaVersion, path::Paths,
4    tree::InstallTree,
5};
6
7use miette::{miette, IntoDiagnostic, Result};
8use which::which;
9
10use std::{env, path::PathBuf};
11use tokio::process::Command;
12
13use super::workspace::current_workspace_or_user_tree;
14
15#[derive(Args)]
16pub struct Shell {
17    /// Add test dependencies to the shell's paths,{n}
18    /// in addition to the regular dependencies.
19    #[arg(long)]
20    test: bool,
21
22    /// Add *only* build dependencies to the shell's paths.
23    #[arg(long, conflicts_with = "test")]
24    build: bool,
25
26    /// Disable the Lux loader.{n}
27    /// If a rock has conflicting transitive dependencies,{n}
28    /// disabling the Lux loader may result in the wrong modules being loaded.{n}
29    #[arg(long)]
30    no_loader: bool,
31}
32
33pub async fn shell(data: Shell, config: Config) -> Result<()> {
34    if env::var("LUX_SHELL").is_ok_and(|lx_shell_var| lx_shell_var == "1") {
35        return Err(miette!(
36            help = r#"Lux does not support nested shells.
37exit the current shell if you would like to enter a new one."#,
38            "already in a Lux shell"
39        ));
40    }
41
42    let tree = current_workspace_or_user_tree(&config)?;
43
44    let path = if data.build {
45        let build_tree_path = tree.build_tree(&config)?;
46        Paths::new(&build_tree_path)?
47    } else {
48        let mut path = Paths::new(&tree)?;
49        if data.test {
50            let test_tree_path = tree.test_tree(&config)?;
51            let test_path = Paths::new(&test_tree_path)?;
52            path.prepend(&test_path);
53        }
54        path
55    };
56
57    let shell: PathBuf = match env::var("SHELL") {
58        Ok(val) => PathBuf::from(val),
59        Err(_) => {
60            #[cfg(any(target_os = "linux", target_os = "android"))]
61            let fallback = which("bash")
62                .into_diagnostic()
63                .map_err(|_| miette!("cannot find `bash` on your system!"))?;
64
65            #[cfg(target_os = "windows")]
66            let fallback = which("cmd.exe")
67                .into_diagnostic()
68                .map_err(|_| miette!("cannot find `cmd.exe` on your system!"))?;
69
70            #[cfg(target_os = "macos")]
71            let fallback = which("zsh")
72                .into_diagnostic()
73                .map_err(|_| miette!("cannot find `zsh` on your system!"))?;
74
75            fallback
76        }
77    };
78
79    let lua_path = path.package_path_prepended();
80    let lua_cpath = path.package_cpath_prepended();
81
82    let lua_init = if data.no_loader {
83        None
84    } else if tree.version().lux_lib_dir().is_none() {
85        tracing::warn!(
86            r#"lux-lua library not found.
87Cannot use the `lux.loader`.
88To suppress this warning, set the `--no-loader` option."#
89        );
90        None
91    } else {
92        Some(path.init())
93    };
94
95    let lua_version = tree.version();
96
97    let mut bin_path = path.path_prepended();
98
99    let lua = LuaInstallation::new(lua_version, &config).await?;
100    if let Some(lua_bin_path) = lua.bin().as_ref().and_then(|lua_bin| lua_bin.parent()) {
101        bin_path.add_path(lua_bin_path.to_path_buf());
102    }
103
104    let mut shell_cmd = Command::new(&shell);
105    shell_cmd.env("PATH", bin_path.joined());
106    if tree.version() != &LuaVersion::Luau {
107        shell_cmd
108            .env("LUA_PATH", lua_path.joined())
109            .env("LUA_CPATH", lua_cpath.joined())
110            .env("LUA_INIT", lua_init.unwrap_or_default());
111    }
112    let _ = shell_cmd
113        .env("LUX_SHELL", "1")
114        .spawn()
115        .into_diagnostic()?
116        .wait()
117        .await
118        .into_diagnostic()?;
119
120    Ok(())
121}