Skip to content

Commit 96b4623

Browse files
fix: 🐛 NPE when finishing Snyk Wizard in empty workspace
SnykWizard was triggering authentication and trust on finishing without checking the workspace for projects. This leads to a NPE, as our language server client is still uninitialized. LSP4e only initializes the language client and starts the language server, when a project / file is associated with the corresponding mime/media type. This fix wraps authentication and trust into a condition, checking, if the workspace contains projects.
1 parent f7b5aed commit 96b4623

1 file changed

Lines changed: 59 additions & 51 deletions

File tree

Lines changed: 59 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package io.snyk.eclipse.plugin.wizards;
22

3+
import java.util.Arrays;
4+
5+
import org.eclipse.core.resources.IProject;
6+
import org.eclipse.core.resources.ResourcesPlugin;
37
import org.eclipse.jface.viewers.IStructuredSelection;
48
import org.eclipse.jface.wizard.Wizard;
59
import org.eclipse.ui.INewWizard;
@@ -9,55 +13,59 @@
913
import io.snyk.languageserver.protocolextension.SnykExtendedLanguageClient;
1014

1115
public class SnykWizard extends Wizard implements INewWizard {
12-
protected SnykWizardConfigureAPIPage configureAPIPage;
13-
protected SnykWizardAuthenticatePage authenticatePage;
14-
15-
protected SnykWizardModel model;
16-
17-
protected IWorkbench workbench;
18-
protected IStructuredSelection selection;
19-
20-
public SnykWizard() {
21-
super();
22-
model = new SnykWizardModel();
23-
setNeedsProgressMonitor(true);
24-
}
25-
26-
@Override
27-
public String getWindowTitle() {
28-
return "Snyk Wizard";
29-
}
30-
31-
@Override
32-
public void addPages() {
33-
configureAPIPage = new SnykWizardConfigureAPIPage();
34-
addPage(configureAPIPage);
35-
36-
authenticatePage = new SnykWizardAuthenticatePage();
37-
addPage(authenticatePage);
38-
}
39-
40-
public void init(IWorkbench workbench, IStructuredSelection selection) {
41-
this.workbench = workbench;
42-
this.selection = selection;
43-
}
44-
45-
public boolean canFinish() {
46-
if (this.getContainer().getCurrentPage() == authenticatePage) {
47-
return true;
48-
}
49-
return false;
50-
}
51-
52-
public boolean performCancel() {
53-
model.resetPreferences();
54-
return true;
55-
}
56-
57-
public boolean performFinish() {
58-
new LsConfigurationUpdater().configurationChanged();
59-
SnykExtendedLanguageClient.getInstance().triggerAuthentication();
60-
SnykExtendedLanguageClient.getInstance().trustWorkspaceFolders();
61-
return true;
62-
}
16+
protected SnykWizardConfigureAPIPage configureAPIPage;
17+
protected SnykWizardAuthenticatePage authenticatePage;
18+
19+
protected SnykWizardModel model;
20+
21+
protected IWorkbench workbench;
22+
protected IStructuredSelection selection;
23+
24+
public SnykWizard() {
25+
super();
26+
model = new SnykWizardModel();
27+
setNeedsProgressMonitor(true);
28+
}
29+
30+
@Override
31+
public String getWindowTitle() {
32+
return "Snyk Wizard";
33+
}
34+
35+
@Override
36+
public void addPages() {
37+
configureAPIPage = new SnykWizardConfigureAPIPage();
38+
addPage(configureAPIPage);
39+
40+
authenticatePage = new SnykWizardAuthenticatePage();
41+
addPage(authenticatePage);
42+
}
43+
44+
public void init(IWorkbench workbench, IStructuredSelection selection) {
45+
this.workbench = workbench;
46+
this.selection = selection;
47+
}
48+
49+
public boolean canFinish() {
50+
if (this.getContainer().getCurrentPage() == authenticatePage) {
51+
return true;
52+
}
53+
return false;
54+
}
55+
56+
public boolean performCancel() {
57+
model.resetPreferences();
58+
return true;
59+
}
60+
61+
public boolean performFinish() {
62+
new LsConfigurationUpdater().configurationChanged();
63+
var projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
64+
if (projects != null && projects.length > 0
65+
&& Arrays.stream(projects).filter(p -> p.isAccessible()).count() > 0) {
66+
SnykExtendedLanguageClient.getInstance().triggerAuthentication();
67+
SnykExtendedLanguageClient.getInstance().trustWorkspaceFolders();
68+
}
69+
return true;
70+
}
6371
}

0 commit comments

Comments
 (0)