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

styra-link: unhandled bad download URL for styra CLI causes cryptic error #78

Merged
merged 6 commits into from
Apr 17, 2024

Conversation

msorens
Copy link
Collaborator

@msorens msorens commented Apr 16, 2024

What code changed, and why?

With a bad URL the automatic install of the Styra CLI by the Styra VSCode plugin still resulted in storing something at /usr/local/bin/styra, but not an executable binary(!). (Looks like a 404 page, essentially.) The code assumed that was the real thing, reported the CLI installation successful(!), tried to invoke it as the styra CLI, and said "Error spawn Unknown system error -8", as shown:

image

This PR will now let the plugin recognize that the non-empty download was not an executable, so fails the "install" operation properly.

The solution here is not terribly elegant—just seeing if the download was larger than one megabyte—but it is a fast check.
I opted for fast rather than thorough because something like this would happen only rarely (like, um, well, now). 🤷
Again, because it is rare, this handles the case of /usr/local/bin/styra being an imposter at install time only. The other case—which it does not handle—is if you already have an imposter /usr/local/bin/styra when you try to run Styra Link: Initialize...

Definition of done

With the current bad URLs for downloading the styra CLI, the system will now recognize the attempted install of /usr/local/bin/styra failed and abort at that point.

image

2024.04.16 update:

Using the HTTP response instead of my initial crude approach; also adjusted the output here slightly:
image

How to test

You can install either from source code or from GitHub.

Install from source

  1. Switch to current branch.
  2. Run npm install.
  3. Open project in VSCode.
  4. In the debugger be sure you have "Run Extension" mode selected.
  5. Invoke "run" (F5).

Install from GitHub

  1. Go to the latest release on the release page.
  2. Download the additional version piggybacked into that release, labelled vscode-styra-2.0.1-next.2.vsix.
  3. Install via the standard command: code --install-extension vscode-styra-2.0.1-next.2.vsix

Exercise the Code

Make sure you do not have styra on your search path, and in particular that you do not have it in /usr/local/bin, since that is where the plugin installs to.

Attempt to run the VSCode palette command Styra Link: Initialize....
Say "Yes" when asked if you want to install the Styra CLI.
Observe the Output pane.

Copy link
Collaborator Author

@msorens msorens left a comment

Choose a reason for hiding this comment

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

Author notes below:

Comment on lines 16 to +18
if (!await StyraInstall.styraCmdExists()) {
info(`"${STYRA_CLI_CMD}" not found; aborting ${command.title}`);
return;
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

PR bonus: OK, let's abort then!

Comment on lines 102 to -103
if (!StyraInstall.checkWorkspace()) {
teeError('Something is wrong! Did you forget to run checkWorkspace in your command?');
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

PR bonus: Not clear why this would ever have been useful, as the only way this appears is if we are running checkWorkspace in the prior line. 😱

Comment on lines -28 to +32
StyraInstall.styraCmdExists = jest.fn().mockResolvedValue(true);
IDE.getConfigValue = mockVSCodeSettings();

beforeEach(() => {
StyraInstall.styraCmdExists = jest.fn().mockResolvedValue(true);
});
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

PR bonus: turns out that the PR bonus above that really did abort when it claimed to exposed an error in the test; this line needed to be in the beforeEach block for the tests to pass.

Copy link
Member

@srenatus srenatus left a comment

Choose a reason for hiding this comment

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

PR boni seem OK, I'm wondering about the check though -- I'd think acting on the HTTP status would be cleaner. Can we do that, or is that path blocked for some reason?

CHANGELOG.md Outdated Show resolved Hide resolved
src/lib/styra-install.ts Outdated Show resolved Hide resolved
Copy link
Member

@srenatus srenatus left a comment

Choose a reason for hiding this comment

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

Thanks!

Base automatically changed from msorens/optimize-package to main April 17, 2024 14:36
With a bad URL the download still resulted in something,
(seemed to be a web page indicating 404) so the code
blithely installed that as /usr/local/bin/styra.
With that in place, trying to invoke it as an executable
resulted in the cryptic error
"Error spawn Unknown system error -8".
This change now recognizes that the non-empty download
was not an executable, so fails the "install" operation properly.
not clear why this was ever useful
Getting here would be very rare, though, if even possible at all, because the checkStartup before would ensure CLI installation.
@msorens msorens merged commit a8a9c0e into main Apr 17, 2024
4 checks passed
@msorens msorens deleted the msorens/bad-url branch April 17, 2024 14:44
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

Successfully merging this pull request may close these issues.

2 participants