forked from noir-lang/vscode-noir
-
Notifications
You must be signed in to change notification settings - Fork 0
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
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d58cd79
Gracefully handle debugger loading/init errors
mverzilli 61179ad
Refactor: extract debugger functionality to own file
mverzilli 49f0de8
Fix lint errors
mverzilli 85c582a
Remove unnecessary line
mverzilli 478bac9
Properly handle launch.json if exists
mverzilli 3addaaf
Add a bit more context on preflight check
mverzilli File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."); | ||
}), | ||
); | ||
} | ||
|
||
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 GitHub Actions / eslint
|
||
if (config.program || config.request == 'attach') | ||
return config; | ||
|
||
if (window.activeTextEditor?.document.languageId != 'noir') | ||
return window.showInformationMessage("Select a Noir file to debug"); | ||
|
||
const currentFilePath = window.activeTextEditor.document.uri.fsPath; | ||
let currentProjectFolder = findNearestPackageFrom(currentFilePath); | ||
|
||
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..."); | ||
outputChannel.appendLine(""); | ||
|
||
// 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 | ||
]); | ||
|
||
// Create a promise to block until the preflight check child process | ||
// ends. | ||
let ready: (r: Boolean) => void; | ||
const preflightCheckMonitor = new Promise((resolve) => 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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 thelaunch.json
file. I think it makes sense to do a preflight using the provided configuration. Otherwise, this only applies to "new" debugging configurations.There was a problem hiding this comment.
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:
program
.So I removed this line.
But you're right in that this is ignoring
projectFolder
when provided through alaunch.json
.This makes me think though, that we should remove the requirement for
projectFolder
from the templatelaunch.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.