дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-04 09:11:10 +02:00
Plugins can open a file in the built-in text editor even when a custom editor claims it (workspace.editor open-as-text, SDK open_editor_as_text), and commands can contribute to the Open In submenu (openIn menu location, file as the argument, no submenus). Docs, versioning table and rebuilt components included. Fixes #6852 Fixes #6853
5795 рядки
247 KiB
Rust
5795 рядки
247 KiB
Rust
//! [`HostApi`] for one plugin's code: what its imports reach in the IDE.
|
||
//! Capabilities were checked by the runtime before any of this runs.
|
||
|
||
use super::{CommitAnswer, CommitRequest, CommitSelection, PluginContextKey, PluginEvent, PluginEventSink};
|
||
use ide_core::ipc_path::to_ipc;
|
||
use ide_core::{CancellationToken, TextEdit};
|
||
use ide_extension::Capability;
|
||
use ide_extension::Package;
|
||
use ide_extension::ledger::{Ledger, Subject};
|
||
use ide_language::ProgramPin;
|
||
use ide_plugin::{
|
||
CommitChange, CommitDraft, EventKind, HostApi, HostError, HttpRequest, HttpResponse, HttpStreamHead,
|
||
NotificationLevel, ProcessChunk, ProcessCommand, ProcessOptions, ProcessOutput, Prompt, RepositoryStatus,
|
||
SettingsTarget, StatusItem,
|
||
};
|
||
use parking_lot::{Mutex, RwLock};
|
||
use std::collections::{BTreeMap, BTreeSet, HashMap};
|
||
use std::io::Read;
|
||
use std::path::{Path, PathBuf};
|
||
use std::process::{Child, ChildStdin, Command as StdCommand, Stdio};
|
||
use std::sync::Arc;
|
||
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
|
||
use std::time::{Duration, Instant};
|
||
|
||
/// Largest storage of one plugin (keys + values).
|
||
const MAX_STORAGE: usize = 64 << 20;
|
||
const MAX_KEY: usize = 256;
|
||
/// Most status bar items per plugin.
|
||
const MAX_STATUS_ITEMS: usize = 8;
|
||
const MAX_CONTEXT_KEYS: usize = 100;
|
||
/// Most diff text of a pending commit, all files together.
|
||
const MAX_COMMIT_DIFF: usize = 1 << 20;
|
||
/// How long a window may take to describe its pending commit (it saves editors first).
|
||
const COMMIT_SELECTION_TIMEOUT: Duration = Duration::from_secs(30);
|
||
const COMMIT_MESSAGE_TIMEOUT: Duration = Duration::from_secs(10);
|
||
|
||
/// (window or `None` for every window, plugin, key).
|
||
type ContextKey = (Option<String>, String, String);
|
||
|
||
/// What plugins see of the open projects; provided by the app
|
||
/// ([`super::PluginHost::set_workspace`]), over [`crate::Host`] on the desktop.
|
||
pub trait PluginWorkspace: Send + Sync {
|
||
/// Execution host of the Database subsystem; absent on unsupported hosts.
|
||
fn database_host(&self) -> Option<Arc<crate::Host>> {
|
||
None
|
||
}
|
||
|
||
/// A freshly resolved configuration owned by this plugin in the invoking project.
|
||
fn run_configuration(&self, _client: &str, _plugin: &str, _config: &str) -> Result<ide_run::RunConfig, HostError> {
|
||
Err(HostError::NotFound("run configuration unavailable".into()))
|
||
}
|
||
|
||
/// Window-owned reports shared with the MCP tools.
|
||
fn open_editors(&self, _client: &str) -> Result<Vec<ide_plugin::OpenEditor>, HostError> {
|
||
Ok(Vec::new())
|
||
}
|
||
/// Shared Find in Files service, preferring the invoking window's document store.
|
||
fn search_text(
|
||
&self,
|
||
_client: Option<&str>,
|
||
_root: &Path,
|
||
_query: ide_plugin::TextQuery,
|
||
_limit: u32,
|
||
_cancel: &CancellationToken,
|
||
) -> Result<ide_plugin::TextSearch, HostError> {
|
||
Err(HostError::Failed("search unavailable".into()))
|
||
}
|
||
/// Problems view reports for this window's project.
|
||
fn problems(&self, _client: &str, _path: Option<&Path>) -> Result<Vec<ide_plugin::FileProblems>, HostError> {
|
||
Ok(Vec::new())
|
||
}
|
||
/// Fork a recorded native agent session into a new task in the invoking
|
||
/// window's project, returning the new task id (API 1.18).
|
||
fn fork_agent_session(
|
||
&self,
|
||
_client: &str,
|
||
_agent: &str,
|
||
_session_id: &str,
|
||
_cwd: &Path,
|
||
) -> Result<String, HostError> {
|
||
Err(HostError::Failed("agent fork unavailable".into()))
|
||
}
|
||
|
||
/// Roots of the open projects.
|
||
fn projects(&self) -> Vec<PathBuf>;
|
||
/// Roots of the workspace members attached to the project at `root` (a
|
||
/// project window group's folders, as the editor and LSP resolve them).
|
||
/// Empty for a plain project.
|
||
fn member_roots(&self, _root: &Path) -> Vec<PathBuf> {
|
||
Vec::new()
|
||
}
|
||
/// Unsaved text of the open document at `path`.
|
||
fn buffer_text(&self, path: &Path) -> Option<String>;
|
||
/// A window's unsaved text, with the normal project overlay as fallback.
|
||
fn buffer_text_in(&self, _client: Option<&str>, path: &Path) -> Option<String> {
|
||
self.buffer_text(path)
|
||
}
|
||
/// An editor owned by this window.
|
||
fn document_in(&self, _client: &str, path: &Path) -> Option<(u64, String)> {
|
||
self.document(path)
|
||
}
|
||
/// Whether the editor holds changes for `path` that are not on disk yet;
|
||
/// writing such a file would lose them.
|
||
fn buffer_dirty(&self, _path: &Path) -> bool {
|
||
false
|
||
}
|
||
/// Up to `limit` files of the project index below `root` whose root-relative path satisfies
|
||
/// `matches`; `None` when no index covers `root` yet (the caller walks the disk instead).
|
||
fn indexed_files(&self, _root: &Path, _matches: &dyn Fn(&Path) -> bool, _limit: usize) -> Option<Vec<PathBuf>> {
|
||
None
|
||
}
|
||
/// Sends `method` to the running language server named `server` of the project at
|
||
/// `root`: a request, or with `notify` a notification (the result is null).
|
||
#[allow(clippy::too_many_arguments)]
|
||
fn lsp_request(
|
||
&self,
|
||
_root: &Path,
|
||
server: &str,
|
||
_method: &str,
|
||
_params: Option<serde_json::Value>,
|
||
_notify: bool,
|
||
_cancel: &CancellationToken,
|
||
_timeout: Duration,
|
||
) -> Result<serde_json::Value, HostError> {
|
||
Err(HostError::NotFound(format!("no language server `{server}` is running for this project")))
|
||
}
|
||
/// What the IDE has resolved of the Maven / Gradle builds of the project at `root`;
|
||
/// never runs a build tool.
|
||
fn build_graph(&self, _root: &Path) -> Result<super::BuildGraph, HostError> {
|
||
Ok(super::BuildGraph::default())
|
||
}
|
||
/// Current index of the project rooted at `root`; never reads disk or calls LSP.
|
||
fn symbols(
|
||
&self,
|
||
_root: &Path,
|
||
_query: &str,
|
||
_limit: usize,
|
||
_cancel: &CancellationToken,
|
||
_deadline: Instant,
|
||
) -> Result<Vec<ide_plugin::SymbolInfo>, HostError> {
|
||
Err(HostError::NotFound("no project index is available".into()))
|
||
}
|
||
/// Version and text of the open document at `path`.
|
||
fn document(&self, path: &Path) -> Option<(u64, String)>;
|
||
/// Applies `edits` to the open document at `path` if it is still at
|
||
/// `version`, as one change: one undo step in the editor, or none for a
|
||
/// `background` caller (a plugin event rather than a command).
|
||
fn apply_edits(&self, path: &Path, version: u64, edits: &[TextEdit], background: bool) -> Result<u64, HostError>;
|
||
/// Code from the project may run (git hooks and config on fetch).
|
||
fn trusted(&self, root: &Path) -> bool;
|
||
/// Git roots with the same directory mappings as the VCS tool window.
|
||
fn repositories(&self, _root: &Path) -> Result<ide_vcs::Repos, HostError> {
|
||
Err(HostError::NotFound("no open project repositories".into()))
|
||
}
|
||
/// The git executable of the project at `root` (see [`crate::Project::git_executable`]).
|
||
fn git_executable(&self, _root: &Path) -> Option<PathBuf> {
|
||
None
|
||
}
|
||
/// Settings › Version Control › Git › Enable Git integration of the project at `root`:
|
||
/// off, plugins find no repository there.
|
||
fn git_enabled(&self, _root: &Path) -> bool {
|
||
true
|
||
}
|
||
/// Root of the project open in `window`.
|
||
fn project_of(&self, window: &str) -> Option<PathBuf>;
|
||
/// Windows showing the open project at `root`, the most recently focused first.
|
||
fn project_windows(&self, _root: &Path) -> Vec<String> {
|
||
Vec::new()
|
||
}
|
||
/// Shows `items` as producer `source`'s diagnostics of `path` (replacing earlier ones).
|
||
fn publish_diagnostics(&self, source: &str, path: &Path, items: Vec<ide_core::Diagnostic>)
|
||
-> Result<(), HostError>;
|
||
/// The encoding git content of `path` is read in (the project's file encoding).
|
||
fn vcs_encoding(&self, _path: &Path) -> ide_vcs::TextEncoding {
|
||
encoding_rs::UTF_8.into()
|
||
}
|
||
/// The user's values of `plugin`'s settings: the project's at `root`
|
||
/// (over global), or global.
|
||
fn plugin_settings(&self, _plugin: &str, _root: Option<&Path>) -> serde_json::Map<String, serde_json::Value> {
|
||
serde_json::Map::new()
|
||
}
|
||
}
|
||
|
||
/// Where plugin secrets live (the OS keychain on the desktop). Plugins can
|
||
/// only ask whether one is set; values go out only inside `net` requests.
|
||
pub trait SecretStore: Send + Sync {
|
||
fn get(&self, plugin: &str, name: &str) -> Option<String>;
|
||
/// Sets, or (`None`) deletes.
|
||
fn set(&self, plugin: &str, name: &str, value: Option<&str>) -> Result<(), String>;
|
||
}
|
||
|
||
/// Fallback [`SecretStore`]: one file per plugin, readable only by the user.
|
||
pub struct FileSecretStore {
|
||
pub dir: PathBuf,
|
||
}
|
||
|
||
impl FileSecretStore {
|
||
fn file(&self, plugin: &str) -> PathBuf {
|
||
self.dir.join(plugin).join("secrets.json")
|
||
}
|
||
|
||
fn load(&self, plugin: &str) -> BTreeMap<String, String> {
|
||
std::fs::read(self.file(plugin)).ok().and_then(|b| serde_json::from_slice(&b).ok()).unwrap_or_default()
|
||
}
|
||
}
|
||
|
||
impl SecretStore for FileSecretStore {
|
||
fn get(&self, plugin: &str, name: &str) -> Option<String> {
|
||
self.load(plugin).remove(name)
|
||
}
|
||
|
||
fn set(&self, plugin: &str, name: &str, value: Option<&str>) -> Result<(), String> {
|
||
let mut map = self.load(plugin);
|
||
match value {
|
||
Some(v) => map.insert(name.to_owned(), v.to_owned()),
|
||
None => map.remove(name),
|
||
};
|
||
let file = self.file(plugin);
|
||
std::fs::create_dir_all(&self.dir).map_err(|e| e.to_string())?;
|
||
ide_vfs::create_private_dir(file.parent().expect("has parent")).map_err(|e| e.to_string())?;
|
||
let bytes = serde_json::to_vec(&map).map_err(|e| e.to_string())?;
|
||
replace_entry(&file, &bytes).map_err(|e| e.to_string())
|
||
}
|
||
}
|
||
|
||
/// Plugin secrets in the OS credential store (Secret Service, Windows Credential
|
||
/// Manager), like saved SSH and agent values. Without a credential store a value
|
||
/// stays in the owner-only [`FileSecretStore`] file, which also still answers for
|
||
/// values saved there by an older version until they are set again.
|
||
pub struct CredentialSecretStore {
|
||
store: ide_ssh::secrets::SecretFile,
|
||
fallback: FileSecretStore,
|
||
}
|
||
|
||
impl CredentialSecretStore {
|
||
/// `dir`: the plugins' data directory (the index file and the fallback files live there).
|
||
pub fn new(dir: PathBuf) -> Self {
|
||
Self::with_store(ide_ssh::secrets::SecretFile::new(dir.join("plugin-secrets.json")), dir)
|
||
}
|
||
|
||
fn with_store(store: ide_ssh::secrets::SecretFile, dir: PathBuf) -> Self {
|
||
Self { store, fallback: FileSecretStore { dir } }
|
||
}
|
||
|
||
fn key(plugin: &str, name: &str) -> String {
|
||
format!("{plugin}/{name}")
|
||
}
|
||
|
||
/// Drops a value from the fallback file, writing it only when it held one.
|
||
fn forget_fallback(&self, plugin: &str, name: &str) -> Result<(), String> {
|
||
if self.fallback.get(plugin, name).is_none() {
|
||
return Ok(());
|
||
}
|
||
self.fallback.set(plugin, name, None)
|
||
}
|
||
}
|
||
|
||
impl SecretStore for CredentialSecretStore {
|
||
fn get(&self, plugin: &str, name: &str) -> Option<String> {
|
||
self.store.get(&Self::key(plugin, name)).or_else(|| self.fallback.get(plugin, name))
|
||
}
|
||
|
||
fn set(&self, plugin: &str, name: &str, value: Option<&str>) -> Result<(), String> {
|
||
let key = Self::key(plugin, name);
|
||
match value {
|
||
Some(v) => match self.store.set(&key, v) {
|
||
Ok(()) => self.forget_fallback(plugin, name),
|
||
Err(e) => {
|
||
tracing::warn!("plugin secrets: {key} is kept in the plugin's data directory: {}", e.message);
|
||
self.fallback.set(plugin, name, Some(v))
|
||
}
|
||
},
|
||
None => {
|
||
self.store.delete(&key);
|
||
self.forget_fallback(plugin, name)
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
/// Largest response body a plugin receives.
|
||
const MAX_RESPONSE: usize = 16 << 20;
|
||
/// Largest stdout / stderr kept from a process by default.
|
||
const MAX_OUTPUT: usize = 16 << 20;
|
||
/// Hard ceiling for configurable process output capture.
|
||
const MAX_ALLOWED_OUTPUT: usize = 128 << 20;
|
||
/// Default in-flight stream buffer for lossless streaming backpressure.
|
||
const DEFAULT_STREAM_BUFFER: usize = 256 << 10;
|
||
/// Default total lifetime stream output limit for lossless streaming.
|
||
const DEFAULT_STREAM_MAX_TOTAL: usize = 32 << 20;
|
||
/// Most processes one plugin keeps running at once.
|
||
const MAX_PROCESSES: usize = 8;
|
||
/// Most output one stream of a handle holds until the plugin reads it.
|
||
const MAX_PENDING: usize = 1 << 20;
|
||
/// Most files `find-files` walks before giving up.
|
||
const MAX_WALK: usize = 200_000;
|
||
|
||
/// Runtime for plugin HTTP requests (actor threads block on it).
|
||
pub(super) fn net_runtime() -> &'static tokio::runtime::Runtime {
|
||
static RT: std::sync::OnceLock<tokio::runtime::Runtime> = std::sync::OnceLock::new();
|
||
RT.get_or_init(|| {
|
||
tokio::runtime::Builder::new_multi_thread()
|
||
.worker_threads(2)
|
||
.thread_name("plugin-net")
|
||
.enable_all()
|
||
.build()
|
||
.expect("plugin network runtime")
|
||
})
|
||
}
|
||
|
||
#[derive(Debug)]
|
||
struct RedirectDenied(String);
|
||
impl std::fmt::Display for RedirectDenied {
|
||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||
write!(f, "{} is not granted for this redirect", self.0)
|
||
}
|
||
}
|
||
impl std::error::Error for RedirectDenied {}
|
||
|
||
fn network_error(error: reqwest::Error) -> HostError {
|
||
let mut source: Option<&(dyn std::error::Error + 'static)> = Some(&error);
|
||
while let Some(e) = source {
|
||
if let Some(denied) = e.downcast_ref::<RedirectDenied>() {
|
||
return HostError::Denied(denied.0.clone());
|
||
}
|
||
source = e.source();
|
||
}
|
||
HostError::Failed(ide_http::client::describe(&error))
|
||
}
|
||
|
||
fn response_headers(response: &reqwest::Response) -> Vec<(String, String)> {
|
||
response
|
||
.headers()
|
||
.iter()
|
||
.map(|(k, v)| (k.as_str().to_owned(), String::from_utf8_lossy(v.as_bytes()).into_owned()))
|
||
.collect()
|
||
}
|
||
|
||
fn redirect_capability(from: &reqwest::Url, to: &reqwest::Url) -> Result<Capability, String> {
|
||
if from.scheme() == "https" && to.scheme() == "http" {
|
||
ide_plugin::net_host(to.as_str()).map(Capability::NetHttp)
|
||
} else {
|
||
ide_plugin::net_capability(to.as_str())
|
||
}
|
||
}
|
||
|
||
/// Replaces `{{secret:<name>}}` in `value` with the plugin's secrets.
|
||
fn fill_secrets(value: &str, lookup: impl Fn(&str) -> Option<String>) -> Result<String, HostError> {
|
||
let mut out = String::with_capacity(value.len());
|
||
let mut rest = value;
|
||
while let Some(i) = rest.find("{{secret:") {
|
||
out.push_str(&rest[..i]);
|
||
let after = &rest[i + "{{secret:".len()..];
|
||
let end = after.find("}}").ok_or_else(|| HostError::Invalid("unterminated {{secret:…}}".into()))?;
|
||
let name = &after[..end];
|
||
let secret = lookup(name).ok_or_else(|| HostError::NotFound(format!("secret {name:?} is not set")))?;
|
||
out.push_str(&secret);
|
||
rest = &after[end + 2..];
|
||
}
|
||
out.push_str(rest);
|
||
Ok(out)
|
||
}
|
||
|
||
/// A plugin task shown in the UI.
|
||
struct TaskEntry {
|
||
plugin: String,
|
||
cancel: CancellationToken,
|
||
cancelled: bool,
|
||
}
|
||
|
||
/// A question a plugin's call waits on: the user's (a prompt) or a window's.
|
||
pub(super) struct PromptEntry {
|
||
pub(super) plugin: String,
|
||
pub(super) client: String,
|
||
pub(super) answer: Waiter,
|
||
}
|
||
|
||
pub(super) enum Waiter {
|
||
Prompt(std::sync::mpsc::SyncSender<Option<Vec<String>>>),
|
||
Commit(std::sync::mpsc::SyncSender<CommitAnswer>),
|
||
Command(std::sync::mpsc::SyncSender<super::CommandAnswer>),
|
||
Clipboard(std::sync::mpsc::SyncSender<super::ClipboardAnswer>),
|
||
}
|
||
|
||
/// What a window read from its clipboard, as `ui.read-clipboard` returns it. Oversized text
|
||
/// or images fail here, before the base64 is decoded.
|
||
fn clipboard_content(answer: super::ClipboardAnswer) -> Result<ide_plugin::ClipboardContent, HostError> {
|
||
use base64::Engine;
|
||
let (text, png) = match answer {
|
||
super::ClipboardAnswer::Content { text, png } => (text, png),
|
||
super::ClipboardAnswer::Refused => return Err(HostError::Denied("clipboard.read (the user refused)".into())),
|
||
super::ClipboardAnswer::Failed { message } => return Err(HostError::Failed(message)),
|
||
};
|
||
let max = ide_plugin::MAX_CLIPBOARD_BYTES;
|
||
let too_large = || HostError::Failed(format!("the clipboard holds more than {} MiB", max >> 20));
|
||
// The longest base64 of `max` bytes.
|
||
if text.as_ref().is_some_and(|t| t.len() > max) || png.as_ref().is_some_and(|p| p.len() > max.div_ceil(3) * 4) {
|
||
return Err(too_large());
|
||
}
|
||
let image = match png.filter(|p| !p.is_empty()) {
|
||
Some(png) => Some(
|
||
base64::engine::general_purpose::STANDARD
|
||
.decode(png)
|
||
.map_err(|e| HostError::Failed(format!("the clipboard image is not valid base64: {e}")))?,
|
||
),
|
||
None => None,
|
||
};
|
||
if image.as_ref().is_some_and(|i| i.len() > max) {
|
||
return Err(too_large());
|
||
}
|
||
Ok(ide_plugin::ClipboardContent { text: text.filter(|t| !t.is_empty()), image })
|
||
}
|
||
|
||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||
enum StreamMode {
|
||
Tail,
|
||
Lossless,
|
||
}
|
||
|
||
/// A process a plugin started and has not released (API 1.3 `process.start`, API 1.15 `process.start-stream`).
|
||
/// Two reader threads copy its output into bounded buffers; the handle lives
|
||
/// as long as the plugin instance that started it.
|
||
struct Proc {
|
||
client: Option<String>,
|
||
can_read: parking_lot::Condvar,
|
||
state: Mutex<ProcState>,
|
||
}
|
||
|
||
struct ProcState {
|
||
child: Child,
|
||
stdin: Option<ChildStdin>,
|
||
/// What it printed and the plugin hasn't read yet.
|
||
stdout: Vec<u8>,
|
||
stderr: Vec<u8>,
|
||
/// Its exit status once it ended (a code, or -1 when a signal ended it).
|
||
status: Option<i32>,
|
||
/// Reader threads still copying its pipes into the buffers above.
|
||
readers: usize,
|
||
/// Active wait_process callers. While positive, reader threads buffer up to
|
||
/// max_total instead of buffer_limit so producers emitting more than the chunk
|
||
/// buffer do not deadlock waiting for process exit.
|
||
waiters: usize,
|
||
mode: StreamMode,
|
||
buffer_limit: usize,
|
||
max_total: usize,
|
||
total_stdout: usize,
|
||
total_stderr: usize,
|
||
overflow_err: Option<String>,
|
||
stopped: bool,
|
||
}
|
||
|
||
impl ProcState {
|
||
/// Ends a child whose output went over the limit. `kill_tree` reaps it, so its status
|
||
/// is noted here: a later reap must not wait on a process id that is already gone.
|
||
fn stop_overflowed(&mut self) {
|
||
self.stdin = None;
|
||
ide_core::process::kill_tree(&mut self.child);
|
||
if self.status.is_none() {
|
||
self.status = Some(self.child.try_wait().ok().flatten().and_then(|s| s.code()).unwrap_or(-1));
|
||
}
|
||
}
|
||
}
|
||
|
||
/// Which pipe a reader thread drains.
|
||
#[derive(Clone, Copy)]
|
||
enum Stream {
|
||
Out,
|
||
Err,
|
||
}
|
||
|
||
impl Proc {
|
||
/// Copies `pipe` into the matching buffer until it closes, keeping the last
|
||
/// [`MAX_PENDING`] bytes (in tail mode) or pausing on backpressure (in lossless mode),
|
||
/// then notes that the reader finished.
|
||
fn read(self: &Arc<Self>, mut pipe: impl Read + Send + 'static, stream: Stream) {
|
||
self.state.lock().readers += 1;
|
||
let this = Arc::clone(self);
|
||
std::thread::spawn(move || {
|
||
let mut buf = [0u8; 8 << 10];
|
||
loop {
|
||
{
|
||
let mut state = this.state.lock();
|
||
while state.mode == StreamMode::Lossless
|
||
&& !state.stopped
|
||
&& state.overflow_err.is_none()
|
||
&& match stream {
|
||
Stream::Out => {
|
||
// While wait_process is active, allow readers to buffer up to max_total
|
||
// to prevent pipe write backpressure from hanging the child process.
|
||
if state.waiters > 0 {
|
||
state.stdout.len() >= state.max_total
|
||
} else {
|
||
state.stdout.len() >= state.buffer_limit
|
||
}
|
||
}
|
||
Stream::Err => {
|
||
if state.waiters > 0 {
|
||
state.stderr.len() >= state.max_total
|
||
} else {
|
||
state.stderr.len() >= state.buffer_limit
|
||
}
|
||
}
|
||
}
|
||
{
|
||
this.can_read.wait(&mut state);
|
||
}
|
||
if state.stopped || state.overflow_err.is_some() {
|
||
break;
|
||
}
|
||
}
|
||
|
||
match pipe.read(&mut buf) {
|
||
Ok(0) => break,
|
||
Ok(n) => {
|
||
let mut state = this.state.lock();
|
||
if state.stopped || state.overflow_err.is_some() {
|
||
break;
|
||
}
|
||
if state.mode == StreamMode::Lossless {
|
||
match stream {
|
||
Stream::Out => {
|
||
state.total_stdout = state.total_stdout.saturating_add(n);
|
||
if state.total_stdout > state.max_total {
|
||
let limit_mib = (state.max_total + (1 << 20) - 1) >> 20;
|
||
state.overflow_err =
|
||
Some(format!("process stdout exceeded {limit_mib} MiB limit"));
|
||
state.stop_overflowed();
|
||
this.can_read.notify_all();
|
||
break;
|
||
}
|
||
state.stdout.extend_from_slice(&buf[..n]);
|
||
}
|
||
Stream::Err => {
|
||
state.total_stderr = state.total_stderr.saturating_add(n);
|
||
if state.total_stderr > state.max_total {
|
||
let limit_mib = (state.max_total + (1 << 20) - 1) >> 20;
|
||
state.overflow_err =
|
||
Some(format!("process stderr exceeded {limit_mib} MiB limit"));
|
||
state.stop_overflowed();
|
||
this.can_read.notify_all();
|
||
break;
|
||
}
|
||
state.stderr.extend_from_slice(&buf[..n]);
|
||
}
|
||
}
|
||
} else {
|
||
let out = match stream {
|
||
Stream::Out => &mut state.stdout,
|
||
Stream::Err => &mut state.stderr,
|
||
};
|
||
out.extend_from_slice(&buf[..n]);
|
||
let over = out.len().saturating_sub(MAX_PENDING);
|
||
if over > 0 {
|
||
out.drain(..over);
|
||
let continuation = out.iter().take_while(|b| (**b & 0xC0) == 0x80).count();
|
||
out.drain(..continuation);
|
||
}
|
||
}
|
||
}
|
||
Err(e) if e.kind() == std::io::ErrorKind::Interrupted => continue,
|
||
Err(_) => break,
|
||
}
|
||
}
|
||
let mut state = this.state.lock();
|
||
state.readers -= 1;
|
||
this.can_read.notify_all();
|
||
});
|
||
}
|
||
|
||
/// Notes its exit status if it ended (dropping its stdin then).
|
||
fn reap(&self) -> Result<(), HostError> {
|
||
let mut state = self.state.lock();
|
||
if state.status.is_none() {
|
||
match try_wait_process(&mut state.child) {
|
||
Ok(Some(status)) => {
|
||
state.status = Some(status.code().unwrap_or(-1));
|
||
state.stdin = None;
|
||
}
|
||
Ok(None) => {}
|
||
Err(e) => return Err(failed(e)),
|
||
}
|
||
}
|
||
Ok(())
|
||
}
|
||
|
||
/// Ends it and reaps it, so its status is known right away; killing twice
|
||
/// does nothing.
|
||
fn kill(&self) {
|
||
let mut state = self.state.lock();
|
||
state.stopped = true;
|
||
state.stdin = None;
|
||
if state.status.is_none() {
|
||
ide_core::process::kill_tree(&mut state.child);
|
||
if let Ok(status) = state.child.wait() {
|
||
state.status = Some(status.code().unwrap_or(-1));
|
||
}
|
||
}
|
||
self.can_read.notify_all();
|
||
}
|
||
|
||
/// What it printed since the last call, each stream at most `max` bytes. The status
|
||
/// comes with the last chunk only: once it ended, its pipes reached the end and
|
||
/// nothing is left to read, so a caller can stop at the first chunk that has one.
|
||
fn chunk(&self, max: usize) -> Result<ProcessChunk, HostError> {
|
||
let mut state = self.state.lock();
|
||
if let Some(err) = state.overflow_err.clone() {
|
||
return Err(HostError::Failed(err));
|
||
}
|
||
let eof = state.readers == 0;
|
||
let stdout = take_text(&mut state.stdout, max, eof);
|
||
let stderr = take_text(&mut state.stderr, max, eof);
|
||
let drained = eof && state.stdout.is_empty() && state.stderr.is_empty();
|
||
self.can_read.notify_all();
|
||
Ok(ProcessChunk { stdout, stderr, status: state.status.filter(|_| drained) })
|
||
}
|
||
}
|
||
|
||
/// Decodes complete characters within the byte budget, retaining partial UTF-8
|
||
/// until the reader gets the rest (or EOF). Even a tiny budget makes progress.
|
||
fn take_text(buf: &mut Vec<u8>, max: usize, eof: bool) -> String {
|
||
let limit = max.clamp(1, MAX_PENDING);
|
||
let mut out = String::new();
|
||
let mut consumed = 0;
|
||
while consumed < buf.len() && out.len() < limit {
|
||
let bytes = &buf[consumed..];
|
||
let (valid, invalid) = match std::str::from_utf8(bytes) {
|
||
Ok(text) => (text.len(), None),
|
||
Err(e) => (e.valid_up_to(), Some(e.error_len())),
|
||
};
|
||
let text = std::str::from_utf8(&bytes[..valid]).expect("valid prefix");
|
||
let mut end = text.len().min(limit - out.len());
|
||
while !text.is_char_boundary(end) {
|
||
end -= 1;
|
||
}
|
||
if end == 0 && out.is_empty() && !text.is_empty() {
|
||
end = text.chars().next().unwrap().len_utf8();
|
||
}
|
||
out.push_str(&text[..end]);
|
||
consumed += end;
|
||
if end < valid {
|
||
break;
|
||
}
|
||
match invalid {
|
||
Some(len) if len.is_some() || eof => {
|
||
if !out.is_empty() && out.len() + '\u{fffd}'.len_utf8() > limit {
|
||
break;
|
||
}
|
||
out.push('\u{fffd}');
|
||
consumed += len.unwrap_or(bytes.len() - valid);
|
||
}
|
||
_ => break,
|
||
}
|
||
}
|
||
buf.drain(..consumed);
|
||
out
|
||
}
|
||
|
||
/// Ends descendants before reaping an exited leader, while its process id is
|
||
/// still reserved. Otherwise inherited pipes can keep a completed call waiting.
|
||
fn try_wait_process(child: &mut Child) -> std::io::Result<Option<std::process::ExitStatus>> {
|
||
#[cfg(unix)]
|
||
{
|
||
use rustix::process::{Pid, WaitId, WaitIdOptions, waitid};
|
||
let status = waitid(
|
||
WaitId::Pid(Pid::from_child(child)),
|
||
WaitIdOptions::EXITED | WaitIdOptions::NOHANG | WaitIdOptions::NOWAIT,
|
||
)?;
|
||
if status.is_none() {
|
||
return Ok(None);
|
||
}
|
||
ide_core::process::kill_tree(child);
|
||
child.try_wait()
|
||
}
|
||
#[cfg(not(unix))]
|
||
{
|
||
let status = child.try_wait()?;
|
||
if status.is_some() {
|
||
ide_core::process::kill_tree(child);
|
||
}
|
||
Ok(status)
|
||
}
|
||
}
|
||
|
||
fn env_name_eq(a: &std::ffi::OsStr, b: &std::ffi::OsStr) -> bool {
|
||
#[cfg(windows)]
|
||
{
|
||
// Use the platform's Command key comparison, including non-ASCII names.
|
||
let mut probe = StdCommand::new("unused");
|
||
probe.env(a, "").env(b, "");
|
||
probe.get_envs().count() == 1
|
||
}
|
||
#[cfg(not(windows))]
|
||
{
|
||
a == b
|
||
}
|
||
}
|
||
|
||
/// Validates against the environment after `prepare`, then freezes that snapshot
|
||
/// so only new keys are added, including when the host environment changes later.
|
||
fn append_process_env(command: &mut StdCommand, env: &[(String, String)]) -> Result<(), HostError> {
|
||
if env.len() > 64 {
|
||
return Err(HostError::Invalid("at most 64 environment variables".into()));
|
||
}
|
||
let mut inherited: Vec<_> = std::env::vars_os().collect();
|
||
for (key, value) in command.get_envs() {
|
||
inherited.retain(|(k, _)| !env_name_eq(k, key));
|
||
if let Some(value) = value {
|
||
inherited.push((key.to_owned(), value.to_owned()));
|
||
}
|
||
}
|
||
for (key, value) in env {
|
||
if key.is_empty() || key.len() > 256 || key.contains(['=', '\0']) {
|
||
return Err(HostError::Invalid(format!("{key:?} is not an environment variable name")));
|
||
}
|
||
if value.len() > 4096 || value.contains('\0') {
|
||
return Err(HostError::Invalid(format!("the value of {key:?} is too long or holds a NUL")));
|
||
}
|
||
if inherited.iter().any(|(k, _)| env_name_eq(k, std::ffi::OsStr::new(key))) {
|
||
return Err(HostError::Invalid(format!(
|
||
"environment variable {key:?} already exists; only new variables may be added"
|
||
)));
|
||
}
|
||
inherited.push((key.into(), value.into()));
|
||
}
|
||
command.env_clear().envs(inherited);
|
||
Ok(())
|
||
}
|
||
|
||
/// Approved project-specific programs.
|
||
pub(super) type ProjectPins = Arc<RwLock<HashMap<(String, String, PathBuf), ProgramPin>>>;
|
||
|
||
type ContentRequests = BTreeMap<(String, String, String), (String, Instant)>;
|
||
|
||
type SettingSnapshots = BTreeMap<(Option<String>, String), BTreeMap<String, String>>;
|
||
|
||
/// State shared by the host and every plugin's [`PluginApi`].
|
||
pub(super) struct Shared {
|
||
pub sink: Arc<dyn PluginEventSink>,
|
||
pub websockets: super::websocket::WebSocketHub,
|
||
pub workspace: RwLock<Option<Arc<dyn PluginWorkspace>>>,
|
||
/// `<plugins>/.data`: one directory per plugin id.
|
||
pub data_dir: PathBuf,
|
||
tasks: Mutex<HashMap<u64, TaskEntry>>,
|
||
next_task: AtomicU64,
|
||
/// (plugin, handle id) → the processes it started and has not released.
|
||
processes: Mutex<HashMap<(String, u64), Arc<Proc>>>,
|
||
next_process: AtomicU64,
|
||
connections: super::ipc::Connections,
|
||
http_streams: super::http_stream::HttpStreams,
|
||
pub loopbacks: RwLock<Arc<dyn super::auth::Service>>,
|
||
process_shutdowns: Mutex<Vec<std::thread::JoinHandle<()>>>,
|
||
pub(super) prompts: Mutex<HashMap<u64, PromptEntry>>,
|
||
/// Plugins being stopped: their calls may not open new prompts (taken before `prompts`).
|
||
pub(super) prompts_blocked: Mutex<BTreeSet<String>>,
|
||
/// One network question per plugin; refusals throttle all hosts for 30 seconds.
|
||
net_prompts: Mutex<HashMap<String, Option<Instant>>>,
|
||
pub net_hosts_session_only: AtomicBool,
|
||
session_net_hosts: Mutex<HashMap<String, BTreeSet<String>>>,
|
||
/// Setting-derived approvals kept for the session only (remote hosts),
|
||
/// plugin → `net:setting:` grant → its approved host.
|
||
pub(super) session_setting_hosts: Mutex<HashMap<String, BTreeMap<String, String>>>,
|
||
pub(super) next_prompt: AtomicU64,
|
||
pub(super) run_configurations: super::run_detectors::DynamicConfigurations,
|
||
pub(super) run_targets: Mutex<super::run_targets::Targets>,
|
||
pub(super) operations: Mutex<BTreeMap<String, super::execution::Operation>>,
|
||
pub(super) content_tabs: Mutex<BTreeMap<(String, String, String), ide_plugin::ContentTab>>,
|
||
pub(super) content_requests: Mutex<ContentRequests>,
|
||
|
||
/// (plugin, item id) → item, for windows opened later.
|
||
output: Mutex<BTreeMap<(String, String), String>>,
|
||
decorations: Mutex<BTreeMap<String, Vec<super::DecorationSet>>>,
|
||
/// plugin → its console highlight rules, in order.
|
||
console_rules: Mutex<BTreeMap<String, Vec<ide_plugin::ConsoleRule>>>,
|
||
status: Mutex<BTreeMap<(String, String), StatusItem>>,
|
||
setting_snapshots: Mutex<SettingSnapshots>,
|
||
window_status: Mutex<BTreeMap<(String, String, String), StatusItem>>,
|
||
/// (scope, plugin, key) → JSON value.
|
||
contexts: Mutex<BTreeMap<ContextKey, String>>,
|
||
pub(super) closed_clients: Mutex<BTreeSet<String>>,
|
||
/// plugin → kinds subscribed at run time (kept across idle reclaim).
|
||
pub subscriptions: Mutex<super::event_paths::Subscriptions>,
|
||
/// Paths outside their project that plugins watch (kept across idle reclaim).
|
||
pub(super) external_watches: super::external_watch::ExternalWatches,
|
||
/// plugin → files it shows problems for, withdrawn when it stops.
|
||
diagnostics: Mutex<HashMap<String, BTreeSet<PathBuf>>>,
|
||
project_generations: Mutex<HashMap<PathBuf, u64>>,
|
||
pub secrets: RwLock<Arc<dyn SecretStore>>,
|
||
/// (plugin, `process:` capability) → the approved program.
|
||
pub pins: Arc<RwLock<HashMap<(String, String), ProgramPin>>>,
|
||
/// The grant ledger, shared with the [`super::PluginHost`]: `net:ask`
|
||
/// looks up and records "always allow" hosts right where a request runs.
|
||
pub ledger: Arc<Mutex<Ledger>>,
|
||
pub project_pins: ProjectPins,
|
||
pub event_editors: RwLock<HashMap<String, ide_plugin::EditorContext>>,
|
||
/// Validated UI declarations (settings schemas, declared views).
|
||
pub ui: Arc<ide_extension::UiStore>,
|
||
/// Extension points plugins declare and what was contributed to them.
|
||
pub registry: Arc<ide_extension::ExtensionRegistry>,
|
||
/// The UI language the windows report (BCP 47).
|
||
pub locale: RwLock<String>,
|
||
/// (plugin, window) → the log tabs its code opened there, oldest first:
|
||
/// what a tab may run when it starts.
|
||
log_tabs: Mutex<HashMap<(String, String), LogTabs>>,
|
||
}
|
||
|
||
/// A window's log tabs of one plugin: key → what it runs, oldest first.
|
||
type LogTabs = Vec<(String, ide_plugin::ServiceLog)>;
|
||
|
||
/// The settings key a `net:setting:` grant derives its host from.
|
||
fn setting_key(cap: &Capability) -> String {
|
||
match cap {
|
||
Capability::NetSetting { key, .. } => key.clone(),
|
||
_ => String::new(),
|
||
}
|
||
}
|
||
|
||
/// The host a setting value names: a bare host, or a URL the user pasted
|
||
/// (`https://jira.corp.example/`), checked with the manifest host rules — a `*`
|
||
/// is refused. `Ok(None)` when the value is empty (the setting is cleared).
|
||
pub(super) fn setting_host(value: &str) -> Result<Option<String>, String> {
|
||
let value = value.trim();
|
||
if value.is_empty() {
|
||
return Ok(None);
|
||
}
|
||
let host =
|
||
if value.contains("://") { ide_plugin::net_host(value)? } else { value.trim_end_matches('.').to_owned() };
|
||
ide_extension::manifest_host(&host)
|
||
.map(Some)
|
||
.map_err(|_| format!("{value:?} is not a host (example.com) or a URL (https://example.com/)"))
|
||
}
|
||
|
||
/// Log tabs a plugin's code may have open in one window; opening more forgets
|
||
/// the oldest (that tab can no longer start its program).
|
||
const MAX_LOG_TABS: usize = 32;
|
||
|
||
impl Shared {
|
||
pub fn new(
|
||
sink: Arc<dyn PluginEventSink>,
|
||
data_dir: PathBuf,
|
||
pins: Arc<RwLock<HashMap<(String, String), ProgramPin>>>,
|
||
ui: Arc<ide_extension::UiStore>,
|
||
ledger: Arc<Mutex<Ledger>>,
|
||
registry: Arc<ide_extension::ExtensionRegistry>,
|
||
) -> Arc<Self> {
|
||
Arc::new(Self {
|
||
ui,
|
||
registry,
|
||
locale: RwLock::new("en".into()),
|
||
sink: sink.clone(),
|
||
websockets: super::websocket::WebSocketHub::new(sink.clone()),
|
||
workspace: RwLock::new(None),
|
||
secrets: RwLock::new(Arc::new(FileSecretStore { dir: data_dir.clone() })),
|
||
pins,
|
||
ledger,
|
||
project_pins: Arc::default(),
|
||
event_editors: RwLock::default(),
|
||
data_dir,
|
||
tasks: Mutex::default(),
|
||
next_task: AtomicU64::new(1),
|
||
processes: Mutex::default(),
|
||
next_process: AtomicU64::new(1),
|
||
connections: super::ipc::Connections::default(),
|
||
http_streams: super::http_stream::HttpStreams::default(),
|
||
loopbacks: RwLock::new(Arc::new(super::auth::Loopbacks::default())),
|
||
process_shutdowns: Mutex::default(),
|
||
prompts: Mutex::default(),
|
||
prompts_blocked: Mutex::default(),
|
||
net_prompts: Mutex::default(),
|
||
net_hosts_session_only: AtomicBool::new(false),
|
||
session_net_hosts: Mutex::default(),
|
||
session_setting_hosts: Mutex::default(),
|
||
next_prompt: AtomicU64::new(1),
|
||
run_configurations: Arc::default(),
|
||
run_targets: Mutex::default(),
|
||
operations: Mutex::default(),
|
||
content_tabs: Mutex::default(),
|
||
content_requests: Mutex::default(),
|
||
|
||
status: Mutex::default(),
|
||
output: Mutex::default(),
|
||
decorations: Mutex::default(),
|
||
console_rules: Mutex::default(),
|
||
window_status: Mutex::default(),
|
||
setting_snapshots: Mutex::default(),
|
||
contexts: Mutex::default(),
|
||
closed_clients: Mutex::default(),
|
||
subscriptions: Mutex::default(),
|
||
external_watches: Default::default(),
|
||
diagnostics: Mutex::default(),
|
||
project_generations: Mutex::default(),
|
||
log_tabs: Mutex::default(),
|
||
})
|
||
}
|
||
|
||
pub(super) fn project_generation(&self, root: &Path) -> Result<u64, HostError> {
|
||
let workspace = self.workspace().ok_or(HostError::Cancelled)?;
|
||
if !workspace.projects().iter().any(|r| r == root) {
|
||
return Err(HostError::NotFound("project is not open".into()));
|
||
}
|
||
let mut gens = self.project_generations.lock();
|
||
Ok(*gens.entry(root.to_path_buf()).or_insert(1))
|
||
}
|
||
|
||
pub(super) fn bump_project_generation(&self, root: &Path) -> u64 {
|
||
let mut gens = self.project_generations.lock();
|
||
let cur = gens.entry(root.to_path_buf()).or_insert(1);
|
||
*cur = cur.saturating_add(1);
|
||
*cur
|
||
}
|
||
|
||
/// Moves a closed project's generation on rather than forgetting it, so a handle
|
||
/// taken before the close stays stale if the project is opened again.
|
||
pub(super) fn close_project_generation(&self, root: &Path) {
|
||
self.bump_project_generation(root);
|
||
}
|
||
|
||
pub(super) fn workspace(&self) -> Option<Arc<dyn PluginWorkspace>> {
|
||
self.workspace.read().clone()
|
||
}
|
||
|
||
pub fn project_of(&self, window: &str) -> Option<PathBuf> {
|
||
self.workspace()?.project_of(window)
|
||
}
|
||
|
||
/// Shows `plugin`'s problems of `path`, remembering the file until they are
|
||
/// cleared (by the plugin, or when it stops).
|
||
fn publish_diagnostics(
|
||
&self,
|
||
plugin: &str,
|
||
path: &Path,
|
||
items: Vec<ide_core::Diagnostic>,
|
||
) -> Result<(), HostError> {
|
||
let workspace = self.workspace().ok_or_else(|| HostError::NotFound("no project is open".into()))?;
|
||
let empty = items.is_empty();
|
||
workspace.publish_diagnostics(plugin, path, items)?;
|
||
let mut published = self.diagnostics.lock();
|
||
if empty {
|
||
if let Some(paths) = published.get_mut(plugin) {
|
||
paths.remove(path);
|
||
if paths.is_empty() {
|
||
published.remove(plugin);
|
||
}
|
||
}
|
||
} else {
|
||
published.entry(plugin.to_owned()).or_default().insert(path.to_path_buf());
|
||
}
|
||
Ok(())
|
||
}
|
||
|
||
pub fn status_items(&self) -> Vec<(String, String, StatusItem)> {
|
||
self.status.lock().iter().map(|((p, id), item)| (p.clone(), id.clone(), item.clone())).collect()
|
||
}
|
||
|
||
pub fn changed_settings(self: &Arc<Self>, pkg: &Package, client: Option<&str>) -> Vec<String> {
|
||
let root = client.and_then(|c| self.project_of(c));
|
||
let api = PluginApi { pkg: pkg.clone(), shared: self.clone() };
|
||
let mut current = BTreeMap::new();
|
||
for item in self.ui.items(super::ui_points::SETTINGS, Some(pkg.id())) {
|
||
if let Some(properties) = item.decl.get("properties").and_then(|p| p.as_array()) {
|
||
for property in properties {
|
||
if let Some(key) = property.get("key").and_then(|k| k.as_str())
|
||
&& let Some(value) = api.setting(root.as_deref(), key)
|
||
{
|
||
current.insert(key.into(), value);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
let previous =
|
||
self.setting_snapshots.lock().insert((client.map(str::to_owned), pkg.id().into()), current.clone());
|
||
match previous {
|
||
// First contact since the plugin loaded (fresh install, update,
|
||
// re-enable): hand over every current key, so the synthetic
|
||
// post-install `settings-changed` wakes the plugin instead of being
|
||
// dropped for having no diff yet.
|
||
None => current.into_keys().collect(),
|
||
Some(old) => current.into_iter().filter(|(k, v)| old.get(k) != Some(v)).map(|(k, _)| k).collect(),
|
||
}
|
||
}
|
||
|
||
pub fn status_snapshot(&self, client: &str) -> Vec<super::PluginStatusItem> {
|
||
let mut items: Vec<_> = self
|
||
.status_items()
|
||
.into_iter()
|
||
.map(|(plugin_id, id, item)| super::PluginStatusItem { plugin_id, id, item, window: false })
|
||
.collect();
|
||
items.extend(self.window_status.lock().iter().filter(|((w, ..), _)| w == client).map(
|
||
|((_, plugin_id, id), item)| super::PluginStatusItem {
|
||
plugin_id: plugin_id.clone(),
|
||
id: id.clone(),
|
||
item: item.clone(),
|
||
window: true,
|
||
},
|
||
));
|
||
items
|
||
}
|
||
|
||
pub fn context_keys(&self, client: Option<&str>) -> Vec<PluginContextKey> {
|
||
let contexts = self.contexts.lock();
|
||
let mut keys: BTreeMap<(&str, &str), &str> = BTreeMap::new();
|
||
for ((_, plugin, key), value) in contexts.iter().filter(|((s, ..), _)| s.is_none()) {
|
||
keys.insert((plugin, key), value);
|
||
}
|
||
if let Some(client) = client {
|
||
for ((_, plugin, key), value) in contexts.iter().filter(|((s, ..), _)| s.as_deref() == Some(client)) {
|
||
keys.insert((plugin, key), value);
|
||
}
|
||
}
|
||
keys.into_iter()
|
||
.map(|((p, k), v)| PluginContextKey { plugin_id: p.to_owned(), key: k.to_owned(), value: v.to_owned() })
|
||
.collect()
|
||
}
|
||
|
||
/// Cancels prompts and drops context keys scoped to window `client` (it closed).
|
||
/// Refuses further writes for it (late commands) until [`Self::reopen_client`].
|
||
pub fn forget_client(&self, client: &str) {
|
||
let mut closed = self.closed_clients.lock();
|
||
closed.insert(client.to_owned());
|
||
self.prompts.lock().retain(|_, entry| entry.client != client);
|
||
drop(closed);
|
||
self.contexts.lock().retain(|(scope, ..), _| scope.as_deref() != Some(client));
|
||
self.loopbacks.read().forget_window(client);
|
||
self.window_status.lock().retain(|(window, ..), _| window != client);
|
||
self.operations.lock().retain(|_, op| op.client != client);
|
||
self.content_tabs.lock().retain(|(_, window, _), _| window != client);
|
||
self.content_requests.lock().retain(|(_, window, _), _| window != client);
|
||
self.event_editors.write().remove(client);
|
||
self.setting_snapshots.lock().retain(|(window, _), _| window.as_deref() != Some(client));
|
||
let mine: Vec<_> = {
|
||
let mut processes = self.processes.lock();
|
||
let keys: Vec<_> = processes
|
||
.iter()
|
||
.filter(|(_, p)| p.client.as_deref() == Some(client))
|
||
.map(|(key, _)| key.clone())
|
||
.collect();
|
||
keys.iter().filter_map(|key| processes.remove(key)).collect()
|
||
};
|
||
if !mine.is_empty() {
|
||
// Window destruction runs on the UI thread; tree shutdown may wait.
|
||
let worker = std::thread::spawn(move || {
|
||
for proc in mine {
|
||
proc.kill();
|
||
}
|
||
});
|
||
let mut workers = self.process_shutdowns.lock();
|
||
workers.retain(|worker| !worker.is_finished());
|
||
workers.push(worker);
|
||
}
|
||
}
|
||
|
||
pub fn finish_process_shutdowns(&self) {
|
||
let workers = std::mem::take(&mut *self.process_shutdowns.lock());
|
||
for worker in workers {
|
||
let _ = worker.join();
|
||
}
|
||
}
|
||
|
||
/// A window labelled `client` is being created (labels are reused after a close).
|
||
pub fn reopen_client(&self, client: &str) {
|
||
self.closed_clients.lock().remove(client);
|
||
}
|
||
|
||
/// What the log tab `key` that `plugin`'s code opened in `window` runs.
|
||
pub fn log_tab(&self, plugin: &str, window: &str, key: &str) -> Option<ide_plugin::ServiceLog> {
|
||
let tabs = self.log_tabs.lock();
|
||
let tabs = tabs.get(&(plugin.to_owned(), window.to_owned()))?;
|
||
tabs.iter().find(|(k, _)| k == key).map(|(_, log)| log.clone())
|
||
}
|
||
|
||
/// Forgets the log tabs of a closed window, or of a stopped plugin.
|
||
pub fn forget_log_tabs(&self, plugin: Option<&str>, window: Option<&str>) {
|
||
self.log_tabs
|
||
.lock()
|
||
.retain(|(p, w), _| plugin.is_some_and(|plugin| plugin != p) || window.is_some_and(|window| window != w));
|
||
}
|
||
|
||
pub fn client_closed(&self, client: &str) -> bool {
|
||
self.closed_clients.lock().contains(client)
|
||
}
|
||
|
||
/// Withdraws a plugin's run-time state (it stopped or failed). Its
|
||
/// subscriptions go only with `subscriptions`, as they survive restarts.
|
||
pub fn clear_plugin(&self, plugin: &str, subscriptions: bool) {
|
||
self.withdraw_run_targets(Some(plugin), None);
|
||
self.run_configurations.write().retain(|(p, _), _| p != plugin);
|
||
self.operations.lock().retain(|_, op| op.plugin != plugin);
|
||
self.content_tabs.lock().retain(|(p, _, _), _| p != plugin);
|
||
self.content_requests.lock().retain(|(p, _, _), _| p != plugin);
|
||
|
||
self.kill_processes(plugin);
|
||
self.connections.clear(plugin);
|
||
self.http_streams.clear(plugin);
|
||
self.websockets.disconnect_plugin(plugin);
|
||
self.setting_snapshots.lock().retain(|(_, p), _| p != plugin);
|
||
let channels: Vec<_> = {
|
||
let mut output = self.output.lock();
|
||
let keys: Vec<_> = output.keys().filter(|(p, _)| p == plugin).cloned().collect();
|
||
for key in &keys {
|
||
output.remove(key);
|
||
}
|
||
keys
|
||
};
|
||
for (_, channel) in channels {
|
||
self.sink.emit(None, PluginEvent::OutputChanged { plugin_id: plugin.into(), channel, text: None });
|
||
}
|
||
|
||
self.loopbacks.read().clear(plugin);
|
||
let removed: Vec<String> = {
|
||
let mut status = self.status.lock();
|
||
let ids: Vec<String> = status.keys().filter(|(p, _)| p == plugin).map(|(_, id)| id.clone()).collect();
|
||
for id in &ids {
|
||
status.remove(&(plugin.to_owned(), id.clone()));
|
||
}
|
||
ids
|
||
};
|
||
for id in removed {
|
||
self.sink.emit(None, PluginEvent::StatusItemChanged { plugin_id: plugin.to_owned(), id, item: None });
|
||
}
|
||
let removed: Vec<_> = {
|
||
let mut items = self.window_status.lock();
|
||
let keys: Vec<_> = items.keys().filter(|(_, p, _)| p == plugin).cloned().collect();
|
||
for key in &keys {
|
||
items.remove(key);
|
||
}
|
||
keys
|
||
};
|
||
for (window, _, id) in removed {
|
||
self.sink
|
||
.emit(Some(&window), PluginEvent::WindowStatusItemChanged { plugin_id: plugin.into(), id, item: None });
|
||
}
|
||
if self.decorations.lock().remove(plugin).is_some() {
|
||
self.sink.emit(None, PluginEvent::DecorationsChanged { plugin_id: plugin.into(), sets: Vec::new() });
|
||
}
|
||
if self.console_rules.lock().remove(plugin).is_some() {
|
||
self.sink.emit(None, PluginEvent::ConsoleRulesChanged { plugin_id: plugin.into(), rules: Vec::new() });
|
||
}
|
||
self.withdraw_prompts(plugin);
|
||
self.net_prompts.lock().remove(plugin);
|
||
self.session_net_hosts.lock().remove(plugin);
|
||
self.session_setting_hosts.lock().remove(plugin);
|
||
let removed: Vec<(Option<String>, String)> = {
|
||
let mut contexts = self.contexts.lock();
|
||
let keys: Vec<_> = contexts.keys().filter(|(_, p, _)| p == plugin).cloned().collect();
|
||
for key in &keys {
|
||
contexts.remove(key);
|
||
}
|
||
keys.into_iter().map(|(scope, _, key)| (scope, key)).collect()
|
||
};
|
||
for (scope, key) in removed {
|
||
self.sink
|
||
.emit(scope.as_deref(), PluginEvent::ContextChanged { plugin_id: plugin.to_owned(), key, value: None });
|
||
}
|
||
let tasks: Vec<u64> = self.tasks.lock().iter().filter(|(_, t)| t.plugin == plugin).map(|(id, _)| *id).collect();
|
||
for id in tasks {
|
||
self.end_task(id);
|
||
}
|
||
// Its problems go with it (an idle reclaim keeps them: it isn't cleared then).
|
||
let paths = self.diagnostics.lock().remove(plugin).unwrap_or_default();
|
||
if let Some(workspace) = self.workspace() {
|
||
for path in paths {
|
||
let _ = workspace.publish_diagnostics(plugin, &path, Vec::new());
|
||
}
|
||
}
|
||
if subscriptions {
|
||
self.external_watches.remove_plugin(plugin);
|
||
}
|
||
if subscriptions && self.subscriptions.lock().remove(plugin) {
|
||
self.sink.emit(None, PluginEvent::SubscriptionsChanged);
|
||
}
|
||
}
|
||
|
||
/// Ends every process `plugin` started: its handles go with its instance
|
||
/// (it stopped, was disabled, or was reclaimed while idle).
|
||
pub fn kill_processes(&self, plugin: &str) {
|
||
let mine: Vec<Arc<Proc>> = {
|
||
let mut processes = self.processes.lock();
|
||
let keys: Vec<(String, u64)> = processes.keys().filter(|(p, _)| p == plugin).cloned().collect();
|
||
keys.iter().filter_map(|k| processes.remove(k)).collect()
|
||
};
|
||
for proc in mine {
|
||
proc.kill();
|
||
}
|
||
}
|
||
|
||
/// The user cancelled a task in the UI.
|
||
pub fn cancel_task(&self, id: u64) -> bool {
|
||
let mut tasks = self.tasks.lock();
|
||
let Some(t) = tasks.get_mut(&id) else { return false };
|
||
t.cancelled = true;
|
||
t.cancel.cancel();
|
||
true
|
||
}
|
||
|
||
/// Closes the prompts `plugin` waits on: the calls waiting for the user
|
||
/// wake up cancelled (dropping the senders).
|
||
fn withdraw_prompts(&self, plugin: &str) {
|
||
let ids: Vec<u64> = {
|
||
let mut prompts = self.prompts.lock();
|
||
let ids: Vec<u64> = prompts.iter().filter(|(_, p)| p.plugin == plugin).map(|(id, _)| *id).collect();
|
||
for id in &ids {
|
||
prompts.remove(id);
|
||
}
|
||
ids
|
||
};
|
||
for id in ids {
|
||
self.sink.emit(None, PluginEvent::PromptClosed { prompt_id: id.to_string() });
|
||
}
|
||
}
|
||
|
||
/// `plugin` is about to be stopped: withdraws its prompts, and its queued
|
||
/// calls can't open new ones (they'd keep the stop waiting) until
|
||
/// [`allow_prompts`](Self::allow_prompts).
|
||
pub fn block_prompts(&self, plugin: &str) {
|
||
self.prompts_blocked.lock().insert(plugin.to_owned());
|
||
self.withdraw_prompts(plugin);
|
||
}
|
||
|
||
pub fn allow_prompts(&self, plugin: &str) {
|
||
self.prompts_blocked.lock().remove(plugin);
|
||
}
|
||
|
||
/// A window other than `client` where `plugin` waits for the user's answer (a prompt or a
|
||
/// Commit request). The plugin runs one call at a time, so anything else asked of it waits
|
||
/// behind that answer.
|
||
pub fn waiting_elsewhere(&self, plugin: &str, client: &str) -> Option<String> {
|
||
self.prompts.lock().values().find(|p| p.plugin == plugin && p.client != client).map(|p| p.client.clone())
|
||
}
|
||
|
||
/// The user answered (`Some`) or dismissed (`None`) a prompt.
|
||
pub fn answer_prompt(&self, id: u64, answer: Option<Vec<String>>) -> bool {
|
||
let mut prompts = self.prompts.lock();
|
||
if !matches!(prompts.get(&id), Some(PromptEntry { answer: Waiter::Prompt(_), .. })) {
|
||
return false;
|
||
}
|
||
let Some(PromptEntry { answer: Waiter::Prompt(tx), .. }) = prompts.remove(&id) else { return false };
|
||
tx.send(answer).is_ok()
|
||
}
|
||
|
||
/// A window answered a [`CommitRequest`].
|
||
pub fn answer_command(&self, client: &str, id: u64, answer: super::CommandAnswer) -> bool {
|
||
let mut prompts = self.prompts.lock();
|
||
if !matches!(prompts.get(&id), Some(PromptEntry { client: window, answer: Waiter::Command(_), .. }) if window == client)
|
||
{
|
||
return false;
|
||
}
|
||
let Some(PromptEntry { answer: Waiter::Command(tx), .. }) = prompts.remove(&id) else { return false };
|
||
tx.try_send(answer).is_ok()
|
||
}
|
||
|
||
/// The window a [`PluginEvent::ClipboardRequested`] went to answered it.
|
||
pub fn answer_clipboard(&self, client: &str, id: u64, answer: super::ClipboardAnswer) -> bool {
|
||
let mut prompts = self.prompts.lock();
|
||
if !matches!(prompts.get(&id), Some(PromptEntry { client: window, answer: Waiter::Clipboard(_), .. }) if window == client)
|
||
{
|
||
return false;
|
||
}
|
||
let Some(PromptEntry { answer: Waiter::Clipboard(tx), .. }) = prompts.remove(&id) else { return false };
|
||
tx.try_send(answer).is_ok()
|
||
}
|
||
|
||
pub fn answer_commit(&self, id: u64, answer: CommitAnswer) -> bool {
|
||
let mut prompts = self.prompts.lock();
|
||
if !matches!(prompts.get(&id), Some(PromptEntry { answer: Waiter::Commit(_), .. })) {
|
||
return false;
|
||
}
|
||
let Some(PromptEntry { answer: Waiter::Commit(tx), .. }) = prompts.remove(&id) else { return false };
|
||
tx.send(answer).is_ok()
|
||
}
|
||
|
||
fn end_task(&self, id: u64) {
|
||
if let Some(t) = self.tasks.lock().remove(&id) {
|
||
self.sink.emit(None, PluginEvent::TaskEnded { plugin_id: t.plugin, task_id: id.to_string() });
|
||
}
|
||
}
|
||
|
||
/// The plugin's stored values: one file per key, so a write only
|
||
/// rewrites its own value.
|
||
pub fn storage_dir(&self, plugin: &str) -> PathBuf {
|
||
self.data_dir.join(plugin).join("storage")
|
||
}
|
||
}
|
||
|
||
fn storage_io(e: std::io::Error) -> HostError {
|
||
HostError::Failed(format!("storage: {e}"))
|
||
}
|
||
|
||
fn replace_entry(path: &Path, bytes: &[u8]) -> std::io::Result<()> {
|
||
use std::io::Write;
|
||
let mut tmp =
|
||
tempfile::Builder::new().prefix(".entry-").suffix(".tmp").tempfile_in(path.parent().expect("has parent"))?;
|
||
tmp.write_all(bytes)?;
|
||
tmp.persist(path).map_err(|e| e.error)?;
|
||
Ok(())
|
||
}
|
||
|
||
/// A key's file name: its hash, since keys may hold any character.
|
||
fn entry_name(key: &str) -> String {
|
||
blake3::hash(key.as_bytes()).to_hex().to_string()
|
||
}
|
||
|
||
/// A stored entry: the key's length (2 bytes, little-endian), the key, then the value.
|
||
fn decode_entry(bytes: &[u8]) -> Option<(&str, &str)> {
|
||
let len = usize::from(u16::from_le_bytes(bytes.get(..2)?.try_into().ok()?));
|
||
let key = std::str::from_utf8(bytes.get(2..2 + len)?).ok()?;
|
||
let value = std::str::from_utf8(&bytes[2 + len..]).ok()?;
|
||
Some((key, value))
|
||
}
|
||
|
||
fn write_entry(dir: &Path, key: &str, value: &str) -> Result<(), HostError> {
|
||
std::fs::create_dir_all(dir).map_err(storage_io)?;
|
||
let file = dir.join(entry_name(key));
|
||
let len = u16::try_from(key.len()).expect("keys are at most MAX_KEY bytes");
|
||
let mut bytes = Vec::with_capacity(2 + key.len() + value.len());
|
||
bytes.extend_from_slice(&len.to_le_bytes());
|
||
bytes.extend_from_slice(key.as_bytes());
|
||
bytes.extend_from_slice(value.as_bytes());
|
||
replace_entry(&file, &bytes).map_err(storage_io)
|
||
}
|
||
|
||
/// The stored entries' files and the bytes of their keys and values.
|
||
fn entries(dir: &Path) -> Result<impl Iterator<Item = (PathBuf, usize)>, HostError> {
|
||
let list = match std::fs::read_dir(dir) {
|
||
Ok(list) => list,
|
||
Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(Vec::new().into_iter()),
|
||
Err(e) => return Err(storage_io(e)),
|
||
};
|
||
let mut out = Vec::new();
|
||
for entry in list {
|
||
let entry = entry.map_err(storage_io)?;
|
||
let path = entry.path();
|
||
if path.extension().is_some() {
|
||
continue; // a write that didn't finish
|
||
}
|
||
let size = entry.metadata().map_err(storage_io)?.len();
|
||
out.push((path, usize::try_from(size).unwrap_or(usize::MAX).saturating_sub(2)));
|
||
}
|
||
Ok(out.into_iter())
|
||
}
|
||
|
||
pub(super) struct PluginApi {
|
||
pub pkg: Package,
|
||
pub shared: Arc<Shared>,
|
||
}
|
||
|
||
impl PluginApi {
|
||
fn id(&self) -> &str {
|
||
self.pkg.id()
|
||
}
|
||
|
||
/// The storage directory, after moving a `storage.json` written by an
|
||
/// older version into it.
|
||
fn storage(&self) -> Result<PathBuf, HostError> {
|
||
let dir = self.shared.storage_dir(self.id());
|
||
std::fs::create_dir_all(&self.shared.data_dir).map_err(storage_io)?;
|
||
ide_vfs::create_private_dir(dir.parent().expect("has parent")).map_err(failed)?;
|
||
ide_vfs::create_private_dir(&dir).map_err(failed)?;
|
||
let legacy = self.shared.data_dir.join(self.id()).join("storage.json");
|
||
match std::fs::read(&legacy) {
|
||
Ok(bytes) => {
|
||
let map: BTreeMap<String, String> =
|
||
serde_json::from_slice(&bytes).map_err(|e| HostError::Failed(format!("storage: {e}")))?;
|
||
for (key, value) in &map {
|
||
write_entry(&dir, key, value)?;
|
||
}
|
||
std::fs::remove_file(&legacy).map_err(storage_io)?;
|
||
}
|
||
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {}
|
||
Err(e) => return Err(storage_io(e)),
|
||
}
|
||
Ok(dir)
|
||
}
|
||
|
||
/// The program approved for the (already checked) `process:` grant,
|
||
/// verified right before it starts.
|
||
#[cfg(test)]
|
||
fn approved_program(&self, program: &str) -> Result<PathBuf, HostError> {
|
||
self.approved_program_in_project(program, None)
|
||
}
|
||
|
||
/// `request` with its secrets filled in and a redirect policy that checks
|
||
/// every hop against its grants; the caller sets the timeout.
|
||
fn http_request(&self, request: HttpRequest) -> Result<reqwest::RequestBuilder, HostError> {
|
||
let secrets = self.shared.secrets.read().clone();
|
||
let id = self.id().to_owned();
|
||
let mut headers = reqwest::header::HeaderMap::new();
|
||
for (name, value) in &request.headers {
|
||
let value = fill_secrets(value, |n| secrets.get(&id, n))?;
|
||
let name = reqwest::header::HeaderName::from_bytes(name.as_bytes())
|
||
.map_err(|e| HostError::Invalid(format!("header {name:?}: {e}")))?;
|
||
let mut value = reqwest::header::HeaderValue::from_str(&value)
|
||
.map_err(|e| HostError::Invalid(format!("header {name}: {e}")))?;
|
||
value.set_sensitive(true);
|
||
headers.append(name, value);
|
||
}
|
||
let method = reqwest::Method::from_bytes(request.method.to_ascii_uppercase().as_bytes())
|
||
.map_err(|_| HostError::Invalid(format!("method {:?}", request.method)))?;
|
||
let grants = request.grants.clone();
|
||
let policy = reqwest::redirect::Policy::custom(move |attempt| {
|
||
if attempt.previous().len() >= 10 {
|
||
return attempt.error("too many redirects");
|
||
}
|
||
let wanted = attempt.previous().last().map_or_else(
|
||
|| ide_plugin::net_capability(attempt.url().as_str()),
|
||
|from| redirect_capability(from, attempt.url()),
|
||
);
|
||
match wanted {
|
||
Ok(wanted) if grants.iter().any(|g| g.covers(&wanted)) => attempt.follow(),
|
||
Ok(wanted) => attempt.error(RedirectDenied(wanted.to_string())),
|
||
Err(error) => attempt.error(error),
|
||
}
|
||
});
|
||
let client = ide_http::client::build_with_redirect(&ide_core::proxy_settings::proxy(), policy)
|
||
.map_err(|e| HostError::Failed(e.message))?;
|
||
let builder = client.request(method, &request.url).headers(headers);
|
||
Ok(match request.body {
|
||
Some(body) => builder.body(body),
|
||
None => builder,
|
||
})
|
||
}
|
||
|
||
pub(super) fn approved_program_in_project(&self, program: &str, root: Option<&Path>) -> Result<PathBuf, HostError> {
|
||
if let Some(pin) = root.and_then(|root| {
|
||
self.shared.project_pins.read().get(&(self.id().into(), format!("process:{program}"), root.into())).cloned()
|
||
}) {
|
||
return pin.verify().map(Path::to_path_buf).map_err(|e| HostError::Denied(e.to_string()));
|
||
}
|
||
let cap = format!("process:{program}");
|
||
// Granted without a pinned file: the program wasn't found when it was
|
||
// allowed, and installing it later approves nothing by itself.
|
||
let pin = self.shared.pins.read().get(&(self.id().to_owned(), cap.clone())).cloned().ok_or_else(|| {
|
||
HostError::Failed(format!(
|
||
"{cap}: `{program}` was not found on PATH when this permission was allowed. \
|
||
Restarting doesn't approve a program installed later: once `{program}` is on PATH, \
|
||
clear {cap} in Settings › Plugins and select it again"
|
||
))
|
||
})?;
|
||
pin.verify().map(Path::to_path_buf).map_err(|e| HostError::Denied(format!("{cap}: {e}")))
|
||
}
|
||
}
|
||
|
||
impl PluginApi {
|
||
/// Runs an approved program until it exits or `cancel` fires, passing
|
||
/// each output line to `on_line`; returns its exit code.
|
||
pub(super) fn stream_process(
|
||
&self,
|
||
program: &str,
|
||
args: Vec<String>,
|
||
cwd: Option<&Path>,
|
||
cancel: &CancellationToken,
|
||
on_line: &dyn Fn(super::PluginOutputLine),
|
||
) -> Result<Option<i32>, HostError> {
|
||
let binary = self.approved_program_in_project(program, cwd)?;
|
||
let cwd = cwd.map(Path::to_path_buf).unwrap_or_else(|| self.shared.data_dir.join(self.id()));
|
||
let _ = std::fs::create_dir_all(&cwd);
|
||
let mut cmd = std::process::Command::new(&binary);
|
||
ide_core::process::prepare(ide_core::process::own_group(&mut cmd));
|
||
let mut child = cmd
|
||
.args(&args)
|
||
.current_dir(&cwd)
|
||
.stdin(std::process::Stdio::null())
|
||
.stdout(std::process::Stdio::piped())
|
||
.stderr(std::process::Stdio::piped())
|
||
.spawn()
|
||
.map_err(|e| HostError::Failed(format!("{}: {e}", binary.display())))?;
|
||
// Bounded: a program printing faster than the lines are sent waits on its pipe.
|
||
let (tx, rx) = std::sync::mpsc::sync_channel(1024);
|
||
let read = |pipe: Box<dyn std::io::Read + Send>, stderr: bool| {
|
||
let tx = tx.clone();
|
||
std::thread::spawn(move || {
|
||
read_lines(pipe, MAX_OUTPUT_LINE, &mut |text| {
|
||
tx.send(super::PluginOutputLine { stderr, text }).is_ok()
|
||
});
|
||
});
|
||
};
|
||
if let Some(out) = child.stdout.take() {
|
||
read(Box::new(out), false);
|
||
}
|
||
if let Some(err) = child.stderr.take() {
|
||
read(Box::new(err), true);
|
||
}
|
||
drop(tx);
|
||
// Revoking the grant removes the pin: stop then.
|
||
let cap = format!("process:{program}");
|
||
let pin = (self.id().to_owned(), cap.clone());
|
||
let stop = |child: &mut std::process::Child| -> Option<HostError> {
|
||
if cancel.is_cancelled() {
|
||
kill_group(child);
|
||
return Some(HostError::Cancelled);
|
||
}
|
||
if !self.shared.pins.read().contains_key(&pin) {
|
||
kill_group(child);
|
||
return Some(HostError::Denied(cap.clone()));
|
||
}
|
||
None
|
||
};
|
||
loop {
|
||
if let Some(e) = stop(&mut child) {
|
||
return Err(e);
|
||
}
|
||
match rx.recv_timeout(Duration::from_millis(50)) {
|
||
Ok(line) => on_line(line),
|
||
// Both pipes closed: the program is done (or detached them).
|
||
Err(std::sync::mpsc::RecvTimeoutError::Disconnected) => break,
|
||
Err(std::sync::mpsc::RecvTimeoutError::Timeout) => {}
|
||
}
|
||
}
|
||
loop {
|
||
if let Some(status) = child.try_wait().map_err(failed)? {
|
||
return Ok(status.code());
|
||
}
|
||
if let Some(e) = stop(&mut child) {
|
||
return Err(e);
|
||
}
|
||
std::thread::sleep(Duration::from_millis(50));
|
||
}
|
||
}
|
||
}
|
||
|
||
/// Longest output line a followed program sends; the rest of the line is dropped.
|
||
const MAX_OUTPUT_LINE: usize = 16 << 10;
|
||
|
||
/// Passes `pipe`'s lines (without `\r\n`, at most `max` bytes each) to `line`
|
||
/// until it returns false or the pipe closes, holding at most one line.
|
||
fn read_lines(pipe: Box<dyn std::io::Read + Send>, max: usize, line: &mut dyn FnMut(String) -> bool) {
|
||
use std::io::{BufRead, Read};
|
||
let mut reader = std::io::BufReader::new(pipe);
|
||
let mut buf = Vec::new();
|
||
loop {
|
||
buf.clear();
|
||
match (&mut reader).take(max as u64).read_until(b'\n', &mut buf) {
|
||
Ok(0) | Err(_) => return,
|
||
Ok(_) => {}
|
||
}
|
||
if buf.last() != Some(&b'\n') && buf.len() == max {
|
||
// Too long: skip to the end of the line.
|
||
let mut rest = Vec::new();
|
||
loop {
|
||
rest.clear();
|
||
match (&mut reader).take(max as u64).read_until(b'\n', &mut rest) {
|
||
Ok(0) | Err(_) => break,
|
||
Ok(_) if rest.last() == Some(&b'\n') => break,
|
||
Ok(_) => {}
|
||
}
|
||
}
|
||
}
|
||
let end = buf.strip_suffix(b"\n").unwrap_or(&buf);
|
||
let end = end.strip_suffix(b"\r").unwrap_or(end);
|
||
if !line(String::from_utf8_lossy(end).into_owned()) {
|
||
return;
|
||
}
|
||
}
|
||
}
|
||
|
||
/// Kills a program spawned in its own group and what it started. A kill that
|
||
/// lands while the program forks can miss the new process, so the group is
|
||
/// signalled again before the program is reaped (until then its group id
|
||
/// can't be reused).
|
||
fn kill_group(child: &mut std::process::Child) {
|
||
ide_core::process::kill_tree_of(child.id());
|
||
std::thread::sleep(Duration::from_millis(50));
|
||
ide_core::process::kill_tree(child);
|
||
}
|
||
|
||
impl PluginApi {
|
||
/// Sends `request` to `client`'s window and waits for its answer, until
|
||
/// `cancel` fires, the plugin stops or `timeout` passes.
|
||
fn ask_commit(
|
||
&self,
|
||
client: &str,
|
||
request: CommitRequest,
|
||
cancel: &CancellationToken,
|
||
timeout: Duration,
|
||
) -> Result<CommitAnswer, HostError> {
|
||
let id = self.shared.next_prompt.fetch_add(1, Ordering::Relaxed);
|
||
let (answer, answers) = std::sync::mpsc::sync_channel(1);
|
||
{
|
||
let closed = self.shared.closed_clients.lock();
|
||
if closed.contains(client) {
|
||
return Err(HostError::Cancelled);
|
||
}
|
||
let blocked = self.shared.prompts_blocked.lock();
|
||
if blocked.contains(self.id()) {
|
||
return Err(HostError::Cancelled);
|
||
}
|
||
self.shared.prompts.lock().insert(
|
||
id,
|
||
PromptEntry { plugin: self.id().to_owned(), client: client.to_owned(), answer: Waiter::Commit(answer) },
|
||
);
|
||
}
|
||
let request_id = id.to_string();
|
||
self.shared.sink.emit(
|
||
Some(client),
|
||
PluginEvent::CommitRequested { plugin_id: self.id().to_owned(), request_id: request_id.clone(), request },
|
||
);
|
||
let deadline = Instant::now() + timeout;
|
||
loop {
|
||
match answers.recv_timeout(Duration::from_millis(100)) {
|
||
Ok(answer) => return Ok(answer),
|
||
Err(std::sync::mpsc::RecvTimeoutError::Timeout)
|
||
if !cancel.is_cancelled() && Instant::now() < deadline => {}
|
||
Err(e) => {
|
||
self.shared.prompts.lock().remove(&id);
|
||
self.shared.sink.emit(Some(client), PluginEvent::PromptClosed { prompt_id: request_id });
|
||
return Err(match e {
|
||
std::sync::mpsc::RecvTimeoutError::Timeout if !cancel.is_cancelled() => {
|
||
HostError::Failed("the window did not answer".into())
|
||
}
|
||
_ => HostError::Cancelled,
|
||
});
|
||
}
|
||
}
|
||
}
|
||
}
|
||
/// Where `cmd` runs: the project root it names, else the plugin's data
|
||
/// directory (created as needed).
|
||
fn spawn_dir(&self, cwd: Option<&Path>) -> PathBuf {
|
||
let dir = cwd.map(Path::to_path_buf).unwrap_or_else(|| self.shared.data_dir.join(self.id()));
|
||
let _ = std::fs::create_dir_all(&dir);
|
||
dir
|
||
}
|
||
|
||
/// The process handle `id`, if this plugin started it and it is still open.
|
||
fn process(&self, id: u64) -> Result<Arc<Proc>, HostError> {
|
||
self.shared
|
||
.processes
|
||
.lock()
|
||
.get(&(self.id().to_owned(), id))
|
||
.cloned()
|
||
.ok_or_else(|| HostError::NotFound(format!("no process {id}: it was closed, or belongs to another plugin")))
|
||
}
|
||
}
|
||
|
||
/// The changes `selection` commits, diffed repository by repository.
|
||
pub(super) fn commit_draft(
|
||
workspace: &dyn PluginWorkspace,
|
||
selection: CommitSelection,
|
||
cancel: &CancellationToken,
|
||
) -> Result<CommitDraft, HostError> {
|
||
let mut changes = Vec::new();
|
||
let mut budget = MAX_COMMIT_DIFF;
|
||
for repo in &selection.repositories {
|
||
if cancel.is_cancelled() {
|
||
return Err(HostError::Cancelled);
|
||
}
|
||
if !workspace.git_enabled(&repo.root.0) {
|
||
return Err(HostError::Failed("Git integration is disabled".into()));
|
||
}
|
||
let Some(r) = ide_vcs::Repo::discover(&repo.root.0).map_err(failed)? else {
|
||
return Err(HostError::NotFound(format!("{} is not in a git repository", repo.root.0.display())));
|
||
};
|
||
// Both callers have checked that the window's project is trusted.
|
||
r.set_execution_allowed(true);
|
||
r.set_git_executable(workspace.git_executable(&repo.root.0).as_deref());
|
||
let backend = &r as &dyn ide_vcs::Backend;
|
||
let pending = backend
|
||
.pending_changes(&repo.options, budget, &|path| Ok(workspace.vcs_encoding(path)))
|
||
.map_err(|e| match e.kind {
|
||
ide_core::error::ErrorKind::InvalidInput => HostError::Invalid(e.message),
|
||
_ => HostError::Failed(e.message),
|
||
})?;
|
||
let root = to_ipc(r.root());
|
||
for c in pending {
|
||
budget -= c.diff.as_ref().map_or(0, String::len).min(budget);
|
||
changes.push(CommitChange {
|
||
repository: root.clone(),
|
||
path: to_ipc(&c.path),
|
||
old_path: c.old_path.as_deref().map(to_ipc),
|
||
status: c.status.to_owned(),
|
||
diff: c.diff,
|
||
truncated: c.truncated,
|
||
});
|
||
}
|
||
}
|
||
Ok(CommitDraft { revision: selection.revision, message: selection.message, amend: selection.amend, changes })
|
||
}
|
||
|
||
fn failed(e: impl std::fmt::Display) -> HostError {
|
||
HostError::Failed(e.to_string())
|
||
}
|
||
|
||
/// `cwd` of `open-terminal-session-in`: a directory of the project, relative to `root`
|
||
/// (`""` is the root), that exists and stays inside the project once symlinks are resolved.
|
||
fn project_subdirectory(root: &Path, cwd: &str) -> Result<PathBuf, HostError> {
|
||
let invalid = |why: &str| HostError::Invalid(format!("terminal directory {cwd:?}: {why}"));
|
||
if cwd.len() > 4096 || cwd.contains('\0') {
|
||
return Err(invalid("not a path"));
|
||
}
|
||
let relative = Path::new(cwd);
|
||
if relative.is_absolute() || relative.has_root() {
|
||
return Err(invalid("must be relative to the project root"));
|
||
}
|
||
let base = dunce::canonicalize(root).map_err(|_| invalid("the project root is gone"))?;
|
||
let dir = dunce::canonicalize(base.join(relative)).map_err(|_| invalid("no such directory"))?;
|
||
if !dir.starts_with(&base) {
|
||
return Err(invalid("outside the project"));
|
||
}
|
||
if !dir.is_dir() {
|
||
return Err(invalid("not a directory"));
|
||
}
|
||
Ok(dir)
|
||
}
|
||
|
||
impl PluginApi {
|
||
/// The value of `key` the user set for this plugin, as a plain string
|
||
/// (`None` when unset or not a string).
|
||
pub(super) fn setting_value(&self, key: &str) -> Option<String> {
|
||
let raw = self.setting(None, key)?;
|
||
serde_json::from_str::<String>(&raw).ok()
|
||
}
|
||
|
||
/// The plugin's `net:setting:` grants with their hosts: the approved host
|
||
/// while the setting still holds it, and the pending one to approve
|
||
/// otherwise (`None` when the value is unset or not a host).
|
||
pub(super) fn net_setting_hosts(&self, subject: &Subject) -> Vec<(Capability, Option<String>, Option<String>)> {
|
||
let derived: Vec<Capability> = self
|
||
.pkg
|
||
.manifest
|
||
.capabilities
|
||
.all()
|
||
.filter(|c| matches!(c, Capability::NetSetting { .. }))
|
||
.cloned()
|
||
.collect();
|
||
if derived.is_empty() {
|
||
return Vec::new();
|
||
}
|
||
let ledger = self.shared.ledger.lock();
|
||
let grant = ledger.get(subject).filter(|g| g.confirmed);
|
||
let session = self.shared.session_setting_hosts.lock().get(self.id()).cloned().unwrap_or_default();
|
||
derived
|
||
.into_iter()
|
||
.map(|cap| {
|
||
let current = self.setting_value(&setting_key(&cap)).and_then(|v| setting_host(&v).ok()).flatten();
|
||
let record = grant.and_then(|g| g.setting_hosts.get(&cap.to_string())).map(String::as_str);
|
||
let session_host = session.get(&cap.to_string()).map(String::as_str);
|
||
// An approval covers requests only while the setting still holds it.
|
||
let approved = [record, session_host]
|
||
.into_iter()
|
||
.flatten()
|
||
.find(|host| current.as_deref() == Some(host))
|
||
.map(str::to_owned);
|
||
let pending = current.filter(|host| approved.as_deref() != Some(host));
|
||
(cap, approved, pending)
|
||
})
|
||
.collect()
|
||
}
|
||
}
|
||
|
||
impl HostApi for PluginApi {
|
||
fn database_upsert(&self, root: &Path, source: ide_plugin::PluginDataSource) -> Result<(), HostError> {
|
||
let (host, db) = self.database_host(root)?;
|
||
let id = source.id.clone();
|
||
let source = super::database::settings(source)?;
|
||
self.remember_database_project(root, &id)?;
|
||
host.runtime.block_on(db.upsert_plugin_source(self.pkg.id(), &id, source)).map_err(super::database::error)?;
|
||
let internal = ide_db::plugin::source_id(self.pkg.id(), &id);
|
||
for project in host.projects().into_iter().filter(|p| p.root == root) {
|
||
host.runtime.block_on(project.db.disconnect_source(&internal));
|
||
}
|
||
super::database::changed(&host, root);
|
||
Ok(())
|
||
}
|
||
fn database_remove(&self, root: &Path, id: &str) -> Result<(), HostError> {
|
||
let (host, db) = self.database_host(root)?;
|
||
host.runtime.block_on(db.remove_plugin_source(self.pkg.id(), id)).map_err(super::database::error)?;
|
||
let internal = ide_db::plugin::source_id(self.pkg.id(), id);
|
||
for project in host.projects().into_iter().filter(|p| p.root == root) {
|
||
host.runtime.block_on(project.db.forget_plugin_source(&internal));
|
||
}
|
||
super::database::changed(&host, root);
|
||
Ok(())
|
||
}
|
||
fn database_sources(&self, root: &Path) -> Result<Vec<String>, HostError> {
|
||
let (_, db) = self.database_host(root)?;
|
||
db.plugin_sources(self.pkg.id()).map_err(super::database::error)
|
||
}
|
||
fn database_credential(&self, root: &Path, id: &str, password: String) -> Result<(), HostError> {
|
||
let (host, db) = self.database_host(root)?;
|
||
let source = db.plugin_source(self.pkg.id(), id).map_err(super::database::error)?;
|
||
self.remember_database_project(root, id)?;
|
||
for project in host.projects().into_iter().filter(|p| p.root == root) {
|
||
host.runtime.block_on(project.db.set_password(&source.id, Some(password.clone()), true)).map_err(failed)?;
|
||
}
|
||
Ok(())
|
||
}
|
||
fn database_connect(
|
||
&self,
|
||
root: &Path,
|
||
client: &str,
|
||
id: &str,
|
||
cancel: &CancellationToken,
|
||
timeout: Duration,
|
||
) -> Result<(), HostError> {
|
||
self.connect_database(root, client, id, cancel, timeout)
|
||
}
|
||
fn mcp_stdio_command(&self, client: &str) -> Option<Vec<String>> {
|
||
let host = self.shared.workspace()?.database_host()?;
|
||
let root = host.project(client).ok()?.root.clone();
|
||
host.mcp_executable.read().as_ref().map(|exe| ide_mcp::stdio_command(exe, &root))
|
||
}
|
||
|
||
fn extension_contributions(&self, point: &str) -> Result<Vec<(String, String)>, HostError> {
|
||
self.require_api("extension points", 5)?;
|
||
match self.shared.registry.point_owner(point) {
|
||
None => Err(HostError::NotFound(format!("no running plugin declares {point}"))),
|
||
Some(owner) if owner != self.id() => Err(HostError::Denied(format!("extension point {point}"))),
|
||
Some(_) => Ok(self
|
||
.shared
|
||
.registry
|
||
.contributions_to(point)
|
||
.into_iter()
|
||
.map(|c| (c.plugin, c.data.to_string()))
|
||
.collect()),
|
||
}
|
||
}
|
||
|
||
fn register_run_target(
|
||
&self,
|
||
root: &Path,
|
||
target: ide_plugin::PluginRunTarget,
|
||
generation: Option<u64>,
|
||
) -> Result<(), HostError> {
|
||
self.register_target(root, target, generation)
|
||
}
|
||
fn unregister_run_target(&self, root: &Path, id: &str) {
|
||
self.unregister_target(root, id);
|
||
}
|
||
|
||
fn start_run(
|
||
&self,
|
||
client: &str,
|
||
config: &str,
|
||
args: Vec<String>,
|
||
options: ide_plugin::PluginRunStartOptions,
|
||
cancel: &CancellationToken,
|
||
timeout: Duration,
|
||
) -> Result<String, HostError> {
|
||
let _ = timeout;
|
||
if cancel.is_cancelled() {
|
||
return Err(HostError::Cancelled);
|
||
}
|
||
let mut run =
|
||
self.shared.workspace().ok_or(HostError::Cancelled)?.run_configuration(client, self.id(), config)?;
|
||
let binary = self.approved_program_in_project(&run.command, self.shared.project_of(client).as_deref())?;
|
||
let user_override = !run.detected;
|
||
let debug = options.debug;
|
||
if debug && run.debug_adapter.is_none() {
|
||
return Err(HostError::Invalid("this configuration has no debugger".into()));
|
||
}
|
||
let program = run.command.clone();
|
||
let has_target = run.extras.target.as_ref().is_some_and(|t| !t.is_empty());
|
||
if debug && has_target {
|
||
return Err(HostError::Invalid("debugging this target is not supported".into()));
|
||
}
|
||
if debug && options.output_mode == Some(ide_plugin::PluginOutputMode::Pty) {
|
||
return Err(HostError::Invalid("terminal emulation cannot be used when debugging".into()));
|
||
}
|
||
if debug {
|
||
run.extras.emulate_terminal = false;
|
||
run.command = binary.to_string_lossy().into_owned();
|
||
} else {
|
||
// Precedence: user-saved override > per-invocation override > plugin default.
|
||
if !user_override && let Some(mode) = options.output_mode {
|
||
run.extras.emulate_terminal = mode == ide_plugin::PluginOutputMode::Pty;
|
||
}
|
||
if run.extras.emulate_terminal {
|
||
if run.is_test {
|
||
return Err(HostError::Invalid("terminal emulation cannot be used for test configurations".into()));
|
||
}
|
||
if run.extras.stdin_file.is_some() {
|
||
return Err(HostError::Invalid(
|
||
"input cannot be redirected from a file when emulating a terminal".into(),
|
||
));
|
||
}
|
||
if has_target {
|
||
return Err(HostError::Invalid("terminal emulation on this target is not supported".into()));
|
||
}
|
||
}
|
||
}
|
||
run.args.extend(args);
|
||
let id = self.new_operation(
|
||
client,
|
||
super::execution::OperationKind::Run { config: config.into(), configuration: run.id.clone(), program },
|
||
)?;
|
||
run.extras.params.insert(super::execution::RUN_OPERATION.into(), id.clone());
|
||
let request = super::WindowRequest::Run { operation: id.clone(), config: Box::new(run), debug };
|
||
self.shared.sink.emit(
|
||
Some(client),
|
||
PluginEvent::WindowRequest { plugin_id: self.id().into(), request_id: String::new(), request },
|
||
);
|
||
Ok(id)
|
||
}
|
||
fn open_terminal_session(
|
||
&self,
|
||
client: &str,
|
||
key: String,
|
||
title: String,
|
||
cmd: ProcessCommand,
|
||
cwd: Option<String>,
|
||
cancel: &CancellationToken,
|
||
timeout: Duration,
|
||
) -> Result<String, HostError> {
|
||
super::execution::label(&key, 128)?;
|
||
super::execution::label(&title, 256)?;
|
||
let root =
|
||
self.shared.project_of(client).ok_or_else(|| HostError::Invalid("no project in this window".into()))?;
|
||
if !self.shared.workspace().is_some_and(|w| w.trusted(&root)) {
|
||
return Err(HostError::Denied("project is not trusted".into()));
|
||
}
|
||
// Trust and the program pin stay the project's; the directory only moves the shell.
|
||
let dir = match &cwd {
|
||
Some(cwd) => project_subdirectory(&root, cwd)?,
|
||
None => root.clone(),
|
||
};
|
||
let binary = self.approved_program_in_project(&cmd.program, Some(&root))?;
|
||
let mut command = StdCommand::new(&binary);
|
||
append_process_env(&mut command, &cmd.env)?;
|
||
if cmd.args.iter().any(|a| a.contains('\0')) {
|
||
return Err(HostError::Invalid("NUL in terminal argument".into()));
|
||
}
|
||
let existing = self
|
||
.shared
|
||
.operations
|
||
.lock()
|
||
.iter()
|
||
.find(|(_, op)| {
|
||
op.plugin == self.id()
|
||
&& op.client == client
|
||
&& matches!(&op.kind, super::execution::OperationKind::Terminal { key: k, .. } if k == &key)
|
||
})
|
||
.map(|(id, _)| id.clone());
|
||
let id = match existing {
|
||
Some(id) => id,
|
||
None => self.new_operation(
|
||
client,
|
||
super::execution::OperationKind::Terminal { key: key.clone(), command: cmd.clone() },
|
||
)?,
|
||
};
|
||
let request = super::WindowRequest::Terminal {
|
||
operation: id.clone(),
|
||
key,
|
||
title,
|
||
program: binary.into(),
|
||
args: cmd.args,
|
||
env: cmd.env.into_iter().collect(),
|
||
cwd: dir.into(),
|
||
};
|
||
if let Err(error) = self.window_request(client, request, cancel, timeout) {
|
||
self.shared.operations.lock().remove(&id);
|
||
return Err(error);
|
||
}
|
||
Ok(id)
|
||
}
|
||
fn write_terminal(
|
||
&self,
|
||
client: &str,
|
||
key: &str,
|
||
text: String,
|
||
cancel: &CancellationToken,
|
||
timeout: Duration,
|
||
) -> Result<(), HostError> {
|
||
let root = self.shared.project_of(client).ok_or(HostError::Cancelled)?;
|
||
if !self.shared.workspace().is_some_and(|w| w.trusted(&root)) {
|
||
return Err(HostError::Denied("project is not trusted".into()));
|
||
}
|
||
let _ = timeout;
|
||
if cancel.is_cancelled() {
|
||
return Err(HostError::Cancelled);
|
||
}
|
||
let terminal = self
|
||
.shared
|
||
.operations
|
||
.lock()
|
||
.iter()
|
||
.find(|(_, op)| {
|
||
op.plugin == self.id()
|
||
&& op.client == client
|
||
&& op.root.as_ref() == Some(&root)
|
||
&& matches!(&op.kind, super::execution::OperationKind::Terminal { key: k, .. } if k == key)
|
||
})
|
||
.and_then(|(_, op)| op.terminal.clone())
|
||
.ok_or_else(|| HostError::NotFound("no running terminal with this key".into()))?;
|
||
let (host, id) = terminal;
|
||
host.upgrade().ok_or(HostError::Cancelled)?.terminals.write(id, &text).map_err(failed)
|
||
}
|
||
|
||
fn open_content_tab(
|
||
&self,
|
||
client: &str,
|
||
tab: ide_plugin::ContentTab,
|
||
cancel: &CancellationToken,
|
||
timeout: Duration,
|
||
) -> Result<(), HostError> {
|
||
super::execution::validate_tab(&tab)?;
|
||
let mut tabs = self.shared.content_tabs.lock();
|
||
let key = (self.id().into(), client.into(), tab.key.clone());
|
||
if !tabs.contains_key(&key) && tabs.keys().filter(|(p, w, _)| p == self.id() && w == client).count() >= 32 {
|
||
return Err(HostError::Invalid("at most 32 content tabs".into()));
|
||
}
|
||
let previous = tabs.insert(key.clone(), tab.clone());
|
||
drop(tabs);
|
||
if let Err(error) = self.window_request(client, super::WindowRequest::ContentTab { tab }, cancel, timeout) {
|
||
let mut tabs = self.shared.content_tabs.lock();
|
||
if let Some(previous) = previous {
|
||
tabs.insert(key, previous);
|
||
} else {
|
||
tabs.remove(&key);
|
||
}
|
||
return Err(error);
|
||
}
|
||
Ok(())
|
||
}
|
||
fn open_diff(&self, client: &str, diff: ide_plugin::FileDiff, file_name: Option<String>) -> Result<(), HostError> {
|
||
super::execution::validate_file_diff(&diff)?;
|
||
if let Some(name) = &file_name {
|
||
super::execution::label(name, 256)?;
|
||
}
|
||
self.shared.sink.emit(Some(client), PluginEvent::OpenDiff { plugin_id: self.id().into(), diff, file_name });
|
||
Ok(())
|
||
}
|
||
fn reply_content_tab(
|
||
&self,
|
||
client: &str,
|
||
request: &str,
|
||
reply: ide_plugin::ContentTabReply,
|
||
) -> Result<(), HostError> {
|
||
super::execution::validate_reply(&reply)?;
|
||
if self.shared.content_requests.lock().remove(&(self.id().into(), client.into(), request.into())).is_none() {
|
||
return Err(HostError::NotFound("unknown content action request".into()));
|
||
}
|
||
self.shared.sink.emit(
|
||
Some(client),
|
||
PluginEvent::ContentTabReply { plugin_id: self.id().into(), request_id: request.into(), reply },
|
||
);
|
||
Ok(())
|
||
}
|
||
|
||
fn owns_command(&self, id: &str) -> bool {
|
||
let commands = self.shared.ui.items(super::COMMANDS, None);
|
||
let mut matches = commands.iter().filter(|c| id == format!("plugin.{}.{}", c.plugin_id, c.key)).peekable();
|
||
matches.peek().is_some() && matches.all(|c| c.plugin_id == self.id())
|
||
}
|
||
|
||
fn open_editors(&self, client: &str) -> Result<Vec<ide_plugin::OpenEditor>, HostError> {
|
||
self.shared.workspace().ok_or_else(|| HostError::NotFound("window project".into()))?.open_editors(client)
|
||
}
|
||
fn editor_open(
|
||
&self,
|
||
client: &str,
|
||
path: &Path,
|
||
selection: Option<ide_core::Range>,
|
||
as_text: bool,
|
||
) -> Result<(), HostError> {
|
||
self.shared.sink.emit(Some(client), PluginEvent::EditorOpen { path: path.into(), selection, as_text });
|
||
Ok(())
|
||
}
|
||
fn editor_ranges(
|
||
&self,
|
||
client: &str,
|
||
path: &Path,
|
||
ranges: Vec<ide_core::Range>,
|
||
reveal: bool,
|
||
) -> Result<(), HostError> {
|
||
let workspace = self.shared.workspace().ok_or_else(|| HostError::NotFound("window project".into()))?;
|
||
if !workspace.open_editors(client)?.iter().any(|e| Path::new(&e.path) == path) {
|
||
return Err(HostError::Invalid("editor does not belong to the invoking window".into()));
|
||
}
|
||
let (_, text) =
|
||
workspace.document_in(client, path).ok_or_else(|| HostError::NotFound("editor is closed".into()))?;
|
||
let document = ide_text::Document::new(&text);
|
||
for r in &ranges {
|
||
if !valid_range(&document, *r) {
|
||
return Err(HostError::Invalid("range outside document".into()));
|
||
}
|
||
}
|
||
self.shared.sink.emit(Some(client), PluginEvent::EditorRanges { path: path.into(), ranges, reveal });
|
||
Ok(())
|
||
}
|
||
fn search_text(
|
||
&self,
|
||
client: Option<&str>,
|
||
root: &Path,
|
||
query: ide_plugin::TextQuery,
|
||
limit: u32,
|
||
cancel: &CancellationToken,
|
||
deadline: Instant,
|
||
) -> Result<ide_plugin::TextSearch, HostError> {
|
||
let workspace = self.shared.workspace().ok_or_else(|| HostError::NotFound("project closed".into()))?;
|
||
if query.pattern.len() > 16_384 || query.include.len() + query.exclude.len() > 256 {
|
||
return Err(HostError::Invalid("search query too large".into()));
|
||
}
|
||
let token = cancel.child_token();
|
||
let timed = token.clone();
|
||
let (done, wait) = std::sync::mpsc::channel();
|
||
let timer = std::thread::spawn(move || {
|
||
if wait.recv_timeout(deadline.saturating_duration_since(Instant::now())).is_err() {
|
||
timed.cancel();
|
||
}
|
||
});
|
||
let result = workspace.search_text(client, root, query, limit.min(10_000), &token);
|
||
let _ = done.send(());
|
||
let _ = timer.join();
|
||
if cancel.is_cancelled() {
|
||
Err(HostError::Cancelled)
|
||
} else if Instant::now() >= deadline {
|
||
Err(HostError::Failed("text search timed out".into()))
|
||
} else {
|
||
result
|
||
}
|
||
}
|
||
fn problems(&self, client: &str, path: Option<&Path>) -> Result<Vec<ide_plugin::FileProblems>, HostError> {
|
||
self.shared.workspace().ok_or_else(|| HostError::NotFound("window project".into()))?.problems(client, path)
|
||
}
|
||
fn decorations(&self, path: Option<&Path>, set: &str, items: Vec<ide_plugin::Decoration>) -> Result<(), HostError> {
|
||
self.set_decorations(path, set, items, None, false)
|
||
}
|
||
fn file_decorations(
|
||
&self,
|
||
path: Option<&Path>,
|
||
set: &str,
|
||
item: Option<ide_plugin::FileDecoration>,
|
||
) -> Result<(), HostError> {
|
||
self.set_decorations(path, set, Vec::new(), item, true)
|
||
}
|
||
fn file_icon_decorations(&self, set: &str, items: Vec<(PathBuf, String)>) -> Result<(), HostError> {
|
||
self.set_file_icon_decorations(set, items)
|
||
}
|
||
fn fork_agent_session(&self, client: &str, agent: &str, session_id: &str, cwd: &Path) -> Result<String, HostError> {
|
||
self.shared
|
||
.workspace()
|
||
.ok_or_else(|| HostError::NotFound("window project".into()))?
|
||
.fork_agent_session(client, agent, session_id, cwd)
|
||
}
|
||
fn console_rules(&self, rules: Vec<ide_plugin::ConsoleRule>) -> Result<(), HostError> {
|
||
let rules = super::console_rules::normalize(rules).map_err(HostError::Invalid)?;
|
||
let mut all = self.shared.console_rules.lock();
|
||
if rules.is_empty() {
|
||
all.remove(self.id());
|
||
} else {
|
||
all.insert(self.id().into(), rules.clone());
|
||
}
|
||
// Emitted under the lock so replacements cannot arrive out of order.
|
||
self.shared.sink.emit(None, PluginEvent::ConsoleRulesChanged { plugin_id: self.id().into(), rules });
|
||
Ok(())
|
||
}
|
||
fn vcs_log(
|
||
&self,
|
||
root: &Path,
|
||
path: Option<&str>,
|
||
limit: u32,
|
||
cancel: &CancellationToken,
|
||
) -> Result<Vec<ide_plugin::CommitInfo>, HostError> {
|
||
let repo = self.read_repo(root)?;
|
||
let path = path
|
||
.map(|p| ide_plugin::resolve_new_in_root(root, p).map(ide_core::IpcPath::from).map_err(HostError::Invalid))
|
||
.transpose()?;
|
||
let Some(repo) = repo else {
|
||
return Ok(Vec::new());
|
||
};
|
||
if limit == 0 {
|
||
return Ok(Vec::new());
|
||
}
|
||
let query = ide_vcs::LogQuery { path, limit: limit.min(1000), ..Default::default() };
|
||
repo.log_cancellable(&query, cancel)
|
||
.map(|cs| {
|
||
cs.into_iter()
|
||
.map(|c| ide_plugin::CommitInfo {
|
||
id: c.id,
|
||
short_id: c.short_id,
|
||
author: c.author,
|
||
author_email: c.email,
|
||
date: (c.time_ms / 1000.0) as i64,
|
||
subject: c.summary,
|
||
body: c
|
||
.message
|
||
.split_once('\n')
|
||
.map_or(String::new(), |(_, body)| body.trim_start_matches('\n').into()),
|
||
parents: c.parents,
|
||
})
|
||
.collect()
|
||
})
|
||
.map_err(failed)
|
||
}
|
||
fn vcs_blame(
|
||
&self,
|
||
client: Option<&str>,
|
||
root: &Path,
|
||
path: &str,
|
||
) -> Result<Vec<ide_plugin::BlameLine>, HostError> {
|
||
let repo = self.read_repo(root)?;
|
||
let path = ide_plugin::resolve_new_in_root(root, path).map_err(HostError::Invalid)?;
|
||
let Some(repo) = repo else {
|
||
return Ok(Vec::new());
|
||
};
|
||
let workspace = self.shared.workspace().unwrap();
|
||
let text = workspace.buffer_text_in(client, &path);
|
||
repo.blame(&path, text.as_deref(), None, workspace.vcs_encoding(&path))
|
||
.map(|lines| {
|
||
lines
|
||
.into_iter()
|
||
.enumerate()
|
||
.map(|(i, l)| ide_plugin::BlameLine {
|
||
line: i as u32,
|
||
commit: l.commit,
|
||
author: l.author,
|
||
date: (l.time_ms / 1000.0) as i64,
|
||
summary: l.summary,
|
||
})
|
||
.collect()
|
||
})
|
||
.map_err(failed)
|
||
}
|
||
fn vcs_diff(
|
||
&self,
|
||
root: &Path,
|
||
path: Option<&str>,
|
||
against: Option<&str>,
|
||
cancel: &CancellationToken,
|
||
) -> Result<Option<String>, HostError> {
|
||
let repo = self.read_repo(root)?;
|
||
let path = path.map(|p| ide_plugin::resolve_new_in_root(root, p).map_err(HostError::Invalid)).transpose()?;
|
||
let Some(repo) = repo else {
|
||
return Ok(None);
|
||
};
|
||
repo.working_tree_patch(path.as_deref(), against, 4 << 20, cancel).map(Some).map_err(failed)
|
||
}
|
||
|
||
fn execute_command(
|
||
&self,
|
||
invocation: &ide_plugin::Invocation,
|
||
id: &str,
|
||
arg: Option<String>,
|
||
cancel: &CancellationToken,
|
||
timeout: Duration,
|
||
) -> Result<Option<String>, HostError> {
|
||
let client = from_window(invocation.client.as_deref(), "execute commands")?;
|
||
let mut chain = invocation.command_chain.clone();
|
||
chain.push(self.id().into());
|
||
if chain.len() > 8 {
|
||
return Err(HostError::Invalid("command nesting limit (8)".into()));
|
||
}
|
||
for command in self.shared.ui.items(super::COMMANDS, None) {
|
||
if id == format!("plugin.{}.{}", command.plugin_id, command.key) && chain.contains(&command.plugin_id) {
|
||
return Err(HostError::Invalid("recursive plugin command".into()));
|
||
}
|
||
}
|
||
let request = self.shared.next_prompt.fetch_add(1, Ordering::Relaxed);
|
||
let (tx, rx) = std::sync::mpsc::sync_channel(1);
|
||
{
|
||
let closed = self.shared.closed_clients.lock();
|
||
let blocked = self.shared.prompts_blocked.lock();
|
||
if closed.contains(client) || blocked.contains(self.id()) {
|
||
return Err(HostError::Cancelled);
|
||
}
|
||
self.shared.prompts.lock().insert(
|
||
request,
|
||
PromptEntry { plugin: self.id().into(), client: client.into(), answer: Waiter::Command(tx) },
|
||
);
|
||
}
|
||
self.shared.sink.emit(
|
||
Some(client),
|
||
PluginEvent::ExecuteCommand { request_id: request.to_string(), id: id.into(), arg, chain },
|
||
);
|
||
let deadline = Instant::now() + timeout;
|
||
loop {
|
||
match rx.recv_timeout(Duration::from_millis(20)) {
|
||
Ok(super::CommandAnswer::Value(value)) => return Ok(value),
|
||
Ok(super::CommandAnswer::NotFound) => return Err(HostError::NotFound(id.into())),
|
||
Ok(super::CommandAnswer::Failed(error)) => return Err(HostError::Failed(error)),
|
||
Err(std::sync::mpsc::RecvTimeoutError::Timeout)
|
||
if !cancel.is_cancelled() && Instant::now() < deadline => {}
|
||
Err(_) => {
|
||
self.shared.prompts.lock().remove(&request);
|
||
return Err(HostError::Cancelled);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
fn output_write(&self, channel: &str, text: Option<&str>) -> Result<(), HostError> {
|
||
if channel.is_empty() || channel.len() > 128 || channel.chars().any(char::is_control) {
|
||
return Err(HostError::Invalid("output channel must be 1-128 bytes without control characters".into()));
|
||
}
|
||
let key = (self.id().to_owned(), channel.to_owned());
|
||
let mut channels = self.shared.output.lock();
|
||
if !channels.contains_key(&key) && channels.keys().filter(|(p, _)| p == self.id()).count() >= 32 {
|
||
return Err(HostError::Invalid("at most 32 output channels".into()));
|
||
}
|
||
let buffer = channels.entry(key).or_default();
|
||
if let Some(text) = text {
|
||
let mut start = text.len().saturating_sub(1 << 20);
|
||
while !text.is_char_boundary(start) {
|
||
start += 1;
|
||
}
|
||
buffer.push_str(&text[start..]);
|
||
} else {
|
||
buffer.clear();
|
||
}
|
||
let mut remove = buffer.len().saturating_sub(1 << 20);
|
||
while !buffer.is_char_boundary(remove) {
|
||
remove += 1;
|
||
}
|
||
buffer.drain(..remove);
|
||
let text = buffer.clone();
|
||
drop(channels);
|
||
self.shared.sink.emit(
|
||
None,
|
||
PluginEvent::OutputChanged { plugin_id: self.id().into(), channel: channel.into(), text: Some(text) },
|
||
);
|
||
Ok(())
|
||
}
|
||
fn output_reveal(&self, client: &str, channel: &str) -> Result<(), HostError> {
|
||
let key = (self.id().to_owned(), channel.to_owned());
|
||
if !self.shared.output.lock().contains_key(&key) {
|
||
self.output_write(channel, None)?;
|
||
}
|
||
if self.shared.client_closed(client) {
|
||
return Err(HostError::Cancelled);
|
||
}
|
||
let text = self.shared.output.lock().get(&key).cloned().unwrap_or_default();
|
||
self.shared.sink.emit(
|
||
Some(client),
|
||
PluginEvent::RevealOutput { plugin_id: self.id().into(), channel: channel.into(), text },
|
||
);
|
||
Ok(())
|
||
}
|
||
fn require_api(&self, what: &str, minor: u64) -> Result<(), HostError> {
|
||
self.pkg.manifest.needs_api(what, minor).map_err(HostError::Invalid)
|
||
}
|
||
|
||
fn notify(
|
||
&self,
|
||
client: Option<&str>,
|
||
level: NotificationLevel,
|
||
title: String,
|
||
body: String,
|
||
actions: Vec<ide_plugin::NotificationAction>,
|
||
) -> Result<(), HostError> {
|
||
check_actions(self, &actions)?;
|
||
let plugin_id = self.id().to_owned();
|
||
self.shared.sink.emit(client, PluginEvent::Notification { plugin_id, level, title, body, actions });
|
||
Ok(())
|
||
}
|
||
|
||
fn notify_project(
|
||
&self,
|
||
root: &Path,
|
||
level: NotificationLevel,
|
||
title: String,
|
||
body: String,
|
||
actions: Vec<ide_plugin::NotificationAction>,
|
||
) -> Result<(), HostError> {
|
||
check_actions(self, &actions)?;
|
||
let windows = self.shared.workspace().map(|w| w.project_windows(root)).unwrap_or_default();
|
||
let window = windows
|
||
.into_iter()
|
||
.find(|w| !self.shared.client_closed(w))
|
||
.ok_or_else(|| HostError::NotFound(format!("the project {} is not open", root.display())))?;
|
||
let plugin_id = self.id().to_owned();
|
||
self.shared.sink.emit(Some(&window), PluginEvent::Notification { plugin_id, level, title, body, actions });
|
||
Ok(())
|
||
}
|
||
|
||
fn copy_to_clipboard(&self, client: Option<&str>, text: String) -> Result<(), HostError> {
|
||
let client = from_window(client, "copy to the clipboard")?;
|
||
self.shared.sink.emit(Some(client), PluginEvent::CopyToClipboard { plugin_id: self.id().to_owned(), text });
|
||
Ok(())
|
||
}
|
||
|
||
fn read_clipboard(
|
||
&self,
|
||
client: &str,
|
||
cancel: &CancellationToken,
|
||
) -> Result<ide_plugin::ClipboardContent, HostError> {
|
||
let id = self.shared.next_prompt.fetch_add(1, Ordering::Relaxed);
|
||
let (answer, answers) = std::sync::mpsc::sync_channel(1);
|
||
{
|
||
let closed = self.shared.closed_clients.lock();
|
||
let blocked = self.shared.prompts_blocked.lock();
|
||
if closed.contains(client) || blocked.contains(self.id()) {
|
||
return Err(HostError::Cancelled);
|
||
}
|
||
self.shared.prompts.lock().insert(
|
||
id,
|
||
PromptEntry {
|
||
plugin: self.id().to_owned(),
|
||
client: client.to_owned(),
|
||
answer: Waiter::Clipboard(answer),
|
||
},
|
||
);
|
||
}
|
||
let request_id = id.to_string();
|
||
self.shared.sink.emit(
|
||
Some(client),
|
||
PluginEvent::ClipboardRequested { plugin_id: self.id().to_owned(), request_id: request_id.clone() },
|
||
);
|
||
// A remote window asks the user first, so there is no deadline: the call's own
|
||
// cancellation, the plugin stopping or the window closing ends the wait.
|
||
let answer = loop {
|
||
match answers.recv_timeout(Duration::from_millis(100)) {
|
||
Ok(answer) => break answer,
|
||
Err(std::sync::mpsc::RecvTimeoutError::Timeout) if !cancel.is_cancelled() => {}
|
||
Err(_) => {
|
||
self.shared.prompts.lock().remove(&id);
|
||
self.shared.sink.emit(Some(client), PluginEvent::PromptClosed { prompt_id: request_id });
|
||
return Err(HostError::Cancelled);
|
||
}
|
||
}
|
||
};
|
||
clipboard_content(answer)
|
||
}
|
||
|
||
fn open_url(&self, client: Option<&str>, url: String) -> Result<(), HostError> {
|
||
let client = from_window(client, "open a URL")?;
|
||
self.shared.sink.emit(Some(client), PluginEvent::OpenUrl { plugin_id: self.id().to_owned(), url });
|
||
Ok(())
|
||
}
|
||
|
||
fn open_project(&self, client: Option<&str>, path: String, new_window: bool) -> Result<(), HostError> {
|
||
let client = from_window(client, "open a project")?;
|
||
self.shared
|
||
.sink
|
||
.emit(Some(client), PluginEvent::OpenProject { plugin_id: self.id().to_owned(), path, new_window });
|
||
Ok(())
|
||
}
|
||
|
||
fn ask(
|
||
&self,
|
||
client: Option<&str>,
|
||
prompt: Prompt,
|
||
cancel: &CancellationToken,
|
||
) -> Result<Option<Vec<String>>, HostError> {
|
||
let client = from_window(client, "ask the user")?;
|
||
let id = self.shared.next_prompt.fetch_add(1, Ordering::Relaxed);
|
||
let (answer, answers) = std::sync::mpsc::sync_channel(1);
|
||
{
|
||
let closed = self.shared.closed_clients.lock();
|
||
if closed.contains(client) {
|
||
return Err(HostError::Cancelled);
|
||
}
|
||
let blocked = self.shared.prompts_blocked.lock();
|
||
if blocked.contains(self.id()) {
|
||
return Err(HostError::Cancelled);
|
||
}
|
||
self.shared.prompts.lock().insert(
|
||
id,
|
||
PromptEntry { plugin: self.id().to_owned(), client: client.to_owned(), answer: Waiter::Prompt(answer) },
|
||
);
|
||
}
|
||
let prompt_id = id.to_string();
|
||
self.shared.sink.emit(
|
||
Some(client),
|
||
PluginEvent::PromptRequested { plugin_id: self.id().to_owned(), prompt_id: prompt_id.clone(), prompt },
|
||
);
|
||
loop {
|
||
match answers.recv_timeout(Duration::from_millis(100)) {
|
||
Ok(answer) => return Ok(answer),
|
||
Err(std::sync::mpsc::RecvTimeoutError::Timeout) if !cancel.is_cancelled() => {}
|
||
Err(_) => {
|
||
self.shared.prompts.lock().remove(&id);
|
||
self.shared.sink.emit(Some(client), PluginEvent::PromptClosed { prompt_id });
|
||
return Err(HostError::Cancelled);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
fn pending_commit(
|
||
&self,
|
||
client: Option<&str>,
|
||
cancel: &CancellationToken,
|
||
) -> Result<Option<CommitDraft>, HostError> {
|
||
let client = from_window(client, "read the pending commit")?;
|
||
let workspace = self.shared.workspace().ok_or_else(|| HostError::Failed("no workspace".into()))?;
|
||
let root =
|
||
workspace.project_of(client).ok_or_else(|| HostError::Invalid("the window has no project".into()))?;
|
||
if !workspace.trusted(&root) {
|
||
return Err(HostError::Invalid("the project is in Restricted Mode: git does not run there".into()));
|
||
}
|
||
let answer = self.ask_commit(client, CommitRequest::Selection, cancel, COMMIT_SELECTION_TIMEOUT)?;
|
||
match answer {
|
||
CommitAnswer::NoRepository => Ok(None),
|
||
CommitAnswer::Selection { selection } => commit_draft(&*workspace, selection, cancel).map(Some),
|
||
CommitAnswer::Done | CommitAnswer::Stale => Err(HostError::Failed("unexpected answer".into())),
|
||
}
|
||
}
|
||
|
||
fn set_commit_message(&self, client: Option<&str>, revision: u64, message: String) -> Result<(), HostError> {
|
||
let client = from_window(client, "set the commit message")?;
|
||
let request = CommitRequest::SetMessage { revision, message };
|
||
match self.ask_commit(client, request, &CancellationToken::new(), COMMIT_MESSAGE_TIMEOUT)? {
|
||
CommitAnswer::Done => Ok(()),
|
||
CommitAnswer::Stale => Err(HostError::Stale),
|
||
CommitAnswer::NoRepository => Err(HostError::Invalid("the window's project has no repository".into())),
|
||
CommitAnswer::Selection { .. } => Err(HostError::Failed("unexpected answer".into())),
|
||
}
|
||
}
|
||
|
||
fn reveal_view(&self, client: Option<&str>, view: &str) -> Result<(), HostError> {
|
||
let client = from_window(client, "reveal a view")?;
|
||
// A tool window comes to the front; a dialog (API 1.18) opens.
|
||
let declared = [super::ui_points::VIEWS, super::ui_points::DIALOGS]
|
||
.into_iter()
|
||
.any(|point| self.shared.ui.items(point, Some(self.id())).iter().any(|i| i.key == view));
|
||
if !declared {
|
||
return Err(HostError::NotFound(format!("the plugin declares no view {view:?}")));
|
||
}
|
||
self.shared
|
||
.sink
|
||
.emit(Some(client), PluginEvent::RevealView { plugin_id: self.id().to_owned(), view: view.to_owned() });
|
||
Ok(())
|
||
}
|
||
|
||
fn open_settings(&self, client: Option<&str>, target: SettingsTarget) -> Result<(), HostError> {
|
||
let client = from_window(client, "open Settings")?;
|
||
let (page, permissions) = match target {
|
||
SettingsTarget::Permissions => (None, true),
|
||
SettingsTarget::Page(page) => {
|
||
let decls = self.shared.ui.items(super::ui_points::SETTINGS, Some(self.id()));
|
||
let Some(decl) = decls.first().map(|i| &i.decl) else {
|
||
return Err(HostError::NotFound("the plugin declares no [contributes.settings]".into()));
|
||
};
|
||
if let Some(page) = &page {
|
||
let pages = decl.get("pages").and_then(|p| p.as_array());
|
||
if !pages.is_some_and(|ps| ps.iter().any(|p| p.get("id").and_then(|id| id.as_str()) == Some(page)))
|
||
{
|
||
return Err(HostError::NotFound(format!("the plugin declares no settings page {page:?}")));
|
||
}
|
||
}
|
||
(page, false)
|
||
}
|
||
};
|
||
self.shared
|
||
.sink
|
||
.emit(Some(client), PluginEvent::OpenSettings { plugin_id: self.id().to_owned(), page, permissions });
|
||
Ok(())
|
||
}
|
||
|
||
fn status_item(&self, id: &str, item: Option<StatusItem>) {
|
||
let key = (self.id().to_owned(), id.to_owned());
|
||
{
|
||
let mut status = self.shared.status.lock();
|
||
match &item {
|
||
Some(item) => {
|
||
let count = status.keys().filter(|(p, _)| p == self.id()).count();
|
||
if !status.contains_key(&key) && count >= MAX_STATUS_ITEMS {
|
||
tracing::warn!(plugin = %self.id(), "more than {MAX_STATUS_ITEMS} status items; {id:?} ignored");
|
||
return;
|
||
}
|
||
if status.get(&key) == Some(item) {
|
||
return;
|
||
}
|
||
status.insert(key, item.clone());
|
||
}
|
||
None => {
|
||
if status.remove(&key).is_none() {
|
||
return;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
self.shared
|
||
.sink
|
||
.emit(None, PluginEvent::StatusItemChanged { plugin_id: self.id().to_owned(), id: id.to_owned(), item });
|
||
}
|
||
|
||
fn check_status_menu(&self, menu: &[ide_plugin::StatusMenuEntry]) -> Result<(), HostError> {
|
||
let actions: Vec<ide_plugin::NotificationAction> = menu
|
||
.iter()
|
||
.filter_map(|entry| match entry {
|
||
ide_plugin::StatusMenuEntry::Action { label, command, argument, .. } => {
|
||
Some(ide_plugin::NotificationAction {
|
||
label: label.clone(),
|
||
command: command.clone(),
|
||
argument: argument.clone(),
|
||
})
|
||
}
|
||
_ => None,
|
||
})
|
||
.collect();
|
||
check_actions(self, &actions)
|
||
}
|
||
|
||
fn window_status_item(&self, client: &str, id: &str, item: Option<StatusItem>) -> Result<(), HostError> {
|
||
let closed = self.shared.closed_clients.lock();
|
||
let blocked = self.shared.prompts_blocked.lock();
|
||
if closed.contains(client) || blocked.contains(self.id()) {
|
||
return Err(HostError::Cancelled);
|
||
}
|
||
let key = (client.to_owned(), self.id().to_owned(), id.to_owned());
|
||
let mut items = self.shared.window_status.lock();
|
||
if let Some(item) = &item {
|
||
if !items.contains_key(&key)
|
||
&& items.keys().filter(|(w, p, _)| w == client && p == self.id()).count() >= MAX_STATUS_ITEMS
|
||
{
|
||
return Err(HostError::Invalid("too many window status items".into()));
|
||
}
|
||
items.insert(key, item.clone());
|
||
} else {
|
||
items.remove(&key);
|
||
}
|
||
drop(items);
|
||
self.shared.sink.emit(
|
||
Some(client),
|
||
PluginEvent::WindowStatusItemChanged { plugin_id: self.id().into(), id: id.into(), item },
|
||
);
|
||
Ok(())
|
||
}
|
||
|
||
fn set_context(&self, client: Option<&str>, key: &str, value: Option<String>) -> Result<(), HostError> {
|
||
let map_key = (client.map(str::to_owned), self.id().to_owned(), key.to_owned());
|
||
{
|
||
let mut contexts = self.shared.contexts.lock();
|
||
if client.is_some_and(|c| self.shared.closed_clients.lock().contains(c)) {
|
||
return Ok(());
|
||
}
|
||
match &value {
|
||
Some(value) => {
|
||
if contexts.get(&map_key) == Some(value) {
|
||
return Ok(());
|
||
}
|
||
let count = contexts.keys().filter(|(_, p, _)| p == self.id()).count();
|
||
if !contexts.contains_key(&map_key) && count >= MAX_CONTEXT_KEYS {
|
||
return Err(HostError::Invalid(format!("more than {MAX_CONTEXT_KEYS} context keys")));
|
||
}
|
||
contexts.insert(map_key, value.clone());
|
||
}
|
||
None => {
|
||
if contexts.remove(&map_key).is_none() {
|
||
return Ok(());
|
||
}
|
||
}
|
||
}
|
||
}
|
||
self.shared
|
||
.sink
|
||
.emit(client, PluginEvent::ContextChanged { plugin_id: self.id().to_owned(), key: key.to_owned(), value });
|
||
Ok(())
|
||
}
|
||
|
||
fn projects(&self) -> Vec<PathBuf> {
|
||
self.shared.workspace().map(|w| w.projects()).unwrap_or_default()
|
||
}
|
||
|
||
fn member_roots(&self, root: &Path) -> Vec<PathBuf> {
|
||
self.shared.workspace().map(|w| w.member_roots(root)).unwrap_or_default()
|
||
}
|
||
|
||
fn buffer_text(&self, path: &Path) -> Option<String> {
|
||
self.shared.workspace()?.buffer_text(path)
|
||
}
|
||
|
||
fn document(&self, path: &Path) -> Option<(u64, String)> {
|
||
self.shared.workspace()?.document(path)
|
||
}
|
||
|
||
fn apply_edits(
|
||
&self,
|
||
client: Option<&str>,
|
||
path: &Path,
|
||
version: u64,
|
||
edits: Vec<TextEdit>,
|
||
) -> Result<u64, HostError> {
|
||
let workspace = self.shared.workspace().ok_or_else(|| HostError::NotFound("no project is open".into()))?;
|
||
// Edits from a window's command are undoable; `editor.apply-edits-at` from a background event isn't.
|
||
workspace.apply_edits(path, version, &edits, client.is_none())
|
||
}
|
||
|
||
fn storage_get(&self, key: &str) -> Result<Option<String>, HostError> {
|
||
let file = self.storage()?.join(entry_name(key));
|
||
match std::fs::read(&file) {
|
||
Ok(bytes) => Ok(decode_entry(&bytes).filter(|(k, _)| *k == key).map(|(_, v)| v.to_owned())),
|
||
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(None),
|
||
Err(e) => Err(storage_io(e)),
|
||
}
|
||
}
|
||
|
||
fn storage_set(&self, key: &str, value: Option<String>) -> Result<(), HostError> {
|
||
if key.is_empty() || key.len() > MAX_KEY {
|
||
return Err(HostError::Invalid(format!("keys are 1–{MAX_KEY} bytes")));
|
||
}
|
||
let dir = self.storage()?;
|
||
let file = dir.join(entry_name(key));
|
||
let Some(value) = value else {
|
||
return match std::fs::remove_file(&file) {
|
||
Err(e) if e.kind() != std::io::ErrorKind::NotFound => Err(storage_io(e)),
|
||
_ => Ok(()),
|
||
};
|
||
};
|
||
let others: usize = entries(&dir)?.filter(|(name, _)| *name != file).map(|(_, size)| size).sum();
|
||
if others + key.len() + value.len() > MAX_STORAGE {
|
||
return Err(HostError::Invalid(format!(
|
||
"storage is limited to {} MiB (UTF-8 keys and values)",
|
||
MAX_STORAGE >> 20
|
||
)));
|
||
}
|
||
write_entry(&dir, key, &value)
|
||
}
|
||
|
||
fn storage_keys(&self, prefix: &str) -> Result<Vec<String>, HostError> {
|
||
let dir = self.storage()?;
|
||
let mut keys = Vec::new();
|
||
for (file, _) in entries(&dir)? {
|
||
let bytes = std::fs::read(&file).map_err(storage_io)?;
|
||
if let Some((key, _)) = decode_entry(&bytes).filter(|(k, _)| k.starts_with(prefix)) {
|
||
keys.push(key.to_owned());
|
||
}
|
||
}
|
||
keys.sort();
|
||
Ok(keys)
|
||
}
|
||
|
||
fn vcs_status(&self, root: &Path) -> Result<Option<RepositoryStatus>, HostError> {
|
||
let Some(repo) = self.repo(root)? else { return Ok(None) };
|
||
let info = repo.branch_info().map_err(failed)?;
|
||
let changed = repo.status().map_err(failed)?.len();
|
||
Ok(Some(RepositoryStatus {
|
||
root: to_ipc(repo.root()),
|
||
branch: info.head,
|
||
commit: info.head_commit,
|
||
upstream: info.upstream,
|
||
ahead: info.ahead,
|
||
behind: info.behind,
|
||
operation: info.operation,
|
||
changed_files: u32::try_from(changed).unwrap_or(u32::MAX),
|
||
}))
|
||
}
|
||
|
||
fn vcs_remotes(&self, root: &Path) -> Result<Vec<(String, String)>, HostError> {
|
||
if self.shared.workspace().is_some_and(|w| !w.git_enabled(root)) {
|
||
return Ok(Vec::new());
|
||
}
|
||
let Some(repo) = ide_vcs::Repo::discover(root).map_err(failed)? else { return Ok(Vec::new()) };
|
||
plugin_remotes(&repo)
|
||
}
|
||
|
||
fn vcs_roots(&self, root: &Path) -> Result<Vec<String>, HostError> {
|
||
let workspace = self.shared.workspace().ok_or(HostError::Cancelled)?;
|
||
Ok(workspace
|
||
.repositories(root)?
|
||
.roots()
|
||
.iter()
|
||
.filter(|r| r.starts_with(root) || root.starts_with(r))
|
||
.map(|r| to_ipc(r))
|
||
.collect())
|
||
}
|
||
|
||
fn vcs_repository_at(&self, root: &Path, path: &str) -> Result<Option<ide_plugin::RepositoryInfo>, HostError> {
|
||
let workspace = self.shared.workspace().ok_or(HostError::Cancelled)?;
|
||
if !workspace.trusted(root) {
|
||
return Err(HostError::Failed("the project is in Restricted Mode: git does not run there".into()));
|
||
}
|
||
let path = ide_plugin::resolve_in_root(root, path).map_err(HostError::Invalid)?;
|
||
let repos = workspace.repositories(root)?;
|
||
// A link to a nested worktree directory denotes that worktree, not its entry in the parent.
|
||
let Some(repo) = repos.at_root(&path).or_else(|| repos.for_path(&path)) else { return Ok(None) };
|
||
let relative = path.strip_prefix(repo.root()).map_err(failed)?;
|
||
let info = repo.branch_info().map_err(failed)?;
|
||
Ok(Some(ide_plugin::RepositoryInfo {
|
||
root: to_ipc(repo.root()),
|
||
relative_path: to_ipc(relative),
|
||
branch: info.head,
|
||
commit: info.head_commit,
|
||
upstream: info.upstream,
|
||
ahead: info.ahead,
|
||
remotes: plugin_remotes(repo)?,
|
||
}))
|
||
}
|
||
|
||
fn vcs_fetch(&self, root: &Path, cancel: &CancellationToken) -> Result<(), HostError> {
|
||
let workspace = self.shared.workspace();
|
||
if !workspace.is_some_and(|w| w.trusted(root)) {
|
||
return Err(HostError::Failed("the project is in Restricted Mode: git does not run there".into()));
|
||
}
|
||
let Some(repo) = self.repo(root)? else {
|
||
return Err(HostError::NotFound("the project is not in a git repository".into()));
|
||
};
|
||
match repo.fetch(cancel, &|_| {}) {
|
||
Ok(()) => Ok(()),
|
||
Err(_) if cancel.is_cancelled() => Err(HostError::Cancelled),
|
||
Err(e) => Err(failed(e.message)),
|
||
}
|
||
}
|
||
|
||
fn symbols(
|
||
&self,
|
||
root: &Path,
|
||
query: &str,
|
||
limit: usize,
|
||
cancel: &CancellationToken,
|
||
deadline: Instant,
|
||
) -> Result<Vec<ide_plugin::SymbolInfo>, HostError> {
|
||
if query.len() > 256 {
|
||
return Err(HostError::Invalid("symbol query exceeds 256 UTF-8 bytes".into()));
|
||
}
|
||
self.shared.workspace().ok_or_else(|| HostError::NotFound("no project index is available".into()))?.symbols(
|
||
root,
|
||
query,
|
||
limit.min(1000),
|
||
cancel,
|
||
deadline,
|
||
)
|
||
}
|
||
|
||
fn lsp_request(
|
||
&self,
|
||
root: &Path,
|
||
server: &str,
|
||
method: &str,
|
||
params: Option<String>,
|
||
notify: bool,
|
||
cancel: &CancellationToken,
|
||
timeout: Duration,
|
||
) -> Result<String, HostError> {
|
||
self.require_api("`language-servers`", 9)?;
|
||
if !ide_lsp::plugin_method_allowed(method) {
|
||
return Err(HostError::Invalid(format!("`{method}` is not one of the server's own methods")));
|
||
}
|
||
let workspace = self
|
||
.shared
|
||
.workspace()
|
||
.ok_or_else(|| HostError::NotFound(format!("no language server `{server}` is running for this project")))?;
|
||
// The server reads and edits the project on the plugin's behalf, as a process would.
|
||
if !workspace.trusted(root) {
|
||
return Err(HostError::Denied("project is not trusted".into()));
|
||
}
|
||
let params = params
|
||
.map(|p| serde_json::from_str(&p).map_err(|e| HostError::Invalid(format!("params are not JSON: {e}"))))
|
||
.transpose()?;
|
||
let result = workspace.lsp_request(root, server, method, params, notify, cancel, timeout)?;
|
||
Ok(result.to_string())
|
||
}
|
||
|
||
fn build_dependencies(
|
||
&self,
|
||
root: &Path,
|
||
module: Option<&str>,
|
||
) -> Result<Vec<ide_plugin::DependencyNode>, HostError> {
|
||
self.require_api("`build`", 10)?;
|
||
let workspace = self.shared.workspace().ok_or_else(|| HostError::NotFound("no project is open".into()))?;
|
||
// The graph comes from running the project's build, as `vcs.read` does git.
|
||
if !workspace.trusted(root) {
|
||
return Err(HostError::Denied("project is not trusted".into()));
|
||
}
|
||
super::build_graph::dependency_nodes(&workspace.build_graph(root)?, module)
|
||
}
|
||
|
||
fn find_files(&self, root: &Path, glob: &str, limit: usize) -> Result<Vec<String>, HostError> {
|
||
let matcher = globset::GlobBuilder::new(glob)
|
||
.literal_separator(true)
|
||
.build()
|
||
.map_err(|e| HostError::Invalid(format!("{glob:?}: {e}")))?
|
||
.compile_matcher();
|
||
// Project scope visits hidden files and folders (`.github`, `.config`):
|
||
// only `.git` internals stay out, as in the walk below.
|
||
let visible = |rel: &Path| !rel.iter().any(|c| c == ".git");
|
||
// The project index already lists the files: walking a large tree on every call can take
|
||
// longer than an editor provider's deadline (the plugin is then stopped as crashed).
|
||
if let Some(files) = self
|
||
.shared
|
||
.workspace()
|
||
.and_then(|w| w.indexed_files(root, &|rel| visible(rel) && matcher.is_match(rel), limit))
|
||
{
|
||
let mut out: Vec<String> = files.iter().map(|p| to_ipc(p)).collect();
|
||
out.sort();
|
||
return Ok(out);
|
||
}
|
||
// A project window group's members are project scope too (their files
|
||
// are in the group's index, and a group root holds none of them).
|
||
let mut roots = vec![root.to_path_buf()];
|
||
roots.extend(self.member_roots(root));
|
||
let mut out = Vec::new();
|
||
'walk: for root in &roots {
|
||
for (n, entry) in ignore::WalkBuilder::new(root)
|
||
.hidden(false)
|
||
.filter_entry(|e| e.file_name() != ".git")
|
||
.build()
|
||
.flatten()
|
||
.enumerate()
|
||
{
|
||
if n >= MAX_WALK || out.len() >= limit {
|
||
break 'walk;
|
||
}
|
||
if !entry.file_type().is_some_and(|t| t.is_file()) {
|
||
continue;
|
||
}
|
||
let Ok(rel) = entry.path().strip_prefix(root) else { continue };
|
||
if matcher.is_match(rel) {
|
||
out.push(to_ipc(entry.path()));
|
||
}
|
||
}
|
||
}
|
||
out.sort();
|
||
Ok(out)
|
||
}
|
||
|
||
fn publish_diagnostics(&self, path: &Path, items: Vec<ide_core::Diagnostic>) -> Result<(), HostError> {
|
||
self.shared.publish_diagnostics(self.id(), path, items)
|
||
}
|
||
|
||
fn fetch(&self, request: HttpRequest, cancel: &CancellationToken) -> Result<HttpResponse, HostError> {
|
||
let timeout = request.timeout;
|
||
let builder = self.http_request(request)?.timeout(timeout);
|
||
let cancel = cancel.clone();
|
||
net_runtime().block_on(async move {
|
||
let send = async {
|
||
let response = builder.send().await.map_err(network_error)?;
|
||
let status = response.status().as_u16();
|
||
let headers = response_headers(&response);
|
||
let mut body = Vec::new();
|
||
let mut response = response;
|
||
while let Some(chunk) = response.chunk().await.map_err(|e| HostError::Failed(e.to_string()))? {
|
||
if body.len() + chunk.len() > MAX_RESPONSE {
|
||
return Err(HostError::Failed(format!("response larger than {} MiB", MAX_RESPONSE >> 20)));
|
||
}
|
||
body.extend_from_slice(&chunk);
|
||
}
|
||
Ok(HttpResponse { status, headers, body })
|
||
};
|
||
tokio::select! {
|
||
r = send => r,
|
||
() = cancel.cancelled() => Err(HostError::Cancelled),
|
||
}
|
||
})
|
||
}
|
||
|
||
fn fetch_stream(&self, request: HttpRequest, cancel: &CancellationToken) -> Result<HttpStreamHead, HostError> {
|
||
self.shared.http_streams.check(self.id())?;
|
||
// Only the wait for the headers is bounded: the body may keep coming.
|
||
let timeout = request.timeout;
|
||
let builder = self.http_request(request)?;
|
||
let cancel = cancel.clone();
|
||
let response = net_runtime().block_on(async move {
|
||
tokio::select! {
|
||
r = tokio::time::timeout(timeout, builder.send()) => r
|
||
.map_err(|_| HostError::Failed("timed out waiting for the response headers".into()))?
|
||
.map_err(network_error),
|
||
() = cancel.cancelled() => Err(HostError::Cancelled),
|
||
}
|
||
})?;
|
||
let status = response.status().as_u16();
|
||
let headers = response_headers(&response);
|
||
let id = self.shared.http_streams.insert(self.id(), response)?;
|
||
Ok(HttpStreamHead { id, status, headers })
|
||
}
|
||
|
||
fn http_stream_read(
|
||
&self,
|
||
id: u64,
|
||
max: usize,
|
||
timeout: Duration,
|
||
cancel: &CancellationToken,
|
||
) -> Result<(Vec<u8>, bool), HostError> {
|
||
self.shared.http_streams.read(self.id(), id, max, timeout, cancel)
|
||
}
|
||
|
||
fn http_stream_close(&self, id: u64) {
|
||
self.shared.http_streams.close(self.id(), id);
|
||
}
|
||
|
||
fn websocket_connect(
|
||
&self,
|
||
root: &Path,
|
||
request: ide_plugin::WebSocketRequest,
|
||
timeout: Duration,
|
||
) -> Result<u64, HostError> {
|
||
let secrets = self.shared.secrets.read().clone();
|
||
let plugin = self.id().to_owned();
|
||
let headers = request
|
||
.headers
|
||
.into_iter()
|
||
.map(|(name, value)| {
|
||
let value = fill_secrets(&value, |name| secrets.get(&plugin, name))?;
|
||
Ok((name, value))
|
||
})
|
||
.collect::<Result<Vec<_>, HostError>>()?;
|
||
net_runtime()
|
||
.block_on(self.shared.websockets.connect(
|
||
&plugin,
|
||
&to_ipc(root),
|
||
&request.url,
|
||
&headers,
|
||
&request.subprotocol,
|
||
timeout,
|
||
))
|
||
.map_err(|error| match error {
|
||
super::websocket::ConnectError::Invalid(message) => HostError::Invalid(message),
|
||
super::websocket::ConnectError::Failed(message) => HostError::Failed(message),
|
||
})
|
||
}
|
||
|
||
fn websocket_send(&self, connection: u64, bytes: Vec<u8>) -> Result<(), HostError> {
|
||
self.shared.websockets.send(self.id(), connection, bytes).map_err(HostError::Failed)
|
||
}
|
||
|
||
fn websocket_drain(&self, connection: u64, limit: usize) -> Result<ide_plugin::WebSocketBatch, HostError> {
|
||
self.shared.websockets.drain(self.id(), connection, limit).map_err(HostError::Invalid)
|
||
}
|
||
|
||
fn websocket_close(&self, connection: u64) {
|
||
let _ = self.shared.websockets.disconnect(self.id(), connection);
|
||
}
|
||
|
||
/// The `net:ask` answer: a host the user allowed with "always" is
|
||
/// remembered in the ledger (no dialog again, background included); a new
|
||
/// host is asked about in `client`'s window when there is one. Dismissal
|
||
/// and refusal both deny and throttle further questions.
|
||
fn net_ask(
|
||
&self,
|
||
subject: &Subject,
|
||
client: Option<&str>,
|
||
host: &str,
|
||
cancel: &CancellationToken,
|
||
) -> Result<Option<bool>, HostError> {
|
||
if cancel.is_cancelled() {
|
||
return Err(HostError::Cancelled);
|
||
}
|
||
if !self.pkg.manifest.declares(&Capability::NetAsk) {
|
||
return Ok(None);
|
||
}
|
||
let host = host.trim_end_matches('.').to_ascii_lowercase();
|
||
// A host the plugin derives from a setting is approved in Settings,
|
||
// not asked about here: pending or revoked, it stays denied without a
|
||
// dialog (#3809).
|
||
if self.pkg.manifest.capabilities.all().any(|cap| {
|
||
matches!(cap, Capability::NetSetting { http: false, key: _ })
|
||
&& self.setting_value(&setting_key(cap)).and_then(|v| setting_host(&v).ok()).flatten().as_deref()
|
||
== Some(host.as_str())
|
||
}) {
|
||
return Ok(None);
|
||
}
|
||
{
|
||
let ledger = self.shared.ledger.lock();
|
||
if let Some(grant) = ledger.get(subject)
|
||
&& grant.confirmed
|
||
&& grant.has(&Capability::NetAsk)
|
||
&& grant.net_hosts.contains(&host)
|
||
{
|
||
return Ok(Some(true));
|
||
}
|
||
}
|
||
if self.shared.session_net_hosts.lock().get(self.id()).is_some_and(|hosts| hosts.contains(&host)) {
|
||
return Ok(Some(true));
|
||
}
|
||
let Some(client) = client else { return Ok(None) };
|
||
{
|
||
let closed = self.shared.closed_clients.lock();
|
||
if closed.contains(client) {
|
||
return Ok(None);
|
||
}
|
||
}
|
||
if self.shared.prompts_blocked.lock().contains(self.id()) {
|
||
return Ok(None);
|
||
}
|
||
{
|
||
let mut prompts = self.shared.net_prompts.lock();
|
||
if prompts.get(self.id()).is_some_and(|until| until.is_none_or(|until| until > Instant::now())) {
|
||
return Ok(None);
|
||
}
|
||
prompts.insert(self.id().to_owned(), None);
|
||
}
|
||
let session_only = self.shared.net_hosts_session_only.load(Ordering::Relaxed);
|
||
let answer = self.ask(Some(client), Prompt::NetPermission { host: host.clone(), session_only }, cancel);
|
||
let result = match answer {
|
||
Ok(Some(keys)) if keys == ["session"] || keys == ["always"] => {
|
||
let mut ledger = self.shared.ledger.lock();
|
||
if cancel.is_cancelled() {
|
||
Err(HostError::Cancelled)
|
||
} else if ledger.get(subject).is_some_and(|g| g.confirmed && g.has(&Capability::NetAsk)) {
|
||
if session_only || keys == ["session"] {
|
||
self.shared.session_net_hosts.lock().entry(self.id().into()).or_default().insert(host);
|
||
} else {
|
||
ledger.remember_net_host(subject, &host);
|
||
if let Err(e) = ledger.save() {
|
||
tracing::warn!("saving plugin grants: {e}");
|
||
}
|
||
}
|
||
drop(ledger);
|
||
self.shared.sink.emit(None, PluginEvent::CommandsChanged);
|
||
Ok(Some(true))
|
||
} else {
|
||
Ok(None)
|
||
}
|
||
}
|
||
Ok(Some(keys)) if keys == ["once"] => Ok(Some(false)),
|
||
Ok(_) => Ok(None),
|
||
Err(e) => Err(e),
|
||
};
|
||
let mut prompts = self.shared.net_prompts.lock();
|
||
if matches!(result, Ok(Some(_))) {
|
||
prompts.remove(self.id());
|
||
} else {
|
||
prompts.insert(self.id().to_owned(), Some(Instant::now() + Duration::from_secs(30)));
|
||
}
|
||
result
|
||
}
|
||
|
||
fn secret_is_set(&self, name: &str) -> bool {
|
||
self.shared.secrets.read().get(self.id(), name).is_some()
|
||
}
|
||
|
||
fn secret_set(&self, name: &str, value: Option<&str>) -> Result<(), HostError> {
|
||
if !self.shared.ui.items(super::providers::SECRETS_POINT, Some(self.id())).iter().any(|s| s.key == name) {
|
||
return Err(HostError::NotFound(format!("undeclared secret {name:?}")));
|
||
}
|
||
self.shared.secrets.read().set(self.id(), name, value).map_err(HostError::Failed)
|
||
}
|
||
fn begin_loopback(&self, client: &str, path: &str) -> Result<(u64, String), HostError> {
|
||
let closed = self.shared.closed_clients.lock();
|
||
let blocked = self.shared.prompts_blocked.lock();
|
||
if closed.contains(client) || blocked.contains(self.id()) {
|
||
return Err(HostError::Cancelled);
|
||
}
|
||
self.shared.loopbacks.read().begin(self.id(), client, path)
|
||
}
|
||
fn wait_loopback(
|
||
&self,
|
||
id: u64,
|
||
timeout: Duration,
|
||
cancel: &CancellationToken,
|
||
) -> Result<Option<String>, HostError> {
|
||
self.shared.loopbacks.read().clone().wait(self.id(), id, timeout, cancel)
|
||
}
|
||
fn close_loopback(&self, id: u64) {
|
||
self.shared.loopbacks.read().close(self.id(), id);
|
||
}
|
||
fn ipc_connect(&self, name: &str, cancel: &CancellationToken, timeout: Duration) -> Result<u64, HostError> {
|
||
self.shared.connections.connect(self.id(), name, cancel, timeout)
|
||
}
|
||
fn ipc_write(
|
||
&self,
|
||
id: u64,
|
||
bytes: Vec<u8>,
|
||
cancel: &CancellationToken,
|
||
timeout: Duration,
|
||
) -> Result<(), HostError> {
|
||
self.shared.connections.write(self.id(), id, bytes, cancel, timeout)
|
||
}
|
||
fn ipc_read(
|
||
&self,
|
||
id: u64,
|
||
max: usize,
|
||
timeout: Duration,
|
||
cancel: &CancellationToken,
|
||
) -> Result<Vec<u8>, HostError> {
|
||
self.shared.connections.read(self.id(), id, max, timeout, cancel)
|
||
}
|
||
fn ipc_close(&self, id: u64) {
|
||
self.shared.connections.close(self.id(), id);
|
||
}
|
||
fn run_process(
|
||
&self,
|
||
cmd: ProcessCommand,
|
||
cwd: Option<&Path>,
|
||
stdin: Option<String>,
|
||
cancel: &CancellationToken,
|
||
timeout: Duration,
|
||
) -> Result<ProcessOutput, HostError> {
|
||
self.run_process_with_options(cmd, cwd, stdin, cancel, timeout, None)
|
||
}
|
||
|
||
fn run_process_with_options(
|
||
&self,
|
||
cmd: ProcessCommand,
|
||
cwd: Option<&Path>,
|
||
stdin: Option<String>,
|
||
cancel: &CancellationToken,
|
||
timeout: Duration,
|
||
options: Option<ProcessOptions>,
|
||
) -> Result<ProcessOutput, HostError> {
|
||
let max_output = options
|
||
.and_then(|o| o.max_output_bytes)
|
||
.map(|b| (b as usize).clamp(1 << 10, MAX_ALLOWED_OUTPUT))
|
||
.unwrap_or(MAX_OUTPUT);
|
||
let binary = self.approved_program_in_project(&cmd.program, cwd.or(cmd.approval_root.as_deref()))?;
|
||
let cwd = self.spawn_dir(cwd);
|
||
let mut command = StdCommand::new(&binary);
|
||
ide_core::process::prepare(&mut command);
|
||
append_process_env(&mut command, &cmd.env)?;
|
||
ide_core::process::own_group(&mut command);
|
||
let mut child = command
|
||
.args(&cmd.args)
|
||
.current_dir(&cwd)
|
||
.stdin(if stdin.is_some() { Stdio::piped() } else { Stdio::null() })
|
||
.stdout(Stdio::piped())
|
||
.stderr(Stdio::piped())
|
||
.spawn()
|
||
.map_err(|e| HostError::Failed(format!("{}: {e}", binary.display())))?;
|
||
if let (Some(input), Some(mut pipe)) = (stdin, child.stdin.take()) {
|
||
std::thread::spawn(move || {
|
||
use std::io::Write;
|
||
let _ = pipe.write_all(input.as_bytes());
|
||
});
|
||
}
|
||
let read = |pipe: Option<Box<dyn Read + Send>>| {
|
||
std::thread::spawn(move || {
|
||
let mut out = Vec::new();
|
||
let mut overflow = false;
|
||
if let Some(p) = pipe {
|
||
let mut p = p;
|
||
let _ = p.by_ref().take((max_output + 1) as u64).read_to_end(&mut out);
|
||
if out.len() > max_output {
|
||
overflow = true;
|
||
out.truncate(max_output);
|
||
}
|
||
let _ = std::io::copy(&mut p, &mut std::io::sink());
|
||
}
|
||
let mut text = String::from_utf8_lossy(&out).into_owned();
|
||
let mut end = text.len().min(max_output);
|
||
while !text.is_char_boundary(end) {
|
||
end -= 1;
|
||
}
|
||
text.truncate(end);
|
||
(text, overflow)
|
||
})
|
||
};
|
||
let stdout = read(child.stdout.take().map(|p| Box::new(p) as Box<dyn Read + Send>));
|
||
let stderr = read(child.stderr.take().map(|p| Box::new(p) as Box<dyn Read + Send>));
|
||
let deadline = Instant::now() + timeout;
|
||
let status = loop {
|
||
if let Some(status) = try_wait_process(&mut child).map_err(|e| HostError::Failed(e.to_string()))? {
|
||
break status;
|
||
}
|
||
if cancel.is_cancelled() || Instant::now() >= deadline {
|
||
ide_core::process::kill_tree(&mut child);
|
||
return Err(if cancel.is_cancelled() {
|
||
HostError::Cancelled
|
||
} else {
|
||
HostError::Failed(format!("{} timed out after {timeout:?}", cmd.program))
|
||
});
|
||
}
|
||
std::thread::sleep(Duration::from_millis(10));
|
||
};
|
||
let (stdout, stdout_overflow) = stdout.join().unwrap_or_default();
|
||
let (stderr, stderr_overflow) = stderr.join().unwrap_or_default();
|
||
if options.is_some() {
|
||
// run_captured fails explicitly if either stdout or stderr exceeds the configured limit.
|
||
if stdout_overflow || stderr_overflow {
|
||
let stream = match (stdout_overflow, stderr_overflow) {
|
||
(true, true) => "stdout and stderr",
|
||
(true, false) => "stdout",
|
||
(false, true) => "stderr",
|
||
(false, false) => unreachable!(),
|
||
};
|
||
let limit_mib = (max_output + (1 << 20) - 1) >> 20;
|
||
tracing::warn!(
|
||
plugin = %self.id(),
|
||
program = %cmd.program,
|
||
limit_mib,
|
||
"process {stream} exceeded limit"
|
||
);
|
||
return Err(HostError::Failed(format!("{} {stream} exceeded {} MiB limit", cmd.program, limit_mib)));
|
||
}
|
||
} else if stdout_overflow {
|
||
// Documented legacy behavior for run / run-command: stdout overflow fails, but
|
||
// stderr overflow is silently truncated to 16 MiB.
|
||
tracing::warn!(
|
||
plugin = %self.id(),
|
||
program = %cmd.program,
|
||
limit_mib = (max_output >> 20),
|
||
"process stdout exceeded limit"
|
||
);
|
||
return Err(HostError::Failed(format!("{} stdout exceeded 16 MiB limit", cmd.program)));
|
||
}
|
||
Ok(ProcessOutput { status: status.code(), stdout, stderr })
|
||
}
|
||
|
||
fn subscribe(&self, kind: EventKind) {
|
||
if self.shared.subscriptions.lock().set(self.id(), kind) {
|
||
self.shared.sink.emit(None, PluginEvent::SubscriptionsChanged);
|
||
}
|
||
}
|
||
|
||
fn subscribe_paths_with_options(
|
||
&self,
|
||
kind: &str,
|
||
globs: Vec<String>,
|
||
debounce_ms: Option<u32>,
|
||
) -> Result<(), HostError> {
|
||
let debounce = match debounce_ms {
|
||
Some(ms) if ms > 60_000 => return Err(HostError::Invalid("debounce must not exceed 60000 ms".into())),
|
||
Some(ms) => Some(Duration::from_millis(ms as u64)),
|
||
None => None,
|
||
};
|
||
let filter = super::event_paths::PathFilter::with_debounce(kind, globs, debounce)?;
|
||
if self.shared.subscriptions.lock().set_paths(self.id(), kind, filter) {
|
||
self.shared.sink.emit(None, PluginEvent::SubscriptionsChanged);
|
||
}
|
||
Ok(())
|
||
}
|
||
|
||
fn subscribe_paths(&self, kind: &str, globs: Vec<String>) -> Result<(), HostError> {
|
||
self.subscribe_paths_with_options(kind, globs, None)
|
||
}
|
||
|
||
fn project_generation(&self, root: &Path) -> Result<u64, HostError> {
|
||
self.shared.project_generation(root)
|
||
}
|
||
|
||
fn watch_paths(&self, client: &str, project: &Path, paths: Vec<PathBuf>) -> Result<(), HostError> {
|
||
self.shared.external_watches.set(self.id(), client, project, paths)
|
||
}
|
||
|
||
fn unsubscribe(&self, kind: &str) {
|
||
if self.shared.subscriptions.lock().unsubscribe(self.id(), kind) {
|
||
self.shared.sink.emit(None, PluginEvent::SubscriptionsChanged);
|
||
}
|
||
}
|
||
fn start_process(&self, cmd: ProcessCommand, cwd: Option<&Path>, client: Option<&str>) -> Result<u64, HostError> {
|
||
self.start_process_stream(cmd, cwd, client, None)
|
||
}
|
||
|
||
fn start_process_stream(
|
||
&self,
|
||
cmd: ProcessCommand,
|
||
cwd: Option<&Path>,
|
||
client: Option<&str>,
|
||
options: Option<ProcessOptions>,
|
||
) -> Result<u64, HostError> {
|
||
let binary = self.approved_program_in_project(&cmd.program, cwd.or(cmd.approval_root.as_deref()))?;
|
||
let cwd = self.spawn_dir(cwd);
|
||
if client.is_some_and(|c| self.shared.closed_clients.lock().contains(c)) {
|
||
return Err(HostError::Cancelled);
|
||
}
|
||
{
|
||
let processes = self.shared.processes.lock();
|
||
let running = processes.keys().filter(|(plugin, _)| plugin == self.id()).count();
|
||
if running >= MAX_PROCESSES {
|
||
return Err(HostError::Invalid(format!(
|
||
"at most {MAX_PROCESSES} processes may run at once: release one first"
|
||
)));
|
||
}
|
||
}
|
||
let mut command = StdCommand::new(&binary);
|
||
ide_core::process::prepare(&mut command);
|
||
append_process_env(&mut command, &cmd.env)?;
|
||
ide_core::process::own_group(&mut command);
|
||
let mut child = command
|
||
.args(&cmd.args)
|
||
.current_dir(&cwd)
|
||
// Always piped: `write` may come later, and a plugin that never
|
||
// writes closes it (or loses it when it releases the handle).
|
||
.stdin(Stdio::piped())
|
||
.stdout(Stdio::piped())
|
||
.stderr(Stdio::piped())
|
||
.spawn()
|
||
.map_err(|e| HostError::Failed(format!("{}: {e}", binary.display())))?;
|
||
let (stdout, stderr) = (child.stdout.take(), child.stderr.take());
|
||
let (mode, buffer_limit, max_total) = match options {
|
||
Some(opts) => {
|
||
let buf =
|
||
opts.buffer_bytes.map(|b| (b as usize).clamp(4 << 10, 4 << 20)).unwrap_or(DEFAULT_STREAM_BUFFER);
|
||
let total = opts
|
||
.max_output_bytes
|
||
.map(|b| (b as usize).clamp(1 << 20, MAX_ALLOWED_OUTPUT))
|
||
.unwrap_or(DEFAULT_STREAM_MAX_TOTAL);
|
||
(StreamMode::Lossless, buf, total)
|
||
}
|
||
None => (StreamMode::Tail, MAX_PENDING, usize::MAX),
|
||
};
|
||
let proc = Arc::new(Proc {
|
||
client: client.map(str::to_owned),
|
||
can_read: parking_lot::Condvar::new(),
|
||
state: Mutex::new(ProcState {
|
||
stdin: child.stdin.take(),
|
||
child,
|
||
stdout: Vec::new(),
|
||
stderr: Vec::new(),
|
||
status: None,
|
||
readers: 0,
|
||
waiters: 0,
|
||
mode,
|
||
buffer_limit,
|
||
max_total,
|
||
total_stdout: 0,
|
||
total_stderr: 0,
|
||
overflow_err: None,
|
||
stopped: false,
|
||
}),
|
||
});
|
||
if let Some(pipe) = stdout {
|
||
proc.read(pipe, Stream::Out);
|
||
}
|
||
if let Some(pipe) = stderr {
|
||
proc.read(pipe, Stream::Err);
|
||
}
|
||
let closed = self.shared.closed_clients.lock();
|
||
if client.is_some_and(|c| closed.contains(c)) {
|
||
drop(closed);
|
||
proc.kill();
|
||
return Err(HostError::Cancelled);
|
||
}
|
||
let id = self.shared.next_process.fetch_add(1, Ordering::Relaxed);
|
||
self.shared.processes.lock().insert((self.id().to_owned(), id), proc);
|
||
drop(closed);
|
||
Ok(id)
|
||
}
|
||
|
||
fn poll_process(&self, id: u64, max: usize) -> Result<ProcessChunk, HostError> {
|
||
let proc = self.process(id)?;
|
||
proc.reap()?;
|
||
proc.chunk(max)
|
||
}
|
||
|
||
fn write_process(
|
||
&self,
|
||
id: u64,
|
||
text: &str,
|
||
cancel: &CancellationToken,
|
||
timeout: Duration,
|
||
) -> Result<(), HostError> {
|
||
let proc = self.process(id)?;
|
||
proc.reap()?;
|
||
let mut pipe = {
|
||
let mut state = proc.state.lock();
|
||
if state.status.is_some() {
|
||
return Err(HostError::Failed("the process already ended".into()));
|
||
}
|
||
state.stdin.take().ok_or_else(|| HostError::Failed("its stdin is closed".into()))?
|
||
};
|
||
let input = text.to_owned();
|
||
let writer = proc.clone();
|
||
let (tx, rx) = std::sync::mpsc::sync_channel(1);
|
||
std::thread::spawn(move || {
|
||
use std::io::Write;
|
||
let result = pipe.write_all(input.as_bytes()).and_then(|()| pipe.flush());
|
||
let mut state = writer.state.lock();
|
||
if state.status.is_none() {
|
||
state.stdin = Some(pipe);
|
||
}
|
||
let _ = tx.send(result);
|
||
});
|
||
let deadline = Instant::now() + timeout;
|
||
loop {
|
||
if cancel.is_cancelled() || Instant::now() >= deadline {
|
||
proc.kill();
|
||
return Err(if cancel.is_cancelled() {
|
||
HostError::Cancelled
|
||
} else {
|
||
HostError::Failed("stdin write timed out".into())
|
||
});
|
||
}
|
||
match rx.recv_timeout(Duration::from_millis(10)) {
|
||
Ok(result) => return result.map_err(|e| HostError::Failed(format!("stdin: {e}"))),
|
||
Err(std::sync::mpsc::RecvTimeoutError::Timeout) => {}
|
||
Err(_) => return Err(HostError::Failed("stdin writer stopped".into())),
|
||
}
|
||
}
|
||
}
|
||
|
||
fn close_process_stdin(&self, id: u64) -> Result<(), HostError> {
|
||
let proc = self.process(id)?;
|
||
proc.reap()?;
|
||
let mut state = proc.state.lock();
|
||
if state.status.is_some() {
|
||
return Err(HostError::Failed("the process already ended".into()));
|
||
}
|
||
state.stdin = None;
|
||
Ok(())
|
||
}
|
||
|
||
fn release_process(&self, id: u64) {
|
||
let proc = self.shared.processes.lock().remove(&(self.id().to_owned(), id));
|
||
if let Some(proc) = proc {
|
||
proc.kill();
|
||
}
|
||
}
|
||
|
||
fn kill_process(&self, id: u64) -> Result<(), HostError> {
|
||
self.process(id)?.kill();
|
||
Ok(())
|
||
}
|
||
|
||
fn wait_process(
|
||
&self,
|
||
id: u64,
|
||
max: usize,
|
||
timeout: Duration,
|
||
cancel: &CancellationToken,
|
||
) -> Result<ProcessChunk, HostError> {
|
||
let proc = self.process(id)?;
|
||
// Track active waiters and wake reader threads so lossless streams continue
|
||
// draining into the retained buffer up to max_total without pipe deadlock.
|
||
struct WaitGuard<'a>(&'a Proc);
|
||
impl Drop for WaitGuard<'_> {
|
||
fn drop(&mut self) {
|
||
let mut state = self.0.state.lock();
|
||
state.waiters = state.waiters.saturating_sub(1);
|
||
self.0.can_read.notify_all();
|
||
}
|
||
}
|
||
let _guard = {
|
||
let mut state = proc.state.lock();
|
||
state.waiters += 1;
|
||
proc.can_read.notify_all();
|
||
WaitGuard(&proc)
|
||
};
|
||
let deadline = Instant::now() + timeout;
|
||
loop {
|
||
proc.reap()?;
|
||
let (ended, readers, overflow) = {
|
||
let state = proc.state.lock();
|
||
(state.status.is_some(), state.readers, state.overflow_err.clone())
|
||
};
|
||
if let Some(err) = overflow {
|
||
return Err(HostError::Failed(err));
|
||
}
|
||
// Its output is only complete once its pipes reached the end too.
|
||
if ended && readers == 0 {
|
||
break;
|
||
}
|
||
if cancel.is_cancelled() {
|
||
return Err(HostError::Cancelled);
|
||
}
|
||
if Instant::now() >= deadline {
|
||
break;
|
||
}
|
||
std::thread::sleep(Duration::from_millis(10));
|
||
}
|
||
proc.chunk(max)
|
||
}
|
||
|
||
fn task_begin(&self, client: Option<&str>, title: String, cancellable: bool, cancel: CancellationToken) -> u64 {
|
||
let id = self.shared.next_task.fetch_add(1, Ordering::Relaxed);
|
||
self.shared.tasks.lock().insert(id, TaskEntry { plugin: self.id().to_owned(), cancel, cancelled: false });
|
||
self.shared.sink.emit(
|
||
client,
|
||
PluginEvent::TaskStarted { plugin_id: self.id().to_owned(), task_id: id.to_string(), title, cancellable },
|
||
);
|
||
id
|
||
}
|
||
|
||
fn task_report(&self, id: u64, done: u32, total: Option<u32>, message: Option<String>) {
|
||
if !self.shared.tasks.lock().contains_key(&id) {
|
||
return;
|
||
}
|
||
self.shared.sink.emit(
|
||
None,
|
||
PluginEvent::TaskProgress {
|
||
plugin_id: self.id().to_owned(),
|
||
task_id: id.to_string(),
|
||
done,
|
||
total,
|
||
message,
|
||
},
|
||
);
|
||
}
|
||
|
||
fn task_cancelled(&self, id: u64) -> bool {
|
||
self.shared.tasks.lock().get(&id).is_some_and(|t| t.cancelled)
|
||
}
|
||
|
||
fn task_end(&self, id: u64) {
|
||
self.shared.end_task(id);
|
||
}
|
||
|
||
fn refresh_view(&self, view: &str) {
|
||
self.shared
|
||
.sink
|
||
.emit(None, PluginEvent::ViewChanged { plugin_id: self.id().to_owned(), view: view.to_owned() });
|
||
}
|
||
|
||
fn post_view_message(&self, view: &str, message: String) -> Result<(), HostError> {
|
||
let declared = super::ui_points::WEB_POINTS
|
||
.iter()
|
||
.any(|point| self.shared.ui.items(point, Some(self.id())).iter().any(|i| i.key == view));
|
||
if !declared {
|
||
return Err(HostError::NotFound(format!("the plugin declares no view {view:?}")));
|
||
}
|
||
let event = PluginEvent::ViewMessage { plugin_id: self.id().to_owned(), view: view.to_owned(), message };
|
||
self.shared.sink.emit(None, event);
|
||
Ok(())
|
||
}
|
||
|
||
fn open_terminal(
|
||
&self,
|
||
client: Option<&str>,
|
||
title: String,
|
||
program: &str,
|
||
args: Vec<String>,
|
||
cwd: Option<&Path>,
|
||
) -> Result<(), HostError> {
|
||
let binary = self.approved_program_in_project(program, cwd)?;
|
||
let cwd = cwd.map(Path::to_path_buf).unwrap_or_else(|| self.shared.data_dir.join(self.id()));
|
||
let _ = std::fs::create_dir_all(&cwd);
|
||
let event = PluginEvent::OpenTerminal {
|
||
plugin_id: self.id().to_owned(),
|
||
title,
|
||
program: binary.into(),
|
||
args,
|
||
cwd: cwd.into(),
|
||
};
|
||
self.shared.sink.emit(client, event);
|
||
Ok(())
|
||
}
|
||
|
||
fn open_location(&self, client: Option<&str>, path: &Path, line: u32, column: u32) -> Result<(), HostError> {
|
||
let event = PluginEvent::OpenLocation {
|
||
plugin_id: self.id().to_owned(),
|
||
path: path.to_path_buf().into(),
|
||
line,
|
||
column,
|
||
};
|
||
self.shared.sink.emit(client, event);
|
||
Ok(())
|
||
}
|
||
|
||
fn open_log_tab(
|
||
&self,
|
||
client: &str,
|
||
key: String,
|
||
title: String,
|
||
log: ide_plugin::ServiceLog,
|
||
) -> Result<(), HostError> {
|
||
// Fails now, rather than in the tab, when the approved file is gone or changed.
|
||
self.approved_program_in_project(&log.program, self.shared.project_of(client).as_deref())?;
|
||
{
|
||
let mut all = self.shared.log_tabs.lock();
|
||
let tabs = all.entry((self.id().to_owned(), client.to_owned())).or_default();
|
||
tabs.retain(|(k, _)| *k != key);
|
||
tabs.push((key.clone(), log.clone()));
|
||
if tabs.len() > MAX_LOG_TABS {
|
||
tabs.remove(0);
|
||
}
|
||
}
|
||
let event = PluginEvent::OpenLogTab { plugin_id: self.id().to_owned(), key, title, log };
|
||
self.shared.sink.emit(Some(client), event);
|
||
Ok(())
|
||
}
|
||
|
||
fn locale(&self) -> String {
|
||
self.shared.locale.read().clone()
|
||
}
|
||
|
||
fn setting(&self, root: Option<&Path>, key: &str) -> Option<String> {
|
||
let schema: Vec<super::ui_points::SettingSchema> = self
|
||
.shared
|
||
.ui
|
||
.items(super::ui_points::SETTINGS, Some(self.id()))
|
||
.into_iter()
|
||
.find_map(|i| serde_json::from_value(i.decl.get("properties")?.clone()).ok())?;
|
||
let setting = schema.into_iter().find(|s| s.key == key)?;
|
||
let user = self.shared.workspace().map(|w| w.plugin_settings(self.id(), root)).unwrap_or_default();
|
||
let value = user.get(key).filter(|v| setting.accepts(v)).cloned().unwrap_or(setting.default);
|
||
serde_json::to_string(&value).ok()
|
||
}
|
||
|
||
fn plugin_dir(&self) -> Result<PathBuf, HostError> {
|
||
ide_extension::validate_plugin_id(self.id()).map_err(HostError::Invalid)?;
|
||
std::fs::create_dir_all(&self.shared.data_dir).map_err(failed)?;
|
||
let data = self.shared.data_dir.join(self.id());
|
||
ide_vfs::create_private_dir(&data).map_err(failed)?;
|
||
let files = data.join("files");
|
||
ide_vfs::create_private_dir(&files).map_err(failed)?;
|
||
dunce::canonicalize(files).map_err(failed)
|
||
}
|
||
|
||
fn install_dir(&self) -> Result<PathBuf, HostError> {
|
||
dunce::canonicalize(&self.pkg.dir).map_err(failed)
|
||
}
|
||
|
||
fn read_text_file(&self, real: &Path, max: usize) -> Result<String, HostError> {
|
||
// Unsaved editor contents win, as they do for project files.
|
||
if let Some(text) = self.shared.workspace().and_then(|w| w.buffer_text(real)) {
|
||
return Ok(text);
|
||
}
|
||
let meta = std::fs::metadata(real).map_err(|e| not_a_file(real, e))?;
|
||
if !meta.is_file() {
|
||
return Err(HostError::Invalid(format!("{}: not a file", real.display())));
|
||
}
|
||
if meta.len() > max as u64 {
|
||
return Err(too_large(real, max));
|
||
}
|
||
let loaded = ide_vfs::load_text(real).map_err(failed)?;
|
||
if loaded.binary {
|
||
return Err(HostError::Invalid(format!("{}: not text; read it as bytes", real.display())));
|
||
}
|
||
Ok(loaded.text)
|
||
}
|
||
|
||
fn write_text_file(&self, real: &Path, text: &str) -> Result<(), HostError> {
|
||
check_unsaved(self.shared.workspace(), real)?;
|
||
// Write back in the file's own encoding, so a GBK properties file stays
|
||
// one; a file that is not there yet becomes UTF-8.
|
||
let (encoding, has_bom) = match ide_vfs::load_text(real) {
|
||
Ok(loaded) if loaded.binary => {
|
||
return Err(HostError::Invalid(format!("{}: not text; write it as bytes", real.display())));
|
||
}
|
||
Ok(loaded) => (loaded.encoding, loaded.has_bom),
|
||
Err(e) if e.kind == ide_core::error::ErrorKind::NotFound => ("utf-8".to_string(), false),
|
||
Err(e) => return Err(not_writable(real, e)),
|
||
};
|
||
let private = self.plugin_dir().is_ok_and(|dir| real.starts_with(dir));
|
||
check_write_path(real)?;
|
||
if private {
|
||
let bytes = ide_vfs::encode(text, &encoding, has_bom).map_err(|e| not_writable(real, e))?;
|
||
ide_vfs::write_atomic_private(real, &bytes).map(drop).map_err(|e| not_writable(real, e))
|
||
} else {
|
||
ide_vfs::save_text(real, text, &encoding, has_bom).map(drop).map_err(|e| not_writable(real, e))
|
||
}
|
||
}
|
||
|
||
fn write_bytes_file(&self, real: &Path, bytes: &[u8]) -> Result<(), HostError> {
|
||
check_unsaved(self.shared.workspace(), real)?;
|
||
let private = self.plugin_dir().is_ok_and(|dir| real.starts_with(dir));
|
||
check_write_path(real)?;
|
||
if private {
|
||
ide_vfs::write_atomic_private(real, bytes).map(drop).map_err(|e| not_writable(real, e))
|
||
} else {
|
||
ide_vfs::write_atomic(real, bytes).map(drop).map_err(|e| not_writable(real, e))
|
||
}
|
||
}
|
||
}
|
||
|
||
/// Approved write paths are already resolved. Refuse substitutions since approval.
|
||
fn check_write_path(path: &Path) -> Result<(), HostError> {
|
||
let parent = path.parent().ok_or_else(|| HostError::Invalid("no directory to put it in".into()))?;
|
||
let checked = ide_plugin::resolve_writable_in_root(parent, &to_ipc(path)).map_err(HostError::Invalid)?;
|
||
if checked != path {
|
||
return Err(HostError::Invalid(format!("{}: file path changed before writing", path.display())));
|
||
}
|
||
Ok(())
|
||
}
|
||
|
||
/// A file that is not there, or cannot be read, as the plugin sees it.
|
||
fn not_a_file(path: &Path, e: std::io::Error) -> HostError {
|
||
match e.kind() {
|
||
std::io::ErrorKind::NotFound => HostError::NotFound(format!("{}: no such file", path.display())),
|
||
std::io::ErrorKind::PermissionDenied => HostError::Denied(format!("{}: {}", path.display(), "not readable")),
|
||
_ => failed(e),
|
||
}
|
||
}
|
||
|
||
fn too_large(path: &Path, max: usize) -> HostError {
|
||
HostError::Invalid(format!("{}: file is larger than {} MiB", path.display(), max >> 20))
|
||
}
|
||
|
||
/// Refuses to replace a file the editor holds unsaved changes for: the write
|
||
/// would lose them.
|
||
fn check_unsaved(workspace: Option<Arc<dyn PluginWorkspace>>, path: &Path) -> Result<(), HostError> {
|
||
match workspace {
|
||
Some(workspace) if workspace.buffer_dirty(path) => Err(HostError::Invalid(format!(
|
||
"{}: the editor has unsaved changes for it; save them first",
|
||
path.display()
|
||
))),
|
||
_ => Ok(()),
|
||
}
|
||
}
|
||
|
||
/// A write that did not land, as the plugin sees it.
|
||
fn not_writable(path: &Path, e: ide_core::error::IdeError) -> HostError {
|
||
let message = format!("{}: {}", path.display(), e.message);
|
||
match e.kind {
|
||
ide_core::error::ErrorKind::NotFound => HostError::NotFound(message),
|
||
ide_core::error::ErrorKind::PermissionDenied => HostError::Denied(message),
|
||
ide_core::error::ErrorKind::InvalidInput => HostError::Invalid(message),
|
||
_ => HostError::Failed(message),
|
||
}
|
||
}
|
||
|
||
/// Notification buttons must name commands the plugin declares.
|
||
fn check_actions(api: &PluginApi, actions: &[ide_plugin::NotificationAction]) -> Result<(), HostError> {
|
||
let commands = if actions.is_empty() { Vec::new() } else { api.shared.ui.items("commands", Some(api.id())) };
|
||
match actions.iter().find(|a| !commands.iter().any(|c| c.key == a.command)) {
|
||
Some(a) => Err(HostError::NotFound(format!("the plugin declares no command {:?}", a.command))),
|
||
None => Ok(()),
|
||
}
|
||
}
|
||
|
||
/// [`PluginWorkspace`] over the desktop host's open projects.
|
||
pub struct HostWorkspace(pub std::sync::Weak<crate::Host>);
|
||
|
||
impl HostWorkspace {
|
||
fn project_for(&self, client: Option<&str>, root: &Path) -> Option<Arc<crate::project::Project>> {
|
||
let host = self.0.upgrade()?;
|
||
client
|
||
.and_then(|w| host.project(w).ok())
|
||
.filter(|p| p.root == root)
|
||
.or_else(|| host.windows_for_root(root).first().and_then(|w| host.project(w).ok()))
|
||
// A project window group's member root: its files belong to the
|
||
// group's project, like everywhere else in the editor.
|
||
.or_else(|| {
|
||
host.projects()
|
||
.into_iter()
|
||
.find(|p| crate::project::workspace_member_roots(&p.root).iter().any(|m| m.as_path() == root))
|
||
})
|
||
}
|
||
fn projects(&self) -> Vec<Arc<crate::project::Project>> {
|
||
self.0.upgrade().map(|h| h.projects()).unwrap_or_default()
|
||
}
|
||
|
||
/// The open project whose workspace group contains `root` (the group root
|
||
/// itself matches `p.root == root` and needs no member lookup).
|
||
fn project_covering(&self, root: &Path) -> Option<Arc<crate::project::Project>> {
|
||
self.projects().into_iter().find(|p| {
|
||
p.root == root || crate::project::workspace_member_roots(&p.root).iter().any(|m| m.as_path() == root)
|
||
})
|
||
}
|
||
|
||
/// The project holding an open document at `path`, and its id.
|
||
fn open_document(&self, path: &Path) -> Option<(Arc<crate::project::Project>, ide_core::DocId)> {
|
||
self.projects().into_iter().find_map(|p| p.documents.by_path(path).map(|doc| (p, doc)))
|
||
}
|
||
}
|
||
|
||
impl PluginWorkspace for HostWorkspace {
|
||
fn database_host(&self) -> Option<Arc<crate::Host>> {
|
||
self.0.upgrade()
|
||
}
|
||
fn run_configuration(&self, client: &str, plugin: &str, config: &str) -> Result<ide_run::RunConfig, HostError> {
|
||
let host = self.0.upgrade().ok_or(HostError::Cancelled)?;
|
||
let prefix = format!("{}{}:", ide_run::EXTERNAL_PREFIX, plugin);
|
||
let id = if config.starts_with(&prefix) { config.to_owned() } else { format!("{prefix}{config}") };
|
||
let project = host.project(client).map_err(failed)?;
|
||
project.require_trust().map_err(failed)?;
|
||
let plugins = host.plugin_host().ok_or(HostError::Cancelled)?;
|
||
let detected = plugins
|
||
.run_detection()
|
||
.detect(&project.root)
|
||
.into_iter()
|
||
.find(|c| c.id == id)
|
||
.ok_or_else(|| HostError::NotFound("no owned run configuration with this id".into()))?;
|
||
let mut config = ide_run::load_user_configs(&project.root)
|
||
.map_err(failed)?
|
||
.into_iter()
|
||
.find(|c| c.id == id)
|
||
.unwrap_or(detected);
|
||
ide_sdk::apply::apply_to_run_configs(
|
||
std::slice::from_mut(&mut config),
|
||
&project.root,
|
||
&project.settings.effective(),
|
||
);
|
||
if config.debug_adapter.is_none()
|
||
&& let Some(language) = config.extras.params.get(super::run_detectors::DEBUG_LANGUAGE)
|
||
{
|
||
config.debug_adapter = project
|
||
.languages
|
||
.get(&ide_core::LanguageId::new(language.as_str()))
|
||
.and_then(|l| l.debugger)
|
||
.map(|d| d.adapter);
|
||
}
|
||
if config.debug_adapter.is_none() && !config.extras.params.contains_key(super::run_detectors::DEBUG_ATTACH) {
|
||
let cwd = config.cwd.as_ref().map(|p| p.as_ref()).unwrap_or(&project.root);
|
||
config.debug_adapter =
|
||
config.args.iter().map(|arg| cwd.join(arg)).filter(|path| path.is_file()).find_map(|path| {
|
||
let language = project.languages.detect(&path, None);
|
||
project.languages.get(&language)?.debugger.map(|d| d.adapter)
|
||
});
|
||
}
|
||
Ok(config)
|
||
}
|
||
|
||
fn fork_agent_session(&self, client: &str, agent: &str, session_id: &str, cwd: &Path) -> Result<String, HostError> {
|
||
let host = self.0.upgrade().ok_or(HostError::Cancelled)?;
|
||
let project = host.project(client).map_err(failed)?;
|
||
project.require_trust().map_err(failed)?;
|
||
// Scans the recorded transcripts on disk; plugin calls already run off the UI thread.
|
||
Ok(project.agents.fork_session(agent, session_id, cwd).map_err(failed)?.id)
|
||
}
|
||
|
||
fn buffer_text_in(&self, client: Option<&str>, path: &Path) -> Option<String> {
|
||
client
|
||
.and_then(|w| self.0.upgrade()?.project(w).ok()?.documents.text_of(path))
|
||
.or_else(|| self.buffer_text(path))
|
||
}
|
||
fn document_in(&self, client: &str, path: &Path) -> Option<(u64, String)> {
|
||
let project = self.0.upgrade()?.project(client).ok()?;
|
||
let doc = project.documents.by_path(path)?;
|
||
let snapshot = project.documents.snapshot(doc).ok()?;
|
||
Some((u64::from(snapshot.version), snapshot.text))
|
||
}
|
||
fn open_editors(&self, client: &str) -> Result<Vec<ide_plugin::OpenEditor>, HostError> {
|
||
let host = self.0.upgrade().ok_or(HostError::Cancelled)?;
|
||
let project = host.project(client).map_err(failed)?;
|
||
let active = host.mcp_reports.focused(client).map(|e| e.path.into_path_buf());
|
||
Ok(host
|
||
.mcp_reports
|
||
.editors(&[client.into()])
|
||
.into_iter()
|
||
.map(|path| {
|
||
let language = project
|
||
.languages
|
||
.detect(&path, project.documents.text_of(&path).as_deref().and_then(|t| t.lines().next()));
|
||
ide_plugin::OpenEditor {
|
||
active: active.as_ref() == Some(&path),
|
||
dirty: project
|
||
.documents
|
||
.by_path(&path)
|
||
.is_some_and(|doc| project.documents.is_dirty(doc).unwrap_or(false)),
|
||
language: language.to_string(),
|
||
path: to_ipc(&path),
|
||
}
|
||
})
|
||
.collect())
|
||
}
|
||
fn search_text(
|
||
&self,
|
||
client: Option<&str>,
|
||
root: &Path,
|
||
query: ide_plugin::TextQuery,
|
||
limit: u32,
|
||
cancel: &CancellationToken,
|
||
) -> Result<ide_plugin::TextSearch, HostError> {
|
||
let host = self.0.upgrade().ok_or(HostError::Cancelled)?;
|
||
let project = self.project_for(client, root).ok_or_else(|| HostError::NotFound("project closed".into()))?;
|
||
// Matches must land in the project root or, for a project window group,
|
||
// in any of its members.
|
||
let mut roots = vec![root.to_path_buf()];
|
||
roots.extend(crate::project::workspace_member_roots(root));
|
||
if limit == 0 {
|
||
return Ok(ide_plugin::TextSearch::default());
|
||
}
|
||
let mut query = ide_search::SearchQuery {
|
||
pattern: query.pattern,
|
||
is_regex: query.regex,
|
||
case_sensitive: query.case_sensitive,
|
||
whole_word: query.whole_word,
|
||
include: query.include,
|
||
exclude: query.exclude,
|
||
scope: ide_search::SearchScope::Project,
|
||
max_matches: limit.min(10_000).saturating_add(1),
|
||
context: Default::default(),
|
||
include_excluded: false,
|
||
skip_large_files: false,
|
||
};
|
||
query.exclude.extend(project.search_excludes());
|
||
let files = Mutex::new(Vec::new());
|
||
let summary = crate::ops::search::search_project(&project, &host.syntax(), &query, cancel, &|file| {
|
||
files.lock().push(file)
|
||
})
|
||
.map_err(failed)?;
|
||
if summary.cancelled || cancel.is_cancelled() {
|
||
return Err(HostError::Cancelled);
|
||
}
|
||
let mut out = ide_plugin::TextSearch { matches: Vec::new(), limit_hit: summary.limit_hit };
|
||
let mut bytes = 0usize;
|
||
for file in files.into_inner() {
|
||
if cancel.is_cancelled() {
|
||
return Err(HostError::Cancelled);
|
||
}
|
||
// Restrict project.read to the project root and its workspace
|
||
// members, including symlink targets.
|
||
let Ok(path) = ide_plugin::resolve_in_roots(&roots, &to_ipc(&file.path.0)) else {
|
||
continue;
|
||
};
|
||
let text = project.documents.text_of(&path).or_else(|| {
|
||
let loaded = project.documents.load_file_bounded(&path, ide_plugin::MAX_READ_BYTES as usize).ok()?;
|
||
(!loaded.binary && !loaded.too_large)
|
||
.then(|| ide_text::normalize_line_endings(&loaded.text).into_owned())
|
||
});
|
||
let Some(text) = text else {
|
||
out.limit_hit = true;
|
||
continue;
|
||
};
|
||
let doc = ide_text::Document::new(&text);
|
||
for (start, end) in ide_search::match_ranges(&text, &query).map_err(failed)? {
|
||
if out.matches.len() >= limit as usize {
|
||
out.limit_hit = true;
|
||
return Ok(out);
|
||
}
|
||
let range = ide_core::Range::new(doc.byte_to_pos(start), doc.byte_to_pos(end));
|
||
let line = text[..start].rfind('\n').map_or(0, |i| i + 1);
|
||
let end_line = text[start..].find('\n').map_or(text.len(), |i| start + i);
|
||
let preview = text[line..end_line].trim_end_matches('\r');
|
||
let mut cut = preview.len().min(4096);
|
||
while !preview.is_char_boundary(cut) {
|
||
cut -= 1;
|
||
}
|
||
let preview = &preview[..cut];
|
||
bytes += preview.len() + path.as_os_str().len() + 64;
|
||
if bytes > 4 << 20 {
|
||
out.limit_hit = true;
|
||
return Ok(out);
|
||
}
|
||
out.matches.push(ide_plugin::TextMatch {
|
||
location: ide_plugin::Location { path: path.clone(), range },
|
||
line: preview.into(),
|
||
});
|
||
}
|
||
}
|
||
Ok(out)
|
||
}
|
||
fn problems(&self, client: &str, path: Option<&Path>) -> Result<Vec<ide_plugin::FileProblems>, HostError> {
|
||
// The host records every `Diagnostics` event it emits (language servers,
|
||
// builds, spelling, plugin providers), so this sees a file's problems
|
||
// whatever window opened it — not just the ones its UI reported.
|
||
let host = self.0.upgrade().ok_or(HostError::Cancelled)?;
|
||
host.project(client).map_err(failed)?;
|
||
let mut out = Vec::new();
|
||
for (file, source, diagnostics) in host.diagnostics.problems() {
|
||
if path.is_some_and(|p| p != file) {
|
||
continue;
|
||
}
|
||
out.push(ide_plugin::FileProblems { path: to_ipc(&file), source, diagnostics });
|
||
}
|
||
Ok(out)
|
||
}
|
||
|
||
fn projects(&self) -> Vec<PathBuf> {
|
||
let mut roots: Vec<PathBuf> = HostWorkspace::projects(self)
|
||
.iter()
|
||
.flat_map(|p| std::iter::once(p.root.clone()).chain(crate::project::workspace_member_roots(&p.root)))
|
||
.collect();
|
||
roots.sort();
|
||
roots.dedup();
|
||
roots
|
||
}
|
||
|
||
fn member_roots(&self, root: &Path) -> Vec<PathBuf> {
|
||
crate::project::workspace_member_roots(root)
|
||
}
|
||
|
||
fn buffer_text(&self, path: &Path) -> Option<String> {
|
||
HostWorkspace::projects(self).iter().find_map(|p| p.documents.text_of(path))
|
||
}
|
||
|
||
fn buffer_dirty(&self, path: &Path) -> bool {
|
||
let Some((project, doc)) = self.open_document(path) else { return false };
|
||
project.documents.is_dirty(doc).unwrap_or(false)
|
||
}
|
||
|
||
fn indexed_files(&self, root: &Path, matches: &dyn Fn(&Path) -> bool, limit: usize) -> Option<Vec<PathBuf>> {
|
||
// The index covering `root`: the project rooted there, or the workspace
|
||
// group project whose member it is (a member's files are in the group's).
|
||
HostWorkspace::project_covering(self, root)?.index.files_matching(root, matches, limit)
|
||
}
|
||
|
||
fn symbols(
|
||
&self,
|
||
root: &Path,
|
||
query: &str,
|
||
limit: usize,
|
||
cancel: &CancellationToken,
|
||
deadline: Instant,
|
||
) -> Result<Vec<ide_plugin::SymbolInfo>, HostError> {
|
||
let project = HostWorkspace::project_covering(self, root)
|
||
.ok_or_else(|| HostError::NotFound("the project is no longer open".into()))?;
|
||
let stopped = || cancel.is_cancelled() || Instant::now() >= deadline;
|
||
let interrupted = || {
|
||
if cancel.is_cancelled() {
|
||
HostError::Cancelled
|
||
} else {
|
||
HostError::Failed("symbol search timed out".into())
|
||
}
|
||
};
|
||
// The symbols of the requested root and, for a project window group, of
|
||
// its members (a member's symbols are in the group's index).
|
||
let members = crate::project::workspace_member_roots(root);
|
||
let in_scope = |path: &Path| path.starts_with(root) || members.iter().any(|m| path.starts_with(m));
|
||
let symbols = project
|
||
.index
|
||
.find_symbols_until(query, ide_index::SymbolFilter::All, limit.min(1000), &in_scope, &stopped)
|
||
.ok_or_else(interrupted)?;
|
||
let mut out = Vec::new();
|
||
for s in symbols {
|
||
if stopped() {
|
||
return Err(interrupted());
|
||
}
|
||
out.push(ide_plugin::SymbolInfo {
|
||
name: s.name,
|
||
kind: serde_json::to_value(s.kind)
|
||
.ok()
|
||
.and_then(|k| k.as_str().map(str::to_owned))
|
||
.unwrap_or_else(|| "unknown".into()),
|
||
container: s.container,
|
||
location: ide_plugin::Location { path: s.path.0, range: s.range },
|
||
});
|
||
}
|
||
if stopped() {
|
||
return Err(interrupted());
|
||
}
|
||
Ok(out)
|
||
}
|
||
|
||
fn document(&self, path: &Path) -> Option<(u64, String)> {
|
||
let (project, doc) = self.open_document(path)?;
|
||
let snap = project.documents.snapshot(doc).ok()?;
|
||
Some((u64::from(snap.version), snap.text))
|
||
}
|
||
|
||
fn apply_edits(&self, path: &Path, version: u64, edits: &[TextEdit], background: bool) -> Result<u64, HostError> {
|
||
let (project, doc) =
|
||
self.open_document(path).ok_or_else(|| HostError::NotFound(format!("{} is not open", path.display())))?;
|
||
let version = u32::try_from(version).map_err(|_| HostError::Stale)?;
|
||
let applied = if background {
|
||
project.documents.apply_background_edits_at(doc, version, edits)
|
||
} else {
|
||
project.documents.apply_edits_at(doc, version, edits)
|
||
};
|
||
match applied {
|
||
Ok(Some(v)) => Ok(u64::from(v)),
|
||
Ok(None) => Err(HostError::Stale),
|
||
Err(e) if e.kind == ide_core::error::ErrorKind::InvalidInput => Err(HostError::Invalid(e.message)),
|
||
Err(e) => Err(HostError::Failed(e.message)),
|
||
}
|
||
}
|
||
|
||
fn lsp_request(
|
||
&self,
|
||
root: &Path,
|
||
server: &str,
|
||
method: &str,
|
||
params: Option<serde_json::Value>,
|
||
notify: bool,
|
||
cancel: &CancellationToken,
|
||
timeout: Duration,
|
||
) -> Result<serde_json::Value, HostError> {
|
||
use ide_core::error::ErrorKind;
|
||
let host = self.0.upgrade().ok_or(HostError::Cancelled)?;
|
||
let project = HostWorkspace::project_covering(self, root)
|
||
.ok_or_else(|| HostError::NotFound("the project is no longer open".into()))?;
|
||
let result = if notify {
|
||
project.lsp.server_notify(root, server, method, params).map(|()| serde_json::Value::Null)
|
||
} else {
|
||
host.runtime.block_on(project.lsp.server_request(root, server, method, params, cancel, timeout))
|
||
};
|
||
result.map_err(|e| match e.kind {
|
||
// A server that stopped while the request was on its way is as absent as one never started.
|
||
ErrorKind::NotFound | ErrorKind::Unavailable => HostError::NotFound(e.message),
|
||
ErrorKind::InvalidInput => HostError::Invalid(e.message),
|
||
ErrorKind::Cancelled => HostError::Cancelled,
|
||
_ => HostError::Failed(e.message),
|
||
})
|
||
}
|
||
|
||
fn build_graph(&self, root: &Path) -> Result<super::BuildGraph, HostError> {
|
||
let host = self.0.upgrade().ok_or(HostError::Cancelled)?;
|
||
let project = HostWorkspace::project_covering(self, root)
|
||
.ok_or_else(|| HostError::NotFound("the project is no longer open".into()))?;
|
||
crate::ops::build::analyzed_dependencies(&project, &host.config_dir, root).map_err(failed)
|
||
}
|
||
|
||
fn trusted(&self, root: &Path) -> bool {
|
||
// A workspace member is as trusted as the group's project it was opened with.
|
||
HostWorkspace::project_covering(self, root).is_some_and(|p| p.is_trusted())
|
||
}
|
||
|
||
fn repositories(&self, root: &Path) -> Result<ide_vcs::Repos, HostError> {
|
||
Ok(self
|
||
.project_covering(root)
|
||
.ok_or_else(|| HostError::NotFound("the project is no longer open".into()))?
|
||
.repos())
|
||
}
|
||
|
||
fn git_executable(&self, root: &Path) -> Option<PathBuf> {
|
||
HostWorkspace::project_covering(self, root)?.git_executable()
|
||
}
|
||
|
||
fn git_enabled(&self, root: &Path) -> bool {
|
||
match HostWorkspace::project_covering(self, root) {
|
||
Some(project) => project.git_enabled(),
|
||
None => self.0.upgrade().is_none_or(|host| host.global_settings.read_effective(|s| s.vcs.git.enabled)),
|
||
}
|
||
}
|
||
|
||
fn project_of(&self, window: &str) -> Option<PathBuf> {
|
||
self.0.upgrade()?.project(window).ok().map(|p| p.root.clone())
|
||
}
|
||
|
||
fn project_windows(&self, root: &Path) -> Vec<String> {
|
||
let Some(host) = self.0.upgrade() else { return Vec::new() };
|
||
let mut windows = host.windows_for_root(root);
|
||
host.mcp_reports.most_recently_focused_first(&mut windows);
|
||
windows
|
||
}
|
||
|
||
fn publish_diagnostics(
|
||
&self,
|
||
source: &str,
|
||
path: &Path,
|
||
items: Vec<ide_core::Diagnostic>,
|
||
) -> Result<(), HostError> {
|
||
// Any open project may publish problems for any file: annotations follow the
|
||
// file, not the window (an editor can show a file outside every project root).
|
||
let projects = HostWorkspace::projects(self);
|
||
let project = projects
|
||
.iter()
|
||
.filter(|p| path.starts_with(&p.root))
|
||
.max_by_key(|p| p.root.as_os_str().len())
|
||
// A project window group's member files report through the group's project.
|
||
.or_else(|| {
|
||
projects
|
||
.iter()
|
||
.find(|p| crate::project::workspace_member_roots(&p.root).iter().any(|m| path.starts_with(m)))
|
||
})
|
||
.or_else(|| projects.first())
|
||
.ok_or_else(|| HostError::NotFound("no project is open".into()))?;
|
||
project.sink.emit(ide_core::IdeEvent::Diagnostics {
|
||
path: path.to_path_buf().into(),
|
||
source: format!("plugin:{source}"),
|
||
items,
|
||
});
|
||
Ok(())
|
||
}
|
||
|
||
fn vcs_encoding(&self, path: &Path) -> ide_vcs::TextEncoding {
|
||
HostWorkspace::projects(self)
|
||
.into_iter()
|
||
.filter(|p| path.starts_with(&p.root))
|
||
.max_by_key(|p| p.root.as_os_str().len())
|
||
.or_else(|| {
|
||
HostWorkspace::projects(self)
|
||
.into_iter()
|
||
.find(|p| crate::project::workspace_member_roots(&p.root).iter().any(|m| path.starts_with(m)))
|
||
})
|
||
.and_then(|p| p.vcs_encoding(path, None).ok())
|
||
.unwrap_or_else(|| encoding_rs::UTF_8.into())
|
||
}
|
||
|
||
fn plugin_settings(&self, plugin: &str, root: Option<&Path>) -> serde_json::Map<String, serde_json::Value> {
|
||
let Some(host) = self.0.upgrade() else { return serde_json::Map::new() };
|
||
// A member root reads the settings of the group's project, like the
|
||
// group's own root does.
|
||
let settings = match root.and_then(|r| host.projects().into_iter().find(|p| p.root == r)) {
|
||
Some(p) => p.settings.effective(),
|
||
None => match root.map(|r| HostWorkspace::project_covering(self, r)) {
|
||
Some(Some(p)) => p.settings.effective(),
|
||
_ => host.global_settings.effective(),
|
||
},
|
||
};
|
||
settings.plugins.get(plugin).cloned().unwrap_or_default()
|
||
}
|
||
}
|
||
|
||
#[cfg(test)]
|
||
mod tests {
|
||
use super::*;
|
||
|
||
#[test]
|
||
fn terminal_directories_stay_inside_the_project() {
|
||
let tmp = tempfile::tempdir().unwrap();
|
||
let root = dunce::canonicalize(tmp.path()).unwrap().join("project");
|
||
std::fs::create_dir_all(root.join("sub/deeper")).unwrap();
|
||
std::fs::write(root.join("file.txt"), "").unwrap();
|
||
let outside = tmp.path().join("outside");
|
||
std::fs::create_dir_all(&outside).unwrap();
|
||
assert_eq!(project_subdirectory(&root, "").unwrap(), root);
|
||
assert_eq!(project_subdirectory(&root, "sub/deeper").unwrap(), root.join("sub/deeper"));
|
||
assert_eq!(project_subdirectory(&root, "sub/../sub").unwrap(), root.join("sub"));
|
||
for bad in ["/tmp", "../outside", "sub/../../outside", "missing", "file.txt", "a\0b"] {
|
||
assert!(matches!(project_subdirectory(&root, bad), Err(HostError::Invalid(_))), "{bad:?}");
|
||
}
|
||
#[cfg(unix)]
|
||
{
|
||
std::os::unix::fs::symlink(&outside, root.join("escape")).unwrap();
|
||
assert!(project_subdirectory(&root, "escape").is_err(), "a link out of the project");
|
||
std::os::unix::fs::symlink(root.join("sub"), root.join("inside")).unwrap();
|
||
assert_eq!(project_subdirectory(&root, "inside").unwrap(), root.join("sub"));
|
||
}
|
||
}
|
||
|
||
fn api(dir: &Path, pins: Arc<RwLock<HashMap<(String, String), ProgramPin>>>) -> PluginApi {
|
||
let manifest = ide_extension::Manifest::parse(
|
||
"id = \"acme.lint\"\nname = \"Lint\"\nversion = \"1.0.0\"\nengines.openidea = \"^1.0\"\n",
|
||
)
|
||
.unwrap();
|
||
let sink: Arc<dyn PluginEventSink> = Arc::new(|_: Option<&str>, _: PluginEvent| {});
|
||
let shared = Shared::new(
|
||
sink,
|
||
dir.join(".data"),
|
||
pins,
|
||
ide_extension::UiStore::new(),
|
||
Arc::default(),
|
||
Arc::new(ide_extension::ExtensionRegistry::new()),
|
||
);
|
||
PluginApi { pkg: Package::new(manifest, dir.to_path_buf()), shared }
|
||
}
|
||
|
||
#[test]
|
||
fn net_questions_are_exclusive_throttled_and_cancelled() {
|
||
let dir = tempfile::tempdir().unwrap();
|
||
let mut api = api(dir.path(), Arc::default());
|
||
api.pkg = Package::new(
|
||
ide_extension::Manifest::parse(
|
||
"id = \"acme.lint\"\nname = \"Lint\"\nversion = \"1.0.0\"\nengines.openidea = \"^1.5\"\n[capabilities]\nrequired = [\"net:ask\"]\n",
|
||
).unwrap(),
|
||
dir.path().to_path_buf(),
|
||
);
|
||
let subject = Subject::Installed(api.id().into());
|
||
api.shared.ledger.lock().confirm(
|
||
&ide_extension::ledger::Request {
|
||
subject: subject.clone(),
|
||
plugin_id: api.id(),
|
||
version: "1.0.0",
|
||
package_hash: "h",
|
||
required: vec![Capability::NetAsk],
|
||
optional: vec![],
|
||
},
|
||
&[],
|
||
BTreeMap::new(),
|
||
);
|
||
for answer in [Some(vec!["always".into(), "once".into()]), None] {
|
||
api.shared.net_prompts.lock().clear();
|
||
let cancel = CancellationToken::new();
|
||
std::thread::scope(|scope| {
|
||
let run = scope.spawn(|| api.net_ask(&subject, Some("main"), "::1", &cancel));
|
||
let deadline = Instant::now() + Duration::from_secs(5);
|
||
let id = loop {
|
||
if let Some(id) = api.shared.prompts.lock().keys().next().copied() {
|
||
break id;
|
||
}
|
||
assert!(Instant::now() < deadline);
|
||
std::thread::sleep(Duration::from_millis(10));
|
||
};
|
||
assert_eq!(api.net_ask(&subject, Some("other"), "other.example", &cancel).unwrap(), None);
|
||
if let Some(answer) = answer {
|
||
assert!(api.shared.answer_prompt(id, Some(answer)));
|
||
assert_eq!(run.join().unwrap().unwrap(), None);
|
||
} else {
|
||
cancel.cancel();
|
||
assert!(matches!(run.join().unwrap(), Err(HostError::Cancelled)));
|
||
}
|
||
});
|
||
assert!(api.shared.prompts.lock().is_empty());
|
||
assert!(api.shared.ledger.lock().get(&subject).unwrap().net_hosts.is_empty());
|
||
assert_eq!(api.net_ask(&subject, Some("main"), "new.example", &CancellationToken::new()).unwrap(), None);
|
||
}
|
||
}
|
||
|
||
#[cfg(unix)]
|
||
#[test]
|
||
fn writes_recheck_approved_paths_after_symlink_substitution() {
|
||
use std::os::unix::fs::symlink;
|
||
let tmp = tempfile::tempdir().unwrap();
|
||
let api = api(tmp.path(), Arc::default());
|
||
let root = tmp.path().join("project");
|
||
std::fs::create_dir_all(root.join("nested")).unwrap();
|
||
let path = ide_plugin::resolve_writable_in_root(&root, "nested/file.txt").unwrap();
|
||
let outside = tmp.path().join("outside.txt");
|
||
std::fs::write(&outside, "unchanged").unwrap();
|
||
symlink(&outside, &path).unwrap();
|
||
assert!(api.write_text_file(&path, "text").is_err());
|
||
assert!(api.write_bytes_file(&path, b"bytes").is_err());
|
||
assert_eq!(std::fs::read_to_string(&outside).unwrap(), "unchanged");
|
||
|
||
std::fs::remove_file(&path).unwrap();
|
||
std::fs::remove_dir(root.join("nested")).unwrap();
|
||
symlink(tmp.path(), root.join("nested")).unwrap();
|
||
assert!(api.write_text_file(&path, "text").is_err());
|
||
assert!(api.write_bytes_file(&path, b"bytes").is_err());
|
||
assert!(!tmp.path().join("file.txt").exists());
|
||
}
|
||
|
||
#[cfg(unix)]
|
||
#[test]
|
||
fn storage_and_secrets_do_not_write_through_temporary_symlinks() {
|
||
use std::os::unix::fs::symlink;
|
||
let tmp = tempfile::tempdir().unwrap();
|
||
let api = api(tmp.path(), Arc::default());
|
||
let outside = tmp.path().join("outside.txt");
|
||
std::fs::write(&outside, "unchanged").unwrap();
|
||
let dir = api.storage().unwrap();
|
||
let entry = dir.join(entry_name("key"));
|
||
symlink(&outside, entry.with_extension("tmp")).unwrap();
|
||
symlink(&outside, &entry).unwrap();
|
||
api.storage_set("key", Some("value".into())).unwrap();
|
||
assert_eq!(api.storage_get("key").unwrap().as_deref(), Some("value"));
|
||
assert_eq!(std::fs::read_to_string(&outside).unwrap(), "unchanged");
|
||
|
||
let secrets = FileSecretStore { dir: tmp.path().join("secrets") };
|
||
secrets.set("acme.lint", "key", Some("before")).unwrap();
|
||
symlink(&outside, secrets.file("acme.lint").with_extension("tmp")).unwrap();
|
||
secrets.set("acme.lint", "key", Some("after")).unwrap();
|
||
assert_eq!(secrets.get("acme.lint", "key").as_deref(), Some("after"));
|
||
assert_eq!(std::fs::read_to_string(&outside).unwrap(), "unchanged");
|
||
}
|
||
|
||
/// Plugin secrets go to the credential store; a value an older version left in
|
||
/// the owner-only file keeps working until it is set again, and then leaves the file.
|
||
#[test]
|
||
fn plugin_secrets_go_to_the_credential_store() {
|
||
ide_ssh::secrets::testing::use_memory_store();
|
||
let tmp = tempfile::tempdir().unwrap();
|
||
let dir = tmp.path().join("plugins");
|
||
FileSecretStore { dir: dir.clone() }.set("acme.lint", "old", Some("legacy-3f9a")).unwrap();
|
||
let secrets = CredentialSecretStore::new(dir.clone());
|
||
assert_eq!(secrets.get("acme.lint", "old").as_deref(), Some("legacy-3f9a"));
|
||
|
||
secrets.set("acme.lint", "token", Some("value-7c1e")).unwrap();
|
||
secrets.set("acme.lint", "old", Some("moved-5d2b")).unwrap();
|
||
let on_disk = ide_ssh::secrets::testing::files_containing(tmp.path(), "value-7c1e");
|
||
assert_eq!(on_disk, Vec::<PathBuf>::new());
|
||
assert_eq!(ide_ssh::secrets::testing::files_containing(tmp.path(), "legacy-3f9a"), Vec::<PathBuf>::new());
|
||
let again = CredentialSecretStore::new(dir);
|
||
assert_eq!(again.get("acme.lint", "token").as_deref(), Some("value-7c1e"));
|
||
assert_eq!(again.get("acme.lint", "old").as_deref(), Some("moved-5d2b"));
|
||
assert_eq!(again.get("other.plugin", "token"), None);
|
||
|
||
again.set("acme.lint", "token", None).unwrap();
|
||
assert_eq!(again.get("acme.lint", "token"), None);
|
||
}
|
||
|
||
/// Without a credential store a value stays in the owner-only file.
|
||
#[test]
|
||
fn plugin_secrets_without_a_credential_store_stay_in_the_file() {
|
||
struct NoStore;
|
||
impl ide_ssh::secrets::Vault for NoStore {
|
||
fn available(&self) -> bool {
|
||
false
|
||
}
|
||
fn get(&self, _: &str, _: &str) -> ide_ssh::secrets::StoreResult<String> {
|
||
Err(ide_ssh::secrets::StoreError::NoDefaultStore)
|
||
}
|
||
fn set(&self, _: &str, _: &str, _: &str) -> ide_ssh::secrets::StoreResult<()> {
|
||
Err(ide_ssh::secrets::StoreError::NoDefaultStore)
|
||
}
|
||
fn delete(&self, _: &str, _: &str) -> ide_ssh::secrets::StoreResult<()> {
|
||
Err(ide_ssh::secrets::StoreError::NoDefaultStore)
|
||
}
|
||
}
|
||
let tmp = tempfile::tempdir().unwrap();
|
||
let dir = tmp.path().join("plugins");
|
||
let store = ide_ssh::secrets::SecretFile::with_vault(
|
||
dir.join("plugin-secrets.json"),
|
||
|v| serde_json::from_value(v).ok(),
|
||
Arc::new(NoStore),
|
||
);
|
||
let secrets = CredentialSecretStore::with_store(store, dir.clone());
|
||
secrets.set("acme.lint", "token", Some("value-7c1e")).unwrap();
|
||
assert_eq!(
|
||
ide_ssh::secrets::testing::files_containing(tmp.path(), "value-7c1e"),
|
||
[dir.join("acme.lint").join("secrets.json")]
|
||
);
|
||
assert_eq!(secrets.get("acme.lint", "token").as_deref(), Some("value-7c1e"));
|
||
secrets.set("acme.lint", "token", None).unwrap();
|
||
assert_eq!(secrets.get("acme.lint", "token"), None);
|
||
}
|
||
|
||
#[cfg(unix)]
|
||
#[test]
|
||
fn storage_and_secrets_refuse_symlinked_private_directories() {
|
||
let tmp = tempfile::tempdir().unwrap();
|
||
let api = api(tmp.path(), Arc::default());
|
||
let dir = api.storage().unwrap();
|
||
std::fs::remove_dir(&dir).unwrap();
|
||
let outside = tmp.path().join("outside");
|
||
std::fs::create_dir(&outside).unwrap();
|
||
std::os::unix::fs::symlink(&outside, &dir).unwrap();
|
||
assert!(api.storage_set("key", Some("value".into())).is_err());
|
||
let secrets = FileSecretStore { dir: tmp.path().join("secrets") };
|
||
std::fs::create_dir(&secrets.dir).unwrap();
|
||
std::os::unix::fs::symlink(&outside, secrets.dir.join("acme.lint")).unwrap();
|
||
assert!(secrets.set("acme.lint", "key", Some("value")).is_err());
|
||
assert_eq!(std::fs::read_dir(outside).unwrap().count(), 0);
|
||
}
|
||
|
||
#[cfg(windows)]
|
||
#[test]
|
||
fn private_directory_matches_resolved_write_paths() {
|
||
let tmp = tempfile::tempdir().unwrap();
|
||
let api = api(tmp.path(), Arc::default());
|
||
let dir = api.plugin_dir().unwrap();
|
||
let verbatim = std::fs::canonicalize(&dir).unwrap();
|
||
for root in [&dir, &verbatim] {
|
||
let path = ide_plugin::resolve_new_in_root(root, "state.txt").unwrap();
|
||
assert!(path.starts_with(&dir), "private writes must use the private-directory branch");
|
||
api.write_text_file(&path, "text").unwrap();
|
||
api.write_bytes_file(&path, b"bytes").unwrap();
|
||
assert_eq!(std::fs::read(&path).unwrap(), b"bytes");
|
||
}
|
||
}
|
||
|
||
#[test]
|
||
fn install_dir_is_the_canonical_package_directory() {
|
||
let tmp = tempfile::tempdir().unwrap();
|
||
let pkg = tmp.path().join("acme.lint");
|
||
std::fs::create_dir_all(&pkg).unwrap();
|
||
std::fs::write(pkg.join("readme.pdf"), b"%PDF").unwrap();
|
||
let api = api(&pkg, Arc::default());
|
||
let install = api.install_dir().unwrap();
|
||
assert_eq!(install, dunce::canonicalize(&pkg).unwrap());
|
||
assert!(install.join("readme.pdf").is_file());
|
||
assert_ne!(install, api.plugin_dir().unwrap());
|
||
}
|
||
|
||
#[test]
|
||
fn storage_moves_an_older_storage_json_to_one_file_per_key() {
|
||
let tmp = tempfile::tempdir().unwrap();
|
||
let legacy = tmp.path().join(".data/acme.lint/storage.json");
|
||
std::fs::create_dir_all(legacy.parent().unwrap()).unwrap();
|
||
std::fs::write(&legacy, r#"{"a":"1","b/c":"two"}"#).unwrap();
|
||
let api = api(tmp.path(), Arc::default());
|
||
assert_eq!(api.storage_keys("").unwrap(), ["a", "b/c"]);
|
||
assert_eq!(api.storage_get("b/c").unwrap().as_deref(), Some("two"));
|
||
assert!(!legacy.exists());
|
||
api.storage_set("a", Some("3".into())).unwrap();
|
||
api.storage_set("b/c", None).unwrap();
|
||
assert_eq!(api.storage_keys("").unwrap(), ["a"]);
|
||
assert_eq!(api.storage_get("a").unwrap().as_deref(), Some("3"));
|
||
assert_eq!(api.storage_get("b/c").unwrap(), None);
|
||
}
|
||
|
||
#[test]
|
||
fn storage_keeps_multimegabyte_indexes_after_host_recreation() {
|
||
let tmp = tempfile::tempdir().unwrap();
|
||
let value = "é".repeat(2 << 20);
|
||
{
|
||
let api = api(tmp.path(), Arc::default());
|
||
api.storage_set("index", Some(value.clone())).unwrap();
|
||
}
|
||
let api = api(tmp.path(), Arc::default());
|
||
assert_eq!(api.storage_get("index").unwrap().as_deref(), Some(value.as_str()));
|
||
api.storage_set("index", None).unwrap();
|
||
assert_eq!(api.storage_get("index").unwrap(), None);
|
||
}
|
||
|
||
#[test]
|
||
fn storage_quota_counts_utf8_keys_and_values_and_preserves_data_on_failure() {
|
||
let tmp = tempfile::tempdir().unwrap();
|
||
let api = api(tmp.path(), Arc::default());
|
||
let key = "index";
|
||
let mut value = "é".repeat(((64 << 20) - key.len()) / 2);
|
||
value.push('x');
|
||
api.storage_set(key, Some(value.clone())).unwrap();
|
||
|
||
for (key, value) in [("x", String::new()), (key, format!("{value}x"))] {
|
||
let err = api.storage_set(key, Some(value)).unwrap_err();
|
||
assert!(matches!(err, HostError::Invalid(ref message) if message.contains("64 MiB")), "{err}");
|
||
}
|
||
assert_eq!(api.storage_keys("").unwrap(), [key]);
|
||
assert_eq!(api.storage_get(key).unwrap().as_deref(), Some(value.as_str()));
|
||
api.storage_set(key, Some("small".into())).unwrap();
|
||
api.storage_set("other", Some("value".into())).unwrap();
|
||
assert_eq!(api.storage_get("other").unwrap().as_deref(), Some("value"));
|
||
}
|
||
|
||
#[test]
|
||
fn notification_actions_run_the_plugins_own_commands() {
|
||
let tmp = tempfile::tempdir().unwrap();
|
||
let api = api(tmp.path(), Arc::default());
|
||
let _command = api.shared.ui.insert(ide_extension::UiContribution {
|
||
plugin_id: api.id().into(),
|
||
point: "commands".into(),
|
||
key: "run".into(),
|
||
decl: serde_json::json!({}),
|
||
});
|
||
let action = |command: &str| ide_plugin::NotificationAction {
|
||
label: "Run".into(),
|
||
command: command.into(),
|
||
argument: Some("a\nb".into()),
|
||
};
|
||
let notify = |actions| api.notify(Some("main"), NotificationLevel::Info, "Done".into(), "".into(), actions);
|
||
notify(vec![]).unwrap();
|
||
notify(vec![action("run")]).unwrap();
|
||
assert!(matches!(notify(vec![action("run"), action("other.command")]), Err(HostError::NotFound(_))));
|
||
}
|
||
|
||
#[test]
|
||
fn repository_and_search_paths_match_ipc_events() {
|
||
let tmp = tempfile::tempdir().unwrap();
|
||
assert!(std::process::Command::new("git").args(["init", "-q"]).arg(tmp.path()).status().unwrap().success());
|
||
let root = dunce::canonicalize(tmp.path()).unwrap();
|
||
std::fs::create_dir(root.join("src")).unwrap();
|
||
let file = root.join("src/Example.java");
|
||
std::fs::write(&file, "class Example {}\n").unwrap();
|
||
let api = api(&root, Arc::default());
|
||
assert_eq!(api.find_files(&root, "**/*.java", 8).unwrap(), [to_ipc(&file)]);
|
||
assert_eq!(api.vcs_status(&root).unwrap().unwrap().root, to_ipc(&root));
|
||
}
|
||
|
||
/// A project index that lists `files` (root-relative), whatever is on disk.
|
||
struct Indexed(PathBuf, Vec<&'static str>);
|
||
impl PluginWorkspace for Indexed {
|
||
fn projects(&self) -> Vec<PathBuf> {
|
||
vec![self.0.clone()]
|
||
}
|
||
fn buffer_text(&self, _: &Path) -> Option<String> {
|
||
None
|
||
}
|
||
fn document(&self, _: &Path) -> Option<(u64, String)> {
|
||
None
|
||
}
|
||
fn apply_edits(&self, _: &Path, _: u64, _: &[TextEdit], _: bool) -> Result<u64, HostError> {
|
||
Err(HostError::Stale)
|
||
}
|
||
fn trusted(&self, _: &Path) -> bool {
|
||
true
|
||
}
|
||
fn project_of(&self, _: &str) -> Option<PathBuf> {
|
||
None
|
||
}
|
||
fn publish_diagnostics(&self, _: &str, _: &Path, _: Vec<ide_core::Diagnostic>) -> Result<(), HostError> {
|
||
Ok(())
|
||
}
|
||
fn indexed_files(&self, root: &Path, matches: &dyn Fn(&Path) -> bool, limit: usize) -> Option<Vec<PathBuf>> {
|
||
(root == self.0).then(|| {
|
||
self.1.iter().map(Path::new).filter(|rel| matches(rel)).take(limit).map(|rel| root.join(rel)).collect()
|
||
})
|
||
}
|
||
}
|
||
|
||
#[test]
|
||
fn find_files_uses_the_project_index_when_there_is_one() {
|
||
let tmp = tempfile::tempdir().unwrap();
|
||
let root = dunce::canonicalize(tmp.path()).unwrap();
|
||
let api = api(&root, Arc::default());
|
||
*api.shared.workspace.write() = Some(Arc::new(Indexed(
|
||
root.clone(),
|
||
vec!["src/b/UserMapper.xml", "src/a/OrderMapper.xml", ".idea/workspace.xml", ".git/index.xml", "pom.xml"],
|
||
)));
|
||
// Listed by the index, not walked: none of these exist on disk.
|
||
assert_eq!(
|
||
api.find_files(&root, "**/*Mapper.xml", 10).unwrap(),
|
||
[to_ipc(&root.join("src/a/OrderMapper.xml")), to_ipc(&root.join("src/b/UserMapper.xml"))]
|
||
);
|
||
// Hidden folders are visited like any project scope; `.git` internals are not.
|
||
assert_eq!(api.find_files(&root, "**/*.xml", 10).unwrap().len(), 4);
|
||
assert_eq!(api.find_files(&root, ".idea/*.xml", 10).unwrap(), [to_ipc(&root.join(".idea/workspace.xml"))]);
|
||
assert_eq!(api.find_files(&root, "**/*.xml", 1).unwrap().len(), 1);
|
||
// Another root: no index covers it, so the disk is walked.
|
||
let other = root.join("other");
|
||
std::fs::create_dir_all(other.join(".config/tool")).unwrap();
|
||
std::fs::write(other.join("a.xml"), "").unwrap();
|
||
std::fs::write(other.join(".config/tool/index.xml"), "").unwrap();
|
||
assert_eq!(api.find_files(&other, "*.xml", 10).unwrap(), [to_ipc(&other.join("a.xml"))]);
|
||
assert_eq!(
|
||
api.find_files(&other, ".config/tool/*.xml", 10).unwrap(),
|
||
[to_ipc(&other.join(".config/tool/index.xml"))]
|
||
);
|
||
}
|
||
|
||
/// A workspace whose container root has no files: the member holds them.
|
||
struct Members(PathBuf, Vec<PathBuf>);
|
||
impl PluginWorkspace for Members {
|
||
fn projects(&self) -> Vec<PathBuf> {
|
||
vec![self.0.clone()]
|
||
}
|
||
fn member_roots(&self, root: &Path) -> Vec<PathBuf> {
|
||
if root == self.0 { self.1.clone() } else { Vec::new() }
|
||
}
|
||
fn buffer_text(&self, _: &Path) -> Option<String> {
|
||
None
|
||
}
|
||
fn document(&self, _: &Path) -> Option<(u64, String)> {
|
||
None
|
||
}
|
||
fn apply_edits(&self, _: &Path, _: u64, _: &[TextEdit], _: bool) -> Result<u64, HostError> {
|
||
Err(HostError::Stale)
|
||
}
|
||
fn trusted(&self, _: &Path) -> bool {
|
||
true
|
||
}
|
||
fn project_of(&self, _: &str) -> Option<PathBuf> {
|
||
None
|
||
}
|
||
fn publish_diagnostics(&self, _: &str, _: &Path, _: Vec<ide_core::Diagnostic>) -> Result<(), HostError> {
|
||
Ok(())
|
||
}
|
||
}
|
||
|
||
#[test]
|
||
fn find_files_cover_workspace_members() {
|
||
let tmp = tempfile::tempdir().unwrap();
|
||
let base = dunce::canonicalize(tmp.path()).unwrap();
|
||
let (root, member) = (base.join("workspace"), base.join("member"));
|
||
std::fs::create_dir_all(root.join(".openidea")).unwrap();
|
||
std::fs::create_dir_all(member.join("src")).unwrap();
|
||
std::fs::write(root.join(".openidea/workspace.json"), "{}").unwrap();
|
||
std::fs::write(member.join("src/UserMapper.xml"), "<mapper/>").unwrap();
|
||
let api = api(&root, Arc::default());
|
||
*api.shared.workspace.write() = Some(Arc::new(Members(root.clone(), vec![member.clone()])));
|
||
// The container holds no sources: the member's files are project scope.
|
||
assert_eq!(api.find_files(&root, "**/*Mapper.xml", 10).unwrap(), [to_ipc(&member.join("src/UserMapper.xml"))]);
|
||
// A member root lists its own files, like any project root.
|
||
assert_eq!(api.find_files(&member, "src/*.xml", 10).unwrap(), [to_ipc(&member.join("src/UserMapper.xml"))]);
|
||
// And a path outside the group is never listed.
|
||
std::fs::write(base.join("OutsideMapper.xml"), "<mapper/>").unwrap();
|
||
assert!(api.find_files(&root, "**/*Mapper.xml", 10).unwrap().len() == 1);
|
||
}
|
||
|
||
#[test]
|
||
fn process_environment_only_adds_variables() {
|
||
let tmp = tempfile::tempdir().unwrap();
|
||
// Environment overrides must be rejected before spawn, so no executable is needed.
|
||
let binary = tmp.path().join("lint.exe");
|
||
std::fs::write(&binary, "not an executable").unwrap();
|
||
let pins: Arc<RwLock<HashMap<(String, String), ProgramPin>>> = Arc::default();
|
||
pins.write().insert(("acme.lint".into(), "process:lint".into()), ProgramPin::approve(&binary).unwrap());
|
||
let api = api(tmp.path(), pins);
|
||
let inherited = std::env::vars_os()
|
||
.find(|(key, _)| {
|
||
!env_name_eq(key, "PATH".as_ref())
|
||
&& key.to_str().is_some_and(|key| !key.is_empty() && !key.contains('='))
|
||
})
|
||
.unwrap()
|
||
.0
|
||
.into_string()
|
||
.unwrap();
|
||
for key in ["PATH".to_owned(), inherited] {
|
||
let cmd = ProcessCommand {
|
||
program: "lint".into(),
|
||
env: vec![(key.clone(), "replacement".into())],
|
||
..Default::default()
|
||
};
|
||
let err = api
|
||
.run_process(cmd.clone(), None, None, &CancellationToken::new(), Duration::from_secs(5))
|
||
.unwrap_err();
|
||
assert!(matches!(err, HostError::Invalid(ref message) if message.contains(&key)), "{err}");
|
||
let err = api.start_process(cmd, None, None).unwrap_err();
|
||
assert!(matches!(err, HostError::Invalid(ref message) if message.contains(&key)), "{err}");
|
||
}
|
||
}
|
||
|
||
#[test]
|
||
fn process_environment_uses_prepared_values_and_windows_key_rules() {
|
||
let mut cmd = StdCommand::new("unused");
|
||
ide_core::process::prepare(&mut cmd);
|
||
cmd.env("OPENIDEA_PREPARED_ENV", "login-shell");
|
||
let err = append_process_env(&mut cmd, &[("OPENIDEA_PREPARED_ENV".into(), "override".into())]).unwrap_err();
|
||
assert!(err.to_string().contains("OPENIDEA_PREPARED_ENV"));
|
||
for name in ["", "BAD=KEY", "BAD\0KEY"] {
|
||
assert!(append_process_env(&mut cmd, &[(name.into(), "value".into())]).is_err());
|
||
}
|
||
for value in ["bad\0value".to_owned(), "x".repeat(4097)] {
|
||
assert!(append_process_env(&mut cmd, &[("OPENIDEA_NEW_ENV".into(), value)]).is_err());
|
||
}
|
||
assert!(
|
||
append_process_env(
|
||
&mut cmd,
|
||
&[("OPENIDEA_NEW_ENV".into(), "a".into()), ("OPENIDEA_NEW_ENV".into(), "b".into())]
|
||
)
|
||
.is_err()
|
||
);
|
||
assert_eq!(env_name_eq("PATH".as_ref(), "Path".as_ref()), cfg!(windows));
|
||
#[cfg(windows)]
|
||
{
|
||
assert!(append_process_env(&mut cmd, &[("pAtH".into(), "override".into())]).is_err());
|
||
assert!(env_name_eq("Ä_KEY".as_ref(), "ä_key".as_ref()));
|
||
}
|
||
cmd.env_remove("OPENIDEA_REMOVED_ENV");
|
||
append_process_env(&mut cmd, &[("OPENIDEA_REMOVED_ENV".into(), "added".into())]).unwrap();
|
||
assert_eq!(cmd.get_envs().find(|(k, _)| *k == "OPENIDEA_REMOVED_ENV").unwrap().1, Some("added".as_ref()));
|
||
}
|
||
|
||
#[test]
|
||
fn process_output_preserves_partial_utf8_and_bounds_replacement_text() {
|
||
let mut bytes = vec![0xe4, 0xb8];
|
||
assert_eq!(take_text(&mut bytes, 1, false), "");
|
||
bytes.extend_from_slice(&[0xad, b'x']);
|
||
assert_eq!(take_text(&mut bytes, 1, false), "中");
|
||
assert_eq!(take_text(&mut bytes, 0, false), "x");
|
||
let mut bytes = vec![0xff; MAX_PENDING];
|
||
let text = take_text(&mut bytes, MAX_PENDING, true);
|
||
assert!(text.len() <= MAX_PENDING);
|
||
assert!(!bytes.is_empty());
|
||
assert_eq!(take_text(&mut vec![0xe4, 0xb8], 8, true), "�");
|
||
}
|
||
|
||
#[cfg(unix)]
|
||
fn shell_api(dir: &Path) -> PluginApi {
|
||
let pins: Arc<RwLock<HashMap<(String, String), ProgramPin>>> = Arc::default();
|
||
pins.write()
|
||
.insert(("acme.lint".into(), "process:sh".into()), ProgramPin::approve(Path::new("/bin/sh")).unwrap());
|
||
api(dir, pins)
|
||
}
|
||
|
||
#[cfg(unix)]
|
||
fn shell(script: &str) -> ProcessCommand {
|
||
ProcessCommand {
|
||
program: "sh".into(),
|
||
args: vec!["-c".into(), script.into()],
|
||
env: Vec::new(),
|
||
approval_root: None,
|
||
}
|
||
}
|
||
|
||
#[cfg(unix)]
|
||
#[test]
|
||
fn process_handles_are_bounded_scoped_and_released() {
|
||
let tmp = tempfile::tempdir().unwrap();
|
||
let api = shell_api(tmp.path());
|
||
let mut ids = Vec::new();
|
||
for _ in 0..MAX_PROCESSES {
|
||
ids.push(api.start_process(shell("exec cat"), None, Some("main")).unwrap());
|
||
}
|
||
assert!(matches!(api.start_process(shell("exec cat"), None, None), Err(HostError::Invalid(_))));
|
||
let proc = api.process(ids[0]).unwrap();
|
||
api.release_process(ids[0]);
|
||
assert!(proc.state.lock().child.try_wait().unwrap().is_some());
|
||
assert!(api.process(ids[0]).is_err());
|
||
let other = api.start_process(shell("exec cat"), None, Some("other")).unwrap();
|
||
api.shared.forget_client("main");
|
||
assert!(api.process(ids[1]).is_err());
|
||
assert!(api.process(other).is_ok());
|
||
assert!(matches!(api.start_process(shell("exec cat"), None, Some("main")), Err(HostError::Cancelled)));
|
||
api.shared.kill_processes(api.id());
|
||
assert!(api.shared.processes.lock().is_empty());
|
||
}
|
||
|
||
#[cfg(unix)]
|
||
#[test]
|
||
fn process_stdin_timeout_and_stop_do_not_deadlock() {
|
||
let tmp = tempfile::tempdir().unwrap();
|
||
let api = Arc::new(shell_api(tmp.path()));
|
||
for stop in [false, true] {
|
||
let id = api.start_process(shell("exec sleep 60"), None, Some("main")).unwrap();
|
||
let proc = api.process(id).unwrap();
|
||
let writer = api.clone();
|
||
let thread = std::thread::spawn(move || {
|
||
writer.write_process(id, &"x".repeat(1 << 20), &CancellationToken::new(), Duration::from_millis(150))
|
||
});
|
||
if stop {
|
||
std::thread::sleep(Duration::from_millis(30));
|
||
api.shared.kill_processes(api.id());
|
||
}
|
||
assert!(thread.join().unwrap().is_err());
|
||
assert!(proc.state.lock().child.try_wait().unwrap().is_some());
|
||
api.release_process(id);
|
||
}
|
||
}
|
||
|
||
#[cfg(unix)]
|
||
#[test]
|
||
fn process_parent_exit_closes_descendant_pipes() {
|
||
let tmp = tempfile::tempdir().unwrap();
|
||
let api = shell_api(tmp.path());
|
||
let token = CancellationToken::new();
|
||
let before = Instant::now();
|
||
let out =
|
||
api.run_process(shell("sleep 60 & echo done"), None, None, &token, Duration::from_millis(300)).unwrap();
|
||
assert_eq!(out.status, Some(0));
|
||
assert_eq!(out.stdout.trim(), "done");
|
||
assert!(before.elapsed() < Duration::from_secs(5));
|
||
let id = api.start_process(shell("sleep 60 & echo done"), None, None).unwrap();
|
||
let out = api.wait_process(id, MAX_PENDING, Duration::from_millis(300), &token).unwrap();
|
||
assert_eq!(out.status, Some(0));
|
||
assert_eq!(out.stdout.trim(), "done");
|
||
assert_eq!(api.process(id).unwrap().state.lock().readers, 0);
|
||
api.release_process(id);
|
||
}
|
||
|
||
#[cfg(unix)]
|
||
#[test]
|
||
fn process_output_is_drained_and_retention_is_bounded() {
|
||
let tmp = tempfile::tempdir().unwrap();
|
||
let api = shell_api(tmp.path());
|
||
let token = CancellationToken::new();
|
||
// stdout within the limit comes back whole; stderr beyond it is truncated (stdout beyond it fails).
|
||
let cmd = shell(&format!("head -c 5242880 /dev/zero; head -c {} /dev/zero >&2", MAX_OUTPUT + (1 << 20)));
|
||
let out = api.run_process(cmd.clone(), None, None, &token, Duration::from_secs(5)).unwrap();
|
||
assert_eq!(out.status, Some(0));
|
||
assert_eq!((out.stdout.len(), out.stderr.len()), (5 << 20, MAX_OUTPUT));
|
||
let id = api.start_process(cmd, None, None).unwrap();
|
||
let out = api.wait_process(id, MAX_PENDING, Duration::from_secs(5), &token).unwrap();
|
||
assert_eq!(out.status, Some(0));
|
||
assert_eq!((out.stdout.len(), out.stderr.len()), (MAX_PENDING, MAX_PENDING));
|
||
let state = api.process(id).unwrap();
|
||
let state = state.state.lock();
|
||
assert!(state.stdout.is_empty() && state.stderr.is_empty());
|
||
drop(state);
|
||
api.release_process(id);
|
||
}
|
||
|
||
#[cfg(unix)]
|
||
#[test]
|
||
fn the_status_comes_with_the_last_of_the_output() {
|
||
let tmp = tempfile::tempdir().unwrap();
|
||
let api = shell_api(tmp.path());
|
||
let token = CancellationToken::new();
|
||
let id = api.start_process(shell("printf 'abcdefghij'; printf 'xyz' >&2"), None, None).unwrap();
|
||
let first = api.wait_process(id, 4, Duration::from_secs(5), &token).unwrap();
|
||
assert_eq!((first.stdout.as_str(), first.stderr.as_str(), first.status), ("abcd", "xyz", None));
|
||
let mut stdout = first.stdout;
|
||
let status = loop {
|
||
let chunk = api.poll_process(id, 4).unwrap();
|
||
stdout.push_str(&chunk.stdout);
|
||
if let Some(status) = chunk.status {
|
||
break status;
|
||
}
|
||
};
|
||
assert_eq!((stdout.as_str(), status), ("abcdefghij", 0));
|
||
api.release_process(id);
|
||
}
|
||
|
||
fn test_proc_api(dir: &Path) -> (PluginApi, impl Fn(&str, &str) -> ProcessCommand) {
|
||
let pins: Arc<RwLock<HashMap<(String, String), ProgramPin>>> = Arc::default();
|
||
#[cfg(unix)]
|
||
let (prog, path) = ("sh", PathBuf::from("/bin/sh"));
|
||
#[cfg(windows)]
|
||
let (prog, path) = {
|
||
let sysroot = std::env::var("SystemRoot").unwrap_or_else(|_| "C:\\Windows".into());
|
||
("powershell", PathBuf::from(sysroot).join(r"System32\WindowsPowerShell\v1.0\powershell.exe"))
|
||
};
|
||
|
||
pins.write().insert(("acme.lint".into(), format!("process:{prog}")), ProgramPin::approve(&path).unwrap());
|
||
let api = api(dir, pins);
|
||
let cmd = move |unix_script: &str, win_script: &str| {
|
||
#[cfg(unix)]
|
||
{
|
||
let _ = win_script;
|
||
ProcessCommand {
|
||
program: prog.into(),
|
||
args: vec!["-c".into(), unix_script.into()],
|
||
env: Vec::new(),
|
||
approval_root: None,
|
||
}
|
||
}
|
||
#[cfg(windows)]
|
||
{
|
||
let _ = unix_script;
|
||
ProcessCommand {
|
||
program: prog.into(),
|
||
args: vec!["-NoProfile".into(), "-Command".into(), win_script.into()],
|
||
env: Vec::new(),
|
||
approval_root: None,
|
||
}
|
||
}
|
||
};
|
||
(api, cmd)
|
||
}
|
||
|
||
#[test]
|
||
fn start_stream_lossless_backpressure_fast_producer() {
|
||
let tmp = tempfile::tempdir().unwrap();
|
||
let (api, make_cmd) = test_proc_api(tmp.path());
|
||
let cmd = make_cmd("head -c 65536 /dev/zero | tr '\\0' 'A'", "[Console]::Out.Write('A' * 65536)");
|
||
let options = ProcessOptions { buffer_bytes: Some(4096), max_output_bytes: Some(1 << 20) };
|
||
let id = api.start_process_stream(cmd, None, None, Some(options)).unwrap();
|
||
let mut total_out = String::new();
|
||
let deadline = Instant::now() + Duration::from_secs(10);
|
||
let status = loop {
|
||
let chunk = api.poll_process(id, 1024).unwrap();
|
||
total_out.push_str(&chunk.stdout);
|
||
if let Some(status) = chunk.status {
|
||
break status;
|
||
}
|
||
assert!(Instant::now() < deadline, "timed out waiting for process completion");
|
||
std::thread::sleep(Duration::from_millis(5));
|
||
};
|
||
assert_eq!(status, 0);
|
||
assert_eq!(total_out.len(), 65536);
|
||
assert!(total_out.chars().all(|c| c == 'A'));
|
||
api.release_process(id);
|
||
}
|
||
|
||
#[test]
|
||
fn start_stream_dual_stream_concurrency() {
|
||
let tmp = tempfile::tempdir().unwrap();
|
||
let (api, make_cmd) = test_proc_api(tmp.path());
|
||
let cmd = make_cmd(
|
||
"head -c 10000 /dev/zero | tr '\\0' 'O'; head -c 10000 /dev/zero | tr '\\0' 'E' >&2",
|
||
"[Console]::Out.Write('O' * 10000); [Console]::Error.Write('E' * 10000)",
|
||
);
|
||
let options = ProcessOptions { buffer_bytes: Some(4096), max_output_bytes: Some(1 << 20) };
|
||
let id = api.start_process_stream(cmd, None, None, Some(options)).unwrap();
|
||
let mut total_stdout = String::new();
|
||
let mut total_stderr = String::new();
|
||
let deadline = Instant::now() + Duration::from_secs(10);
|
||
let status = loop {
|
||
let chunk = api.poll_process(id, 1024).unwrap();
|
||
total_stdout.push_str(&chunk.stdout);
|
||
total_stderr.push_str(&chunk.stderr);
|
||
if let Some(status) = chunk.status {
|
||
break status;
|
||
}
|
||
assert!(Instant::now() < deadline, "timed out waiting for dual streams");
|
||
std::thread::sleep(Duration::from_millis(5));
|
||
};
|
||
assert_eq!(status, 0);
|
||
assert_eq!(total_stdout.len(), 10000);
|
||
assert_eq!(total_stderr.len(), 10000);
|
||
assert!(total_stdout.chars().all(|c| c == 'O'));
|
||
assert!(total_stderr.chars().all(|c| c == 'E'));
|
||
api.release_process(id);
|
||
}
|
||
|
||
#[test]
|
||
fn start_stream_total_limit_overflow() {
|
||
let tmp = tempfile::tempdir().unwrap();
|
||
let (api, make_cmd) = test_proc_api(tmp.path());
|
||
let cmd = make_cmd("head -c 2097152 /dev/zero | tr '\\0' 'X'", "[Console]::Out.Write('X' * 2097152)");
|
||
let options = ProcessOptions { buffer_bytes: Some(4096), max_output_bytes: Some(1 << 20) };
|
||
let id = api.start_process_stream(cmd, None, None, Some(options)).unwrap();
|
||
let deadline = Instant::now() + Duration::from_secs(10);
|
||
let mut err = None;
|
||
while Instant::now() < deadline {
|
||
match api.poll_process(id, 65536) {
|
||
Ok(chunk) => {
|
||
if chunk.status.is_some() {
|
||
break;
|
||
}
|
||
}
|
||
Err(e) => {
|
||
err = Some(e);
|
||
break;
|
||
}
|
||
}
|
||
std::thread::sleep(Duration::from_millis(10));
|
||
}
|
||
let err = err.expect("expected overflow error");
|
||
assert!(matches!(err, HostError::Failed(ref msg) if msg.contains("exceeded 1 MiB limit")), "{err}");
|
||
api.release_process(id);
|
||
}
|
||
|
||
#[test]
|
||
fn run_captured_configurable_limits() {
|
||
let tmp = tempfile::tempdir().unwrap();
|
||
let (api, make_cmd) = test_proc_api(tmp.path());
|
||
let token = CancellationToken::new();
|
||
let cmd = make_cmd("head -c 2097152 /dev/zero | tr '\\0' 'Z'", "[Console]::Out.Write('Z' * 2097152)");
|
||
let opts_ok = ProcessOptions { max_output_bytes: Some(4 << 20), buffer_bytes: None };
|
||
let out = api
|
||
.run_process_with_options(cmd.clone(), None, None, &token, Duration::from_secs(10), Some(opts_ok))
|
||
.unwrap();
|
||
assert_eq!(out.status, Some(0));
|
||
assert_eq!(out.stdout.len(), 2097152);
|
||
|
||
let opts_fail = ProcessOptions { max_output_bytes: Some(1 << 20), buffer_bytes: None };
|
||
let err = api
|
||
.run_process_with_options(cmd, None, None, &token, Duration::from_secs(10), Some(opts_fail))
|
||
.unwrap_err();
|
||
assert!(matches!(err, HostError::Failed(ref msg) if msg.contains("stdout exceeded 1 MiB limit")), "{err}");
|
||
|
||
// Stderr overflow in run_captured also fails explicitly
|
||
let cmd_err = make_cmd("head -c 2097152 /dev/zero >&2", "[Console]::Error.Write('E' * 2097152)");
|
||
let err = api
|
||
.run_process_with_options(cmd_err, None, None, &token, Duration::from_secs(10), Some(opts_fail))
|
||
.unwrap_err();
|
||
assert!(matches!(err, HostError::Failed(ref msg) if msg.contains("stderr exceeded 1 MiB limit")), "{err}");
|
||
}
|
||
|
||
#[test]
|
||
fn start_stream_wait_drains_producer_larger_than_buffer() {
|
||
let tmp = tempfile::tempdir().unwrap();
|
||
let (api, make_cmd) = test_proc_api(tmp.path());
|
||
let token = CancellationToken::new();
|
||
// Emits 64 KiB, which is 16x larger than the 4 KiB buffer. Calling wait drains without blocking on pipe.
|
||
let cmd = make_cmd("head -c 65536 /dev/zero | tr '\\0' 'W'", "[Console]::Out.Write('W' * 65536)");
|
||
let options = ProcessOptions { buffer_bytes: Some(4096), max_output_bytes: Some(1 << 20) };
|
||
let id = api.start_process_stream(cmd, None, None, Some(options)).unwrap();
|
||
let chunk = api.wait_process(id, 100_000, Duration::from_secs(10), &token).unwrap();
|
||
assert_eq!(chunk.status, Some(0));
|
||
assert_eq!(chunk.stdout.len(), 65536);
|
||
assert!(chunk.stdout.chars().all(|c| c == 'W'));
|
||
api.release_process(id);
|
||
}
|
||
|
||
#[test]
|
||
fn start_stream_cancellation_terminates_child() {
|
||
let tmp = tempfile::tempdir().unwrap();
|
||
let (api, make_cmd) = test_proc_api(tmp.path());
|
||
let cmd = make_cmd("sleep 60", "Start-Sleep -Seconds 60");
|
||
let id = api.start_process_stream(cmd, None, None, None).unwrap();
|
||
let proc = api.process(id).unwrap();
|
||
api.kill_process(id).unwrap();
|
||
assert!(proc.state.lock().child.try_wait().unwrap().is_some());
|
||
api.release_process(id);
|
||
}
|
||
|
||
#[test]
|
||
fn programs_run_only_as_approved() {
|
||
let tmp = tempfile::tempdir().unwrap();
|
||
let bin = tmp.path().join("lint");
|
||
std::fs::write(&bin, "#!/bin/sh\n").unwrap();
|
||
let pins: Arc<RwLock<HashMap<(String, String), ProgramPin>>> = Arc::default();
|
||
let api = api(tmp.path(), pins.clone());
|
||
|
||
// Allowed while the program was missing: say how to fix it rather than "not granted".
|
||
let err = api.approved_program("lint").unwrap_err().to_string();
|
||
assert!(
|
||
err.contains("was not found on PATH when this permission was allowed")
|
||
&& err.contains("clear process:lint in Settings › Plugins and select it again"),
|
||
"{err}"
|
||
);
|
||
|
||
pins.write().insert(("acme.lint".into(), "process:lint".into()), ProgramPin::approve(&bin).unwrap());
|
||
assert_eq!(api.approved_program("lint").unwrap(), bin);
|
||
|
||
std::fs::write(&bin, "#!/bin/sh\necho replaced\n").unwrap();
|
||
assert!(
|
||
matches!(api.approved_program("lint"), Err(HostError::Denied(e)) if e.contains("changed since it was approved"))
|
||
);
|
||
}
|
||
#[test]
|
||
fn redirects_check_grants_before_connecting_and_downgrades_require_http() {
|
||
assert_eq!(
|
||
redirect_capability(&"https://localhost/start".parse().unwrap(), &"http://localhost/end".parse().unwrap())
|
||
.unwrap(),
|
||
Capability::NetHttp("localhost".into())
|
||
);
|
||
let source = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
|
||
let target = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
|
||
target.set_nonblocking(true).unwrap();
|
||
let source_port = source.local_addr().unwrap().port();
|
||
let target_port = target.local_addr().unwrap().port();
|
||
let worker = std::thread::spawn(move || {
|
||
use std::io::{Read, Write};
|
||
let (mut stream, _) = source.accept().unwrap();
|
||
let mut buf = [0; 4096];
|
||
let _ = stream.read(&mut buf);
|
||
write!(stream, "HTTP/1.1 302 Found\r\nLocation: http://127.0.0.1:{target_port}/\r\nContent-Length: 0\r\nConnection: close\r\n\r\n").unwrap();
|
||
});
|
||
let dir = tempfile::tempdir().unwrap();
|
||
let api = api(dir.path(), Arc::default());
|
||
let result = api.fetch(
|
||
HttpRequest {
|
||
method: "GET".into(),
|
||
url: format!("http://localhost:{source_port}/"),
|
||
headers: Vec::new(),
|
||
body: None,
|
||
timeout: Duration::from_secs(2),
|
||
grants: vec![Capability::Net("localhost".into())],
|
||
},
|
||
&CancellationToken::new(),
|
||
);
|
||
assert!(matches!(result, Err(HostError::Denied(cap)) if cap == "net:127.0.0.1"));
|
||
assert!(target.accept().is_err(), "ungranted target must never receive a connection");
|
||
worker.join().unwrap();
|
||
}
|
||
|
||
#[test]
|
||
fn fetch_accepts_responses_up_to_16_mib_and_rejects_larger_bodies() {
|
||
fn serve_bytes(total: usize) -> (u16, std::thread::JoinHandle<()>) {
|
||
let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
|
||
let port = listener.local_addr().unwrap().port();
|
||
let handle = std::thread::spawn(move || {
|
||
use std::io::{Read, Write};
|
||
let (mut stream, _) = listener.accept().unwrap();
|
||
let mut buf = [0; 4096];
|
||
let _ = stream.read(&mut buf);
|
||
let header = format!("HTTP/1.1 200 OK\r\nContent-Length: {total}\r\nConnection: close\r\n\r\n");
|
||
if stream.write_all(header.as_bytes()).is_err() {
|
||
return;
|
||
}
|
||
let chunk = [b'x'; 64 << 10];
|
||
let mut sent = 0;
|
||
while sent < total {
|
||
let n = (total - sent).min(chunk.len());
|
||
if stream.write_all(&chunk[..n]).is_err() {
|
||
break;
|
||
}
|
||
sent += n;
|
||
}
|
||
});
|
||
(port, handle)
|
||
}
|
||
|
||
let dir = tempfile::tempdir().unwrap();
|
||
let api = api(dir.path(), Arc::default());
|
||
|
||
let (ok_port, ok_worker) = serve_bytes(10 << 20);
|
||
let resp = api
|
||
.fetch(
|
||
HttpRequest {
|
||
method: "GET".into(),
|
||
url: format!("http://127.0.0.1:{ok_port}/"),
|
||
headers: Vec::new(),
|
||
body: None,
|
||
timeout: Duration::from_secs(10),
|
||
grants: vec![Capability::Net("127.0.0.1".into())],
|
||
},
|
||
&CancellationToken::new(),
|
||
)
|
||
.expect("10 MiB response within 16 MiB limit succeeds");
|
||
assert_eq!(resp.status, 200);
|
||
assert_eq!(resp.body.len(), 10 << 20);
|
||
ok_worker.join().unwrap();
|
||
|
||
let (big_port, big_worker) = serve_bytes(MAX_RESPONSE + (1 << 20));
|
||
let err = api
|
||
.fetch(
|
||
HttpRequest {
|
||
method: "GET".into(),
|
||
url: format!("http://127.0.0.1:{big_port}/"),
|
||
headers: Vec::new(),
|
||
body: None,
|
||
timeout: Duration::from_secs(10),
|
||
grants: vec![Capability::Net("127.0.0.1".into())],
|
||
},
|
||
&CancellationToken::new(),
|
||
)
|
||
.expect_err("response exceeding 16 MiB fails explicitly");
|
||
assert!(matches!(&err, HostError::Failed(msg) if msg.contains("response larger than 16 MiB")), "{err:?}");
|
||
big_worker.join().unwrap();
|
||
}
|
||
|
||
#[test]
|
||
fn output_ring_keeps_utf8_and_is_withdrawn_on_stop() {
|
||
let dir = tempfile::tempdir().unwrap();
|
||
let api = api(dir.path(), Arc::default());
|
||
api.output_write("build", Some(&"😀".repeat(300_000))).unwrap();
|
||
let buffer = api.shared.output.lock().get(&(api.id().into(), "build".into())).cloned().unwrap();
|
||
assert_eq!(buffer.len(), 1 << 20);
|
||
api.output_write("build", Some("x")).unwrap();
|
||
let buffer = api.shared.output.lock().get(&(api.id().into(), "build".into())).cloned().unwrap();
|
||
assert!(buffer.len() <= 1 << 20 && buffer.ends_with('x'));
|
||
api.output_write("build", None).unwrap();
|
||
assert!(api.shared.output.lock().get(&(api.id().into(), "build".into())).unwrap().is_empty());
|
||
api.shared.clear_plugin(api.id(), true);
|
||
assert!(api.shared.output.lock().is_empty());
|
||
}
|
||
}
|
||
|
||
/// The window a user action invoked the plugin from: browser tabs and clipboard writes
|
||
/// follow what the user did, never background work (which would reach every window).
|
||
fn from_window<'a>(client: Option<&'a str>, what: &str) -> Result<&'a str, HostError> {
|
||
client.ok_or_else(|| HostError::Invalid(format!("can only {what} from a command the user invoked")))
|
||
}
|
||
|
||
fn plugin_remotes(repo: &ide_vcs::Repo) -> Result<Vec<(String, String)>, HostError> {
|
||
Ok(repo.remotes().map_err(failed)?.into_iter().map(|r| (r.name, without_credentials(&r.url))).collect())
|
||
}
|
||
|
||
/// `url` without the `user:password@` a remote may carry (`https://user:token@host/…`):
|
||
/// `vcs.read` must not hand out credentials. scp-style `git@host:path` has no password.
|
||
fn without_credentials(url: &str) -> String {
|
||
let Some((scheme, rest)) = url.split_once("://") else { return url.to_owned() };
|
||
let authority_end = rest.find('/').unwrap_or(rest.len());
|
||
match rest[..authority_end].rfind('@') {
|
||
Some(at) => format!("{scheme}://{}", &rest[at + 1..]),
|
||
None => url.to_owned(),
|
||
}
|
||
}
|
||
|
||
#[cfg(test)]
|
||
mod credential_tests {
|
||
use super::*;
|
||
|
||
#[test]
|
||
fn remote_urls_lose_their_credentials() {
|
||
for (url, expected) in [
|
||
("https://user:[email protected]/o/r.git", "https://github.com/o/r.git"),
|
||
("https://[email protected]/o/r", "https://gitlab.com/o/r"),
|
||
("ssh://git@host:2222/o/r.git", "ssh://host:2222/o/r.git"),
|
||
("https://github.com/o/r.git", "https://github.com/o/r.git"),
|
||
("https://github.com/o/r@v1", "https://github.com/o/r@v1"),
|
||
("[email protected]:o/r.git", "[email protected]:o/r.git"),
|
||
("/srv/git/r.git", "/srv/git/r.git"),
|
||
] {
|
||
assert_eq!(without_credentials(url), expected, "{url}");
|
||
}
|
||
assert!(matches!(from_window(None, "open a URL"), Err(HostError::Invalid(_))));
|
||
assert_eq!(from_window(Some("main"), "open a URL").unwrap(), "main");
|
||
}
|
||
}
|
||
|
||
fn valid_range(document: &ide_text::Document, range: ide_core::Range) -> bool {
|
||
range.start <= range.end
|
||
&& [range.start, range.end].into_iter().all(|pos| document.byte_to_pos(document.pos_to_byte(pos)) == pos)
|
||
}
|
||
|
||
impl PluginApi {
|
||
/// The repository of `root`, which may execute only when `root` is a trusted project.
|
||
/// Status needs it there: a restricted repository is read without its config, so it knows
|
||
/// no upstream. The other reads use [`Self::read_repo`] and never execute.
|
||
fn repo(&self, root: &Path) -> Result<Option<ide_vcs::Repo>, HostError> {
|
||
if self.shared.workspace().is_some_and(|w| !w.git_enabled(root)) {
|
||
return Ok(None);
|
||
}
|
||
let repo = ide_vcs::Repo::discover(root).map_err(failed)?;
|
||
if let Some(repo) = &repo
|
||
&& let Some(workspace) = self.shared.workspace()
|
||
&& workspace.trusted(root)
|
||
{
|
||
repo.set_execution_allowed(true);
|
||
repo.set_git_executable(workspace.git_executable(root).as_deref());
|
||
}
|
||
Ok(repo)
|
||
}
|
||
fn read_repo(&self, root: &Path) -> Result<Option<ide_vcs::Repo>, HostError> {
|
||
if !self.shared.workspace().is_some_and(|w| w.trusted(root)) {
|
||
return Err(HostError::Failed("the project is in Restricted Mode: git does not run there".into()));
|
||
}
|
||
if self.shared.workspace().is_some_and(|w| !w.git_enabled(root)) {
|
||
return Ok(None);
|
||
}
|
||
let repo = ide_vcs::Repo::discover(root).map_err(failed)?;
|
||
if let Some(repo) = &repo {
|
||
repo.set_execution_allowed(false);
|
||
}
|
||
Ok(repo)
|
||
}
|
||
fn set_decorations(
|
||
&self,
|
||
path: Option<&Path>,
|
||
set: &str,
|
||
mut items: Vec<ide_plugin::Decoration>,
|
||
file: Option<ide_plugin::FileDecoration>,
|
||
tree: bool,
|
||
) -> Result<(), HostError> {
|
||
if set.is_empty() || set.len() > 128 {
|
||
return Err(HostError::Invalid("set id must be 1-128 bytes".into()));
|
||
}
|
||
if items.len() > 10_000 || file.as_ref().and_then(|f| f.badge.as_ref()).is_some_and(|b| b.chars().count() > 2) {
|
||
return Err(HostError::Invalid("decoration limit exceeded".into()));
|
||
}
|
||
if let Some(note) = file.as_ref().and_then(|f| f.note.as_ref())
|
||
&& (note.trim().is_empty() || note.chars().count() > 200 || note.contains(['\n', '\r']))
|
||
{
|
||
return Err(HostError::Invalid("a file note is one non-empty line of at most 200 characters".into()));
|
||
}
|
||
if let Some(icon) = file.as_ref().and_then(|f| f.icon.as_ref()) {
|
||
ide_extension::validate_item_id(icon, "file icon id").map_err(HostError::Invalid)?;
|
||
}
|
||
for colors in items.iter_mut().filter_map(|d| d.colors.as_mut()) {
|
||
for color in [&mut colors.foreground, &mut colors.background].into_iter().flatten() {
|
||
*color = decoration_color(color).ok_or_else(|| {
|
||
HostError::Invalid(format!("decoration color '{color}' is not #rrggbb or #rrggbbaa"))
|
||
})?;
|
||
}
|
||
}
|
||
items
|
||
.iter_mut()
|
||
.for_each(|d| d.colors = d.colors.take().filter(|c| c.foreground.is_some() || c.background.is_some()));
|
||
// An item with its own colors (API 1.17) may leave the style empty.
|
||
items.retain(|d| decoration_style(&d.style) || (d.style.is_empty() && d.colors.is_some()));
|
||
if items.iter().any(|d| d.range.start > d.range.end) {
|
||
return Err(HostError::Invalid("reversed decoration range".into()));
|
||
}
|
||
let replacement = if let Some(path) = path {
|
||
// The decoration follows the file, not the window's project: editors render
|
||
// whatever they have open, including files outside every project root. The
|
||
// set's root stays the path's own (deepest open) project for tree filtering.
|
||
let root = self.decoration_root(path);
|
||
Some(super::DecorationSet {
|
||
root: root.into(),
|
||
path: ide_core::IpcPath(path.to_path_buf()),
|
||
set_id: set.into(),
|
||
tree,
|
||
items,
|
||
file: file.filter(|f| f.note.is_some() || f.icon.is_some() || decoration_style(&f.style)),
|
||
})
|
||
} else {
|
||
None
|
||
};
|
||
let mut all = self.shared.decorations.lock();
|
||
let mut sets = all.get(self.id()).cloned().unwrap_or_default();
|
||
sets.retain(|s| !(s.tree == tree && s.set_id == set && replacement.as_ref().is_none_or(|r| r.path == s.path)));
|
||
if let Some(replacement) = replacement {
|
||
sets.push(replacement);
|
||
}
|
||
let names: BTreeSet<_> = sets.iter().filter(|s| s.tree == tree).map(|s| &s.set_id).collect();
|
||
if names.len() > 50 || serde_json::to_vec(&sets).map_err(failed)?.len() > 4 << 20 {
|
||
return Err(HostError::Invalid("decoration sets exceed plugin limits".into()));
|
||
}
|
||
// A plugin may spread one file's annotations across sets, but the combined limit is fixed.
|
||
let mut counts = BTreeMap::new();
|
||
for s in &sets {
|
||
*counts.entry(&s.path).or_insert(0usize) += s.items.len();
|
||
}
|
||
if counts.values().any(|n| *n > 10_000) {
|
||
return Err(HostError::Invalid("at most 10000 decorations per file".into()));
|
||
}
|
||
all.insert(self.id().into(), sets.clone());
|
||
// Emit while holding the state lock so replacement snapshots cannot arrive out of order.
|
||
self.shared.sink.emit(None, PluginEvent::DecorationsChanged { plugin_id: self.id().into(), sets });
|
||
Ok(())
|
||
}
|
||
|
||
fn decoration_root(&self, path: &Path) -> PathBuf {
|
||
self.shared
|
||
.workspace()
|
||
.map(|w| {
|
||
w.projects()
|
||
.into_iter()
|
||
.filter(|root| path.starts_with(root))
|
||
.max_by_key(|root| root.components().count())
|
||
})
|
||
.unwrap_or_default()
|
||
.unwrap_or_else(|| path.parent().map(Path::to_path_buf).unwrap_or_else(|| path.to_path_buf()))
|
||
}
|
||
|
||
fn set_file_icon_decorations(&self, set: &str, items: Vec<(PathBuf, String)>) -> Result<(), HostError> {
|
||
if set.is_empty() || set.len() > 128 {
|
||
return Err(HostError::Invalid("set id must be 1-128 bytes".into()));
|
||
}
|
||
if items.len() > 10_000 {
|
||
return Err(HostError::Invalid("at most 10000 file icons per set".into()));
|
||
}
|
||
let mut by_path = BTreeMap::new();
|
||
for (path, icon) in items {
|
||
ide_extension::validate_item_id(&icon, "file icon id").map_err(HostError::Invalid)?;
|
||
by_path.insert(path, icon);
|
||
}
|
||
let mut all = self.shared.decorations.lock();
|
||
let mut sets = all.get(self.id()).cloned().unwrap_or_default();
|
||
sets.retain(|s| !(s.tree && s.set_id == set));
|
||
for (path, icon) in by_path {
|
||
let root = self.decoration_root(&path);
|
||
sets.push(super::DecorationSet {
|
||
root: root.into(),
|
||
path: ide_core::IpcPath(path),
|
||
set_id: set.into(),
|
||
tree: true,
|
||
items: Vec::new(),
|
||
file: Some(ide_plugin::FileDecoration {
|
||
badge: None,
|
||
style: String::new(),
|
||
tooltip: None,
|
||
note: None,
|
||
icon: Some(icon),
|
||
}),
|
||
});
|
||
}
|
||
let names: BTreeSet<_> = sets.iter().filter(|s| s.tree).map(|s| &s.set_id).collect();
|
||
if names.len() > 50 || serde_json::to_vec(&sets).map_err(failed)?.len() > 4 << 20 {
|
||
return Err(HostError::Invalid("decoration sets exceed plugin limits".into()));
|
||
}
|
||
all.insert(self.id().into(), sets.clone());
|
||
self.shared.sink.emit(None, PluginEvent::DecorationsChanged { plugin_id: self.id().into(), sets });
|
||
Ok(())
|
||
}
|
||
}
|
||
/// A `set-styled` color as the editor renders it: `#rrggbb` or `#rrggbbaa`, lower case.
|
||
fn decoration_color(color: &str) -> Option<String> {
|
||
let hex = color.strip_prefix('#')?;
|
||
((hex.len() == 6 || hex.len() == 8) && hex.bytes().all(|b| b.is_ascii_hexdigit()))
|
||
.then(|| format!("#{}", hex.to_ascii_lowercase()))
|
||
}
|
||
|
||
fn decoration_style(style: &str) -> bool {
|
||
matches!(
|
||
style,
|
||
"highlight"
|
||
| "search"
|
||
| "added"
|
||
| "modified"
|
||
| "removed"
|
||
| "warning"
|
||
| "error"
|
||
| "info"
|
||
| "typo"
|
||
| "weak"
|
||
| "underline"
|
||
| "strikethrough"
|
||
| "muted"
|
||
)
|
||
}
|
||
impl Shared {
|
||
pub(super) fn decoration_sets(&self) -> BTreeMap<String, Vec<super::DecorationSet>> {
|
||
self.decorations.lock().clone()
|
||
}
|
||
pub(super) fn console_rules(&self) -> BTreeMap<String, Vec<ide_plugin::ConsoleRule>> {
|
||
self.console_rules.lock().clone()
|
||
}
|
||
}
|
||
|
||
#[cfg(test)]
|
||
mod workbench_tests {
|
||
use super::*;
|
||
struct Workspace {
|
||
root: PathBuf,
|
||
trusted: bool,
|
||
text: Option<String>,
|
||
}
|
||
impl PluginWorkspace for Workspace {
|
||
fn projects(&self) -> Vec<PathBuf> {
|
||
vec![self.root.clone()]
|
||
}
|
||
fn buffer_text(&self, _: &Path) -> Option<String> {
|
||
self.text.clone()
|
||
}
|
||
fn document(&self, _: &Path) -> Option<(u64, String)> {
|
||
self.text.clone().map(|s| (1, s))
|
||
}
|
||
fn apply_edits(&self, _: &Path, _: u64, _: &[TextEdit], _: bool) -> Result<u64, HostError> {
|
||
Err(HostError::Stale)
|
||
}
|
||
fn trusted(&self, _: &Path) -> bool {
|
||
self.trusted
|
||
}
|
||
fn project_of(&self, _: &str) -> Option<PathBuf> {
|
||
Some(self.root.clone())
|
||
}
|
||
fn publish_diagnostics(&self, _: &str, _: &Path, _: Vec<ide_core::Diagnostic>) -> Result<(), HostError> {
|
||
Ok(())
|
||
}
|
||
fn open_editors(&self, client: &str) -> Result<Vec<ide_plugin::OpenEditor>, HostError> {
|
||
Ok(if client == "main" {
|
||
vec![ide_plugin::OpenEditor {
|
||
path: to_ipc(&self.root.join("a.txt")),
|
||
language: "text".into(),
|
||
active: true,
|
||
dirty: true,
|
||
}]
|
||
} else {
|
||
Vec::new()
|
||
})
|
||
}
|
||
}
|
||
fn api(root: &Path, trusted: bool, text: Option<&str>) -> PluginApi {
|
||
let manifest = ide_extension::Manifest::parse(
|
||
"id = \"test\"\nname = \"Test\"\nversion = \"1.0.0\"\nengines.openidea = \"^1.4\"\n",
|
||
)
|
||
.unwrap();
|
||
let shared = Shared::new(
|
||
Arc::new(|_: Option<&str>, _: PluginEvent| {}),
|
||
root.join(".data"),
|
||
Arc::default(),
|
||
ide_extension::UiStore::new(),
|
||
Arc::default(),
|
||
Arc::new(ide_extension::ExtensionRegistry::new()),
|
||
);
|
||
*shared.workspace.write() =
|
||
Some(Arc::new(Workspace { root: root.into(), trusted, text: text.map(str::to_owned) }));
|
||
PluginApi { pkg: Package::new(manifest, root.into()), shared }
|
||
}
|
||
#[test]
|
||
fn command_exemption_requires_a_declared_unambiguous_owner() {
|
||
let dir = tempfile::tempdir().unwrap();
|
||
let api = api(dir.path(), true, None);
|
||
let own = api.shared.ui.insert(ide_extension::UiContribution {
|
||
plugin_id: "test".into(),
|
||
point: super::super::COMMANDS.into(),
|
||
key: "other.run".into(),
|
||
decl: serde_json::json!({}),
|
||
});
|
||
assert!(api.owns_command("plugin.test.other.run"));
|
||
assert!(!api.owns_command("plugin.test.unknown"));
|
||
let other = api.shared.ui.insert(ide_extension::UiContribution {
|
||
plugin_id: "test.other".into(),
|
||
point: super::super::COMMANDS.into(),
|
||
key: "run".into(),
|
||
decl: serde_json::json!({}),
|
||
});
|
||
assert!(!api.owns_command("plugin.test.other.run"));
|
||
drop(other);
|
||
assert!(api.owns_command("plugin.test.other.run"));
|
||
drop(own);
|
||
}
|
||
#[test]
|
||
fn selections_validate_window_and_utf16_without_requiring_a_grant() {
|
||
let dir = tempfile::tempdir().unwrap();
|
||
let root = dunce::canonicalize(dir.path()).unwrap();
|
||
let path = root.join("a.txt");
|
||
let api = api(&root, true, Some("😀abc"));
|
||
let range = |col| ide_core::Range::point(ide_core::Pos::new(0, col));
|
||
assert!(api.editor_ranges("main", &path, vec![range(2)], false).is_ok());
|
||
assert!(api.editor_ranges("main", &path, vec![range(1)], true).is_err());
|
||
assert!(api.editor_ranges("other", &path, vec![range(2)], false).is_err());
|
||
}
|
||
#[test]
|
||
fn decoration_sets_validate_paths_limits_and_cleanup() {
|
||
let dir = tempfile::tempdir().unwrap();
|
||
let root = dunce::canonicalize(dir.path()).unwrap();
|
||
let path = root.join("a.txt");
|
||
std::fs::write(&path, "text").unwrap();
|
||
let api = api(&root, true, None);
|
||
let item = |style: &str| ide_plugin::Decoration {
|
||
range: ide_core::Range::default(),
|
||
style: style.into(),
|
||
hover: None,
|
||
after: Some("after".into()),
|
||
markdown_hover: None,
|
||
colors: None,
|
||
};
|
||
api.decorations(Some(&path), "set", vec![item("highlight"), item("unknown")]).unwrap();
|
||
assert_eq!(api.shared.decoration_sets()["test"][0].items.len(), 1);
|
||
// Styled items (API 1.17): colors are validated and normalized; an empty style needs colors.
|
||
let colored = |style: &str, foreground: Option<&str>, background: Option<&str>| ide_plugin::Decoration {
|
||
colors: Some(ide_plugin::DecorationColors {
|
||
foreground: foreground.map(Into::into),
|
||
background: background.map(Into::into),
|
||
}),
|
||
..item(style)
|
||
};
|
||
api.decorations(
|
||
Some(&path),
|
||
"styled",
|
||
vec![
|
||
colored("", Some("#1A1A1A"), Some("#FFEB3BFF")),
|
||
colored("highlight", None, Some("#00e5ff")),
|
||
colored("", None, None),
|
||
item(""),
|
||
],
|
||
)
|
||
.unwrap();
|
||
let styled = api.shared.decoration_sets()["test"].iter().find(|s| s.set_id == "styled").unwrap().items.clone();
|
||
assert_eq!(styled.len(), 2);
|
||
let first = styled[0].colors.as_ref().unwrap();
|
||
assert_eq!((first.foreground.as_deref(), first.background.as_deref()), (Some("#1a1a1a"), Some("#ffeb3bff")));
|
||
for bad in ["red", "#fff", "#12345g", "rgb(0,0,0)", "#1234567", "#ffffff; x: y"] {
|
||
let err = api.decorations(Some(&path), "styled", vec![colored("", Some(bad), None)]).unwrap_err();
|
||
assert!(matches!(err, HostError::Invalid(ref m) if m.contains("#rrggbb")), "{bad}: {err}");
|
||
}
|
||
api.decorations(None, "styled", vec![]).unwrap();
|
||
// Decorations follow the file: a path outside the open project is accepted too.
|
||
api.decorations(Some(&root.join("../outside.txt")), "set", vec![]).unwrap();
|
||
assert_eq!(api.shared.decoration_sets()["test"].len(), 2);
|
||
api.decorations(None, "set", vec![]).unwrap();
|
||
assert!(api.shared.decoration_sets()["test"].is_empty());
|
||
for i in 0..50 {
|
||
api.decorations(Some(&path), &format!("s{i}"), vec![]).unwrap();
|
||
}
|
||
assert!(api.decorations(Some(&path), "overflow", vec![]).is_err());
|
||
api.decorations(Some(&path), "s0", vec![item("info"); 10_000]).unwrap();
|
||
assert!(api.decorations(Some(&path), "s1", vec![item("info")]).is_err());
|
||
assert!(
|
||
api.file_decorations(
|
||
Some(&path),
|
||
"tree",
|
||
Some(ide_plugin::FileDecoration {
|
||
badge: Some("abc".into()),
|
||
style: "info".into(),
|
||
tooltip: None,
|
||
note: None,
|
||
icon: None,
|
||
})
|
||
)
|
||
.is_err()
|
||
);
|
||
let note = |text: &str| ide_plugin::FileDecoration {
|
||
badge: None,
|
||
style: String::new(),
|
||
tooltip: Some("why".into()),
|
||
note: Some(text.into()),
|
||
icon: None,
|
||
};
|
||
api.file_decorations(Some(&path), "notes", Some(note("entry point"))).unwrap();
|
||
let notes = |api: &PluginApi| {
|
||
api.shared.decoration_sets()["test"]
|
||
.iter()
|
||
.filter(|s| s.tree && s.set_id == "notes")
|
||
.map(|s| s.file.as_ref().and_then(|f| f.note.clone()))
|
||
.collect::<Vec<_>>()
|
||
};
|
||
assert_eq!(notes(&api), [Some("entry point".to_string())], "kept without a style");
|
||
for bad in ["", "two\nlines", &"x".repeat(201)] {
|
||
assert!(api.file_decorations(Some(&path), "notes", Some(note(bad))).is_err(), "{bad:?}");
|
||
}
|
||
api.file_icon_decorations(
|
||
"icons",
|
||
vec![(path.clone(), "helm-chart".into()), (root.join("chart"), "helm-dir".into())],
|
||
)
|
||
.unwrap();
|
||
let icons = api.shared.decoration_sets()["test"]
|
||
.iter()
|
||
.filter(|s| s.tree && s.set_id == "icons")
|
||
.filter_map(|s| s.file.as_ref().and_then(|f| f.icon.clone()))
|
||
.collect::<Vec<_>>();
|
||
assert_eq!(icons.len(), 2);
|
||
assert!(api.file_icon_decorations("icons", vec![(path.clone(), "BAD ID".into())]).is_err());
|
||
api.shared.clear_plugin("test", true);
|
||
assert!(api.shared.decoration_sets().is_empty());
|
||
}
|
||
#[test]
|
||
fn console_rules_replace_keep_the_last_valid_list_and_go_with_the_plugin() {
|
||
let dir = tempfile::tempdir().unwrap();
|
||
let api = api(dir.path(), false, None);
|
||
let rule = |pattern: &str| ide_plugin::ConsoleRule {
|
||
pattern: pattern.into(),
|
||
ignore_case: false,
|
||
foreground: Some("bright-yellow".into()),
|
||
background: Some("navy".into()),
|
||
styles: vec!["bold".into()],
|
||
whole_line: true,
|
||
enabled: true,
|
||
};
|
||
// No capability, and no trusted project, is needed: the rules read nothing.
|
||
api.console_rules(vec![rule(r"\[AUDIT\]"), rule(r"JOB-\d+")]).unwrap();
|
||
let rules = &api.shared.console_rules()["test"];
|
||
assert_eq!(rules.len(), 2);
|
||
assert_eq!((rules[0].foreground.as_deref(), rules[0].background.as_deref()), (Some("bright-yellow"), None));
|
||
assert!(matches!(api.console_rules(vec![rule("(a+)+b")]), Err(HostError::Invalid(_))));
|
||
assert_eq!(api.shared.console_rules()["test"].len(), 2);
|
||
api.console_rules(vec![rule("x")]).unwrap();
|
||
assert_eq!(api.shared.console_rules()["test"].len(), 1);
|
||
api.console_rules(Vec::new()).unwrap();
|
||
assert!(api.shared.console_rules().is_empty());
|
||
api.console_rules(vec![rule("x")]).unwrap();
|
||
api.shared.clear_plugin("test", true);
|
||
assert!(api.shared.console_rules().is_empty());
|
||
}
|
||
#[test]
|
||
fn decoration_styles_keep_the_severity_palette_the_editor_renders() {
|
||
// Inspection Lens colours typos and weak warnings with their own styles; the
|
||
// host must keep them, not silently drop them the way an unknown style is dropped.
|
||
for style in [
|
||
"highlight",
|
||
"search",
|
||
"added",
|
||
"modified",
|
||
"removed",
|
||
"warning",
|
||
"error",
|
||
"info",
|
||
"typo",
|
||
"weak",
|
||
"underline",
|
||
"strikethrough",
|
||
"muted",
|
||
] {
|
||
assert!(decoration_style(style), "{style} should be a known style");
|
||
}
|
||
assert!(!decoration_style("unknown"));
|
||
}
|
||
fn git(root: &Path, args: &[&str]) {
|
||
let out = StdCommand::new("git").current_dir(root).args(args).output().unwrap();
|
||
assert!(out.status.success(), "{}", String::from_utf8_lossy(&out.stderr));
|
||
}
|
||
#[test]
|
||
fn vcs_reads_reuse_history_and_dirty_blame_and_require_trust() {
|
||
let dir = tempfile::tempdir().unwrap();
|
||
let root = dunce::canonicalize(dir.path()).unwrap();
|
||
git(&root, &["init", "-q"]);
|
||
git(&root, &["config", "user.name", "Test"]);
|
||
git(&root, &["config", "user.email", "[email protected]"]);
|
||
let path = root.join("a.txt");
|
||
std::fs::write(&path, "committed\nsecond\n").unwrap();
|
||
git(&root, &["add", "."]);
|
||
git(&root, &["commit", "-qm", "Subject\n\nBody"]);
|
||
std::fs::write(&path, "disk\nsecond\n").unwrap();
|
||
let api = api(&root, true, Some("unsaved\nsecond\nnew\n"));
|
||
let cancel = CancellationToken::new();
|
||
let history = api.vcs_log(&root, Some("a.txt"), 10, &cancel).unwrap();
|
||
assert_eq!(history.len(), 1);
|
||
assert_eq!(history[0].subject, "Subject");
|
||
assert_eq!(history[0].body.trim(), "Body");
|
||
assert!(history[0].date > 0);
|
||
let lines = api.vcs_blame(None, &root, "a.txt").unwrap();
|
||
assert_eq!(lines.len(), 3);
|
||
assert!(lines[0].commit.bytes().all(|b| b == b'0'));
|
||
assert_eq!(lines[1].commit, history[0].id);
|
||
assert_eq!(lines[2].line, 2);
|
||
let patch = api.vcs_diff(&root, Some("a.txt"), None, &cancel).unwrap().unwrap();
|
||
assert!(patch.contains("+disk"));
|
||
assert!(!patch.contains("+unsaved"));
|
||
assert!(api.vcs_diff(&root, None, Some("--output=outside"), &cancel).is_err());
|
||
assert!(api.vcs_log(&root, Some("../outside"), 1, &cancel).is_err());
|
||
std::fs::remove_file(&path).unwrap();
|
||
assert_eq!(api.vcs_log(&root, Some("a.txt"), 10, &cancel).unwrap().len(), 1);
|
||
assert!(api.vcs_diff(&root, Some("a.txt"), None, &cancel).unwrap().unwrap().contains("-committed"));
|
||
let restricted = super::workbench_tests::api(&root, false, None);
|
||
assert!(restricted.vcs_log(&root, None, 1, &cancel).is_err());
|
||
assert!(restricted.vcs_blame(None, &root, "a.txt").is_err());
|
||
assert!(restricted.vcs_diff(&root, None, None, &cancel).is_err());
|
||
std::fs::write(&path, "x".repeat(5 << 20)).unwrap();
|
||
assert!(api.vcs_diff(&root, None, None, &cancel).is_err());
|
||
}
|
||
/// Security audit lead 2: status has no trust check to stop it, so in Restricted Mode the
|
||
/// repository it opens must not run what its config names.
|
||
#[test]
|
||
fn vcs_status_runs_no_repository_program_in_restricted_mode() {
|
||
let dir = tempfile::tempdir().unwrap();
|
||
let root = dunce::canonicalize(dir.path()).unwrap();
|
||
git(&root, &["init", "-q"]);
|
||
git(&root, &["config", "user.name", "Test"]);
|
||
git(&root, &["config", "user.email", "[email protected]"]);
|
||
git(&root, &["config", "commit.gpgsign", "false"]);
|
||
std::fs::write(root.join("a.txt"), "base\n").unwrap();
|
||
git(&root, &["add", "."]);
|
||
git(&root, &["commit", "-qm", "base"]);
|
||
let marker = dir.path().join("FILTER-RAN");
|
||
let clean = format!("echo x > '{}'; cat", marker.to_str().unwrap().replace('\\', "/"));
|
||
git(&root, &["config", "filter.marker.clean", &clean]);
|
||
std::fs::write(root.join(".gitattributes"), "*.txt filter=marker\n").unwrap();
|
||
// Same length as the committed text: only reading the file tells them apart.
|
||
std::fs::write(root.join("a.txt"), "BASE\n").unwrap();
|
||
|
||
let restricted = api(&root, false, None);
|
||
let status = restricted.vcs_status(&root);
|
||
assert!(!marker.exists(), "status ran the repository's clean filter in Restricted Mode");
|
||
assert_eq!(status.unwrap().unwrap().changed_files, 2);
|
||
|
||
// Control: the same call runs the fixture's filter in a trusted project.
|
||
api(&root, true, None).vcs_status(&root).unwrap();
|
||
assert!(marker.exists(), "control: a trusted status did not run the clean filter, fixture is not live");
|
||
}
|
||
|
||
#[cfg(unix)]
|
||
#[test]
|
||
fn vcs_reads_never_run_repository_configured_helpers() {
|
||
let dir = tempfile::tempdir().unwrap();
|
||
let root = dunce::canonicalize(dir.path()).unwrap();
|
||
git(&root, &["init", "-q"]);
|
||
git(&root, &["config", "user.name", "Test"]);
|
||
git(&root, &["config", "user.email", "[email protected]"]);
|
||
std::fs::write(root.join("a.txt"), "base\n").unwrap();
|
||
git(&root, &["add", "."]);
|
||
git(&root, &["commit", "-qm", "base"]);
|
||
let marker = root.join("HELPER-RAN");
|
||
let helper = format!("touch '{}'", marker.display());
|
||
for key in [
|
||
"core.fsmonitor",
|
||
"diff.external",
|
||
"diff.test.command",
|
||
"diff.test.textconv",
|
||
"filter.test.clean",
|
||
"filter.test.process",
|
||
] {
|
||
git(&root, &["config", key, &helper]);
|
||
}
|
||
git(&root, &["config", "filter.test.required", "true"]);
|
||
assert!(!marker.exists(), "setup ran helper");
|
||
std::fs::write(root.join(".gitattributes"), "*.txt diff=test filter=test\n").unwrap();
|
||
std::fs::write(root.join("a.txt"), "changed\n").unwrap();
|
||
let api = api(&root, true, Some("unsaved\n"));
|
||
let cancel = CancellationToken::new();
|
||
api.vcs_log(&root, None, 10, &cancel).unwrap();
|
||
assert!(!marker.exists(), "log ran helper");
|
||
api.vcs_blame(None, &root, "a.txt").unwrap();
|
||
assert!(!marker.exists(), "blame ran helper");
|
||
api.vcs_diff(&root, None, None, &cancel).unwrap();
|
||
assert!(!marker.exists());
|
||
}
|
||
}
|