Skip to content

Commit

Permalink
Merge pull request #15 from disneystreaming/dfrancoeur/debug
Browse files Browse the repository at this point in the history
Improve the debugging experience
  • Loading branch information
lewisjkl authored Nov 17, 2022
2 parents 67e4267 + 5279b0a commit 4eddb14
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 4 deletions.
54 changes: 54 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,57 @@ An example Smithy build file looks like this:
"mavenRepositories": ["https://full.url.to.your.mavenrepository.tld"]
}
```

## Debugging guide

This guide is to help you work on the VS Code extension along with the [Smithy Language Server](https://github.com/awslabs/smithy-language-server).

### Editor

We recommend that you use VS Code to debug the extension. The support exists out of the box.

### Debuging the extension

To start a debug session:

1. Open this project in VS Code
2. In a terminal, launch: `yarn && yarn run watch`. This will start a compile watch loop for the extension sources
3. Open the `src/extension.ts` file and press F5. this launches a new VS Code window with the extension installed and in debug mode.
4. Add debug point in the extesion source.

Note that this extension activates when you open a Smithy file. So to trigger the execution, once you press F5, make sure to open a Smithy file in the `Extension Development Host` VS Code window.

#### Debugging the language server

If you believe the issue exists in the language server rather than the extension itself, you may want to use a Java debugger and attach to the language server.

1. Checkout the code at the link above and cd into it
2. Open the code in your IDE, we suggest IntelliJ because it's the best in class with Java
3. Update the version number in `./VERSION`, and append `-SNAPSHOT`, eg: `0.2.1-SNAPSHOT`
4. In a terminal, launch: `./gradlew -t publishToMavenLocal`. this will publish to your local maven repository (usually somewhere under `~/.m2`) with the following artifact coordinates: `software.amazon.smithy:smithy-language-server:$VERSION`
5. Configure your IDE to use the new language server artifact:
1. Extension settings: change the `lspCoordinates` value
2. Per project, via a `smithy-build.json` file that contains: `{"languageServer":"software.amazon.smithy:smithy-language-server:0.2.1-SNAPSHOT"}`

At this point, you should be able to run custom code within the language-server to work on your issue. To have good visibility though, you may want to use `System.out.println`. **Don't**. Instead, use `LspLog.println("message")` and your message will be printed to a file in the directory where the extension is opened. See below for more information on why you should not print to standard out.

If you want to start debugging the JVM, change the Extension settings `lspJavaOpts` and set: `-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005,quiet=y`. The important bit here is: `quiet=y`, because as I've said already, it's important to avoid printing to standard out.

With that, you can reload your VS Code window. At that point, when the extension starts the language server with a debug agent and you'll be able to connect on port `5005`. You can also use `suspend=y` as opposed to `suspend=n` if you have to troubleshoot something in the startup of the language server.

### Known errors:

Error in `Log(Extension Host)` channel:

```
2022-11-17 10:33:51.138 [error] Error: Header must provide a Content-Length property.
at StreamMessageReader.onData (/Users/David.Francoeur/.vscode/extensions/disneystreaming.smithy-0.0.0/node_modules/vscode-jsonrpc/lib/messageReader.js:163:27)
at Socket.<anonymous> (/Users/David.Francoeur/.vscode/extensions/disneystreaming.smithy-0.0.0/node_modules/vscode-jsonrpc/lib/messageReader.js:148:18)
at Socket.emit (node:events:526:28)
at addChunk (node:internal/streams/readable:315:12)
at readableAddChunk (node:internal/streams/readable:289:9)
at Readable.push (node:internal/streams/readable:228:10)
at Pipe.onStreamRead (node:internal/stream_base_commons:190:23)
```

This happens because the extension exchange with the language server via std in/out of the child process. If any of the code that runs in the language server prints to the standard out, VS Code will complain (unless it is specifically formatted that way).
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@
"type": "string",
"default": "com.disneystreaming.smithy:smithy-language-server",
"description": "Language Server JAR to power the extension"
},
"smithyLsp.lspJavaOpt": {
"scope": "window",
"type": "string",
"description": "Java options to be forwarded to the Language Server launch. Options should be separated by spaces."
}
}
},
Expand Down
23 changes: 19 additions & 4 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ export function activate(context: ExtensionContext) {
.getConfiguration("smithyLsp")
.get("lspCoordinates", "`");

const lspJavaOpt: string | undefined = vscode.workspace
.getConfiguration("smithyLsp")
.get("lspJavaOpt");

const javaOptions: Array<string> = lspJavaOpt
? lspJavaOpt.split(" ").flatMap((o) => ["--java-opt", o])
: [];

return Promise.all([
getCoursierExecutable(context.globalStoragePath),
parseSmithyBuild(),
Expand All @@ -69,10 +77,17 @@ export function activate(context: ExtensionContext) {
? projectLanguageServerArtifact
: `${lspCoordinates}:${version}`;

const startServer = {
command: csBinaryPath,
args: ["launch", finalLanguageServerArtifact, "--ttl", "1h", "--", "0"],
};
const launcher = ["launch", finalLanguageServerArtifact];
// m2Local is relevant when `gradle` is used to publish locally
// coursier default resolvers are ivy2Local and maven central at the time of writing
const coursierOptions = ["--ttl", "1h", "--repository", "m2Local"].concat(
javaOptions
);
const split = ["--"];
const lspArguments = ["0"]; // port 0 means we use std in/out to exchange with the language server
const args = launcher.concat(coursierOptions, split, lspArguments);

const startServer = { command: csBinaryPath, args };

client = new LanguageClient(
"smithyLsp",
Expand Down

0 comments on commit 4eddb14

Please sign in to comment.