Sublime Forum

Error with sublime.active_window() after setting project data

#1

I have been having occasional issues with various sublime commands not returning the correct data. For instance, when setting project data, it causes the call to “sublime.active_window().folders()[0].split(’\’)[-1]” to have errors. Likewise, getting the folder just before setting the project data works fine… I’m using Dev Build 3111

Error:

proj_path = F:\git\My_Projects\test
proj_name = test
Traceback (most recent call last):
File “C:\Program Files\Sublime Text 3\Data\Packages\test\test.py”, line 438, in
self.view.window().show_input_panel(msg, prompt, lambda t: self.set_user_input( t ), None, None)
File “C:\Program Files\Sublime Text 3\Data\Packages\test\test.py”, line 487, in set_user_input
proj_name = sublime.active_window().folders()[0].split(’\’)[-1]
IndexError: list index out of range

code:

    proj_name = sublime.active_window().folders()[0].split('\\')[-1]
    print("proj_name = ", proj_name )
    self.project_data["settings"]["PROMPTED_BUILD_LAST"] = text
    sublime.active_window().set_project_data( self.project_data )
    # Extract project repo name
    proj_name = sublime.active_window().folders()[0].split('\\')[-1]
0 Likes

#2

That’s a lot of stuff happening in one line. It would help if you could pin down the exception to the folders()[0] access or split('\\')[-1], although I expect the first one to fail.

0 Likes

#3

I added the following code:

    proj_name = sublime.active_window().folders()[0].split('\\')[-1]
    print("proj_name = ", proj_name )
    self.project_data["settings"]["PROMPTED_BUILD_LAST"] = text
    sublime.active_window().set_project_data( self.project_data )
    # Extract project repo name
    try:
        print( "running on thread ", ( threading.current_thread() ))
        w = sublime.active_window()
        f = w.folders()
        f1 = f[0]
        split = f1.split("\\")
        proj_name = split[-1]
        print("proj_name = ", proj_name )
    except:
        print( "Failed - running on thread ", ( threading.current_thread() ))
        print(w)
        print(f)
        print(f1)
        print(split)

I got the following output:

proj_name = test
running on thread <_MainThread(MainThread, started 18416)>
Failed - running on thread <_MainThread(MainThread, started 18416)>
<sublime.Window object at 0x00000000047EA828>
[]
Traceback (most recent call last):
File “C:\Program Files\Sublime Text 3\Data\Packages\test\test.py”, line 493, in set_user_input
f1 = f[0]
IndexError: list index out of range

So it looks like sublime.active_window().folders() is empty. This error doesn’t happen all the time, but it seems like i get it to happen several times a day.

0 Likes

#4

Also, I’ve noticed this error occurs more when “text” has actually changed from what is currently saved. So i’m wondering if calling set_project_data( self.project_data ) blocks until the data is saved or does it queue the data and return. If it queue’s the data and returns, i’m wondering if it finally gets saved and maybe triggers the sublime.active_window() data to no longer be valid.

0 Likes