Skip to content
Merged
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
69 changes: 65 additions & 4 deletions crates/once-core/src/execution_path.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::borrow::Cow;
use std::collections::BTreeMap;
use std::path::Path;

Expand All @@ -8,10 +9,44 @@ pub fn resolve_execution_value(value: &str, execution_root: &Path) -> String {
if !value.contains(EXECUTION_ROOT_MARKER) {
return value.to_string();
}
value.replace(
EXECUTION_ROOT_MARKER,
execution_root.to_string_lossy().as_ref(),
)
value.replace(EXECUTION_ROOT_MARKER, &command_path(execution_root))
}

/// Spell a path for a command line or environment value. On Windows,
/// `std::fs::canonicalize` returns verbatim paths (`\\?\D:\...`), which
/// shells such as Git Bash cannot open and which Windows rejects once a
/// command appends `/`-separated components. Those values get their plain
/// spelling; Once's own filesystem calls keep the canonical path.
#[must_use]
pub fn command_path(path: &Path) -> String {
let path = path.to_string_lossy();
if cfg!(windows) {
strip_verbatim_prefix(&path).into_owned()
} else {
path.into_owned()
}
}

/// Windows `MAX_PATH`: longer paths only resolve with the verbatim prefix.
const MAX_PATH: usize = 260;

fn strip_verbatim_prefix(path: &str) -> Cow<'_, str> {
if let Some(rest) = path.strip_prefix(r"\\?\UNC\") {
let plain = format!(r"\\{rest}");
if plain.len() < MAX_PATH {
return Cow::Owned(plain);
}
} else if let Some(rest) = path.strip_prefix(r"\\?\") {
let bytes = rest.as_bytes();
let drive = bytes.len() >= 3
&& bytes[0].is_ascii_alphabetic()
&& bytes[1] == b':'
&& bytes[2] == b'\\';
if drive && rest.len() < MAX_PATH {
return Cow::Borrowed(rest);
}
}
Cow::Borrowed(path)
}

#[must_use]
Expand Down Expand Up @@ -57,4 +92,30 @@ mod tests {
"/workspace/bin:/usr/bin"
);
}

#[test]
fn strips_verbatim_prefixes_that_have_a_plain_spelling() {
assert_eq!(
strip_verbatim_prefix(r"\\?\D:\a\_temp\source"),
r"D:\a\_temp\source"
);
assert_eq!(
strip_verbatim_prefix(r"\\?\UNC\server\share\repo"),
r"\\server\share\repo"
);
assert_eq!(strip_verbatim_prefix(r"D:\a\source"), r"D:\a\source");
assert_eq!(strip_verbatim_prefix("/sandbox"), "/sandbox");
}

#[test]
fn keeps_verbatim_prefixes_without_a_plain_spelling() {
assert_eq!(
strip_verbatim_prefix(r"\\?\Volume{0b1c}\repo"),
r"\\?\Volume{0b1c}\repo"
);
let long = format!(r"\\?\D:\{}", "a".repeat(MAX_PATH));
assert_eq!(strip_verbatim_prefix(&long), long);
let long_unc = format!(r"\\?\UNC\server\{}", "a".repeat(MAX_PATH));
assert_eq!(strip_verbatim_prefix(&long_unc), long_unc);
}
}
3 changes: 2 additions & 1 deletion crates/once-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ pub use evidence::{
EvidenceCacheState, EvidenceRecord, EvidenceStatus, EvidenceStore, EvidenceSubject,
};
pub use execution_path::{
resolve_execution_argv, resolve_execution_env, resolve_execution_value, EXECUTION_ROOT_MARKER,
command_path, resolve_execution_argv, resolve_execution_env, resolve_execution_value,
EXECUTION_ROOT_MARKER,
};
pub use executor::{ExecutionPermit, Executor, ExecutorResources, RemoteIoLimits, ResourceDomain};
pub use input_digest::{
Expand Down
5 changes: 3 additions & 2 deletions crates/once-frontend/src/analysis/globals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,16 @@ fn prelude_globals(builder: &mut GlobalsBuilder) {
Ok(sha256)
}

/// Active workspace root as an absolute path. Schema parsing
/// Active workspace root as an absolute path, spelled for command
/// lines (without a Windows verbatim prefix). Schema parsing
/// returns `""`.
fn workspace_root() -> anyhow::Result<String> {
if !analysis_active() {
return Ok(String::new());
}
with_store(|store| -> Result<String> {
let store = store.ok_or_else(|| anyhow!("workspace_root called outside analysis"))?;
Ok(store.workspace_root.to_string_lossy().into_owned())
Ok(once_core::command_path(&store.workspace_root))
})
}

Expand Down
Loading