Skip to content

Commit 708bfc4

Browse files
authored
More verbosity in logs/output panel in case of errors (#105)
* More verbosity in logs/output panel in case of errors * Wrap exception in collectDefinitionLocations * Revert exception widening in SmithyLanguageServer Description of changes: While fighting some issues in a project not loading (recently seeing some similar to #100), I found that we were discarding some useful information, such as stack traces, which makes it more difficult to debug issues on remote machines (such as my colleagues'). This PR adds more e.printStackTrace() in cases where we only printed the exception messages. While this isn't perfect, and it's far from ideal UX when you see potentially duplicate stack traces, I find that in this case more is better than none. Many of the issues I'm seeing anytime something goes wrong in loading a project are NPEs on the project field in SmithyTextDocumentService, so I also marked that as nullable to make it clearer that it's not a safe field to use in all cases. We can amend the usages of that field in future PRs so that they have reasonable fallbacks.
1 parent 8b31c02 commit 708bfc4

File tree

4 files changed

+36
-10
lines changed

4 files changed

+36
-10
lines changed

src/main/java/software/amazon/smithy/lsp/SmithyLanguageServer.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public CompletableFuture<InitializeResult> initialize(InitializeParams params) {
6969
loadSmithyBuild(workspaceRoot);
7070
} catch (Exception e) {
7171
LspLog.println("Failure trying to load extensions from workspace root: " + workspaceRoot.getAbsolutePath());
72-
LspLog.println(e.toString());
72+
e.printStackTrace();
7373
}
7474
} else {
7575
LspLog.println("Workspace root was null");
@@ -78,7 +78,7 @@ public CompletableFuture<InitializeResult> initialize(InitializeParams params) {
7878
if (params.getWorkspaceFolders() == null) {
7979
try {
8080
tempWorkspaceRoot = Files.createTempDirectory("smithy-lsp-workspace").toFile();
81-
System.out.println("Created temporary workspace root: " + tempWorkspaceRoot);
81+
LspLog.println("Created temporary workspace root: " + tempWorkspaceRoot);
8282
tempWorkspaceRoot.deleteOnExit();
8383
WorkspaceFolder workspaceFolder = new WorkspaceFolder(tempWorkspaceRoot.toURI().toString());
8484
params.setWorkspaceFolders(ListUtils.of(workspaceFolder));
@@ -95,6 +95,7 @@ public CompletableFuture<InitializeResult> initialize(InitializeParams params) {
9595
loadSmithyBuild(root);
9696
} catch (Exception e) {
9797
LspLog.println("Error when loading workspace folder " + ws.toString() + ": " + e);
98+
e.printStackTrace();
9899
}
99100
}
100101

src/main/java/software/amazon/smithy/lsp/SmithyTextDocumentService.java

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import java.util.concurrent.ConcurrentHashMap;
3939
import java.util.stream.Collectors;
4040
import java.util.stream.Stream;
41+
import javax.annotation.Nullable;
4142
import org.eclipse.lsp4j.CodeAction;
4243
import org.eclipse.lsp4j.CodeActionParams;
4344
import org.eclipse.lsp4j.Command;
@@ -102,6 +103,7 @@ public class SmithyTextDocumentService implements TextDocumentService {
102103
private final List<CompletionItem> baseCompletions = new ArrayList<>();
103104
private Optional<LanguageClient> client;
104105
private final List<Location> noLocations = Collections.emptyList();
106+
@Nullable
105107
private SmithyProject project;
106108
private final File temporaryFolder;
107109

@@ -142,12 +144,17 @@ public Optional<File> getRoot() {
142144
public void createProject(SmithyBuildExtensions ext, File root) {
143145
Either<Exception, SmithyProject> loaded = SmithyProject.load(ext, root);
144146
if (loaded.isRight()) {
145-
this.project = loaded.getRight();
147+
SmithyProject project = loaded.getRight();
148+
this.project = project;
146149
clearAllDiagnostics();
147-
sendInfo("Project loaded with " + this.project.getExternalJars().size() + " external jars and "
148-
+ this.project.getSmithyFiles().size() + " discovered smithy files");
150+
sendInfo("Project loaded with " + project.getExternalJars().size() + " external jars and "
151+
+ project.getSmithyFiles().size() + " discovered smithy files");
149152
} else {
150-
sendError("Failed to create Smithy project: " + loaded.getLeft().toString());
153+
sendError(
154+
"Failed to create Smithy project. See output panel for details. Uncaught exception: "
155+
+ loaded.getLeft().toString()
156+
);
157+
loaded.getLeft().printStackTrace();
151158
}
152159
}
153160

@@ -170,6 +177,7 @@ public void createProject(File root) {
170177
LspLog.println("Loaded build extensions " + local + " from " + smithyBuild.getAbsolutePath());
171178
} catch (Exception e) {
172179
LspLog.println("Failed to load config from" + smithyBuild + ": " + e);
180+
e.printStackTrace();
173181
brokenFiles.add(smithyBuild.toString());
174182
}
175183
}
@@ -178,7 +186,10 @@ public void createProject(File root) {
178186
if (brokenFiles.isEmpty()) {
179187
createProject(result.build(), root);
180188
} else {
181-
sendError("Failed to load the build, following files have problems: \n" + String.join("\n", brokenFiles));
189+
sendError(
190+
"Failed to load the build, the following build files have problems: \n"
191+
+ String.join("\n", brokenFiles)
192+
);
182193
}
183194
}
184195

@@ -210,6 +221,7 @@ public CompletableFuture<Either<List<CompletionItem>, CompletionList>> completio
210221
} catch (Exception e) {
211222
LspLog.println(
212223
"Failed to identify token for completion in " + params.getTextDocument().getUri() + ": " + e);
224+
e.printStackTrace();
213225
}
214226
return Utils.completableFuture(Either.forLeft(baseCompletions));
215227
}
@@ -438,7 +450,7 @@ public CompletableFuture<List<Either<SymbolInformation, DocumentSymbol>>> docume
438450
.collect(Collectors.toList())
439451
);
440452
} catch (Exception e) {
441-
e.printStackTrace(System.err);
453+
e.printStackTrace();
442454

443455
return Utils.completableFuture(Collections.emptyList());
444456
}
@@ -526,6 +538,7 @@ public CompletableFuture<Hover> hover(HoverParams params) {
526538
}
527539
} catch (Exception e) {
528540
LspLog.println("Failed to determine hover content: " + e);
541+
e.printStackTrace();
529542
}
530543

531544
hover.setContents(content);
@@ -612,6 +625,7 @@ public void didChange(DidChangeTextDocumentParams params) {
612625
} catch (Exception e) {
613626
LspLog.println("Failed to write temporary contents for file " + original + " into temporary file "
614627
+ tempFile + " : " + e);
628+
e.printStackTrace();
615629
}
616630

617631
report(recompile(original, Optional.ofNullable(tempFile)));

src/main/java/software/amazon/smithy/lsp/ext/FileCachingCollector.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,12 @@ public Map<ShapeId, Location> collectDefinitionLocations(Model model) {
6363
this.membersToUpdateMap = new HashMap<>();
6464

6565
for (ModelFile modelFile : this.fileCache.values()) {
66-
collectContainerShapeLocationsInModelFile(modelFile);
66+
try {
67+
collectContainerShapeLocationsInModelFile(modelFile);
68+
} catch (Exception e) {
69+
throw new RuntimeException("Exception while collecting container shape locations in model file: "
70+
+ modelFile.filename, e);
71+
}
6772
}
6873

6974
operationsWithInlineInputOutputMap.forEach((this::collectInlineInputOutputLocations));

src/main/java/software/amazon/smithy/lsp/ext/SmithyProject.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,13 @@ private static Either<Exception, SmithyProject> load(
172172
return Either.forLeft(model.getLeft());
173173
} else {
174174
model.getRight().getValidationEvents().forEach(LspLog::println);
175-
return Either.forRight(new SmithyProject(imports, smithyFiles, externalJars, root, model.getRight()));
175+
176+
try {
177+
SmithyProject p = new SmithyProject(imports, smithyFiles, externalJars, root, model.getRight());
178+
return Either.forRight(p);
179+
} catch (Exception e) {
180+
return Either.forLeft(e);
181+
}
176182
}
177183
}
178184

0 commit comments

Comments
 (0)