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
37 changes: 37 additions & 0 deletions rust/crates/core/src/tests/toolchain_resolution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,43 @@ fn toolchain_resolution_accepts_a_provider_home_reached_through_an_alias_link()
);
}

/// A multi-call program picks its tool from the name it starts with: rustup links `cargo`
/// to itself and runs cargo, but started as `rustup` it runs rustup. The selection keeps
/// the layout's file name beneath the canonical home instead of the link target.
#[test]
fn toolchain_resolution_starts_a_multi_call_link_by_its_layout_name() {
let mut value = input();
installation(&mut value, HOME, "21.0.4");
let link = format!("{HOME}/bin/java");
let target = format!("{HOME}/bin/multiplexer");
let facts = value["filesystem"].as_array_mut().unwrap();
facts.retain(|fact| fact["path"] != link.as_str());
facts.push(json!({"path":link,"canonicalPath":target,"kind":"file","executable":true}));
value["fixed"] = json!({"home":HOME});
let mut unprobed = value.clone();
unprobed["probes"] = json!([]);
let candidates =
candidates_command(value_for_candidates(&unprobed), validated_manifest_json).unwrap();
assert!(
candidates["candidates"]["skipped"]
.as_array()
.unwrap()
.iter()
.any(|row| row["reason"] == "probeFailed" && row["executable"] == link.as_str()),
"{candidates}"
);
// Started by its target's name the program answers as itself.
value["probes"]
.as_array_mut()
.unwrap()
.push(json!({"executable":target,"output":"multiplexer 1.28.2","exitCode":0}));
let result = resolve(&value);
let selected = &result["outcome"]["selection"];
assert_eq!(selected["home"], HOME, "{result}");
assert_eq!(selected["executable"], link.as_str());
assert_eq!(selected["version"], "21.0.4");
}

#[test]
fn toolchain_resolution_definition_plugin_can_be_its_own_installation_provider() {
let mut value = input();
Expand Down
7 changes: 7 additions & 0 deletions rust/crates/toolchain/src/paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@ pub fn within(path: &str, root: &str, platform: &str, case_sensitive: bool) -> b
path == root || path.starts_with(&format!("{}/", root.trim_end_matches('/')))
}

/// The last component of a native path, split with the target platform's separators.
pub fn file_name<'a>(path: &'a str, platform: &str) -> &'a str {
path.rsplit(|character| is_separator(character, rules(platform)))
.next()
.unwrap_or(path)
}

/// Joins a package-independent relative executable layout beneath a native home.
pub fn join(home: &str, relative: &str, platform: &str) -> Option<String> {
if relative.is_empty()
Expand Down
34 changes: 33 additions & 1 deletion rust/crates/toolchain/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,7 @@ fn checked_installation(
"The executable escapes its installation or enters the workspace.",
));
}
let executable = invoked_executable(&executable, &canonical_home, layout, facts)?;
return Ok(json!({"home":canonical_home,"executable":executable}));
}
Err(reject(
Expand All @@ -551,8 +552,39 @@ fn checked_installation(
))
}

/// The path a checked installation's program starts from: its canonical target,
/// unless the layout's file links to a program with another file name. Such a
/// program may pick what to run from the name it was started with (rustup links
/// `cargo` and `rust-analyzer` to itself and runs the named tool; started as
/// `rustup` it runs rustup), so it starts from the layout path beneath the canonical
/// home. The canonical home is the same directory as the home the facts checked, so
/// that path names the same file and resolves to the same checked target.
fn invoked_executable(
canonical: &str,
canonical_home: &str,
layout: &Value,
facts: &Facts,
) -> Validation<String> {
let layout_path = layout["path"].as_str().unwrap_or("");
let name = |path: &str| {
let leaf = paths::file_name(path, &facts.platform);
if facts.case_sensitive {
leaf.to_owned()
} else {
leaf.to_lowercase()
}
};
if name(canonical) == name(layout_path) {
return Ok(canonical.to_owned());
}
paths::join(canonical_home, layout_path, &facts.platform)
.ok_or_else(|| reject(UNSAFE, "executableLayouts", "Invalid executable layout."))
}

/// Validates an external installation against trusted, authority-local native facts.
/// Returns canonical home/executable paths without touching the native filesystem.
/// Returns the canonical home and the executable to start (the canonical target, or the
/// layout path when that target has another file name)
/// without touching the native filesystem.
/// Project-code candidates require their separate consumer trust policy.
pub fn validate_installation(
home: &str,
Expand Down
6 changes: 5 additions & 1 deletion shared/plugins/contract/v1/toolchains/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,11 @@ platform,workspaceRoot,filesystem}` and accepts `workspaceRoots`,
document. Probes are host observations `{executable,output,exitCode}` keyed by the
executable a selection reports (canonical for external installations, lexical for
`fixed:{executable}` and `projectFile` candidates); system installations are
`{tool,home}`.
`{tool,home}`. When an external installation's layout file links to a file with
another name, the reported executable is that layout path beneath the canonical
home instead: a multi-call program such as rustup links `cargo` to itself and picks
the tool from the name it starts with, so starting the link target would run the
wrong program. The facts still check the target's kind, mode and containment.
Shell environment home variables and `commandHomes` come from the native login-shell
adapter, not command-name guessing. An environment candidate selects only through
its declared `path` layout.
Expand Down
4 changes: 4 additions & 0 deletions shared/plugins/reference/domains/toolchains.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@

版本探针 `version.arguments` 的第一个参数可以是选项(`-version`、`--version`),也可以是子命令(Go 这类只用 `go version` 打印版本的工具写 `version`);之后的参数只能是选项,选项不带 `=值`。第一个参数之后出现不带 `-` 的词就拒绝,所以探针不会把脚本、文件之类的操作数交给工具。

外部安装报告的可执行文件是解析掉链接后的规范路径。例外是 `executableLayouts` 里的文件链接到另一个文件名:这时报告规范主目录下的布局路径。rustup 把 `cargo`、`rust-analyzer` 链接到自身,按启动时的名字决定运行哪个工具,从链接目标启动只会运行 rustup;版本探针和启动都用布局路径。

### 选择顺序

| 层 | 规则 |
Expand Down Expand Up @@ -219,6 +221,8 @@ The declarative primitives are frozen: root kinds are `home`, `environment`, and

The first argument of the version probe `version.arguments` is a flag (`-version`, `--version`) or a subcommand (`version`, for a tool such as Go that prints its version only with `go version`); every later argument is a flag, and flags carry no `=value`. A bare word after the first argument is refused, so a probe never hands the tool an operand such as a script or a file.

An external installation reports its canonical executable, with links resolved. The exception is a file in `executableLayouts` that links to a file with another name: Core then reports the layout path beneath the canonical home. rustup links `cargo` and `rust-analyzer` to itself and picks the tool from the name it starts with; started by the link target's name it runs rustup. Version probes and launches use the layout path.

### Selection order

| Layer | Rule |
Expand Down
Loading
Loading