Skip to content

Commit 3478c0b

Browse files
agorischekCopilot
andauthored
Materialize file names valid on the host; skip and name ones it can't write (#293)
A tracked file with `:` or `\` in its name made every projection of the revision fail with an unnamed "Unsafe repository path". On macOS and Linux those are ordinary file names, so they are now listed, written and read like any other, by the runner and the extractor SDK. On Windows, names it can't create are skipped and named in provenance limitations, as are paths that collide on a case-insensitive file system. A declared source or extractor program that was skipped fails, naming the file. Unsafe paths (traversal, absolute, .git) still fail the revision, and every error from these checks names the path. `projections doctor` warns about tracked paths this platform can't materialize. Refs #276 Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent a0cd42e commit 3478c0b

24 files changed

Lines changed: 927 additions & 108 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
section: behaviour-changes
3+
---
4+
5+
- **On macOS and Linux, `\` in a path is a file-name character, not a separator** (#293). The extractor SDK used to read `\` as `/` in every path it resolves, including paths in a definition's `inputs`. Now only Windows does. A definition written with `\` separators, such as `"root": "src\\app"`, needs `/` instead.

‎changelog.d/293.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
section: fixes
3+
---
4+
5+
- **A tracked file whose name has `:` or `\` no longer breaks every projection** (#293, #276, reported by @IgnacioN99). On macOS and Linux, such names are valid file names, so Projections now reads and writes them like any other file. Before, one such file made every projection of the revision fail with `Unsafe repository path`, even projections that never read it. On Windows, names the operating system can't create are skipped and listed in provenance `limitations`, with the reason for each. Paths that collide on a case-insensitive file system are handled the same way. A definition that explicitly requires a skipped name fails and names the file. Paths that would leave the revision's directory or reach `.git` are still refused, and every error from these checks now names the path. `projections doctor` warns about tracked paths this platform can't write. See [Execution](docs/runner/execution.md).

‎docs/reference/extractor-sdk.md‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,18 @@ pub fn text<'a>(value: &'a Value, label: &str) -> Result<&'a str>
367367
pub fn unique_relationships(relationships: Vec<Value>) -> Vec<Value>
368368
```
369369

370+
### `windows_reason`
371+
372+
```rust
373+
pub fn windows_reason(filename: &str) -> Option<String>
374+
```
375+
376+
Why Windows can't create `filename`: a character it doesn't allow in file
377+
names (`:` would write an NTFS alternate data stream, `\` another
378+
directory), a trailing dot or space it would drop, or a reserved device
379+
name. Defined on every platform so it is tested everywhere; the runner
380+
skips such paths on Windows, and so does reading a Git tree there.
381+
370382
### `within`
371383

372384
```rust

‎docs/runner/commands.md‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,11 @@ projections validate /tmp/services.extraction.json
137137
declared, what the working tree has prepared, stale steps, and missing tools),
138138
the active workspace strategy and why it was chosen, each extractor's program
139139
and files, custom renderer bundles (and running dev servers), comparator
140-
files, the agent skill, and an available browser for images and checks. It
141-
never runs repository code or modifies files, exits nonzero if anything is
142-
missing, and supports `--json` for the `projections.doctor` document.
140+
files, tracked paths this platform can't write to disk (naming each one; see
141+
[Execution](execution.md)), the agent skill, and an available browser for
142+
images and checks. It never runs repository code or modifies files, exits
143+
nonzero if anything is missing, and supports `--json` for the
144+
`projections.doctor` document.
143145
- `prepare` runs the environment file's prepare steps in the working tree—or,
144146
with `--revision REV`, in the workspace for that commit. Each step runs only
145147
if its key files changed since its last run there (`--force` reruns all steps;

‎docs/runner/execution.md‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,24 @@ On Windows, symlinks are skipped the same way because creating them requires
105105
Developer Mode or administrator rights; extractors that depend on symlinks
106106
should run on Unix or inside a container.
107107

108+
File names follow the runner's platform. On macOS and Linux, any name Git can
109+
track is written as is, including names with `:` or `\`. On Windows, Projections
110+
skips names containing `<>:"|?*\` or control characters, names ending in a dot
111+
or space, and reserved device names such as `NUL` or `COM1`. If a definition
112+
explicitly requires one of those skipped names (a declared source or extractor
113+
program), extraction fails and names the file. On a case-insensitive file system
114+
(the default on macOS and Windows), two tracked paths that differ only in case
115+
can't both be written out. Projections writes the first in byte order and skips
116+
the second. Skipped paths are listed in provenance `limitations`, each with its
117+
reason, and `projections doctor` warns about them ahead of time.
118+
119+
Skipping never applies to a path that would land outside the revision's
120+
directory or inside `.git`. That covers empty or absolute paths, paths with `.`
121+
or `..` segments, and `.git` (along with the names Windows treats as `.git`). On
122+
Windows, it also covers a drive such as `C:` or a `\..\` segment. Git itself
123+
refuses to check out these paths, so Projections refuses any revision that
124+
contains one, and the error names the path.
125+
108126
For the working tree, Projections sees the same files Git does: tracked files as
109127
they currently exist on disk, plus untracked non-ignored files, respecting
110128
deletions. File-only built-ins see that exact set (the SDK refuses ignored

‎packages/cli/src/doctor.rs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ use crate::{browser, collectors, dev_server, runtime::Runner, scaffold, workspac
77
use serde_json::{Value, json};
88
use std::path::{Path, PathBuf};
99

10+
mod paths;
11+
1012
fn item(status: &str, title: &str, message: impl Into<String>) -> Value {
1113
json!({"status": status, "title": title, "message": message.into()})
1214
}
@@ -211,6 +213,7 @@ pub fn doctor(runner: &Runner, ids: &[String]) -> Value {
211213
"This repository has no commits yet; commit once (git commit --allow-empty -m Start)",
212214
));
213215
}
216+
items.extend(paths::items(runner));
214217
let (status, message) = crate::skill::health::status(&root);
215218
items.push(item(status, "Agent skill", message));
216219
let environment = runner.environment_report();

‎packages/cli/src/doctor/paths.rs‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
//! `projections doctor`'s check of the paths the repository tracks: whether
2+
//! this platform can materialize each as named.
3+
4+
use super::item;
5+
use crate::runtime::Runner;
6+
use serde_json::Value;
7+
8+
/// Tracked paths this platform can't materialize, named: unsafe ones fail
9+
/// every projection of a revision that holds them; names it can't write are
10+
/// skipped, so projections that read them miss them. Nothing when all can be,
11+
/// and a warning when they can't be checked.
12+
pub(super) fn items(runner: &Runner) -> Vec<Value> {
13+
let paths = match runner.unmaterializable_paths() {
14+
Ok(paths) => paths,
15+
Err(error) => {
16+
return vec![item(
17+
"warn",
18+
"Repository paths",
19+
format!("Couldn't check which tracked paths this platform can materialize: {error:#}"),
20+
)];
21+
}
22+
};
23+
let mut items = Vec::new();
24+
if !paths.refused.is_empty() {
25+
items.push(item(
26+
"fail",
27+
"Repository paths",
28+
format!(
29+
"Projections refuses to materialize {}, so every projection of a revision that tracks {} fails: {}",
30+
plural(paths.refused.len(), "unsafe path"),
31+
if paths.refused.len() == 1 { "it" } else { "one" },
32+
named(&paths.refused)
33+
),
34+
));
35+
}
36+
if !paths.skipped.is_empty() {
37+
let them = if paths.skipped.len() == 1 { "it" } else { "them" };
38+
items.push(item(
39+
"warn",
40+
"Repository paths",
41+
format!(
42+
"{} can't be written on this platform, so Projections skips {them}, and a projection that reads {them} misses {them} or fails. Rename {them} to use {them} here: {}",
43+
plural(paths.skipped.len(), "tracked path"),
44+
named(&paths.skipped)
45+
),
46+
));
47+
}
48+
items
49+
}
50+
51+
fn plural(count: usize, noun: &str) -> String {
52+
format!("{count} {noun}{}", if count == 1 { "" } else { "s" })
53+
}
54+
55+
/// At most this many paths are named in one item.
56+
const MAX_NAMED_PATHS: usize = 20;
57+
58+
fn named(paths: &[String]) -> String {
59+
let mut named = paths.iter().take(MAX_NAMED_PATHS).cloned().collect::<Vec<_>>().join(", ");
60+
if paths.len() > MAX_NAMED_PATHS {
61+
named.push_str(&format!(" and {} more", paths.len() - MAX_NAMED_PATHS));
62+
}
63+
named
64+
}

‎packages/cli/src/runtime/capture/locate.rs‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use serde_json::json;
1212
use super::{Listing, names, pathspec, skip_nested};
1313
use crate::runtime::{
1414
Runner,
15-
revision::{Revision, Storage, listed_entry, mode, safe_path, windows_symlink},
15+
revision::{Revision, Storage, listed_entry, mode, windows_symlink},
1616
};
1717

1818
impl Runner {
@@ -41,11 +41,8 @@ impl Runner {
4141
None,
4242
));
4343
}
44-
let (listed, skipped) = self.tree_entries(&repository, &commit, &pathspec, 64 * 1024 * 1024)?;
45-
let mut listing = Listing {
46-
skipped,
47-
..Listing::default()
48-
};
44+
let mut listing = Listing::default();
45+
let listed = self.tree_entries(&repository, &commit, &pathspec, 64 * 1024 * 1024, &mut listing)?;
4946
for entry in listed {
5047
if windows_symlink(&entry.mode) {
5148
listing.skipped.push(entry.filename);
@@ -83,7 +80,9 @@ impl Runner {
8380
if deleted.contains(filename) || listing.entries.contains_key(filename) {
8481
continue;
8582
}
86-
safe_path(filename)?;
83+
if !listing.admits(filename)? {
84+
continue;
85+
}
8786
if !["100644", "100755", "120000"].contains(&mode) || windows_symlink(mode) {
8887
// A submodule's gitlink or another special entry.
8988
listing.skipped.push(filename.to_owned());
@@ -99,7 +98,9 @@ impl Runner {
9998
if skip_nested(filename, &mut listing.skipped)? {
10099
continue;
101100
}
102-
safe_path(filename)?;
101+
if !listing.admits(filename)? {
102+
continue;
103+
}
103104
let Some(metadata) = listed_entry(repository, filename, &mut directories)? else {
104105
continue;
105106
};

‎packages/cli/src/runtime/capture/materialize.rs‎

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use serde_json::json;
99
use super::{Listing, names, pathspec, read::batch_contents, skip_nested};
1010
use crate::runtime::{
1111
MAX_BYTES, MAX_FILES, Runner, WorkingTreeChanged, read_regular,
12-
revision::{Revision, Storage, mode, safe_path, windows_symlink, write_entry},
12+
revision::{Revision, Storage, mode, windows_symlink},
1313
};
1414

1515
const STATUS: [&str; 4] = ["status", "--porcelain=v1", "-z", "--untracked-files=all"];
@@ -74,7 +74,9 @@ impl Runner {
7474
if skip_nested(filename, &mut listing.skipped)? {
7575
continue;
7676
}
77-
safe_path(filename)?;
77+
if !listing.admits(filename)? {
78+
continue;
79+
}
7880
if parent_missing(repository, filename)? {
7981
continue;
8082
}
@@ -98,16 +100,14 @@ impl Runner {
98100
listing.skipped.push(filename.to_owned());
99101
continue;
100102
}
101-
write_entry(target, filename, &bytes, mode(&before))?;
102-
listing.add(filename, mode(&before), None);
103+
listing.write(target, filename, &bytes, mode(&before), None)?;
103104
}
104105
Ok(())
105106
}
106107

107108
/// Writes a commit's blobs to `target`, read from Git in one batch.
108109
fn copy_commit(&self, repository: &Path, commit: &str, pathspec: &[&str], target: &Path, listing: &mut Listing) -> Result<()> {
109-
let (listed, skipped) = self.tree_entries(repository, commit, pathspec, 16 * 1024 * 1024)?;
110-
listing.skipped.extend(skipped);
110+
let listed = self.tree_entries(repository, commit, pathspec, 16 * 1024 * 1024, listing)?;
111111
too_large(listed.len() <= MAX_FILES, || format!("{MAX_FILES} files"))?;
112112
if listed.is_empty() {
113113
return Ok(());
@@ -132,8 +132,7 @@ impl Runner {
132132
if windows_symlink(&entry.mode) {
133133
listing.skipped.push(entry.filename.clone());
134134
} else {
135-
write_entry(target, &entry.filename, bytes, &entry.mode)?;
136-
listing.add(&entry.filename, &entry.mode, Some(entry.object.clone()));
135+
listing.write(target, &entry.filename, bytes, &entry.mode, Some(entry.object.clone()))?;
137136
}
138137
}
139138
ensure!(end == output.len(), "Trailing Git object bytes");
@@ -235,7 +234,10 @@ fn parent_missing(repository: &Path, filename: &str) -> Result<bool> {
235234
}
236235
Err(error) => return Err(error.into()),
237236
};
238-
ensure!(metadata.is_dir() && !metadata.is_symlink(), "Working path traverses a symlink");
237+
ensure!(
238+
metadata.is_dir() && !metadata.is_symlink(),
239+
"Working path traverses a symlink: {filename}"
240+
);
239241
}
240242
Ok(false)
241243
}
@@ -261,7 +263,7 @@ fn read_unchanged(source: &Path, before: &fs::Metadata, filename: &str) -> Resul
261263
use std::os::unix::fs::MetadataExt;
262264
ensure!(
263265
before.ino() == after.ino() && before.ctime() == after.ctime() && before.ctime_nsec() == after.ctime_nsec(),
264-
WorkingTreeChanged("Working file replaced during capture".into())
266+
WorkingTreeChanged(format!("Working file replaced during capture: {filename}"))
265267
);
266268
}
267269
Ok(bytes)

0 commit comments

Comments
 (0)