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

Unable to detect ag if an unrelated error occurs (I fixed it myself, desc and code provided) #103

Open
Derpius opened this issue May 2, 2020 · 0 comments

Comments

@Derpius
Copy link

Derpius commented May 2, 2020

I have a very unlikely to occur error on my PC in that whatever command is run, even just opening cmd, results in The system cannot find the path specified. error, despite no invalid entries in both system and user PATH variables.

After spending an hour looking through the plugin's code, I found it was using the exit code returned by os.system, which, due to the error described above, always returns 1, meaning that because of the way the plugin is programmed, it will never detect ag (The Silver Searcher).

I've fixed this by rewriting the plugin_loaded function to use subprocess.Popen and check the output of the command for "Geoff Greer", which will only be there if the command was successful, meaning any unrelated errors won't affect it.

I've added comments to the code describing whats changed, and why.
Here's the function by itself:

def plugin_loaded():
	global F_EXT_SEARCH
	
	# This creates a process and outputs both the response you would get in the command line and an error if one occured
	# shell had to be set to false for it to work for some reason so a command line opens for a split second when the plugin is loaded
	# Note: you don't need to assign both stdout and stderr if you dont need to use both, I've just done it here for an example
	process = subprocess.Popen(["ag", "--help"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=False)
	(out, err) = process.communicate()

	# This checks if the command's output contains Geoff Greer, which would only be there if the command was sucessfull,
	# you could also check if err is None, but that ends up with the same problem as os.system == 0 if an unrelated error occurs (which is what happend in this case)
	F_EXT_SEARCH = "Geoff Greer" in str(out)

	if F_EXT_SEARCH:
		print('Sublime_ZK: Using ag!')
	else:
		settings = get_settings()
		ag = settings.get('path_to_ag', '/usr/local/bin/ag')
		if ag:
			# This is almost identical as above, except uses the value stored in the setting file (replaces what you already had here)
			process = subprocess.Popen([ag, "--help"], stdout=subprocess.PIPE, shell=True)
			(out, err) = process.communicate()
			
			if "Geoff Greer" in str(out):
				ExternalSearch.SEARCH_COMMAND = ag
				F_EXT_SEARCH = True
				print('Sublime_ZK: Using ', ag)
			else:
				print('Sublime_ZK: Not using ag!')
		else:
			print('Sublime_ZK: Not using ag!')
	settings = get_settings()
	settings.clear_on_change("sublime_zk_notify")
	settings.add_on_change("sublime_zk_notify", settings_changed)
	settings_changed()

I've also attached the full script, for ease of replacement. (Had to change the extension to txt as github doesn't support python files)
sublime_zk.txt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant