Skip to content

Commit 375708b

Browse files
authored
fix: Prevent crashes on couple of commands from command palette (#216)
When run from the command palette (`Ctrl+Shift+P`), a couple of commands crash with an unhelpful error message. This commit adds a more helpful error message. PR checklist: - [x] Purpose of the code is [evident to future readers](https://semgrep.dev/docs/contributing/contributing-code/#explaining-code) - [x] Tests included or PR comment includes a reproducible test plan - [x] Documentation is up-to-date - [x] A changelog entry was for any user-facing change - [x] Change has no security implications (otherwise, ping security team)
1 parent b442382 commit 375708b

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

src/commands.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,12 @@ export function registerCommands(env: Environment): Disposable[] {
181181
vscode.commands.registerCommand(
182182
"semgrep.search",
183183
async (searchParams: SearchParams) => {
184+
if (!searchParams) {
185+
vscode.window.showErrorMessage(
186+
"Semgrep Search by pattern can only be run via the Semgrep Sidebar.",
187+
);
188+
return;
189+
}
184190
await handleSearch(env, searchParams);
185191
},
186192
),
@@ -207,17 +213,15 @@ export function registerCommands(env: Environment): Disposable[] {
207213

208214
vscode.commands.registerCommand(
209215
"semgrep.search.replace",
210-
async ({
211-
uri,
212-
fix,
213-
range,
214-
}: {
215-
uri: string;
216-
fix: string;
217-
range: vscode.Range;
218-
}) => {
216+
async (args: { uri?: string; fix?: string; range?: vscode.Range }) => {
217+
if (!args?.uri || !args?.fix || !args?.range) {
218+
vscode.window.showErrorMessage(
219+
"Semgrep Semantic Replace can only be run via the Semgrep Sidebar.",
220+
);
221+
return;
222+
}
219223
const edit = new vscode.WorkspaceEdit();
220-
edit.replace(vscode.Uri.parse(uri), range, fix);
224+
edit.replace(vscode.Uri.parse(args.uri), args.range, args.fix);
221225
await applyFixAndSave(edit);
222226
},
223227
),

src/search.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@ export async function handleSearch(
129129
env: Environment,
130130
searchParams: SearchParams,
131131
): Promise<void> {
132-
env.scanID = searchParams.scanID;
133132
if (searchParams != null) {
133+
env.scanID = searchParams.scanID;
134134
const results = await env.client?.sendRequest(
135135
search,
136136
searchParams.lspParams,

0 commit comments

Comments
 (0)