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

Can't find socket when using emacsclient as homebrew editor #17334

Open
3 tasks done
tshu-w opened this issue May 19, 2024 · 10 comments
Open
3 tasks done

Can't find socket when using emacsclient as homebrew editor #17334

tshu-w opened this issue May 19, 2024 · 10 comments
Labels
bug Reproducible Homebrew/brew bug help wanted We want help addressing this

Comments

@tshu-w
Copy link

tshu-w commented May 19, 2024

brew doctor output

Your system is ready to brew.

Verification

  • My "brew doctor output" above says Your system is ready to brew. and am still able to reproduce my issue.
  • I ran brew update twice and am still able to reproduce my issue.
  • This issue's title and/or description do not reference a single formula e.g. brew install wget. If they do, open an issue at https://github.com/Homebrew/homebrew-core/issues/new/choose instead.

brew config output

HOMEBREW_VERSION: 4.3.0-87-ge9d989b
ORIGIN: https://github.com/Homebrew/brew
HEAD: e9d989bdea3dafe0227c45861d017ccd4a93bd3c
Last commit: 14 hours ago
Core tap JSON: 19 May 05:53 UTC
Core cask tap JSON: 19 May 05:53 UTC
HOMEBREW_PREFIX: /opt/homebrew
HOMEBREW_BUNDLE_USER_CACHE: /Users/wangtianshu/.cache/bundle
HOMEBREW_CASK_OPTS: []
HOMEBREW_EDITOR: emacsclient
HOMEBREW_MAKE_JOBS: 8
HOMEBREW_SORBET_RUNTIME: set
Homebrew Ruby: 3.3.1 => /opt/homebrew/Library/Homebrew/vendor/portable-ruby/3.3.1/bin/ruby
CPU: octa-core 64-bit arm_firestorm_icestorm
Clang: 15.0.0 build 1500
Git: 2.39.3 => /Library/Developer/CommandLineTools/usr/bin/git
Curl: 8.4.0 => /usr/bin/curl
macOS: 14.4.1-arm64
CLT: 15.3.0.0.1.1708646388
Xcode: N/A
Rosetta 2: false

What were you trying to do (and why)?

I want to use emacsclient as my EDITOR for brew edit. This command opens a document in an already-running emacs process.

What happened (include all command output)?

$ brew edit karabiner-elements
Editing /opt/homebrew/Caskroom/karabiner-elements/.metadata/14.13.0/20240416114923.815/Casks/karabiner-elements.json
emacsclient: can't find socket; have you started the server?
emacsclient: To start the server in Emacs, type "M-x server-start".
emacsclient: No socket or alternate editor.  Please use:

	--socket-name
	--server-file      (or environment variable EMACS_SERVER_FILE)
	--alternate-editor (or environment variable ALTERNATE_EDITOR)
Error: Failure while executing; `emacsclient /opt/homebrew/Caskroom/karabiner-elements/.metadata/14.13.0/20240416114923.815/Casks/karabiner-elements.json` exited with 1.

Copy from: Homebrew/homebrew-core#72968

The command fails because brew edit does not preserve the shell's TMPDIR variable. The appropriate /private/var directories seems to be allowed by sandbox-exec in sandbox.rb, but it looks like TMPDIR is set to the value of HOMEBREW_TEMP in build.rb.

def allow_write_temp_and_cache
allow_write_path "/private/tmp"
allow_write_path "/private/var/tmp"
allow_write "^/private/var/folders/[^/]+/[^/]+/[C,T]/", type: :regex
allow_write_path HOMEBREW_TEMP
allow_write_path HOMEBREW_CACHE
end

new_env = {
"TMPDIR" => HOMEBREW_TEMP,
"TEMP" => HOMEBREW_TEMP,
"TMP" => HOMEBREW_TEMP,
}

What did you expect to happen?

This has worked in the past. It appears to have been broken by the upgrade to emacs version 27. There is a reference to a change in emacsclient socket behavior in the changelog, but it is not totally clear that this is the root cause.

emacsclient will put its socket in a subdirectory of $TMPDIR. The sandbox seems to change this from a subdirectory of /var/folders to /private/tmp

Step-by-step reproduction instructions (by running brew commands)

See here: https://github.com/Homebrew/homebrew-core/issues/72968
@tshu-w tshu-w added the bug Reproducible Homebrew/brew bug label May 19, 2024
@Bo98
Copy link
Member

Bo98 commented May 21, 2024

Original issue analysis is incorrect as the sandbox and build.rb doesn't apply here.

But there is overall environment filtering going on and setting HOMEBREW_TEMP does work: HOMEBREW_TEMP=$TMPDIR brew edit karabiner-elements. This perhaps should happen by default for brew edit but currently doesn't.

@tshu-w
Copy link
Author

tshu-w commented May 22, 2024

Original issue analysis is incorrect as the sandbox and build.rb doesn't apply here.

Thank you for pointing this out.

But there is overall environment filtering going on and setting HOMEBREW_TEMP does work: HOMEBREW_TEMP=$TMPDIR brew edit karabiner-elements. This perhaps should happen by default for brew edit but currently doesn't.

Is there any side effect if the user sets HOMEBREW_TEMP to TMPDIR globally? Or do I need to wait for changes from brew edit?

@carlocab
Copy link
Member

Is there any side effect if the user sets HOMEBREW_TEMP to TMPDIR globally?

It could break some builds that get confused by the long paths that TMPDIR typically point to.

If you don't build anything from source you'll probably be fine. If you do encounter build failures, you can always try to unset HOMEBREW_TEMP.

@tshu-w
Copy link
Author

tshu-w commented May 22, 2024

Thank you for your reply ;), then I will temporarily leave this issue open. Feel free to close it if your team thinks it should not be implemented for brew edit.

@carlocab
Copy link
Member

You could probably also set

TMPDIR=/private/tmp

as a likely safer alternative (though I don't think HOMEBREW_TEMP=$TMPDIR is that unsafe either).

But we should try fixing this for brew edit. PRs welcome -- if you're game to give it a try, we'd be happy to help you finish off even a half-working attempt.

@tshu-w
Copy link
Author

tshu-w commented May 22, 2024

PRs welcome -- if you're game to give it a try, we'd be happy to help you finish off even a half-working attempt.

Sure, will give it a try when I am free.

@carlocab carlocab added the help wanted We want help addressing this label May 22, 2024
@MikeMcQuaid
Copy link
Member

But we should try fixing this for brew edit. PRs welcome -- if you're game to give it a try, we'd be happy to help you finish off even a half-working attempt.

Agreed. Please scope this logic entirely into dev-cmd/edit.rb if so, thanks!

@tshu-w
Copy link
Author

tshu-w commented May 23, 2024

Hi, after a rough reading of the edit command code I have some doubts.

I don't quite understand why HOMEBREW_TEMP=$TMPDIR brew edit karabiner-elements can work. How does setting HOMEBREW_TEMP affect the access to the sock file in TMPDIR by exec emacsclient? Is it because TMPDIR is reset to HOMEBREW_TEMP during command execution (not found in the code) or is there another reason?

@MikeMcQuaid
Copy link
Member

Is it because TMPDIR is reset to HOMEBREW_TEMP during command execution (not found in the code)

Yes, this is done by bin/brew.

@tshu-w
Copy link
Author

tshu-w commented May 23, 2024

Is it because TMPDIR is reset to HOMEBREW_TEMP during command execution (not found in the code)

Yes, this is done by bin/brew.

But bin/brew is just filter TMPDIR and I miss up the logic that sets TMPDIR back to HOMEBREW_TEMP in dev-cmd/edit.rb or other places like

# Set TMPDIR so Xcode's `make` doesn't fall back to `/var/tmp/`,
# which may be not user-writable.
ENV["TMPDIR"] = ENV.fetch("HOMEBREW_TEMP", nil)
. So I am really confused why HOMEBREW_TEMP=$TMPDIR brew edit karabiner-elements work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Reproducible Homebrew/brew bug help wanted We want help addressing this
Projects
None yet
Development

No branches or pull requests

4 participants