Skip to main content

lux_lib/workspace/
mod.rs

1use std::{
2    io,
3    ops::Deref,
4    path::{Path, PathBuf},
5};
6
7use glob::glob;
8use itertools::Itertools;
9use lets_find_up::{find_up_with, FindUpKind, FindUpOptions};
10use nonempty::NonEmpty;
11use path_slash::PathBufExt;
12use thiserror::Error;
13
14use crate::{
15    config::Config,
16    lockfile::{LockfileError, ReadOnly, WorkspaceLockfile},
17    lua_rockspec::LuaVersionError,
18    lua_version::LuaVersion,
19    package::PackageName,
20    project::{Project, ProjectError, PROJECT_TOML},
21    tree::{Tree, TreeError},
22    workspace::workspace_toml::{WorkspaceMemberSpec, WorkspaceToml},
23};
24
25pub mod workspace_toml;
26
27pub const WORKSPACE_TOML: &str = PROJECT_TOML;
28pub(crate) const LUX_DIR_NAME: &str = ".lux";
29const LUARC: &str = ".luarc.json";
30const EMMYRC: &str = ".emmyrc.json";
31
32/// A newtype for the workspace root directory.
33/// This is used to ensure that the workspace root is a valid project directory.
34#[derive(Clone, Debug)]
35#[cfg_attr(test, derive(Default))]
36pub struct WorkspaceRoot(PathBuf);
37
38impl AsRef<Path> for WorkspaceRoot {
39    fn as_ref(&self) -> &Path {
40        self.0.as_ref()
41    }
42}
43
44impl Deref for WorkspaceRoot {
45    type Target = PathBuf;
46
47    fn deref(&self) -> &Self::Target {
48        &self.0
49    }
50}
51
52#[derive(Debug, Error)]
53pub enum WorkspaceError {
54    #[error("cannot get current directory: {0}")]
55    GetCwd(io::Error),
56    #[error("error reading lux.toml at {0}:\n{1}")]
57    ReadLuxTOML(String, io::Error),
58    #[error("error deserializing workspace TOML:\n{0}")]
59    TOML(String),
60    #[error("no project found at '{0}'")]
61    ProjectNotFound(PathBuf),
62    #[error("glob error: '{0}'")]
63    Glob(String),
64    #[error("error deserializing project TOML:\n{0}")]
65    Project(#[from] ProjectError),
66    #[error("no project or workspace found")]
67    NoWorkspaceOrProject,
68    #[error("empty workspace at '{0}'")]
69    EmptyWorkspace(PathBuf),
70    #[error(transparent)]
71    Lockfile(#[from] LockfileError),
72    #[error("not a lux project or workspace directory:\n'{0}'")]
73    NotAWorkspaceDir(PathBuf),
74    #[error("package must be specified in a multi-project workspace")]
75    NoPackageSpecified,
76    #[error("package '{0}' not found in workspace '{1}'")]
77    PackageNotFound(PackageName, WorkspaceRoot),
78}
79
80#[derive(Error, Debug)]
81pub enum WorkspaceTreeError {
82    #[error(transparent)]
83    Tree(#[from] TreeError),
84    #[error(transparent)]
85    LuaVersionError(#[from] LuaVersionError),
86}
87
88#[derive(Clone, Debug)]
89pub struct Workspace {
90    root: WorkspaceRoot,
91    members: NonEmpty<Project>,
92}
93
94// TODO: move lockfile from project to workspace
95
96impl Workspace {
97    pub fn current() -> Result<Option<Self>, WorkspaceError> {
98        let cwd = std::env::current_dir().map_err(WorkspaceError::GetCwd)?;
99        Self::from(&cwd)
100    }
101
102    pub fn current_or_err() -> Result<Self, WorkspaceError> {
103        let cwd = std::env::current_dir().map_err(WorkspaceError::GetCwd)?;
104        Self::current()?.ok_or(WorkspaceError::NotAWorkspaceDir(cwd))
105    }
106
107    /// The path where the root `lux.toml` resides.
108    pub fn root(&self) -> &WorkspaceRoot {
109        &self.root
110    }
111
112    /// The members of this workspace.
113    pub fn members(&self) -> &NonEmpty<Project> {
114        &self.members
115    }
116
117    /// Mutable reference to the members of this workspace.
118    pub fn members_mut(&mut self) -> &mut NonEmpty<Project> {
119        &mut self.members
120    }
121
122    /// Get a workspace member, defaulting to the first one if none is specified.
123    /// Fails if a package name is specified, but not found.
124    pub fn single_member_or_select(
125        &self,
126        name: &Option<PackageName>,
127    ) -> Result<&Project, WorkspaceError> {
128        match name {
129            Some(name) => self
130                .members()
131                .iter()
132                .find(|project| &project.toml().package == name)
133                .ok_or_else(|| WorkspaceError::PackageNotFound(name.clone(), self.root.clone())),
134            None => Ok(self.members().first()),
135        }
136    }
137
138    /// Get a mutable workspace member, defaulting to the first one if none is specified.
139    /// Fails if a package name is specified, but not found.
140    pub fn single_member_or_select_mut(
141        &mut self,
142        package: &Option<PackageName>,
143    ) -> Result<&mut Project, WorkspaceError> {
144        match package.as_ref() {
145            Some(package) => self.select_member_mut(package),
146            None => self.single_member_mut(),
147        }
148    }
149
150    /// Get the single member of this workspace, failing if it has multiple members.
151    pub fn single_member(&self) -> Result<&Project, WorkspaceError> {
152        if self.members().len() == 1 {
153            Ok(self.members().first())
154        } else {
155            Err(WorkspaceError::NoPackageSpecified)
156        }
157    }
158
159    /// Get the single mutable member of this workspace, failing if it has multiple members.
160    pub fn single_member_mut(&mut self) -> Result<&mut Project, WorkspaceError> {
161        if self.members().len() == 1 {
162            Ok(self.members_mut().first_mut())
163        } else {
164            Err(WorkspaceError::NoPackageSpecified)
165        }
166    }
167
168    /// Select a member of this workspace, failing if it is not found.
169    pub fn select_member(&self, package: &PackageName) -> Result<&Project, WorkspaceError> {
170        let workspace_root = self.root.clone();
171        self.members()
172            .iter()
173            .find(|project| &project.toml().package == package)
174            .ok_or_else(|| WorkspaceError::PackageNotFound(package.clone(), workspace_root))
175    }
176
177    /// Select a mutable member of this workspace, failing if it is not found.
178    pub fn select_member_mut(
179        &mut self,
180        package: &PackageName,
181    ) -> Result<&mut Project, WorkspaceError> {
182        let workspace_root = self.root.clone();
183        self.members_mut()
184            .iter_mut()
185            .find(|project| &project.toml().package == package)
186            .ok_or_else(|| WorkspaceError::PackageNotFound(package.clone(), workspace_root))
187    }
188
189    /// Get the `lux.lock` lockfile path.
190    pub fn lockfile_path(&self) -> PathBuf {
191        self.root.join("lux.lock")
192    }
193
194    /// Get the `lux.lock` lockfile in the project root.
195    pub fn lockfile(&self) -> Result<WorkspaceLockfile<ReadOnly>, WorkspaceError> {
196        Ok(WorkspaceLockfile::new(self.lockfile_path())?)
197    }
198
199    /// Get the `lux.lock` lockfile in the project root, if present.
200    pub fn try_lockfile(&self) -> Result<Option<WorkspaceLockfile<ReadOnly>>, WorkspaceError> {
201        let path = self.lockfile_path();
202        if path.is_file() {
203            Ok(Some(WorkspaceLockfile::load(path)?))
204        } else {
205            Ok(None)
206        }
207    }
208
209    pub fn tree(&self, config: &Config) -> Result<Tree, WorkspaceTreeError> {
210        self.lua_version_tree(self.lua_version(config)?, config)
211    }
212
213    pub fn lua_version(&self, config: &Config) -> Result<LuaVersion, LuaVersionError> {
214        let mut lua_version = self.members().first().lua_version(config)?;
215        // Ensure the lua version specified by the config matches all projects
216        for project in self.members() {
217            lua_version = project.lua_version(config)?;
218        }
219        Ok(lua_version)
220    }
221
222    pub(crate) fn lua_version_tree(
223        &self,
224        lua_version: LuaVersion,
225        config: &Config,
226    ) -> Result<Tree, WorkspaceTreeError> {
227        Ok(Tree::new(
228            self.default_tree_root_dir(),
229            lua_version,
230            config,
231        )?)
232    }
233
234    pub(crate) fn default_tree_root_dir(&self) -> PathBuf {
235        self.root.join(LUX_DIR_NAME)
236    }
237
238    pub fn test_tree(&self, config: &Config) -> Result<Tree, WorkspaceTreeError> {
239        Ok(self.tree(config)?.test_tree(config)?)
240    }
241
242    pub fn build_tree(&self, config: &Config) -> Result<Tree, WorkspaceTreeError> {
243        Ok(self.tree(config)?.build_tree(config)?)
244    }
245
246    /// Get the `.luarc.json` or `.emmyrc.json` path.
247    pub fn luarc_path(&self) -> PathBuf {
248        let luarc_path = self.root.join(LUARC);
249        if luarc_path.is_file() {
250            luarc_path
251        } else {
252            let emmy_path = self.root.join(EMMYRC);
253            if emmy_path.is_file() {
254                emmy_path
255            } else {
256                luarc_path
257            }
258        }
259    }
260
261    pub fn from_exact(start: impl AsRef<Path>) -> Result<Option<Self>, WorkspaceError> {
262        if !start.as_ref().exists() {
263            return Ok(None);
264        }
265        if start.as_ref().join(WORKSPACE_TOML).exists() {
266            let toml_path = start.as_ref().join(WORKSPACE_TOML);
267            let toml_content = std::fs::read_to_string(&toml_path).map_err(|err| {
268                WorkspaceError::ReadLuxTOML(toml_path.to_string_lossy().to_string(), err)
269            })?;
270            let root = start.as_ref();
271            let toml_obj: Option<toml::Table> = toml::from_str(&toml_content).ok();
272            if toml_obj.is_some_and(|toml| toml.contains_key("workspace")) {
273                Ok(Some(Self::from_toml(&toml_content, root)?))
274            } else {
275                let project =
276                    Project::from_exact(root)?.ok_or(WorkspaceError::NoWorkspaceOrProject)?;
277                Ok(Some(Workspace {
278                    root: WorkspaceRoot(root.to_path_buf()),
279                    members: NonEmpty::new(project),
280                }))
281            }
282        } else {
283            Ok(None)
284        }
285    }
286
287    pub fn from(start: impl AsRef<Path>) -> Result<Option<Self>, WorkspaceError> {
288        if !start.as_ref().exists() {
289            return Ok(None);
290        }
291        match find_up_with(
292            WORKSPACE_TOML,
293            FindUpOptions {
294                cwd: start.as_ref(),
295                kind: FindUpKind::File,
296            },
297        ) {
298            Ok(Some(path)) => {
299                if let Some(root) = path.parent() {
300                    let toml_content = std::fs::read_to_string(&path).map_err(|err| {
301                        WorkspaceError::ReadLuxTOML(path.to_string_lossy().to_string(), err)
302                    })?;
303                    let toml_obj: Option<toml::Table> = toml::from_str(&toml_content).ok();
304                    if toml_obj.is_some_and(|toml| toml.contains_key("workspace")) {
305                        Ok(Some(Self::from_toml(&toml_content, root)?))
306                    } else {
307                        if let Some(parent) = root.parent() {
308                            match Self::from(parent)? {
309                                Some(workspace) => Ok(Some(workspace)),
310                                None => {
311                                    let project = Project::from_exact(root)?
312                                        .ok_or(WorkspaceError::NoWorkspaceOrProject)?;
313                                    Ok(Some(Workspace {
314                                        root: WorkspaceRoot(root.to_path_buf()),
315                                        members: NonEmpty::new(project),
316                                    }))
317                                }
318                            }
319                        } else {
320                            Ok(None)
321                        }
322                    }
323                } else {
324                    Ok(None)
325                }
326            }
327            // NOTE: If we hit a read error, it could be because we haven't found a PROJECT_TOML
328            // or WORKSPACE_TOML and have started searching too far upwards.
329            // See for example https://github.com/lumen-oss/lux/issues/532
330            _ => Ok(None),
331        }
332    }
333
334    fn from_toml(toml_content: &str, root: &Path) -> Result<Self, WorkspaceError> {
335        let toml = WorkspaceToml::new(toml_content)
336            .map_err(|err| WorkspaceError::TOML(err.to_string()))?;
337        let mut members = Vec::new();
338        for member in toml.workspace.members {
339            match member {
340                WorkspaceMemberSpec::RelativeProjectGlob(pattern) => {
341                    let potential_paths = glob(root.join(pattern).to_slash_lossy().deref())
342                        .ok() // This is fine because we fail to deserialize invalid globs
343                        .into_iter()
344                        .flat_map(|paths| {
345                            paths.map(|path| {
346                                path.map_err(|err| WorkspaceError::Glob(err.to_string()))
347                            })
348                        })
349                        .try_collect::<_, Vec<_>, _>()?;
350                    for project_path in potential_paths {
351                        if let Some(project) = Project::from_exact(&project_path)? {
352                            members.push(project)
353                        }
354                    }
355                }
356                WorkspaceMemberSpec::RelativeProjectPath(relative_project_path) => {
357                    let project_path = root.join(relative_project_path);
358                    match Project::from_exact(&project_path)? {
359                        Some(project) => members.push(project),
360                        None => return Err(WorkspaceError::ProjectNotFound(project_path)),
361                    }
362                }
363            }
364        }
365        match NonEmpty::from_vec(members) {
366            Some(members) => Ok(Workspace {
367                root: WorkspaceRoot(root.to_path_buf()),
368                members,
369            }),
370            None => Err(WorkspaceError::EmptyWorkspace(root.to_path_buf())),
371        }
372    }
373}
374
375#[cfg(test)]
376mod tests {
377    use super::*;
378    use std::path::PathBuf;
379
380    use assert_fs::prelude::PathCopy;
381
382    #[tokio::test]
383    async fn find_single_project_workspace() {
384        let sample_project: PathBuf = "resources/test/sample-projects/init/".into();
385        let project_root = assert_fs::TempDir::new().unwrap();
386        project_root.copy_from(&sample_project, &["**"]).unwrap();
387        let work_dir: PathBuf = project_root.join("src");
388        let workspace = Workspace::from(&work_dir).unwrap().unwrap();
389        assert_eq!(workspace.members.len(), 1);
390        let project = workspace.members.first();
391        assert_eq!(project.root().to_path_buf(), project_root.to_path_buf());
392    }
393
394    #[tokio::test]
395    async fn find_multi_project_workspace() {
396        let sample_workspace: PathBuf = "resources/test/sample-projects/multi-project/".into();
397        let workspace_root = assert_fs::TempDir::new().unwrap();
398        workspace_root
399            .copy_from(&sample_workspace, &["**"])
400            .unwrap();
401        let work_dir: PathBuf = workspace_root.join("projects");
402        let workspace = Workspace::from(&work_dir).unwrap().unwrap();
403        assert_eq!(workspace.members.len(), 2);
404        let foo = workspace.select_member(&"foo".into()).unwrap();
405        assert_eq!(
406            foo.root().to_path_buf(),
407            workspace_root.join("projects/foo").to_path_buf()
408        );
409        let bar = workspace.select_member(&"bar".into()).unwrap();
410        assert_eq!(
411            bar.root().to_path_buf(),
412            workspace_root.join("projects/bar").to_path_buf()
413        );
414    }
415
416    #[tokio::test]
417    async fn find_multi_project_workspace_members_glob() {
418        let sample_workspace: PathBuf = "resources/test/sample-projects/multi-project/".into();
419        let workspace_root = assert_fs::TempDir::new().unwrap();
420        workspace_root
421            .copy_from(&sample_workspace, &["**"])
422            .unwrap();
423        let work_dir: PathBuf = workspace_root.join("projects");
424        let workspace_toml_file = workspace_root.join(WORKSPACE_TOML);
425        let workspace_toml_content = r#"
426[workspace]
427members = [ "glob:projects/*" ]
428"#;
429        tokio::fs::write(&workspace_toml_file, workspace_toml_content)
430            .await
431            .unwrap();
432
433        let workspace = Workspace::from(&work_dir).unwrap().unwrap();
434        assert_eq!(workspace.members.len(), 2);
435        let foo = workspace.select_member(&"foo".into()).unwrap();
436        assert_eq!(
437            foo.root().to_path_buf(),
438            workspace_root.join("projects/foo").to_path_buf()
439        );
440        let bar = workspace.select_member(&"bar".into()).unwrap();
441        assert_eq!(
442            bar.root().to_path_buf(),
443            workspace_root.join("projects/bar").to_path_buf()
444        );
445    }
446
447    #[tokio::test]
448    async fn test_no_find_workspace_upwards() {
449        let work_dir = assert_fs::TempDir::new().unwrap();
450        assert!(Workspace::from(&work_dir).unwrap().is_none())
451    }
452}