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

Dap ux enhancements #1

Merged
merged 6 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
],
"activationEvents": [
"onLanguage:noir",
"onStartupFinished"
"onStartupFinished",
"onDebug"
],
"main": "./out/extension",
"contributes": {
Expand Down
126 changes: 126 additions & 0 deletions src/debugger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
import {
debug,
window,
workspace,
DebugAdapterDescriptorFactory,
DebugSession,
DebugAdapterExecutable,
DebugAdapterDescriptor,
ExtensionContext,
OutputChannel,
DebugConfigurationProvider,
CancellationToken,
DebugConfiguration,
ProviderResult,
} from 'vscode';

import { spawn } from 'child_process';
import findNargo from './find-nargo';
import findNearestPackageFrom from './find-nearest-package';

let outputChannel: OutputChannel;

export function activateDebugger(context: ExtensionContext) {
outputChannel = window.createOutputChannel('NoirDebugger');

context.subscriptions.push(
debug.registerDebugAdapterDescriptorFactory('noir', new NoirDebugAdapterDescriptorFactory()),
debug.registerDebugConfigurationProvider('noir', new NoirDebugConfigurationProvider()),
debug.onDidTerminateDebugSession(() => {
outputChannel.appendLine("Debug session ended.");

Check failure on line 30 in src/debugger.ts

View workflow job for this annotation

GitHub Actions / eslint

Replace `"Debug·session·ended."` with `'Debug·session·ended.'`
}),
);
}

export class NoirDebugAdapterDescriptorFactory implements DebugAdapterDescriptorFactory {
async createDebugAdapterDescriptor(
_session: DebugSession,
_executable: DebugAdapterExecutable,
): ProviderResult<DebugAdapterDescriptor> {
const config = workspace.getConfiguration('noir');

const configuredNargoPath = config.get<string | undefined>('nargoPath');
const nargoPath = configuredNargoPath || findNargo();

return new DebugAdapterExecutable(nargoPath, ['dap']);
}
}

class NoirDebugConfigurationProvider implements DebugConfigurationProvider {
async resolveDebugConfiguration(folder: WorkspaceFolder | undefined, config: DebugConfiguration, token?: CancellationToken): ProviderResult<DebugConfiguration> {

Check failure on line 50 in src/debugger.ts

View workflow job for this annotation

GitHub Actions / eslint

Replace `folder:·WorkspaceFolder·|·undefined,·config:·DebugConfiguration,·token?:·CancellationToken` with `⏎····folder:·WorkspaceFolder·|·undefined,⏎····config:·DebugConfiguration,⏎····token?:·CancellationToken,⏎··`

Check warning on line 50 in src/debugger.ts

View workflow job for this annotation

GitHub Actions / eslint

'token' is defined but never used. Allowed unused args must match /^_/u
if (config.program || config.request == 'attach')

Check failure on line 51 in src/debugger.ts

View workflow job for this annotation

GitHub Actions / eslint

Delete `⏎·····`
return config;
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't we run preflight checks for user provided configurations as well? I'm not sure how this works, but if config.program probably means the configuration comes from the launch.json file. I think it makes sense to do a preflight using the provided configuration. Otherwise, this only applies to "new" debugging configurations.

Copy link
Author

Choose a reason for hiding this comment

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

This line is actually a remnant of some earlier tests, but with the final implementation it's effectively dead code, because:

  1. Our launch json doesn't include define program.
  2. We're never attaching to a running debugger.

So I removed this line.

But you're right in that this is ignoring projectFolder when provided through a launch.json.

This makes me think though, that we should remove the requirement for projectFolder from the template launch.json, because in the most common case you will want it to be discovered based on the file you want to debug. We can still allow people to manually specify tho, so I'll add those tweaks.


if (window.activeTextEditor?.document.languageId != 'noir')
return window.showInformationMessage("Select a Noir file to debug");

Check failure on line 55 in src/debugger.ts

View workflow job for this annotation

GitHub Actions / eslint

Replace `"Select·a·Noir·file·to·debug"` with `'Select·a·Noir·file·to·debug'`

const currentFilePath = window.activeTextEditor.document.uri.fsPath;
let currentProjectFolder = findNearestPackageFrom(currentFilePath);

Check failure on line 58 in src/debugger.ts

View workflow job for this annotation

GitHub Actions / eslint

'currentProjectFolder' is never reassigned. Use 'const' instead

const workspaceConfig = workspace.getConfiguration('noir');
const nargoPath = workspaceConfig.get<string | undefined>('nargoPath') || findNargo();

outputChannel.clear();
outputChannel.appendLine(`Using nargo at ${nargoPath}`);
outputChannel.appendLine("Compiling Noir project...");

Check failure on line 65 in src/debugger.ts

View workflow job for this annotation

GitHub Actions / eslint

Replace `"Compiling·Noir·project..."` with `'Compiling·Noir·project...'`
outputChannel.appendLine("");

Check failure on line 66 in src/debugger.ts

View workflow job for this annotation

GitHub Actions / eslint

Replace `""` with `''`

// Run Nargo's DAP in "pre-flight mode", which test runs
// the DAP initialization code without actually starting the DAP server.
// This lets us gracefully handle errors that happen *before*
// the DAP loop is established, which otherwise are considered
// "out of band".
const preflightCheck = spawn(nargoPath, [
'dap',
'--preflight-check',
'--preflight-project-folder',
currentProjectFolder

Check failure on line 77 in src/debugger.ts

View workflow job for this annotation

GitHub Actions / eslint

Insert `,`
]);

// Create a promise to block until the preflight check child process
// ends.
let ready: (r: Boolean) => void;

Check failure on line 82 in src/debugger.ts

View workflow job for this annotation

GitHub Actions / eslint

Don't use `Boolean` as a type. Use boolean instead
const preflightCheckMonitor = new Promise((resolve) => ready = resolve);

Check failure on line 83 in src/debugger.ts

View workflow job for this annotation

GitHub Actions / eslint

Replace `ready·=·resolve` with `(ready·=·resolve)`

preflightCheck.stderr.on('data', ev_buffer => preflightCheckPrinter(ev_buffer, outputChannel));
preflightCheck.stdout.on('data', ev_buffer => preflightCheckPrinter(ev_buffer, outputChannel));
preflightCheck.on('data', ev_buffer => preflightCheckPrinter(ev_buffer, outputChannel));
preflightCheck.on('exit', async code => {
if (code !== 0) {
outputChannel.appendLine(`Exited with code ${code}`);
}
ready(code == 0);
});

if (!await preflightCheckMonitor) {
outputChannel.show();
throw new Error("Error launching debugger. Please inspect the Output pane for more details.");
} else {
outputChannel.appendLine("Starting debugger session...");
}

return {
type: 'noir',
name: 'Noir binary package',
request: 'launch',
program: currentFilePath,
projectFolder: currentProjectFolder,
}
}
}

/**
* Takes stderr or stdout output from the Nargo's DAP
* preflight check and formats it in an Output pane friendly way,
* by removing all special characters.
*
* Note: VS Code's output panes only support plain text.
*
*/
function preflightCheckPrinter(buffer: Buffer, output: OutputChannel) {
const formattedOutput = buffer.toString()
.replace(/\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])/g, '')
.replace(/[^ -~\n\t]/g, '');

output.appendLine(formattedOutput);
}
62 changes: 6 additions & 56 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
window,
workspace,
commands,
debug,
ExtensionContext,
Disposable,
TextDocument,
Expand All @@ -35,39 +34,19 @@
TaskPanelKind,
TaskGroup,
ProcessExecution,
window,
ProgressLocation,
DebugAdapterDescriptorFactory,
DebugConfigurationProvider,
CancellationToken,
DebugConfiguration,
DebugAdapterDescriptor,
DebugAdapterExecutable,
DebugSession,
ProviderResult,
ProgressLocation,
} from 'vscode';

import { activateDebugger } from './debugger';

import { languageId } from './constants';
import Client from './client';
import findNargo from './find-nargo';
import findNearestPackageFrom from './find-nearest-package';
import { lspClients, editorLineDecorationManager } from './noir';

const activeCommands: Map<string, Disposable> = new Map();

class NoirDebugAdapterDescriptorFactory implements DebugAdapterDescriptorFactory {
createDebugAdapterDescriptor(
_session: DebugSession,
_executable: DebugAdapterExecutable,
): ProviderResult<DebugAdapterDescriptor> {
const config = workspace.getConfiguration('noir');

const configuredNargoPath = config.get<string | undefined>('nargoPath');
const nargoPath = configuredNargoPath || findNargo();

return new DebugAdapterExecutable(nargoPath, ['dap']);
}
}
let outputChannel: OutputChannel;

Check warning on line 49 in src/extension.ts

View workflow job for this annotation

GitHub Actions / eslint

'outputChannel' is defined but never used. Allowed unused vars must match /^_/u

const activeMutex: Set<string> = new Set();

Expand Down Expand Up @@ -359,40 +338,11 @@
context.subscriptions.push(disposable);
}

context.subscriptions.push(
debug.registerDebugAdapterDescriptorFactory('noir', new NoirDebugAdapterDescriptorFactory()),
);

context.subscriptions.push(
debug.registerDebugConfigurationProvider('noir', new NoirDebugConfigurationProvider()),
);
activateDebugger(context);
}

export async function deactivate(): Promise<void> {
for (const client of lspClients.values()) {
await client.stop();
}
}

class NoirDebugConfigurationProvider implements DebugConfigurationProvider {
resolveDebugConfiguration(folder: WorkspaceFolder | undefined, config: DebugConfiguration, token?: CancellationToken): ProviderResult<DebugConfiguration> {
if (config.program || config.request == 'attach')
return config;

if (window.activeTextEditor?.document.languageId != 'noir')
return window.showInformationMessage("Select a Noir file to debug").then(_ => {
return null;
});

const currentFilePath = window.activeTextEditor.document.uri.fsPath;
let currentProject = findNearestPackageFrom(currentFilePath);

return {
type: 'noir',
name: 'Noir binary package',
request: 'launch',
program: currentFilePath,
projectFolder: currentProject,
}
}
}
}