-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Allow opening files, folders, and workspaces in existing code-server from CLI #164
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Alternatively just running |
Hi, @code-asher, @kylecarbs I'm interesting to help out. Can you please give me some estimated effort and a pointer to get start? |
@xerothermic code-server is currently in the midst of a large rework, so once that's merged in we'll be glad to accept your contributions. |
This would be neat. We'd need to make code-server listen on a socket then add a flag of some kind to open in an existing instance at which point it would send the file name on the socket. The existing instance would pick that up and send it to the client running on the browser which would then open the file. |
@code-asher this will also need some kind of instance mapping to the client the client needs to be aware via pid files what is running then it could send via that pid a signal to optain the ports or it stores also the state in some file. Use Casecode-server "instance 1"
code-server "instance 2" |
That makes sense. One idea floating around is that we daemonize
code-server so there would only ever be one instance running anyway. I'm
not sure how much reliance there is on being able to run multiple
instances of code-server on the same machine though (although it seems
Docker would be better suited for that kind of setup).
|
@code-asher It depends on context switching in Project that depend them self on multiple docker images this is a hugh win. i for my self often want to open diffrent projects as root on my servers. i like doing changes to /etc/* via code-server as it allows me fast search replace and all that. |
oh and a other usecase is when you apply expensiv security stuff befor the instance gets routed then its also a hugh win to switch the context of a already running editor. |
It was suggested in #1586 that we could also make it possible to choose the pane to open a file in. Since VS Code is obviously capable of opening files in an existing instance, I wonder if they're using their sockets to accomplish this? And if so, can we just do the same thing instead of creating our own socket? |
I tried 'code-server filename' to open a certain file (Code-server version 3.1.0). However, code-server regards it as a new folder name. How can I open a file? Thank you. : ) |
I have a proposal. When code-server is started we could create an IPC channel or a socket in a standard directory with a standard name format based on the PID of code-server (e.g. If code-server detects this env variable, cc: @code-asher @nhooyr Edit: Could alternatively be a named fifo pipe, and the filename could be a random string instead of PID (and the env var would be Dunno what protocol we'd run on the socket, could have a HTTP API (kinda wack) or something backed by zeromq/grpc. |
@HankHuangX this feature has not been implemented yet, so you cannot open files in the current code-server instance from a terminal for now. |
@deansheather Thanks for your reply.😃 |
@deansheather I took a look at what VS Code does and it looks like they're doing essentially what you described. They create a randomly named socket and put the path in an environment variable then spawn an http server on that socket which expects some JSON for each request that describes the action to perform. So I think we can just check for that environment variable and if it exists then send the appropriate http request to the socket. |
JSON-RPC I imagine |
Just wanted to chime in and mention that I think it would also be really cool if you could pass a flag and have the given folder open in a new tab a la |
Having a Curious what the status of this is, happy to help. Example: opens a folder const http = require("http");
const req = http.request(
{
socketPath: process.env["VSCODE_IPC_HOOK_CLI"],
path: "/",
method: "POST",
}
);
req.on("error", (err) => console.error(err));
req.write(
JSON.stringify({
type: "open",
folderURIs: ["/path/to/folder"],
forceReuseWindow: true,
// forceNewWindow: true
})
);
req.end(); Example: opens a fle const http = require("http");
const req = http.request(
{
socketPath: process.env["VSCODE_IPC_HOOK_CLI"],
path: "/",
method: "POST",
}
);
req.on("error", (err) => console.error(err));
req.write(
JSON.stringify({
type: "open",
fileURIs: ["/path/to/file.ext"]
})
); Interestingly using Relevant source: |
@shayne This is awesome. We haven't started work on this so if you're interested in submitting a PR that'd be fantastic. I imagine we'd just modify |
I have 2 instances of code-server running, how do I select the instance and make it open a specific file the next time the UI is loaded? |
Currently it always uses the last instance and I think the UI has
to be already open when the command is ran.
|
Actually there would be a way to select the instance by setting
VSCODE_IPC_HOOK_CLI but you would have to manually find the
socket.
|
As a user who use Terminal a lot i would like to open files that way.
Sometimes ne also navigate away from workspace to find something, and again sometimes need to previev or edit files. And then it would be awsome if that could happend from the Terminal.
new feature on VSCode 1.31 Current working directory used for links
see: https://github.com/Microsoft/vscode/blob/622b3d62dd92ea1bcdc7d250e0a7565b018edcee/src/vs/workbench/contrib/terminal/electron-browser/terminalLinkHandler.ts
The text was updated successfully, but these errors were encountered: