Skip to content

Commit dd09bb4

Browse files
authored
refactor(platform): 平台规则改为数据,平台专属测试移到 lithe-platform-tests (#48)
- 新增 lithe-platform crate:Platform { Unix, Windows } 与 Platform::HOST, 路径包含判断、斜杠路径、Windows 保留设备名、owner-only 权限、可执行位、 隐藏控制台窗口、batch 参数转义统一成按平台参数计算的函数 - LSP 进程在所有平台都放进 ProcessTree:macOS/Linux 终止或释放语言服务器时 一并结束它启动的子进程(旧代码下子进程会存活);Unix 语言服务器进入独立 进程组,终端 Ctrl-C 不再直接传给它 - Git 路径校验统一使用同一份保留名规则,新增拒绝 CONIN$/CONOUT$ - 补丁指纹无条件计入文件模式,Windows 打开远程 Unix 工作区时结果一致 - SSH Connection::close 调整关闭顺序,Unix 下不再固定等满 2 秒 - 新增 lithe-platform-tests 包:fixture 程序代替 shell 命令,迁入 git-host/tests/process.rs,并新增 LSP 进程树、Windows batch 参数、权限、 Git 可执行文件解析的真实系统测试 - 顺手修复 shared/plugins/sdk/e2e 中原有的 clippy needless-borrow 报错
1 parent a58e07e commit dd09bb4

46 files changed

Lines changed: 1721 additions & 747 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎rust/Cargo.lock‎

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎rust/crates/diagnostics/Cargo.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ description = "Structured log catalog, channels, records, queries and the bounde
1111
test-support = []
1212

1313
[dependencies]
14+
lithe-platform = { path = "../platform" }
1415
lithe-protocol = { path = "../protocol" }
1516
chrono = { version = "0.4", default-features = false, features = ["std", "clock"] }
1617
regex = "1.11"

‎rust/crates/diagnostics/src/channel.rs‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,7 @@ pub fn validate_segment(value: &str) -> Result<(), LogError> {
131131
.all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || b"._-".contains(&c));
132132
// Win32 strips trailing dots and reserves device basenames even with an extension.
133133
// Reject them on every host so the same identity always names the same file.
134-
let basename = value.split('.').next().unwrap_or("");
135-
let device = matches!(basename, "con" | "prn" | "aux" | "nul")
136-
|| (basename.len() == 4
137-
&& (basename.starts_with("com") || basename.starts_with("lpt"))
138-
&& matches!(basename.as_bytes()[3], b'1'..=b'9'));
139-
if !valid || value.ends_with('.') || device {
134+
if !valid || !lithe_platform::names::is_portable_windows_component(value) {
140135
return Err(LogError::new(-32033, "Invalid channel segment"));
141136
}
142137
Ok(())

‎rust/crates/git-host/Cargo.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ libc = "0.2"
1212
windows-sys = { version = "0.61", features = ["Win32_Foundation", "Win32_System_Pipes", "Win32_System_JobObjects", "Win32_System_Diagnostics_ToolHelp", "Win32_System_Threading", "Win32_Security"] }
1313

1414
[dependencies]
15+
lithe-platform = { path = "../platform" }
1516
rand = "0.8"
1617
serde = { version = "1", features = ["derive"] }
1718
serde_json = "1"

‎rust/crates/git-host/src/configuration.rs‎

Lines changed: 30 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,42 @@
11
//! Native executable resolution and per-child Git environment configuration.
2+
use lithe_platform::fs::is_launchable_file;
3+
use lithe_platform::paths::slash_separated;
4+
use lithe_platform::Platform;
25
use std::path::PathBuf;
36
use std::process::Command;
47

58
/// Resolves a user-selected executable or the first runnable Git on PATH.
69
pub fn executable(selected: Option<&str>) -> Option<PathBuf> {
10+
let platform = Platform::HOST;
711
let candidates = match selected {
812
Some(path) => vec![PathBuf::from(path)],
9-
None => std::env::split_paths(&std::env::var_os("PATH").unwrap_or_default())
10-
.flat_map(|directory| {
11-
#[cfg(windows)]
12-
let names = ["git.exe"];
13-
#[cfg(not(windows))]
14-
let names = ["git"];
15-
names.map(|name| directory.join(name))
16-
})
17-
.collect(),
13+
None => {
14+
let name = platform.executable_name("git");
15+
std::env::split_paths(&std::env::var_os("PATH").unwrap_or_default())
16+
.map(|directory| directory.join(&name))
17+
.collect()
18+
}
1819
};
1920
candidates
2021
.into_iter()
2122
.find(|path| {
22-
let Ok(metadata) = path.metadata() else {
23-
return false;
24-
};
25-
#[cfg(unix)]
26-
{
27-
use std::os::unix::fs::PermissionsExt;
28-
metadata.is_file() && metadata.permissions().mode() & 0o111 != 0
29-
}
30-
#[cfg(not(unix))]
31-
{
32-
metadata.is_file()
33-
}
23+
path.metadata()
24+
.is_ok_and(|metadata| is_launchable_file(&metadata))
3425
})
35-
.map(launch_path)
26+
.map(|path| launch_path(path, platform))
3627
}
3728

3829
/// Some Windows hosts (disk-encryption or endpoint-security filters) make a Git started through a
3930
/// backslash path print nothing to a captured pipe, while the same executable started through a
4031
/// forward-slash path behaves normally. Windows accepts both separators, so launch with `/`.
41-
fn launch_path(path: PathBuf) -> PathBuf {
42-
#[cfg(windows)]
43-
{
44-
if let Some(text) = path.to_str() {
45-
return PathBuf::from(forward_slash_path(text));
32+
/// Verbatim (`\\?\`) and UNC (`\\server`) paths keep their native form because `/` is not valid
33+
/// there.
34+
fn launch_path(path: PathBuf, platform: Platform) -> PathBuf {
35+
match path.to_str() {
36+
Some(text) if !text.starts_with(r"\\") => {
37+
PathBuf::from(slash_separated(text, platform).into_owned())
4638
}
47-
}
48-
path
49-
}
50-
51-
/// Verbatim (`\\?\`) and UNC (`\\server`) paths keep their native form because `/` is not valid there.
52-
#[cfg(any(windows, test))]
53-
fn forward_slash_path(path: &str) -> String {
54-
if path.starts_with(r"\\") {
55-
path.to_owned()
56-
} else {
57-
path.replace('\\', "/")
39+
_ => path,
5840
}
5941
}
6042

@@ -158,34 +140,24 @@ mod tests {
158140
}
159141

160142
#[test]
161-
fn launch_paths_use_forward_slashes_except_unc_and_verbatim() {
143+
fn windows_launch_paths_use_forward_slashes_except_unc_and_verbatim() {
144+
let launch = |path: &str| launch_path(PathBuf::from(path), Platform::Windows);
162145
assert_eq!(
163-
forward_slash_path(r"D:\Program Files\Git\cmd\git.exe"),
164-
"D:/Program Files/Git/cmd/git.exe"
146+
launch(r"D:\Program Files\Git\cmd\git.exe"),
147+
PathBuf::from("D:/Program Files/Git/cmd/git.exe")
165148
);
166149
assert_eq!(
167-
forward_slash_path("D:/Git/cmd/git.exe"),
168-
"D:/Git/cmd/git.exe"
150+
launch("D:/Git/cmd/git.exe"),
151+
PathBuf::from("D:/Git/cmd/git.exe")
169152
);
170153
for native in [r"\\server\share\git.exe", r"\\?\D:\Git\cmd\git.exe"] {
171-
assert_eq!(forward_slash_path(native), native);
154+
assert_eq!(launch(native), PathBuf::from(native));
172155
}
173156
}
174157

175-
// Regression: a backslash launch path makes some Windows hosts capture empty Git output, so the
176-
// resolved executable must reach the process spawner with forward slashes.
177-
#[cfg(windows)]
178158
#[test]
179-
fn selected_executable_is_resolved_with_forward_slashes() {
180-
let file = std::env::temp_dir().join(format!(
181-
"lithe-git-host-launch-path-{}.exe",
182-
std::process::id()
183-
));
184-
std::fs::write(&file, b"").unwrap();
185-
let resolved = executable(file.to_str());
186-
std::fs::remove_file(&file).unwrap();
187-
let resolved = resolved.unwrap().to_string_lossy().into_owned();
188-
assert!(!resolved.contains('\\'), "{resolved}");
189-
assert!(resolved.ends_with(".exe"), "{resolved}");
159+
fn unix_launch_paths_keep_backslashes_as_name_characters() {
160+
let path = PathBuf::from(r"/opt/odd\name/git");
161+
assert_eq!(launch_path(path.clone(), Platform::Unix), path);
190162
}
191163
}

‎rust/crates/git-host/src/lib.rs‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -355,12 +355,7 @@ impl InputFile {
355355
));
356356
let mut options = OpenOptions::new();
357357
options.read(true).write(true).create_new(true);
358-
#[cfg(unix)]
359-
{
360-
use std::os::unix::fs::OpenOptionsExt;
361-
options.mode(0o600);
362-
}
363-
match options.open(&path) {
358+
match lithe_platform::fs::owner_only_file(&mut options).open(&path) {
364359
Ok(file) => {
365360
let mut owned = Self { file, path };
366361
owned.file.write_all(input)?;

0 commit comments

Comments
 (0)