Skip to main content

lux_cli/
shell.rs

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