Skip to content
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

linux: Fix file not opening from file explorer #21137

Merged

Conversation

0xtimsb
Copy link
Contributor

@0xtimsb 0xtimsb commented Nov 24, 2024

Closes #20070

Release Notes:

  • Fixed issue where files wouldn't open from the file explorer.
  • Fixed "Open a new workspace" option on the desktop entry right-click menu.

Context:

Zed consists of two binaries:

  • zed (CLI component, located at crates/cli/main.rs)
  • zed-editor (GUI component, located at crates/zed/main.rs)

When zed is used in the terminal, it checks if an existing instance is running. If one is found, it sends data via a socket to open the specified file. Otherwise, it launches a new instance of zed-editor. For more details, see the detect and boot_background functions in crates/cli/main.rs.

Root Cause:

Install process creates directories like .local/zed.app and .local/zed-preview.app, which contain desktop entries for the corresponding release. For example, .local/zed.app/share/applications contains zed.desktop.

This desktop entry includes a generic Exec field, which is correct by default:

Comment=A high-performance, multiplayer code editor.
TryExec=zed
StartupNotify=true

The issue is in the install.sh script. This script copies the above desktop file to the common directory for desktop entries (.local/share/applications). During this process, it replaces the TryExec value from zed with the exact binary path to avoid relying on the shell's PATH resolution and to make it explicit.

However, replacement incorrectly uses the path for zed-editor instead of the zed CLI binary. This results in not opening a file as if you use zed-editor directly to do this it will throw zed is already running error on production and open new instance on dev.

Note: This PR solves it for new users. For existing users, they will either have to update .desktop file manually, or use install.sh script again. I'm not aware of zed auto-update method, if it runs install.sh under the hood.

@maxdeviant maxdeviant changed the title fix file not opening from file explorer Fix file not opening from file explorer Nov 24, 2024
@maxdeviant maxdeviant changed the title Fix file not opening from file explorer linux: Fix file not opening from file explorer Nov 24, 2024
@SomeoneToIgnore SomeoneToIgnore added the cla-signed The user has signed the Contributor License Agreement label Nov 24, 2024
Copy link
Contributor

@SomeoneToIgnore SomeoneToIgnore left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, thank you.

@SomeoneToIgnore SomeoneToIgnore merged commit 5f29f21 into zed-industries:main Nov 29, 2024
14 checks passed
JaLnYn pushed a commit to JaLnYn/zed that referenced this pull request Nov 29, 2024
Closes zed-industries#20070

Release Notes:

- Fixed issue where files wouldn't open from the file explorer.
- Fixed "Open a new workspace" option on the desktop entry right-click
menu.

Context:

Zed consists of two binaries:

- `zed` (CLI component, located at `crates/cli/main.rs`)
- `zed-editor` (GUI component, located at `crates/zed/main.rs`)

When `zed` is used in the terminal, it checks if an existing instance is
running. If one is found, it sends data via a socket to open the
specified file. Otherwise, it launches a new instance of `zed-editor`.
For more details, see the `detect` and `boot_background` functions in
`crates/cli/main.rs`.

Root Cause:

Install process creates directories like `.local/zed.app` and
`.local/zed-preview.app`, which contain desktop entries for the
corresponding release. For example, `.local/zed.app/share/applications`
contains `zed.desktop`.

This desktop entry includes a generic `Exec` field, which is correct by
default:

```sh
Comment=A high-performance, multiplayer code editor.
TryExec=zed
StartupNotify=true
```

The issue is in the `install.sh` script. This script copies the above
desktop file to the common directory for desktop entries
(.local/share/applications). During this process, it replaces the
`TryExec` value from `zed` with the exact binary path to avoid relying
on the shell's PATH resolution and to make it explicit.

However, replacement incorrectly uses the path for `zed-editor` instead
of the `zed` CLI binary. This results in not opening a file as if you
use `zed-editor` directly to do this it will throw `zed is already
running` error on production and open new instance on dev.


Note: This PR solves it for new users. For existing users, they will
either have to update `.desktop` file manually, or use `install.sh`
script again. I'm not aware of zed auto-update method, if it runs
`install.sh` under the hood.
github-merge-queue bot pushed a commit that referenced this pull request Dec 22, 2024
… from .desktop file (#22335)

Closes #21406

Context:

A few weeks ago on Linux, we resolved an
[issue](#20070) where users
could not open more than one file from the file explorer. This was fixed
by replacing `zed-editor` (zed binary in the code) with `zed` (cli
binary in the code) in the `.desktop` file. The reason for this change
was that using the cli to open files is more convenient - it determines
weather to spawn a new Zed instance or use an existing one, if we use
main binary instead it would throw error `Zed is already running`.

You can read the complete PR here: [linux: Fix file not opening from
file explorer](#21137).

While this fix resolved the original issue, it introduced a new one.

Problem:

When the cli binary is used, it assumes it is always being invoked from
a terminal and relies on `std::env::vars()` to retrieve the environment
variables needed to spawn Zed. These env vars are then passed to the
worktree, and eventually, languages use the `PATH` from this env to find
binaries. This leads to the "Failed to start language server" error when
the `.desktop` entry is used on Linux.

Solution:

When the `zed-editor` binary is used, it uses some clever Unix-specific
logic to retrieve the default shell (`load_shell_from_passwd`) and then
fetch the env vars from that shell (`load_login_shell_environment`).
This same logic should be used in the cli binary when it is invoked via
a `.desktop` entry rather than from a terminal.

Approach:

I moved these two functions mentioned above to a utils file and reused
them in cli binary to fetch env vars only on Linux when it is not run
from a terminal. This provides missing paths, and fix the issue.

It is also possible to handle this in the `zed-editor` binary by
modifying the logic in `handle_cli_connection`, where `CliRequest::Open`
is processed. There we can discard incoming env, and use our logic. But
discarding incoming envs felt weird, and I thought it's better to handle
this at source.

Release Notes:

- Fixed `Failed to start language server` errors when starting from
dekstop entry on Linux
CharlesChen0823 pushed a commit to CharlesChen0823/zed that referenced this pull request Dec 24, 2024
… from .desktop file (zed-industries#22335)

Closes zed-industries#21406

Context:

A few weeks ago on Linux, we resolved an
[issue](zed-industries#20070) where users
could not open more than one file from the file explorer. This was fixed
by replacing `zed-editor` (zed binary in the code) with `zed` (cli
binary in the code) in the `.desktop` file. The reason for this change
was that using the cli to open files is more convenient - it determines
weather to spawn a new Zed instance or use an existing one, if we use
main binary instead it would throw error `Zed is already running`.

You can read the complete PR here: [linux: Fix file not opening from
file explorer](zed-industries#21137).

While this fix resolved the original issue, it introduced a new one.

Problem:

When the cli binary is used, it assumes it is always being invoked from
a terminal and relies on `std::env::vars()` to retrieve the environment
variables needed to spawn Zed. These env vars are then passed to the
worktree, and eventually, languages use the `PATH` from this env to find
binaries. This leads to the "Failed to start language server" error when
the `.desktop` entry is used on Linux.

Solution:

When the `zed-editor` binary is used, it uses some clever Unix-specific
logic to retrieve the default shell (`load_shell_from_passwd`) and then
fetch the env vars from that shell (`load_login_shell_environment`).
This same logic should be used in the cli binary when it is invoked via
a `.desktop` entry rather than from a terminal.

Approach:

I moved these two functions mentioned above to a utils file and reused
them in cli binary to fetch env vars only on Linux when it is not run
from a terminal. This provides missing paths, and fix the issue.

It is also possible to handle this in the `zed-editor` binary by
modifying the logic in `handle_cli_connection`, where `CliRequest::Open`
is processed. There we can discard incoming env, and use our logic. But
discarding incoming envs felt weird, and I thought it's better to handle
this at source.

Release Notes:

- Fixed `Failed to start language server` errors when starting from
dekstop entry on Linux
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla-signed The user has signed the Contributor License Agreement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cannot open more than one file normally from file explorer
3 participants