дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-11-22 09:30:46 +02:00
Порівняти коміти
23
Коміти
| Автор | SHA1 | Дата | |
|---|---|---|---|
|
|
1a12c88f0c | ||
|
|
6400206a94 | ||
|
|
49d8e1bc71 | ||
|
|
8a56c204f4 | ||
|
|
5bf4d4bb15 | ||
|
|
84dcd99f7b | ||
|
|
ea8351d9f6 | ||
|
|
aab6d9ffdc | ||
|
|
72f286d619 | ||
|
|
ecb0631b89 | ||
|
|
f26d3e2612 | ||
|
|
e36cd37e56 | ||
|
|
0f652824b3 | ||
|
|
10aa367df3 | ||
|
|
9622bff6bd | ||
|
|
ad1621dee6 | ||
|
|
450c78b90d | ||
|
|
62058d5932 | ||
|
|
f0c383fc6d | ||
|
|
4766a4bdef | ||
|
|
44e7508af3 | ||
|
|
42b99b9df7 | ||
|
|
72c84b9131 |
No files matched your search
@@ -5,6 +5,7 @@ on:
|
||||
pull_request:
|
||||
branches: [main]
|
||||
types: [opened, synchronize, reopened]
|
||||
merge_group:
|
||||
workflow_dispatch:
|
||||
|
||||
permissions:
|
||||
@@ -12,8 +13,9 @@ permissions:
|
||||
|
||||
concurrency:
|
||||
group: ci-${{ github.ref }}
|
||||
# A newer push supersedes the run in progress, on main too.
|
||||
cancel-in-progress: true
|
||||
# A newer push supersedes a pull request's run in progress. Runs on main and in the merge
|
||||
# queue finish, so a broken main shows up and the queue's required checks report.
|
||||
cancel-in-progress: ${{ github.event_name == 'pull_request' }}
|
||||
|
||||
env:
|
||||
CARGO_TERM_COLOR: always
|
||||
@@ -23,7 +25,7 @@ env:
|
||||
|
||||
jobs:
|
||||
# Which parts a pull request touches, so jobs it can't affect skip and leave the
|
||||
# shared runners to others. Pushes and manual runs always run everything.
|
||||
# shared runners to others. Pushes, the merge queue and manual runs always run everything.
|
||||
changes:
|
||||
name: Changes
|
||||
runs-on: ubuntu-latest
|
||||
@@ -140,6 +142,22 @@ jobs:
|
||||
if: runner.os == 'Linux'
|
||||
run: git diff --exit-code src/bindings.ts src-tauri/tests/commands.snap
|
||||
|
||||
# One check that always reports, for the ruleset to require: a skipped matrix job reports
|
||||
# under its unexpanded name, so the per-OS checks never appear on frontend-only PRs.
|
||||
rust-checks:
|
||||
name: Rust checks
|
||||
needs: [changes, rust]
|
||||
if: always()
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 5
|
||||
steps:
|
||||
- env:
|
||||
CHANGES: ${{ needs.changes.result }}
|
||||
RUST: ${{ needs.rust.result }}
|
||||
run: |
|
||||
echo "changes: $CHANGES, rust: $RUST"
|
||||
[ "$CHANGES" = success ] && { [ "$RUST" = success ] || [ "$RUST" = skipped ]; }
|
||||
|
||||
# The remote development server ships as a static Linux binary (release-build.yml):
|
||||
# keep that build working.
|
||||
server:
|
||||
|
||||
Створено
+60
-46
@@ -4144,7 +4144,7 @@ checksum = "3d3067d79b975e8844ca9eb072e16b31c3c1c36928edf9c6789548c524d0d954"
|
||||
|
||||
[[package]]
|
||||
name = "ide-agent"
|
||||
version = "0.1.48"
|
||||
version = "0.1.49"
|
||||
dependencies = [
|
||||
"blake3",
|
||||
"dunce",
|
||||
@@ -4169,7 +4169,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "ide-analysis"
|
||||
version = "0.1.48"
|
||||
version = "0.1.49"
|
||||
dependencies = [
|
||||
"ast-grep-core",
|
||||
"ide-core",
|
||||
@@ -4183,7 +4183,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "ide-android"
|
||||
version = "0.1.48"
|
||||
version = "0.1.49"
|
||||
dependencies = [
|
||||
"ide-core",
|
||||
"serde",
|
||||
@@ -4194,7 +4194,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "ide-build"
|
||||
version = "0.1.48"
|
||||
version = "0.1.49"
|
||||
dependencies = [
|
||||
"blake3",
|
||||
"chardetng",
|
||||
@@ -4212,7 +4212,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "ide-cli"
|
||||
version = "0.1.48"
|
||||
version = "0.1.49"
|
||||
dependencies = [
|
||||
"dirs 6.0.0",
|
||||
"serde",
|
||||
@@ -4226,7 +4226,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "ide-clipboard"
|
||||
version = "0.1.48"
|
||||
version = "0.1.49"
|
||||
dependencies = [
|
||||
"gtk",
|
||||
"objc2 0.6.5",
|
||||
@@ -4237,7 +4237,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "ide-command"
|
||||
version = "0.1.48"
|
||||
version = "0.1.49"
|
||||
dependencies = [
|
||||
"heck 0.5.0",
|
||||
"proc-macro2",
|
||||
@@ -4248,7 +4248,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "ide-core"
|
||||
version = "0.1.48"
|
||||
version = "0.1.49"
|
||||
dependencies = [
|
||||
"dunce",
|
||||
"encoding_rs",
|
||||
@@ -4265,7 +4265,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "ide-coverage"
|
||||
version = "0.1.48"
|
||||
version = "0.1.49"
|
||||
dependencies = [
|
||||
"ide-core",
|
||||
"ide-run",
|
||||
@@ -4279,7 +4279,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "ide-dap"
|
||||
version = "0.1.48"
|
||||
version = "0.1.49"
|
||||
dependencies = [
|
||||
"base64 0.22.1",
|
||||
"futures",
|
||||
@@ -4308,7 +4308,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "ide-db"
|
||||
version = "0.1.48"
|
||||
version = "0.1.49"
|
||||
dependencies = [
|
||||
"base64 0.22.1",
|
||||
"bb8",
|
||||
@@ -4319,6 +4319,7 @@ dependencies = [
|
||||
"futures",
|
||||
"ide-core",
|
||||
"ide-http",
|
||||
"ide-sdk",
|
||||
"ide-ssh",
|
||||
"notify",
|
||||
"open",
|
||||
@@ -4340,11 +4341,12 @@ dependencies = [
|
||||
"tracing",
|
||||
"uuid",
|
||||
"zeroize",
|
||||
"zip",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ide-docker"
|
||||
version = "0.1.48"
|
||||
version = "0.1.49"
|
||||
dependencies = [
|
||||
"ide-core",
|
||||
"serde",
|
||||
@@ -4357,7 +4359,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "ide-extension"
|
||||
version = "0.1.48"
|
||||
version = "0.1.49"
|
||||
dependencies = [
|
||||
"base64 0.22.1",
|
||||
"blake3",
|
||||
@@ -4376,9 +4378,20 @@ dependencies = [
|
||||
"zip",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ide-flutter"
|
||||
version = "0.1.49"
|
||||
dependencies = [
|
||||
"ide-android",
|
||||
"ide-core",
|
||||
"parking_lot",
|
||||
"serde_json",
|
||||
"tempfile",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ide-fonts"
|
||||
version = "0.1.48"
|
||||
version = "0.1.49"
|
||||
dependencies = [
|
||||
"objc2-core-foundation",
|
||||
"objc2-core-text",
|
||||
@@ -4390,7 +4403,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "ide-github"
|
||||
version = "0.1.48"
|
||||
version = "0.1.49"
|
||||
dependencies = [
|
||||
"ide-core",
|
||||
"ide-vcs",
|
||||
@@ -4403,7 +4416,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "ide-heap"
|
||||
version = "0.1.48"
|
||||
version = "0.1.49"
|
||||
dependencies = [
|
||||
"criterion",
|
||||
"ide-core",
|
||||
@@ -4421,7 +4434,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "ide-host"
|
||||
version = "0.1.48"
|
||||
version = "0.1.49"
|
||||
dependencies = [
|
||||
"base64 0.22.1",
|
||||
"blake3",
|
||||
@@ -4442,6 +4455,7 @@ dependencies = [
|
||||
"ide-db",
|
||||
"ide-docker",
|
||||
"ide-extension",
|
||||
"ide-flutter",
|
||||
"ide-heap",
|
||||
"ide-http",
|
||||
"ide-index",
|
||||
@@ -4495,7 +4509,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "ide-http"
|
||||
version = "0.1.48"
|
||||
version = "0.1.49"
|
||||
dependencies = [
|
||||
"ide-core",
|
||||
"ide-vfs",
|
||||
@@ -4514,7 +4528,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "ide-index"
|
||||
version = "0.1.48"
|
||||
version = "0.1.49"
|
||||
dependencies = [
|
||||
"blake3",
|
||||
"criterion",
|
||||
@@ -4536,7 +4550,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "ide-jdwp"
|
||||
version = "0.1.48"
|
||||
version = "0.1.49"
|
||||
dependencies = [
|
||||
"ide-jvm-inspect",
|
||||
"proptest",
|
||||
@@ -4548,7 +4562,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "ide-jsonrpc"
|
||||
version = "0.1.48"
|
||||
version = "0.1.49"
|
||||
dependencies = [
|
||||
"encoding_rs",
|
||||
"futures",
|
||||
@@ -4565,7 +4579,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "ide-jvm-attach"
|
||||
version = "0.1.48"
|
||||
version = "0.1.49"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"tempfile",
|
||||
@@ -4574,7 +4588,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "ide-jvm-inspect"
|
||||
version = "0.1.48"
|
||||
version = "0.1.49"
|
||||
dependencies = [
|
||||
"futures",
|
||||
"serde",
|
||||
@@ -4585,7 +4599,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "ide-language"
|
||||
version = "0.1.48"
|
||||
version = "0.1.49"
|
||||
dependencies = [
|
||||
"blake3",
|
||||
"criterion",
|
||||
@@ -4644,7 +4658,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "ide-lsp"
|
||||
version = "0.1.48"
|
||||
version = "0.1.49"
|
||||
dependencies = [
|
||||
"dunce",
|
||||
"encoding_rs",
|
||||
@@ -4672,7 +4686,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "ide-mcp"
|
||||
version = "0.1.48"
|
||||
version = "0.1.49"
|
||||
dependencies = [
|
||||
"bytes",
|
||||
"dunce",
|
||||
@@ -4691,7 +4705,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "ide-notebook"
|
||||
version = "0.1.48"
|
||||
version = "0.1.49"
|
||||
dependencies = [
|
||||
"futures",
|
||||
"ide-core",
|
||||
@@ -4714,7 +4728,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "ide-plugin"
|
||||
version = "0.1.48"
|
||||
version = "0.1.49"
|
||||
dependencies = [
|
||||
"blake3",
|
||||
"dunce",
|
||||
@@ -4737,7 +4751,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "ide-profiler"
|
||||
version = "0.1.48"
|
||||
version = "0.1.49"
|
||||
dependencies = [
|
||||
"ide-core",
|
||||
"ide-jvm-attach",
|
||||
@@ -4751,7 +4765,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "ide-remote"
|
||||
version = "0.1.48"
|
||||
version = "0.1.49"
|
||||
dependencies = [
|
||||
"dunce",
|
||||
"futures",
|
||||
@@ -4771,7 +4785,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "ide-run"
|
||||
version = "0.1.48"
|
||||
version = "0.1.49"
|
||||
dependencies = [
|
||||
"dunce",
|
||||
"encoding_rs",
|
||||
@@ -4795,7 +4809,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "ide-scaffold"
|
||||
version = "0.1.48"
|
||||
version = "0.1.49"
|
||||
dependencies = [
|
||||
"ide-core",
|
||||
"ide-tools",
|
||||
@@ -4811,7 +4825,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "ide-sdk"
|
||||
version = "0.1.48"
|
||||
version = "0.1.49"
|
||||
dependencies = [
|
||||
"dunce",
|
||||
"flate2",
|
||||
@@ -4834,7 +4848,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "ide-search"
|
||||
version = "0.1.48"
|
||||
version = "0.1.49"
|
||||
dependencies = [
|
||||
"criterion",
|
||||
"encoding_rs",
|
||||
@@ -4855,7 +4869,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "ide-spell"
|
||||
version = "0.1.48"
|
||||
version = "0.1.49"
|
||||
dependencies = [
|
||||
"ide-core",
|
||||
"parking_lot",
|
||||
@@ -4868,7 +4882,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "ide-ssh"
|
||||
version = "0.1.48"
|
||||
version = "0.1.49"
|
||||
dependencies = [
|
||||
"dirs 6.0.0",
|
||||
"flate2",
|
||||
@@ -4893,7 +4907,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "ide-streams"
|
||||
version = "0.1.48"
|
||||
version = "0.1.49"
|
||||
dependencies = [
|
||||
"ide-core",
|
||||
"ide-language",
|
||||
@@ -4906,7 +4920,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "ide-syntax"
|
||||
version = "0.1.48"
|
||||
version = "0.1.49"
|
||||
dependencies = [
|
||||
"ide-core",
|
||||
"ide-language",
|
||||
@@ -4921,7 +4935,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "ide-terminal"
|
||||
version = "0.1.48"
|
||||
version = "0.1.49"
|
||||
dependencies = [
|
||||
"ide-core",
|
||||
"libc",
|
||||
@@ -4934,7 +4948,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "ide-text"
|
||||
version = "0.1.48"
|
||||
version = "0.1.49"
|
||||
dependencies = [
|
||||
"criterion",
|
||||
"ide-core",
|
||||
@@ -4947,7 +4961,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "ide-tools"
|
||||
version = "0.1.48"
|
||||
version = "0.1.49"
|
||||
dependencies = [
|
||||
"flate2",
|
||||
"hex",
|
||||
@@ -4968,7 +4982,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "ide-vcs"
|
||||
version = "0.1.48"
|
||||
version = "0.1.49"
|
||||
dependencies = [
|
||||
"blake3",
|
||||
"dunce",
|
||||
@@ -4989,7 +5003,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "ide-vfs"
|
||||
version = "0.1.48"
|
||||
version = "0.1.49"
|
||||
dependencies = [
|
||||
"chardetng",
|
||||
"dunce",
|
||||
@@ -5015,7 +5029,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "ide-web"
|
||||
version = "0.1.48"
|
||||
version = "0.1.49"
|
||||
dependencies = [
|
||||
"dirs 6.0.0",
|
||||
"ide-core",
|
||||
@@ -5031,7 +5045,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "ide-workspace"
|
||||
version = "0.1.48"
|
||||
version = "0.1.49"
|
||||
dependencies = [
|
||||
"blake3",
|
||||
"dunce",
|
||||
@@ -6660,7 +6674,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "openidea"
|
||||
version = "0.1.48"
|
||||
version = "0.1.49"
|
||||
dependencies = [
|
||||
"base64 0.22.1",
|
||||
"blake3",
|
||||
|
||||
+1
-1
@@ -5,7 +5,7 @@ members = ["crates/*", "src-tauri"]
|
||||
exclude = ["sdk", "plugins"]
|
||||
|
||||
[workspace.package]
|
||||
version = "0.1.48"
|
||||
version = "0.1.49"
|
||||
edition = "2024"
|
||||
rust-version = "1.96"
|
||||
license = "Apache-2.0"
|
||||
|
||||
@@ -125,6 +125,9 @@ type Connector = Arc<
|
||||
/// Returns the configured MCP servers (User and Workspace) for this project.
|
||||
pub type McpServersFn = dyn Fn() -> Vec<McpServer> + Send + Sync;
|
||||
|
||||
/// Returns the executable of the running built-in MCP server, if any.
|
||||
pub type McpExecutableFn = dyn Fn() -> Option<PathBuf> + Send + Sync;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
|
||||
struct CopilotBinaryKey {
|
||||
command: String,
|
||||
@@ -148,6 +151,7 @@ struct Shared {
|
||||
/// [`IDLE_RELEASE`], in milliseconds (shorter in tests).
|
||||
idle_release_ms: AtomicU64,
|
||||
mcp_servers_hook: RwLock<Option<Arc<McpServersFn>>>,
|
||||
mcp_executable_hook: RwLock<Option<Arc<McpExecutableFn>>>,
|
||||
/// Monotonic id for [`AgentEvent::Attention`] episodes.
|
||||
attention_seq: AtomicU64,
|
||||
}
|
||||
@@ -220,8 +224,12 @@ impl Shared {
|
||||
overrides: &BTreeMap<String, bool>,
|
||||
) -> McpForSession {
|
||||
let builtin_off = overrides.get(mcp::BUILTIN_MCP_NAME) == Some(&false);
|
||||
let mut servers =
|
||||
if builtin_off { Vec::new() } else { mcp_servers_for(init.mcp_stdio(), &self.paths.project_root) };
|
||||
let mut servers = if builtin_off {
|
||||
Vec::new()
|
||||
} else {
|
||||
let exe = self.mcp_executable_hook.read().as_ref().and_then(|hook| hook());
|
||||
stdio_mcp_servers(exe.as_deref(), init.mcp_stdio(), &self.paths.project_root)
|
||||
};
|
||||
let configured = self.mcp_servers_hook.read().as_ref().map(|hook| hook()).unwrap_or_default();
|
||||
servers.extend(mcp::acp_mcp_servers(&configured, provider, init, overrides));
|
||||
let off = Self::mcp_off(&configured, provider, overrides);
|
||||
@@ -516,6 +524,7 @@ impl AgentManager {
|
||||
copilot_acp_fallback: Mutex::new(HashSet::new()),
|
||||
idle_release_ms: AtomicU64::new(IDLE_RELEASE.as_millis() as u64),
|
||||
mcp_servers_hook: RwLock::new(None),
|
||||
mcp_executable_hook: RwLock::new(None),
|
||||
attention_seq: AtomicU64::new(0),
|
||||
});
|
||||
shared.paths.catalogs.subscribe(&shared);
|
||||
@@ -650,6 +659,11 @@ impl AgentManager {
|
||||
*self.shared.mcp_servers_hook.write() = Some(Arc::from(hook));
|
||||
}
|
||||
|
||||
/// Provides the executable of the running built-in MCP server for this host.
|
||||
pub fn set_mcp_executable_hook(&self, hook: Box<McpExecutableFn>) {
|
||||
*self.shared.mcp_executable_hook.write() = Some(Arc::from(hook));
|
||||
}
|
||||
|
||||
/// The slash commands to complete for `provider`, in `task`'s directory or (without one)
|
||||
/// in the project: what the agent offered last time there, unadvertised builtins,
|
||||
/// and, for Claude Code, skills and custom commands on disk it has not listed.
|
||||
@@ -3768,21 +3782,14 @@ mod tests;
|
||||
/// The name agents know the built-in server by (its tools are `mcp__OpenIDEA__…` in Claude Code).
|
||||
const BUILTIN_SERVER: &str = "OpenIDEA";
|
||||
|
||||
// The desktop advertises its relay only while the built-in server is listening.
|
||||
static MCP_EXECUTABLE: parking_lot::RwLock<Option<std::path::PathBuf>> = parking_lot::RwLock::new(None);
|
||||
|
||||
pub fn set_mcp_executable(executable: Option<std::path::PathBuf>) {
|
||||
*MCP_EXECUTABLE.write() = executable;
|
||||
}
|
||||
|
||||
fn mcp_servers_for(supported: bool, root: &std::path::Path) -> Vec<Value> {
|
||||
stdio_mcp_servers(MCP_EXECUTABLE.read().as_deref(), supported, root)
|
||||
}
|
||||
|
||||
fn stdio_mcp_servers(executable: Option<&std::path::Path>, supported: bool, root: &std::path::Path) -> Vec<Value> {
|
||||
let Some(executable) = executable.filter(|_| supported) else { return Vec::new() };
|
||||
let command = ide_mcp::stdio_command(executable, root);
|
||||
vec![json!({ "name": BUILTIN_SERVER, "command": command[0], "args": command[1..], "env": [] })]
|
||||
let env: Vec<Value> = ["OPENIDEA_SERVER_HOME", "OPENIDEA_DATA_DIR"]
|
||||
.into_iter()
|
||||
.filter_map(|name| std::env::var(name).ok().map(|value| json!({ "name": name, "value": value })))
|
||||
.collect();
|
||||
vec![json!({ "name": BUILTIN_SERVER, "command": command[0], "args": command[1..], "env": env })]
|
||||
}
|
||||
|
||||
/// What was sent of a streaming message: its first `bytes` (`utf16` UTF-16 units), which
|
||||
|
||||
@@ -53,6 +53,10 @@ pub enum IdeEvent {
|
||||
root: crate::IpcPath,
|
||||
status: DirenvStatus,
|
||||
},
|
||||
/// The project's `flutter daemon` changed state, or a device came or went.
|
||||
FlutterDevices {
|
||||
list: FlutterDeviceList,
|
||||
},
|
||||
/// Files or directories changed on disk (after debouncing, ignore rules applied).
|
||||
FsChanged {
|
||||
created: Vec<crate::IpcPath>,
|
||||
@@ -234,6 +238,46 @@ pub enum AskpassKind {
|
||||
Confirm,
|
||||
}
|
||||
|
||||
/// A target `flutter run -d <id>` can name, or an Android device adb sees but Flutter can't use yet.
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Type)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct FlutterDevice {
|
||||
pub id: String,
|
||||
pub name: String,
|
||||
/// `android`, `ios`, `macos`, `web`, …
|
||||
pub platform_type: Option<String>,
|
||||
/// `mobile`, `desktop` or `web`.
|
||||
pub category: Option<String>,
|
||||
pub emulator: bool,
|
||||
/// The adb state keeping it from being used (`unauthorized`, `offline`, …).
|
||||
pub unavailable: Option<String>,
|
||||
}
|
||||
|
||||
/// See [`FlutterDeviceList`].
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, Type)]
|
||||
#[serde(tag = "kind", rename_all = "camelCase", rename_all_fields = "camelCase")]
|
||||
pub enum FlutterDaemonStatus {
|
||||
#[default]
|
||||
Stopped,
|
||||
/// Started; the first device discovery has not finished.
|
||||
Starting,
|
||||
Ready,
|
||||
/// No `flutter` executable on the search path.
|
||||
NotInstalled,
|
||||
/// The daemon could not start or exited: what it printed last. It is not restarted on its own.
|
||||
Failed {
|
||||
message: String,
|
||||
},
|
||||
}
|
||||
|
||||
/// The project's Flutter devices, best default first.
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, Type)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct FlutterDeviceList {
|
||||
pub status: FlutterDaemonStatus,
|
||||
pub devices: Vec<FlutterDevice>,
|
||||
}
|
||||
|
||||
/// See [`IdeEvent::Direnv`].
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Type)]
|
||||
#[serde(tag = "kind", rename_all = "camelCase", rename_all_fields = "camelCase")]
|
||||
@@ -323,6 +367,9 @@ pub enum DebugEvent {
|
||||
change_type: String,
|
||||
message: String,
|
||||
},
|
||||
/// `flutter debug_adapter`'s `flutter.appStarted` event (launch and attach): the session is
|
||||
/// a Flutter app that can take a hot reload now. The plain Dart adapter never sends it.
|
||||
FlutterAppStarted,
|
||||
/// The session's capabilities changed after it started: fetch them again.
|
||||
Capabilities,
|
||||
/// The adapter's `invalidated` event: what it shows for `areas` (`all`, `stacks`, `threads`,
|
||||
|
||||
@@ -50,6 +50,8 @@ pub struct Settings {
|
||||
/// Per language server, by name (`rust-analyzer`): launch and protocol
|
||||
/// options on top of the language definition, or a server defined here.
|
||||
pub language_servers: BTreeMap<String, LanguageServerSettings>,
|
||||
/// Tools › Download sources: a mirror prefix for the release archives the installer fetches.
|
||||
pub tool_downloads: ToolDownloadSettings,
|
||||
/// Editor › Inspections profiles.
|
||||
pub inspections: InspectionSettings,
|
||||
/// Editor › Natural Languages › Spelling.
|
||||
@@ -361,6 +363,22 @@ impl Default for McpServerSettings {
|
||||
}
|
||||
}
|
||||
|
||||
/// Tools › Download sources: a mirror the installer tries when `github.com` is slow or
|
||||
/// unreachable.
|
||||
///
|
||||
/// The mirror is a proxy prefix: the official URL is appended to it unchanged
|
||||
/// (`https://mirror.example/https://github.com/o/r/releases/download/…`), which is what the
|
||||
/// public GitHub proxies speak. Only `github.com` downloads are affected — those proxies
|
||||
/// hard-code that host, so Eclipse JDT LS and Open VSX keep their official addresses. Empty by
|
||||
/// default, so every download stays on its official host until the user opts in.
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, Type)]
|
||||
#[serde(default, rename_all = "camelCase")]
|
||||
pub struct ToolDownloadSettings {
|
||||
/// Proxy prefix prepended to `https://github.com/…` archive addresses only; empty keeps
|
||||
/// every download on its official host.
|
||||
pub github_mirror: String,
|
||||
}
|
||||
|
||||
/// Absent profiles retain automatic discovery for existing projects.
|
||||
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize, Type)]
|
||||
#[serde(default, rename_all = "camelCase")]
|
||||
@@ -499,6 +517,7 @@ impl Default for Settings {
|
||||
external_tools: Vec::new(),
|
||||
tools: BTreeMap::new(),
|
||||
language_servers: BTreeMap::new(),
|
||||
tool_downloads: ToolDownloadSettings::default(),
|
||||
inspections: InspectionSettings::default(),
|
||||
spelling: SpellingSettings::default(),
|
||||
ssh: SshSettings::default(),
|
||||
@@ -1384,6 +1403,8 @@ pub struct GoSettings {
|
||||
pub goroot: String,
|
||||
/// Empty = `go env GOPATH`.
|
||||
pub gopath: String,
|
||||
/// Format with gofumpt, a stricter gofmt (gopls `gofumpt`).
|
||||
pub gofumpt: bool,
|
||||
pub build_tags: GoBuildTags,
|
||||
pub modules: GoModules,
|
||||
}
|
||||
|
||||
@@ -367,6 +367,8 @@ pub struct DebuggerSettings {
|
||||
pub skip_library_code: bool,
|
||||
/// JavaScript: debugging a dev server (`vite`, `next dev`) also opens its page in a browser session.
|
||||
pub debug_server_page: bool,
|
||||
/// Flutter: saving a Dart file of a running debug session hot reloads the app.
|
||||
pub flutter_hot_reload_on_save: bool,
|
||||
pub python: PythonDebuggerSettings,
|
||||
}
|
||||
|
||||
@@ -387,6 +389,7 @@ impl Default for DebuggerSettings {
|
||||
value_tooltip_delay: 700,
|
||||
skip_library_code: true,
|
||||
debug_server_page: false,
|
||||
flutter_hot_reload_on_save: true,
|
||||
python: PythonDebuggerSettings::default(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -280,6 +280,14 @@ pub struct SessionExceptionFilters {
|
||||
pub filters: Vec<ExceptionFilter>,
|
||||
}
|
||||
|
||||
/// What triggered a hot reload; `flutter debug_adapter` takes it as `reason`.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize, Type)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub enum HotReloadReason {
|
||||
Manual,
|
||||
Save,
|
||||
}
|
||||
|
||||
/// A live session, as listed by [`DebugManager::sessions`].
|
||||
#[derive(Clone, Debug, Serialize, Deserialize, Type)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
@@ -404,6 +412,14 @@ fn is_flutter_project(dir: &Path, program: Option<&Path>) -> bool {
|
||||
.any(|s| s.contains("sdk: flutter"))
|
||||
}
|
||||
|
||||
/// `flutter debug_adapter` passes `toolArgs` on to `flutter run` / `flutter attach`.
|
||||
fn add_flutter_device(arguments: &mut Value, device: Value) {
|
||||
let args = arguments.as_object_mut().map(|o| o.entry("toolArgs").or_insert_with(|| json!([])));
|
||||
if let Some(args) = args.and_then(Value::as_array_mut) {
|
||||
args.extend([json!("-d"), device]);
|
||||
}
|
||||
}
|
||||
|
||||
fn no_session(s: DebugSessionId) -> IdeError {
|
||||
IdeError::not_found(format!("no debug session {}", s.0))
|
||||
}
|
||||
@@ -768,6 +784,8 @@ impl DebugManager {
|
||||
// one regardless of what the working directory holds.
|
||||
let force_flutter =
|
||||
arguments.as_object_mut().and_then(|o| o.remove("__flutter")).and_then(|v| v.as_bool()).unwrap_or(false);
|
||||
// Also IDE-side: the device to run on, should this turn out to be a Flutter session.
|
||||
let flutter_device = arguments.as_object_mut().and_then(|o| o.remove("__flutterDevice"));
|
||||
let cwd = config
|
||||
.cwd
|
||||
.clone()
|
||||
@@ -812,10 +830,15 @@ impl DebugManager {
|
||||
// Flutter: a project depending on the Flutter SDK needs `flutter debug_adapter`
|
||||
// (the Dart adapter cannot start a program importing `dart:ui`, and hot reload
|
||||
// is Flutter-only). Plain Dart projects keep the configured `dart`.
|
||||
if config.adapter == "dart" && spec.command == "dart" {
|
||||
if config.adapter == "dart" {
|
||||
let program = arguments.get("program").and_then(Value::as_str).map(Path::new);
|
||||
if force_flutter || is_flutter_project(&cwd, program) {
|
||||
spec.command = "flutter".into();
|
||||
if spec.command == "dart" {
|
||||
spec.command = "flutter".into();
|
||||
}
|
||||
if let Some(device) = flutter_device {
|
||||
add_flutter_device(&mut arguments, device);
|
||||
}
|
||||
}
|
||||
}
|
||||
let id = DebugSessionId::next();
|
||||
@@ -1646,8 +1669,8 @@ impl DebugManager {
|
||||
|
||||
/// Flutter's DAP adapter: injects the changed sources into the running app.
|
||||
/// Not part of the DAP standard; adapters that do not know the request reject it.
|
||||
pub async fn hot_reload(&self, s: DebugSessionId) -> IdeResult<()> {
|
||||
self.session(s)?.conn.request_timeout("hotReload", json!({ "reason": "manual" }), REQUEST_TIMEOUT).await?;
|
||||
pub async fn hot_reload(&self, s: DebugSessionId, reason: HotReloadReason) -> IdeResult<()> {
|
||||
self.session(s)?.conn.request_timeout("hotReload", json!({ "reason": reason }), REQUEST_TIMEOUT).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
@@ -504,6 +504,9 @@ impl Session {
|
||||
|
||||
/// How long Stop waits for a launched program to exit before reporting the session ended.
|
||||
pub(crate) const DEBUGGEE_EXIT_WAIT: Duration = Duration::from_secs(10);
|
||||
/// How long a stop waits for a killed program to be gone (Windows ends processes asynchronously).
|
||||
#[cfg(windows)]
|
||||
const DEBUGGEE_KILL_WAIT: Duration = Duration::from_secs(5);
|
||||
|
||||
pub(crate) async fn remember_debuggee(s: &Arc<Session>) {
|
||||
#[cfg(windows)]
|
||||
@@ -643,14 +646,24 @@ pub(crate) async fn wait_for_debuggee(s: &Arc<Session>, terminate: bool) {
|
||||
}
|
||||
#[cfg(windows)]
|
||||
{
|
||||
use std::time::Instant;
|
||||
remember_debuggee(s).await;
|
||||
let deadline = std::time::Instant::now() + DEBUGGEE_EXIT_WAIT;
|
||||
let deadline = Instant::now() + DEBUGGEE_EXIT_WAIT;
|
||||
let mut killed: Option<Instant> = None;
|
||||
// The captured tree is the oracle when it was captured; otherwise the job (every
|
||||
// process the launch started) is, so a failed capture can no longer end the wait
|
||||
// while the program still holds its port.
|
||||
while launch_running(s) {
|
||||
if s.stop_force.swap(false, Ordering::AcqRel) || std::time::Instant::now() >= deadline {
|
||||
let now = Instant::now();
|
||||
if s.stop_force.swap(false, Ordering::AcqRel) || (killed.is_none() && now >= deadline) {
|
||||
signal_debuggee(s, true).await;
|
||||
killed.get_or_insert(now);
|
||||
}
|
||||
// Termination is asynchronous, and a process TerminateProcess can't end (stuck in
|
||||
// the kernel) would hold the stop forever: give up on it, as on Unix.
|
||||
if killed.is_some_and(|at| now.duration_since(at) >= DEBUGGEE_KILL_WAIT) {
|
||||
tracing::warn!(session = s.id.0, pid, "stop: the debugged program did not exit after it was killed");
|
||||
break;
|
||||
}
|
||||
tokio::time::sleep(Duration::from_millis(50)).await;
|
||||
}
|
||||
@@ -866,6 +879,7 @@ async fn on_event(mgr: &Arc<DebugManager>, s: &Arc<Session>, event: &str, mut bo
|
||||
change_type: crate::hot_code_replace_type(body.get("changeType")),
|
||||
message: str_("message").unwrap_or_default(),
|
||||
}),
|
||||
"flutter.appStarted" => s.emit(DebugEvent::FlutterAppStarted),
|
||||
// Sent only by adapters told `supportsInvalidatedEvent` (kotlin-lsp).
|
||||
"invalidated" => s.emit(DebugEvent::Invalidated {
|
||||
areas: body
|
||||
|
||||
@@ -559,6 +559,10 @@ async fn fake_adapter<S: AsyncRead + AsyncWrite + Unpin + Send + 'static>(stream
|
||||
}
|
||||
}
|
||||
"stepIn" | "stepOut" | "pause" => ok!(msg, Value::Null),
|
||||
"hotReload" => {
|
||||
ok!(msg, Value::Null);
|
||||
send!(json!({"type": "event", "event": "flutter.appStarted", "body": {}}));
|
||||
}
|
||||
"redefineClasses" => {
|
||||
send!(
|
||||
json!({"type": "event", "event": "hotcodereplace", "body": {"changeType": "STARTING", "message": "Start hot code replacement procedure..."}})
|
||||
@@ -1891,6 +1895,17 @@ async fn startup_sends_every_file_at_once_and_skips_files_with_nothing_enabled()
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn hot_reload_sends_its_reason_and_reports_the_flutter_app() {
|
||||
let h = harness(Script::default());
|
||||
let id = h.start(config()).await.unwrap();
|
||||
h.manager.hot_reload(id, HotReloadReason::Manual).await.unwrap();
|
||||
h.manager.hot_reload(id, HotReloadReason::Save).await.unwrap();
|
||||
let reasons: Vec<_> = requests(&h, "hotReload").iter().map(|args| args["reason"].clone()).collect();
|
||||
assert_eq!(reasons, [json!("manual"), json!("save")]);
|
||||
h.wait_event(|e| matches!(e, DebugEvent::FlutterAppStarted)).await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn hot_swap_only_for_java_debug() {
|
||||
let h = harness(Script::default());
|
||||
@@ -2280,6 +2295,16 @@ fn flutter_projects_are_found_from_the_program_or_the_working_directory() {
|
||||
std::fs::remove_dir_all(&root).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_flutter_device_follows_the_tool_arguments() {
|
||||
let mut arguments = json!({ "toolArgs": ["--flavor", "dev"] });
|
||||
add_flutter_device(&mut arguments, json!("macos"));
|
||||
assert_eq!(arguments["toolArgs"], json!(["--flavor", "dev", "-d", "macos"]));
|
||||
let mut arguments = json!({});
|
||||
add_flutter_device(&mut arguments, json!("chrome"));
|
||||
assert_eq!(arguments["toolArgs"], json!(["-d", "chrome"]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rdbg_runs_the_debuggee_after_its_options_with_the_configuration_environment() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
|
||||
@@ -7,6 +7,9 @@ license.workspace = true
|
||||
|
||||
[dependencies]
|
||||
ide-core.workspace = true
|
||||
ide-sdk.workspace = true
|
||||
tempfile.workspace = true
|
||||
zip.workspace = true
|
||||
dunce.workspace = true
|
||||
serde.workspace = true
|
||||
serde_json = { workspace = true, features = ["raw_value"] }
|
||||
@@ -38,5 +41,4 @@ open = "5"
|
||||
|
||||
[dev-dependencies]
|
||||
tokio = { workspace = true, features = ["rt-multi-thread", "macros", "net"] }
|
||||
tempfile.workspace = true
|
||||
ide-ssh = { workspace = true, features = ["testing"] }
|
||||
@@ -237,6 +237,7 @@ pub enum Pool {
|
||||
Mysql(ServerPool<MysqlManager>),
|
||||
SqlServer(Bb8Pool<MssqlConnectionManager>),
|
||||
Clickhouse(crate::clickhouse::ClickhousePool),
|
||||
Jdbc(Box<crate::jdbc::JdbcPool>),
|
||||
Oracle(crate::oracle::OraclePool),
|
||||
Snowflake(crate::snowflake::SnowflakePool),
|
||||
}
|
||||
@@ -729,6 +730,7 @@ impl Pool {
|
||||
Pool::Mysql(_) => DbDriver::Mysql,
|
||||
Pool::SqlServer(_) => DbDriver::SqlServer,
|
||||
Pool::Clickhouse(_) => DbDriver::Clickhouse,
|
||||
Pool::Jdbc(_) => DbDriver::Jdbc,
|
||||
Pool::Oracle(_) => DbDriver::Oracle,
|
||||
Pool::Snowflake(_) => DbDriver::Snowflake,
|
||||
}
|
||||
@@ -919,6 +921,7 @@ impl Pool {
|
||||
conn.query("SELECT 1", &[]).await.map_err(mssql_err)?.into_results().await.map_err(mssql_err)?;
|
||||
Ok(Pool::SqlServer(pool))
|
||||
}
|
||||
DbDriver::Jdbc => Err(IdeError::invalid("An installed JDBC driver profile is required")),
|
||||
DbDriver::Clickhouse => unreachable!(),
|
||||
DbDriver::Oracle | DbDriver::Snowflake => unreachable!(),
|
||||
}
|
||||
@@ -928,6 +931,7 @@ impl Pool {
|
||||
match self {
|
||||
Pool::SqlServer(_) | Pool::Oracle(_) => {}
|
||||
Pool::Clickhouse(p) => p.close(),
|
||||
Pool::Jdbc(p) => p.close().await,
|
||||
Pool::Snowflake(p) => p.close().await,
|
||||
Pool::Sqlite(p) => p.close().await,
|
||||
Pool::Postgres(p) => p.closed.store(true, Ordering::Relaxed),
|
||||
@@ -940,6 +944,7 @@ impl Pool {
|
||||
Pool::SqlServer(_) => false,
|
||||
Pool::Oracle(p) => p.is_closed(),
|
||||
Pool::Clickhouse(p) => p.is_closed(),
|
||||
Pool::Jdbc(p) => p.is_closed(),
|
||||
Pool::Snowflake(p) => p.is_closed(),
|
||||
Pool::Sqlite(p) => p.is_closed(),
|
||||
Pool::Postgres(p) => p.closed.load(Ordering::Relaxed),
|
||||
@@ -949,6 +954,9 @@ impl Pool {
|
||||
|
||||
/// Runs `sql` (no row limit beyond `max`), for introspection: cells are text (see [`Cells::Text`]).
|
||||
pub async fn rows(&self, sql: &str, max: usize) -> IdeResult<Vec<Vec<Option<String>>>> {
|
||||
if let Pool::Jdbc(p) = self {
|
||||
return Ok(p.query(sql, 0, max, &CancellationToken::new()).await?.rows);
|
||||
}
|
||||
if let Pool::Clickhouse(p) = self {
|
||||
return Ok(p.execute(sql, max, &CancellationToken::new()).await?.rows);
|
||||
}
|
||||
@@ -976,6 +984,9 @@ impl Pool {
|
||||
max_rows: usize,
|
||||
cancel: &CancellationToken,
|
||||
) -> IdeResult<QueryResult> {
|
||||
if let Pool::Jdbc(p) = self {
|
||||
return p.query(sql, skip, max_rows, cancel).await;
|
||||
}
|
||||
if let Pool::Clickhouse(p) = self {
|
||||
return p.execute_from(sql, skip, max_rows, cancel).await;
|
||||
}
|
||||
@@ -998,6 +1009,7 @@ impl Pool {
|
||||
/// row when flagged (table editor submits).
|
||||
pub async fn transaction(&self, statements: &[(String, bool)]) -> IdeResult<u64> {
|
||||
match self {
|
||||
Pool::Jdbc(_) => Err(IdeError::invalid("Custom JDBC table editing is unsupported")),
|
||||
Pool::Clickhouse(_) => Err(crate::clickhouse::transaction_error()),
|
||||
Pool::Snowflake(p) => p.transaction(statements).await,
|
||||
Pool::SqlServer(_) => {
|
||||
@@ -1088,6 +1100,7 @@ pub enum Held {
|
||||
impl Held {
|
||||
pub async fn acquire(pool: &Pool) -> IdeResult<Self> {
|
||||
Ok(match pool {
|
||||
Pool::Jdbc(_) => return Err(IdeError::invalid("Custom JDBC manual transactions are unsupported")),
|
||||
Pool::Sqlite(p) => Held::Sqlite(p.acquire().await.map_err(db_err)?),
|
||||
Pool::Postgres(p) => Held::Postgres(Box::new(p.get().await?)),
|
||||
Pool::Mysql(p) => Held::Mysql(Box::new(p.get().await?)),
|
||||
@@ -1377,7 +1390,7 @@ pub fn begin_statement(driver: DbDriver) -> &'static str {
|
||||
DbDriver::Mysql => "START TRANSACTION",
|
||||
DbDriver::SqlServer => "BEGIN TRAN",
|
||||
DbDriver::Oracle => "", // opens implicitly with autocommit off
|
||||
DbDriver::Sqlite | DbDriver::Postgres | DbDriver::Clickhouse | DbDriver::Snowflake => "BEGIN",
|
||||
DbDriver::Sqlite | DbDriver::Postgres | DbDriver::Clickhouse | DbDriver::Snowflake | DbDriver::Jdbc => "BEGIN",
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,678 @@
|
||||
//! A trusted local driver profile is shared by data sources. JDBC URLs and properties
|
||||
//! can run driver-specific initialization. A project source may connect only with the
|
||||
//! settings its user last accepted in the dialog; the approval lives outside the project.
|
||||
use crate::{DataSource, DbManager, QueryResult, ResultColumn};
|
||||
use ide_core::{CancellationToken, IdeError, IdeResult};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use specta::Type;
|
||||
use std::io::{Cursor, Read};
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::process::Stdio;
|
||||
use std::time::{Duration, Instant};
|
||||
use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};
|
||||
use tokio::process::{ChildStdin, ChildStdout, Command};
|
||||
use tokio::sync::Mutex;
|
||||
use tokio::time::{Instant as Deadline, timeout_at};
|
||||
|
||||
const LIMIT: usize = 16 * 1024 * 1024;
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, Type)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct DbJdbcDriver {
|
||||
pub id: String,
|
||||
pub name: String,
|
||||
pub jars: Vec<String>,
|
||||
pub class_name: String,
|
||||
pub url_template: String,
|
||||
}
|
||||
|
||||
fn persist_local(path: &Path, data: &[u8]) -> IdeResult<()> {
|
||||
let dir = path.parent().expect("registry paths have a parent");
|
||||
std::fs::create_dir_all(dir)?;
|
||||
let mut tmp = tempfile::NamedTempFile::new_in(dir)?;
|
||||
std::io::Write::write_all(&mut tmp, data)?;
|
||||
tmp.persist(path).map_err(|e| IdeError::from(e.error))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn local_jar(jar: &str) -> IdeResult<&Path> {
|
||||
if jar.contains(if cfg!(windows) { ';' } else { ':' }) {
|
||||
return Err(IdeError::invalid("A JDBC JAR path contains the system classpath separator"));
|
||||
}
|
||||
let path = Path::new(jar);
|
||||
if !path.is_absolute() || !path.is_file() || !path.extension().is_some_and(|ext| ext.eq_ignore_ascii_case("jar")) {
|
||||
return Err(IdeError::invalid("Choose an existing local .jar file for each JDBC driver file"));
|
||||
}
|
||||
Ok(path)
|
||||
}
|
||||
|
||||
/// Read the standard JDBC service descriptors without executing any driver code.
|
||||
pub fn jdbc_driver_classes(jars: &[String]) -> IdeResult<Vec<String>> {
|
||||
let mut classes = Vec::new();
|
||||
if jars.len() > 128 {
|
||||
return Err(IdeError::invalid("Choose at most 128 JDBC driver files"));
|
||||
}
|
||||
for jar in jars {
|
||||
let file = std::fs::File::open(local_jar(jar)?)?;
|
||||
let mut archive = zip::ZipArchive::new(file).map_err(|_| IdeError::invalid("Invalid JDBC JAR archive"))?;
|
||||
let descriptor = match archive.by_name("META-INF/services/java.sql.Driver") {
|
||||
Ok(entry) => entry,
|
||||
Err(zip::result::ZipError::FileNotFound) => continue,
|
||||
Err(_) => return Err(IdeError::invalid("Cannot read the JDBC driver descriptor")),
|
||||
};
|
||||
let mut text = String::new();
|
||||
descriptor.take(65537).read_to_string(&mut text)?;
|
||||
if text.len() > 65536 {
|
||||
return Err(IdeError::invalid("JDBC driver descriptor is too large"));
|
||||
}
|
||||
for line in text.lines() {
|
||||
let name = line.split('#').next().unwrap_or_default().trim();
|
||||
if !name.is_empty()
|
||||
&& name.split('.').all(|part| {
|
||||
let mut chars = part.chars();
|
||||
chars.next().is_some_and(|c| c.is_alphabetic() || matches!(c, '_' | '$'))
|
||||
&& chars.all(|c| c.is_alphanumeric() || matches!(c, '_' | '$'))
|
||||
})
|
||||
&& !classes.iter().any(|s| s == name)
|
||||
{
|
||||
classes.push(name.to_owned());
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(classes)
|
||||
}
|
||||
|
||||
impl DbJdbcDriver {
|
||||
fn validate(&self) -> IdeResult<()> {
|
||||
if self.id.is_empty()
|
||||
|| self.name.trim().is_empty()
|
||||
|| self.jars.is_empty()
|
||||
|| self.class_name.trim().is_empty()
|
||||
{
|
||||
return Err(IdeError::invalid("A JDBC driver needs a name, a driver class and at least one JAR"));
|
||||
}
|
||||
if !self.url_template.is_empty() && !self.url_template.starts_with("jdbc:") {
|
||||
return Err(IdeError::invalid("The JDBC URL template must start with jdbc:"));
|
||||
}
|
||||
for jar in &self.jars {
|
||||
local_jar(jar)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl DbManager {
|
||||
fn jdbc_registry(&self) -> IdeResult<PathBuf> {
|
||||
self.global
|
||||
.as_ref()
|
||||
.map(|(dir, _)| dir.join("jdbc-drivers.json"))
|
||||
.ok_or_else(|| IdeError::invalid("Global JDBC driver profiles are unavailable here"))
|
||||
}
|
||||
|
||||
pub fn jdbc_drivers(&self) -> IdeResult<Vec<DbJdbcDriver>> {
|
||||
let path = self.jdbc_registry()?;
|
||||
match std::fs::File::open(path) {
|
||||
Ok(file) => {
|
||||
serde_json::from_reader(file).map_err(|e| IdeError::invalid(format!("JDBC driver profiles: {e}")))
|
||||
}
|
||||
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(vec![]),
|
||||
Err(e) => Err(e.into()),
|
||||
}
|
||||
}
|
||||
|
||||
fn update_jdbc_driver(&self, id: &str, driver: Option<DbJdbcDriver>) -> IdeResult<()> {
|
||||
let _guard = crate::plugin::EDIT.lock();
|
||||
let path = self.jdbc_registry()?;
|
||||
let mut list = self.jdbc_drivers()?;
|
||||
list.retain(|d| d.id != id);
|
||||
list.extend(driver);
|
||||
let data = serde_json::to_vec_pretty(&list).map_err(|e| IdeError::internal(e.to_string()))?;
|
||||
persist_local(&path, &data)
|
||||
}
|
||||
|
||||
pub fn save_jdbc_driver(&self, driver: DbJdbcDriver) -> IdeResult<()> {
|
||||
driver.validate()?;
|
||||
self.update_jdbc_driver(&driver.id.clone(), Some(driver))
|
||||
}
|
||||
|
||||
pub fn remove_jdbc_driver(&self, id: &str) -> IdeResult<()> {
|
||||
if self.global_sources().iter().any(|s| s.driver == crate::DbDriver::Jdbc && s.jdbc_driver == id) {
|
||||
return Err(IdeError::invalid("This JDBC driver is in use by a data source"));
|
||||
}
|
||||
self.update_jdbc_driver(id, None)
|
||||
}
|
||||
|
||||
fn jdbc_approval(&self, source: &DataSource) -> IdeResult<(PathBuf, String)> {
|
||||
let registry = self.jdbc_registry()?;
|
||||
let key = serde_json::to_vec(&(&self.root, &source.id)).map_err(|e| IdeError::internal(e.to_string()))?;
|
||||
let settings = serde_json::to_vec(&(
|
||||
&source.jdbc_driver,
|
||||
&source.url,
|
||||
&source.user,
|
||||
source.read_only,
|
||||
&source.properties,
|
||||
&source.options,
|
||||
))
|
||||
.map_err(|e| IdeError::internal(e.to_string()))?;
|
||||
Ok((
|
||||
registry.with_file_name("jdbc-approvals").join(blake3::hash(&key).to_hex().as_str()),
|
||||
blake3::hash(&settings).to_hex().to_string(),
|
||||
))
|
||||
}
|
||||
|
||||
/// Called only for the source the user explicitly saved in the connection dialog.
|
||||
pub fn approve_jdbc_source(&self, source: &DataSource) -> IdeResult<()> {
|
||||
if source.driver != crate::DbDriver::Jdbc || source.plugin_owner.is_some() {
|
||||
return Err(IdeError::invalid("Expected a user-managed JDBC data source"));
|
||||
}
|
||||
let (path, fingerprint) = self.jdbc_approval(source)?;
|
||||
persist_local(&path, fingerprint.as_bytes())
|
||||
}
|
||||
|
||||
pub(crate) async fn open_pool(
|
||||
&self,
|
||||
source: &DataSource,
|
||||
password: Option<&str>,
|
||||
testing: bool,
|
||||
) -> IdeResult<crate::Pool> {
|
||||
if source.driver != crate::DbDriver::Jdbc {
|
||||
return crate::Pool::connect(source, password, Some(&self.root)).await;
|
||||
}
|
||||
if source.plugin_owner.is_some() {
|
||||
return Err(IdeError::invalid("Plugins cannot start custom JDBC drivers"));
|
||||
}
|
||||
if !testing && !source.global {
|
||||
let (path, expected) = self.jdbc_approval(source)?;
|
||||
if std::fs::read_to_string(path).ok().as_deref() != Some(&expected) {
|
||||
return Err(IdeError::invalid(
|
||||
"Review this project's JDBC URL and properties, then save the data source before connecting",
|
||||
));
|
||||
}
|
||||
}
|
||||
let driver = self
|
||||
.jdbc_drivers()?
|
||||
.into_iter()
|
||||
.find(|d| d.id == source.jdbc_driver)
|
||||
.ok_or_else(|| IdeError::invalid("Select an installed JDBC driver profile"))?;
|
||||
JdbcPool::connect(source, password, driver, self.jdbc_registry()?.with_file_name("jdbc"))
|
||||
.await
|
||||
.map(Box::new)
|
||||
.map(crate::Pool::Jdbc)
|
||||
}
|
||||
}
|
||||
|
||||
struct Bridge {
|
||||
stop: tokio::sync::oneshot::Sender<()>,
|
||||
exited: tokio::task::JoinHandle<()>,
|
||||
sequence: u64,
|
||||
input: ChildStdin,
|
||||
output: ChildStdout,
|
||||
}
|
||||
|
||||
pub struct JdbcPool {
|
||||
bridge: Mutex<Option<Bridge>>,
|
||||
closed: CancellationToken,
|
||||
pub(crate) version: String,
|
||||
pub(crate) identifier_quote: String,
|
||||
}
|
||||
|
||||
impl Bridge {
|
||||
fn start(cache: &Path, jars: &[String]) -> IdeResult<Self> {
|
||||
let jdks = ide_sdk::java::installed_jdks();
|
||||
let java = ide_sdk::java::java_runtime(&jdks, 17)
|
||||
.ok_or_else(|| IdeError::invalid("Install a Java 17 or newer runtime to use custom JDBC drivers"))?;
|
||||
let jar = ide_sdk::java::jdbc_bridge_jar(cache)
|
||||
.ok_or_else(|| IdeError::invalid("Cannot install the JDBC bridge in the application data directory"))?;
|
||||
let classpath = std::env::join_paths(std::iter::once(jar.as_path()).chain(jars.iter().map(Path::new)))
|
||||
.map_err(|_| IdeError::invalid("A JDBC JAR path contains the system classpath separator"))?;
|
||||
// Embedded databases can resolve file URLs against the JVM's working directory.
|
||||
let files = cache.join("files");
|
||||
std::fs::create_dir_all(&files)?;
|
||||
let mut command = Command::new(ide_sdk::java::java_executable(&java.home));
|
||||
command.current_dir(files);
|
||||
ide_core::process::prepare(command.as_std_mut());
|
||||
let mut child = command
|
||||
.args(["-Xms8m", "-Xmx192m", "--enable-native-access=ALL-UNNAMED", "-cp"])
|
||||
.arg(classpath)
|
||||
.arg("ai.openidea.jdbc.OpenIdeaJdbc")
|
||||
.env_remove("CLASSPATH")
|
||||
.env_remove("JDK_JAVA_OPTIONS")
|
||||
.env_remove("JAVA_TOOL_OPTIONS")
|
||||
.env_remove("_JAVA_OPTIONS")
|
||||
.kill_on_drop(true)
|
||||
.stdin(Stdio::piped())
|
||||
.stdout(Stdio::piped())
|
||||
.stderr(Stdio::null())
|
||||
.spawn()?;
|
||||
let input = child.stdin.take().expect("piped");
|
||||
let output = child.stdout.take().expect("piped");
|
||||
let (stop, stopped) = tokio::sync::oneshot::channel();
|
||||
let exited = tokio::spawn(async move {
|
||||
tokio::select! {
|
||||
stop = stopped => {
|
||||
// Disconnect closes stdin so a file-backed driver can flush. A dropped
|
||||
// request or an unresponsive driver must be reaped without waiting.
|
||||
if stop.is_err() || tokio::time::timeout(Duration::from_secs(2), child.wait()).await.is_err() {
|
||||
let _ = child.kill().await;
|
||||
}
|
||||
}
|
||||
_ = child.wait() => {}
|
||||
}
|
||||
});
|
||||
Ok(Self { input, output, stop, exited, sequence: 0 })
|
||||
}
|
||||
|
||||
async fn close(self) {
|
||||
drop(self.input);
|
||||
let _ = self.stop.send(());
|
||||
let _ = self.exited.await;
|
||||
}
|
||||
}
|
||||
|
||||
// Request: u64 request id, u32 field count, then length-prefixed UTF-8 fields.
|
||||
// Zero fields cancels that id. Replies have a u32 length; zero acknowledges Cancel.
|
||||
fn encode(id: u64, fields: &[&str]) -> IdeResult<Vec<u8>> {
|
||||
if fields.is_empty() || fields.len() > 8192 {
|
||||
return Err(IdeError::invalid("Invalid JDBC field count"));
|
||||
}
|
||||
let size = fields
|
||||
.iter()
|
||||
.try_fold(12usize, |n, s| n.checked_add(4)?.checked_add(s.len()).filter(|n| *n <= LIMIT))
|
||||
.ok_or_else(|| IdeError::invalid("JDBC request exceeds 16 MiB"))?;
|
||||
let mut data = Vec::with_capacity(size);
|
||||
data.extend_from_slice(&id.to_be_bytes());
|
||||
data.extend_from_slice(&(fields.len() as u32).to_be_bytes());
|
||||
for field in fields {
|
||||
data.extend_from_slice(&(field.len() as u32).to_be_bytes());
|
||||
data.extend_from_slice(field.as_bytes());
|
||||
}
|
||||
Ok(data)
|
||||
}
|
||||
|
||||
async fn read_frame(output: &mut (impl AsyncRead + Unpin)) -> IdeResult<Vec<u8>> {
|
||||
let n = output.read_u32().await? as usize;
|
||||
if n > LIMIT {
|
||||
return Err(IdeError::invalid("JDBC response exceeds 16 MiB"));
|
||||
}
|
||||
let mut data = vec![0; n];
|
||||
output.read_exact(&mut data).await?;
|
||||
Ok(data)
|
||||
}
|
||||
|
||||
const CANCEL_GRACE: Duration = Duration::from_secs(3);
|
||||
|
||||
fn cancel_timeout() -> IdeError {
|
||||
IdeError::invalid("The JDBC driver did not stop in time. The session was closed. Reconnect to continue.")
|
||||
}
|
||||
|
||||
async fn exchange(
|
||||
input: &mut (impl AsyncWrite + Unpin),
|
||||
output: &mut (impl AsyncRead + Unpin),
|
||||
frame: Vec<u8>,
|
||||
cancel: &CancellationToken,
|
||||
) -> IdeResult<Option<Vec<u8>>> {
|
||||
let mut stop = [0; 12];
|
||||
stop[..8].copy_from_slice(&frame[..8]);
|
||||
let early_deadline = {
|
||||
let send = input.write_all(&frame);
|
||||
tokio::pin!(send);
|
||||
tokio::select! {
|
||||
result = &mut send => { result?; None },
|
||||
() = cancel.cancelled() => {
|
||||
let end = Deadline::now() + CANCEL_GRACE;
|
||||
timeout_at(end, send).await.map_err(|_| cancel_timeout())??;
|
||||
Some(end)
|
||||
}
|
||||
}
|
||||
};
|
||||
drop(frame);
|
||||
let (first, deadline) = {
|
||||
let reply = read_frame(output);
|
||||
tokio::pin!(reply);
|
||||
let end = tokio::select! {
|
||||
biased;
|
||||
() = cancel.cancelled() => early_deadline.unwrap_or_else(|| Deadline::now() + CANCEL_GRACE),
|
||||
data = &mut reply => {
|
||||
let data = data?;
|
||||
if data.is_empty() { return Err(IdeError::invalid("Unexpected JDBC cancellation acknowledgement")); }
|
||||
return Ok(Some(data));
|
||||
}
|
||||
};
|
||||
let first = timeout_at(end, async {
|
||||
input.write_all(&stop).await?;
|
||||
reply.await
|
||||
})
|
||||
.await
|
||||
.map_err(|_| cancel_timeout())??;
|
||||
(first, end)
|
||||
};
|
||||
// Drain both frames before the next statement can use this session. The driver
|
||||
// may complete the statement before or after the control thread acknowledges it.
|
||||
let data = if first.is_empty() {
|
||||
timeout_at(deadline, read_frame(output)).await.map_err(|_| cancel_timeout())??
|
||||
} else {
|
||||
if timeout_at(deadline, output.read_u32()).await.map_err(|_| cancel_timeout())?? != 0 {
|
||||
return Err(IdeError::invalid("Invalid JDBC cancellation acknowledgement"));
|
||||
}
|
||||
first
|
||||
};
|
||||
match data.first() {
|
||||
// A committed statement may have finished before the control thread received Cancel.
|
||||
Some(1) => Ok(Some(data)),
|
||||
Some(0) => Ok(None),
|
||||
_ => Err(IdeError::invalid("Invalid JDBC cancellation response")),
|
||||
}
|
||||
}
|
||||
|
||||
impl JdbcPool {
|
||||
async fn connect(
|
||||
source: &DataSource,
|
||||
password: Option<&str>,
|
||||
driver: DbJdbcDriver,
|
||||
cache: PathBuf,
|
||||
) -> IdeResult<Self> {
|
||||
driver.validate()?;
|
||||
let mut fields = vec![
|
||||
"open".to_string(),
|
||||
driver.class_name,
|
||||
source.url.clone(),
|
||||
source.user.clone(),
|
||||
password.unwrap_or_default().to_owned(),
|
||||
source.read_only.to_string(),
|
||||
];
|
||||
for p in &source.properties {
|
||||
if p.name.eq_ignore_ascii_case("password") {
|
||||
return Err(IdeError::invalid("Enter the JDBC password in the password field"));
|
||||
}
|
||||
fields.extend([p.name.clone(), p.value.clone()]);
|
||||
}
|
||||
let bridge = tokio::task::spawn_blocking(move || Bridge::start(&cache, &driver.jars))
|
||||
.await
|
||||
.map_err(|e| IdeError::internal(e.to_string()))??;
|
||||
let mut pool = Self {
|
||||
bridge: Mutex::new(Some(bridge)),
|
||||
closed: CancellationToken::new(),
|
||||
version: String::new(),
|
||||
identifier_quote: String::new(),
|
||||
};
|
||||
let args: Vec<_> = fields.iter().map(String::as_str).collect();
|
||||
let cancel = CancellationToken::new();
|
||||
let result = tokio::time::timeout(Duration::from_secs(30), pool.request(&args, "", &cancel))
|
||||
.await
|
||||
.map_err(|_| IdeError::new(ide_core::error::ErrorKind::TimedOut, "JDBC connection timed out"))??;
|
||||
let mut metadata = result.rows.into_iter().next().unwrap_or_default().into_iter();
|
||||
pool.version = metadata.next().flatten().unwrap_or_else(|| "Connected".into());
|
||||
pool.identifier_quote = metadata.next().flatten().unwrap_or_default().trim().into();
|
||||
Ok(pool)
|
||||
}
|
||||
|
||||
pub(crate) async fn close(&self) {
|
||||
self.closed.cancel();
|
||||
if let Some(bridge) = self.bridge.lock().await.take() {
|
||||
bridge.close().await;
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn is_closed(&self) -> bool {
|
||||
self.closed.is_cancelled()
|
||||
}
|
||||
|
||||
pub(crate) async fn query(
|
||||
&self,
|
||||
sql: &str,
|
||||
skip: usize,
|
||||
max: usize,
|
||||
cancel: &CancellationToken,
|
||||
) -> IdeResult<QueryResult> {
|
||||
self.query_in_schema(sql, skip, max, cancel, None).await
|
||||
}
|
||||
|
||||
pub(crate) async fn query_in_schema(
|
||||
&self,
|
||||
sql: &str,
|
||||
skip: usize,
|
||||
max: usize,
|
||||
cancel: &CancellationToken,
|
||||
schema: Option<&str>,
|
||||
) -> IdeResult<QueryResult> {
|
||||
self.request(
|
||||
&["query", sql, &skip.to_string(), &max.min(i32::MAX as usize).to_string(), schema.unwrap_or_default()],
|
||||
sql,
|
||||
cancel,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
pub(crate) async fn page(
|
||||
&self,
|
||||
sql: &str,
|
||||
offset: u64,
|
||||
limit: u64,
|
||||
cancel: &CancellationToken,
|
||||
) -> IdeResult<QueryResult> {
|
||||
self.request(&["page", sql, &offset.to_string(), &limit.min(i32::MAX as u64).to_string()], sql, cancel).await
|
||||
}
|
||||
|
||||
pub(crate) async fn metadata(&self, fields: &[&str]) -> IdeResult<Vec<Vec<Option<String>>>> {
|
||||
Ok(self.request(fields, "", &CancellationToken::new()).await?.rows)
|
||||
}
|
||||
|
||||
async fn request(&self, fields: &[&str], sql: &str, cancel: &CancellationToken) -> IdeResult<QueryResult> {
|
||||
let mut slot = tokio::select! {
|
||||
biased;
|
||||
() = cancel.cancelled() => return Err(IdeError::cancelled()),
|
||||
() = self.closed.cancelled() => return Err(IdeError::invalid("JDBC connection is closed")),
|
||||
slot = self.bridge.lock() => slot,
|
||||
};
|
||||
let bridge = slot
|
||||
.as_mut()
|
||||
.ok_or_else(|| IdeError::invalid("JDBC connection is closed; reconnect to start a new session"))?;
|
||||
bridge.sequence = bridge.sequence.wrapping_add(1);
|
||||
let frame = encode(bridge.sequence, fields)?;
|
||||
let mut bridge = slot.take().expect("checked above");
|
||||
// Dropping an in-flight request must discard its process and partially consumed frame.
|
||||
let interrupted = self.closed.clone().drop_guard();
|
||||
let start = Instant::now();
|
||||
let response = tokio::select! {
|
||||
biased;
|
||||
() = self.closed.cancelled() => Err(IdeError::cancelled()),
|
||||
response = exchange(&mut bridge.input, &mut bridge.output, frame, cancel) => response,
|
||||
};
|
||||
let data = match response {
|
||||
Ok(data) => {
|
||||
*slot = Some(bridge);
|
||||
interrupted.disarm();
|
||||
data
|
||||
}
|
||||
Err(e) => {
|
||||
drop(bridge.stop);
|
||||
let _ = bridge.exited.await;
|
||||
return Err(e);
|
||||
}
|
||||
};
|
||||
decode(&data.ok_or_else(IdeError::cancelled)?, sql, start.elapsed().as_secs_f64() * 1000.0)
|
||||
}
|
||||
}
|
||||
|
||||
fn decode(data: &[u8], sql: &str, elapsed_ms: f64) -> IdeResult<QueryResult> {
|
||||
let mut input = Cursor::new(data);
|
||||
fn bytes<const N: usize>(input: &mut Cursor<&[u8]>) -> IdeResult<[u8; N]> {
|
||||
let mut buf = [0; N];
|
||||
Read::read_exact(input, &mut buf)?;
|
||||
Ok(buf)
|
||||
}
|
||||
fn flag(input: &mut Cursor<&[u8]>) -> IdeResult<bool> {
|
||||
match bytes::<1>(input)? {
|
||||
[0] => Ok(false),
|
||||
[1] => Ok(true),
|
||||
_ => Err(IdeError::invalid("Invalid JDBC flag")),
|
||||
}
|
||||
}
|
||||
fn field(input: &mut Cursor<&[u8]>) -> IdeResult<Option<String>> {
|
||||
let n = i32::from_be_bytes(bytes(input)?);
|
||||
if n == -1 {
|
||||
return Ok(None);
|
||||
}
|
||||
let start = input.position() as usize;
|
||||
let end = start
|
||||
.checked_add(n as usize)
|
||||
.filter(|end| *end <= input.get_ref().len())
|
||||
.ok_or_else(|| IdeError::invalid("Invalid JDBC field length"))?;
|
||||
let value = String::from_utf8_lossy(&input.get_ref()[start..end]).into_owned();
|
||||
input.set_position(end as u64);
|
||||
Ok(Some(value))
|
||||
}
|
||||
if !flag(&mut input)? {
|
||||
return Err(IdeError::invalid(field(&mut input)?.unwrap_or_else(|| "JDBC request failed".into())));
|
||||
}
|
||||
let count = u32::from_be_bytes(bytes(&mut input)?) as usize;
|
||||
if count > data.len() / 8 {
|
||||
return Err(IdeError::invalid("Invalid JDBC column count"));
|
||||
}
|
||||
let mut columns = Vec::new();
|
||||
for _ in 0..count {
|
||||
columns.push(ResultColumn {
|
||||
name: field(&mut input)?.unwrap_or_default(),
|
||||
type_name: field(&mut input)?.unwrap_or_default(),
|
||||
});
|
||||
}
|
||||
let mut rows = vec![];
|
||||
while flag(&mut input)? {
|
||||
if count == 0 {
|
||||
return Err(IdeError::invalid("JDBC returned a row with no columns"));
|
||||
}
|
||||
rows.push((0..count).map(|_| field(&mut input)).collect::<IdeResult<_>>()?);
|
||||
}
|
||||
let has_more = flag(&mut input)?;
|
||||
let affected = i64::from_be_bytes(bytes(&mut input)?);
|
||||
if input.position() != data.len() as u64 {
|
||||
return Err(IdeError::invalid("Invalid trailing JDBC response data"));
|
||||
}
|
||||
Ok(QueryResult {
|
||||
statement: sql.into(),
|
||||
columns,
|
||||
rows,
|
||||
has_more,
|
||||
affected: (affected >= 0).then_some(affected as f64),
|
||||
elapsed_ms,
|
||||
execution_ms: elapsed_ms,
|
||||
})
|
||||
}
|
||||
|
||||
impl DbManager {
|
||||
pub(crate) async fn jdbc_completion(&self, id: &str, pool: &JdbcPool) -> IdeResult<crate::CompletionModel> {
|
||||
let mut tables = vec![];
|
||||
let schemas = self.schemas(id).await?;
|
||||
for schema in &schemas {
|
||||
for table in self.tables(id, schema).await? {
|
||||
tables.push(crate::CompletionTable {
|
||||
schema: schema.clone(),
|
||||
name: table.name,
|
||||
kind: table.kind,
|
||||
columns: vec![],
|
||||
});
|
||||
if tables.len() >= crate::MAX_META_ROWS {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if tables.len() >= crate::MAX_META_ROWS {
|
||||
break;
|
||||
}
|
||||
}
|
||||
Ok(crate::CompletionModel {
|
||||
default_schema: pool
|
||||
.metadata(&["namespace"])
|
||||
.await?
|
||||
.into_iter()
|
||||
.flatten()
|
||||
.flatten()
|
||||
.next()
|
||||
.unwrap_or_default(),
|
||||
tables,
|
||||
foreign_keys: vec![],
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[tokio::test]
|
||||
async fn frame_codec_checks_both_16_mib_boundaries() {
|
||||
assert_eq!(encode(7, &["a"]).unwrap(), [0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 1, 0, 0, 0, 1, b'a']);
|
||||
let mut field = "x".repeat(LIMIT - 16);
|
||||
assert_eq!(encode(1, &[&field]).unwrap().len(), LIMIT);
|
||||
field.push('x');
|
||||
assert!(encode(1, &[&field]).is_err());
|
||||
let mut framed = vec![0u8; LIMIT + 4];
|
||||
framed[..4].copy_from_slice(&(LIMIT as u32).to_be_bytes());
|
||||
assert_eq!(read_frame(&mut framed.as_slice()).await.unwrap().len(), LIMIT);
|
||||
assert!(read_frame(&mut &((LIMIT + 1) as u32).to_be_bytes()[..]).await.is_err());
|
||||
assert!(read_frame(&mut &[0, 0, 0, 2, 1u8][..]).await.is_err());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn cancel_drains_partial_replies_in_either_order() {
|
||||
for ack_first in [true, false] {
|
||||
let (mut input, mut peer_input) = tokio::io::duplex(1);
|
||||
let (mut output, mut peer_output) = tokio::io::duplex(1);
|
||||
let cancel = CancellationToken::new();
|
||||
let frame = encode(42, &["query"]).unwrap();
|
||||
let peer = async {
|
||||
let mut request = vec![0; frame.len()];
|
||||
peer_input.read_exact(&mut request).await.unwrap();
|
||||
assert_eq!(request, frame);
|
||||
// The one-byte pipe forces the reader to consume part of the header before Cancel.
|
||||
peer_output.write_all(&[0, 0]).await.unwrap();
|
||||
cancel.cancel();
|
||||
let mut stop = [0; 12];
|
||||
peer_input.read_exact(&mut stop).await.unwrap();
|
||||
assert_eq!(&stop[..8], &42u64.to_be_bytes());
|
||||
assert_eq!(&stop[8..], &[0; 4]);
|
||||
let frames = if ack_first { [0, 0, 0, 0, 0, 1, 0] } else { [0, 1, 1, 0, 0, 0, 0] };
|
||||
peer_output.write_all(&frames).await.unwrap();
|
||||
};
|
||||
let (result, ()) = tokio::join!(exchange(&mut input, &mut output, frame.clone(), &cancel), peer);
|
||||
assert_eq!(result.unwrap(), (!ack_first).then(|| vec![1]));
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn exchange_rejects_unexpected_acknowledgements() {
|
||||
let cancel = CancellationToken::new();
|
||||
let mut sink = tokio::io::sink();
|
||||
let mut reply = &[0, 0, 0, 1, 1u8][..];
|
||||
assert_eq!(
|
||||
exchange(&mut sink, &mut reply, encode(1, &["query"]).unwrap(), &cancel).await.unwrap(),
|
||||
Some(vec![1])
|
||||
);
|
||||
for cancelled in [false, true] {
|
||||
if cancelled {
|
||||
cancel.cancel();
|
||||
}
|
||||
let mut duplicate_ack = &[0u8; 8][..];
|
||||
assert!(exchange(&mut sink, &mut duplicate_ack, encode(1, &["query"]).unwrap(), &cancel).await.is_err());
|
||||
}
|
||||
let (mut peer, mut output) = tokio::io::duplex(4);
|
||||
peer.write_u32(0).await.unwrap();
|
||||
let error = exchange(&mut sink, &mut output, encode(1, &["query"]).unwrap(), &cancel).await.unwrap_err();
|
||||
assert_eq!(error.message, cancel_timeout().message);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn result_codec_preserves_null_and_rejects_incomplete_frames() {
|
||||
let mut frame =
|
||||
vec![1, 0, 0, 0, 1, 0, 0, 0, 1, b'n', 0, 0, 0, 3, b'i', b'n', b't', 1, 255, 255, 255, 255, 0, 0];
|
||||
frame.extend_from_slice(&(-1i64).to_be_bytes());
|
||||
let result = decode(&frame, "select", 0.).unwrap();
|
||||
assert_eq!(result.columns[0].name, "n");
|
||||
assert_eq!(result.rows, [vec![None]]);
|
||||
for end in 0..frame.len() {
|
||||
assert!(decode(&frame[..end], "", 0.).is_err());
|
||||
}
|
||||
frame.push(0);
|
||||
assert!(decode(&frame, "", 0.).is_err());
|
||||
assert!(decode(&[2], "", 0.).is_err());
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,8 @@
|
||||
|
||||
pub mod catalog;
|
||||
mod clickhouse;
|
||||
mod jdbc;
|
||||
pub use jdbc::{DbJdbcDriver, jdbc_driver_classes};
|
||||
pub mod conn;
|
||||
pub mod history;
|
||||
mod oracle;
|
||||
@@ -339,7 +341,7 @@ fn group_foreign_keys(rows: Vec<Vec<Option<String>>>, tables: &[CompletionTable]
|
||||
/// never contain `:`.
|
||||
/// Snowflake is HTTPS to a named host: a tunnel's local end cannot pass its certificate check.
|
||||
fn tunneled(driver: DbDriver) -> bool {
|
||||
!matches!(driver, DbDriver::Sqlite | DbDriver::Snowflake)
|
||||
!matches!(driver, DbDriver::Sqlite | DbDriver::Snowflake | DbDriver::Jdbc)
|
||||
}
|
||||
|
||||
/// The database host and port the SSH host forwards to.
|
||||
@@ -759,7 +761,7 @@ impl DbManager {
|
||||
};
|
||||
let (target, tunnel) = self.endpoint(source, ssh_secret, false).await?;
|
||||
self.restore_sso(source).await;
|
||||
let connected = Pool::connect(&target, password.as_deref(), Some(&self.root)).await;
|
||||
let connected = self.open_pool(&target, password.as_deref(), true).await;
|
||||
self.keep_sso(source).await;
|
||||
let pool = connected.map_err(|e| {
|
||||
through_tunnel(
|
||||
@@ -768,7 +770,13 @@ impl DbManager {
|
||||
tunnel.is_some().then(|| self.ssh_label(source.ssh.as_ref().expect("tunneled"))).as_deref(),
|
||||
)
|
||||
})?;
|
||||
if let Pool::Jdbc(p) = &pool {
|
||||
let version = p.version.clone();
|
||||
pool.close().await;
|
||||
return Ok(version);
|
||||
}
|
||||
let version = match source.driver {
|
||||
DbDriver::Jdbc => unreachable!("JDBC uses driver metadata"),
|
||||
DbDriver::Sqlite => "SELECT 'SQLite ' || sqlite_version()",
|
||||
DbDriver::Postgres | DbDriver::Clickhouse => "SELECT version()",
|
||||
DbDriver::Mysql => "SELECT CONCAT(@@version_comment, ' ', VERSION())",
|
||||
@@ -861,7 +869,7 @@ impl DbManager {
|
||||
let password = self.password(&source).await;
|
||||
let (target, tunnel) = self.endpoint(&source, None, true).await?;
|
||||
self.restore_sso(&source).await;
|
||||
let connected = Pool::connect(&target, password.as_deref(), Some(&self.root)).await;
|
||||
let connected = self.open_pool(&target, password.as_deref(), false).await;
|
||||
self.keep_sso(&source).await;
|
||||
let pool = match connected {
|
||||
Ok(p) => Arc::new(p),
|
||||
@@ -1071,7 +1079,10 @@ impl DbManager {
|
||||
if let Pool::Snowflake(p) = &*pool {
|
||||
return snowflake::meta::schemas(p, system).await;
|
||||
}
|
||||
let rows = pool.rows(sql::schemas(pool.driver(), system), MAX_META_ROWS).await?;
|
||||
let rows = match &*pool {
|
||||
Pool::Jdbc(p) => p.metadata(&["schemas"]).await?,
|
||||
_ => pool.rows(sql::schemas(pool.driver(), system), MAX_META_ROWS).await?,
|
||||
};
|
||||
Ok(rows.into_iter().filter_map(|r| r.into_iter().next().flatten()).collect())
|
||||
}
|
||||
|
||||
@@ -1080,7 +1091,10 @@ impl DbManager {
|
||||
if let Pool::Snowflake(p) = &*pool {
|
||||
return snowflake::meta::tables(p, schema).await;
|
||||
}
|
||||
let rows = pool.rows(&sql::tables(pool.driver(), schema), MAX_META_ROWS).await?;
|
||||
let rows = match &*pool {
|
||||
Pool::Jdbc(p) => p.metadata(&["tables", schema]).await?,
|
||||
_ => pool.rows(&sql::tables(pool.driver(), schema), MAX_META_ROWS).await?,
|
||||
};
|
||||
Ok(rows
|
||||
.into_iter()
|
||||
.filter_map(|r| {
|
||||
@@ -1097,9 +1111,11 @@ impl DbManager {
|
||||
return snowflake::meta::table_details(p, schema, table).await;
|
||||
}
|
||||
let driver = pool.driver();
|
||||
let columns = pool
|
||||
.rows(&sql::columns(driver, schema, table), MAX_META_ROWS)
|
||||
.await?
|
||||
let rows = match &*pool {
|
||||
Pool::Jdbc(p) => p.metadata(&["columns", schema, table]).await?,
|
||||
_ => pool.rows(&sql::columns(driver, schema, table), MAX_META_ROWS).await?,
|
||||
};
|
||||
let columns = rows
|
||||
.into_iter()
|
||||
.filter_map(|r| {
|
||||
let mut it = r.into_iter();
|
||||
@@ -1111,6 +1127,9 @@ impl DbManager {
|
||||
Some(ColumnInfo { name, data_type, nullable, default, primary_key })
|
||||
})
|
||||
.collect();
|
||||
if driver == DbDriver::Jdbc {
|
||||
return Ok(TableDetails { columns, indexes: vec![], foreign_keys: vec![], triggers: vec![] });
|
||||
}
|
||||
let mut indexes: Vec<IndexInfo> = Vec::new();
|
||||
for r in pool.rows(&sql::indexes(driver, schema, table), MAX_META_ROWS).await? {
|
||||
let mut it = r.into_iter();
|
||||
@@ -1271,6 +1290,7 @@ impl DbManager {
|
||||
rows.into_iter().filter_map(|r| r.into_iter().nth(i).flatten()).collect()
|
||||
};
|
||||
match driver {
|
||||
DbDriver::Jdbc => Err(IdeError::invalid("Custom JDBC sources do not provide vendor-specific DDL")),
|
||||
DbDriver::Sqlite => {
|
||||
let stmts = cells(pool.rows(&sql::sqlite_ddl(schema, name), MAX_META_ROWS).await?, 0);
|
||||
if stmts.is_empty() {
|
||||
@@ -1395,6 +1415,7 @@ impl DbManager {
|
||||
let driver = pool.driver();
|
||||
let mut tables: Vec<CompletionTable> = Vec::new();
|
||||
let rows = match &*pool {
|
||||
Pool::Jdbc(p) => return self.jdbc_completion(id, p).await,
|
||||
Pool::Snowflake(p) => {
|
||||
tables = snowflake::meta::completion(p).await?;
|
||||
Vec::new()
|
||||
@@ -1515,6 +1536,9 @@ impl DbManager {
|
||||
schema: Option<&str>,
|
||||
) -> IdeResult<Vec<PlanNode>> {
|
||||
let driver = self.pool(id).await?.driver();
|
||||
if driver == DbDriver::Jdbc {
|
||||
return Err(IdeError::invalid("Custom JDBC execution plans are unsupported"));
|
||||
}
|
||||
if driver == DbDriver::Oracle {
|
||||
// EXPLAIN PLAN writes PLAN_TABLE; the plan is read (and cleaned up) separately.
|
||||
let plan_id = format!(
|
||||
@@ -1554,6 +1578,9 @@ impl DbManager {
|
||||
schema: Option<&str>,
|
||||
) -> IdeResult<QueryResult> {
|
||||
let pool = self.pool(id).await?;
|
||||
if let Pool::Jdbc(p) = &*pool {
|
||||
return p.query_in_schema(statement, skip, max_rows, cancel, schema).await;
|
||||
}
|
||||
let session = self.sessions.lock().get(id).cloned();
|
||||
let Some(session) = session else {
|
||||
if schema.is_none() {
|
||||
@@ -1595,6 +1622,9 @@ impl DbManager {
|
||||
/// Switches `id` between auto-commit and manual commit. Leaving manual
|
||||
/// mode rolls back what was not committed.
|
||||
pub async fn set_manual_commit(&self, id: &str, manual: bool) -> IdeResult<()> {
|
||||
if manual && self.source(id)?.driver == DbDriver::Jdbc {
|
||||
return Err(IdeError::invalid("Custom JDBC manual transactions are unsupported"));
|
||||
}
|
||||
if manual {
|
||||
if self.source(id)?.driver == DbDriver::Clickhouse {
|
||||
return Err(IdeError::invalid("ClickHouse manual transactions are unsupported"));
|
||||
@@ -1614,6 +1644,9 @@ impl DbManager {
|
||||
if self.source(id).is_ok_and(|s| s.driver == DbDriver::Clickhouse) {
|
||||
return Err(IdeError::invalid("ClickHouse manual transactions are unsupported"));
|
||||
}
|
||||
if self.source(id).is_ok_and(|s| s.driver == DbDriver::Jdbc) {
|
||||
return Err(IdeError::invalid("Custom JDBC manual transactions are unsupported"));
|
||||
}
|
||||
let session = self.sessions.lock().get(id).cloned();
|
||||
let Some(session) = session else { return Ok(()) };
|
||||
let mut held = session.lock().await;
|
||||
@@ -1649,8 +1682,22 @@ impl DbManager {
|
||||
) -> IdeResult<TablePage> {
|
||||
let details = self.table_details(id, schema, table).await?;
|
||||
let pool = self.pool(id).await?;
|
||||
let sql = sql::table_page(pool.driver(), schema, table, filter, sort, offset, limit);
|
||||
let mut result = pool.execute(&sql, limit as usize, cancel).await?;
|
||||
let mut result = if let Pool::Jdbc(p) = &*pool {
|
||||
let q = &p.identifier_quote;
|
||||
let names = [schema, table].into_iter().chain(sort.iter().map(|k| k.column.as_str()));
|
||||
if q.is_empty()
|
||||
&& names.into_iter().any(|name| !name.chars().all(|c| c.is_ascii_alphanumeric() || c == '_'))
|
||||
{
|
||||
return Err(IdeError::invalid("This JDBC driver cannot quote the selected identifier"));
|
||||
}
|
||||
let quote = |s: &str| format!("{q}{}{q}", s.replace(q, &format!("{q}{q}")));
|
||||
let name = if schema.is_empty() { quote(table) } else { format!("{}.{}", quote(schema), quote(table)) };
|
||||
let sql = sql::table_select(&name, filter, sort, quote);
|
||||
p.page(&sql, offset, limit, cancel).await?
|
||||
} else {
|
||||
let sql = sql::table_page(pool.driver(), schema, table, filter, sort, offset, limit);
|
||||
pool.execute(&sql, limit as usize, cancel).await?
|
||||
};
|
||||
if result.columns.is_empty() {
|
||||
result.columns = details
|
||||
.columns
|
||||
@@ -1663,7 +1710,11 @@ impl DbManager {
|
||||
|
||||
/// Applies the table editor's pending changes in one transaction.
|
||||
pub async fn apply_edits(&self, id: &str, schema: &str, table: &str, edits: &[DbRowEdit]) -> IdeResult<u64> {
|
||||
if self.source(id)?.driver == DbDriver::Clickhouse {
|
||||
let source = self.source(id)?;
|
||||
if source.read_only {
|
||||
return Err(IdeError::new(ErrorKind::PermissionDenied, "the data source is read-only"));
|
||||
}
|
||||
if source.driver == DbDriver::Clickhouse {
|
||||
return Err(IdeError::invalid("ClickHouse table editing is unsupported"));
|
||||
}
|
||||
let pool = self.pool(id).await?;
|
||||
|
||||
@@ -36,7 +36,7 @@ pub fn explain_statement(driver: DbDriver, statement: &str) -> String {
|
||||
DbDriver::Sqlite => format!("EXPLAIN QUERY PLAN {s}"),
|
||||
DbDriver::Postgres => format!("EXPLAIN (FORMAT JSON) {s}"),
|
||||
DbDriver::Clickhouse => format!("EXPLAIN json = 1 {s}"),
|
||||
DbDriver::Mysql => format!("EXPLAIN {s}"),
|
||||
DbDriver::Mysql | DbDriver::Jdbc => format!("EXPLAIN {s}"),
|
||||
DbDriver::SqlServer => format!("SET SHOWPLAN_TEXT ON; {s}"),
|
||||
DbDriver::Oracle => format!("EXPLAIN PLAN FOR {s}"),
|
||||
DbDriver::Snowflake => format!("EXPLAIN USING TABULAR {s}"),
|
||||
@@ -77,6 +77,7 @@ pub fn parse(driver: DbDriver, result: &QueryResult) -> IdeResult<Vec<PlanNode>>
|
||||
let names: Vec<&str> = result.columns.iter().map(|c| c.name.as_str()).collect();
|
||||
Ok(mysql(&names, &result.rows))
|
||||
}
|
||||
DbDriver::Jdbc => Err(IdeError::invalid("Custom JDBC execution plans are unsupported")),
|
||||
DbDriver::SqlServer => Ok(Vec::new()),
|
||||
DbDriver::Snowflake => {
|
||||
let names: Vec<&str> = result.columns.iter().map(|c| c.name.as_str()).collect();
|
||||
|
||||
@@ -23,12 +23,13 @@ pub enum DbDriver {
|
||||
/// Oracle Database (pure-Rust thin driver, no client library).
|
||||
Oracle,
|
||||
Snowflake,
|
||||
Jdbc,
|
||||
}
|
||||
|
||||
impl DbDriver {
|
||||
pub fn default_port(self) -> Option<u16> {
|
||||
match self {
|
||||
DbDriver::Sqlite => None,
|
||||
DbDriver::Sqlite | DbDriver::Jdbc => None,
|
||||
DbDriver::Postgres => Some(5432),
|
||||
DbDriver::Mysql => Some(3306),
|
||||
DbDriver::SqlServer => Some(1433),
|
||||
@@ -191,6 +192,9 @@ pub struct DataSource {
|
||||
pub id: String,
|
||||
pub name: String,
|
||||
pub driver: DbDriver,
|
||||
/// An app-wide driver profile id; classpaths never come from a project.
|
||||
#[serde(skip_serializing_if = "String::is_empty")]
|
||||
pub jdbc_driver: String,
|
||||
pub host: String,
|
||||
pub port: Option<u16>,
|
||||
pub database: String,
|
||||
@@ -237,6 +241,7 @@ impl Default for DataSource {
|
||||
id: String::new(),
|
||||
name: String::new(),
|
||||
driver: DbDriver::Sqlite,
|
||||
jdbc_driver: String::new(),
|
||||
host: "localhost".into(),
|
||||
port: None,
|
||||
database: String::new(),
|
||||
@@ -390,6 +395,12 @@ impl DataSource {
|
||||
/// database, user, SSL and driver properties come from the URL (properties
|
||||
/// of the Advanced tab still apply after the URL's).
|
||||
pub fn resolved(&self) -> IdeResult<DataSource> {
|
||||
if self.driver == DbDriver::Jdbc {
|
||||
if !self.url.starts_with("jdbc:") {
|
||||
return Err(IdeError::invalid("Enter a JDBC URL starting with jdbc:"));
|
||||
}
|
||||
return Ok(self.clone());
|
||||
}
|
||||
if !self.url_only {
|
||||
return Ok(self.clone());
|
||||
}
|
||||
@@ -476,7 +487,7 @@ impl DataSource {
|
||||
|
||||
/// The password written in the URL of a [`DataSource::url_only`] data source.
|
||||
pub fn url_password(&self) -> Option<String> {
|
||||
if !self.url_only {
|
||||
if self.driver == DbDriver::Jdbc || !self.url_only {
|
||||
return None;
|
||||
}
|
||||
parse_url(&self.url).ok()?.password
|
||||
@@ -491,7 +502,11 @@ struct SourcesFile {
|
||||
|
||||
/// The project's data sources.
|
||||
pub fn load(root: &Path) -> IdeResult<Vec<DataSource>> {
|
||||
read(&project_dir::path(root, FILE))
|
||||
let mut sources = read(&project_dir::path(root, FILE))?;
|
||||
for source in &mut sources {
|
||||
source.global = false;
|
||||
}
|
||||
Ok(sources)
|
||||
}
|
||||
|
||||
/// Saves the project's data sources.
|
||||
|
||||
@@ -70,6 +70,7 @@ pub fn sqlserver_databases() -> &'static str {
|
||||
|
||||
pub fn schemas(driver: DbDriver, system: bool) -> &'static str {
|
||||
match driver {
|
||||
DbDriver::Jdbc => unreachable!("JDBC introspection uses DatabaseMetaData"),
|
||||
DbDriver::Sqlite => "SELECT name FROM pragma_database_list WHERE name <> 'temp' ORDER BY seq",
|
||||
DbDriver::Clickhouse if system => "SELECT name FROM system.databases ORDER BY name",
|
||||
DbDriver::Clickhouse => {
|
||||
@@ -114,6 +115,7 @@ pub fn schemas(driver: DbDriver, system: bool) -> &'static str {
|
||||
/// Rows: `name, kind` where kind is `table`, `view` or `materialized view`.
|
||||
pub fn tables(driver: DbDriver, schema: &str) -> String {
|
||||
match driver {
|
||||
DbDriver::Jdbc => unreachable!("JDBC introspection uses DatabaseMetaData"),
|
||||
DbDriver::Clickhouse => format!(
|
||||
"SELECT name, CASE engine WHEN 'View' THEN 'view' WHEN 'MaterializedView' THEN 'materialized view' \
|
||||
WHEN 'WindowView' THEN 'view' ELSE 'table' END FROM system.tables \
|
||||
@@ -164,6 +166,7 @@ const SNOWFLAKE_TYPE: &str = "CASE WHEN data_type = 'NUMBER' THEN 'NUMBER(' || n
|
||||
pub fn columns(driver: DbDriver, schema: &str, table: &str) -> String {
|
||||
let (s, t) = (quote_literal(driver, schema), quote_literal(driver, table));
|
||||
match driver {
|
||||
DbDriver::Jdbc => unreachable!("JDBC introspection uses DatabaseMetaData"),
|
||||
DbDriver::Clickhouse => format!(
|
||||
"SELECT name, type, CASE WHEN startsWith(type, 'Nullable(') \
|
||||
OR startsWith(type, 'LowCardinality(Nullable(') THEN 'YES' ELSE 'NO' END, \
|
||||
@@ -225,6 +228,7 @@ pub fn columns(driver: DbDriver, schema: &str, table: &str) -> String {
|
||||
pub fn indexes(driver: DbDriver, schema: &str, table: &str) -> String {
|
||||
let (s, t) = (quote_literal(driver, schema), quote_literal(driver, table));
|
||||
match driver {
|
||||
DbDriver::Jdbc => unreachable!("JDBC introspection uses DatabaseMetaData"),
|
||||
DbDriver::Clickhouse => format!(
|
||||
"SELECT * FROM (SELECT 'PRIMARY KEY' AS index_name, 0 AS is_unique, primary_key AS expression \
|
||||
FROM system.tables WHERE database = {s} AND name = {t} AND primary_key != '' \
|
||||
@@ -269,6 +273,7 @@ pub fn indexes(driver: DbDriver, schema: &str, table: &str) -> String {
|
||||
/// for SQL completion, grouped by table in column order.
|
||||
pub fn completion_columns(driver: DbDriver) -> &'static str {
|
||||
match driver {
|
||||
DbDriver::Jdbc => unreachable!("JDBC introspection uses DatabaseMetaData"),
|
||||
DbDriver::Clickhouse => {
|
||||
"SELECT c.database, c.table, CASE t.engine WHEN 'View' THEN 'view' \
|
||||
WHEN 'MaterializedView' THEN 'materialized view' WHEN 'WindowView' THEN 'view' ELSE 'table' END, \
|
||||
@@ -357,6 +362,7 @@ pub fn completion_columns(driver: DbDriver) -> &'static str {
|
||||
/// when the key references the primary key implicitly.
|
||||
pub fn foreign_keys(driver: DbDriver) -> &'static str {
|
||||
match driver {
|
||||
DbDriver::Jdbc => unreachable!("JDBC introspection uses DatabaseMetaData"),
|
||||
DbDriver::Clickhouse => "SELECT '', '', '', '', '', '', '' WHERE 0",
|
||||
DbDriver::Snowflake => "SELECT '', '', '', '', '', '', '' WHERE 1 = 0",
|
||||
DbDriver::Sqlite => {
|
||||
@@ -409,6 +415,7 @@ pub fn foreign_keys(driver: DbDriver) -> &'static str {
|
||||
pub fn table_foreign_keys(driver: DbDriver, schema: &str, table: &str) -> String {
|
||||
let (s, t) = (quote_literal(driver, schema), quote_literal(driver, table));
|
||||
match driver {
|
||||
DbDriver::Jdbc => unreachable!("JDBC introspection uses DatabaseMetaData"),
|
||||
DbDriver::Clickhouse | DbDriver::Snowflake => foreign_keys(driver).to_owned(),
|
||||
DbDriver::Sqlite => format!(
|
||||
"SELECT {s}, {t}, f.id, f.\"from\", {s}, f.\"table\", f.\"to\" FROM pragma_foreign_key_list({t}, {s}) f ORDER BY f.id, f.seq"
|
||||
@@ -432,7 +439,7 @@ pub fn table_foreign_keys(driver: DbDriver, schema: &str, table: &str) -> String
|
||||
pub fn current_schema(driver: DbDriver) -> Option<&'static str> {
|
||||
match driver {
|
||||
DbDriver::Clickhouse => Some("SELECT currentDatabase()"),
|
||||
DbDriver::Sqlite => None,
|
||||
DbDriver::Sqlite | DbDriver::Jdbc => None,
|
||||
DbDriver::Postgres => Some("SELECT current_schema()"),
|
||||
DbDriver::Mysql => Some("SELECT DATABASE()"),
|
||||
DbDriver::SqlServer => Some("SELECT DB_NAME()"),
|
||||
@@ -446,7 +453,7 @@ pub fn current_schema(driver: DbDriver) -> Option<&'static str> {
|
||||
/// has no schema to switch — SQLite's `main`, ClickHouse's `USE` differs).
|
||||
pub fn use_schema(driver: DbDriver, schema: &str) -> Option<String> {
|
||||
match driver {
|
||||
DbDriver::Sqlite | DbDriver::Clickhouse => None,
|
||||
DbDriver::Sqlite | DbDriver::Clickhouse | DbDriver::Jdbc => None,
|
||||
DbDriver::Postgres => Some(format!("SET search_path TO {}", quote_ident(driver, schema))),
|
||||
DbDriver::Mysql | DbDriver::SqlServer => Some(format!("USE {}", quote_ident(driver, schema))),
|
||||
DbDriver::Oracle => Some(format!("ALTER SESSION SET CURRENT_SCHEMA = {}", quote_ident(driver, schema))),
|
||||
@@ -462,17 +469,13 @@ pub struct SortKey {
|
||||
pub descending: bool,
|
||||
}
|
||||
|
||||
/// One page of a table: `limit + 1` rows so the caller knows whether more exist.
|
||||
pub fn table_page(
|
||||
driver: DbDriver,
|
||||
schema: &str,
|
||||
table: &str,
|
||||
pub(crate) fn table_select(
|
||||
qualified_name: &str,
|
||||
filter: Option<&str>,
|
||||
sort: &[SortKey],
|
||||
offset: u64,
|
||||
limit: u64,
|
||||
quote: impl Fn(&str) -> String,
|
||||
) -> String {
|
||||
let mut sql = format!("SELECT * FROM {}", qualified(driver, schema, table));
|
||||
let mut sql = format!("SELECT * FROM {}", qualified_name);
|
||||
if let Some(f) = filter.map(str::trim).filter(|f| !f.is_empty()) {
|
||||
let mut f = f.strip_prefix("WHERE ").or_else(|| f.strip_prefix("where ")).unwrap_or(f);
|
||||
if let Some(i) = f.find(';') {
|
||||
@@ -484,12 +487,24 @@ pub fn table_page(
|
||||
}
|
||||
}
|
||||
if !sort.is_empty() {
|
||||
let keys: Vec<String> = sort
|
||||
.iter()
|
||||
.map(|k| format!("{}{}", quote_ident(driver, &k.column), if k.descending { " DESC" } else { "" }))
|
||||
.collect();
|
||||
let keys: Vec<String> =
|
||||
sort.iter().map(|k| format!("{}{}", quote(&k.column), if k.descending { " DESC" } else { "" })).collect();
|
||||
sql.push_str(&format!(" ORDER BY {}", keys.join(", ")));
|
||||
}
|
||||
sql
|
||||
}
|
||||
|
||||
/// One page of a table: `limit + 1` rows so the caller knows whether more exist.
|
||||
pub fn table_page(
|
||||
driver: DbDriver,
|
||||
schema: &str,
|
||||
table: &str,
|
||||
filter: Option<&str>,
|
||||
sort: &[SortKey],
|
||||
offset: u64,
|
||||
limit: u64,
|
||||
) -> String {
|
||||
let mut sql = table_select(&qualified(driver, schema, table), filter, sort, |name| quote_ident(driver, name));
|
||||
if driver == DbDriver::SqlServer {
|
||||
if sort.is_empty() {
|
||||
sql.push_str(" ORDER BY (SELECT 1)");
|
||||
@@ -535,6 +550,9 @@ fn where_key(driver: DbDriver, key: &[CellValue]) -> String {
|
||||
/// The DML statement for `edit`. Values are written as quoted literals, which
|
||||
/// every supported database coerces to the column type.
|
||||
pub fn edit_statement(driver: DbDriver, schema: &str, table: &str, edit: &DbRowEdit) -> Result<String, String> {
|
||||
if driver == DbDriver::Jdbc {
|
||||
return Err("Custom JDBC table editing is unsupported".into());
|
||||
}
|
||||
if driver == DbDriver::Clickhouse {
|
||||
return Err("ClickHouse table editing is unsupported: primary keys do not uniquely identify rows and edits require transactions".into());
|
||||
}
|
||||
@@ -631,6 +649,7 @@ pub fn postgres_ddl_indexes(oid: u32) -> String {
|
||||
pub fn triggers(driver: DbDriver, schema: &str, table: &str) -> String {
|
||||
let (s, t) = (quote_literal(driver, schema), quote_literal(driver, table));
|
||||
match driver {
|
||||
DbDriver::Jdbc => unreachable!("JDBC introspection uses DatabaseMetaData"),
|
||||
DbDriver::Clickhouse => "SELECT '', '', '' WHERE 0".to_owned(),
|
||||
DbDriver::Snowflake => "SELECT '', '', '' WHERE 1 = 0".to_owned(),
|
||||
DbDriver::Sqlite => format!(
|
||||
@@ -691,7 +710,7 @@ pub fn postgres_legacy(sql: &str) -> Option<String> {
|
||||
pub fn routines(driver: DbDriver, schema: &str) -> Option<String> {
|
||||
let s = quote_literal(driver, schema);
|
||||
match driver {
|
||||
DbDriver::Sqlite | DbDriver::Clickhouse => None,
|
||||
DbDriver::Sqlite | DbDriver::Clickhouse | DbDriver::Jdbc => None,
|
||||
// Functions of extensions are left out, as in IDEA.
|
||||
DbDriver::Postgres => Some(format!(
|
||||
"SELECT p.proname, CASE WHEN p.prokind = 'p' THEN 'procedure' ELSE 'function' END, \
|
||||
|
||||
@@ -0,0 +1,215 @@
|
||||
use ide_core::{CancellationToken, IdeResult};
|
||||
use ide_db::{DataSource, DbDriver, DbJdbcDriver, DbManager, SortKey};
|
||||
use std::{path::Path, sync::Arc};
|
||||
|
||||
fn manager(dir: &Path) -> DbManager {
|
||||
let ssh = Arc::new(ide_ssh::SshManager::new(dir.join("ssh.json")));
|
||||
DbManager::new(dir.join("project"), dir.join("passwords.json"), dir.join("history.json"), ssh)
|
||||
.with_global_sources(dir.join("global"))
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn jdbc_profiles_persist_and_project_files_cannot_start_drivers() -> IdeResult<()> {
|
||||
let tmp = tempfile::tempdir()?;
|
||||
let jar = tmp.path().join("fixture.jar");
|
||||
std::fs::write(&jar, [])?;
|
||||
let db = manager(tmp.path());
|
||||
db.save_jdbc_driver(DbJdbcDriver {
|
||||
id: "fixture".into(),
|
||||
name: "Test".into(),
|
||||
jars: vec![jar.display().to_string()],
|
||||
class_name: "test.Driver".into(),
|
||||
url_template: "jdbc:test:".into(),
|
||||
})?;
|
||||
let reopened = manager(tmp.path());
|
||||
assert_eq!(reopened.jdbc_drivers()?[0].class_name, "test.Driver");
|
||||
let source = DataSource {
|
||||
id: "local".into(),
|
||||
name: "local".into(),
|
||||
driver: DbDriver::Jdbc,
|
||||
jdbc_driver: "fixture".into(),
|
||||
url: "jdbc:test:".into(),
|
||||
..Default::default()
|
||||
};
|
||||
reopened.save_sources(vec![source.clone()]).await?;
|
||||
let denied = reopened.pool("local").await.err().expect("project JDBC requires explicit approval");
|
||||
assert!(denied.message.contains("Review this project"));
|
||||
let readonly = DataSource { read_only: true, ..source.clone() };
|
||||
reopened.save_sources(vec![readonly]).await?;
|
||||
let err = reopened.apply_edits("local", "main", "sample", &[]).await.unwrap_err();
|
||||
assert_eq!(err.kind, ide_core::error::ErrorKind::PermissionDenied);
|
||||
reopened.approve_jdbc_source(&source)?;
|
||||
let mut changed = source.clone();
|
||||
changed.url.push_str("changed");
|
||||
reopened.save_sources(vec![changed]).await?;
|
||||
assert!(reopened.pool("local").await.err().unwrap().message.contains("Review this project"));
|
||||
let mut global = source;
|
||||
global.global = true;
|
||||
reopened.save_sources(vec![global]).await?;
|
||||
assert!(reopened.remove_jdbc_driver("fixture").is_err());
|
||||
reopened.save_sources(vec![]).await?;
|
||||
reopened.remove_jdbc_driver("fixture")?;
|
||||
assert!(manager(tmp.path()).jdbc_drivers()?.is_empty());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[ignore = "requires DUCKDB_JDBC_JAR and a local Java 17+ runtime"]
|
||||
async fn duckdb_custom_driver_through_database_manager() -> IdeResult<()> {
|
||||
let tmp = tempfile::tempdir()?;
|
||||
let db = manager(tmp.path());
|
||||
let jar = std::env::var("DUCKDB_JDBC_JAR").expect("DUCKDB_JDBC_JAR");
|
||||
assert_eq!(ide_db::jdbc_driver_classes(std::slice::from_ref(&jar))?, ["org.duckdb.DuckDBDriver"]);
|
||||
db.save_jdbc_driver(DbJdbcDriver {
|
||||
id: "duck".into(),
|
||||
name: "DuckDB".into(),
|
||||
jars: vec![jar],
|
||||
class_name: "org.duckdb.DuckDBDriver".into(),
|
||||
url_template: "jdbc:duckdb:".into(),
|
||||
})?;
|
||||
let source = DataSource {
|
||||
id: "test".into(),
|
||||
name: "DuckDB".into(),
|
||||
driver: DbDriver::Jdbc,
|
||||
global: true,
|
||||
jdbc_driver: "duck".into(),
|
||||
url: "jdbc:duckdb:local.duckdb".into(),
|
||||
..Default::default()
|
||||
};
|
||||
assert!(db.test(&source, None, None).await?.to_lowercase().contains("duckdb"));
|
||||
db.save_sources(vec![source]).await?;
|
||||
let db = manager(tmp.path());
|
||||
let c = CancellationToken::new();
|
||||
db.execute("test", "create table items(id int primary key, label varchar)", 1, &c).await?;
|
||||
db.execute("test", "insert into items values (1, NULL), (2, 'row')", 1, &c).await?;
|
||||
assert!(db.schemas("test").await?.iter().any(|v| v == "main"));
|
||||
assert!(db.tables("test", "main").await?.iter().any(|v| v.name == "items"));
|
||||
let details = db.table_details("test", "main", "items").await?;
|
||||
assert_eq!(details.columns.len(), 2);
|
||||
let result = db.execute("test", "select id, label from items order by id", 1, &c).await?;
|
||||
assert!(result.has_more);
|
||||
assert_eq!(result.rows, vec![vec![Some("1".into()), None]]);
|
||||
assert_eq!(result.columns[1].name, "label");
|
||||
assert_eq!(db.completion("test").await?.default_schema, "main");
|
||||
db.execute("test", "create temporary table session_marker as select 42 as id", 1, &c).await?;
|
||||
let stop = CancellationToken::new();
|
||||
let (cancelled, ()) =
|
||||
tokio::join!(db.execute("test", "select sum(sin(i)) from range(1000000000) t(i)", 1, &stop), async {
|
||||
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
|
||||
stop.cancel();
|
||||
},);
|
||||
assert_eq!(cancelled.unwrap_err().kind, ide_core::error::ErrorKind::Cancelled);
|
||||
let marker = db.execute("test", "select id from session_marker", 1, &c).await?;
|
||||
assert_eq!(marker.rows, [vec![Some("42".into())]]);
|
||||
let sort = [SortKey { column: "id".into(), descending: false }];
|
||||
let first = db.table_page("test", "main", "items", None, &sort, 0, 1, &c).await?.result;
|
||||
assert_eq!(first.rows, result.rows);
|
||||
assert!(first.has_more);
|
||||
let last = db.table_page("test", "main", "items", None, &sort, 1, 1, &c).await?.result;
|
||||
assert_eq!(last.rows, vec![vec![Some("2".into()), Some("row".into())]]);
|
||||
assert!(!last.has_more);
|
||||
let past = db.table_page("test", "main", "items", None, &sort, 2, 1, &c).await?.result;
|
||||
assert!(past.rows.is_empty() && !past.has_more);
|
||||
db.disconnect("test").await;
|
||||
let reopened = manager(tmp.path());
|
||||
assert_eq!(reopened.execute("test", "select count(*) from items", 1, &c).await?.rows, [vec![Some("2".into())]]);
|
||||
reopened.disconnect("test").await;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[ignore = "requires CLICKHOUSE_JDBC_CLASSPATH and a loopback ClickHouse fixture on CLICKHOUSE_JDBC_TEST_PORT"]
|
||||
async fn clickhouse_vendor_driver_reads_catalog_and_results() -> IdeResult<()> {
|
||||
let tmp = tempfile::tempdir()?;
|
||||
let db = manager(tmp.path());
|
||||
let jars: Vec<String> =
|
||||
std::env::split_paths(&std::env::var_os("CLICKHOUSE_JDBC_CLASSPATH").expect("CLICKHOUSE_JDBC_CLASSPATH"))
|
||||
.map(|p| p.display().to_string())
|
||||
.collect();
|
||||
let classes = ide_db::jdbc_driver_classes(&jars)?;
|
||||
assert_eq!(classes.first().map(String::as_str), Some("com.clickhouse.jdbc.ClickHouseDriver"));
|
||||
let port: u16 = std::env::var("CLICKHOUSE_JDBC_TEST_PORT").unwrap().parse().unwrap();
|
||||
db.save_jdbc_driver(DbJdbcDriver {
|
||||
id: "clickhouse".into(),
|
||||
name: "ClickHouse JDBC".into(),
|
||||
jars,
|
||||
class_name: "com.clickhouse.jdbc.ClickHouseDriver".into(),
|
||||
url_template: "jdbc:clickhouse://".into(),
|
||||
})?;
|
||||
let source = DataSource {
|
||||
id: "test".into(),
|
||||
name: "ClickHouse JDBC".into(),
|
||||
driver: DbDriver::Jdbc,
|
||||
jdbc_driver: "clickhouse".into(),
|
||||
url: format!("jdbc:clickhouse://127.0.0.1:{port}/default"),
|
||||
user: "fixture".into(),
|
||||
read_only: true,
|
||||
..Default::default()
|
||||
};
|
||||
let password = "local-fixture";
|
||||
assert!(db.test(&source, Some(password.into()), None).await?.contains("ClickHouse"));
|
||||
db.approve_jdbc_source(&source)?;
|
||||
db.save_sources(vec![source]).await?;
|
||||
db.set_password("test", Some(password.into()), false).await?;
|
||||
assert!(db.schemas("test").await?.iter().any(|v| v == "system"));
|
||||
assert!(db.tables("test", "system").await?.iter().any(|v| v.name == "one"));
|
||||
let cols = db.table_details("test", "system", "one").await?.columns;
|
||||
assert!(cols.iter().any(|col| col.name == "dummy"));
|
||||
let r = db.execute("test", "SELECT number FROM numbers(3) ORDER BY number", 2, &CancellationToken::new()).await?;
|
||||
assert_eq!(r.rows.len(), 2);
|
||||
assert!(r.has_more);
|
||||
let c = CancellationToken::new();
|
||||
let page = db.table_page("test", "system", "one", None, &[], 0, 1, &c).await?.result;
|
||||
assert_eq!(page.rows.len(), 1);
|
||||
assert!(!page.has_more);
|
||||
let past = db.table_page("test", "system", "one", None, &[], 1, 1, &c).await?.result;
|
||||
assert!(past.rows.is_empty() && !past.has_more);
|
||||
db.disconnect("test").await;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[ignore = "requires JDBC_FIXTURE_JAR (java/jdbc-bridge/build.py --test --out fixture.jar) and Java 17+"]
|
||||
async fn unresponsive_driver_closes_before_reconnect() -> IdeResult<()> {
|
||||
use std::time::Duration;
|
||||
let tmp = tempfile::tempdir()?;
|
||||
let db = manager(tmp.path());
|
||||
let started = tmp.path().join("started");
|
||||
db.save_jdbc_driver(DbJdbcDriver {
|
||||
id: "fixture".into(),
|
||||
name: "Fixture".into(),
|
||||
jars: vec![std::env::var("JDBC_FIXTURE_JAR").unwrap()],
|
||||
class_name: "ai.openidea.jdbc.SelfTest$Fixture".into(),
|
||||
url_template: String::new(),
|
||||
})?;
|
||||
let source = DataSource {
|
||||
id: "test".into(),
|
||||
name: "Fixture".into(),
|
||||
driver: DbDriver::Jdbc,
|
||||
global: true,
|
||||
read_only: true,
|
||||
jdbc_driver: "fixture".into(),
|
||||
url: format!("jdbc:fixture:{}", started.display()),
|
||||
..Default::default()
|
||||
};
|
||||
db.test(&source, None, None).await?;
|
||||
db.save_sources(vec![source]).await?;
|
||||
let normal = CancellationToken::new();
|
||||
assert!(db.execute("test", "write", 1, &normal).await.unwrap_err().message.contains("read-only"));
|
||||
let stop = CancellationToken::new();
|
||||
let (result, ()) = tokio::time::timeout(Duration::from_secs(12), async {
|
||||
tokio::join!(db.execute("test", "ignore cancel", 1, &stop), async {
|
||||
while !started.is_file() {
|
||||
tokio::time::sleep(Duration::from_millis(10)).await;
|
||||
}
|
||||
stop.cancel();
|
||||
})
|
||||
})
|
||||
.await
|
||||
.expect("driver cancellation must be bounded");
|
||||
assert!(result.unwrap_err().message.contains("Reconnect"));
|
||||
db.disconnect("test").await;
|
||||
assert_eq!(db.execute("test", "after reconnect", 1, &normal).await?.affected, Some(1.0));
|
||||
db.disconnect("test").await;
|
||||
Ok(())
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
[package]
|
||||
name = "ide-flutter"
|
||||
description = "Flutter devices, kept current by the `flutter daemon` protocol"
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
license.workspace = true
|
||||
|
||||
[dependencies]
|
||||
ide-android = { path = "../ide-android" }
|
||||
ide-core.workspace = true
|
||||
parking_lot.workspace = true
|
||||
serde_json.workspace = true
|
||||
|
||||
[dev-dependencies]
|
||||
tempfile.workspace = true
|
||||
@@ -0,0 +1,563 @@
|
||||
//! The devices a Flutter project can run on, from one `flutter daemon` per project window.
|
||||
//!
|
||||
//! The daemon speaks JSON-RPC over stdio, one bracketed message per line. After
|
||||
//! `daemon.connected` it is asked for `device.enable`, which makes it report `device.added` /
|
||||
//! `device.removed`, and once for `device.getDevices`, whose answer ends the "discovering" state.
|
||||
//!
|
||||
//! The daemon keeps running after its stdin closes, so closing the pipe does not end it: on Unix
|
||||
//! a watcher in its process group ends the group once the IDE is gone, even after a crash. A
|
||||
//! daemon that fails stays failed until the user restarts it.
|
||||
|
||||
use ide_android::AndroidDevice;
|
||||
use ide_core::event::{FlutterDaemonStatus, FlutterDevice, FlutterDeviceList};
|
||||
use ide_core::process::{kill_tree, own_group, prepare, terminate_tree};
|
||||
use ide_core::tool_path::{find_command, search_path_in};
|
||||
use ide_core::{IdeEvent, SharedSink};
|
||||
use parking_lot::Mutex;
|
||||
use serde_json::{Value, json};
|
||||
use std::collections::{BTreeMap, VecDeque};
|
||||
use std::io::{BufRead, BufReader, Read, Write};
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::process::{Child, ChildStdin, Command, Stdio};
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
|
||||
const ENABLE: u64 = 1;
|
||||
const GET_DEVICES: u64 = 2;
|
||||
/// How long the daemon gets to exit on `SIGTERM`.
|
||||
const KILL_GRACE: Duration = Duration::from_millis(200);
|
||||
/// Lines of unrecognised output kept to explain a failure.
|
||||
const OUTPUT_TAIL: usize = 8;
|
||||
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
enum Message {
|
||||
Connected,
|
||||
Added(FlutterDevice),
|
||||
Removed(String),
|
||||
Devices(Vec<FlutterDevice>),
|
||||
Other,
|
||||
}
|
||||
|
||||
fn device(value: &Value) -> Option<FlutterDevice> {
|
||||
let text = |key| value.get(key).and_then(Value::as_str).map(str::to_owned);
|
||||
Some(FlutterDevice {
|
||||
id: text("id")?,
|
||||
name: text("name")?,
|
||||
platform_type: text("platformType"),
|
||||
category: text("category"),
|
||||
emulator: value.get("emulator").and_then(Value::as_bool).unwrap_or(false),
|
||||
unavailable: None,
|
||||
})
|
||||
}
|
||||
|
||||
/// The messages of one stdout line; `None` for anything outside the protocol (the tool's own
|
||||
/// chatter, such as waiting for the startup lock).
|
||||
fn parse_line(line: &str) -> Option<Vec<Message>> {
|
||||
let line = line.trim();
|
||||
if !line.starts_with('[') {
|
||||
return None;
|
||||
}
|
||||
let messages: Vec<Value> = serde_json::from_str(line).ok()?;
|
||||
Some(messages.iter().map(parse_message).collect())
|
||||
}
|
||||
|
||||
fn parse_message(message: &Value) -> Message {
|
||||
let params = message.get("params").unwrap_or(&Value::Null);
|
||||
match message.get("event").and_then(Value::as_str) {
|
||||
Some("daemon.connected") => Message::Connected,
|
||||
Some("device.added") => device(params).map_or(Message::Other, Message::Added),
|
||||
Some("device.removed") => {
|
||||
params.get("id").and_then(Value::as_str).map_or(Message::Other, |id| Message::Removed(id.to_owned()))
|
||||
}
|
||||
Some(_) => Message::Other,
|
||||
None if message.get("id").and_then(Value::as_u64) == Some(GET_DEVICES) => {
|
||||
let result = message.get("result").and_then(Value::as_array);
|
||||
Message::Devices(result.into_iter().flatten().filter_map(device).collect())
|
||||
}
|
||||
None => Message::Other,
|
||||
}
|
||||
}
|
||||
|
||||
fn request(id: u64, method: &str) -> String {
|
||||
format!("{}\n", json!([{ "id": id, "method": method }]))
|
||||
}
|
||||
|
||||
/// The daemon's devices, plus the Android devices adb sees that Flutter leaves out until they
|
||||
/// are usable. A daemon Android device's id is its adb serial.
|
||||
fn merged(daemon: &BTreeMap<String, FlutterDevice>, adb: &[AndroidDevice]) -> Vec<FlutterDevice> {
|
||||
let mut devices: Vec<FlutterDevice> = daemon.values().cloned().collect();
|
||||
devices.extend(adb.iter().filter(|d| !d.is_online() && !daemon.contains_key(&d.serial)).map(|d| FlutterDevice {
|
||||
id: d.serial.clone(),
|
||||
name: d.label().replace('_', " "),
|
||||
platform_type: Some("android".into()),
|
||||
category: Some("mobile".into()),
|
||||
emulator: d.serial.starts_with("emulator-"),
|
||||
unavailable: Some(d.state.clone()),
|
||||
}));
|
||||
let rank = |d: &FlutterDevice| match d.category.as_deref() {
|
||||
Some("mobile") => 0,
|
||||
Some("desktop") => 1,
|
||||
Some("web") => 2,
|
||||
_ => 3,
|
||||
};
|
||||
devices.sort_by_cached_key(|d| (d.unavailable.is_some(), rank(d), d.emulator, d.name.to_lowercase(), d.id.clone()));
|
||||
devices
|
||||
}
|
||||
|
||||
struct Running {
|
||||
child: Child,
|
||||
stdin: Option<ChildStdin>,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct State {
|
||||
/// Bumped by every start and stop: a reader of an older process no longer has a say.
|
||||
generation: u64,
|
||||
running: Option<Running>,
|
||||
status: FlutterDaemonStatus,
|
||||
devices: BTreeMap<String, FlutterDevice>,
|
||||
adb: Vec<AndroidDevice>,
|
||||
}
|
||||
|
||||
impl State {
|
||||
fn list(&self) -> FlutterDeviceList {
|
||||
FlutterDeviceList { status: self.status.clone(), devices: merged(&self.devices, &self.adb) }
|
||||
}
|
||||
|
||||
fn shut_down(&mut self) {
|
||||
self.generation += 1;
|
||||
if let Some(Running { mut child, stdin }) = self.running.take() {
|
||||
drop(stdin);
|
||||
// Off the caller's thread: closing a window must not wait for the grace period.
|
||||
std::thread::spawn(move || terminate_tree(&mut child, KILL_GRACE));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct Shared {
|
||||
root: PathBuf,
|
||||
sink: SharedSink,
|
||||
/// Replaces the lookup on the search path (tests).
|
||||
executable: Option<PathBuf>,
|
||||
state: Mutex<State>,
|
||||
}
|
||||
|
||||
impl Shared {
|
||||
fn emit(&self, list: FlutterDeviceList) {
|
||||
self.sink.emit(IdeEvent::FlutterDevices { list });
|
||||
}
|
||||
}
|
||||
|
||||
/// One project window's `flutter daemon`, started on first use.
|
||||
pub struct FlutterDaemon(Arc<Shared>);
|
||||
|
||||
impl FlutterDaemon {
|
||||
pub fn new(root: PathBuf, sink: SharedSink) -> Self {
|
||||
Self::with_executable(root, sink, None)
|
||||
}
|
||||
|
||||
fn with_executable(root: PathBuf, sink: SharedSink, executable: Option<PathBuf>) -> Self {
|
||||
Self(Arc::new(Shared { root, sink, executable, state: Mutex::default() }))
|
||||
}
|
||||
|
||||
/// The current list; starts the daemon unless it already ran. Never blocks on the process.
|
||||
pub fn devices(&self) -> FlutterDeviceList {
|
||||
let mut state = self.0.state.lock();
|
||||
if state.status == FlutterDaemonStatus::Stopped {
|
||||
start(&self.0, &mut state);
|
||||
}
|
||||
state.list()
|
||||
}
|
||||
|
||||
/// Replaces the daemon with a new one (the user's way out of a failure).
|
||||
pub fn restart(&self) -> FlutterDeviceList {
|
||||
let mut state = self.0.state.lock();
|
||||
state.shut_down();
|
||||
start(&self.0, &mut state);
|
||||
state.list()
|
||||
}
|
||||
|
||||
/// Reads adb again: a device awaiting authorization changes no daemon event.
|
||||
pub fn refresh(&self) {
|
||||
let generation = self.0.state.lock().generation;
|
||||
refresh_adb(&self.0, generation);
|
||||
}
|
||||
|
||||
/// Ends the daemon (project closed, or no longer trusted).
|
||||
pub fn stop(&self) {
|
||||
let mut state = self.0.state.lock();
|
||||
state.shut_down();
|
||||
state.status = FlutterDaemonStatus::Stopped;
|
||||
state.devices.clear();
|
||||
state.adb.clear();
|
||||
}
|
||||
|
||||
/// Ends the daemon before returning, for a process about to exit.
|
||||
pub fn kill(&self) {
|
||||
let mut state = self.0.state.lock();
|
||||
state.generation += 1;
|
||||
if let Some(mut running) = state.running.take() {
|
||||
kill_tree(&mut running.child);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn start(shared: &Arc<Shared>, state: &mut State) {
|
||||
state.generation += 1;
|
||||
state.status = FlutterDaemonStatus::Starting;
|
||||
state.devices.clear();
|
||||
state.adb.clear();
|
||||
let (shared, generation) = (shared.clone(), state.generation);
|
||||
std::thread::spawn(move || run(&shared, generation));
|
||||
}
|
||||
|
||||
fn refresh_adb(shared: &Arc<Shared>, generation: u64) {
|
||||
let shared = shared.clone();
|
||||
std::thread::spawn(move || {
|
||||
// Without adb there is simply nothing to add.
|
||||
let adb = ide_android::devices().unwrap_or_default();
|
||||
let mut state = shared.state.lock();
|
||||
if state.generation != generation || state.adb == adb {
|
||||
return;
|
||||
}
|
||||
state.adb = adb;
|
||||
let list = state.list();
|
||||
drop(state);
|
||||
shared.emit(list);
|
||||
});
|
||||
}
|
||||
|
||||
/// `flutter daemon`, ended with its whole process group when process `parent` is gone.
|
||||
#[cfg(unix)]
|
||||
fn daemon_command(executable: &Path, parent: u32) -> Command {
|
||||
const WATCHED: &str = r#"(while kill -0 "$1" 2>/dev/null; do sleep 5; done; kill -KILL 0) </dev/null >/dev/null 2>&1 &
|
||||
exec "$2" daemon"#;
|
||||
let mut cmd = Command::new("/bin/sh");
|
||||
cmd.args(["-c", WATCHED, "sh"]).arg(parent.to_string()).arg(executable);
|
||||
cmd
|
||||
}
|
||||
|
||||
// No watcher on Windows, so a crashed IDE leaves the daemon; a Job Object would cover it.
|
||||
#[cfg(not(unix))]
|
||||
fn daemon_command(executable: &Path, _parent: u32) -> Command {
|
||||
let mut cmd = Command::new(executable);
|
||||
cmd.arg("daemon");
|
||||
cmd
|
||||
}
|
||||
|
||||
fn spawn_daemon(executable: &Path, root: &Path) -> std::io::Result<Child> {
|
||||
let mut cmd = daemon_command(executable, std::process::id());
|
||||
cmd.current_dir(root).stdin(Stdio::piped()).stdout(Stdio::piped()).stderr(Stdio::piped());
|
||||
prepare(own_group(&mut cmd));
|
||||
cmd.spawn()
|
||||
}
|
||||
|
||||
/// Sets the final status of a daemon that is gone, unless it was replaced or stopped meanwhile.
|
||||
fn finish(shared: &Shared, generation: u64, status: FlutterDaemonStatus) {
|
||||
let mut state = shared.state.lock();
|
||||
if state.generation != generation {
|
||||
return;
|
||||
}
|
||||
let running = state.running.take();
|
||||
state.status = status;
|
||||
let list = state.list();
|
||||
drop(state);
|
||||
if let Some(Running { mut child, .. }) = running {
|
||||
std::thread::spawn(move || terminate_tree(&mut child, Duration::ZERO));
|
||||
}
|
||||
shared.emit(list);
|
||||
}
|
||||
|
||||
fn keep_output(tail: &Mutex<VecDeque<String>>, line: &str) {
|
||||
if line.trim().is_empty() {
|
||||
return;
|
||||
}
|
||||
let mut tail = tail.lock();
|
||||
if tail.len() == OUTPUT_TAIL {
|
||||
tail.pop_front();
|
||||
}
|
||||
tail.push_back(line.trim_end().to_owned());
|
||||
}
|
||||
|
||||
fn read_lines(pipe: impl Read, mut on_line: impl FnMut(&str) -> bool) {
|
||||
let mut reader = BufReader::new(pipe);
|
||||
let mut bytes = Vec::new();
|
||||
loop {
|
||||
bytes.clear();
|
||||
match reader.read_until(b'\n', &mut bytes) {
|
||||
Ok(0) | Err(_) => return,
|
||||
Ok(_) if !on_line(&String::from_utf8_lossy(&bytes)) => return,
|
||||
Ok(_) => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// One daemon's whole life, on its own thread.
|
||||
fn run(shared: &Arc<Shared>, generation: u64) {
|
||||
let executable =
|
||||
shared.executable.clone().or_else(|| find_command("flutter", &search_path_in(&shared.root), &shared.root));
|
||||
let Some(executable) = executable else {
|
||||
return finish(shared, generation, FlutterDaemonStatus::NotInstalled);
|
||||
};
|
||||
let mut child = match spawn_daemon(&executable, &shared.root) {
|
||||
Ok(child) => child,
|
||||
Err(e) => {
|
||||
let message = format!("could not run {}: {e}", executable.display());
|
||||
return finish(shared, generation, FlutterDaemonStatus::Failed { message });
|
||||
}
|
||||
};
|
||||
let (Some(stdout), Some(stderr)) = (child.stdout.take(), child.stderr.take()) else {
|
||||
return finish(shared, generation, FlutterDaemonStatus::Failed { message: "no output pipe".into() });
|
||||
};
|
||||
let stdin = child.stdin.take();
|
||||
{
|
||||
let mut state = shared.state.lock();
|
||||
if state.generation != generation {
|
||||
drop(state);
|
||||
drop(stdin);
|
||||
terminate_tree(&mut child, KILL_GRACE);
|
||||
return;
|
||||
}
|
||||
state.running = Some(Running { child, stdin });
|
||||
}
|
||||
let tail = Arc::new(Mutex::new(VecDeque::new()));
|
||||
let errors = {
|
||||
let tail = tail.clone();
|
||||
std::thread::spawn(move || {
|
||||
read_lines(stderr, |line| {
|
||||
keep_output(&tail, line);
|
||||
true
|
||||
});
|
||||
})
|
||||
};
|
||||
read_lines(stdout, |line| match parse_line(line) {
|
||||
Some(messages) => messages.into_iter().all(|message| handle(shared, generation, message)),
|
||||
None => {
|
||||
keep_output(&tail, line);
|
||||
true
|
||||
}
|
||||
});
|
||||
let _ = errors.join();
|
||||
let message = Vec::from(std::mem::take(&mut *tail.lock())).join("\n");
|
||||
let message = if message.is_empty() { "flutter daemon exited".to_owned() } else { message };
|
||||
finish(shared, generation, FlutterDaemonStatus::Failed { message });
|
||||
}
|
||||
|
||||
/// Applies one message; false once this daemon has been replaced or stopped.
|
||||
fn handle(shared: &Arc<Shared>, generation: u64, message: Message) -> bool {
|
||||
let mut state = shared.state.lock();
|
||||
if state.generation != generation {
|
||||
return false;
|
||||
}
|
||||
match message {
|
||||
Message::Other => return true,
|
||||
Message::Connected => {
|
||||
if let Some(stdin) = state.running.as_mut().and_then(|r| r.stdin.as_mut()) {
|
||||
let requests = request(ENABLE, "device.enable") + &request(GET_DEVICES, "device.getDevices");
|
||||
let _ = stdin.write_all(requests.as_bytes()).and_then(|()| stdin.flush());
|
||||
}
|
||||
return true;
|
||||
}
|
||||
Message::Added(device) => {
|
||||
state.devices.insert(device.id.clone(), device);
|
||||
}
|
||||
Message::Removed(id) => {
|
||||
state.devices.remove(&id);
|
||||
}
|
||||
// Merged, not replaced: events may have overtaken this answer.
|
||||
Message::Devices(devices) => {
|
||||
state.devices.extend(devices.into_iter().map(|d| (d.id.clone(), d)));
|
||||
state.status = FlutterDaemonStatus::Ready;
|
||||
}
|
||||
}
|
||||
let list = state.list();
|
||||
drop(state);
|
||||
shared.emit(list);
|
||||
refresh_adb(shared, generation);
|
||||
true
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn dev(id: &str, name: &str, category: &str, emulator: bool) -> FlutterDevice {
|
||||
FlutterDevice {
|
||||
id: id.into(),
|
||||
name: name.into(),
|
||||
platform_type: None,
|
||||
category: Some(category.into()),
|
||||
emulator,
|
||||
unavailable: None,
|
||||
}
|
||||
}
|
||||
|
||||
fn adb(serial: &str, state: &str) -> AndroidDevice {
|
||||
AndroidDevice {
|
||||
serial: serial.into(),
|
||||
state: state.into(),
|
||||
model: None,
|
||||
product: None,
|
||||
device: None,
|
||||
transport_id: None,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reads_the_daemon_protocol() {
|
||||
assert_eq!(
|
||||
parse_line(r#"[{"event":"daemon.connected","params":{"version":"0.6.1","pid":7}}]"#),
|
||||
Some(vec![Message::Connected])
|
||||
);
|
||||
let added = parse_line(
|
||||
r#"[{"event":"device.added","params":{"id":"emulator-5554","name":"Pixel","platform":"android-arm64",
|
||||
"emulator":true,"category":"mobile","platformType":"android","ephemeral":true}}]"#
|
||||
.replace('\n', " ")
|
||||
.as_str(),
|
||||
);
|
||||
let Some([Message::Added(device)]) = added.as_deref() else { panic!("{added:?}") };
|
||||
assert_eq!(device.id, "emulator-5554");
|
||||
assert_eq!(device.platform_type.as_deref(), Some("android"));
|
||||
assert!(device.emulator);
|
||||
assert_eq!(
|
||||
parse_line(r#"[{"event":"device.removed","params":{"id":"macos","name":"macOS"}}]"#),
|
||||
Some(vec![Message::Removed("macos".into())])
|
||||
);
|
||||
let answer = parse_line(r#"[{"id":2,"result":[{"id":"chrome","name":"Chrome","category":null}]}]"#);
|
||||
let Some([Message::Devices(devices)]) = answer.as_deref() else { panic!("{answer:?}") };
|
||||
assert_eq!((devices[0].id.as_str(), devices[0].category.as_deref()), ("chrome", None));
|
||||
assert_eq!(parse_line(r#"[{"id":1,"result":null}]"#), Some(vec![Message::Other]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn output_outside_the_protocol_is_not_a_message() {
|
||||
assert_eq!(parse_line("Waiting for another flutter command to release the startup lock..."), None);
|
||||
assert_eq!(parse_line("[not json"), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lists_usable_devices_first_and_adds_what_only_adb_sees() {
|
||||
let daemon: BTreeMap<_, _> = [
|
||||
dev("chrome", "Chrome", "web", false),
|
||||
dev("macos", "macOS", "desktop", false),
|
||||
dev("emulator-5554", "Pixel", "mobile", true),
|
||||
dev("R5C", "Galaxy", "mobile", false),
|
||||
]
|
||||
.into_iter()
|
||||
.map(|d| (d.id.clone(), d))
|
||||
.collect();
|
||||
let adb = [adb("R5C", "device"), adb("emulator-5554", "offline"), adb("X1", "unauthorized")];
|
||||
let list = merged(&daemon, &adb);
|
||||
let ids: Vec<_> = list.iter().map(|d| d.id.as_str()).collect();
|
||||
assert_eq!(ids, ["R5C", "emulator-5554", "macos", "chrome", "X1"]);
|
||||
assert_eq!(list[4].unavailable.as_deref(), Some("unauthorized"));
|
||||
assert_eq!(list[1].unavailable, None, "what the daemon lists is usable");
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
mod process {
|
||||
use super::super::*;
|
||||
use ide_core::EventSink;
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
use std::time::Instant;
|
||||
|
||||
#[derive(Default)]
|
||||
struct Lists(Mutex<Vec<FlutterDeviceList>>);
|
||||
|
||||
impl EventSink for Lists {
|
||||
fn emit(&self, event: IdeEvent) {
|
||||
if let IdeEvent::FlutterDevices { list } = event {
|
||||
self.0.lock().push(list);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn daemon(dir: &Path, script: &str) -> (FlutterDaemon, Arc<Lists>) {
|
||||
let path = dir.join("flutter");
|
||||
std::fs::write(&path, format!("#!/bin/sh\n{script}")).unwrap();
|
||||
std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o755)).unwrap();
|
||||
let lists = Arc::new(Lists::default());
|
||||
(FlutterDaemon::with_executable(dir.to_owned(), lists.clone(), Some(path)), lists)
|
||||
}
|
||||
|
||||
fn wait_for(what: &str, done: impl Fn() -> bool) {
|
||||
let deadline = Instant::now() + Duration::from_secs(10);
|
||||
while !done() {
|
||||
assert!(Instant::now() < deadline, "timed out waiting for {what}");
|
||||
std::thread::sleep(Duration::from_millis(10));
|
||||
}
|
||||
}
|
||||
|
||||
const SESSION: &str = r#"
|
||||
echo $$ > pid
|
||||
echo 'Waiting for another flutter command to release the startup lock...'
|
||||
echo '[{"event":"daemon.connected","params":{"version":"0.6.1","pid":1}}]'
|
||||
read enable; read get; echo "$enable$get" > requests
|
||||
echo '[{"id":1,"result":null}]'
|
||||
echo '[{"id":2,"result":[{"id":"macos","name":"macOS","category":"desktop","emulator":false}]}]'
|
||||
echo '[{"event":"device.added","params":{"id":"chrome","name":"Chrome","category":"web","emulator":false}}]'
|
||||
echo '[{"event":"device.removed","params":{"id":"macos","name":"macOS"}}]'
|
||||
cat > /dev/null
|
||||
"#;
|
||||
|
||||
#[test]
|
||||
fn follows_devices_and_ends_with_the_project() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let (daemon, lists) = daemon(dir.path(), SESSION);
|
||||
assert_eq!(daemon.devices().status, FlutterDaemonStatus::Starting);
|
||||
wait_for("the removal", || {
|
||||
let list = daemon.devices();
|
||||
list.status == FlutterDaemonStatus::Ready
|
||||
&& list.devices.iter().map(|d| d.id.as_str()).collect::<Vec<_>>() == ["chrome"]
|
||||
});
|
||||
let requests = std::fs::read_to_string(dir.path().join("requests")).unwrap();
|
||||
assert!(requests.contains("device.enable") && requests.contains("device.getDevices"), "{requests}");
|
||||
assert!(lists.0.lock().iter().any(|l| l.devices.iter().any(|d| d.id == "macos")));
|
||||
|
||||
let pid = std::fs::read_to_string(dir.path().join("pid")).unwrap();
|
||||
let alive =
|
||||
|| Command::new("kill").args(["-0", pid.trim()]).stderr(Stdio::null()).status().unwrap().success();
|
||||
assert!(alive());
|
||||
daemon.stop();
|
||||
wait_for("the process to end", || !alive());
|
||||
assert_eq!(daemon.0.state.lock().status, FlutterDaemonStatus::Stopped);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_daemon_does_not_outlive_the_ide() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let flutter = dir.path().join("flutter");
|
||||
std::fs::write(&flutter, "#!/bin/sh\necho $$ > pid\nexec sleep 60\n").unwrap();
|
||||
std::fs::set_permissions(&flutter, std::fs::Permissions::from_mode(0o755)).unwrap();
|
||||
let mut ide = Command::new("sleep").arg("60").spawn().unwrap();
|
||||
let mut cmd = daemon_command(&flutter, ide.id());
|
||||
cmd.current_dir(dir.path()).stdin(Stdio::piped());
|
||||
let mut daemon = own_group(&mut cmd).spawn().unwrap();
|
||||
wait_for("the daemon", || dir.path().join("pid").is_file());
|
||||
assert_eq!(daemon.try_wait().unwrap(), None);
|
||||
ide.kill().unwrap();
|
||||
ide.wait().unwrap();
|
||||
let deadline = Instant::now() + Duration::from_secs(10);
|
||||
while daemon.try_wait().unwrap().is_none() {
|
||||
assert!(Instant::now() < deadline, "the daemon outlived its parent");
|
||||
std::thread::sleep(Duration::from_millis(50));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_crash_is_reported_and_not_restarted_until_asked() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let (daemon, _lists) = daemon(dir.path(), "echo run >> runs\necho 'SDK is broken' >&2\nexit 3\n");
|
||||
daemon.devices();
|
||||
let failed = || matches!(daemon.0.state.lock().status, FlutterDaemonStatus::Failed { .. });
|
||||
wait_for("the failure", failed);
|
||||
let FlutterDaemonStatus::Failed { message } = daemon.devices().status else { panic!("still failed") };
|
||||
assert_eq!(message, "SDK is broken");
|
||||
std::thread::sleep(Duration::from_millis(100));
|
||||
let runs = || std::fs::read_to_string(dir.path().join("runs")).unwrap().lines().count();
|
||||
assert_eq!(runs(), 1);
|
||||
daemon.restart();
|
||||
wait_for("the second run", || runs() == 2);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -58,6 +58,7 @@ ide-plugin.workspace = true
|
||||
ide-tools = { path = "../ide-tools" }
|
||||
ide-scaffold.workspace = true
|
||||
ide-docker = { path = "../ide-docker" }
|
||||
ide-flutter = { path = "../ide-flutter" }
|
||||
semver = "1"
|
||||
serde.workspace = true
|
||||
jsonschema = { version = "0.33", default-features = false }
|
||||
|
||||
@@ -11,7 +11,12 @@ use std::path::PathBuf;
|
||||
use std::sync::{Arc, OnceLock};
|
||||
|
||||
fn main() {
|
||||
let args: Vec<String> = std::env::args().skip(1).collect();
|
||||
let raw_args: Vec<String> = std::env::args().collect();
|
||||
if let Some(project) = ide_mcp::stdio_request(&raw_args) {
|
||||
let base = server_home();
|
||||
std::process::exit(ide_mcp::stdio::run(&ide_mcp::discovery_path(&base.join("data")), project));
|
||||
}
|
||||
let args: Vec<String> = raw_args.into_iter().skip(1).collect();
|
||||
if args.iter().any(|a| a == "--version") {
|
||||
println!("openidea-server {} (protocol {})", env!("CARGO_PKG_VERSION"), rpc::PROTOCOL_VERSION);
|
||||
return;
|
||||
@@ -31,7 +36,9 @@ fn main() {
|
||||
std::process::exit(code);
|
||||
}
|
||||
if !args.iter().any(|a| a == "--stdio") {
|
||||
eprintln!("usage: openidea-server --stdio | --connect <host> <port> | --version");
|
||||
eprintln!(
|
||||
"usage: openidea-server --stdio | --mcp-stdio [--project <path>] | --connect <host> <port> | --version"
|
||||
);
|
||||
std::process::exit(2);
|
||||
}
|
||||
// stdout carries the protocol: logs go to stderr.
|
||||
@@ -39,8 +46,7 @@ fn main() {
|
||||
.with_writer(std::io::stderr)
|
||||
.with_env_filter(tracing_subscriber::EnvFilter::try_from_env("OPENIDEA_LOG").unwrap_or_else(|_| "info".into()))
|
||||
.init();
|
||||
let home = dirs::home_dir().unwrap_or_default();
|
||||
let base = std::env::var_os("OPENIDEA_SERVER_HOME").map(PathBuf::from).unwrap_or_else(|| home.join(".openidea"));
|
||||
let base = server_home();
|
||||
// An SSH exec channel gets a minimal environment: take the login shell's, read
|
||||
// afresh on every start, plus the IDE-managed tools. Before any other thread starts.
|
||||
ide_host::shell_env::inherit_login_shell_env("openidea-server");
|
||||
@@ -53,6 +59,11 @@ fn main() {
|
||||
std::process::exit(code);
|
||||
}
|
||||
|
||||
fn server_home() -> PathBuf {
|
||||
let home = dirs::home_dir().unwrap_or_default();
|
||||
std::env::var_os("OPENIDEA_SERVER_HOME").map(PathBuf::from).unwrap_or_else(|| home.join(".openidea"))
|
||||
}
|
||||
|
||||
async fn run(base: PathBuf) -> i32 {
|
||||
let (config_dir, data_dir) = (base.join("config"), base.join("data"));
|
||||
for dir in [&config_dir, &data_dir] {
|
||||
@@ -85,6 +96,9 @@ async fn run(base: PathBuf) -> i32 {
|
||||
runtime: tokio::runtime::Handle::current(),
|
||||
project_environment: true,
|
||||
}));
|
||||
// A stable path for `--mcp-stdio` client configurations across server upgrades.
|
||||
host.mcp.set_stdio_link(base.join("bin").join("openidea-server"));
|
||||
host.mcp.apply(&host);
|
||||
// The desktop's code plugins run here, next to the project (`plugin_sync_packages`).
|
||||
let plugins = match RemotePlugins::start(&plugins_data, &host, peer.clone()) {
|
||||
Ok(p) => Some(p),
|
||||
@@ -112,6 +126,7 @@ async fn run(base: PathBuf) -> i32 {
|
||||
}
|
||||
});
|
||||
rpc::serve(host.clone(), dispatcher, peer, tokio::io::stdin(), tokio::io::stdout()).await;
|
||||
host.mcp.stop(&host);
|
||||
if let Some(plugins) = &plugins {
|
||||
plugins.host().shutdown();
|
||||
}
|
||||
|
||||
@@ -59,7 +59,8 @@ pub struct HostConfig {
|
||||
}
|
||||
|
||||
pub struct Host {
|
||||
pub(crate) mcp_executable: RwLock<Option<PathBuf>>,
|
||||
pub(crate) mcp_executable: Arc<RwLock<Option<PathBuf>>>,
|
||||
pub mcp: mcp::McpService,
|
||||
plugin_host: RwLock<std::sync::Weak<plugins::PluginHost>>,
|
||||
/// Every `Diagnostics` event the host ever emitted, per file and producer:
|
||||
/// the snapshot plugins read (`problems`), independent of windows and reports.
|
||||
@@ -152,7 +153,8 @@ impl Host {
|
||||
ssh.set_configs(global_settings.effective().ssh.configs);
|
||||
let host = Self {
|
||||
plugin_host: RwLock::default(),
|
||||
mcp_executable: RwLock::default(),
|
||||
mcp_executable: Arc::default(),
|
||||
mcp: Default::default(),
|
||||
diagnostics,
|
||||
mcp_reports: Default::default(),
|
||||
mcp_requests: Default::default(),
|
||||
@@ -222,12 +224,12 @@ impl Host {
|
||||
}
|
||||
}
|
||||
|
||||
/// Associates the plugin runtime that executes this host's project operations.
|
||||
/// Publishes only the executable of the running built-in MCP service.
|
||||
/// Publishes the executable of the running built-in MCP service (`None` while it is off).
|
||||
pub fn set_mcp_executable(&self, executable: Option<PathBuf>) {
|
||||
*self.mcp_executable.write() = executable;
|
||||
}
|
||||
|
||||
/// Associates the plugin runtime that executes this host's project operations.
|
||||
pub fn set_plugin_host(&self, plugins: &Arc<plugins::PluginHost>) {
|
||||
*self.plugin_host.write() = Arc::downgrade(plugins);
|
||||
}
|
||||
@@ -341,6 +343,7 @@ impl Host {
|
||||
}
|
||||
project.documents.clear_workspace_edit_history();
|
||||
project.agents.shutdown();
|
||||
project.flutter.stop();
|
||||
project.notebooks.stop_admission();
|
||||
// What the closing window started goes with it — the builds and searches it
|
||||
// asked for, its runs, its shells and its debug sessions — and its language
|
||||
@@ -459,6 +462,9 @@ impl Host {
|
||||
pub fn stop_processes_on_exit(&self) {
|
||||
self.processes.kill_all();
|
||||
self.terminals.kill_all();
|
||||
for project in self.projects() {
|
||||
project.flutter.kill();
|
||||
}
|
||||
}
|
||||
|
||||
/// Called before desktop exit, which may bypass document destructors.
|
||||
@@ -480,6 +486,7 @@ impl Host {
|
||||
let projects: Vec<_> = self.projects.write().drain().map(|(_, p)| p).collect();
|
||||
for project in &projects {
|
||||
project.files_events_active.store(false, std::sync::atomic::Ordering::Release);
|
||||
project.flutter.kill();
|
||||
}
|
||||
for project in &projects {
|
||||
project.plugin_diagnostics.close();
|
||||
|
||||
@@ -11,6 +11,7 @@ mod http;
|
||||
mod page;
|
||||
pub mod reports;
|
||||
mod search;
|
||||
pub mod service;
|
||||
mod symbols;
|
||||
mod tool;
|
||||
mod util;
|
||||
@@ -22,6 +23,7 @@ pub use reports::{
|
||||
FileProblems, FocusedEditor, Reports, mcp_report_problems, mcp_set_focused_editor, mcp_set_open_editors,
|
||||
};
|
||||
pub use run::agent::{AGENT_PARAM, AgentRun, AgentRuns, NAME_PREFIX, SHELL_PARAM};
|
||||
pub use service::{McpServerStatus, McpService};
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::Arc;
|
||||
|
||||
@@ -55,7 +57,7 @@ fn allowed(tool: &ide_mcp::Tool, trusted: bool) -> bool {
|
||||
impl Registry {
|
||||
fn target(&self, root: &Path) -> Result<(String, Arc<crate::Project>), String> {
|
||||
if self.0.mcp_reports.remote_roots().iter().any(|p| p == root) {
|
||||
return Err("not supported for remote projects yet".into());
|
||||
return Err("project is on a remote host; connect to the MCP server on that host".into());
|
||||
}
|
||||
let projects = self.0.projects.read();
|
||||
let open = || projects.iter().filter(|(_, p)| !p.light);
|
||||
|
||||
@@ -0,0 +1,296 @@
|
||||
//! Agents › IDE MCP Server: runs `ide_mcp::McpServer` over built-in host tools
|
||||
//! while `mcpServer.enabled` is on, and reports where it listens.
|
||||
|
||||
use crate::Host;
|
||||
use ide_command::command;
|
||||
use ide_core::{IdeResult, IpcPath};
|
||||
use parking_lot::{Mutex, RwLock};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use specta::Type;
|
||||
use std::net::IpAddr;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::Arc;
|
||||
|
||||
/// The server while it runs, or why it doesn't.
|
||||
#[derive(Default)]
|
||||
pub struct McpService {
|
||||
server: Mutex<Option<ide_mcp::McpServer>>,
|
||||
/// The address, port and token settings the running server was started with.
|
||||
running_with: Mutex<Option<Listen>>,
|
||||
error: Mutex<Option<String>>,
|
||||
/// A link to this executable advertised for `--mcp-stdio` instead of `current_exe()`.
|
||||
stdio_link: RwLock<Option<PathBuf>>,
|
||||
}
|
||||
|
||||
/// What the address, port and token settings ask the server for.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
struct Listen {
|
||||
/// `mcpServer.address`.
|
||||
address: IpAddr,
|
||||
/// `mcpServer.port` with `mcpServer.fixedPort`; otherwise a free port.
|
||||
port: Option<u16>,
|
||||
fixed_token: bool,
|
||||
}
|
||||
|
||||
impl McpService {
|
||||
/// Advertises `link` in `--mcp-stdio` client configs and agent sessions, pointed at this
|
||||
/// executable whenever the server starts. Without one (or where it can't be written),
|
||||
/// `current_exe()` is advertised.
|
||||
pub fn set_stdio_link(&self, link: PathBuf) {
|
||||
*self.stdio_link.write() = Some(link);
|
||||
}
|
||||
|
||||
pub(crate) fn stdio_executable(&self) -> PathBuf {
|
||||
self.stdio_link.read().clone().or_else(|| std::env::current_exe().ok()).unwrap_or_default()
|
||||
}
|
||||
|
||||
fn update_stdio_link(&self) {
|
||||
let mut link = self.stdio_link.write();
|
||||
if let Some(path) = link.as_deref()
|
||||
&& let Err(e) = point_at_current_exe(path)
|
||||
{
|
||||
tracing::warn!("MCP server: cannot link {} ({e}); advertising this executable", path.display());
|
||||
*link = None;
|
||||
}
|
||||
}
|
||||
|
||||
/// Starts, stops or restarts the server to match the global `mcpServer` settings:
|
||||
/// `enabled`, and the address, port and token it listens with.
|
||||
pub fn apply(&self, host: &Arc<Host>) {
|
||||
let settings = host.global_settings.effective().mcp_server;
|
||||
let mut server = self.server.lock();
|
||||
if !settings.enabled {
|
||||
let was_running = server.take();
|
||||
let stopped = was_running.is_some();
|
||||
if let Some(running) = was_running {
|
||||
running.stop();
|
||||
}
|
||||
*self.running_with.lock() = None;
|
||||
host.set_mcp_executable(None);
|
||||
*self.error.lock() = None;
|
||||
drop(server);
|
||||
if stopped {
|
||||
for project in host.projects() {
|
||||
project.agents.notify_mcp_servers_changed();
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
let Ok(address) = settings.address.trim().parse::<IpAddr>() else {
|
||||
let stopped = server.take().map(|running| running.stop()).is_some();
|
||||
*self.running_with.lock() = None;
|
||||
host.set_mcp_executable(None);
|
||||
*self.error.lock() = Some(format!("the listen address `{}` is not an IP address", settings.address.trim()));
|
||||
drop(server);
|
||||
if stopped {
|
||||
for project in host.projects() {
|
||||
project.agents.notify_mcp_servers_changed();
|
||||
}
|
||||
}
|
||||
return;
|
||||
};
|
||||
let wanted = Listen {
|
||||
address,
|
||||
port: settings.fixed_port.then_some(settings.port).filter(|p| *p != 0),
|
||||
fixed_token: settings.fixed_token,
|
||||
};
|
||||
if server.is_some() && *self.running_with.lock() == Some(wanted) {
|
||||
return;
|
||||
}
|
||||
// Another address, port or token: clients connect anew.
|
||||
if let Some(running) = server.take() {
|
||||
let held = (running.address(), running.port());
|
||||
running.stop();
|
||||
if let Some(port) = port_to_wait_for(held, wanted.port) {
|
||||
wait_until_free(address, port);
|
||||
}
|
||||
}
|
||||
let token = if wanted.fixed_token {
|
||||
match ide_mcp::kept_token(&ide_mcp::token_path(&host.data_dir)) {
|
||||
Ok(token) => Some(token),
|
||||
Err(e) => {
|
||||
tracing::warn!("MCP server: cannot keep the token ({e}); using a new one for this run");
|
||||
None
|
||||
}
|
||||
}
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let discovery = ide_mcp::discovery_path(&host.data_dir);
|
||||
match ide_mcp::McpServer::start_with(
|
||||
Arc::new(crate::mcp::Registry::new(host.clone())),
|
||||
Some(discovery),
|
||||
&host.runtime,
|
||||
ide_mcp::ServerOptions { address: Some(address), port: wanted.port, token },
|
||||
) {
|
||||
Ok(started) => {
|
||||
self.update_stdio_link();
|
||||
host.set_mcp_executable(Some(self.stdio_executable()));
|
||||
*server = Some(started);
|
||||
*self.running_with.lock() = Some(wanted);
|
||||
*self.error.lock() = None;
|
||||
drop(server);
|
||||
for project in host.projects() {
|
||||
project.agents.notify_mcp_servers_changed();
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
tracing::warn!("MCP server: {e}");
|
||||
host.set_mcp_executable(None);
|
||||
*self.running_with.lock() = None;
|
||||
*self.error.lock() = Some(e.to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Stops the server on exit (its discovery file goes with it).
|
||||
pub fn stop(&self, host: &Host) {
|
||||
host.set_mcp_executable(None);
|
||||
if let Some(server) = self.server.lock().take() {
|
||||
server.stop();
|
||||
}
|
||||
*self.running_with.lock() = None;
|
||||
}
|
||||
}
|
||||
|
||||
/// Replaces the symlink `link` with one to this executable.
|
||||
fn point_at_current_exe(link: &std::path::Path) -> std::io::Result<()> {
|
||||
#[cfg(unix)]
|
||||
{
|
||||
let exe = std::env::current_exe()?;
|
||||
let dir = link.parent().ok_or_else(|| std::io::Error::other("no parent directory"))?;
|
||||
std::fs::create_dir_all(dir)?;
|
||||
let tmp = dir.join(format!(".openidea-server.{}", std::process::id()));
|
||||
let _ = std::fs::remove_file(&tmp);
|
||||
std::os::unix::fs::symlink(&exe, &tmp)?;
|
||||
std::fs::rename(&tmp, link).inspect_err(|_| {
|
||||
let _ = std::fs::remove_file(&tmp);
|
||||
})
|
||||
}
|
||||
#[cfg(not(unix))]
|
||||
{
|
||||
let _ = link;
|
||||
Err(std::io::ErrorKind::Unsupported.into())
|
||||
}
|
||||
}
|
||||
|
||||
/// The port a restart listening on `wanted` waits for after stopping the server that listened
|
||||
/// on `held`: only its own, at any address (`127.0.0.1` and `0.0.0.0` overlap). A port another
|
||||
/// program holds (the run fell back to a free one) stays taken, and waiting for it would block
|
||||
/// the window.
|
||||
fn port_to_wait_for(held: (IpAddr, u16), wanted: Option<u16>) -> Option<u16> {
|
||||
wanted.filter(|port| held.1 == *port)
|
||||
}
|
||||
|
||||
/// Waits briefly for a stopped server's port to close, so the restart gets it again.
|
||||
fn wait_until_free(address: IpAddr, port: u16) {
|
||||
for _ in 0..100 {
|
||||
if std::net::TcpListener::bind((address, port)).is_ok() {
|
||||
return;
|
||||
}
|
||||
std::thread::sleep(std::time::Duration::from_millis(20));
|
||||
}
|
||||
}
|
||||
|
||||
/// What Settings › Agents › IDE MCP Server shows.
|
||||
#[derive(Clone, Debug, Serialize, Deserialize, Type)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct McpServerStatus {
|
||||
pub running: bool,
|
||||
/// Why it isn't running though turned on.
|
||||
pub error: Option<String>,
|
||||
/// The address listened on (`0.0.0.0` / `::`: every address).
|
||||
pub address: Option<String>,
|
||||
pub port: Option<u16>,
|
||||
/// Why the fixed port (`mcpServer.port`) isn't the one listened on: it was in use or
|
||||
/// reserved, so a free port was taken for this run.
|
||||
pub port_unavailable: Option<String>,
|
||||
/// The window's project, which the URL and command below are for.
|
||||
pub project: Option<IpcPath>,
|
||||
/// `http://<address>:<port>/mcp?project=<root>`, with loopback for every address.
|
||||
pub url: Option<String>,
|
||||
/// Bearer token of `url` (changes with every run unless `mcpServer.fixedToken` is on).
|
||||
pub token: Option<String>,
|
||||
/// The stdio bridge: this host's executable and its arguments.
|
||||
pub stdio_command: Vec<String>,
|
||||
}
|
||||
|
||||
#[command(trust = outside_restricted)]
|
||||
pub fn mcp_server_status(host: &Host, window: &str) -> IdeResult<McpServerStatus> {
|
||||
let root: Option<PathBuf> = host.project(window).ok().filter(|p| !p.light).map(|p| p.root.clone());
|
||||
let server = host.mcp.server.lock();
|
||||
let exe = host.mcp.stdio_executable();
|
||||
Ok(McpServerStatus {
|
||||
running: server.is_some(),
|
||||
error: host.mcp.error.lock().clone(),
|
||||
address: server.as_ref().map(|s| s.address().to_string()),
|
||||
port: server.as_ref().map(|s| s.port()),
|
||||
port_unavailable: server.as_ref().and_then(|s| s.port_unavailable().map(str::to_owned)),
|
||||
url: server.as_ref().zip(root.as_ref()).map(|(s, r)| s.url(r)),
|
||||
token: server.as_ref().map(|s| s.token().to_owned()),
|
||||
stdio_command: root.as_ref().map(|r| ide_mcp::stdio_command(&exe, r)).unwrap_or_default(),
|
||||
project: root.map(IpcPath::from),
|
||||
})
|
||||
}
|
||||
|
||||
/// Settings › Agents › IDE MCP Server › Regenerate Token: forgets the kept token, and restarts the
|
||||
/// server with a new one. Clients configured with the old token stop working.
|
||||
#[command(trust = outside_restricted)]
|
||||
pub fn mcp_regenerate_token(host: &Arc<Host>) -> IdeResult<()> {
|
||||
match std::fs::remove_file(ide_mcp::token_path(&host.data_dir)) {
|
||||
Ok(()) => {}
|
||||
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {}
|
||||
Err(e) => return Err(e.into()),
|
||||
}
|
||||
*host.mcp.running_with.lock() = None;
|
||||
host.mcp.apply(host);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[command(trust = trusted)]
|
||||
pub fn mcp_ui_ready(host: &Host, window: &str, ready: bool) -> IdeResult<()> {
|
||||
let window = host.owner_window(window);
|
||||
host.project(window)?;
|
||||
host.mcp_requests.set_ready(window, ready);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// The user closed the Run tool window tab of an agent command (`run_command`).
|
||||
#[command(trust = trusted)]
|
||||
pub fn mcp_agent_run_closed(host: &Host, window: &str, name: String) -> IdeResult<()> {
|
||||
let project = host.project(window)?;
|
||||
host.mcp_agent_runs.closed(&project.root, &name);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[command(trust = trusted)]
|
||||
pub fn mcp_respond(host: &Host, window: &str, id: u32, allow: bool, error: Option<String>) -> IdeResult<()> {
|
||||
host.mcp_requests.answer(host.owner_window(window), id, allow, error)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::net::Ipv4Addr;
|
||||
|
||||
#[test]
|
||||
fn a_restart_waits_only_for_the_port_the_stopped_server_held() {
|
||||
let local = IpAddr::V4(Ipv4Addr::LOCALHOST);
|
||||
let any = IpAddr::V4(Ipv4Addr::UNSPECIFIED);
|
||||
assert_eq!(port_to_wait_for((local, 64342), Some(64342)), Some(64342));
|
||||
// Another address on the same port overlaps the old listener: wait for it too.
|
||||
assert_eq!(port_to_wait_for((any, 64342), Some(64342)), Some(64342));
|
||||
// The fixed port was taken by another program: the server ran on a free one.
|
||||
assert_eq!(port_to_wait_for((local, 51234), Some(64342)), None);
|
||||
assert_eq!(port_to_wait_for((local, 64342), None), None);
|
||||
}
|
||||
}
|
||||
|
||||
ide_command::domain!(
|
||||
mcp:
|
||||
mcp_agent_run_closed,
|
||||
mcp_regenerate_token,
|
||||
mcp_respond,
|
||||
mcp_server_status,
|
||||
mcp_ui_ready,
|
||||
);
|
||||
@@ -24,6 +24,34 @@ pub struct StatementOutcome {
|
||||
pub error: Option<String>,
|
||||
}
|
||||
|
||||
#[command(trust = trusted)]
|
||||
pub fn db_approve_jdbc_source(host: &Host, window: &str, source: DataSource) -> IdeResult<()> {
|
||||
host.project(window)?.db.approve_jdbc_source(&source)
|
||||
}
|
||||
|
||||
#[command(trust = trusted)]
|
||||
pub async fn db_jdbc_driver_classes(host: &Host, window: &str, jars: Vec<String>) -> IdeResult<Vec<String>> {
|
||||
host.project(window)?;
|
||||
tokio::task::spawn_blocking(move || ide_db::jdbc_driver_classes(&jars))
|
||||
.await
|
||||
.map_err(|e| IdeError::internal(e.to_string()))?
|
||||
}
|
||||
|
||||
#[command(trust = trusted)]
|
||||
pub fn db_jdbc_drivers(host: &Host, window: &str) -> IdeResult<Vec<ide_db::DbJdbcDriver>> {
|
||||
host.project(window)?.db.jdbc_drivers()
|
||||
}
|
||||
|
||||
#[command(trust = trusted)]
|
||||
pub fn db_save_jdbc_driver(host: &Host, window: &str, driver: ide_db::DbJdbcDriver) -> IdeResult<()> {
|
||||
host.project(window)?.db.save_jdbc_driver(driver)
|
||||
}
|
||||
|
||||
#[command(trust = trusted)]
|
||||
pub fn db_remove_jdbc_driver(host: &Host, window: &str, id: String) -> IdeResult<()> {
|
||||
host.project(window)?.db.remove_jdbc_driver(&id)
|
||||
}
|
||||
|
||||
#[command(trust = trusted)]
|
||||
pub fn db_sources(host: &Host, window: &str) -> IdeResult<Vec<DataSource>> {
|
||||
// The data sources configured for the project (the tree's roots).
|
||||
@@ -514,6 +542,11 @@ ide_command::domain!(
|
||||
db_explain,
|
||||
db_finish_transaction,
|
||||
db_history,
|
||||
db_jdbc_driver_classes,
|
||||
db_jdbc_drivers,
|
||||
db_approve_jdbc_source,
|
||||
db_save_jdbc_driver,
|
||||
db_remove_jdbc_driver,
|
||||
db_object_ddl,
|
||||
db_remove_history,
|
||||
db_retain_catalog,
|
||||
|
||||
@@ -357,8 +357,8 @@ pub async fn debug_send_input(host: &Host, session: DebugSessionId, text: String
|
||||
host.debugger.send_input(session, &text)
|
||||
}
|
||||
|
||||
pub async fn debug_hot_reload(host: &Host, session: DebugSessionId) -> IdeResult<()> {
|
||||
host.debugger.hot_reload(session).await
|
||||
pub async fn debug_hot_reload(host: &Host, session: DebugSessionId, reason: HotReloadReason) -> IdeResult<()> {
|
||||
host.debugger.hot_reload(session, reason).await
|
||||
}
|
||||
|
||||
pub async fn debug_hot_restart(host: &Host, session: DebugSessionId) -> IdeResult<()> {
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
//! Flutter's devices. The daemon runs on the project host, where `flutter run` and the debug
|
||||
//! adapter run too, so a remote project lists the devices of its remote machine.
|
||||
use crate::Host;
|
||||
use ide_command::command;
|
||||
use ide_core::IdeResult;
|
||||
use ide_core::event::FlutterDeviceList;
|
||||
|
||||
/// The project's Flutter devices, starting its `flutter daemon` on first use. Changes arrive as
|
||||
/// `IdeEvent::FlutterDevices`.
|
||||
#[command(trust = trusted)]
|
||||
pub fn flutter_devices(host: &Host, window: &str) -> IdeResult<FlutterDeviceList> {
|
||||
Ok(host.project(window)?.flutter.devices())
|
||||
}
|
||||
|
||||
/// Looks at adb again, for Android devices Flutter doesn't report (awaiting authorization, offline).
|
||||
#[command(trust = trusted)]
|
||||
pub fn flutter_devices_refresh(host: &Host, window: &str) -> IdeResult<()> {
|
||||
host.project(window)?.flutter.refresh();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Replaces the project's `flutter daemon`, after a failure or an SDK change.
|
||||
#[command(trust = trusted)]
|
||||
pub fn flutter_daemon_restart(host: &Host, window: &str) -> IdeResult<FlutterDeviceList> {
|
||||
Ok(host.project(window)?.flutter.restart())
|
||||
}
|
||||
|
||||
ide_command::domain!(flutter: flutter_devices, flutter_devices_refresh, flutter_daemon_restart);
|
||||
@@ -15,6 +15,7 @@ pub mod deploy;
|
||||
pub mod doc;
|
||||
pub mod docker;
|
||||
pub mod env;
|
||||
pub mod flutter;
|
||||
pub mod fs;
|
||||
pub mod heap;
|
||||
pub mod history;
|
||||
@@ -85,12 +86,14 @@ ide_command::domains!(
|
||||
doc_commands: crate::ops::doc::commands,
|
||||
docker: crate::ops::docker,
|
||||
env: crate::ops::env,
|
||||
flutter: crate::ops::flutter,
|
||||
fs: crate::ops::fs,
|
||||
heap: crate::ops::heap,
|
||||
history: crate::ops::history,
|
||||
java: crate::ops::java,
|
||||
jvm_icons: crate::ops::jvm_icons,
|
||||
lsp: crate::ops::lsp,
|
||||
mcp: crate::mcp::service,
|
||||
mcp_reports: crate::mcp::reports,
|
||||
nav: crate::ops::nav,
|
||||
notebook: crate::ops::notebook,
|
||||
|
||||
@@ -80,6 +80,13 @@ pub fn settings_get(host: &Host, window: &str) -> Settings {
|
||||
})
|
||||
}
|
||||
|
||||
/// Defaults plus the global layer, without any project's values: what new
|
||||
/// projects (and projects without their own value) get.
|
||||
#[command(trust = always)]
|
||||
pub fn settings_get_global(host: &Host) -> Settings {
|
||||
host.with_proxy_password(host.global_settings.effective())
|
||||
}
|
||||
|
||||
/// The removed built-in theme id a settings file of this window still names, if any:
|
||||
/// `settings_get` reports the built-in that stands in for it, so the notice about
|
||||
/// a theme that moved to a plugin can address the installs it concerns.
|
||||
@@ -96,13 +103,16 @@ pub fn settings_removed_theme(host: &Host, window: &str) -> Option<String> {
|
||||
/// re-reads its effective settings.
|
||||
#[command(trust = always, desktop_body = sync)]
|
||||
pub fn settings_set(
|
||||
host: &Host,
|
||||
host: &Arc<Host>,
|
||||
window: &str,
|
||||
scope: SettingsScope,
|
||||
key: String,
|
||||
value: String,
|
||||
) -> IdeResult<Settings> {
|
||||
let applied = set_settings(host, window, scope, &key, &value)?;
|
||||
if key == "mcpServer" || key.starts_with("mcpServer.") {
|
||||
host.mcp.apply(host);
|
||||
}
|
||||
let sink = host.window_sink(window);
|
||||
sink.emit(IdeEvent::SettingsChanged { key });
|
||||
if let Some(root) = applied.roots_changed {
|
||||
@@ -386,6 +396,7 @@ ide_command::domain!(
|
||||
project_trust_set,
|
||||
project_trust_status,
|
||||
settings_get,
|
||||
settings_get_global,
|
||||
settings_removed_theme,
|
||||
settings_set,
|
||||
);
|
||||
@@ -17,16 +17,23 @@ fn tools(host: &Host, window: &str) -> IdeResult<Tools> {
|
||||
Ok(project) => (project.settings.effective(), project.root.clone()),
|
||||
Err(_) => (host.global_settings.effective(), Default::default()),
|
||||
};
|
||||
let custom = settings
|
||||
let mut custom: std::collections::BTreeMap<_, _> = settings
|
||||
.tools
|
||||
.iter()
|
||||
.filter_map(|(command, t)| Some((command.clone(), ide_sdk::apply::setting_path(&root, &t.path)?)))
|
||||
.collect();
|
||||
let langs = host.languages.all();
|
||||
custom.extend(ide_sdk::apply::server_command_paths(
|
||||
langs.iter().flat_map(|l| &l.language_servers),
|
||||
&root,
|
||||
&settings,
|
||||
));
|
||||
// Godot is usually an app bundle / Flatpak outside the search path.
|
||||
let found = ide_sdk::godot::find(None).map(|p| (ide_sdk::godot::COMMAND.to_owned(), p));
|
||||
let runtime = ide_sdk::apply::tool_install_environment(&root, &settings);
|
||||
let work_dir = (!root.as_os_str().is_empty()).then_some(root);
|
||||
Ok(Tools::new(&data)
|
||||
.with_downloads(settings.tool_downloads.clone())
|
||||
.with_runtime(runtime.commands, runtime.env)
|
||||
.with_custom_paths(custom)
|
||||
.with_found_paths(found.into_iter().collect())
|
||||
|
||||
@@ -61,6 +61,8 @@ pub struct Project {
|
||||
pub agents: Arc<ide_agent::AgentManager>,
|
||||
/// Native notebooks and owned kernels, scoped to this project incarnation.
|
||||
pub notebooks: Arc<crate::notebook::NotebookService>,
|
||||
/// Flutter's devices; its daemon starts when the UI first asks for them.
|
||||
pub(crate) flutter: ide_flutter::FlutterDaemon,
|
||||
pub(crate) kotlin_bridge: Option<Arc<crate::kotlin_bridge::KotlinBridge>>,
|
||||
pub(crate) phpstan: Option<Arc<crate::phpstan::Phpstan>>,
|
||||
pub(crate) watcher: Arc<Watcher>,
|
||||
@@ -601,6 +603,7 @@ impl Host {
|
||||
let project = Arc::new(Project {
|
||||
plugin_diagnostics,
|
||||
trusted,
|
||||
flutter: ide_flutter::FlutterDaemon::new(root.clone(), sink.clone()),
|
||||
root,
|
||||
sink,
|
||||
languages,
|
||||
@@ -731,6 +734,10 @@ impl Host {
|
||||
agents.set_mcp_servers_hook(Box::new(move || {
|
||||
crate::ops::agent::session_mcp_servers(&settings, &approvals, &secrets, &root)
|
||||
}));
|
||||
{
|
||||
let mcp_executable = self.mcp_executable.clone();
|
||||
agents.set_mcp_executable_hook(Box::new(move || mcp_executable.read().clone()));
|
||||
}
|
||||
(db, http_history, agents)
|
||||
}
|
||||
}
|
||||
@@ -809,6 +816,7 @@ impl Host {
|
||||
let project = Arc::new(Project {
|
||||
plugin_diagnostics,
|
||||
trusted: Arc::new(AtomicBool::new(false)),
|
||||
flutter: ide_flutter::FlutterDaemon::new(dir.clone(), sink.clone()),
|
||||
sink,
|
||||
languages,
|
||||
settings,
|
||||
|
||||
@@ -171,20 +171,21 @@ pub fn event_sink(peer: Arc<OnceLock<Peer>>, window: Option<String>) -> ide_core
|
||||
}
|
||||
|
||||
/// Whether a desktop shows an `event` notification of its server in the window. A server reports
|
||||
/// on its project; what this machine answers or owns itself (MCP approvals, app updates, window
|
||||
/// groups, its own connection to the server) never comes from it.
|
||||
pub fn client_accepts(event: &IdeEvent) -> bool {
|
||||
/// on its project; what this machine owns itself (app updates, window groups, its own connection
|
||||
/// to the server) never comes from it, and MCP UI requests are accepted only when this machine
|
||||
/// trusts the remote project (their answers route back to the server, never locally).
|
||||
pub fn client_accepts(event: &IdeEvent, trusted: bool) -> bool {
|
||||
match event {
|
||||
IdeEvent::McpRequest { .. }
|
||||
| IdeEvent::McpRequestClosed { .. }
|
||||
| IdeEvent::WindowGroupsChanged
|
||||
IdeEvent::WindowGroupsChanged
|
||||
| IdeEvent::WindowFullscreenChanged
|
||||
| IdeEvent::RemoteConnectionLost { .. }
|
||||
| IdeEvent::UpdateStatusChanged { .. }
|
||||
| IdeEvent::UpdateAvailable { .. } => false,
|
||||
IdeEvent::McpRequest { .. } | IdeEvent::McpRequestClosed { .. } => trusted,
|
||||
IdeEvent::DbSourcesChanged
|
||||
| IdeEvent::ProjectTrustChanged { .. }
|
||||
| IdeEvent::Direnv { .. }
|
||||
| IdeEvent::FlutterDevices { .. }
|
||||
| IdeEvent::FsChanged { .. }
|
||||
| IdeEvent::ExcludedDirsChanged { .. }
|
||||
| IdeEvent::DocPatched { .. }
|
||||
@@ -301,8 +302,21 @@ mod tests {
|
||||
date: None,
|
||||
};
|
||||
for event in [
|
||||
IdeEvent::McpRequest { id: 1, request: confirm },
|
||||
IdeEvent::McpRequest { id: 1, request: confirm.clone() },
|
||||
IdeEvent::McpRequestClosed { id: 1 },
|
||||
IdeEvent::UpdateAvailable { update: update.clone() },
|
||||
IdeEvent::UpdateStatusChanged {
|
||||
status: ide_core::updates::UpdateStatus { last_checked: None, checking: true },
|
||||
},
|
||||
IdeEvent::WindowGroupsChanged,
|
||||
IdeEvent::WindowFullscreenChanged,
|
||||
] {
|
||||
assert!(!client_accepts(&event, false), "{event:?}");
|
||||
}
|
||||
// Even for a trusted remote project, desktop-owned events stay blocked while remote MCP prompts pass.
|
||||
assert!(client_accepts(&IdeEvent::McpRequest { id: 1, request: confirm }, true));
|
||||
assert!(client_accepts(&IdeEvent::McpRequestClosed { id: 1 }, true));
|
||||
for event in [
|
||||
IdeEvent::UpdateAvailable { update },
|
||||
IdeEvent::UpdateStatusChanged {
|
||||
status: ide_core::updates::UpdateStatus { last_checked: None, checking: true },
|
||||
@@ -310,7 +324,7 @@ mod tests {
|
||||
IdeEvent::WindowGroupsChanged,
|
||||
IdeEvent::WindowFullscreenChanged,
|
||||
] {
|
||||
assert!(!client_accepts(&event), "{event:?}");
|
||||
assert!(!client_accepts(&event, true), "{event:?}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -328,7 +342,8 @@ mod tests {
|
||||
IdeEvent::Askpass { id: 1, prompt: "Password".into(), kind: AskpassKind::Secret, remember: false },
|
||||
IdeEvent::ProjectTrustChanged { root: "/srv/app".into(), trusted: true },
|
||||
] {
|
||||
assert!(client_accepts(&event), "{event:?}");
|
||||
assert!(client_accepts(&event, false), "{event:?}");
|
||||
assert!(client_accepts(&event, true), "{event:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -282,6 +282,7 @@ impl crate::Host {
|
||||
self.environment.project_closed();
|
||||
self.environment.direnv_closed(&project.root);
|
||||
project.agents.shutdown();
|
||||
project.flutter.stop();
|
||||
self.processes.kill_project(&project.root);
|
||||
project.notebooks.restrict().await;
|
||||
let windows: Vec<_> = self
|
||||
|
||||
@@ -185,7 +185,10 @@ async fn restricted_projects_have_only_read_tools_and_remote_projects_explain_th
|
||||
assert!(registry.tools(&h.root).unwrap().iter().all(|t| t.read_only && !t.destructive));
|
||||
assert!(!h.host.global_settings.effective().mcp_server.enabled);
|
||||
h.host.mcp_reports.set_remote("remote", PathBuf::from("/remote/project"));
|
||||
assert_eq!(registry.tools(Path::new("/remote/project")).unwrap_err(), "not supported for remote projects yet");
|
||||
assert_eq!(
|
||||
registry.tools(Path::new("/remote/project")).unwrap_err(),
|
||||
"project is on a remote host; connect to the MCP server on that host"
|
||||
);
|
||||
h.host.close_project_detached("remote");
|
||||
assert!(!registry.projects().contains(&PathBuf::from("/remote/project")));
|
||||
}
|
||||
|
||||
@@ -471,7 +471,8 @@ async fn run_target_validation_gates_launches_and_preserves_selection() {
|
||||
f._tmp.path().join("plugins"),
|
||||
Arc::new(|_: Option<&str>, _| {}),
|
||||
Limits {
|
||||
timeout: Duration::from_millis(200),
|
||||
// Room for a reclaimed instance to start again on a slow runner.
|
||||
timeout: Duration::from_millis(750),
|
||||
idle_timeout: Some(Duration::from_millis(20)),
|
||||
restart_backoff: Duration::ZERO,
|
||||
..Default::default()
|
||||
|
||||
@@ -1560,7 +1560,11 @@ fn edit_path_calls_have_soft_and_hard_deadlines() {
|
||||
// Past the hard deadline: interrupted; the next call gets a new instance.
|
||||
assert!(provider.highlights(&mis, "spin").is_none());
|
||||
std::thread::sleep(Duration::from_millis(450));
|
||||
host.tick();
|
||||
// A slow runner may interrupt the spinning call a little after its deadline.
|
||||
assert!(eventually(|| {
|
||||
host.tick();
|
||||
host.info_of("misbehave").unwrap().stats.crashes > 0
|
||||
}));
|
||||
let info = host.info_of("misbehave").unwrap();
|
||||
assert_eq!(info.stats.crashes, 1, "{:?}", info.error);
|
||||
assert!(info.error.as_deref().is_some_and(|e| e.contains("timed out after 400ms")), "{:?}", info.error);
|
||||
|
||||
@@ -522,3 +522,131 @@ async fn renames_a_branch_with_the_clients_argument_names() {
|
||||
invoke(&peer, "close_project", json!({})).await.unwrap();
|
||||
peer.kill();
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
|
||||
async fn serves_builtin_mcp_over_remote_dispatch_and_routes_ui_requests() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let root = tmp.path().join("remote-mcp");
|
||||
std::fs::create_dir_all(root.join("src")).unwrap();
|
||||
std::fs::write(root.join("src/hello.txt"), "from remote mcp\n").unwrap();
|
||||
let root = dunce::canonicalize(root).unwrap();
|
||||
let base = tmp.path().join("server");
|
||||
let data_dir = base.join("data");
|
||||
|
||||
let peer_slot = Arc::new(OnceLock::new());
|
||||
let sink_peer = peer_slot.clone();
|
||||
let host = Arc::new(Host::new(HostConfig {
|
||||
config_dir: base.join("config"),
|
||||
data_dir: data_dir.clone(),
|
||||
local_data_dir: data_dir.clone(),
|
||||
languages: Arc::new(ide_language::LanguageRegistry::load(None).unwrap()),
|
||||
syntax: Arc::default(),
|
||||
sink_for: Box::new(move |w| rpc::event_sink(sink_peer.clone(), Some(w.to_owned()))),
|
||||
owner: |w| w,
|
||||
runtime: tokio::runtime::Handle::current(),
|
||||
project_environment: false,
|
||||
}));
|
||||
let server_exe = base.join("bin/openidea-server");
|
||||
host.mcp.set_stdio_link(server_exe.clone());
|
||||
host.mcp.apply(&host);
|
||||
|
||||
let (client_io, server_io) = tokio::io::duplex(1 << 20);
|
||||
let (sr, sw) = tokio::io::split(server_io);
|
||||
let serve_host = host.clone();
|
||||
tokio::spawn(rpc::serve(serve_host, Dispatcher::standard(), peer_slot, sr, sw));
|
||||
let (cr, cw) = tokio::io::split(client_io);
|
||||
let client = Arc::new(Client::default());
|
||||
let peer = Peer::connect(cr, cw, client.clone());
|
||||
|
||||
invoke(&peer, "open_project", json!({ "path": root })).await.unwrap();
|
||||
for cmd in ["mcp_server_status", "mcp_regenerate_token", "mcp_ui_ready", "mcp_respond", "mcp_agent_run_closed"] {
|
||||
let err = invoke(&peer, cmd, json!({})).await.unwrap_err();
|
||||
assert!(err.message.contains("Restricted Mode"), "{cmd}: {err:?}");
|
||||
}
|
||||
|
||||
invoke(&peer, "project_trust_set", json!({ "path": root, "trusted": true, "folder": false })).await.unwrap();
|
||||
let initial = invoke(&peer, "mcp_server_status", json!({})).await.unwrap();
|
||||
assert_eq!(initial["running"], false);
|
||||
assert!(!server_exe.exists(), "the stdio link is written only once the server runs");
|
||||
|
||||
invoke(&peer, "settings_set", json!({ "scope": "global", "key": "mcpServer.fixedPort", "value": "false" }))
|
||||
.await
|
||||
.unwrap();
|
||||
invoke(&peer, "settings_set", json!({ "scope": "global", "key": "mcpServer.fixedToken", "value": "true" }))
|
||||
.await
|
||||
.unwrap();
|
||||
invoke(&peer, "settings_set", json!({ "scope": "global", "key": "mcpServer.enabled", "value": "true" }))
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let global = invoke(&peer, "settings_get_global", json!({})).await.unwrap();
|
||||
assert_eq!(global["mcpServer"]["enabled"], true);
|
||||
|
||||
let status = invoke(&peer, "mcp_server_status", json!({})).await.unwrap();
|
||||
assert_eq!(status["running"], true);
|
||||
assert_eq!(status["project"], json!(ide_core::IpcPath::from(&root)));
|
||||
#[cfg(unix)]
|
||||
{
|
||||
assert_eq!(status["stdioCommand"][0], server_exe.to_string_lossy().as_ref());
|
||||
assert_eq!(std::fs::read_link(&server_exe).unwrap(), std::env::current_exe().unwrap());
|
||||
}
|
||||
let first_token = status["token"].as_str().unwrap().to_owned();
|
||||
assert!(status["url"].as_str().unwrap().starts_with("http://127.0.0.1:"));
|
||||
|
||||
invoke(&peer, "mcp_ui_ready", json!({ "ready": true })).await.unwrap();
|
||||
let discovery = ide_mcp::discovery_path(&data_dir);
|
||||
|
||||
let read_call = json!({
|
||||
"jsonrpc": "2.0",
|
||||
"id": 1,
|
||||
"method": "tools/call",
|
||||
"params": { "name": "read_file", "arguments": { "file_path": "src/hello.txt" } }
|
||||
});
|
||||
let mut out = Vec::new();
|
||||
ide_mcp::stdio::bridge(&discovery, &root, format!("{read_call}\n").as_bytes(), &mut out).await.unwrap();
|
||||
let reply: Value = serde_json::from_slice(&out).unwrap();
|
||||
assert_eq!(reply["result"]["content"][0]["text"], "L1: from remote mcp\nL2: ");
|
||||
|
||||
// UI-bound MCP requests stream over RPC events and are answered via routed `mcp_respond`.
|
||||
let open_call = json!({
|
||||
"jsonrpc": "2.0",
|
||||
"id": 2,
|
||||
"method": "tools/call",
|
||||
"params": { "name": "open_file_in_editor", "arguments": { "filePath": "src/hello.txt", "line": 1, "column": 1 } }
|
||||
});
|
||||
let (disc2, root2) = (discovery.clone(), root.clone());
|
||||
let call_task = tokio::spawn(async move {
|
||||
let mut out = Vec::new();
|
||||
ide_mcp::stdio::bridge(&disc2, &root2, format!("{open_call}\n").as_bytes(), &mut out).await.unwrap();
|
||||
serde_json::from_slice::<Value>(&out).unwrap()
|
||||
});
|
||||
until("remote MCP UI request", || {
|
||||
client.events.lock().iter().any(|m| matches!(m.event, ide_core::IdeEvent::McpRequest { .. }))
|
||||
})
|
||||
.await;
|
||||
let req_id = client
|
||||
.events
|
||||
.lock()
|
||||
.iter()
|
||||
.find_map(|m| match &m.event {
|
||||
event @ ide_core::IdeEvent::McpRequest { id, .. } => {
|
||||
assert!(!rpc::client_accepts(event, false));
|
||||
assert!(rpc::client_accepts(event, true));
|
||||
Some(*id)
|
||||
}
|
||||
_ => None,
|
||||
})
|
||||
.unwrap();
|
||||
invoke(&peer, "mcp_respond", json!({ "id": req_id, "allow": true, "error": null })).await.unwrap();
|
||||
let opened = call_task.await.unwrap();
|
||||
assert_eq!(opened["result"]["isError"], false, "{opened}");
|
||||
|
||||
invoke(&peer, "mcp_regenerate_token", json!({})).await.unwrap();
|
||||
let rotated = invoke(&peer, "mcp_server_status", json!({})).await.unwrap();
|
||||
assert_eq!(rotated["running"], true);
|
||||
assert_ne!(rotated["token"].as_str().unwrap(), first_token);
|
||||
|
||||
host.mcp.stop(&host);
|
||||
invoke(&peer, "close_project", json!({})).await.unwrap();
|
||||
peer.kill();
|
||||
}
|
||||
@@ -690,7 +690,7 @@ mod tests {
|
||||
for (lang, servers) in [
|
||||
("python", vec!["basedpyright", "pyright", "ty", "pyrefly"]),
|
||||
("typescript", vec!["vtsls", "typescript-language-server"]),
|
||||
("java", vec!["jdtls", "kmp-lsp"]),
|
||||
("java", vec!["jdtls", "java-analyzer", "kmp-lsp"]),
|
||||
("kotlin", vec!["kotlin-lsp", "kotlin-language-server", "kmp-lsp"]),
|
||||
] {
|
||||
let cfg = reg.get(&LanguageId::new(lang)).unwrap();
|
||||
|
||||
@@ -61,18 +61,96 @@ impl Discovery {
|
||||
serde_json::from_slice(&bytes).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))
|
||||
}
|
||||
|
||||
/// Writes the file atomically, readable only by the user.
|
||||
/// Writes the file atomically, readable only by the user, and records a per-server
|
||||
/// entry alongside it so multiple `openidea-server` processes sharing one data
|
||||
/// directory remain discoverable by project.
|
||||
pub fn write(&self, path: &Path) -> io::Result<()> {
|
||||
let bytes = serde_json::to_vec_pretty(self).map_err(io::Error::other)?;
|
||||
write_atomic(path, &bytes)
|
||||
write_atomic(path, &bytes)?;
|
||||
if let Some(entry) = server_entry_path(path, self.pid, self.port) {
|
||||
let _ = write_atomic(&entry, &bytes);
|
||||
}
|
||||
// Entries of servers that ended without removing theirs (a crash) go now, so bridges
|
||||
// stop trying them.
|
||||
for (file, other) in sibling_entry_files(path) {
|
||||
if (other.pid, other.port) != (self.pid, self.port) && !other.listening() {
|
||||
let _ = std::fs::remove_file(file);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn listening(&self) -> bool {
|
||||
let addr = std::net::SocketAddr::new(self.address, self.port);
|
||||
std::net::TcpStream::connect_timeout(&addr, std::time::Duration::from_millis(50)).is_ok()
|
||||
}
|
||||
|
||||
/// Removes the file at `path` if it still describes this process's server.
|
||||
pub fn remove_if_ours(path: &Path) {
|
||||
if Self::read(path).is_ok_and(|d| d.pid == std::process::id()) {
|
||||
if let Ok(d) = Self::read(path)
|
||||
&& d.pid == std::process::id()
|
||||
{
|
||||
Self::remove_server(path, d.port);
|
||||
}
|
||||
}
|
||||
|
||||
/// Removes this server's entry and updates `path` to another live entry when one exists.
|
||||
pub fn remove_server(path: &Path, port: u16) {
|
||||
let pid = std::process::id();
|
||||
if let Some(entry) = server_entry_path(path, pid, port) {
|
||||
let _ = std::fs::remove_file(entry);
|
||||
}
|
||||
if Self::read(path).is_ok_and(|d| d.pid == pid && d.port == port) {
|
||||
for (file, other) in sibling_entry_files(path) {
|
||||
if other.pid == pid && other.port == port {
|
||||
continue;
|
||||
}
|
||||
if other.listening() {
|
||||
if let Ok(bytes) = serde_json::to_vec_pretty(&other)
|
||||
&& write_atomic(path, &bytes).is_ok()
|
||||
{
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
let _ = std::fs::remove_file(file);
|
||||
}
|
||||
}
|
||||
let _ = std::fs::remove_file(path);
|
||||
}
|
||||
}
|
||||
|
||||
/// Running server entries recorded for `path`, starting with `path` itself.
|
||||
pub fn candidates(path: &Path) -> Vec<Self> {
|
||||
let mut out = Vec::new();
|
||||
if let Ok(primary) = Self::read(path) {
|
||||
out.push(primary);
|
||||
}
|
||||
for (_, item) in sibling_entry_files(path) {
|
||||
if !out.iter().any(|d| d.address == item.address && d.port == item.port) {
|
||||
out.push(item);
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
}
|
||||
|
||||
fn servers_dir(path: &Path) -> Option<PathBuf> {
|
||||
path.parent().map(|dir| dir.join("servers"))
|
||||
}
|
||||
|
||||
fn server_entry_path(path: &Path, pid: u32, port: u16) -> Option<PathBuf> {
|
||||
servers_dir(path).map(|dir| dir.join(format!("{pid}-{port}.json")))
|
||||
}
|
||||
|
||||
fn sibling_entry_files(path: &Path) -> Vec<(PathBuf, Discovery)> {
|
||||
let Some(dir) = servers_dir(path) else { return Vec::new() };
|
||||
let Ok(entries) = std::fs::read_dir(dir) else { return Vec::new() };
|
||||
let mut files: Vec<PathBuf> = entries
|
||||
.filter_map(|e| e.ok().map(|e| e.path()))
|
||||
.filter(|p| p.extension().is_some_and(|ext| ext == "json"))
|
||||
.collect();
|
||||
files.sort();
|
||||
files.into_iter().filter_map(|p| Discovery::read(&p).ok().map(|d| (p, d))).collect()
|
||||
}
|
||||
|
||||
/// Writes `bytes` to `path` atomically, readable only by the user.
|
||||
|
||||
@@ -185,7 +185,7 @@ impl Drop for McpServer {
|
||||
self.cancel.cancel();
|
||||
let _ = self.shutdown.send(true);
|
||||
if let Some(path) = &self.discovery {
|
||||
Discovery::remove_if_ours(path);
|
||||
Discovery::remove_server(path, self.port);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,9 +9,19 @@ use bytes::Bytes;
|
||||
use http_body_util::{BodyExt, Full};
|
||||
use hyper::{Request, StatusCode, header};
|
||||
use serde_json::Value;
|
||||
use std::collections::HashSet;
|
||||
use std::net::SocketAddr;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::{Arc, Mutex};
|
||||
use tokio::io::{AsyncBufRead, AsyncBufReadExt, AsyncWrite, AsyncWriteExt};
|
||||
|
||||
#[derive(Default)]
|
||||
struct BridgeState {
|
||||
preferred: Option<SocketAddr>,
|
||||
init_message: Option<Value>,
|
||||
initialized_on: HashSet<SocketAddr>,
|
||||
}
|
||||
|
||||
/// Runs the bridge on this process's stdin and stdout until stdin closes.
|
||||
/// `project` defaults to the current directory. Returns the exit code.
|
||||
pub fn run(discovery: &Path, project: Option<PathBuf>) -> i32 {
|
||||
@@ -43,6 +53,7 @@ where
|
||||
W: AsyncWrite + Unpin,
|
||||
{
|
||||
let scope = crate::server::new_token()?;
|
||||
let state = Arc::new(Mutex::new(BridgeState::default()));
|
||||
let mut lines = input.lines();
|
||||
let mut pending = tokio::task::JoinSet::new();
|
||||
let (sent, mut received) = tokio::sync::mpsc::unbounded_channel::<Value>();
|
||||
@@ -57,10 +68,13 @@ where
|
||||
if line.trim().is_empty() { continue; }
|
||||
match serde_json::from_str::<Value>(&line) {
|
||||
Ok(message) => {
|
||||
let (discovery, project, scope, sent) =
|
||||
(discovery.to_owned(), project.to_owned(), scope.clone(), sent.clone());
|
||||
if is_initialize(&message) {
|
||||
state.lock().unwrap().init_message = Some(message.clone());
|
||||
}
|
||||
let (discovery, project, scope, sent, state) =
|
||||
(discovery.to_owned(), project.to_owned(), scope.clone(), sent.clone(), state.clone());
|
||||
pending.spawn(async move {
|
||||
if let Some(reply) = relay(&discovery, &project, &message, &scope, &sent).await {
|
||||
if let Some(reply) = relay(&discovery, &project, &message, &scope, &sent, &state).await {
|
||||
let _ = sent.send(reply);
|
||||
}
|
||||
});
|
||||
@@ -86,6 +100,10 @@ where
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn is_initialize(message: &Value) -> bool {
|
||||
message.get("method").and_then(Value::as_str) == Some("initialize")
|
||||
}
|
||||
|
||||
/// The IDE's answer to `message`, or an error response for each request in
|
||||
/// it when the IDE can't be reached. An answer on an event stream goes to `sent`
|
||||
/// with the messages before it.
|
||||
@@ -95,8 +113,9 @@ async fn relay(
|
||||
message: &Value,
|
||||
scope: &str,
|
||||
sent: &tokio::sync::mpsc::UnboundedSender<Value>,
|
||||
state: &Mutex<BridgeState>,
|
||||
) -> Option<Value> {
|
||||
let failure = match post(discovery, project, message, scope, sent).await {
|
||||
let failure = match post(discovery, project, message, scope, sent, state).await {
|
||||
Ok(Answer::Streamed { answered: true }) => return None,
|
||||
Ok(Answer::Streamed { answered: false }) => "OpenIDEA's answer ended early".to_owned(),
|
||||
Ok(Answer::Body(StatusCode::ACCEPTED, _)) => return None,
|
||||
@@ -132,14 +151,101 @@ enum Answer {
|
||||
},
|
||||
}
|
||||
|
||||
/// POSTs `message` to the server in the discovery file; a message for the
|
||||
/// user when that fails.
|
||||
/// POSTs `message` to the server in the discovery file; when several servers share a data
|
||||
/// directory (multiple remote windows on one host), picks the one where `project` is open.
|
||||
async fn post(
|
||||
discovery: &Path,
|
||||
project: &Path,
|
||||
message: &Value,
|
||||
scope: &str,
|
||||
sent: &tokio::sync::mpsc::UnboundedSender<Value>,
|
||||
state: &Mutex<BridgeState>,
|
||||
) -> Result<Answer, String> {
|
||||
let not_running = || {
|
||||
format!(
|
||||
"OpenIDEA's MCP server is not running. Start OpenIDEA, open {} in it, and turn on Settings › Agents › MCP Server",
|
||||
project.display()
|
||||
)
|
||||
};
|
||||
let mut candidates = Discovery::candidates(discovery);
|
||||
if candidates.is_empty() {
|
||||
return Err(not_running());
|
||||
}
|
||||
if let Some(preferred) = state.lock().unwrap().preferred
|
||||
&& let Some(pos) = candidates.iter().position(|c| SocketAddr::new(c.address, c.port) == preferred)
|
||||
{
|
||||
let chosen = candidates.remove(pos);
|
||||
candidates.insert(0, chosen);
|
||||
}
|
||||
let initializing = is_initialize(message);
|
||||
let mut fallback: Option<(SocketAddr, Answer)> = None;
|
||||
let mut last_error: Option<String> = None;
|
||||
let total = candidates.len();
|
||||
for (idx, info) in candidates.into_iter().enumerate() {
|
||||
let at = SocketAddr::new(info.address, info.port);
|
||||
if !initializing {
|
||||
let init = {
|
||||
let guard = state.lock().unwrap();
|
||||
(!guard.initialized_on.contains(&at)).then(|| guard.init_message.clone()).flatten()
|
||||
};
|
||||
if let Some(mut init_msg) = init {
|
||||
if let Some(obj) = init_msg.as_object_mut() {
|
||||
obj.insert("id".into(), Value::String(format!("__openidea_init_{idx}__")));
|
||||
}
|
||||
if post_one(&info, project, &init_msg, scope, sent).await.is_ok() {
|
||||
state.lock().unwrap().initialized_on.insert(at);
|
||||
}
|
||||
}
|
||||
}
|
||||
match post_one(&info, project, message, scope, sent).await {
|
||||
Ok(answer) => {
|
||||
if initializing {
|
||||
state.lock().unwrap().initialized_on.insert(at);
|
||||
if total > 1 && !serves_project(&info, project, scope).await {
|
||||
fallback.get_or_insert((at, answer));
|
||||
continue;
|
||||
}
|
||||
} else if idx + 1 < total && is_not_open_reply(&answer) {
|
||||
fallback = Some((at, answer));
|
||||
continue;
|
||||
}
|
||||
state.lock().unwrap().preferred = Some(at);
|
||||
return Ok(answer);
|
||||
}
|
||||
Err(err) => last_error = Some(err),
|
||||
}
|
||||
}
|
||||
if let Some((at, answer)) = fallback {
|
||||
state.lock().unwrap().preferred = Some(at);
|
||||
return Ok(answer);
|
||||
}
|
||||
Err(last_error.unwrap_or_else(not_running))
|
||||
}
|
||||
|
||||
/// Whether the server of `info` has `project` open: its `tools/list` doesn't answer "not open".
|
||||
async fn serves_project(info: &Discovery, project: &Path, scope: &str) -> bool {
|
||||
let probe = serde_json::json!({ "jsonrpc": "2.0", "id": "__openidea_probe__", "method": "tools/list" });
|
||||
let (discard, _) = tokio::sync::mpsc::unbounded_channel();
|
||||
match post_one(info, project, &probe, scope, &discard).await {
|
||||
Ok(answer @ Answer::Body(StatusCode::OK, _)) => !is_not_open_reply(&answer),
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
fn is_not_open_reply(answer: &Answer) -> bool {
|
||||
let Answer::Body(StatusCode::OK, body) = answer else { return false };
|
||||
serde_json::from_slice::<Value>(body)
|
||||
.ok()
|
||||
.and_then(|v| v.pointer("/error/code").and_then(Value::as_i64))
|
||||
.is_some_and(|code| code == NOT_OPEN)
|
||||
}
|
||||
|
||||
async fn post_one(
|
||||
info: &Discovery,
|
||||
project: &Path,
|
||||
message: &Value,
|
||||
scope: &str,
|
||||
sent: &tokio::sync::mpsc::UnboundedSender<Value>,
|
||||
) -> Result<Answer, String> {
|
||||
let not_running = || {
|
||||
format!(
|
||||
@@ -147,8 +253,7 @@ async fn post(
|
||||
project.display()
|
||||
)
|
||||
};
|
||||
let info = Discovery::read(discovery).map_err(|_| not_running())?;
|
||||
let at = std::net::SocketAddr::new(info.address, info.port);
|
||||
let at = SocketAddr::new(info.address, info.port);
|
||||
let stream = tokio::net::TcpStream::connect(at).await.map_err(|_| not_running())?;
|
||||
let (mut sender, conn) = hyper::client::conn::http1::handshake(hyper_util::rt::TokioIo::new(stream))
|
||||
.await
|
||||
|
||||
@@ -424,3 +424,52 @@ async fn another_listen_address_is_used_and_one_not_on_this_computer_fails() {
|
||||
let error = McpServer::start_with(registry, None, &runtime, elsewhere).err().expect("no such address here");
|
||||
assert!(error.to_string().contains("192.0.2.1"), "{error}");
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn multiple_servers_sharing_app_data_route_by_project_and_survive_one_closing() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let base = dunce::canonicalize(tmp.path()).unwrap();
|
||||
let (proj_a, proj_b) = (base.join("proj-a"), base.join("proj-b"));
|
||||
std::fs::create_dir_all(&proj_a).unwrap();
|
||||
std::fs::create_dir_all(&proj_b).unwrap();
|
||||
let discovery = discovery_path(&base.join("app-data"));
|
||||
let runtime = tokio::runtime::Handle::current();
|
||||
|
||||
let server_a =
|
||||
McpServer::start(Arc::new(Registry { project: proj_a.clone() }), Some(discovery.clone()), &runtime).unwrap();
|
||||
let server_b =
|
||||
McpServer::start(Arc::new(Registry { project: proj_b.clone() }), Some(discovery.clone()), &runtime).unwrap();
|
||||
|
||||
let session = |text: &str| {
|
||||
[
|
||||
json!({ "jsonrpc": "2.0", "id": 1, "method": "initialize", "params": { "protocolVersion": "2025-06-18", "clientInfo": { "name": "agent", "version": "1" } } }),
|
||||
json!({ "jsonrpc": "2.0", "id": 2, "method": "tools/call", "params": { "name": "echo", "arguments": { "text": text } } }),
|
||||
]
|
||||
.iter()
|
||||
.map(|m| format!("{m}\n"))
|
||||
.collect::<String>()
|
||||
};
|
||||
|
||||
for (proj, tag) in [(&proj_a, "alpha"), (&proj_b, "beta")] {
|
||||
let mut out = Vec::new();
|
||||
stdio::bridge(&discovery, proj, session(tag).as_bytes(), &mut out).await.unwrap();
|
||||
let mut replies: Vec<Value> =
|
||||
String::from_utf8(out).unwrap().lines().map(|l| serde_json::from_str(l).unwrap()).collect();
|
||||
replies.sort_by_key(|r| r["id"].as_i64());
|
||||
assert!(replies[0]["result"]["instructions"].as_str().unwrap().contains(&proj.display().to_string()));
|
||||
assert_eq!(replies[1]["result"]["content"][0]["text"], format!("{tag} in {}", proj.display()));
|
||||
}
|
||||
|
||||
// Closing the newer server promotes the remaining server back to server.json.
|
||||
server_b.stop();
|
||||
assert_eq!(Discovery::read(&discovery).unwrap().port, server_a.port());
|
||||
let mut out = Vec::new();
|
||||
stdio::bridge(&discovery, &proj_a, session("still-here").as_bytes(), &mut out).await.unwrap();
|
||||
let mut replies: Vec<Value> =
|
||||
String::from_utf8(out).unwrap().lines().map(|l| serde_json::from_str(l).unwrap()).collect();
|
||||
replies.sort_by_key(|r| r["id"].as_i64());
|
||||
assert_eq!(replies[1]["result"]["content"][0]["text"], format!("still-here in {}", proj_a.display()));
|
||||
|
||||
server_a.stop();
|
||||
assert!(!discovery.exists());
|
||||
}
|
||||
@@ -88,6 +88,7 @@ pub(crate) fn detect_with_profiles(
|
||||
detect_projects(root, &["Makefile", "makefile", "GNUmakefile"], detect_make, &mut out);
|
||||
detect_projects(root, &["justfile", "Justfile", ".justfile"], detect_just, &mut out);
|
||||
detect_godot(root, &mut out);
|
||||
detect_projects(root, &["pubspec.yaml"], detect_flutter, &mut out);
|
||||
crate::external::detect(root, &mut out);
|
||||
|
||||
let mut seen = HashSet::new();
|
||||
@@ -1545,6 +1546,17 @@ fn detect_godot(root: &Path, out: &mut Vec<RunConfig>) {
|
||||
}
|
||||
}
|
||||
|
||||
/// A Flutter app: depends on the SDK and has the conventional entry point (packages have none).
|
||||
fn detect_flutter(root: &Path, out: &mut Vec<RunConfig>) {
|
||||
if root.join("lib/main.dart").is_file()
|
||||
&& read(&root.join("pubspec.yaml")).is_some_and(|pubspec| pubspec.contains("sdk: flutter"))
|
||||
{
|
||||
out.push(
|
||||
config("flutter:run", "flutter run", "flutter", "flutter", &["run", "-t", "lib/main.dart"]).debug("dart"),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
fn detect_just(root: &Path, out: &mut Vec<RunConfig>) {
|
||||
let Some(path) = first_existing(root, &["justfile", "Justfile", ".justfile"]) else { return };
|
||||
let Some(src) = read(&path) else { return };
|
||||
|
||||
@@ -627,6 +627,20 @@ fn godot_projects_run_their_main_scene() {
|
||||
assert!(detect(project(&[("main.gd", "extends Node\n")]).path()).is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn flutter_apps_run_their_entry_point() {
|
||||
let pubspec = "name: app\ndependencies:\n flutter:\n sdk: flutter\n";
|
||||
let dir = project(&[("pubspec.yaml", pubspec), ("lib/main.dart", "void main() {}\n")]);
|
||||
let c = detect(dir.path());
|
||||
let app = find(&c, "flutter:run");
|
||||
assert_eq!(app.kind, "flutter");
|
||||
assert_eq!(cmdline(app), "flutter run -t lib/main.dart");
|
||||
assert_eq!(app.debug_adapter.as_deref(), Some("dart"));
|
||||
// A Flutter package has no app to start, and a Dart package no Flutter.
|
||||
assert!(detect(project(&[("pubspec.yaml", pubspec), ("lib/widgets.dart", "")]).path()).is_empty());
|
||||
assert!(detect(project(&[("pubspec.yaml", "name: tool\n"), ("lib/main.dart", "")]).path()).is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn just_recipes() {
|
||||
let justfile = "\
|
||||
|
||||
@@ -1187,13 +1187,19 @@ pub fn configure_server(cfg: &mut LanguageServerConfig, root: &Path, server_root
|
||||
for (key, value) in module_env {
|
||||
build["env"][key] = json!(value);
|
||||
}
|
||||
if s.languages.go.gofumpt {
|
||||
build["gofumpt"] = json!(true);
|
||||
}
|
||||
let mut settings = cfg.settings.take().unwrap_or_else(|| json!({}));
|
||||
// gopls retains omitted keys on reconfiguration. Explicit empty values
|
||||
// restore defaults when the last project constraint is cleared.
|
||||
// Settings › Language Servers options are merged later, so look at them too.
|
||||
let init = cfg.initialization_options.as_ref().and_then(|v| serde_json::to_value(v).ok());
|
||||
for (key, default) in [("buildFlags", json!([])), ("env", json!({}))] {
|
||||
let user_init = s.language_servers.get(&cfg.name).and_then(|ls| ls.initialization_options.as_ref());
|
||||
for (key, default) in [("buildFlags", json!([])), ("env", json!({})), ("gofumpt", json!(false))] {
|
||||
if settings.get(key).is_none() {
|
||||
settings[key] = init.as_ref().and_then(|v| v.get(key)).cloned().unwrap_or(default);
|
||||
let explicit = user_init.and_then(|v| v.get(key)).or_else(|| init.as_ref()?.get(key));
|
||||
settings[key] = explicit.cloned().unwrap_or(default);
|
||||
}
|
||||
}
|
||||
merge(&mut settings, build);
|
||||
@@ -1241,6 +1247,21 @@ pub fn configure_server(cfg: &mut LanguageServerConfig, root: &Path, server_root
|
||||
}
|
||||
}
|
||||
|
||||
if name == "java-analyzer" {
|
||||
// Unset, java-analyzer finds a JDK and the Maven local repository the way Maven does;
|
||||
// the server's own `initializationOptions` in settings are merged later and win.
|
||||
let mut layer = serde_json::Map::new();
|
||||
if let Some(jdk) = project_jdk(root, s) {
|
||||
layer.insert("jdkHome".into(), json!(jdk.home.to_string_lossy()));
|
||||
}
|
||||
if let Some(repo) = maven_local_repository(root, s) {
|
||||
layer.insert("maven".into(), json!({ "localRepository": repo.to_string_lossy() }));
|
||||
}
|
||||
if !layer.is_empty() {
|
||||
merge_init_options(cfg, Value::Object(layer));
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(lib) = tsdk(server_root, s).or_else(|| tsdk(root, s)) {
|
||||
let lib_s = lib.to_string_lossy().into_owned();
|
||||
match name {
|
||||
@@ -1399,6 +1420,23 @@ pub fn language_servers(language: &str, defined: &[LanguageServerConfig], s: &Se
|
||||
out
|
||||
}
|
||||
|
||||
/// Built-in servers given an executable path in Settings › Language Servers, as
|
||||
/// (definition's command, path): they run that executable (see [`server_launch_overrides`]).
|
||||
pub fn server_command_paths<'a>(
|
||||
servers: impl IntoIterator<Item = &'a LanguageServerConfig>,
|
||||
root: &Path,
|
||||
s: &Settings,
|
||||
) -> Vec<(String, PathBuf)> {
|
||||
servers
|
||||
.into_iter()
|
||||
.filter_map(|server| {
|
||||
let command = s.language_servers.get(&server.name)?.command.trim();
|
||||
let path = command.contains(['/', '\\']).then(|| setting_path(root, command))??;
|
||||
Some((server.command.clone(), path))
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Adds the servers defined in settings to the languages they serve (to list
|
||||
/// them with the tools); disabled ones stay so they can be turned back on.
|
||||
pub fn with_custom_servers(langs: &mut [LanguageConfig], s: &Settings) {
|
||||
|
||||
@@ -86,6 +86,11 @@ pub(crate) fn java_runs(java: &Path) -> bool {
|
||||
runnable
|
||||
}
|
||||
|
||||
/// First runnable Java installation in discovery order, including standalone JREs.
|
||||
pub fn java_runtime(jdks: &[Jdk], min_java: u32) -> Option<&Jdk> {
|
||||
jdks.iter().find(|j| j.major >= min_java && java_runs(&java_executable(&j.home)))
|
||||
}
|
||||
|
||||
fn project_jdk_is_usable(jdk: &Jdk) -> bool {
|
||||
jdk.home.join("bin").join(crate::exe("javac")).is_file()
|
||||
&& (jdk.major < 9 || ide_core::java_runtime::supports_java_bundles(&jdk.home))
|
||||
@@ -748,6 +753,16 @@ pub(crate) fn write_test_java(home: &Path) {
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn java_runtime_accepts_a_jre_without_a_compiler() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let jre = test_jdk(tmp.path(), 17, "jre");
|
||||
std::fs::remove_file(jre.home.join("bin").join(crate::exe("javac"))).unwrap();
|
||||
let jdks = [test_jdk(tmp.path(), 11, "old"), jre.clone()];
|
||||
assert_eq!(java_runtime(&jdks, 17), Some(&jre));
|
||||
assert_eq!(java_runtime(&jdks, 21), None);
|
||||
}
|
||||
|
||||
fn test_jdk(root: &Path, major: u32, name: &str) -> Jdk {
|
||||
let home = root.join(name.trim_start_matches('/'));
|
||||
std::fs::create_dir_all(home.join("bin")).unwrap();
|
||||
@@ -1619,6 +1634,16 @@ pub fn usages_bundle(data_dir: &Path) -> Option<PathBuf> {
|
||||
)
|
||||
}
|
||||
|
||||
/// Extracts the JDBC bridge into the application's data directory.
|
||||
pub fn jdbc_bridge_jar(data_dir: &Path) -> Option<PathBuf> {
|
||||
install_bundle(
|
||||
data_dir,
|
||||
"jdbc-bridge",
|
||||
"1.0.0",
|
||||
include_bytes!("../../../java/jdbc-bridge/openidea-jdbc-bridge.jar"),
|
||||
)
|
||||
}
|
||||
|
||||
/// Installs the app-owned JDT LS bundle that inspects java-debug's JVMs.
|
||||
pub fn jvm_inspect_bundle(data_dir: &Path) -> Option<PathBuf> {
|
||||
install_bundle(
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
use crate::apply::{
|
||||
apply_to_run_configs, apply_to_run_configs_in, configure_python_extra_paths, configure_server,
|
||||
configure_vue_module_resolution, jdtls_jdk, maven_command, maven_options, project_jdk, python_interpreter,
|
||||
ruff_configuration, terminal_activation, tool_install_environment, tsdk, without_derived, without_derived_in,
|
||||
ruff_configuration, server_command_paths, server_protocol_overrides, terminal_activation, tool_install_environment,
|
||||
tsdk, without_derived, without_derived_in,
|
||||
};
|
||||
use crate::python::parse_pip_list;
|
||||
use crate::{bin_dir, exe, venv_of};
|
||||
@@ -340,6 +341,39 @@ fn jdtls_honors_the_configured_maven_local_repository() {
|
||||
assert!(cfg.args.iter().all(|a| !a.contains("maven.repo.local")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn java_analyzer_gets_the_project_jdk_and_maven_local_repository() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let configured = |s: &Settings| {
|
||||
let mut cfg = server("java-analyzer");
|
||||
configure_server(&mut cfg, tmp.path(), tmp.path(), s);
|
||||
server_protocol_overrides(&mut cfg, s);
|
||||
cfg.initialization_options.map(|v| serde_json::to_value(v).unwrap())
|
||||
};
|
||||
// Unset: the server discovers both itself.
|
||||
assert_eq!(configured(&Settings::default()), None);
|
||||
let jdk = tmp.path().join("jdk17");
|
||||
fake_jdk(&jdk);
|
||||
let repo = tmp.path().join("m2-repo");
|
||||
let mut s = Settings::default();
|
||||
s.languages.java.jdk = jdk.to_string_lossy().into_owned();
|
||||
s.languages.java.maven.local_repository = repo.to_string_lossy().into_owned();
|
||||
let options = configured(&s).unwrap();
|
||||
assert_eq!(options["jdkHome"], dunce::canonicalize(&jdk).unwrap().to_string_lossy().as_ref());
|
||||
assert_eq!(options["maven"]["localRepository"], repo.to_string_lossy().as_ref());
|
||||
// A `jdkHome` in the server's own settings wins.
|
||||
s.language_servers.insert(
|
||||
"java-analyzer".into(),
|
||||
ide_core::settings::LanguageServerSettings {
|
||||
initialization_options: Some(serde_json::json!({ "jdkHome": "/opt/jdk21" })),
|
||||
..Default::default()
|
||||
},
|
||||
);
|
||||
let options = configured(&s).unwrap();
|
||||
assert_eq!(options["jdkHome"], "/opt/jdk21");
|
||||
assert_eq!(options["maven"]["localRepository"], repo.to_string_lossy().as_ref());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn jdtls_m2e_gets_effective_settings_with_local_repository() {
|
||||
fn user_settings(cfg: &LanguageServerConfig) -> Option<std::path::PathBuf> {
|
||||
@@ -2452,7 +2486,10 @@ fn go_constraints_change_only_protocol_settings() {
|
||||
original.settings = Some(json!({"analyses":{"unusedparams":true}}));
|
||||
let mut defaults = original.clone();
|
||||
configure_server(&mut defaults, tmp.path(), tmp.path(), &settings);
|
||||
assert_eq!(defaults.settings, Some(json!({"analyses":{"unusedparams":true}, "buildFlags":[], "env":{}})));
|
||||
assert_eq!(
|
||||
defaults.settings,
|
||||
Some(json!({"analyses":{"unusedparams":true}, "buildFlags":[], "env":{}, "gofumpt":false}))
|
||||
);
|
||||
defaults.settings = original.settings.clone();
|
||||
assert_eq!(defaults, original);
|
||||
settings.languages.go.build_tags.custom = "integration".into();
|
||||
@@ -2462,7 +2499,8 @@ fn go_constraints_change_only_protocol_settings() {
|
||||
assert_eq!(
|
||||
configured.settings.take(),
|
||||
Some(json!({
|
||||
"analyses":{"unusedparams":true}, "buildFlags":["-tags=integration"], "env":{"GOOS":"linux"}
|
||||
"analyses":{"unusedparams":true}, "buildFlags":["-tags=integration"], "env":{"GOOS":"linux"},
|
||||
"gofumpt":false
|
||||
}))
|
||||
);
|
||||
configured.settings = original.settings.clone();
|
||||
@@ -2477,10 +2515,50 @@ fn go_empty_constraints_preserve_explicit_server_defaults() {
|
||||
use serde_json::json;
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let mut cfg = server("gopls");
|
||||
cfg.initialization_options =
|
||||
Some(toml::Value::try_from(json!({"buildFlags":["-tags=legacy"], "env":{"GOPROXY":"direct"}})).unwrap());
|
||||
cfg.initialization_options = Some(
|
||||
toml::Value::try_from(json!({"buildFlags":["-tags=legacy"], "env":{"GOPROXY":"direct"}, "gofumpt":true}))
|
||||
.unwrap(),
|
||||
);
|
||||
configure_server(&mut cfg, tmp.path(), tmp.path(), &Settings::default());
|
||||
assert_eq!(cfg.settings, Some(json!({"buildFlags":["-tags=legacy"], "env":{"GOPROXY":"direct"}})));
|
||||
assert_eq!(cfg.settings, Some(json!({"buildFlags":["-tags=legacy"], "env":{"GOPROXY":"direct"}, "gofumpt":true})));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn go_gofumpt_setting_reaches_gopls_and_turns_off_in_place() {
|
||||
use crate::apply::server_protocol_overrides;
|
||||
use serde_json::json;
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let mut settings = Settings::default();
|
||||
settings.languages.go.gofumpt = true;
|
||||
let mut on = server("gopls");
|
||||
configure_server(&mut on, tmp.path(), tmp.path(), &settings);
|
||||
assert_eq!(on.settings.as_ref().unwrap()["gofumpt"], json!(true));
|
||||
assert_eq!(on.initialization_options, server("gopls").initialization_options);
|
||||
// The setting wins over the language definition's value.
|
||||
let mut explicit = server("gopls");
|
||||
explicit.settings = Some(json!({"gofumpt": false}));
|
||||
configure_server(&mut explicit, tmp.path(), tmp.path(), &settings);
|
||||
assert_eq!(explicit.settings.unwrap()["gofumpt"], json!(true));
|
||||
// Turned off: an explicit false, because gopls keeps omitted keys on reconfiguration.
|
||||
let mut off = server("gopls");
|
||||
configure_server(&mut off, tmp.path(), tmp.path(), &Settings::default());
|
||||
assert_eq!(off.settings.unwrap()["gofumpt"], json!(false));
|
||||
// Off leaves a server-level value alone.
|
||||
let mut own = server("gopls");
|
||||
own.settings = Some(json!({"gofumpt": true}));
|
||||
configure_server(&mut own, tmp.path(), tmp.path(), &Settings::default());
|
||||
assert_eq!(own.settings.unwrap()["gofumpt"], json!(true));
|
||||
// Off doesn't shadow gofumpt in Settings › Language Servers initializationOptions.
|
||||
let mut user = Settings::default();
|
||||
user.language_servers
|
||||
.insert("gopls".into(), serde_json::from_value(json!({"initializationOptions": {"gofumpt": true}})).unwrap());
|
||||
let mut from_user = server("gopls");
|
||||
configure_server(&mut from_user, tmp.path(), tmp.path(), &user);
|
||||
server_protocol_overrides(&mut from_user, &user);
|
||||
assert_eq!(from_user.settings.unwrap()["gofumpt"], json!(true));
|
||||
let mut other = server("rust-analyzer");
|
||||
configure_server(&mut other, tmp.path(), tmp.path(), &settings);
|
||||
assert_eq!(other.settings, None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -2918,3 +2996,26 @@ fn without_the_vue_plugin_the_module_resolution_plugin_stays_out() {
|
||||
assert!(!configure_vue_module_resolution(&mut cfg, tmp.path()));
|
||||
assert!(cfg.initialization_options.is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn server_command_paths_report_executables_set_for_built_in_servers() {
|
||||
let servers = [server("java-analyzer"), server("jdtls"), server("kmp-lsp")];
|
||||
let mut s = Settings::default();
|
||||
let entry = |command: &str| ide_core::settings::LanguageServerSettings {
|
||||
command: command.into(),
|
||||
languages: vec!["java".into()],
|
||||
..Default::default()
|
||||
};
|
||||
s.language_servers.insert("java-analyzer".into(), entry("/opt/java-analyzer/bin/java-analyzer"));
|
||||
s.language_servers.insert("jdtls".into(), entry("bin/jdtls"));
|
||||
// A bare name is looked up like the definition's command.
|
||||
s.language_servers.insert("kmp-lsp".into(), entry("kmp-lsp-nightly"));
|
||||
let root = Path::new("/work");
|
||||
assert_eq!(
|
||||
server_command_paths(&servers, root, &s),
|
||||
[
|
||||
("java-analyzer".to_owned(), "/opt/java-analyzer/bin/java-analyzer".into()),
|
||||
("jdtls".to_owned(), root.join("bin/jdtls")),
|
||||
]
|
||||
);
|
||||
}
|
||||
@@ -317,7 +317,7 @@ impl Tools {
|
||||
let _ = std::fs::remove_dir_all(&staging);
|
||||
std::fs::create_dir_all(&staging)?;
|
||||
let result = (|| {
|
||||
let archive = fetch_verified(&http, &dl, &staging, cancel, emit)?;
|
||||
let archive = fetch_verified(&self.downloads, &http, &dl, &staging, cancel, emit)?;
|
||||
emit(InstallEvent::Line { text: "Extracting…".into() });
|
||||
let pkg = staging.join("pkg");
|
||||
archive::extract(&archive, archive::ArchiveKind::from_name(&dl.name), &pkg, "java", cancel)?;
|
||||
|
||||
@@ -19,6 +19,7 @@ pub mod archive;
|
||||
mod bundled;
|
||||
pub mod formatter;
|
||||
pub mod jdk;
|
||||
pub mod mirror;
|
||||
mod moved_venv;
|
||||
pub mod platform;
|
||||
#[cfg(any(windows, test))]
|
||||
@@ -29,6 +30,7 @@ pub mod server;
|
||||
pub mod sources;
|
||||
|
||||
use ide_core::error::ErrorKind;
|
||||
use ide_core::settings::ToolDownloadSettings;
|
||||
use ide_core::{CancellationToken, IdeError, IdeResult};
|
||||
use ide_language::LanguageConfig;
|
||||
use parking_lot::Mutex;
|
||||
@@ -468,6 +470,8 @@ pub struct Tools {
|
||||
/// The project the tools are used for: tools that install into "the active runtime" (gems) run here,
|
||||
/// so a version manager picks the project's Ruby.
|
||||
work_dir: Option<PathBuf>,
|
||||
/// Tools › Download sources: the mirror prefix and its fallback behaviour.
|
||||
downloads: ToolDownloadSettings,
|
||||
}
|
||||
|
||||
impl Tools {
|
||||
@@ -492,9 +496,16 @@ impl Tools {
|
||||
runtime_env: BTreeMap::new(),
|
||||
version_timeout: Duration::from_secs(4),
|
||||
work_dir: None,
|
||||
downloads: ToolDownloadSettings::default(),
|
||||
}
|
||||
}
|
||||
|
||||
/// [`Self::downloads`] from Tools › Download sources.
|
||||
pub fn with_downloads(mut self, downloads: ToolDownloadSettings) -> Self {
|
||||
self.downloads = downloads;
|
||||
self
|
||||
}
|
||||
|
||||
/// Applies the project's SDKs to prerequisite lookup, probes and installer children.
|
||||
pub fn with_runtime(mut self, commands: BTreeMap<String, PathBuf>, env: BTreeMap<String, String>) -> Self {
|
||||
self.runtime_commands = commands;
|
||||
@@ -1470,7 +1481,7 @@ impl Tools {
|
||||
cancel: &CancellationToken,
|
||||
emit: Emit,
|
||||
) -> IdeResult<()> {
|
||||
let archive = fetch_verified(http, dl, staging, cancel, emit)?;
|
||||
let archive = fetch_verified(&self.downloads, http, dl, staging, cancel, emit)?;
|
||||
emit(InstallEvent::Line { text: "Extracting…".into() });
|
||||
let pkg = staging.join("pkg");
|
||||
let kind = archive::ArchiveKind::from_name(&dl.name);
|
||||
@@ -1703,9 +1714,14 @@ impl Tools {
|
||||
}
|
||||
}
|
||||
|
||||
/// Downloads `dl` into `staging` and checks its SHA-256 when one is published;
|
||||
/// returns the downloaded file.
|
||||
/// Downloads `dl` from the best source that answers (Tools › Download sources) and checks its
|
||||
/// SHA-256 when one is published; returns the downloaded file.
|
||||
///
|
||||
/// A mirror may only deliver bytes that can be checked: when neither the release nor its
|
||||
/// checksum file gives a hash, an archive from a mirror is discarded and the official host is
|
||||
/// tried, because a third party must not be the only word on what gets installed.
|
||||
fn fetch_verified(
|
||||
downloads: &ToolDownloadSettings,
|
||||
http: &Http,
|
||||
dl: &sources::Download,
|
||||
staging: &Path,
|
||||
@@ -1713,33 +1729,92 @@ fn fetch_verified(
|
||||
emit: Emit,
|
||||
) -> IdeResult<PathBuf> {
|
||||
let archive = staging.join(&dl.name);
|
||||
http.download(&dl.url, &archive, dl.size, cancel, emit)?;
|
||||
let expected = match (&dl.sha256, &dl.checksum_url) {
|
||||
(Some(h), _) => Some(h.clone()),
|
||||
(None, Some(url)) => match http.get_text(url, cancel) {
|
||||
Ok(text) => sources::parse_checksum(&text, &dl.name),
|
||||
let candidates = mirror::candidates(&dl.url, downloads);
|
||||
let expected = expected_sha256(http, dl, cancel)?;
|
||||
let mut failures: Vec<String> = Vec::new();
|
||||
for (index, candidate) in candidates.iter().enumerate() {
|
||||
if cancel.is_cancelled() {
|
||||
return Err(IdeError::cancelled());
|
||||
}
|
||||
// Bytes a failed source left behind must not be resumed or mistaken for this one's.
|
||||
if index > 0 {
|
||||
let _ = std::fs::remove_file(&archive);
|
||||
}
|
||||
// Only worth saying when there is a choice of source: with one candidate the line
|
||||
// would just repeat the download message that follows.
|
||||
if candidates.len() > 1 {
|
||||
emit(InstallEvent::Line {
|
||||
text: format!("Trying {} ({} of {})…", candidate.describe(), index + 1, candidates.len()),
|
||||
});
|
||||
}
|
||||
let attempt = http.download(&candidate.url, &archive, dl.size, cancel, emit);
|
||||
match attempt {
|
||||
Ok(()) => match check_sha256(&archive, dl, expected.as_deref(), candidate.mirror.is_some(), emit) {
|
||||
Ok(()) => {
|
||||
if cancel.is_cancelled() {
|
||||
return Err(IdeError::cancelled());
|
||||
}
|
||||
return Ok(archive);
|
||||
}
|
||||
// A mismatch from a mirror is that mirror's failure: the official host below
|
||||
// still gets its chance. From the official host it is the download's failure.
|
||||
Err(e) if candidate.mirror.is_some() => {
|
||||
failures.push(format!("{}: {}", candidate.describe(), e.message))
|
||||
}
|
||||
Err(e) => return Err(e),
|
||||
},
|
||||
// Cancelling stops the install: the remaining sources are not tried.
|
||||
Err(e) if e.kind == ErrorKind::Cancelled => return Err(e),
|
||||
Err(_) => None,
|
||||
},
|
||||
(None, None) => None,
|
||||
};
|
||||
match expected {
|
||||
Some(expected) => {
|
||||
let actual = sources::sha256_file(&archive)?;
|
||||
if actual != expected {
|
||||
return Err(IdeError::new(
|
||||
ErrorKind::Io,
|
||||
format!("checksum mismatch for {}: expected {expected}, got {actual}", dl.name),
|
||||
));
|
||||
}
|
||||
emit(InstallEvent::Line { text: "Checksum verified (SHA-256)".into() });
|
||||
Err(e) => failures.push(format!("{}: {}", candidate.describe(), e.message)),
|
||||
}
|
||||
None => emit(InstallEvent::Line { text: "No checksum published; skipping verification".into() }),
|
||||
}
|
||||
if cancel.is_cancelled() {
|
||||
return Err(IdeError::cancelled());
|
||||
Err(IdeError::new(ErrorKind::Io, format!("could not download {} ({})", dl.name, failures.join("; "))))
|
||||
}
|
||||
|
||||
/// The SHA-256 `dl` must match, when one is published.
|
||||
///
|
||||
/// The checksum file is read from its official URL, never through the mirror: otherwise a
|
||||
/// mirror could vouch for bytes it chose itself.
|
||||
fn expected_sha256(http: &Http, dl: &sources::Download, cancel: &CancellationToken) -> IdeResult<Option<String>> {
|
||||
match (&dl.sha256, &dl.checksum_url) {
|
||||
(Some(h), _) => Ok(Some(h.clone())),
|
||||
(None, Some(url)) => match http.get_text(url, cancel) {
|
||||
Ok(text) => Ok(sources::parse_checksum(&text, &dl.name)),
|
||||
Err(e) if e.kind == ErrorKind::Cancelled => Err(e),
|
||||
Err(_) => Ok(None),
|
||||
},
|
||||
(None, None) => Ok(None),
|
||||
}
|
||||
Ok(archive)
|
||||
}
|
||||
|
||||
/// Checks `archive` against `expected`, if any. `via_mirror` refuses an archive nothing can
|
||||
/// vouch for rather than installing unverifiable bytes from a third party.
|
||||
fn check_sha256(
|
||||
archive: &Path,
|
||||
dl: &sources::Download,
|
||||
expected: Option<&str>,
|
||||
via_mirror: bool,
|
||||
emit: Emit,
|
||||
) -> IdeResult<()> {
|
||||
let Some(expected) = expected else {
|
||||
if via_mirror {
|
||||
return Err(IdeError::new(
|
||||
ErrorKind::Io,
|
||||
format!("{} has no published checksum, so the mirror's copy is not trusted", dl.name),
|
||||
));
|
||||
}
|
||||
emit(InstallEvent::Line { text: "No checksum published; skipping verification".into() });
|
||||
return Ok(());
|
||||
};
|
||||
let actual = sources::sha256_file(archive)?;
|
||||
if actual != expected {
|
||||
return Err(IdeError::new(
|
||||
ErrorKind::Io,
|
||||
format!("checksum mismatch for {}: expected {expected}, got {actual}", dl.name),
|
||||
));
|
||||
}
|
||||
emit(InstallEvent::Line { text: "Checksum verified (SHA-256)".into() });
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Removes the quarantine flag archives may carry, so macOS runs what was unpacked.
|
||||
|
||||
@@ -0,0 +1,127 @@
|
||||
//! Download mirrors: a user-configured proxy prefix the installer tries when an official host
|
||||
//! is slow or unreachable (Tools › Download sources).
|
||||
//!
|
||||
//! The official URL is appended to the prefix unchanged, which is what the public GitHub
|
||||
//! proxies speak. Only `github.com` is mirrored: those proxies hard-code that host, so an
|
||||
//! Eclipse or Open VSX address through one answers 502. Hosts that carry credentials are
|
||||
//! never mirrored either — the GitHub token must not reach a third party.
|
||||
|
||||
use ide_core::settings::ToolDownloadSettings;
|
||||
|
||||
/// The only host a mirror prefix may be prepended to.
|
||||
const MIRRORED_PREFIX: &str = "https://github.com/";
|
||||
|
||||
/// Where one download is fetched from.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct Candidate {
|
||||
pub url: String,
|
||||
/// The mirror this URL goes through; `None` for the official host.
|
||||
pub mirror: Option<String>,
|
||||
}
|
||||
|
||||
impl Candidate {
|
||||
/// The source shown to the user in install messages.
|
||||
pub fn describe(&self) -> &str {
|
||||
match &self.mirror {
|
||||
Some(mirror) => mirror,
|
||||
None => "the official source",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The mirror prefix, normalised to end in `/`; `None` when none is set or it is not an
|
||||
/// `http(s)://` URL.
|
||||
pub fn prefix(mirror: &str) -> Option<String> {
|
||||
let trimmed = mirror.trim();
|
||||
let rest = trimmed.strip_prefix("https://").or_else(|| trimmed.strip_prefix("http://"))?;
|
||||
if rest.split('/').next().unwrap_or("").is_empty() {
|
||||
return None;
|
||||
}
|
||||
Some(if trimmed.ends_with('/') { trimmed.to_owned() } else { format!("{trimmed}/") })
|
||||
}
|
||||
|
||||
/// `url` through `mirror`, or `None` when it is not a source a prefix can carry.
|
||||
pub fn through(url: &str, mirror: &str) -> Option<String> {
|
||||
let prefix = prefix(mirror)?;
|
||||
if !url.starts_with(MIRRORED_PREFIX) {
|
||||
return None;
|
||||
}
|
||||
Some(format!("{prefix}{url}"))
|
||||
}
|
||||
|
||||
/// The URLs to try for `url`: the mirror when one is set, then the official host.
|
||||
///
|
||||
/// A configured mirror is tried first — the user set it because the official host is slow, so
|
||||
/// waiting for that host to fail first would defeat the point. The official host stays last as
|
||||
/// the fallback, and with no mirror the list is exactly `[url]`, so an unconfigured IDE
|
||||
/// downloads as it always did.
|
||||
pub fn candidates(url: &str, settings: &ToolDownloadSettings) -> Vec<Candidate> {
|
||||
let official = Candidate { url: url.to_owned(), mirror: None };
|
||||
match through(url, &settings.github_mirror) {
|
||||
Some(mirrored) => vec![Candidate { url: mirrored, mirror: Some(settings.github_mirror.clone()) }, official],
|
||||
None => vec![official],
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
const ASSET: &str = "https://github.com/LuaLS/lua-language-server/releases/download/3.13.5/x.zip";
|
||||
|
||||
fn settings(mirror: &str) -> ToolDownloadSettings {
|
||||
ToolDownloadSettings { github_mirror: mirror.into() }
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_mirror_keeps_the_official_url_only() {
|
||||
assert_eq!(candidates(ASSET, &settings("")), vec![Candidate { url: ASSET.into(), mirror: None }]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_prefix_gets_one_trailing_slash() {
|
||||
for mirror in ["https://gh-proxy.com", "https://gh-proxy.com/", " https://gh-proxy.com "] {
|
||||
assert_eq!(
|
||||
through(ASSET, mirror).as_deref(),
|
||||
Some(
|
||||
"https://gh-proxy.com/https://github.com/LuaLS/lua-language-server/releases/download/3.13.5/x.zip"
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hosts_a_prefix_cannot_carry_stay_official() {
|
||||
for url in [
|
||||
// The public proxies only answer for github.com; these answer 502 through them.
|
||||
"https://download.eclipse.org/jdtls/milestones/1.63.0/x.tar.gz",
|
||||
"https://open-vsx.org/api/vscjava/vscode-java-debug/0.60.0/file/x.vsix",
|
||||
"https://api.github.com/repos/cli/cli/releases/latest",
|
||||
"https://repo1.maven.org/maven2/org/eclipse/lsp4j/1.0.0/x.jar",
|
||||
] {
|
||||
assert_eq!(through(url, "https://gh-proxy.com"), None, "{url}");
|
||||
assert_eq!(candidates(url, &settings("https://gh-proxy.com")).len(), 1, "{url}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_configured_mirror_goes_first() {
|
||||
let candidates = candidates(ASSET, &settings("https://gh-proxy.com"));
|
||||
assert_eq!(candidates[0].mirror.as_deref(), Some("https://gh-proxy.com"));
|
||||
assert_eq!(candidates[1].mirror, None, "the official host is the fallback");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_prefix_that_is_not_a_url_is_ignored() {
|
||||
for mirror in ["gh-proxy.com", "ftp://x", "https://", " "] {
|
||||
assert_eq!(prefix(mirror), None, "{mirror}");
|
||||
assert_eq!(through(ASSET, mirror), None, "{mirror}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_already_prefixed_url_is_not_prefixed_twice() {
|
||||
let once = through(ASSET, "https://gh-proxy.com").unwrap();
|
||||
assert_eq!(through(&once, "https://gh-proxy.com"), None);
|
||||
}
|
||||
}
|
||||
@@ -222,6 +222,31 @@ mod tests {
|
||||
assert_eq!(select_asset(&names[5..8], rules, WIN), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn java_analyzer_assets() {
|
||||
let names = [
|
||||
"java-analyzer-macos-aarch64.gz",
|
||||
"java-analyzer-macos-x86_64.gz",
|
||||
"java-analyzer-linux-aarch64.gz",
|
||||
"java-analyzer-linux-x86_64.gz",
|
||||
"java-analyzer-windows-aarch64.zip",
|
||||
"java-analyzer-windows-x86_64.zip",
|
||||
"java-analyzer-macos-aarch64.gz.sha256",
|
||||
"java-analyzer-windows-x86_64.zip.sha256",
|
||||
];
|
||||
let rules = crate::registry::find("java-analyzer").unwrap().asset_rules();
|
||||
for (os, arch, expected) in [
|
||||
(Os::Mac, Arch::Arm64, names[0]),
|
||||
(Os::Mac, Arch::X64, names[1]),
|
||||
(Os::Linux, Arch::Arm64, names[2]),
|
||||
(Os::Linux, Arch::X64, names[3]),
|
||||
(Os::Windows, Arch::Arm64, names[4]),
|
||||
(Os::Windows, Arch::X64, names[5]),
|
||||
] {
|
||||
assert_eq!(select_asset(&names, rules, Platform { os, arch }), Some(expected));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rosetta_fallback_and_missing() {
|
||||
let names = ["zls-x86_64-macos.tar.xz", "zls-x86_64-linux.tar.xz", "zls-x86_64-windows.zip"];
|
||||
|
||||
@@ -793,6 +793,28 @@ pub static TOOLS: &[ToolSpec] = &[
|
||||
homepage: "https://github.com/eclipse-jdtls/eclipse.jdt.ls",
|
||||
needs: &[],
|
||||
},
|
||||
ToolSpec {
|
||||
name: "java-analyzer",
|
||||
display: "java-analyzer",
|
||||
kind: LanguageServer,
|
||||
commands: &["java-analyzer"],
|
||||
method: Method::Release {
|
||||
source: ReleaseSource::GitHub { repo: "l0ng-ai/java-analyzer" },
|
||||
assets: &[
|
||||
rule(Mac, Arm64, "java-analyzer-macos-aarch64.gz"),
|
||||
rule(Mac, X64, "java-analyzer-macos-x86_64.gz"),
|
||||
rule(Linux, Arm64, "java-analyzer-linux-aarch64.gz"),
|
||||
rule(Linux, X64, "java-analyzer-linux-x86_64.gz"),
|
||||
rule(Windows, Arm64, "java-analyzer-windows-aarch64.zip"),
|
||||
rule(Windows, X64, "java-analyzer-windows-x86_64.zip"),
|
||||
],
|
||||
locate: &["java-analyzer"],
|
||||
},
|
||||
fallback: None,
|
||||
version: ver("java-analyzer"),
|
||||
homepage: "https://github.com/l0ng-ai/java-analyzer",
|
||||
needs: &[],
|
||||
},
|
||||
ToolSpec {
|
||||
name: "kmp-lsp",
|
||||
display: "kmp-lsp",
|
||||
|
||||
@@ -59,7 +59,9 @@ pub fn fetch(
|
||||
size: None,
|
||||
sha256: Some(sha256),
|
||||
};
|
||||
let archive = fetch_verified(&http, &dl, &staging, cancel, emit)?;
|
||||
// The remote server binary is not part of Tools › Download sources: it always comes
|
||||
// from its own release.
|
||||
let archive = fetch_verified(&Default::default(), &http, &dl, &staging, cancel, emit)?;
|
||||
let unpacked = staging.join("openidea-server");
|
||||
let mut gz = flate2::read::GzDecoder::new(std::fs::File::open(&archive)?);
|
||||
std::io::copy(&mut gz, &mut std::fs::File::create(&unpacked)?)?;
|
||||
|
||||
@@ -193,8 +193,8 @@ fn interchangeable_servers_list_their_alternatives() {
|
||||
assert_eq!(find(&list, name).alternatives, python);
|
||||
}
|
||||
assert_eq!(find(&list, "vtsls").alternatives, ["vtsls", "typescript-language-server"]);
|
||||
for name in ["jdtls", "kmp-lsp"] {
|
||||
assert_eq!(find(&list, name).alternatives, ["jdtls", "kmp-lsp"]);
|
||||
for name in ["jdtls", "java-analyzer", "kmp-lsp"] {
|
||||
assert_eq!(find(&list, name).alternatives, ["jdtls", "java-analyzer", "kmp-lsp"]);
|
||||
assert_eq!(find(&list, name).languages, if name == "kmp-lsp" { &["Java", "Kotlin"][..] } else { &["Java"] });
|
||||
}
|
||||
for name in ["kotlin-lsp", "kotlin-language-server"] {
|
||||
@@ -1131,3 +1131,103 @@ fn npm_tools_report_a_newer_registry_version() {
|
||||
);
|
||||
assert_eq!(tools("5.4.0").updates(&cancel).unwrap(), vec![]);
|
||||
}
|
||||
|
||||
/// Tools › Download sources: a configured mirror serves a GitHub release the official host
|
||||
/// cannot, and the official host is kept as the fallback.
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn a_mirror_serves_what_the_official_host_cannot() {
|
||||
let env = Env::new();
|
||||
let fixtures = env.root.parent().unwrap().join("fixtures");
|
||||
fs::create_dir_all(&fixtures).unwrap();
|
||||
let archive = fixtures.join("lls.tar.gz");
|
||||
{
|
||||
let gz = flate2::write::GzEncoder::new(fs::File::create(&archive).unwrap(), flate2::Compression::fast());
|
||||
let mut b = tar::Builder::new(gz);
|
||||
let body = b"#!/bin/sh\necho 3.13.5\n";
|
||||
let mut h = tar::Header::new_gnu();
|
||||
h.set_size(body.len() as u64);
|
||||
h.set_mode(0o755);
|
||||
h.set_cksum();
|
||||
b.append_data(&mut h, "bin/lua-language-server", &body[..]).unwrap();
|
||||
b.into_inner().unwrap().finish().unwrap();
|
||||
}
|
||||
let digest = sources::sha256_file(&archive).unwrap();
|
||||
// Release assets live on github.com: only those addresses are mirrored.
|
||||
let assets: Vec<String> = ["darwin-arm64.tar.gz", "darwin-x64.tar.gz", "linux-x64.tar.gz", "linux-arm64.tar.gz"]
|
||||
.iter()
|
||||
.map(|s| {
|
||||
format!(
|
||||
r#"{{"name":"lua-language-server-3.13.5-{s}","browser_download_url":"https://github.com/LuaLS/lua-language-server/releases/download/3.13.5/{s}","size":{},"digest":"sha256:{digest}"}}"#,
|
||||
fs::metadata(&archive).unwrap().len()
|
||||
)
|
||||
})
|
||||
.collect();
|
||||
let json = fixtures.join("release.json");
|
||||
fs::write(&json, format!(r#"{{"tag_name":"3.13.5","assets":[{}]}}"#, assets.join(","))).unwrap();
|
||||
// The mirror serves the asset; the official host is unreachable.
|
||||
let log = fixtures.join("asked.log");
|
||||
env.script(
|
||||
&env.sys,
|
||||
"curl",
|
||||
&format!(
|
||||
r#"out=""; url=""
|
||||
while [ $# -gt 0 ]; do case "$1" in -o) out="$2"; shift 2;; -A|-H|--retry|--connect-timeout) shift 2;; -*) shift;; *) url="$1"; shift;; esac; done
|
||||
/bin/echo "$url" >> '{log}'
|
||||
case "$url" in
|
||||
https://api.github.com/*) /bin/cat '{json}';;
|
||||
https://mirror.test/https://github.com/*) /bin/cp '{archive}' "$out";;
|
||||
*) exit 22;;
|
||||
esac"#,
|
||||
log = log.display(),
|
||||
json = json.display(),
|
||||
archive = archive.display()
|
||||
),
|
||||
);
|
||||
let downloads = ToolDownloadSettings { github_mirror: "https://mirror.test/".into() };
|
||||
let emit = |_: InstallEvent| {};
|
||||
env.tools()
|
||||
.with_downloads(downloads)
|
||||
.install("lua-language-server", &langs(), &CancellationToken::new(), &emit)
|
||||
.unwrap();
|
||||
let asked = fs::read_to_string(&log).unwrap();
|
||||
// A configured mirror is tried first — waiting for a slow official host would defeat it.
|
||||
let mirror_at = asked.find("https://mirror.test/https://github.com/").expect("the mirror is tried");
|
||||
assert!(!asked[..mirror_at].contains("releases/download"), "the mirror goes first: {asked}");
|
||||
}
|
||||
|
||||
/// A mirror may only deliver bytes something can vouch for; the official host may not.
|
||||
#[test]
|
||||
fn a_mirror_never_installs_what_it_alone_vouches_for() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let archive = tmp.path().join("tool.tar.gz");
|
||||
fs::write(&archive, b"bytes").unwrap();
|
||||
let digest = sources::sha256_file(&archive).unwrap();
|
||||
let dl = |sha256: Option<&str>| sources::Download {
|
||||
version: "1.0.0".into(),
|
||||
name: "tool.tar.gz".into(),
|
||||
url: "https://github.com/o/r/releases/download/v1/tool.tar.gz".into(),
|
||||
size: None,
|
||||
sha256: sha256.map(str::to_owned),
|
||||
checksum_url: None,
|
||||
};
|
||||
let lines = Mutex::new(Vec::new());
|
||||
let emit = |e: InstallEvent| {
|
||||
if let InstallEvent::Line { text } = e {
|
||||
lines.lock().push(text);
|
||||
}
|
||||
};
|
||||
|
||||
// Nothing published: the official host is trusted, a mirror is not.
|
||||
check_sha256(&archive, &dl(None), None, false, &emit).unwrap();
|
||||
let err = check_sha256(&archive, &dl(None), None, true, &emit).unwrap_err();
|
||||
assert!(err.message.contains("no published checksum"), "{}", err.message);
|
||||
|
||||
// A matching hash passes from either source.
|
||||
check_sha256(&archive, &dl(Some(&digest)), Some(&digest), true, &emit).unwrap();
|
||||
assert!(lines.lock().iter().any(|l| l.contains("Checksum verified")));
|
||||
|
||||
// A mismatch is refused whoever served it.
|
||||
let err = check_sha256(&archive, &dl(Some(&digest)), Some(&"0".repeat(64)), true, &emit).unwrap_err();
|
||||
assert!(err.message.contains("checksum mismatch"), "{}", err.message);
|
||||
}
|
||||
@@ -38,7 +38,7 @@ flowchart LR
|
||||
## The server
|
||||
|
||||
```text
|
||||
openidea-server --stdio | --connect <host> <port> | --version
|
||||
openidea-server --stdio | --mcp-stdio [--project <path>] | --connect <host> <port> | --version
|
||||
```
|
||||
|
||||
- `--stdio` serves one desktop client over stdin/stdout. stdout carries the protocol, so logs go to stderr (filter with `OPENIDEA_LOG`).
|
||||
@@ -78,12 +78,12 @@ A few commands are always local even if the server knows them: `open_project` an
|
||||
|
||||
### What runs where
|
||||
|
||||
| On the remote machine | On the desktop |
|
||||
| -------------------------------------------------------- | ------------------------------------ |
|
||||
| The project: files, documents, settings | Windows, menus, the webview |
|
||||
| Language servers, index, search | Client UI settings, keymaps, themes |
|
||||
| Git, terminals, runs, debug adapters | The SSH connection and port forwards |
|
||||
| Code plugins of the project, their processes and fetches | Plugin secrets (keychain) |
|
||||
| On the remote machine | On the desktop |
|
||||
| --------------------------------------------------------- | ------------------------------------ |
|
||||
| The project: files, documents, settings | Windows, menus, the webview |
|
||||
| Language servers, index, search | Client UI settings, keymaps, themes |
|
||||
| Git, terminals, runs, debug adapters, built-in MCP server | The SSH connection and port forwards |
|
||||
| Code plugins of the project, their processes and fetches | Plugin secrets (keychain) |
|
||||
|
||||
## Port forwarding
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ title: "Databases"
|
||||
description: "Connect to SQL databases, browse schemas, run queries in a console, and view, edit, import and export data."
|
||||
---
|
||||
|
||||
The **Database** tool window connects to SQLite, PostgreSQL, MySQL/MariaDB, Microsoft SQL Server, Oracle, ClickHouse and Snowflake, plus databases that speak one of their protocols. Drivers are built into the IDE; there are no JDBC drivers to download.
|
||||
The **Database** tool window connects to SQLite, PostgreSQL, MySQL/MariaDB, Microsoft SQL Server, Oracle, ClickHouse and Snowflake, plus databases that speak one of their protocols. These presets use built-in drivers. You can also connect with a vendor JDBC driver installed locally.
|
||||
|
||||
Open it from **Tools › Database** or **View › Tool Windows › Database**. The **Tools** menu also has **New Data Source…** and **Open Query Console**.
|
||||
|
||||
@@ -23,6 +23,16 @@ Open it from **Tools › Database** or **View › Tool Windows › Database**. T
|
||||
|
||||
Presets set the default port and, where needed, SSL (for example Redshift, Neon and Supabase require SSL).
|
||||
|
||||
## Custom JDBC drivers
|
||||
|
||||
Choose **New › Data Source › Custom JDBC**, then **Manage Drivers…**. Add a named profile with the vendor's driver JAR, any dependency JARs, the fully qualified driver class, and an optional JDBC URL template. OpenIDEA reads the driver classes declared in the selected JARs. You can choose a different class or enter its full name manually. Use `{host}`, `{port}`, `{database}` or `{file}` in a template to get matching connection fields, or edit the JDBC URL directly. Only import drivers you trust. OpenIDEA loads their code in a separate process using an installed Java 17 or newer runtime.
|
||||
|
||||
Select the profile, enter the vendor's JDBC URL, and use the **User**, **Password**, and **Advanced** fields for credentials and driver properties. The URL and Advanced properties are stored in the data source configuration. Put credentials in the password field to use the existing OS credential store. On Linux, saving a password requires a running Secret Service. You can also enter the password for the current session. The **Read-only** option asks the JDBC driver for a read-only connection. Use a database account with read-only privileges to enforce it. Cancelling a query keeps the session and its temporary tables when the driver stops the statement. If the driver does not respond within three seconds, OpenIDEA closes the session. A submitted statement can still finish on the server. **Test Connection** reports the database product and version. Schema, table and column browsing use JDBC metadata; the SQL console supports auto-commit queries and updates. Vendor-specific DDL, execution plans and table-grid writes are unavailable for custom drivers.
|
||||
|
||||
For a file-backed database, use an absolute path to choose its location. Relative paths start in the persistent `jdbc/files` folder inside OpenIDEA’s application data directory.
|
||||
|
||||
Profiles are reused across projects. If a project file changes a connection's URL or properties outside the IDE, review the settings in the data source dialog and save them before reconnecting.
|
||||
|
||||
## Add a data source
|
||||
|
||||
Click **+** in the Database tool window (or press `⌘N` / `Alt+Insert` there) and choose:
|
||||
@@ -205,7 +215,7 @@ Limits: `TIMESTAMP_LTZ` values are shown in UTC; SSH tunnels are not offered; re
|
||||
|
||||
## Not supported
|
||||
|
||||
- MongoDB, Redis, BigQuery and other non-listed databases; JDBC drivers.
|
||||
- Native drivers for MongoDB, Redis and BigQuery.
|
||||
- SQL Server `GO` batch separators.
|
||||
- A "run the whole console" action (use a selection, or **Run SQL Script…**).
|
||||
- Exporting more than the loaded page.
|
||||
|
||||
@@ -163,7 +163,7 @@ Each form has **Host** and **Port** fields and shows the command to start the re
|
||||
|
||||
- **Java HotSwap**: **Run › Debugging Actions › Reload Changed Classes** compiles your changes and reloads the changed classes into the running Java debug session. Active frames keep running the old code until they return. When JDT LS finishes a build after a save, the IDE offers the reload.
|
||||
- **Maven and Gradle**: debugging a Maven or Gradle configuration starts the JVM with a JDWP agent and attaches java-debug to it.
|
||||
- **Dart / Flutter**: **Hot Reload** and **Hot Restart** are available while a Dart session is running.
|
||||
- **Dart / Flutter**: **Hot Reload** and **Hot Restart** are available while a Dart session is running. In a Flutter project, the device selector next to the run configuration chooses where Run and Debug start the app; a run configuration's **Device** field pins it to one device. Saving a Dart file of a running Flutter debug session also hot reloads the app, unless the file has errors. Turn this off with **Hot reload on save** in **Settings › Build, Execution, Deployment › Debugger**.
|
||||
- **PHP**: if the PHP interpreter does not load Xdebug, breakpoints are skipped; the IDE warns about this when the session starts.
|
||||
- **Python**: **Settings › Build, Execution, Deployment › Python Debugger** has **Attach to subprocess automatically while debugging** and **Gevent compatible**.
|
||||
|
||||
|
||||
@@ -132,8 +132,11 @@ Plugins can add more tools; they appear in the same list. A plugin tool marked a
|
||||
- **Running and building ask first.** `execute_run_configuration`, `build_project`, `run_command` and `http_run_request` show a dialog in OpenIDEA naming the client, the project, the tool and what will run. Choose **Allow** or **Deny**; several requests queue up.
|
||||
- Tools that write refuse paths outside the project.
|
||||
|
||||
## Remote development
|
||||
|
||||
In a remote window ([SSH](/en/guide/remote/ssh), WSL or [Dev Containers](/en/guide/remote/containers)), `openidea-server` runs its own MCP server on the remote host, configured independently in **Settings › Agents › IDE MCP Server** while that remote window is focused. Coding agents started inside the remote window connect to it automatically; external CLI clients on the remote host can use **Copy Stdio Config** (`~/.openidea/bin/openidea-server --mcp-stdio`) or **Copy HTTP Config**, and clients on your desktop can reach it by forwarding its port in the **Ports** tool window.
|
||||
|
||||
## Limits
|
||||
|
||||
- Remote projects (opened over SSH, on a server or in a container) are not supported by the MCP server yet.
|
||||
- The server serves only projects open in OpenIDEA. Closing the project's window makes its tools unavailable.
|
||||
- Responses are plain JSON; the server does not stream over SSE.
|
||||
@@ -92,11 +92,12 @@ Build on, or cross-compile for, the host's platform, then point `OPENIDEA_SERVER
|
||||
|
||||
The flags are meant for the desktop app, but `--version` is handy for checking an installed build.
|
||||
|
||||
| Flag | Meaning |
|
||||
| ------------------------- | -------------------------------------------------------------------------------------------------------------- |
|
||||
| `--version` | Print the server version and protocol version, then exit. |
|
||||
| `--stdio` | Serve one desktop client over stdin / stdout. Logs go to stderr. |
|
||||
| `--connect <host> <port>` | Relay one TCP connection to `<host>:<port>` over stdin / stdout. Used for port forwarding into dev containers. |
|
||||
| Flag | Meaning |
|
||||
| ------------------------------- | -------------------------------------------------------------------------------------------------------------- |
|
||||
| `--version` | Print the server version and protocol version, then exit. |
|
||||
| `--stdio` | Serve one desktop client over stdin / stdout. Logs go to stderr. |
|
||||
| `--mcp-stdio [--project <dir>]` | Relay stdio MCP traffic to the running `openidea-server` MCP server on this host. |
|
||||
| `--connect <host> <port>` | Relay one TCP connection to `<host>:<port>` over stdin / stdout. Used for port forwarding into dev containers. |
|
||||
|
||||
```bash
|
||||
ls ~/.openidea/server/*/*/openidea-server
|
||||
|
||||
@@ -15,10 +15,11 @@ If gopls is missing, OpenIDEA shows a **gopls is not installed** notification wi
|
||||
|
||||
### Go SDK
|
||||
|
||||
**Settings › Languages & Frameworks › Go** has two settings:
|
||||
**Settings › Languages & Frameworks › Go** has three settings:
|
||||
|
||||
- **GOROOT**: the Go SDK used by gopls and the go run and test configurations. **Default** uses `go` on `PATH`; if the project pins a Go version for mise or asdf, that install is used instead.
|
||||
- **Project GOPATH**: empty uses `go env GOPATH`.
|
||||
- **Format with gofumpt**: gopls formats with [gofumpt](https://github.com/mvdan/gofumpt), a stricter gofmt, when you reformat code (gopls `gofumpt` setting). Off by default.
|
||||
|
||||
The GOROOT list shows the SDKs the IDE finds: the `go` on `PATH`, `/usr/local/go`, Homebrew and `/usr/lib/go` installs, SDKs in `~/sdk` (installed with `golang.org/dl`), toolchains Go downloaded itself (`~/go/pkg/mod/golang.org/toolchain@…`), and mise and asdf installs.
|
||||
|
||||
|
||||
@@ -128,7 +128,7 @@ The table lists every built-in language definition. Server and debugger names in
|
||||
| HTTP Request | `http` | `.http`, `.rest` | Yes | — | — | — |
|
||||
| Ignore File | `ignore` | `.gitignore`, `.dockerignore` | — | — | — | — |
|
||||
| INI | `ini` | `.ini`, `.cfg`, `.conf`, `.cnf`, `.inf`, … | Yes | — | — | — |
|
||||
| Java | `java` | `.java` | Yes | Yes | **jdtls** → **kmp-lsp** | **java-debug** |
|
||||
| Java | `java` | `.java` | Yes | Yes | **jdtls** → **java-analyzer** → **kmp-lsp** | **java-debug** |
|
||||
| JavaScript | `javascript` | `.js`, `.mjs`, `.cjs` | Yes | Yes | **vtsls** → **typescript-language-server** + **oxlint** | **js-debug** |
|
||||
| JavaScript JSX | `javascriptreact` | `.jsx` | Yes | Yes | **vtsls** → **typescript-language-server** + **oxlint** | **js-debug** |
|
||||
| JSON | `json` | `.json`, `.geojson`, `.topojson`, `.webmanifest`, `.har`, … | Yes | Yes | **vscode-json-language-server** | — |
|
||||
|
||||
@@ -7,15 +7,17 @@ Java code insight comes from the Eclipse JDT Language Server (jdtls), extended w
|
||||
|
||||
## Language servers
|
||||
|
||||
| Language | Server | Fallback | Debugger |
|
||||
| ---------------------- | ----------------------------------------- | ------------------------------------------------------------------ | --------------------------------------- |
|
||||
| Java | Eclipse JDT Language Server (`jdtls`) | `kmp-lsp` | Microsoft java-debug, loaded into jdtls |
|
||||
| Kotlin | Kotlin LSP (JetBrains, `kotlin-lsp`) | Kotlin Language Server (fwcd, `kotlin-language-server`), `kmp-lsp` | Kotlin Debug Adapter (fwcd) |
|
||||
| Groovy, Gradle scripts | `groovy-language-server` (you provide it) | | |
|
||||
| JSP | none (highlighting only) | | |
|
||||
| Language | Server | Fallback | Debugger |
|
||||
| ---------------------- | ----------------------------------------- | ------------------------------------------------------------------ | ------------------------------------------------------------------------------- |
|
||||
| Java | Eclipse JDT Language Server (`jdtls`) | `java-analyzer`, `kmp-lsp` | Microsoft java-debug, loaded into jdtls; `java-analyzer dap` with java-analyzer |
|
||||
| Kotlin | Kotlin LSP (JetBrains, `kotlin-lsp`) | Kotlin Language Server (fwcd, `kotlin-language-server`), `kmp-lsp` | Kotlin Debug Adapter (fwcd) |
|
||||
| Groovy, Gradle scripts | `groovy-language-server` (you provide it) | | |
|
||||
| JSP | none (highlighting only) | | |
|
||||
|
||||
A fallback starts only while the server listed before it is not installed. To pick one explicitly, open **Settings › Languages & Frameworks › Language Servers**, select either server and use **Language server for Java** (or Kotlin): **Automatic** uses the first one available, **None** turns code insight off. The choice can be global or for the current project.
|
||||
|
||||
`java-analyzer` is a native Java language server that needs no JVM to run. It imports Maven and Gradle projects itself (or a `java-analyzer.toml`), and also serves Run and Debug: Application configurations take their classpath from it, and debugging uses its own adapter (`java-analyzer dap`). OpenIDEA passes it the project JDK and the Maven local repository when they are set; otherwise it finds them itself.
|
||||
|
||||
`kmp-lsp` gives syntax-based Java code insight without running a JVM. It has no JDT compiler analysis, no Lombok support and none of the JDT refactorings. Building and running Java Application configurations, classpath discovery and the Java debugger need jdtls.
|
||||
|
||||
### What ships with the IDE
|
||||
@@ -43,6 +45,7 @@ When you open a Java or Kotlin file and its server is missing, a notification of
|
||||
| Kotlin LSP (JetBrains) | GitHub release of `Kotlin/kotlin-lsp` |
|
||||
| Kotlin Language Server (fwcd) | GitHub release of `fwcd/kotlin-language-server`; needs Java |
|
||||
| Kotlin Debug Adapter | GitHub release of `fwcd/kotlin-debug-adapter`; needs Java |
|
||||
| java-analyzer | GitHub release of `l0ng-ai/java-analyzer` |
|
||||
| kmp-lsp | GitHub release of `Hessesian/kmp-lsp` |
|
||||
|
||||
A `jdtls` already on your `PATH` (Homebrew, Scoop or a release archive) is used too. OpenIDEA starts it on the JVM directly instead of through its Python launcher script, so Python is not required. When you start a debug session without java-debug, the IDE offers to install it and restarts jdtls with the bundle.
|
||||
@@ -140,7 +143,7 @@ Navigation between MyBatis mapper interfaces and their XML statements is availab
|
||||
## Limitations
|
||||
|
||||
- One jdtls runs per project window. With several large projects open at once, memory adds up. Lower `-Xmx` in **JVM options** or close windows you are not using. **Share JDT LS across builds** helps only within one window.
|
||||
- Without jdtls (for example with `kmp-lsp` only), Application configurations, classpath-based features and Java debugging are not available.
|
||||
- With `kmp-lsp` only (no jdtls or java-analyzer), Application configurations, classpath-based features and Java debugging are not available.
|
||||
- Java tests run through Maven or Gradle. A project without either build tool has no test runner.
|
||||
- The jdtls compiler (ECJ) is stricter than javac in some places. Known case: a generic method call used directly as an `if`, `while`, `for` or `do` condition or a ternary condition — for example `if (getCacheObject(key))` with a `<T> T getCacheObject(String key)` method — compiles with javac but ECJ reports `Type mismatch: cannot convert from Object to boolean`. Assigning the call to a `boolean` variable first compiles on both (see [#2624](https://github.com/l0ng-ai/openidea/issues/2624)).
|
||||
- The Kotlin bridge is Maven only and exposes compiled Kotlin classes to Java. It does not give Java-to-Kotlin source navigation or refactoring.
|
||||
|
||||
@@ -38,7 +38,7 @@ flowchart LR
|
||||
## 服务端
|
||||
|
||||
```text
|
||||
openidea-server --stdio | --connect <host> <port> | --version
|
||||
openidea-server --stdio | --mcp-stdio [--project <path>] | --connect <host> <port> | --version
|
||||
```
|
||||
|
||||
- `--stdio` 通过 stdin/stdout 为一个桌面客户端提供服务。stdout 承载协议,所以日志写到 stderr(用 `OPENIDEA_LOG` 过滤)。
|
||||
@@ -78,12 +78,12 @@ openidea-server --stdio | --connect <host> <port> | --version
|
||||
|
||||
### 什么在哪里运行
|
||||
|
||||
| 远程机器上 | 桌面端 |
|
||||
| -------------------------------- | ---------------------------- |
|
||||
| 项目:文件、文档、设置 | 窗口、菜单、webview |
|
||||
| 语言服务器、索引、搜索 | 客户端 UI 设置、keymap、主题 |
|
||||
| Git、终端、运行、调试适配器 | SSH 连接和端口转发 |
|
||||
| 项目的代码插件及其进程和网络请求 | 插件密钥(钥匙串) |
|
||||
| 远程机器上 | 桌面端 |
|
||||
| -------------------------------------------- | ---------------------------- |
|
||||
| 项目:文件、文档、设置 | 窗口、菜单、webview |
|
||||
| 语言服务器、索引、搜索 | 客户端 UI 设置、keymap、主题 |
|
||||
| Git、终端、运行、调试适配器、内置 MCP 服务器 | SSH 连接和端口转发 |
|
||||
| 项目的代码插件及其进程和网络请求 | 插件密钥(钥匙串) |
|
||||
|
||||
## 端口转发
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ title: "数据库"
|
||||
description: "连接 SQL 数据库、浏览架构、在查询控制台中执行查询,以及查看、编辑、导入和导出数据。"
|
||||
---
|
||||
|
||||
**数据库** 工具窗口可以连接 SQLite、PostgreSQL、MySQL/MariaDB、Microsoft SQL Server、Oracle、ClickHouse 和 Snowflake,以及兼容这些协议的数据库。驱动内置在 IDE 中,不需要下载 JDBC 驱动。
|
||||
**数据库** 工具窗口可以连接 SQLite、PostgreSQL、MySQL/MariaDB、Microsoft SQL Server、Oracle、ClickHouse 和 Snowflake,以及兼容这些协议的数据库。这些预设使用 IDE 内置的驱动,也可以选择本地安装的厂商 JDBC 驱动。
|
||||
|
||||
通过 **工具 › 数据库** 或 **视图 › 工具窗口 › 数据库** 打开。**工具** 菜单中还有 **新建数据源…** 和 **Open Query Console**(打开查询控制台)。
|
||||
|
||||
@@ -23,6 +23,16 @@ description: "连接 SQL 数据库、浏览架构、在查询控制台中执行
|
||||
|
||||
预设会填好默认端口,必要时还会设置 SSL(例如 Redshift、Neon 和 Supabase 要求 SSL)。
|
||||
|
||||
## 自定义 JDBC 驱动
|
||||
|
||||
选择 **新建 › 数据源 › 自定义 JDBC**,然后选择 **管理驱动…**。为驱动配置命名,添加厂商提供的驱动 JAR 及依赖 JAR,填写完整的驱动类名和可选的 JDBC URL 模板。请仅导入可信来源的驱动。OpenIDEA 使用已安装的 Java 17 或更高版本运行时在单独的进程中加载驱动。 IDE 会读取 JAR 声明的驱动类,也支持手动输入完整类名。URL 模板支持 `{host}`、`{port}`、`{database}` 和 `{file}`,也可直接填写 JDBC URL。 URL 和高级属性会写入数据源配置,密码请填在密码栏,以使用系统凭据存储。“只读”选项会请求 JDBC 驱动建立只读连接。使用具备只读权限的数据库账号可强制限制写入。取消查询时,如果驱动停止了语句,会话及其临时表会保留。如果驱动在三秒内未响应,OpenIDEA 会关闭该会话。已提交的语句仍可能在服务器上完成。
|
||||
|
||||
选择驱动配置,填写厂商的 JDBC URL,在 **用户**、**密码** 和 **高级** 中设置凭据和驱动属性。通过密码字段输入的密码可以保存到系统凭据库。在 Linux 上,保存密码需要正在运行的 Secret Service;也可以仅为当前会话输入密码。**测试连接** 显示数据库产品和版本。可以通过 JDBC 元数据浏览模式、表和列,在 SQL 控制台中查询或以自动提交方式更新数据。自定义驱动不支持厂商特有的 DDL、执行计划或表格编辑。
|
||||
|
||||
文件数据库可使用绝对路径指定存储位置。相对路径以 OpenIDEA 应用数据目录下的持久化 `jdbc/files` 文件夹为起点。
|
||||
|
||||
驱动配置可以跨项目复用。在 IDE 外更改项目数据源的 URL 或连接属性后,需要在数据源对话框中检查并保存设置,才能重新连接。
|
||||
|
||||
## 添加数据源
|
||||
|
||||
在数据库工具窗口中点击 **+**(或在窗口内按 `⌘N` / `Alt+Insert`),然后选择:
|
||||
@@ -200,7 +210,7 @@ Oracle 的架构就是数据库用户;系统账户只在勾选 **显示内部
|
||||
|
||||
## 暂不支持
|
||||
|
||||
- MongoDB、Redis、BigQuery 等未列出的数据库,以及 JDBC 驱动。
|
||||
- MongoDB、Redis 和 BigQuery 的原生驱动。
|
||||
- SQL Server 的 `GO` 批处理分隔符。
|
||||
- “执行整个控制台”的操作(请选中后执行,或使用 **运行 SQL 脚本…**)。
|
||||
- 导出超出已加载页面的数据。
|
||||
|
||||
@@ -165,7 +165,7 @@ C 和 C++ 的配置也可以改用 GDB 14 及以上版本(`gdb -i dap`):
|
||||
|
||||
- **Java HotSwap**:**运行 › 调试操作 › 重新加载已更改的类** 编译改动,并把变化的类重新加载到正在运行的 Java 调试会话中。已经在执行的帧要等返回后才会用上新代码。保存后 JDT LS 自动完成构建时,IDE 也会提示是否重新加载。
|
||||
- **Maven 和 Gradle**:调试 Maven 或 Gradle 配置时,IDE 会让 JVM 带上 JDWP agent 启动,再用 java-debug 附加上去。
|
||||
- **Dart / Flutter**:Dart 会话运行时可以用 **热重载** 和 **热重启**。
|
||||
- **Dart / Flutter**:Dart 会话运行时可以用 **热重载** 和 **热重启**。在 Flutter 项目中,运行配置旁边的设备选择器决定运行和调试在哪台设备上启动应用;运行配置的 **设备** 字段可以固定使用某台设备。保存正在运行的 Flutter 调试会话里的 Dart 文件时也会自动热重载,文件有错误时跳过。可在 **设置 › 构建、执行、部署 › 调试器** 中关闭 **保存时热重载**。
|
||||
- **PHP**:如果 PHP 解释器没有加载 Xdebug,断点会被跳过;会话启动时 IDE 会提示。
|
||||
- **Python**:**设置 › 构建、执行、部署 › Python 调试器** 中有 **调试时自动附加到子进程** 和 **兼容 Gevent**。
|
||||
|
||||
|
||||
@@ -131,8 +131,11 @@ stdio 配置大致如下(路径以你机器上的为准):
|
||||
- **运行和构建会先询问。** `execute_run_configuration`、`build_project`、`run_command` 和 `http_run_request` 会在 OpenIDEA 中弹出对话框,说明是哪个客户端、哪个项目、哪个工具以及要运行什么,选择 **允许** 或 **拒绝**;多个请求会排队。
|
||||
- 会写入的工具拒绝项目之外的路径。
|
||||
|
||||
## 远程开发
|
||||
|
||||
在远程窗口([SSH](/zh/guide/remote/ssh)、WSL 或[容器](/zh/guide/remote/containers))中,`openidea-server` 会在远程主机上运行自己的 MCP 服务器,可在该远程窗口的 **Settings › Agents › IDE MCP Server** 中独立配置。远程窗口内启动的编程智能体会立即可用内置工具;在远程主机终端运行的外部客户端可使用 **Copy Stdio Config**(`~/.openidea/bin/openidea-server --mcp-stdio`)或 **Copy HTTP Config**,桌面端客户端则可在 **Ports** 工具窗口转发该端口后连接。
|
||||
|
||||
## 限制
|
||||
|
||||
- 远程项目(通过 SSH、服务器或容器打开的项目)暂不支持 MCP 服务器。
|
||||
- 服务器只为 OpenIDEA 中已打开的项目提供服务,关闭项目窗口后该项目的工具就不可用了。
|
||||
- 响应是普通 JSON,服务器不通过 SSE 推送流式结果。
|
||||
@@ -79,11 +79,12 @@ cargo build --release -p ide-host --bin openidea-server
|
||||
|
||||
这些参数是给桌面端用的,不过 `--version` 可以用来检查已安装的构建。
|
||||
|
||||
| 参数 | 含义 |
|
||||
| ------------------------- | -------------------------------------------------------------------------------- |
|
||||
| `--version` | 输出服务端版本和协议版本后退出。 |
|
||||
| `--stdio` | 通过标准输入输出服务一个桌面客户端,日志写到 stderr。 |
|
||||
| `--connect <host> <port>` | 通过标准输入输出中转一个到 `<host>:<port>` 的 TCP 连接,用于开发容器的端口转发。 |
|
||||
| 参数 | 含义 |
|
||||
| ------------------------------- | -------------------------------------------------------------------------------- |
|
||||
| `--version` | 输出服务端版本和协议版本后退出。 |
|
||||
| `--stdio` | 通过标准输入输出服务一个桌面客户端,日志写到 stderr。 |
|
||||
| `--mcp-stdio [--project <dir>]` | 将标准输入输出上的 MCP 消息转发给当前主机正在运行的 `openidea-server` MCP 服务。 |
|
||||
| `--connect <host> <port>` | 通过标准输入输出中转一个到 `<host>:<port>` 的 TCP 连接,用于开发容器的端口转发。 |
|
||||
|
||||
```bash
|
||||
ls ~/.openidea/server/*/*/openidea-server
|
||||
|
||||
@@ -15,10 +15,11 @@ gopls 负责 `.go`、`go.mod` 和 `go.work` 文件。工作区从最近的 `go.w
|
||||
|
||||
### Go SDK
|
||||
|
||||
**设置 › 语言和框架 › Go** 有两项设置:
|
||||
**设置 › 语言和框架 › Go** 有三项设置:
|
||||
|
||||
- **GOROOT**:gopls 以及 go run / test 运行配置使用的 Go SDK。选 **默认** 时使用 `PATH` 上的 `go`;如果项目通过 mise 或 asdf 固定了 Go 版本,则改用那个安装。
|
||||
- **项目 GOPATH**:留空时使用 `go env GOPATH`。
|
||||
- **使用 gofumpt 格式化**:重新格式化代码时,gopls 使用更严格的 [gofumpt](https://github.com/mvdan/gofumpt) 代替 gofmt(gopls 的 `gofumpt` 设置)。默认关闭。
|
||||
|
||||
GOROOT 列表会显示 IDE 找到的 SDK:`PATH` 上的 `go`、`/usr/local/go`、Homebrew 和 `/usr/lib/go` 的安装、`~/sdk` 下的 SDK(通过 `golang.org/dl` 安装)、Go 自己下载的工具链(`~/go/pkg/mod/golang.org/toolchain@…`),以及 mise 和 asdf 的安装。
|
||||
|
||||
|
||||
@@ -123,7 +123,7 @@ IDE 安装工具从不需要 `sudo`。工具装在本地数据目录下 IDE 自
|
||||
| HTTP Request | `http` | `.http`, `.rest` | 是 | — | — | — |
|
||||
| Ignore File | `ignore` | `.gitignore`, `.dockerignore` | — | — | — | — |
|
||||
| INI | `ini` | `.ini`, `.cfg`, `.conf`, `.cnf`, `.inf`, … | 是 | — | — | — |
|
||||
| Java | `java` | `.java` | 是 | 是 | **jdtls** → **kmp-lsp** | **java-debug** |
|
||||
| Java | `java` | `.java` | 是 | 是 | **jdtls** → **java-analyzer** → **kmp-lsp** | **java-debug** |
|
||||
| JavaScript | `javascript` | `.js`, `.mjs`, `.cjs` | 是 | 是 | **vtsls** → **typescript-language-server** + **oxlint** | **js-debug** |
|
||||
| JavaScript JSX | `javascriptreact` | `.jsx` | 是 | 是 | **vtsls** → **typescript-language-server** + **oxlint** | **js-debug** |
|
||||
| JSON | `json` | `.json`, `.geojson`, `.topojson`, `.webmanifest`, `.har`, … | 是 | 是 | **vscode-json-language-server** | — |
|
||||
|
||||
@@ -7,15 +7,17 @@ Java 的代码智能由 Eclipse JDT Language Server(jdtls)提供,OpenIDEA
|
||||
|
||||
## 语言服务器
|
||||
|
||||
| 语言 | 服务器 | 备选 | 调试器 |
|
||||
| ------------------- | -------------------------------------- | ------------------------------------------------------------------- | ------------------------------------- |
|
||||
| Java | Eclipse JDT Language Server(`jdtls`) | `kmp-lsp` | Microsoft java-debug,加载在 jdtls 内 |
|
||||
| Kotlin | Kotlin LSP(JetBrains,`kotlin-lsp`) | Kotlin Language Server(fwcd,`kotlin-language-server`)、`kmp-lsp` | Kotlin Debug Adapter(fwcd) |
|
||||
| Groovy、Gradle 脚本 | `groovy-language-server`(需自行提供) | | |
|
||||
| JSP | 无(仅语法高亮) | | |
|
||||
| 语言 | 服务器 | 备选 | 调试器 |
|
||||
| ------------------- | -------------------------------------- | ------------------------------------------------------------------- | -------------------------------------------------------------------------------- |
|
||||
| Java | Eclipse JDT Language Server(`jdtls`) | `java-analyzer`、`kmp-lsp` | Microsoft java-debug,加载在 jdtls 内;用 java-analyzer 时为 `java-analyzer dap` |
|
||||
| Kotlin | Kotlin LSP(JetBrains,`kotlin-lsp`) | Kotlin Language Server(fwcd,`kotlin-language-server`)、`kmp-lsp` | Kotlin Debug Adapter(fwcd) |
|
||||
| Groovy、Gradle 脚本 | `groovy-language-server`(需自行提供) | | |
|
||||
| JSP | 无(仅语法高亮) | | |
|
||||
|
||||
备选服务器只在排在它前面的服务器未安装时才会启动。想手动指定,打开 **设置 › 语言和框架 › 语言服务器**,选中其中一个服务器,在 **Java 语言服务器**(或 Kotlin 语言服务器)里选择:**自动** 使用第一个可用的,**无** 关闭代码智能。这个选择可以全局生效,也可以只对当前项目生效。
|
||||
|
||||
`java-analyzer` 是原生的 Java 语言服务器,运行时不需要 JVM。它自己导入 Maven 和 Gradle 项目(或 `java-analyzer.toml`),也支撑运行和调试:“应用程序”配置从它获取类路径,调试使用它自带的调试适配器(`java-analyzer dap`)。设置了项目 JDK 和 Maven 本地仓库时,OpenIDEA 会把它们传给 java-analyzer,否则它自己查找。
|
||||
|
||||
`kmp-lsp` 不运行 JVM,提供基于语法的 Java 代码智能,没有 JDT 的编译分析、Lombok 支持和 JDT 的重构。构建和运行 Java “应用程序”配置、类路径发现以及 Java 调试器都需要 jdtls。
|
||||
|
||||
### 随 IDE 提供的部分
|
||||
@@ -43,6 +45,7 @@ Java 的代码智能由 Eclipse JDT Language Server(jdtls)提供,OpenIDEA
|
||||
| Kotlin LSP(JetBrains) | `Kotlin/kotlin-lsp` 的 GitHub release |
|
||||
| Kotlin Language Server(fwcd) | `fwcd/kotlin-language-server` 的 GitHub release,需要 Java |
|
||||
| Kotlin Debug Adapter | `fwcd/kotlin-debug-adapter` 的 GitHub release,需要 Java |
|
||||
| java-analyzer | `l0ng-ai/java-analyzer` 的 GitHub release |
|
||||
| kmp-lsp | `Hessesian/kmp-lsp` 的 GitHub release |
|
||||
|
||||
`PATH` 上已有的 `jdtls`(Homebrew、Scoop 或 release 压缩包)也能直接用。OpenIDEA 直接在 JVM 上启动它,不经过它自带的 Python 启动脚本,所以不需要装 Python。没装 java-debug 就开始调试时,IDE 会提示安装,并带着这个 bundle 重启 jdtls。
|
||||
@@ -140,7 +143,7 @@ MyBatis mapper 接口与 XML 语句之间的跳转由 MyBatis Navigator 插件
|
||||
## 已知限制
|
||||
|
||||
- 每个项目窗口运行一个 jdtls,同时打开多个大项目时内存会叠加。可以在 **JVM 选项** 中调低 `-Xmx`,或关闭暂时不用的窗口。**在构建间共享 JDT LS** 只在单个窗口内有效。
|
||||
- 没有 jdtls 时(例如只用 `kmp-lsp`),“应用程序”配置、依赖类路径的功能和 Java 调试都不可用。
|
||||
- 只用 `kmp-lsp`(没有 jdtls 或 java-analyzer)时,“应用程序”配置、依赖类路径的功能和 Java 调试都不可用。
|
||||
- Java 测试通过 Maven 或 Gradle 运行,两者都没有的项目没有测试运行器。
|
||||
- jdtls 自带的 ECJ 编译器有些地方比 javac 严格。已知例子:把泛型方法调用直接用作 `if`、`while`、`for`、`do` 条件或三元运算符条件——例如 `<T> T getCacheObject(String key)` 方法配合 `if (getCacheObject(key))`——javac 可以编译,ECJ 则报 `Type mismatch: cannot convert from Object to boolean`。先把调用赋值给 `boolean` 变量即可两边都编译通过(见 [#2624](https://github.com/l0ng-ai/openidea/issues/2624))。
|
||||
- Kotlin 桥接只支持 Maven,只把编译后的 Kotlin 类暴露给 Java,不提供从 Java 到 Kotlin 源码的跳转或重构。
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
/**
|
||||
* The Flutter device selector next to the run configuration. The daemon is stubbed at the IPC
|
||||
* layer; `crates/ide-flutter` covers its protocol and process.
|
||||
*/
|
||||
import type { Page } from "@playwright/test";
|
||||
import { expect, ipcCalls, stubDesktop, test } from "./fixtures";
|
||||
|
||||
const device = (id: string, name: string, category: string, unavailable: string | null = null) => ({
|
||||
id,
|
||||
name,
|
||||
platformType: null,
|
||||
category,
|
||||
emulator: false,
|
||||
unavailable,
|
||||
});
|
||||
const MACOS = device("macos", "macOS", "desktop");
|
||||
const CHROME = device("chrome", "Chrome", "web");
|
||||
const UNAUTHORIZED = device("R5C", "R5C", "mobile", "unauthorized");
|
||||
|
||||
async function stubFlutter(page: Page, status: unknown = { kind: "ready" }) {
|
||||
await stubDesktop(page);
|
||||
await page.evaluate(
|
||||
async ({ list }) => {
|
||||
const config = {
|
||||
id: "flutter:run",
|
||||
name: "flutter run",
|
||||
kind: "flutter",
|
||||
command: "flutter",
|
||||
args: ["run", "-t", "lib/main.dart"],
|
||||
cwd: null,
|
||||
env: {},
|
||||
detected: true,
|
||||
isTest: false,
|
||||
debugAdapter: "dart",
|
||||
};
|
||||
const h = window.__ipc.handlers;
|
||||
h.run_configs = () => [config];
|
||||
h.run_apply_settings = (args) => args.configs;
|
||||
h.flutter_devices = () => list;
|
||||
h.flutter_devices_refresh = () => null;
|
||||
h.run_start = () => ({ id: 3, configId: config.id, name: config.name, pid: 42, running: true, pty: false });
|
||||
const runModule = "/src/features/run/store.ts";
|
||||
const { loadConfigs, selectConfig } = await import(/* @vite-ignore */ runModule);
|
||||
await loadConfigs();
|
||||
selectConfig(config.id);
|
||||
},
|
||||
{ list: { status, devices: [MACOS, CHROME, UNAUTHORIZED] } },
|
||||
);
|
||||
}
|
||||
|
||||
const chooser = (page: Page) => page.getByRole("button", { name: /^Flutter device:/ });
|
||||
|
||||
test("the chosen device is used for Run, and replaced while it is disconnected", async ({ workbench: page }) => {
|
||||
await stubFlutter(page);
|
||||
await expect(chooser(page)).toContainText("macOS");
|
||||
|
||||
await chooser(page).click();
|
||||
await expect(page.getByRole("menuitemcheckbox", { name: "R5C (unauthorized)" })).toBeDisabled();
|
||||
await page.getByRole("menuitemcheckbox", { name: "Chrome" }).click();
|
||||
await expect(chooser(page)).toContainText("Chrome");
|
||||
|
||||
await page.keyboard.press("Control+r");
|
||||
await expect.poll(async () => (await ipcCalls(page, "run_start")).length).toBe(1);
|
||||
const [started] = await ipcCalls(page, "run_start");
|
||||
expect((started!.config as { args: string[] }).args).toEqual(["run", "-t", "lib/main.dart", "-d", "chrome"]);
|
||||
|
||||
const emit = (devices: unknown[]) =>
|
||||
page.evaluate(
|
||||
(devices) => window.__ipc.emit({ type: "flutterDevices", list: { status: { kind: "ready" }, devices } }),
|
||||
devices,
|
||||
);
|
||||
await emit([MACOS]);
|
||||
await expect(chooser(page)).toContainText("macOS");
|
||||
await emit([MACOS, CHROME]);
|
||||
await expect(chooser(page)).toContainText("Chrome");
|
||||
});
|
||||
|
||||
test("says when the Flutter SDK is missing", async ({ workbench: page }) => {
|
||||
await stubFlutter(page, { kind: "notInstalled" });
|
||||
await expect(chooser(page)).toContainText("Flutter SDK not found");
|
||||
});
|
||||
@@ -0,0 +1,122 @@
|
||||
import { expect, ipcCalls, stubDesktop, test } from "./fixtures";
|
||||
|
||||
test("configures a local JDBC profile and a source through mocked IPC", async ({ workbench: page }, info) => {
|
||||
await stubDesktop(page);
|
||||
await page.evaluate(async () => {
|
||||
const drivers: Record<string, unknown>[] = [];
|
||||
let sources: Record<string, unknown>[] = [];
|
||||
Object.assign(window.__ipc.handlers, {
|
||||
db_jdbc_drivers: () => drivers,
|
||||
db_jdbc_driver_classes: () => ["org.example.jdbc.Driver"],
|
||||
db_save_jdbc_driver: ({ driver }: { driver: Record<string, unknown> }) => {
|
||||
const at = drivers.findIndex((d) => d.id === driver.id);
|
||||
if (at < 0) drivers.push(driver);
|
||||
else drivers[at] = driver;
|
||||
return null;
|
||||
},
|
||||
db_remove_jdbc_driver: ({ id }: { id: string }) => {
|
||||
drivers.splice(
|
||||
drivers.findIndex((d) => d.id === id),
|
||||
1,
|
||||
);
|
||||
return null;
|
||||
},
|
||||
file_dialog_needs_gtk: () => false,
|
||||
"plugin:dialog|open": () => ["/mock/driver.jar", "/mock/dependency.jar"],
|
||||
db_test: () => "Mock JDBC database",
|
||||
db_approve_jdbc_source: () => null,
|
||||
db_save_sources: ({ sources: value }: { sources: Record<string, unknown>[] }) => {
|
||||
sources = value;
|
||||
return null;
|
||||
},
|
||||
db_sources: () => sources,
|
||||
db_connected: () => [],
|
||||
db_set_password: () => null,
|
||||
});
|
||||
const storePath = "/src/features/database/store.ts";
|
||||
const { openSourceDialog } = await import(/* @vite-ignore */ storePath);
|
||||
openSourceDialog(null, {
|
||||
driver: "clickhouse",
|
||||
name: "Mock connection",
|
||||
host: "example.invalid",
|
||||
port: 8443,
|
||||
ssl: "verifyFull",
|
||||
sslCaFile: "/mock/ca.pem",
|
||||
sslCertFile: "/mock/client.pem",
|
||||
sslKeyFile: "/mock/client.key",
|
||||
});
|
||||
});
|
||||
const source = page.getByRole("dialog", { name: "New Data Source", exact: true });
|
||||
await source.getByRole("tab", { name: "SSH/SSL", exact: true }).click();
|
||||
await source.screenshot({
|
||||
path: info.outputPath("mock-native-clickhouse-before-switch.png"),
|
||||
animations: "disabled",
|
||||
});
|
||||
await source.getByRole("tab", { name: "General", exact: true }).click();
|
||||
await source.getByRole("button", { name: "Data source type", exact: true }).click();
|
||||
await page.getByRole("option", { name: "Custom JDBC", exact: true }).click();
|
||||
await expect(source.getByRole("tab", { name: "SSH/SSL", exact: true })).toHaveCount(0);
|
||||
await expect(source.locator("#db-url")).toHaveValue("");
|
||||
await source.getByRole("button", { name: "Manage Drivers…", exact: true }).click();
|
||||
const profiles = page.getByRole("dialog", { name: "JDBC Driver Profiles", exact: true });
|
||||
await profiles.getByRole("button", { name: "Add…", exact: true }).click();
|
||||
const editor = page.getByRole("dialog", { name: "Add JDBC Driver", exact: true });
|
||||
await editor.getByLabel("Name:", { exact: true }).fill("Example local driver");
|
||||
await editor.getByRole("button", { name: "Add JARs…", exact: true }).click();
|
||||
await expect(editor.getByText("dependency.jar", { exact: true })).toBeVisible();
|
||||
await expect(editor.getByLabel("Driver class:", { exact: true })).toHaveValue("org.example.jdbc.Driver");
|
||||
await editor.getByLabel("URL template:", { exact: true }).fill("jdbc:example://{host}/{database}");
|
||||
await editor.screenshot({ path: info.outputPath("jdbc-profile-editor.png"), animations: "disabled" });
|
||||
await editor.getByRole("button", { name: "Save", exact: true }).click();
|
||||
const trust = page.getByRole("dialog", { name: "Trust JDBC Driver?", exact: true });
|
||||
await expect(trust).toContainText("execute local code with your user permissions");
|
||||
expect(await ipcCalls(page, "db_save_jdbc_driver")).toHaveLength(0);
|
||||
await trust.screenshot({ path: info.outputPath("mock-jdbc-driver-trust.png"), animations: "disabled" });
|
||||
await trust.getByRole("button", { name: "Trust and Save", exact: true }).click();
|
||||
await profiles.getByRole("button", { name: "Use Driver", exact: true }).click();
|
||||
await expect(source.getByRole("button", { name: "JDBC driver profile", exact: true })).toHaveText(
|
||||
"Example local driver",
|
||||
);
|
||||
await source.getByLabel("Host:", { exact: true }).fill("example.invalid");
|
||||
await source.getByLabel("Database:", { exact: true }).fill("catalog");
|
||||
await expect(source.locator("#db-url")).toHaveValue("jdbc:example://example.invalid/catalog");
|
||||
await source.screenshot({ path: info.outputPath("jdbc-template.png"), animations: "disabled" });
|
||||
const url = "jdbc:example://example.invalid/catalog?ssl=true";
|
||||
await source.locator("#db-url").fill(url);
|
||||
const test = source.getByRole("button", { name: "Test Connection", exact: true });
|
||||
await expect(test).toBeDisabled();
|
||||
await source.getByRole("checkbox", { name: "I have configured JDBC connection security.", exact: true }).check();
|
||||
await source.locator("#db-user").fill("example-user");
|
||||
await source.locator("#db-password").fill("mock secret");
|
||||
await source.screenshot({ path: info.outputPath("mock-jdbc-source-after-switch.png"), animations: "disabled" });
|
||||
await source.getByRole("tab", { name: "Options", exact: true }).click();
|
||||
await expect(source.getByRole("button", { name: "Transaction control", exact: true })).toHaveCount(0);
|
||||
await source.getByRole("checkbox", { name: "Read-only", exact: true }).check();
|
||||
await expect(
|
||||
source.getByText("For read-only access, use a database account with read-only privileges."),
|
||||
).toBeVisible();
|
||||
await source.screenshot({ path: info.outputPath("jdbc-read-only.png"), animations: "disabled" });
|
||||
await source.getByRole("tab", { name: "Advanced", exact: true }).click();
|
||||
await source.getByRole("button", { name: "Add Property", exact: true }).click();
|
||||
await source.getByLabel("Property name", { exact: true }).fill("ssl");
|
||||
await source.getByLabel("Property value", { exact: true }).fill("true");
|
||||
await test.click();
|
||||
await expect(source.getByRole("status")).toHaveText("Succeeded: Mock JDBC database");
|
||||
const [{ source: tested, password }] = await ipcCalls(page, "db_test");
|
||||
expect(password).toBe("mock secret");
|
||||
expect(tested).toMatchObject({
|
||||
driver: "jdbc",
|
||||
url,
|
||||
user: "example-user",
|
||||
readOnly: true,
|
||||
properties: [{ name: "ssl", value: "true" }],
|
||||
});
|
||||
await source.getByRole("button", { name: "OK", exact: true }).click();
|
||||
await expect(source).not.toBeVisible();
|
||||
const [{ sources }] = await ipcCalls(page, "db_save_sources");
|
||||
const [saved] = sources as Record<string, unknown>[];
|
||||
const [{ driver }] = await ipcCalls(page, "db_save_jdbc_driver");
|
||||
expect(saved.readOnly).toBe(true);
|
||||
expect(saved.jdbcDriver).toBe((driver as Record<string, unknown>).id);
|
||||
for (const key of ["jars", "className", "classpath", "exec", "password"]) expect(saved).not.toHaveProperty(key);
|
||||
});
|
||||
@@ -0,0 +1,32 @@
|
||||
"""Build the committed Java 17 JDBC bridge, or run its standalone protocol tests."""
|
||||
import argparse
|
||||
import os
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import tempfile
|
||||
import zipfile
|
||||
|
||||
root = Path(__file__).resolve().parent
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("--test", action="store_true")
|
||||
parser.add_argument("--out", type=Path, help="output JAR (with --test: the fixture driver for JDBC_FIXTURE_JAR)")
|
||||
args = parser.parse_args()
|
||||
java_home = os.environ.get("JAVA_HOME")
|
||||
def tool(name):
|
||||
return str(Path(java_home) / "bin" / name) if java_home else name
|
||||
|
||||
with tempfile.TemporaryDirectory() as scratch:
|
||||
classes = Path(scratch) / "classes"
|
||||
classes.mkdir()
|
||||
sources = sorted((root / "src").rglob("*.java"))
|
||||
if args.test:
|
||||
sources += sorted((root / "tests").glob("*.java"))
|
||||
subprocess.run([tool("javac"), "--release", "17", "-encoding", "UTF-8", "-g:none",
|
||||
"-d", classes, *sources], check=True)
|
||||
if args.test:
|
||||
subprocess.run([tool("java"), "-ea", "-cp", classes, "ai.openidea.jdbc.SelfTest"], check=True, timeout=45)
|
||||
if not args.test or args.out:
|
||||
with zipfile.ZipFile(args.out or root / "openidea-jdbc-bridge.jar", "w") as jar:
|
||||
for path in sorted(classes.rglob("SelfTest*.class" if args.test else "*.class")):
|
||||
entry = zipfile.ZipInfo(path.relative_to(classes).as_posix(), (2026, 1, 1, 0, 0, 0))
|
||||
jar.writestr(entry, path.read_bytes(), compress_type=zipfile.ZIP_DEFLATED)
|
||||
Бінарний файл не відображається.
@@ -0,0 +1,348 @@
|
||||
// The JDBC driver lives in this process only. The parent owns its lifetime and sends
|
||||
// credentials over stdin. Driver console output must never enter the reply stream.
|
||||
package ai.openidea.jdbc;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.sql.*;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.*;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
public final class OpenIdeaJdbc {
|
||||
static final int LIMIT = 16 * 1024 * 1024;
|
||||
final DataInputStream input = new DataInputStream(new BufferedInputStream(System.in));
|
||||
final DataOutputStream output = new DataOutputStream(new BufferedOutputStream(System.out));
|
||||
Connection connection;
|
||||
boolean catalogs;
|
||||
boolean namespaceKnown;
|
||||
|
||||
static void text(DataOutputStream out, String value) throws IOException {
|
||||
// Leave room for the two flags and update count that terminate a result.
|
||||
int available = LIMIT - out.size() - Integer.BYTES - 2 - Long.BYTES;
|
||||
if (available < 0 || (value != null && value.length() > available))
|
||||
throw new IOException("JDBC result exceeds 16 MiB; select fewer or smaller values");
|
||||
if (value == null) { out.writeInt(-1); return; }
|
||||
byte[] data = value.getBytes(StandardCharsets.UTF_8);
|
||||
if (data.length > available) throw new IOException("JDBC result exceeds 16 MiB; select fewer or smaller values");
|
||||
out.writeInt(data.length);
|
||||
out.write(data);
|
||||
}
|
||||
|
||||
static void header(DataOutputStream out, String... names) throws IOException {
|
||||
out.writeInt(names.length);
|
||||
for (String name : names) { text(out, name); text(out, "VARCHAR"); }
|
||||
}
|
||||
|
||||
static void row(DataOutputStream out, String... cells) throws IOException {
|
||||
out.writeBoolean(true);
|
||||
for (String cell : cells) text(out, cell);
|
||||
}
|
||||
|
||||
static void end(DataOutputStream out, boolean more, long affected) throws IOException {
|
||||
out.writeBoolean(false);
|
||||
out.writeBoolean(more);
|
||||
out.writeLong(affected);
|
||||
}
|
||||
|
||||
private static class UnsupportedUrl extends SQLException {}
|
||||
|
||||
private static class ReadOnlyUnavailable extends SQLException {}
|
||||
|
||||
void open(List<String> a, DataOutputStream out) throws Exception {
|
||||
String className = a.get(0), url = a.get(1);
|
||||
Driver driver = Class.forName(className).asSubclass(Driver.class).getConstructor().newInstance();
|
||||
var props = new Properties();
|
||||
for (int i = 5; i < a.size(); i += 2) props.setProperty(a.get(i), a.get(i+1));
|
||||
if (!a.get(2).isEmpty()) props.setProperty("user", a.get(2));
|
||||
if (!a.get(3).isEmpty()) props.setProperty("password", a.get(3));
|
||||
connection = driver.connect(url, props);
|
||||
if (connection == null) throw new UnsupportedUrl();
|
||||
if (Boolean.parseBoolean(a.get(4))) {
|
||||
try { connection.setReadOnly(true); }
|
||||
catch (SQLException | UnsupportedOperationException e) {
|
||||
throw new ReadOnlyUnavailable();
|
||||
}
|
||||
}
|
||||
var m = connection.getMetaData();
|
||||
header(out, "version", "quote");
|
||||
row(out, m.getDatabaseProductName() + " " + m.getDatabaseProductVersion(), m.getIdentifierQuoteString());
|
||||
}
|
||||
|
||||
static String pattern(DatabaseMetaData m, String literal) throws SQLException {
|
||||
String escape = m.getSearchStringEscape();
|
||||
if (escape == null || escape.isEmpty()) return literal;
|
||||
return literal.replace(escape, escape + escape).replace("%", escape + "%").replace("_", escape + "_");
|
||||
}
|
||||
|
||||
void schemas(DataOutputStream out) throws Exception {
|
||||
var m = connection.getMetaData();
|
||||
header(out, "schema");
|
||||
Set<String> names = new TreeSet<>();
|
||||
try (ResultSet rs = m.getSchemas()) {
|
||||
while (rs.next() && names.size() < 20000) {
|
||||
String s = rs.getString("TABLE_SCHEM");
|
||||
if (s != null && !s.isEmpty()) names.add(s);
|
||||
}
|
||||
} catch (SQLFeatureNotSupportedException ignored) {}
|
||||
catalogs = names.isEmpty();
|
||||
namespaceKnown = true;
|
||||
if (catalogs) {
|
||||
try (ResultSet rs = m.getCatalogs()) {
|
||||
while (rs.next() && names.size() < 20000) {
|
||||
String s = rs.getString(1);
|
||||
if (s != null && !s.isEmpty()) names.add(s);
|
||||
}
|
||||
} catch (SQLFeatureNotSupportedException ignored) {}
|
||||
}
|
||||
if (names.isEmpty()) names.add("");
|
||||
for (String s : names) row(out, s);
|
||||
}
|
||||
|
||||
void discoverNamespace() throws Exception {
|
||||
if (!namespaceKnown) schemas(new DataOutputStream(OutputStream.nullOutputStream()));
|
||||
}
|
||||
|
||||
void metadata(String op, List<String> a, DataOutputStream out) throws Exception {
|
||||
discoverNamespace();
|
||||
var m = connection.getMetaData();
|
||||
String name = a.get(0);
|
||||
String catalog = catalogs && !name.isEmpty() ? name : connection.getCatalog();
|
||||
String schema = catalogs || name.isEmpty() ? null : pattern(m, name);
|
||||
if (op.equals("tables")) {
|
||||
header(out, "name", "kind");
|
||||
try (ResultSet rs = m.getTables(catalog, schema, "%", null)) {
|
||||
int n = 0;
|
||||
while (rs.next() && n < 20000) {
|
||||
String type = rs.getString("TABLE_TYPE").toLowerCase(Locale.ROOT);
|
||||
if (!Set.of("table","base table","view","materialized view","system table","local temporary","foreign table").contains(type)) continue;
|
||||
row(out, rs.getString("TABLE_NAME"), type);
|
||||
n++;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
header(out, "name", "type", "nullable", "default", "primary");
|
||||
String table = a.get(1);
|
||||
Set<String> keys = new HashSet<>();
|
||||
try (ResultSet rs = m.getPrimaryKeys(catalog, catalogs || name.isEmpty() ? null : name, table)) {
|
||||
while (rs.next()) keys.add(rs.getString("COLUMN_NAME"));
|
||||
} catch (SQLFeatureNotSupportedException ignored) {}
|
||||
try (ResultSet rs = m.getColumns(catalog, schema, pattern(m, table), "%")) {
|
||||
int n = 0;
|
||||
while (rs.next() && n < 20000) {
|
||||
if (!table.equals(rs.getString("TABLE_NAME"))) continue;
|
||||
String col = rs.getString("COLUMN_NAME");
|
||||
row(out, col, rs.getString("TYPE_NAME"), rs.getInt("NULLABLE") == 0 ? "0" : "1",
|
||||
rs.getString("COLUMN_DEF"), keys.contains(col) ? "1" : "0");
|
||||
n++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void execute(Work work, List<String> a, DataOutputStream out, boolean tablePage) throws Exception {
|
||||
long skip = Long.parseLong(a.get(1));
|
||||
int limit = Integer.parseInt(a.get(2));
|
||||
try (Statement st = connection.createStatement()) {
|
||||
long window = skip + limit + 1;
|
||||
if (tablePage && window > 0 && window <= Integer.MAX_VALUE) {
|
||||
try { st.setMaxRows((int)window); } catch (SQLException | UnsupportedOperationException ignored) {}
|
||||
}
|
||||
work.begin(st);
|
||||
if (!st.execute(a.get(0))) {
|
||||
header(out);
|
||||
end(out, false, st.getUpdateCount());
|
||||
return;
|
||||
}
|
||||
try (ResultSet rs = st.getResultSet()) {
|
||||
var m = rs.getMetaData();
|
||||
int cols = m.getColumnCount();
|
||||
out.writeInt(cols);
|
||||
for (int c = 1; c <= cols; c++) { text(out, m.getColumnLabel(c)); text(out, m.getColumnTypeName(c)); }
|
||||
while (skip > 0) {
|
||||
if (!rs.next()) { end(out, false, -1); return; }
|
||||
skip--;
|
||||
}
|
||||
int n = 0;
|
||||
while (n < limit && rs.next()) {
|
||||
out.writeBoolean(true);
|
||||
for (int c = 1; c <= cols; c++) {
|
||||
int t = m.getColumnType(c);
|
||||
if (t == Types.BINARY || t == Types.VARBINARY || t == Types.LONGVARBINARY || t == Types.BLOB) {
|
||||
byte[] b = rs.getBytes(c);
|
||||
if (b != null && b.length > (LIMIT - out.size()) / 2) throw new IOException("JDBC binary value exceeds the result limit");
|
||||
text(out, b == null ? null : "0x" + HexFormat.of().formatHex(b));
|
||||
} else {
|
||||
text(out, rs.getString(c));
|
||||
}
|
||||
}
|
||||
n++;
|
||||
}
|
||||
end(out, n == limit && rs.next(), -1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Only the query worker uses the connection. The reader may cancel its current statement.
|
||||
static final class Work {
|
||||
final long id;
|
||||
private Statement statement;
|
||||
private boolean cancelled;
|
||||
|
||||
Work(long id) { this.id = id; }
|
||||
|
||||
synchronized void begin(Statement st) throws SQLException {
|
||||
if (cancelled) throw new SQLException("Cancelled");
|
||||
statement = st;
|
||||
}
|
||||
|
||||
void cancel() {
|
||||
Statement st;
|
||||
synchronized (this) {
|
||||
cancelled = true;
|
||||
st = statement;
|
||||
}
|
||||
if (st != null) {
|
||||
try { st.cancel(); } catch (SQLException | UnsupportedOperationException ignored) {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static List<String> readFields(DataInputStream in, int count) throws IOException {
|
||||
if (count < 1 || count > 8192) throw new IOException("Invalid JDBC field count");
|
||||
int remaining = LIMIT - Long.BYTES - Integer.BYTES;
|
||||
var fields = new ArrayList<String>(count);
|
||||
for (int i = 0; i < count; i++) {
|
||||
remaining -= Integer.BYTES;
|
||||
int n = in.readInt();
|
||||
if (n < 0 || n > remaining) throw new IOException("Invalid JDBC request length");
|
||||
byte[] bytes = in.readNBytes(n);
|
||||
if (bytes.length != n) throw new EOFException();
|
||||
fields.add(new String(bytes, StandardCharsets.UTF_8));
|
||||
remaining -= n;
|
||||
}
|
||||
return fields;
|
||||
}
|
||||
|
||||
ByteArrayOutputStream reply(Work work, List<String> a) throws IOException {
|
||||
var buffer = new ByteArrayOutputStream();
|
||||
var reply = new DataOutputStream(buffer);
|
||||
String op = a.remove(0);
|
||||
try {
|
||||
reply.writeBoolean(true);
|
||||
switch (op) {
|
||||
case "open" -> open(a, reply);
|
||||
case "schemas" -> schemas(reply);
|
||||
case "tables", "columns" -> metadata(op, a, reply);
|
||||
case "namespace" -> {
|
||||
header(reply, "namespace");
|
||||
String name = null;
|
||||
try {
|
||||
discoverNamespace();
|
||||
name = catalogs ? connection.getCatalog() : connection.getSchema();
|
||||
} catch (SQLException | AbstractMethodError ignored) {}
|
||||
row(reply, name);
|
||||
}
|
||||
case "page" -> execute(work, a, reply, true);
|
||||
case "query" -> {
|
||||
if (a.size() == 3 || a.get(3).isEmpty()) {
|
||||
execute(work, a, reply, false);
|
||||
} else {
|
||||
discoverNamespace();
|
||||
String previous = catalogs ? connection.getCatalog() : connection.getSchema();
|
||||
String wanted = a.get(3);
|
||||
if (Objects.equals(previous, wanted)) {
|
||||
execute(work, a, reply, false);
|
||||
} else {
|
||||
try {
|
||||
if (catalogs) connection.setCatalog(wanted); else connection.setSchema(wanted);
|
||||
if (!Objects.equals(wanted, catalogs ? connection.getCatalog() : connection.getSchema()))
|
||||
throw new SQLException("The JDBC driver cannot switch to the selected schema");
|
||||
execute(work, a, reply, false);
|
||||
} finally {
|
||||
try {
|
||||
if (catalogs) connection.setCatalog(previous); else connection.setSchema(previous);
|
||||
} catch (SQLException restoreError) {
|
||||
connection.close();
|
||||
throw restoreError;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
default -> throw new IOException("Unknown JDBC operation");
|
||||
}
|
||||
if (!op.equals("query") && !op.equals("page")) end(reply, false, -1);
|
||||
} catch (Exception | LinkageError e) {
|
||||
buffer = new ByteArrayOutputStream();
|
||||
reply = new DataOutputStream(buffer);
|
||||
reply.writeBoolean(false);
|
||||
if ((e instanceof ClassNotFoundException || e instanceof NoClassDefFoundError)
|
||||
&& e.getMessage() != null && e.getMessage().matches("[A-Za-z0-9_.$/]+")) {
|
||||
text(reply, "JDBC class " + e.getMessage() + " was not found. Check the driver class and include its dependency JARs.");
|
||||
} else if (e instanceof ReadOnlyUnavailable) {
|
||||
text(reply, "The JDBC driver rejected Read-only. Check the driver settings and database account privileges.");
|
||||
} else if (e instanceof UnsupportedUrl) {
|
||||
text(reply, "The selected JDBC driver does not accept this URL");
|
||||
} else if (op.equals("open")) {
|
||||
// Login exceptions may contain properties or credentials embedded in a vendor URL.
|
||||
String state = e instanceof SQLException ? ((SQLException)e).getSQLState() : null;
|
||||
text(reply, "JDBC connection failed (" + e.getClass().getSimpleName()
|
||||
+ (state == null ? "" : ", SQLState " + state) + "). Check driver files, class, URL and credentials.");
|
||||
} else {
|
||||
text(reply, e.getMessage() == null ? e.getClass().getSimpleName() : e.getMessage());
|
||||
}
|
||||
}
|
||||
return buffer;
|
||||
}
|
||||
|
||||
synchronized void send(ByteArrayOutputStream reply) throws IOException {
|
||||
output.writeInt(reply == null ? 0 : reply.size());
|
||||
if (reply != null) reply.writeTo(output);
|
||||
output.flush();
|
||||
}
|
||||
|
||||
void run() throws IOException {
|
||||
System.setOut(System.err);
|
||||
var active = new AtomicReference<Work>();
|
||||
var worker = Executors.newSingleThreadExecutor(r -> {
|
||||
var thread = new Thread(r, "jdbc-session");
|
||||
thread.setDaemon(true);
|
||||
return thread;
|
||||
});
|
||||
try {
|
||||
for (;;) {
|
||||
long id;
|
||||
try { id = input.readLong(); } catch (EOFException eof) { return; }
|
||||
int count = input.readInt();
|
||||
if (count == 0) {
|
||||
var work = active.get();
|
||||
if (work != null && work.id == id) work.cancel();
|
||||
send(null); // Acknowledge even a cancel that raced with a completed request.
|
||||
continue;
|
||||
}
|
||||
var fields = readFields(input, count);
|
||||
var work = new Work(id);
|
||||
if (!active.compareAndSet(null, work)) throw new IOException("Overlapping JDBC requests");
|
||||
worker.execute(() -> {
|
||||
try {
|
||||
var result = reply(work, fields);
|
||||
active.compareAndSet(work, null);
|
||||
send(result);
|
||||
} catch (Throwable fatal) {
|
||||
// Without a complete frame the parent must discard this session.
|
||||
Runtime.getRuntime().halt(1);
|
||||
}
|
||||
});
|
||||
}
|
||||
} finally {
|
||||
worker.shutdownNow();
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
var bridge = new OpenIdeaJdbc();
|
||||
try { bridge.run(); }
|
||||
finally { if (bridge.connection != null) bridge.connection.close(); }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,129 @@
|
||||
package ai.openidea.jdbc;
|
||||
|
||||
import java.io.*;
|
||||
import java.lang.reflect.Proxy;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Path;
|
||||
import java.sql.*;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/** Drives the bridge over its actual pipes with a deterministic JDBC driver. */
|
||||
public class SelfTest {
|
||||
public static class Fixture implements Driver {
|
||||
private boolean readonly;
|
||||
private int changes;
|
||||
private Path started;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
static <T> T proxy(Class<T> type, java.lang.reflect.InvocationHandler handler) {
|
||||
return (T) Proxy.newProxyInstance(type.getClassLoader(), new Class<?>[]{type}, handler);
|
||||
}
|
||||
|
||||
public Connection connect(String url, Properties props) {
|
||||
String path = url.substring("jdbc:fixture:".length());
|
||||
started = path.isEmpty() ? null : Path.of(path);
|
||||
var meta = proxy(DatabaseMetaData.class, (p, m, a) -> switch (m.getName()) {
|
||||
case "getDatabaseProductName" -> "Fixture";
|
||||
case "getDatabaseProductVersion" -> "1";
|
||||
case "getIdentifierQuoteString" -> "\"";
|
||||
default -> throw new SQLFeatureNotSupportedException();
|
||||
});
|
||||
return proxy(Connection.class, (p, m, a) -> switch (m.getName()) {
|
||||
case "setReadOnly" -> { readonly = (boolean) a[0]; yield null; }
|
||||
case "getMetaData" -> meta;
|
||||
case "close" -> null;
|
||||
case "createStatement" -> statement();
|
||||
default -> throw new SQLFeatureNotSupportedException();
|
||||
});
|
||||
}
|
||||
|
||||
Statement statement() {
|
||||
var stop = new CountDownLatch(1);
|
||||
return proxy(Statement.class, (p, m, a) -> switch (m.getName()) {
|
||||
case "execute" -> {
|
||||
String sql = (String) a[0];
|
||||
if (sql.equals("wait") || sql.equals("ignore cancel")) {
|
||||
if (started != null) java.nio.file.Files.writeString(started, "started");
|
||||
System.err.println("statement started");
|
||||
if (sql.equals("ignore cancel")) Thread.sleep(60_000);
|
||||
if (!stop.await(20, TimeUnit.SECONDS)) throw new SQLException("test timed out");
|
||||
throw new SQLException("statement cancelled");
|
||||
}
|
||||
if (sql.equals("write") && readonly) throw new SQLException("read-only");
|
||||
changes++;
|
||||
yield false;
|
||||
}
|
||||
case "cancel" -> { stop.countDown(); yield null; }
|
||||
case "getUpdateCount" -> changes;
|
||||
case "close" -> null;
|
||||
default -> throw new SQLFeatureNotSupportedException();
|
||||
});
|
||||
}
|
||||
public boolean acceptsURL(String url) { return true; }
|
||||
public DriverPropertyInfo[] getPropertyInfo(String url, Properties p) { return new DriverPropertyInfo[0]; }
|
||||
public int getMajorVersion() { return 1; }
|
||||
public int getMinorVersion() { return 0; }
|
||||
public boolean jdbcCompliant() { return false; }
|
||||
public Logger getParentLogger() { return Logger.getGlobal(); }
|
||||
}
|
||||
|
||||
static void send(DataOutputStream out, long id, String... fields) throws IOException {
|
||||
out.writeLong(id);
|
||||
out.writeInt(fields.length);
|
||||
for (String s : fields) {
|
||||
byte[] b = s.getBytes(StandardCharsets.UTF_8);
|
||||
out.writeInt(b.length);
|
||||
out.write(b);
|
||||
}
|
||||
out.flush();
|
||||
}
|
||||
|
||||
static byte[] reply(DataInputStream in) throws IOException {
|
||||
int size = in.readInt();
|
||||
assert size >= 0 && size <= OpenIdeaJdbc.LIMIT;
|
||||
byte[] bytes = new byte[size];
|
||||
in.readFully(bytes);
|
||||
return bytes;
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
var boundary = new DataOutputStream(OutputStream.nullOutputStream());
|
||||
OpenIdeaJdbc.text(boundary, "x".repeat(OpenIdeaJdbc.LIMIT - 14));
|
||||
OpenIdeaJdbc.end(boundary, false, -1);
|
||||
assert boundary.size() == OpenIdeaJdbc.LIMIT;
|
||||
try {
|
||||
OpenIdeaJdbc.text(boundary, "");
|
||||
throw new AssertionError("Oversized reply accepted");
|
||||
} catch (IOException expected) {}
|
||||
var java = Path.of(System.getProperty("java.home"), "bin", "java").toString();
|
||||
var child = new ProcessBuilder(java, "-cp", System.getProperty("java.class.path"),
|
||||
OpenIdeaJdbc.class.getName()).start();
|
||||
try (var out = new DataOutputStream(child.getOutputStream());
|
||||
var in = new DataInputStream(child.getInputStream());
|
||||
var log = child.errorReader()) {
|
||||
send(out, 1, "open", Fixture.class.getName(), "jdbc:fixture:", "", "", "true");
|
||||
assert reply(in)[0] == 1;
|
||||
send(out, 2, "query", "write", "0", "2");
|
||||
assert reply(in)[0] == 0;
|
||||
send(out, 3, "query", "first", "0", "2");
|
||||
assert reply(in)[0] == 1;
|
||||
send(out, 4, "query", "wait", "0", "2");
|
||||
assert log.lines().anyMatch("statement started"::equals);
|
||||
send(out, 3); // A late Cancel for the previous request cannot stop this one.
|
||||
assert reply(in).length == 0;
|
||||
send(out, 4);
|
||||
var first = reply(in);
|
||||
var second = reply(in);
|
||||
assert (first.length == 0) != (second.length == 0);
|
||||
assert (first.length == 0 ? second : first)[0] == 0;
|
||||
send(out, 5, "query", "after cancel", "0", "2");
|
||||
var result = reply(in);
|
||||
assert result[0] == 1 && result[result.length - 1] == 2;
|
||||
} finally {
|
||||
child.destroyForcibly().waitFor();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -70,6 +70,15 @@ exclusions = ["**/node_modules/**", "**/.metadata/**", "**/archetype-resources/*
|
||||
maven = { enabled = true }
|
||||
gradle = { enabled = true, wrapper = { enabled = true } }
|
||||
|
||||
# java-analyzer: a native Java server that imports Maven / Gradle builds itself and
|
||||
# also serves debugging (`java-analyzer dap`) and the run classpath without jdtls.
|
||||
[[language-servers]]
|
||||
name = "java-analyzer"
|
||||
fallback = true
|
||||
command = "java-analyzer"
|
||||
root-markers = ["java-analyzer.toml", "pom.xml", "build.gradle", "build.gradle.kts", "settings.gradle", "settings.gradle.kts", "build.xml", ".project"]
|
||||
install-hint = "Install java-analyzer in Settings › Languages & Frameworks › Language Servers (https://github.com/l0ng-ai/java-analyzer)"
|
||||
|
||||
[[language-servers]]
|
||||
name = "kmp-lsp"
|
||||
fallback = true
|
||||
|
||||
+2
-2
@@ -2,12 +2,12 @@
|
||||
"name": "openidea",
|
||||
"private": true,
|
||||
"description": "An open-source IDE inspired by IntelliJ IDEA",
|
||||
"version": "0.1.48",
|
||||
"version": "0.1.49",
|
||||
"license": "Apache-2.0",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"dev": "vite",
|
||||
"build": "tsc --noEmit && vite build && node scripts/check-webkit.ts",
|
||||
"build": "node --max-old-space-size=4096 node_modules/typescript/bin/tsc --noEmit && vite build && node scripts/check-webkit.ts",
|
||||
"preview": "vite preview",
|
||||
"tauri": "node scripts/tauri.ts",
|
||||
"test": "vitest run",
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package openidea:[email protected]1.0;
|
||||
package openidea:[email protected]3.0;
|
||||
|
||||
/// The commit being prepared in the window the current command or event came
|
||||
/// from (the Commit tool window, or the Commit dialog while it is open): the
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package openidea:[email protected]1.0;
|
||||
package openidea:[email protected]3.0;
|
||||
|
||||
/// Plugin-owned project data sources. Requires database.sources and project trust.
|
||||
/// Runs on the Database subsystem's host, including on remote project servers.
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package openidea:[email protected]1.0;
|
||||
package openidea:[email protected]3.0;
|
||||
|
||||
/// Editor and project-tree decorations (API 1.4). Requires project.read. An editor set
|
||||
/// follows its file: the file's own project, or the nearest one when it is outside them all.
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package openidea:[email protected]1.0;
|
||||
package openidea:[email protected]3.0;
|
||||
|
||||
/// What the pane beside the Services tree shows for a plugin's node.
|
||||
interface detail-types {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package openidea:[email protected]1.0;
|
||||
package openidea:[email protected]3.0;
|
||||
|
||||
/// Optional export (world `plugin-diagnostics`): problems in the current,
|
||||
/// possibly unsaved document, for the languages declared with the
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package openidea:[email protected]1.0;
|
||||
package openidea:[email protected]3.0;
|
||||
|
||||
/// HTTP endpoints found in a project (`[[contributes.endpoints]]`).
|
||||
interface endpoints-provider {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package openidea:[email protected]1.0;
|
||||
package openidea:[email protected]3.0;
|
||||
|
||||
/// Workbench events, delivered in batches to `event-handler.handle`.
|
||||
interface events {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package openidea:[email protected]1.0;
|
||||
package openidea:[email protected]3.0;
|
||||
|
||||
interface lifecycle {
|
||||
use types.{context, error};
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package openidea:[email protected]1.0;
|
||||
package openidea:[email protected]3.0;
|
||||
|
||||
/// What other plugins contributed to the extension points this plugin declares
|
||||
/// (`[[extension-points]]` in `plugin.toml`; the contributors list this plugin
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package openidea:[email protected]1.0;
|
||||
package openidea:[email protected]3.0;
|
||||
|
||||
/// Optional export (world `plugin-file-actions`): quick fixes and
|
||||
/// refactorings that create, rename or delete files, next to the text edits
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package openidea:[email protected]1.0;
|
||||
package openidea:[email protected]3.0;
|
||||
|
||||
/// Files outside the open projects: the configuration of the program a
|
||||
/// plugin integrates with, a tool installed elsewhere, a licence file.
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package openidea:[email protected]1.0;
|
||||
package openidea:[email protected]3.0;
|
||||
|
||||
/// Writes to the IDE log, tagged with the plugin id.
|
||||
interface log {
|
||||
@@ -34,6 +34,31 @@ interface ui {
|
||||
text(string),
|
||||
}
|
||||
|
||||
/// A highlight rule for Run and Debug console output (API 1.12).
|
||||
record console-rule {
|
||||
/// A regular expression (at most 512 bytes) in the syntax shared by
|
||||
/// Rust's `regex` and JavaScript: no inline flags, named groups,
|
||||
/// lookaround, backreferences, `\A`, `\z`, `\p`, POSIX classes or
|
||||
/// repeated groups such as `(…)+`. A pattern that could take too long
|
||||
/// on a long line is `invalid`. It sees the first 2000 characters of a line.
|
||||
pattern: string,
|
||||
ignore-case: bool,
|
||||
/// A console palette colour: `black`, `red`, `green`, `yellow`,
|
||||
/// `blue`, `magenta`, `cyan`, `white`, or one of them with `bright-`
|
||||
/// (`bright-red`). The colour scheme's Console Colors decide how they
|
||||
/// look. Unknown names are ignored.
|
||||
foreground: option<string>,
|
||||
/// A palette colour behind the text, as `foreground`.
|
||||
background: option<string>,
|
||||
/// Any of `bold`, `italic`, `underline`, `strikethrough`, `dim`.
|
||||
/// Unknown names are ignored.
|
||||
styles: list<string>,
|
||||
/// Style the whole line instead of only the matched text.
|
||||
whole-line: bool,
|
||||
/// A disabled rule keeps its place in the list but never matches.
|
||||
enabled: bool,
|
||||
}
|
||||
|
||||
/// A notification button that runs one of the plugin's commands.
|
||||
record notification-action {
|
||||
/// Up to 256 bytes, one line.
|
||||
@@ -65,6 +90,17 @@ interface ui {
|
||||
set-window-status-item: func(id: string, item: status-item) -> result<_, error>;
|
||||
/// Removes this window's override, falling back to the global item.
|
||||
remove-window-status-item: func(id: string) -> result<_, error>;
|
||||
/// Replaces the plugin's console highlight rules (API 1.12); an empty
|
||||
/// list removes them. Every window applies them to the stdout and stderr
|
||||
/// lines of its Run and Debug consoles; the plugin never sees the output.
|
||||
/// The first enabled whole-line rule that matches a line styles it,
|
||||
/// instead of the colour of its log level; each enabled match-only rule
|
||||
/// styles its matches on top, an earlier rule winning where matches
|
||||
/// overlap. A rule's colours and styles replace the program's own. Rules
|
||||
/// of several plugins apply in plugin id order. At most 100 rules; an
|
||||
/// invalid one fails the call and keeps the previous rules. They go
|
||||
/// away when the plugin stops. Needs no capability.
|
||||
set-console-rules: func(rules: list<console-rule>) -> result<_, error>;
|
||||
/// Sets the plugin's context key `key`, which `when` clauses test as
|
||||
/// `plugin.<id>.<key>`, in the window the current command or event came
|
||||
/// from (every window when there is none). A window's value takes
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package openidea:[email protected]1.0;
|
||||
package openidea:[email protected]3.0;
|
||||
|
||||
/// Optional export (world `plugin-hover`): Markdown added to the hover of
|
||||
/// documents of the languages declared with the `hover` provider kind,
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package openidea:[email protected]1.0;
|
||||
package openidea:[email protected]3.0;
|
||||
|
||||
/// Cross-file associations a plugin publishes for the files it declares under
|
||||
/// `[[contributes.documentIndexers]]`. The host calls `index` in cancellable
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package openidea:[email protected]1.0;
|
||||
package openidea:[email protected]3.0;
|
||||
|
||||
/// Embedded languages. The host routes plugin providers and configured language
|
||||
/// servers inside these ranges, preserving host-document UTF-16 coordinates.
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package openidea:[email protected]1.0;
|
||||
package openidea:[email protected]3.0;
|
||||
|
||||
/// Optional export (world `plugin-inlay-hints`): inline hints in documents of
|
||||
/// the languages declared with the `inlay-hints` provider kind, including
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package openidea:[email protected]1.0;
|
||||
package openidea:[email protected]3.0;
|
||||
|
||||
/// Problems in files, shown in the editor and the Problems view.
|
||||
interface diagnostics {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package openidea:[email protected]1.0;
|
||||
package openidea:[email protected]3.0;
|
||||
|
||||
/// Additional language features, called only for declared API 1.4 provider kinds.
|
||||
/// Editor calls have a 50 ms soft / 500 ms hard budget; formatting has 5 seconds.
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package openidea:[email protected]1.0;
|
||||
package openidea:[email protected]3.0;
|
||||
|
||||
/// Discovery of the IDE's built-in MCP server; never exposes its URL or token.
|
||||
interface mcp {
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
/// `fs` and `workspace`, and everything else goes through the imports below,
|
||||
/// gated by the capabilities granted in `plugin.toml` (a missing one returns
|
||||
/// `error::denied`).
|
||||
package openidea:[email protected]1.0;
|
||||
package openidea:[email protected]3.0;
|
||||
|
||||
world plugin {
|
||||
import log;
|
||||
@@ -212,3 +212,8 @@ world mcp-tools { export mcp-provider; }
|
||||
world plugin-run-target-validation { include plugin; export run-target-validation; }
|
||||
/// Export-only validation, composed with the base plugin world.
|
||||
world run-target-validator { export run-target-validation; }
|
||||
|
||||
/// A plugin with custom Search Everywhere categories.
|
||||
world plugin-search { include plugin; export search-provider; }
|
||||
/// Optional export composed with the base plugin by the SDK.
|
||||
world search { export search-provider; }
|
||||
@@ -1,4 +1,4 @@
|
||||
package openidea:[email protected]1.0;
|
||||
package openidea:[email protected]3.0;
|
||||
|
||||
/// Editor features a plugin provides for the languages it declares under
|
||||
/// `[[contributes.providers]]` (the host calls only declared kinds). They run
|
||||
|
||||
Loaded 100 of 194 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Заблокувати користувача