Files
openidea/src/features/remote-dev/index.ts
T
Bert Verhelstandbertyhell 6ad6559f4a feat(env): load the project's direnv environment (#4049)
A trusted project with an .envrc in its root now gets its direnv environment, locally and over remote development. The IDE runs `direnv export json` in the background (cancellable, 10-minute timeout); terminals, run configurations, language servers, build tools and debuggers started in the project get what it exports, and language servers wait until it has loaded.

The environment layer was process-wide, but one desktop process holds several projects: ide_core::project_env keeps one environment per project root, and the shared spawn helpers pick it from the child's working directory (host_env / prepare from current_dir, plus the *_in variants for PTYs and PATH lookups). Restricted Mode never runs direnv, and revoking trust drops the environment.

A missing direnv gives a one-time hint, a blocked .envrc a warning to run direnv allow (the IDE never runs it), failures and timeouts an error with the end of direnv's output. Changes to .envrc, flake.nix or flake.lock offer Reload Environment, which loads direnv again too. The changed-file notice's title is now translated.

Closes #3555

Co-authored-by: bertyhell <1710840+bertyhell@users.noreply.github.com>
2026-11-18 09:29:59 +08:00

144 lines
4.7 KiB
TypeScript

/**
* Remote development: a window whose project lives on an SSH host, in a WSL distribution or in a
* dev container. The backend (`openidea-server`) runs there and this window routes its commands
* to it (platform/remote.ts).
*/
import { registerActions } from "@/platform/actions";
import { disconnectRemote, useRemote } from "@/platform/remote";
import { lazyComponent } from "@/ui";
import { maybeEditorService, registerOverlay, registerStatusItem, registerToolWindow } from "@/workbench/contributions";
import { confirmLeaveProject } from "@/platform/project";
import { ConnectHost } from "./ConnectDialog";
import { reopenInContainer } from "./container";
import { installDirenvNotifications, reloadEnvironment } from "./environment";
import { RemoteStatus } from "./RemoteStatus";
import { closeConnectDialog, openConnectDialog } from "./model";
import { restorePorts, usePorts } from "./ports";
import { installReconnect, reconnectNow } from "./reconnect";
/** Forwarded ports are available while this window is connected to an SSH host or a dev container. */
function trackPortsWindow(): () => void {
let off: (() => void) | null = null;
let session = useRemote.getState().session;
const sync = () => {
const next = useRemote.getState().session;
const connected = next?.target.kind === "ssh" || next?.target.kind === "devContainer";
if (connected && (next !== session || !off)) {
usePorts.setState({ ports: [] });
void restorePorts();
}
session = next;
if (connected && !off) {
off = registerToolWindow({
id: "remote.ports",
title: "Ports",
icon: "web",
anchor: "bottom",
order: 40,
stripe: "onUse",
component: lazyComponent(() => import("./PortsView").then((m) => m.PortsView)),
});
} else if (!connected && off) {
off();
off = null;
usePorts.setState({ ports: [] });
}
};
sync();
const unsubscribe = useRemote.subscribe(sync);
return () => {
unsubscribe();
off?.();
};
}
export function activate(): () => void {
const disposers = [
trackPortsWindow(),
installReconnect(),
installDirenvNotifications(),
registerOverlay({ id: "remote.connect", component: ConnectHost }),
registerStatusItem({
id: "remote.location",
title: "Remote Host",
align: "left",
priority: 100,
component: RemoteStatus,
}),
registerActions([
{
id: "remote.connect",
title: "Remote Development…",
description: "Opens a project folder on an SSH host; the IDE backend runs on that host.",
category: "File",
icon: "folder",
keywords: ["ssh", "remote", "server", "gateway", "connect", "host"],
run: () => openConnectDialog(),
},
{
id: "remote.reconnect",
title: "Reconnect to Remote Host",
description: "Connects this window to its remote project again after the connection was lost.",
category: "File",
keywords: ["ssh", "remote", "reconnect", "connection", "lost"],
when: "remoteConnectionLost",
run: () => reconnectNow(),
},
{
id: "remote.disconnect",
title: "Disconnect from Remote Host",
category: "File",
keywords: ["ssh", "remote", "close"],
when: "projectOpen",
run: async () => {
if (!(await confirmLeaveProject(false))) return;
try {
await disconnectRemote();
} catch (e) {
maybeEditorService()?.cancelClose?.();
throw e;
}
},
},
{
id: "remote.reopenInContainer",
title: "Reopen in Container",
description:
"Builds or starts the project's dev container (devcontainer.json) with the Dev Containers CLI and opens the project in it.",
category: "File",
icon: "folder",
keywords: ["devcontainer", "dev container", "docker", "container", "remote"],
when: "projectOpen",
run: () => reopenInContainer(),
},
{
id: "remote.reloadEnvironment",
title: "Reload Environment",
description:
"Reads the login shell's environment (and devcontainer.json remoteEnv on a remote host) again for new terminals and processes.",
category: "File",
keywords: [
"env",
"environment",
"variables",
"path",
"shell",
"remoteEnv",
"devcontainer",
"direnv",
"envrc",
"nix",
"flake",
"reload",
],
when: "projectOpen",
run: () => reloadEnvironment(),
},
]),
];
return () => {
disposers.reverse().forEach((d) => d());
closeConnectDialog();
};
}