Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

"subl foo" has a high chance of opening a blank window with certain preferences [video] #27

Closed
frou opened this Issue Apr 28, 2013 · 40 comments

Comments

frou commented Apr 28, 2013

Filing an issue for what I reported in the forum at http://www.sublimetext.com/forum/viewtopic.php?f=2&t=12168


There has been a long time bug in ST2 and now ST3 that bites me many times a day.

Essentially, using the subl shell command to launch Sublime to view a file has a high chance of showing a blank window instead of the file. I have personally reduced the reproduction to a fresh install of ST3 build 3030 on OS X 10.8.3 with these User Preferences:

{
   "hot_exit": false,
   "remember_open_files": false
}

CLICK HERE FOR VIDEO

Although I launch and quit Sublime rapidly in the video, this is not needed for the bug to occur. It can occur first thing in the morning after the computer has been idling for 10 hours.

At least 7 other people have have mentioned this bug:
http://www.sublimetext.com/forum/viewtopic.php?f=2&t=11573
http://www.sublimetext.com/forum/viewtopic.php?f=2&t=11029

Owner

FichteFoll commented Apr 29, 2013

Not sure about a "General" category here because it's so unspecific but could be used then to bundle these kind of various issues. Option is a "subl" or "command line" cat but I don't like that too much. Comments?

frou commented Apr 29, 2013

C: Startup

jakerr commented May 10, 2013

I'm not 100% convinced that the hot_exit or remember_open_files settings have an effect. I've seen this bug approximately as often with or without those settings.

There is a 3 month old user echo bug on the official tracker that could use some up-votes here:
http://sublimetext.userecho.com/topic/62343-subl-with-no-window-open-doesnt-open-the-file

Narretz commented May 15, 2013

What is meant by "certain preferences"? Certain file types, or certain command line options, or ...?

frou commented May 20, 2013

The body covers that. Others are welcome to describe alternate reproduction steps. I so wish this bug would get some attention and be fixed. It spoils my otherwise very enjoyable experience using Sublime.

Same issue, I often have to do multiple subl ... retries before the project actually opens up.

recurser commented Jul 1, 2013

+1 - if I don't already have a window open, subl opens a blank window.

This is an extremely annoying bug that seems to be affecting me more and more each day.

Here's another topic from over a year ago describing the same problem: http://www.sublimetext.com/forum/viewtopic.php?f=2&t=5137&start=0

Every time this bug occurs, the following error shows up in Console.app:

7/17/13 4:10:27.505 PM Sublime Text[44434]: *** -[NSMachPort handlePortMessage:]: dropping incoming DO message because the connection is invalid

I am on build 3047, on OS X 10.8.4.

The occurence of this bug seems to be entirely random. I haven't seen any combination of preferences or command line options that consistently prevents this from happening.

frou commented Jul 18, 2013

Good info. Thanks. Unfortunately I can't do anything about it. Can anyone?

jakerr commented Jul 18, 2013

This is really hacky but I used the info @chasetopher mentioned to make this retry script:
https://gist.github.com/jakerr/6027228
It's minimally tested so use at your own risk, but seems to do the trick for me.

Also I noticed when writing the above that subl -w seems to always succeed so aliasing subl to subl -w and explicitly backgrounding it when you need to may be another viable work around.

frou commented Jul 18, 2013

Very creative! The script seems to work well, and I'd say is preferable because it directly tackles the bug. Thanks for this.

frou commented Aug 17, 2013

Another observation: with ffmpeg running in the background completely saturating CPU, the bug occurs close to 100% of the time.

paulp commented Nov 24, 2013

I was still suffering from this and I finally gave up and fired up the yakmobile. This bug really sucks and it makes me feel like an idiot that I even use sublime given how long it has persisted unfixed. Has the world been completely depopulated of command line users? How did this yak survive long enough to meet my razor?

So far this approach has been robust for me. Call this script subl and use it instead. If you want to hear about its struggles to bring you a working command line, throw a -d in front of your normal args to subl.

#!/bin/sh
#
# subl [-d] <args for sublime>

declare debug
[[ $1 == -d ]] && shift && debug=true

# Ah, the famed simplicity of OS X
declare -r sublimeRoot="$(mdfind "kMDItemDisplayName=='Sublime Text' && kMDItemKind==Application")"
declare -r sublime="$sublimeRoot/Contents/SharedSupport/bin/subl"
declare -r pluginHost="$sublimeRoot/Contents/MacOS/plugin_host"

log ()     { [[ $debug ]] && echo >&2 "$*"; true; }
run ()     { log "% $@" && "$@"; }
running () { ps -xo comm | fgrep -q "$pluginHost"; }
execute () { exec "$sublime" -a "$@"; }

checkRunning ()  {
  if running; then
    log "... plugin_host sighted."
  else
    log "... no plugin_host after $1 ms" && false
  fi
}

if running; then
  execute "$@"
else
  run "$sublime" </dev/null -n -s "$@" &
  disown -har
  # This allows 20 seconds for plugin_host to show up, which seems
  # like a lot except I've seen 8800 ms on a macbook pro.
  for i in {1..200}; do
    sleep 0.1 && checkRunning "$(( i * 100 ))" && execute "$@"
  done
fi

log "... gave up on plugin_host ever appearing."
exit 1

paulp commented Nov 24, 2013

I also made a homebrew tap, so you should be able to install the above with

brew tap paulp/extras && brew install subl

Thanks for this - it's working great so far. I had to change

kMDItemDisplayName=='Sublime Text'

to

kMDItemDisplayName=='Sublime Text 2.app'

for some reason to get it to work, but apart from that it's looking pretty solid.

paulp commented Nov 24, 2013

Sorry, I forgot there's still a 'Sublime Text 2'. 'Sublime Text' is what 3 calls itself. I'll fix it to work with both.

paulp commented Nov 24, 2013

What version of OSX are you on? You had to change it to "'Sublime Text 2.app" ? For me with Sublime 3 and OSX 10.9 it has to be "Sublime Text" all by itself - if I add .app there are no results. I'm afraid there might be MxN variations to consider if that difference arises from OS version.

paulp commented Nov 24, 2013

I'll go with

"kMDItemCFBundleIdentifier == 'com.sublimetext.*' && kMDItemKind==Application"

paulp commented Nov 24, 2013

For the record I'm not going to maintain the issue-pasted version, but the homebrew tap is updated, or directly from https://raw.github.com/paulp/homebrew-extras/master/bin/subl .

Thanks @paulp - I'm on OSX 10.9. Your updated version works perfectly:

declare -r sublimeId="kMDItemCFBundleIdentifier == 'com.sublimetext.*' && kMDItemKind==Application"
declare -r sublimeRoot="$(mdfind "$sublimeId")"

One other minor issue is that if I have a project folder open in Sublime Text, then subl project-folder/some-file.txt, it opens the file in a new window rather than the context of its containing folder window, if that makes sense. Since this is a thin wrapper for Contents/SharedSupport/bin/subl there's probably not much that can be done about that I guess?

paulp commented Nov 24, 2013

If the plugin_host is already running, it issues "subl -a" which is supposed to add to the current window. So it might be a bug somewhere - either that it's not detecting that the plugin_host is running, or a bug in sublime's subl. But it doesn't happen that way for me, so it might also be a sublime2 thing.

It works as expected in Sublime Text 3, so I'll just start using that. Thanks again for writing this!

Using the brew version, seems to work most of the time, but I've still had a few blank windows.

paulp commented Aug 13, 2014

Yeah, me too. It's still a problem.

frou commented Aug 13, 2014

A psychic told me that this is going to get fixed upon the surprise release of Sublime Text 6 in 2019. Hold strong, guys!

paulp commented Aug 13, 2014

That's actually better information than I had. Trend lines are good!

davidhq commented Nov 14, 2014

Very annoyinh bug... I'm now using this script and hope it will be much better... also pestering them on the forum and via email to finally (try to) fix it...

davidhq commented Nov 14, 2014

Hmm the script doesn't seem to help at all... The issue happened in 2 out of 5 tries...

The bug is especially annoying because I'm trying to use https://github.com/randy3k/Project-Manager to open my projects easily and it affects the plugin as well...

paulp commented Nov 15, 2014

At some point it lost effectiveness and I haven't had the heart to dive back into fixing sublime bugs again.

JimZM commented Dec 10, 2014

I'm running OS X 10.10 on a MacBook with Core i7 (quad core), with Sublime Text 3 (build 3065).
I can reproduce this problem most of the time by saturating the CPU with 8 instances of: yes > /dev/null &
I wrote a simple AppleScript (saved as "Bring app to front.scpt" with Script Editor) to bring the app to front and wait for it to complete:

on run argv
    tell application (argv as string)
        activate
        repeat with i from 1 to 100
            if frontmost then return i
            delay 0.02
            activate
        end repeat
    end tell
end run

Then I created a bash alias (I use edit instead of subl as the name):

alias edit="osascript ...path_to_script.../Bring\ app\ to\ front.scpt 'Sublime Text' ; edit --new-window"

This reduces the chance of the bug happening from around 90% to around 10%, under 95% CPU load.
On average, the AppleScript takes 0.2 seconds to run, and it does indeed feel a bit slower to open Sublime Text using my method.

EDIT: After more testing, I've found that my method doesn't work (or barely works).

frou commented Jan 30, 2015

Build 3067
Release Date: 30 January 2015
...
OSX: subl command line tool no longer uses Distributed Objects, resolving some intermittent failures
...

davidhq commented Jan 30, 2015

Where is 3067 available? Not here: http://www.sublimetext.com/3 and not via updates...

davidhq commented Jan 30, 2015

👌

davidhq commented Jan 30, 2015

after the upgrade I get high CPU usage :/ posted on forum as well.

frou commented Mar 2, 2015

This is fixed. Don't think I can add the fixed label myself.

@frou frou closed this Mar 2, 2015

@FichteFoll FichteFoll added the R: fixed label Mar 2, 2015

@FichteFoll FichteFoll added this to the Build 3067 milestone Mar 2, 2015

This is definitely not fixed. I'm experiencing this happening to me almost always on Ubuntu. It doesn't only happen when invoking it from the CLI.

frou commented Sep 18, 2016

@jorgedfbranco You should probably open a fresh issue. The title I originally wrote for this one sounds generic, but it turned out we were tracking an OS X specific bug (& workarounds) that did get an official fix.

I am using Build 3126 on MacOS Seirra and whenever I invoke sublime text form command line using subl, it opens an empty window irrespective of the arguments I supply.

If anyone experiences this issue and can't get the subl command to stop opening blank windows I solved it with the following steps:

  1. Copy your "User Settings" into a file (you'll need to paste them in later)
  2. Locate your data folder as described here
  3. Move that data folder to a place you can still access it but not in the default location
  4. Quit Sublime Text
  5. Open Sublime Text
  6. (At this point your subl command should work, you just need to restore your settings/packages etc...)
  7. You should see that Sublime Text now created a new folder where the old one was
  8. Replace the Installed Packages, Local and Packages folders in the new folder with those of the old one
  9. Paste your "User Settings" back into the JSON file for your user settings
  10. Quit Sublime Text and restart and you should now have your old settings back

I hope this helps

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment