diff --git a/Cargo.lock b/Cargo.lock index 67a936d1..dd9b0768 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -13927,6 +13927,7 @@ dependencies = [ name = "raea-app-registry" version = "0.1.0" dependencies = [ + "dunce", "oxc", "raea-core-facts", "serde_json", @@ -13983,6 +13984,7 @@ dependencies = [ name = "raea-app-workspace" version = "0.1.0" dependencies = [ + "dunce", "raea-app-check", "raea-app-registry", "raea-core-brief", @@ -14280,6 +14282,7 @@ version = "0.1.0" name = "raea-core-facts" version = "0.1.0" dependencies = [ + "dunce", "raea-core-brief", "serde", "serde_json", diff --git a/apps/workspace/Cargo.toml b/apps/workspace/Cargo.toml index 1f40125b..db0e2511 100644 --- a/apps/workspace/Cargo.toml +++ b/apps/workspace/Cargo.toml @@ -7,6 +7,7 @@ publish = false description = "`raea workspace`, `raea project` and `raea controller`: a workspace's folders, projects and controllers, each change a commit" [dependencies] +dunce = "1" # the controller schema (core/brief controller.rs): a controller file is checked and normalized against it raea-core-brief = { path = "../../core/brief" } serde_json = "1" diff --git a/apps/workspace/registry/Cargo.toml b/apps/workspace/registry/Cargo.toml index 28c87c59..45bc3b37 100644 --- a/apps/workspace/registry/Cargo.toml +++ b/apps/workspace/registry/Cargo.toml @@ -7,6 +7,7 @@ publish = false description = "A raea project's compositions, components and controllers, read from their source without running it, and lint for what isn't connected (docs/projects.feature PJ8)" [dependencies] +dunce = "1" # the version engine/bundle parses TSX with oxc = { version = "=0.152.0", features = ["ast_visit"] } serde_json = "1" diff --git a/apps/workspace/registry/src/lib.rs b/apps/workspace/registry/src/lib.rs index f4c6e7ca..18e90772 100644 --- a/apps/workspace/registry/src/lib.rs +++ b/apps/workspace/registry/src/lib.rs @@ -157,7 +157,7 @@ pub(crate) fn resolve(from: &Path, spec: &str) -> Option { } tries.extend([base.join("index.tsx"), base.join("index.ts")]); let found = tries.into_iter().find(|p| p.is_file())?; - Some(std::fs::canonicalize(&found).unwrap_or(found)) + Some(dunce::canonicalize(&found).unwrap_or(found)) } /// An import of a file (not a package): relative, absolute, or the workspace's shared/ (@shared/...). @@ -183,7 +183,7 @@ fn manifest(path: &Path) -> Option { /// The folder's registry. `overridden`: the controller ids the project's overrides name, each checked against the /// controllers there (the folder's and its workspace's controllers/). pub fn scan(dir: &Path, overridden: &[String]) -> Registry { - let dir = std::fs::canonicalize(dir).unwrap_or_else(|_| dir.to_path_buf()); + let dir = dunce::canonicalize(dir).unwrap_or_else(|_| dir.to_path_buf()); let project = up(&dir, ".raea/project").is_some(); let mut found = Vec::new(); sources(&dir, "", 0, &mut found); @@ -192,7 +192,7 @@ pub fn scan(dir: &Path, overridden: &[String]) -> Registry { .map(|(path, rel)| { let src = std::fs::read_to_string(&path).unwrap_or_default(); let facts = read::read(&src, rel.ends_with(".tsx")); - File { path: std::fs::canonicalize(&path).unwrap_or(path), rel, facts } + File { path: dunce::canonicalize(&path).unwrap_or(path), rel, facts } }) .collect(); diff --git a/apps/workspace/registry/src/lint.rs b/apps/workspace/registry/src/lint.rs index ed4b86c0..0fd080ee 100644 --- a/apps/workspace/registry/src/lint.rs +++ b/apps/workspace/registry/src/lint.rs @@ -74,7 +74,10 @@ pub(super) fn lint(reg: &Registry, files: &[File], used_by: &BTreeMap = reg.compositions.iter().map(|c| c.file.as_str()).collect(); let controller_files: BTreeSet<&str> = reg.controllers.iter().map(|c| c.file.as_str()).collect(); // the workspace's shared/, which a project reaches as @shared/ - let shared = super::up(®.dir, ".raea/workspace").map(|w| w.join("shared")).map(|s| s.canonicalize().unwrap_or(s)); + let shared = super::up(®.dir, ".raea/workspace").map(|w| { + let s = w.join("shared"); + dunce::canonicalize(&s).unwrap_or(s) + }); for f in files { if let Some(why) = &f.facts.broken { @@ -102,7 +105,7 @@ pub(super) fn lint(reg: &Registry, files: &[File], used_by: &BTreeMap format!("import it by the workspace's name for it: \"@shared/{}\"", r.with_extension("").display()), + Some(r) => format!("import it by the workspace's name for it: \"@shared/{}\"", r.with_extension("").to_string_lossy().replace(std::path::MAIN_SEPARATOR, "/")), None => "move it to the workspace's shared/ and import it as @shared//, or copy it into the project (raea component add, for raea's sets)".into(), }, )) diff --git a/apps/workspace/registry/src/tests.rs b/apps/workspace/registry/src/tests.rs index c2341719..76514efe 100644 --- a/apps/workspace/registry/src/tests.rs +++ b/apps/workspace/registry/src/tests.rs @@ -35,7 +35,7 @@ mod tempdir { static N: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); let d = std::env::temp_dir().join(format!("raea-registry-{}-{}", std::process::id(), N.fetch_add(1, std::sync::atomic::Ordering::Relaxed))); std::fs::create_dir_all(&d).unwrap(); - Dir(std::fs::canonicalize(&d).unwrap()) + Dir(dunce::canonicalize(&d).unwrap()) } } impl Drop for Dir { diff --git a/apps/workspace/src/doctor.rs b/apps/workspace/src/doctor.rs index a3310b30..922eab26 100644 --- a/apps/workspace/src/doctor.rs +++ b/apps/workspace/src/doctor.rs @@ -56,7 +56,7 @@ fn format(repo: &Path, file: &Path, keys: &[(&str, String)], out: &mut Vec Option { if let Some(p) = up(from, ".raea/project") { return Some(p); } - let from = from.canonicalize().unwrap_or_else(|_| from.to_path_buf()); + let from = dunce::canonicalize(from).unwrap_or_else(|_| from.to_path_buf()); (up(&from, ".raea/workspace").as_deref() != Some(from.as_path())).then_some(from) } diff --git a/apps/workspace/src/tests.rs b/apps/workspace/src/tests.rs index c9fe4e96..b7463d6b 100644 --- a/apps/workspace/src/tests.rs +++ b/apps/workspace/src/tests.rs @@ -22,7 +22,7 @@ fn a_spec_module_is_tidy_typescript() { #[test] fn up_finds_the_nearest_marker() { - let root = std::env::temp_dir().canonicalize().unwrap().join(format!("raea-ws-up-{}", std::process::id())); + let root = dunce::canonicalize(std::env::temp_dir()).unwrap().join(format!("raea-ws-up-{}", std::process::id())); let deep = root.join("projects/launch/media"); std::fs::create_dir_all(&deep).unwrap(); std::fs::create_dir_all(root.join(".raea")).unwrap(); @@ -139,6 +139,43 @@ fn the_root_with_none_set() { assert_eq!(default_root(&synced), synced.join("raea")); } +#[test] +fn a_workspace_reads_back_through_git() { + // Windows: canonicalize() gives \\?\C:\..., a path Git for Windows can neither read nor lock, and one that shares no + // prefix with the C:/... git prints, so the include climbed to the root (../../../../\\?\C:/...) and every git + // command in the workspace failed; ids were never written and nothing read back (0.1.5, 0.1.6) + let ws = scratch("plain"); + let made = crate::workspace::workspace(&["adopt".into(), ws.display().to_string(), "--name".into(), "Plain".into()]); + assert!(made.is_ok(), "{made:?}"); + let git = |dir: &Path, a: &[&str]| { + let o = std::process::Command::new("git").arg("-C").arg(dir).args(a).output().unwrap(); + (o.status.success(), String::from_utf8_lossy(&o.stdout).trim().to_string(), String::from_utf8_lossy(&o.stderr).trim().to_string()) + }; + let (ok, inc, err) = git(&ws, &["config", "--get-all", "include.path"]); + assert!(ok && inc == "../.raea/workspace", "include.path {inc:?} {err}"); + let (ok, id, err) = git(&ws, &["config", "workspace.id"]); + assert!(ok && id.starts_with("ws_"), "workspace.id {id:?} {err}"); + let found = up(&ws.join("projects"), ".raea/workspace").expect("up finds the workspace"); + assert!(!found.to_string_lossy().starts_with(r"\\?\"), "{}", found.display()); + crate::workspace::new_project_in(&found, "Desk", None).unwrap(); + let (ok, pid, err) = git(&ws.join("projects/desk"), &["config", "-f", ".raea/project", "project.id"]); + assert!(ok && pid.starts_with("pj_"), "project.id {pid:?} {err}"); + let _ = std::fs::remove_dir_all(&ws); +} + +#[test] +fn an_include_path_is_relative_with_slashes() { + use crate::workspace::relative; + assert_eq!(relative(Path::new("/w/.git"), Path::new("/w/.raea/workspace")), "../.raea/workspace"); + assert_eq!(relative(Path::new("/w/.git/modules/projects/p"), Path::new("/w/projects/p/.raea/project")), "../../../../projects/p/.raea/project"); + #[cfg(windows)] + { + // git's C:/... and a plain C:\... are one root; another drive is named whole, not climbed to + assert_eq!(relative(Path::new("C:/w/.git"), Path::new(r"C:\w\.raea\workspace")), "../.raea/workspace"); + assert_eq!(relative(Path::new(r"C:\w\.git"), Path::new(r"D:\o\.raea\workspace")), "D:/o/.raea/workspace"); + } +} + #[test] fn a_new_workspace_and_project_have_the_layout() { // docs/layout.feature LY1: a folder per kind in the project and in the workspace's shared/, no media/ brand/ parts/ diff --git a/apps/workspace/src/workspace.rs b/apps/workspace/src/workspace.rs index 5f67bee7..2b9b4a20 100644 --- a/apps/workspace/src/workspace.rs +++ b/apps/workspace/src/workspace.rs @@ -92,7 +92,9 @@ pub(crate) fn new_id(prefix: &str) -> String { /// Where workspaces live on this machine: `git config --global raea.root`, else default_root. pub fn root() -> PathBuf { let set = config(None, &["--global", "raea.root"]).ok().filter(|s| !s.is_empty()); - let home = std::env::var_os("HOME").map(PathBuf::from).unwrap_or_default(); + // HOME, else USERPROFILE: Windows sets no HOME for an app opened from the Start menu, which made the root + // Documents/raea under whatever folder raea ran in. git's global config is found the same way. + let home = std::env::var_os("HOME").or_else(|| std::env::var_os("USERPROFILE")).map(PathBuf::from).unwrap_or_default(); match set { Some(s) if s.starts_with("~/") => home.join(&s[2..]), Some(s) => PathBuf::from(s), @@ -116,8 +118,12 @@ pub fn default_root(home: &Path) -> PathBuf { /// The include in a repo's own config that makes `git config` read a committed file. Idempotent; the path is relative /// to the config file, which for a submodule lives under the workspace's .git/modules. pub(crate) fn include(repo: &Path, file: &Path) -> R<()> { + // both ends the same way, plain (dunce): on Windows git prints C:/... and canonicalize() alone gave \\?\C:\..., which + // share no root, so the include climbed to the drive and named the file by its \\?\ path, and every git command in + // the workspace failed ("Invalid argument") from its first commit (0.1.5, 0.1.6) let cfg = PathBuf::from(git(repo, &["rev-parse", "--path-format=absolute", "--git-path", "config"])?); - let file = file.canonicalize().map_err(|e| format!("{}: {e}", file.display()))?; + let cfg = dunce::canonicalize(&cfg).unwrap_or(cfg); + let file = dunce::canonicalize(file).map_err(|e| format!("{}: {e}", file.display()))?; let rel = relative(cfg.parent().unwrap_or(Path::new("/")), &file); let have = config(Some(&cfg), &["--get-all", "include.path"]).unwrap_or_default(); if have.lines().any(|l| l.trim() == rel) { @@ -126,9 +132,14 @@ pub(crate) fn include(repo: &Path, file: &Path) -> R<()> { config(Some(&cfg), &["--add", "include.path", &rel]).map(drop) } -fn relative(from: &Path, to: &Path) -> String { +/// `to` from the folder `from`, '/' between its parts as git reads an include.path; `to` itself (with '/') when the two +/// share no root, as two drives don't. +pub(crate) fn relative(from: &Path, to: &Path) -> String { let (a, b): (Vec<_>, Vec<_>) = (from.components().collect(), to.components().collect()); let same = a.iter().zip(&b).take_while(|(x, y)| x == y).count(); + if same == 0 { + return to.to_string_lossy().replace(std::path::MAIN_SEPARATOR, "/"); + } let mut out: Vec = std::iter::repeat_n("..".to_string(), a.len() - same).collect(); out.extend(b[same..].iter().map(|c| c.as_os_str().to_string_lossy().into_owned())); out.join("/") @@ -213,7 +224,7 @@ fn init(name: Option<&str>, layout: Option<&str>) -> R { /// An existing folder made a workspace in place: git when it isn't a repo, .raea/workspace, and each folder in it /// holding a brief.ts registered as an inline project. Nothing is moved or deleted. fn adopt(dir: &Path, name: Option<&str>, layout: Option<&str>) -> R { - let dir = dir.canonicalize().map_err(|e| format!("{}: {e}", dir.display()))?; + let dir = dunce::canonicalize(dir).map_err(|e| format!("{}: {e}", dir.display()))?; let name = name.map(String::from).unwrap_or_else(|| dir.file_name().map(|n| n.to_string_lossy().into_owned()).unwrap_or_default()); make_workspace(&dir, &name, layout_of(layout)?)?; let mut found = vec![]; @@ -244,7 +255,7 @@ pub(crate) fn make_workspace(dir: &Path, name: &str, layout: &str) -> R<()> { let parent = dir.parent().unwrap_or(Path::new("/")); if !dir.join(".git").exists() && parent.exists() { let top = git(parent, &["rev-parse", "--show-toplevel"]).ok().map(PathBuf::from); - if top.is_some_and(|t| t.canonicalize().ok() == parent.canonicalize().ok()) { + if top.is_some_and(|t| dunce::canonicalize(t).ok() == dunce::canonicalize(parent).ok()) { return Err(format!("{} would be a folder of the git repo {}: set where workspaces go, `git config --global raea.root ~/Documents/raea`", dir.display(), parent.display())); } } diff --git a/core/facts/Cargo.toml b/core/facts/Cargo.toml index f494134a..5aace12e 100644 --- a/core/facts/Cargo.toml +++ b/core/facts/Cargo.toml @@ -7,6 +7,7 @@ publish = false description = "A project as one world of facts in raea/ecs's form: its layers, text, parts, shots, clips and cues as entities with components, for any kind of piece" [dependencies] +dunce = "1" # a rule's taste form and controller field (rule.rs) raea-core-brief = { path = "../brief" } serde = { version = "1", features = ["derive"] } diff --git a/core/facts/src/record.rs b/core/facts/src/record.rs index f239d84b..1c57bcf3 100644 --- a/core/facts/src/record.rs +++ b/core/facts/src/record.rs @@ -44,15 +44,17 @@ pub struct Entry { pub despite: Vec, } -/// The nearest folder at or above `from` that holds `marker` (.raea/workspace, .raea/project). +/// The nearest folder at or above `from` that holds `marker` (.raea/workspace, .raea/project). Plain on Windows +/// (C:\..., through dunce): canonicalize() alone gives \\?\C:\..., a path Git for Windows can neither read nor lock, so +/// every id raea kept in git config read back empty and was never written (0.1.5, 0.1.6). pub fn up(from: &Path, marker: &str) -> Option { - let from = from.canonicalize().ok()?; + let from = dunce::canonicalize(from).ok()?; from.ancestors().find(|d| d.join(marker).is_file()).map(Path::to_path_buf) } /// A path made absolute through its nearest folder that exists (an output may not be written yet). fn real(p: &Path) -> PathBuf { - if let Ok(c) = p.canonicalize() { + if let Ok(c) = dunce::canonicalize(p) { return c; } match (p.parent().filter(|d| !d.as_os_str().is_empty()), p.file_name()) { @@ -61,10 +63,11 @@ fn real(p: &Path) -> PathBuf { } } -/// A path as the record names it: relative to the project when it's in it. +/// A path as the record names it: relative to the project when it's in it, with '/' between its parts on Windows too (a +/// project's record moves between machines, and `raea next` quotes it in commands). pub fn rel(project: &Path, p: &Path) -> String { let full = real(p); - full.strip_prefix(project).map(|r| r.display().to_string()).unwrap_or_else(|_| full.display().to_string()) + full.strip_prefix(project).map(|r| r.to_string_lossy().replace(std::path::MAIN_SEPARATOR, "/")).unwrap_or_else(|_| full.display().to_string()) } /// The project's commit, with "+" when `of` (a path from the project; "." for all of it) differs from it.