Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions rust/contracts/rust-core-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,13 @@ or removing a command fails until the table changes in the same commit.
| `workspace.configureIndexing` | Install a process-local, canonical-root indexing policy before workspace consumers run; returns a monotonic revision, effective directory reasons and Git failure state; see `rust/fixtures/workspace/indexing-policy-v1.json` |
| `workspace.refreshIndexing` | Refresh external Git decisions for a live `root`, `ownerId`, `revision` lease without changing preferences or release tokens; returns lazy `IndexingStatus` |
| `workspace.filterChanges` | Filter an array of workspace-relative paths, or `{ paths, hiddenFilePatterns }` whose `!` entries turn default exclusions off, against Core's default hidden folders and patterns, including the folders enabled plugins declare; preserve order, spelling, and the empty root path. Native event loss bypasses filtering. Hosts relativize to the most specific explicit watch root and retain explicitly watched files |
| `workspace.findFiles` | Bounded plugin disk file-name search with a query-bound cursor (API-08) |
| `workspace.findText` | Bounded plugin disk text search, without dirty buffers (API-08) |
| `workspace.watchPlan` | Validate a local watch target before native registration (API-07) |
| `workspace.watchEvents` | Filter and bound one native root-relative event batch (API-07) |
| `workspace.readDirectory` | Return one bounded no-follow disk directory page for API-06; see Workspace disk reads below |
| `workspace.stat` | Stat a workspace leaf without following it or any ancestor link (API-06) |
| `workspace.readFile` | Read at most 128 KiB of regular-file disk bytes without following links (API-06) |
| `workspace.listDirectories` | Return one visible level of children for each requested workspace-relative directory (`""` is the root); missing, hidden, external/cyclic symlink, or nested-worktree paths are omitted; workspace-internal aliases are browsable and each listing reports `linked: true` when reached through an alias, and each listing reports the nested worktrees it left out. Child directories have `children: []` only when a shallow probe proves them visibly empty; otherwise children remain omitted (unloaded), with no recursive tree allocation. Children list directories first, then names in IDEA's natural file-name order (digit runs compare by value, case-insensitive, `-` next to `_`; fixture `rust/fixtures/workspace/file-name-order-v1.json`) |
| `workspace.orderEntries` | Order natively enumerated directory siblings |
| `workspace.projectTree.rows` | Say which loaded project tree nodes show as rows, keep the selection on a row that shows, and find the row the top of the view was on, without reading the disk (from `lithe-project-tree`) |
Expand Down Expand Up @@ -7492,6 +7499,93 @@ to exist, creates the copy without replacing anything (`FileManager.copyItem`, w
appeared meanwhile, adds the name to `existing` and plans again. After 512 rounds the host reports
the stable `destination_exists` error. Cases are in `rust/fixtures/workspace/file-copy-names-v1.json`.

### Plugin workspace search and watches

`workspace.findFiles` takes `{root,query,fileMask?,cursor?,limit?,hiddenDirectoryNames?,hiddenFilePatterns?}`.
`workspace.findText` takes `{root,pattern,fileMask?,caseSensitive?,wholeWords?,regularExpression?,cursor?,limit?,hiddenDirectoryNames?,hiddenFilePatterns?}`.
Queries/patterns are nonempty and at most 4096 UTF-8 bytes; masks have the same
byte cap. Limits default to 100 and accept 1–200. Matching reuses the workspace
literal/regular-expression matcher and comma-separated basename globs. Files
sort by UTF-8 relative path bytes, text hits by path then one-based line, one
hit per line with `range:null` and a trimmed preview of at most 500 characters.
The returned envelope is `{matches,nextCursor,isTruncated,source:"disk"}`;
file matches are `{path}`, text matches are `{path,line,preview,range:null}`.

Search walks no-follow handles, applies shared hidden rules and the installed
indexing exclusion policy (including its Git-ignore decisions), skips links and
special entries, and consults no dirty buffer. File contents use the existing
search text decoder, with binary and undecodable files omitted. At most 2 MiB
are read per text file, with one sentinel byte; traversal depth caps at 128.
Oversized files, deeper directories and an overall 200-hit cap set
`isTruncated:true`. At most a page plus one candidate is retained; serialized
pages reserve host envelopes under 900 KiB. A live cursor binds canonical root,
query/options, effective visibility and indexing generation, keeps no server
snapshot, and resumes after the last result; page size may change. A changed
query/policy or malformed cursor uses `invalid_request`; invalid regex uses
`parse_failed`. Cancellation and a 30-second deadline are checked per entry,
chunk and line; native syscalls can still block. I/O failures are returned,
except concurrently vanished entries. Remote workspaces are unsupported.

`workspace.watchPlan` takes `{root,path,recursive?}` (false by default) and
returns `{root,path,isDirectory,recursive}` with a canonical root. Targets
must exist and be a regular file or directory, with no plugin-supplied link
component. The native adapter watches root changes, checks the root/target
identity again after registration, and owns the native stream and its cleanup.
`workspace.watchEvents` takes this plan plus `paths`, optional
`requiresRescan`, `hiddenDirectoryNames`, `hiddenFilePatterns`. It returns
`{changedPaths,requiresRescan}` with sorted, deduplicated, root-relative visible
names. File watches select the exact file; directory watches select immediate
children or descendants when recursive. Ancestor changes request a rescan.
Deleted names remain valid, existing ancestor links/reparse points are rejected,
and leaf links are omitted. Native loss, malformed names, more than 1000 input
paths or the 900 KiB event budget set `requiresRescan:true`. This command is
untraced because it runs on each native watch batch. Native registration and
plugin/context/session ownership remain in the desktop adapter, without polling,
filesystem writes or retained file content.

Fixture: `rust/fixtures/workspace/find-watch-v1.json`.

### Workspace disk reads

`workspace.readDirectory` takes `{root,path,cursor?,limit?,hiddenDirectoryNames?,hiddenFilePatterns?}`.
`workspace.stat` takes `{root,path}` and `workspace.readFile` takes
`{root,path,maximumBytes?}`. `root` is an absolute authorized local directory;
`path` is a strictly `/`-separated path below it, at most 4096 UTF-8 bytes with
no backslash, NUL, absolute prefix, empty, `.` or `..` component. Empty path names
the root for listing/stat only. Windows components obey native storable-name rules.
Unknown payload fields and invalid bounds fail with `invalid_request`.

Listing defaults to 200, accepts 1–1000, and retains at most `limit + 1` ordered
candidates while streaming a directory. It applies shared effective hidden rules,
omits links/reparse points and unrepresentable paths, and returns
`{entries:[{name,path,kind}],nextCursor}`. Entries sort by UTF-8 name bytes.
A serialized page has a 900 KiB budget, reserving space for host resource envelopes,
so a page may be shorter than its limit. Cursors bind canonical root, directory and
filters and resume after the last name, without keeping a server-side snapshot.
`stat` returns `{path,kind,size,modifiedMilliseconds}`; `kind` is `file`,
`directory`, `symlink` or `other`, and unavailable/pre-epoch timestamps are null.
The leaf's own metadata is returned; ancestor links are refused.

Reading accepts `maximumBytes` in 1–131072, default 131072, and returns
`{path,source:"disk",byteCount,isTruncated,identity,text?,encoding?,base64?}`.
It reads at most the limit plus one byte, discards the sentinel and hashes only
the returned prefix. `identity` is `{sha256,byteCount,isTruncated}` and describes
those exact bytes, not an atomic whole-file snapshot. Strict UTF-8 without control
characters except tab/CR/LF returns text with `encoding:"utf-8"` (BOM preserved);
all other prefixes, including incomplete UTF-8, use standard base64. Exactly one
of text/base64 is present. No editor buffer is consulted.

Unix walks held directory descriptors with `openat/O_NOFOLLOW`; Windows rejects
reparse points and retains directory handles without write/delete sharing.
Non-regular files cannot be read. These commands reject `remoteWorkspace` before
local I/O. Missing roots use `workspace_not_found`, missing entries use
`entry_not_found`, link traversal and access failures use `permission_denied`,
and other I/O failures use `unknown`. Enumeration and chunk reads check cancellation
and a 30-second deadline; native filesystem calls may still block in the kernel.
The host checks context/root ownership and wraps returned paths as resources;
it maps the Core errors to the public API-06 errors.
Fixture: `rust/fixtures/workspace/read-v1.json`.

`workspace.createEntry`, `workspace.renameEntry`, `workspace.copyEntry` and `workspace.resolveEntry`
change and name entries below a local workspace root on a plugin's behalf (plan API-11). Each takes
`root`, the absolute path of the root as the host spells it, and `path`, the entry below it.
Expand Down
21 changes: 21 additions & 0 deletions rust/crates/core/src/protocol/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,20 @@ pub enum WorkspaceCommand {
RefreshIndexing,
/// Releases an indexing generation when its project closes.
ReleaseIndexing,
/// Reads a bounded no-follow directory page (`workspace.readDirectory`).
ReadDirectory,
/// Stats a workspace entry without following its leaf (`workspace.stat`).
Stat,
/// Reads bounded disk bytes without following links (`workspace.readFile`).
ReadFile,
/// Searches plugin-visible disk file names (`workspace.findFiles`).
FindFiles,
/// Searches plugin-visible disk text (`workspace.findText`).
FindText,
/// Validates a native workspace watch target (`workspace.watchPlan`).
WatchPlan,
/// Projects a native watch batch (`workspace.watchEvents`).
WatchEvents,
/// Lists the visible children of project directories (`workspace.listDirectories`).
ListDirectories,
/// Says which loaded project tree nodes show as rows and keeps the selection on one
Expand Down Expand Up @@ -1367,6 +1381,13 @@ impl CoreCommand {
}
"workspace.refreshIndexing" => Some(Self::Workspace(WorkspaceCommand::RefreshIndexing)),
"workspace.filterChanges" => Some(Self::Workspace(WorkspaceCommand::FilterChanges)),
"workspace.findFiles" => Some(Self::Workspace(WorkspaceCommand::FindFiles)),
"workspace.findText" => Some(Self::Workspace(WorkspaceCommand::FindText)),
"workspace.watchPlan" => Some(Self::Workspace(WorkspaceCommand::WatchPlan)),
"workspace.watchEvents" => Some(Self::Workspace(WorkspaceCommand::WatchEvents)),
"workspace.readDirectory" => Some(Self::Workspace(WorkspaceCommand::ReadDirectory)),
"workspace.stat" => Some(Self::Workspace(WorkspaceCommand::Stat)),
"workspace.readFile" => Some(Self::Workspace(WorkspaceCommand::ReadFile)),
"workspace.listDirectories" => Some(Self::Workspace(WorkspaceCommand::ListDirectories)),
"workspace.projectTree.rows" => {
Some(Self::Workspace(WorkspaceCommand::ProjectTreeRows))
Expand Down
21 changes: 21 additions & 0 deletions rust/crates/core/src/runtime/dispatcher/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,27 @@ pub(super) fn dispatch(
payload: Value,
) -> CoreResponse {
match command {
WorkspaceCommand::FindFiles => {
plugin_response(id, payload, project::workspace_find::find_files_command)
}
WorkspaceCommand::FindText => {
plugin_response(id, payload, project::workspace_find::find_text_command)
}
WorkspaceCommand::WatchPlan => {
plugin_response(id, payload, project::workspace_watch::plan_command)
}
WorkspaceCommand::WatchEvents => {
plugin_response(id, payload, project::workspace_watch::events_command)
}
WorkspaceCommand::ReadDirectory => {
plugin_response(id, payload, project::workspace_read::read_directory_command)
}
WorkspaceCommand::Stat => {
plugin_response(id, payload, project::workspace_read::stat_command)
}
WorkspaceCommand::ReadFile => {
plugin_response(id, payload, project::workspace_read::read_file_command)
}
WorkspaceCommand::SearchPresentation => match serde_json::from_value::<
project::SearchPresentationRequest,
>(payload)
Expand Down
2 changes: 1 addition & 1 deletion rust/crates/core/src/runtime/request_trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ pub(crate) fn is_hot(method: &str) -> bool {
// File watchers forward one batch per debounce window.
|| matches!(
method,
"workspace.filterChanges" | "lsp.workspaceFilesChanged" | "git.classifyMetadataChanges"
"workspace.watchEvents" | "workspace.filterChanges" | "lsp.workspaceFilesChanged" | "git.classifyMetadataChanges"
)
// The status bar sums the instance's memory every few seconds while it is shown,
// and active runs probe their process tree's listening ports periodically.
Expand Down
3 changes: 3 additions & 0 deletions rust/crates/core/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,6 @@ mod toolchain_queries;
mod toolchain_resolution;
mod toolchain_vectors;
mod vcs;

mod workspace_find_watch;
mod workspace_read;
161 changes: 161 additions & 0 deletions rust/crates/core/src/tests/workspace_find_watch.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
//! API-07/08 commands against real files and the shared host-boundary fixture.
use super::support::{temporary_root, Cleanup};
use crate::execute_json;
use serde_json::{json, Value};
use std::fs;
use std::path::PathBuf;

fn fixture() -> Value {
serde_json::from_str(include_str!(
"../../../../fixtures/workspace/find-watch-v1.json"
))
.unwrap()
}

fn workspace(label: &str) -> (PathBuf, Cleanup) {
let root = temporary_root(label);
fs::create_dir_all(&root).unwrap();
for file in fixture()["files"].as_array().unwrap() {
let path = root.join(file["path"].as_str().unwrap());
fs::create_dir_all(path.parent().unwrap()).unwrap();
fs::write(path, file["text"].as_str().unwrap()).unwrap();
}
(root.clone(), Cleanup(root))
}

fn execute(command: &str, payload: Value) -> Value {
serde_json::from_str(&execute_json(
&json!({"command":command,"payload":payload}).to_string(),
))
.unwrap()
}

#[test]
fn workspace_find_pages_match_the_fixture_and_bind_query_options() {
let (root, _cleanup) = workspace("workspace-find");
let fixture = fixture();
for (method, key, expected) in [
("workspace.findFiles", "fileRequest", "filePaths"),
("workspace.findText", "textRequest", "textMatches"),
] {
let mut request = fixture[key].clone();
request["root"] = json!(root);
let mut actual = Vec::new();
for _ in 0..5 {
let response = execute(method, request.clone());
assert_eq!(response["ok"], true, "{response}");
assert_eq!(response["data"]["source"], "disk");
for item in response["data"]["matches"].as_array().unwrap() {
actual.push(if expected == "filePaths" {
item["path"].clone()
} else {
item.clone()
});
}
let cursor = &response["data"]["nextCursor"];
if cursor.is_null() {
break;
}
request["cursor"] = cursor.clone();
let mut changed = request.clone();
changed["fileMask"] = json!("*.different");
assert_eq!(execute(method, changed)["error"]["code"], "invalid_request");
}
assert_eq!(json!(actual), fixture[expected]);
}
let bad = execute(
"workspace.findText",
json!({"root":root,"pattern":"[","regularExpression":true}),
);
assert_eq!(bad["error"]["code"], "parse_failed");
}

#[test]
fn workspace_find_reports_caps_and_does_not_read_binary_or_oversized_files() {
let (root, _cleanup) = workspace("workspace-find-caps");
let lines = "alpha\n".repeat(202);
fs::write(root.join("a.txt"), lines).unwrap();
fs::write(root.join("binary.txt"), b"alpha\0").unwrap();
fs::write(root.join("large.txt"), vec![b'a'; 2 * 1024 * 1024 + 1]).unwrap();
let response = execute(
"workspace.findText",
json!({"root":root,"pattern":"alpha","limit":200}),
);
assert_eq!(response["ok"], true, "{response}");
assert_eq!(response["data"]["matches"].as_array().unwrap().len(), 200);
assert_eq!(response["data"]["isTruncated"], true);
assert!(response["data"]["nextCursor"].is_null());
let response = execute(
"workspace.findText",
json!({"root":root,"pattern":"BETA","caseSensitive":true,"wholeWords":true}),
);
assert!(response["data"]["matches"].as_array().unwrap().is_empty());
assert_eq!(response["data"]["isTruncated"], true);
}

#[test]
fn workspace_watch_plans_and_events_are_scoped_filtered_and_bounded() {
let (root, _cleanup) = workspace("workspace-watch-events");
let plan = execute(
"workspace.watchPlan",
json!({"root":root,"path":"src","recursive":true}),
);
assert_eq!(plan["ok"], true, "{plan}");
assert_eq!(plan["data"]["isDirectory"], true);
let request = json!({"root":root,"path":"","isDirectory":true,"recursive":true,"paths":fixture()["watch"]["paths"]});
let response = execute("workspace.watchEvents", request.clone());
assert_eq!(response["data"], fixture()["watch"]["expected"]);
let response = execute(
"workspace.watchEvents",
json!({"root":root,"path":"src","isDirectory":true,"recursive":false,"paths":["src/b.txt","src/deleted","src/deep/file","a.txt"]}),
);
assert_eq!(
response["data"]["changedPaths"],
json!(["src/b.txt", "src/deleted"])
);
let response = execute(
"workspace.watchEvents",
json!({"root":root,"path":"a.txt","isDirectory":false,"paths":["a.txt","z.md"],"requiresRescan":true}),
);
assert_eq!(response["data"]["changedPaths"], json!(["a.txt"]));
assert_eq!(response["data"]["requiresRescan"], true);
let mut overflow = request;
overflow["paths"] = json!(vec!["a.txt"; 1001]);
assert_eq!(
execute("workspace.watchEvents", overflow)["data"]["requiresRescan"],
true
);
assert_eq!(
execute(
"workspace.watchPlan",
json!({"root":root,"path":"../escape"})
)["error"]["code"],
"invalid_request"
);
}

#[cfg(unix)]
#[test]
fn workspace_find_and_watch_never_traverse_a_link() {
let (root, _cleanup) = workspace("workspace-find-links");
std::os::unix::fs::symlink(root.join("src"), root.join("alias")).unwrap();
std::os::unix::fs::symlink(root.join("a.txt"), root.join("link.txt")).unwrap();
assert_eq!(
execute("workspace.watchPlan", json!({"root":root,"path":"alias"}))["error"]["code"],
"permission_denied"
);
let response = execute(
"workspace.findFiles",
json!({"root":root,"query":".txt","limit":200}),
);
assert_eq!(
response["data"]["matches"],
json!([{"path":"a.txt"},{"path":"src/b.txt"}])
);
let response = execute(
"workspace.watchEvents",
json!({"root":root,"path":"","isDirectory":true,"recursive":true,"paths":["alias/b.txt","link.txt"]}),
);
assert_eq!(response["data"]["changedPaths"], json!([]));
}
Loading
Loading