Skip to content

Commit 43da0b3

Browse files
committed
feat(execution): plan run configurations of a remote workspace on the host (Refs 1lck/Lithe-IDEA#2231)
- runConfig.* is routed to the workspace host like workspace.*, git.* and build.*: configuration documents, scripts, interpreters and working directories are read and checked through lithe_workspace::fs. - A host that cannot be reached fails the request instead of reporting a file or folder as missing. - Process kinds, interpreters and inline script suffixes follow the host (Linux or macOS) through run_platform(), so a Windows client never plans a batch or PowerShell run on a remote host. - runConfig.createLaunchPlan returns host paths for executable.path, arguments and inlineScript.path, so the client starts the process on the host without translating the plan. - Test fixtures that build local files keep std::fs.
1 parent 62d44ce commit 43da0b3

13 files changed

Lines changed: 210 additions & 25 deletions

File tree

‎rust/contracts/rust-core-api.md‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4417,6 +4417,15 @@ script file, or a program or interpreter named by a path that is not a file, is
44174417

44184418
Batch runs are `not_supported` outside Windows.
44194419

4420+
In a remote workspace (a request carrying `remoteWorkspace`) every
4421+
`runConfig.*` command reads and checks the host's files, and process runs are
4422+
planned for the host, which is Linux or macOS, so they never get a Windows
4423+
interpreter or a batch script even when the client is Windows. The plan's
4424+
`executable.path`, `arguments` and `inlineScript.path` are host paths, and
4425+
`inlineScriptBase` must name a host temporary directory, so the client starts
4426+
the process on the host without translating the plan. A host that cannot be
4427+
reached fails the request instead of reporting a file as missing.
4428+
44204429
`diagnostics.redactText` accepts `text` and returns `redacted` with
44214430
credentials, tokens, and home-directory paths replaced by stable placeholders
44224431
(`<redacted>` for secrets, `<HOME>` for a macOS/Linux `Users`/`home` path or a

‎rust/crates/core/src/runtime/dispatcher.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,7 @@ fn execute_parsed(mut parsed: CoreRequest, document: Option<&str>) -> CoreRespon
748748
fn has_remote_workspace_adapter(command: &str) -> bool {
749749
command.starts_with("git.")
750750
|| command.starts_with("build.")
751+
|| command.starts_with("runConfig.")
751752
|| matches!(
752753
command,
753754
"workspace.repositories"

‎rust/crates/core/src/tests/remote_sessions.rs‎

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,116 @@ fn remote_build_detects_host_models_normalizes_diagnostics_and_invalidates_sync(
159159
assert_eq!(std::fs::read_dir(placeholder).unwrap().count(), 0);
160160
}
161161

162+
/// Run configurations of a remote workspace are read from the host, checked
163+
/// against the host's files, and planned with host paths, so the client starts
164+
/// the process on the host without translating the plan.
165+
#[test]
166+
fn remote_run_configurations_resolve_and_plan_against_the_host() {
167+
let host = HostFixture::new();
168+
let root = host.root.join("host");
169+
let placeholder = host.root.join("placeholder");
170+
std::fs::create_dir_all(root.join("scripts")).unwrap();
171+
std::fs::create_dir_all(root.join("web")).unwrap();
172+
std::fs::write(
173+
root.join("scripts/build.sh"),
174+
"#!/bin/bash -e\necho built\n",
175+
)
176+
.unwrap();
177+
super::support::write_run_document(
178+
&root,
179+
".lithe/run/configurations.yaml",
180+
&json!({"version": 2, "configurations": [
181+
{"id": "user:build", "name": "build", "provider": "process.shell",
182+
"confidence": "declared", "cwd": "web",
183+
"extensions": {"process": {"script": "scripts/build.sh"}}},
184+
{"id": "user:inline", "name": "inline", "provider": "process.shell",
185+
"confidence": "declared", "extensions": {"process": {"text": "echo hi\n"}}},
186+
{"id": "user:missing", "name": "missing", "provider": "process.shell",
187+
"confidence": "declared", "extensions": {"process": {"script": "scripts/gone.sh"}}},
188+
]}),
189+
);
190+
191+
let resolved = host.request("runConfig.resolve", json!({"root": placeholder}));
192+
let availability = |id: &str| {
193+
resolved["configurations"]
194+
.as_array()
195+
.unwrap()
196+
.iter()
197+
.find(|entry| entry["id"] == id)
198+
.unwrap_or_else(|| panic!("{id} missing from {resolved}"))["availability"]
199+
.clone()
200+
};
201+
// The documents exist only on the host; a runnable entry carries no
202+
// availability.
203+
assert!(availability("user:build").is_null(), "{resolved}");
204+
205+
// The script and its `#!` line are read on the host; every path in the
206+
// plan is the host's.
207+
let plan = host.request(
208+
"runConfig.createLaunchPlan",
209+
json!({"root": placeholder, "configurationId": "user:build"}),
210+
);
211+
let script = root.join("scripts/build.sh");
212+
assert_eq!(plan["executable"], json!({"path": "/bin/bash"}), "{plan}");
213+
assert_eq!(plan["arguments"], json!(["-e", script]), "{plan}");
214+
assert_eq!(plan["workingDirectory"], "web", "{plan}");
215+
216+
// An inline script goes where the client asked, on the host.
217+
let inline = host.request(
218+
"runConfig.createLaunchPlan",
219+
json!({"root": placeholder, "configurationId": "user:inline",
220+
"inlineScriptBase": "/tmp/lithe-run-x/script"}),
221+
);
222+
assert_eq!(
223+
inline["inlineScript"]["path"], "/tmp/lithe-run-x/script.sh",
224+
"{inline}"
225+
);
226+
assert_eq!(inline["inlineScript"]["content"], "echo hi\n", "{inline}");
227+
228+
// A script missing on the host is refused before anything starts.
229+
let response: Value = serde_json::from_str(&crate::execute_json(
230+
&json!({
231+
"id": test_support::unique_id(), "command": "runConfig.createLaunchPlan",
232+
"remoteWorkspace": {"sessionId": host.id, "localRoot": placeholder, "remoteRoot": root},
233+
"payload": {"root": placeholder, "configurationId": "user:missing"},
234+
})
235+
.to_string(),
236+
))
237+
.unwrap();
238+
assert_eq!(response["ok"], false, "{response}");
239+
assert_eq!(
240+
response["error"]["message"], "Script file was not found",
241+
"{response}"
242+
);
243+
244+
// The editor checks a working directory against the host's folders and
245+
// saves through changes the client applies on the host.
246+
let form = host.request(
247+
"runConfig.editorForm",
248+
json!({"root": placeholder, "configurationId": "user:build"}),
249+
);
250+
let mut values = form["values"].as_object().unwrap().clone();
251+
values.insert("workingDirectory".into(), json!("nowhere"));
252+
let refused = host.request(
253+
"runConfig.saveConfiguration",
254+
json!({"root": placeholder, "provider": "process.shell",
255+
"configurationId": "user:build", "values": values}),
256+
);
257+
assert_eq!(
258+
refused["errors"],
259+
json!([{"field": "workingDirectory", "code": "notFound"}]),
260+
"{refused}"
261+
);
262+
values.insert("workingDirectory".into(), json!("scripts"));
263+
let saved = host.request(
264+
"runConfig.saveConfiguration",
265+
json!({"root": placeholder, "provider": "process.shell",
266+
"configurationId": "user:build", "values": values}),
267+
);
268+
assert_eq!(saved["status"], "saved", "{saved}");
269+
assert_eq!(std::fs::read_dir(placeholder).unwrap().count(), 0);
270+
}
271+
162272
/// A remote root opened through a host symbolic link resolves outside the
163273
/// local placeholder; the empty-root boundary must ask the host, not the
164274
/// unmaterialized local projection, before scanning.

‎rust/crates/execution/src/configuration.rs‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ use lithe_platform::paths::{
1010
};
1111
use lithe_platform::Platform;
1212
use lithe_protocol::{CoreError, ErrorCode};
13+
use lithe_workspace::fs;
1314
use serde::{Deserialize, Serialize};
1415
use serde_json::{json, Value};
1516
use std::collections::{BTreeMap, BTreeSet};
16-
use std::fs;
1717
use std::path::{Path, PathBuf};
1818

1919
mod arrangement;
@@ -34,6 +34,18 @@ mod resolve;
3434
mod storage;
3535
mod validation;
3636

37+
/// The platform whose rules decide how a run's process starts: the
38+
/// workspace host's when the workspace is remote, else this machine's.
39+
/// Remote hosts are Linux or macOS, so a remote run never gets a Windows
40+
/// interpreter or a batch script even when this machine is Windows.
41+
pub(crate) fn run_platform() -> Platform {
42+
if lithe_workspace::is_remote() {
43+
Platform::Unix
44+
} else {
45+
Platform::HOST
46+
}
47+
}
48+
3749
pub use arrangement::{arrange, ArrangeRequest};
3850
pub(crate) use arrangement::{valid_folder, Arrangement};
3951
pub use before_launch::{

‎rust/crates/execution/src/configuration/declared.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -781,6 +781,8 @@ pub(crate) fn validate(configuration: &RunConfiguration) -> Result<(), CoreError
781781
#[cfg(test)]
782782
mod tests {
783783
use super::*;
784+
// Fixtures live on this machine.
785+
use std::fs;
784786

785787
fn kind() -> DeclaredRunType {
786788
DeclaredRunType {

‎rust/crates/execution/src/configuration/form.rs‎

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ pub fn configuration_types(request: ConfigurationTypesRequest) -> Result<Value,
4646
.into_iter()
4747
.map(|kind| {
4848
let mut item = json!({ "provider": kind.provider() });
49-
if let Some(availability) = process::platform_availability(kind, Platform::HOST) {
49+
if let Some(availability) = process::platform_availability(kind, run_platform()) {
5050
item["availability"] = json!(availability);
5151
}
5252
item
@@ -138,7 +138,7 @@ pub fn editor_form(request: EditorFormRequest) -> Result<Value, CoreError> {
138138
});
139139
match editor {
140140
Editor::Process(kind) => {
141-
if let Some(availability) = process::platform_availability(kind, Platform::HOST) {
141+
if let Some(availability) = process::platform_availability(kind, run_platform()) {
142142
form["availability"] = json!(availability);
143143
}
144144
}
@@ -672,7 +672,7 @@ fn header_fields(automatic_name: Option<String>) -> Vec<Field> {
672672

673673
/// The fields of `kind`'s editor, with automatic values for `draft`.
674674
fn fields(kind: Kind, root: &Path, draft: &Draft, choices: StepChoices) -> Vec<Value> {
675-
let platform = Platform::HOST;
675+
let platform = run_platform();
676676
let options = draft.options();
677677
let mut fields = header_fields(process::default_name(&options));
678678
if kind.runs_script() {
@@ -906,8 +906,8 @@ impl Common {
906906
let working_directory = match self.working_directory.trim() {
907907
"" | "." => ".".to_string(),
908908
value => normalize_working_directory(root, value).unwrap_or_else(|_| {
909-
let target = process::file_path(root, value, Platform::HOST);
910-
let code = if target.is_dir() {
909+
let target = process::file_path(root, value, run_platform());
910+
let code = if fs::is_dir(&target).unwrap_or(false) {
911911
"outsideProject"
912912
} else {
913913
"notFound"
@@ -1084,7 +1084,7 @@ impl Draft {
10841084
"process",
10851085
serde_json::to_value(&options).expect("process options encode"),
10861086
);
1087-
if let Ok(found) = process::field_errors(&configuration, Platform::HOST) {
1087+
if let Ok(found) = process::field_errors(&configuration, run_platform()) {
10881088
for error in found {
10891089
if error.code != "required"
10901090
&& !errors.iter().any(|known| known.field == error.field)
@@ -1134,14 +1134,19 @@ fn stored_path(
11341134
if let Some(code) = process::text_error(value, 4096) {
11351135
return Err(code);
11361136
}
1137-
let platform = Platform::HOST;
1137+
let platform = run_platform();
11381138
let absolute = lithe_platform::paths::is_absolute_path(value, platform);
11391139
let path = if absolute {
11401140
PathBuf::from(value)
11411141
} else {
11421142
root.join(value)
11431143
};
1144-
if (directory && !path.is_dir()) || (!directory && !path.is_file()) {
1144+
let found = if directory {
1145+
fs::is_dir(&path)
1146+
} else {
1147+
fs::is_file(&path)
1148+
};
1149+
if !found.unwrap_or(false) {
11451150
return Err("notFound");
11461151
}
11471152
let canonical = fs::canonicalize(&path).map_err(|_| "notFound")?;

‎rust/crates/execution/src/configuration/launch.rs‎

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,40 @@ pub fn provider_launch_inputs(request: ProviderLaunchInputsRequest) -> Result<Va
7272
/// the provider or the saved entry named, or an executable owned by a runtime
7373
/// toolchain.
7474
/// Ecosystem launch semantics stay in the plugin that declared the run.
75+
///
76+
/// In a remote workspace the plan names the host's files: the placeholder
77+
/// paths it was assembled with become the host paths they stand for, so the
78+
/// client starts the process on the host without translating any of it.
7579
pub fn create_launch_plan(request: LaunchPlanRequest) -> Result<Value, CoreError> {
80+
assemble_launch_plan(request).map(host_paths)
81+
}
82+
83+
/// Replaces placeholder paths in `plan`'s executable, arguments and inline
84+
/// script with the host paths they stand for; other values stay as they are.
85+
fn host_paths(mut plan: Value) -> Value {
86+
if !lithe_workspace::is_remote() {
87+
return plan;
88+
}
89+
let host = |value: &mut Value| {
90+
if let Some(text) = value.as_str() {
91+
if let Some(mapped) = lithe_workspace::remote_path(Path::new(text)).to_str() {
92+
*value = json!(mapped);
93+
}
94+
}
95+
};
96+
if let Some(path) = plan.pointer_mut("/executable/path") {
97+
host(path);
98+
}
99+
if let Some(arguments) = plan.get_mut("arguments").and_then(Value::as_array_mut) {
100+
arguments.iter_mut().for_each(host);
101+
}
102+
if let Some(path) = plan.pointer_mut("/inlineScript/path") {
103+
host(path);
104+
}
105+
plan
106+
}
107+
108+
fn assemble_launch_plan(request: LaunchPlanRequest) -> Result<Value, CoreError> {
76109
let root = existing_root(&request.root)?;
77110
let layers = read_layers(&root, None, request.local_document)?;
78111
let resolved = resolve_layers(&root, &layers, None)?;
@@ -90,7 +123,7 @@ pub fn create_launch_plan(request: LaunchPlanRequest) -> Result<Value, CoreError
90123
return super::process::launch_plan(
91124
&root,
92125
config,
93-
Platform::HOST,
126+
run_platform(),
94127
request.inline_script_base.as_deref(),
95128
);
96129
}

‎rust/crates/execution/src/configuration/layers.rs‎

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use super::*;
1111
/// editors would otherwise be stored as a literal directory name.
1212
pub(crate) fn normalize_working_directory(root: &Path, value: &str) -> Result<String, CoreError> {
1313
normalize_project_directory(root, value).map_err(|error| {
14-
if value.contains('$') && !root.join(value).is_dir() {
14+
if value.contains('$') && !fs::is_dir(root.join(value)).unwrap_or(false) {
1515
CoreError::new(
1616
ErrorCode::InvalidRequest,
1717
"Working directory variables are not supported; use a path relative to the project",
@@ -73,7 +73,7 @@ pub(crate) fn project_directory_exists(root: &Path, value: &str) -> bool {
7373
let Ok(canonical_target) = fs::canonicalize(root.join(value)) else {
7474
return false;
7575
};
76-
canonical_target.is_dir() && canonical_target.starts_with(canonical_root)
76+
canonical_target.starts_with(canonical_root) && fs::is_dir(&canonical_target).unwrap_or(false)
7777
}
7878

7979
/// Reads a sidecar document below `.lithe`, such as `project.json`, with the
@@ -111,11 +111,15 @@ pub(crate) fn generated_ids(root: &Path) -> BTreeSet<String> {
111111

112112
pub(crate) fn existing_root(value: &str) -> Result<PathBuf, CoreError> {
113113
let path = PathBuf::from(value);
114-
if !path.is_dir() {
115-
return Err(CoreError::new(
116-
ErrorCode::WorkspaceNotFound,
117-
"Project root does not exist",
118-
));
114+
match fs::is_dir(&path) {
115+
Ok(true) => {}
116+
Err(error) if fs::is_transport_failure(&error) => return Err(CoreError::from(error)),
117+
Ok(false) | Err(_) => {
118+
return Err(CoreError::new(
119+
ErrorCode::WorkspaceNotFound,
120+
"Project root does not exist",
121+
))
122+
}
119123
}
120124
Ok(path)
121125
}

‎rust/crates/execution/src/configuration/process.rs‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -688,7 +688,7 @@ pub(crate) fn launch_plan(
688688
let (script, shebang) = match (options.script(), options.text()) {
689689
(Some(script), _) => {
690690
let file = file_path(root, script, platform);
691-
if !file.is_file() {
691+
if !fs::is_file(&file).unwrap_or(false) {
692692
return Err(
693693
CoreError::new(ErrorCode::InvalidRequest, "Script file was not found")
694694
.with_details(script),
@@ -761,7 +761,9 @@ fn require_program(
761761
executable: &Executable,
762762
platform: Platform,
763763
) -> Result<(), CoreError> {
764-
if matches!(executable, Executable::Path(_)) && !file_path(root, value, platform).is_file() {
764+
if matches!(executable, Executable::Path(_))
765+
&& !fs::is_file(file_path(root, value, platform)).unwrap_or(false)
766+
{
765767
return Err(
766768
CoreError::new(ErrorCode::InvalidRequest, "Program was not found").with_details(value),
767769
);
@@ -801,6 +803,8 @@ pub(crate) fn cmd_quote(argument: &str) -> String {
801803
#[cfg(test)]
802804
mod tests {
803805
use super::*;
806+
// Fixtures live on this machine.
807+
use std::fs;
804808

805809
/// A fresh project directory, removed when dropped.
806810
struct Project(PathBuf);

‎rust/crates/execution/src/configuration/resolve.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ pub(crate) fn resolve_layers(
5656
}
5757
}
5858
let global_toolchain = local.get("toolchain").cloned();
59-
let platform = Platform::HOST;
59+
let platform = run_platform();
6060
for configuration in &mut configurations {
6161
configuration.plugin = layers
6262
.plugin_dependency(&configuration.id)

0 commit comments

Comments
 (0)