-
Notifications
You must be signed in to change notification settings - Fork 108
Use fastapi run for FastAPI debug configs, with file and project variants
#1048
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
base: main
Are you sure you want to change the base?
Changes from all commits
dcd8b80
06fc5bb
f855c11
94c97b0
dbe5c81
78d8e1c
8679707
e8179ca
46a83ea
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,65 +4,82 @@ | |
| 'use strict'; | ||
|
|
||
| import * as path from 'path'; | ||
| import * as fs from 'fs-extra'; | ||
| import { WorkspaceFolder } from 'vscode'; | ||
| import { MultiStepInput } from '../../../common/multiStepInput'; | ||
| import { DebugConfigStrings } from '../../../common/utils/localize'; | ||
| import { sendTelemetryEvent } from '../../../telemetry'; | ||
| import { EventName } from '../../../telemetry/constants'; | ||
| import { DebuggerTypeName } from '../../../constants'; | ||
| import { LaunchRequestArguments } from '../../../types'; | ||
| import { DebugConfigurationState, DebugConfigurationType } from '../../types'; | ||
| import { getFastApiPaths, tryResolveFastApiArgs } from '../utils/configuration'; | ||
|
|
||
| async function promptForAppPath( | ||
| input: MultiStepInput<DebugConfigurationState>, | ||
| value?: string, | ||
| ): Promise<string | undefined> { | ||
| const entered = await input.showInputBox({ | ||
| title: DebugConfigStrings.fastapi.enterAppPath.title, | ||
| prompt: DebugConfigStrings.fastapi.enterAppPath.prompt, | ||
| value: value ?? '', | ||
| validate: (v) => | ||
| Promise.resolve(v && v.trim().length > 0 ? undefined : DebugConfigStrings.fastapi.enterAppPath.invalid), | ||
| }); | ||
| return entered?.trim(); | ||
| } | ||
|
|
||
| export async function buildFastAPILaunchDebugConfiguration( | ||
| input: MultiStepInput<DebugConfigurationState>, | ||
| state: DebugConfigurationState, | ||
| ): Promise<void> { | ||
| const application = await getApplicationPath(state.folder); | ||
| let manuallyEnteredAValue: boolean | undefined; | ||
| const fastApiPaths = await getFastApiPaths(state.folder); | ||
| const autoArgs = state.folder ? tryResolveFastApiArgs(state.folder, fastApiPaths) : undefined; | ||
|
|
||
| let args: string[]; | ||
| let manuallyEnteredAValue = false; | ||
| if (autoArgs) { | ||
| args = autoArgs; | ||
| } else { | ||
| const workspaceRoot = state.folder?.uri.fsPath; | ||
| const prefill = | ||
| workspaceRoot && fastApiPaths.length > 0 ? path.relative(workspaceRoot, fastApiPaths[0].fsPath) : undefined; | ||
| const entered = await promptForAppPath(input, prefill); | ||
| if (!entered) { | ||
| return; | ||
| } | ||
| args = ['run', entered]; | ||
| manuallyEnteredAValue = true; | ||
| } | ||
|
|
||
| const config: Partial<LaunchRequestArguments> = { | ||
|
savannahostrowski marked this conversation as resolved.
|
||
| name: DebugConfigStrings.fastapi.snippet.name, | ||
| type: DebuggerTypeName, | ||
| request: 'launch', | ||
| module: 'uvicorn', | ||
| args: ['main:app', '--reload'], | ||
| module: 'fastapi', | ||
| args, | ||
| jinja: true, | ||
| }; | ||
|
|
||
| if (!application) { | ||
|
savannahostrowski marked this conversation as resolved.
|
||
| const selectedPath = await input.showInputBox({ | ||
| title: DebugConfigStrings.fastapi.enterAppPathOrNamePath.title, | ||
| value: 'main.py', | ||
| prompt: DebugConfigStrings.fastapi.enterAppPathOrNamePath.prompt, | ||
| validate: (value) => | ||
| Promise.resolve( | ||
| value && value.trim().length > 0 | ||
| ? undefined | ||
| : DebugConfigStrings.fastapi.enterAppPathOrNamePath.invalid, | ||
| ), | ||
| }); | ||
| if (selectedPath) { | ||
| manuallyEnteredAValue = true; | ||
| config.args = [`${path.basename(selectedPath, '.py').replace('/', '.')}:app`, '--reload']; | ||
| } else { | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| sendTelemetryEvent(EventName.DEBUGGER_CONFIGURATION_PROMPTS, undefined, { | ||
| configurationType: DebugConfigurationType.launchFastAPI, | ||
| autoDetectedFastAPIMainPyPath: !!application, | ||
| autoDetectedFastAPIMainPyPath: !!autoArgs, | ||
| manuallyEnteredAValue, | ||
| }); | ||
| Object.assign(state.config, config); | ||
| } | ||
| export async function getApplicationPath(folder: WorkspaceFolder | undefined): Promise<string | undefined> { | ||
| if (!folder) { | ||
| return undefined; | ||
| } | ||
| const defaultLocationOfManagePy = path.join(folder.uri.fsPath, 'main.py'); | ||
| if (await fs.pathExists(defaultLocationOfManagePy)) { | ||
| return 'main.py'; | ||
| } | ||
| return undefined; | ||
|
|
||
| export async function buildFastAPIWithFileLaunchDebugConfiguration( | ||
| _input: MultiStepInput<DebugConfigurationState>, | ||
| state: DebugConfigurationState, | ||
| ): Promise<void> { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Copilot generated: [verified]
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried this but extracting |
||
| const config: Partial<LaunchRequestArguments> = { | ||
| name: DebugConfigStrings.fastapi.snippetFile.name, | ||
| type: DebuggerTypeName, | ||
| request: 'launch', | ||
| module: 'fastapi', | ||
| args: ['run', '${file}'], | ||
| jinja: true, | ||
| }; | ||
| sendTelemetryEvent(EventName.DEBUGGER_CONFIGURATION_PROMPTS, undefined, { | ||
| configurationType: DebugConfigurationType.launchFastAPIWithFile, | ||
| }); | ||
| Object.assign(state.config, config); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ | |
| 'use strict'; | ||
|
|
||
| import * as fs from 'fs-extra'; | ||
| import * as path from 'path'; | ||
| import { MultiStepInput } from '../../../common/multiStepInput'; | ||
| import { sendTelemetryEvent } from '../../../telemetry'; | ||
| import { EventName } from '../../../telemetry/constants'; | ||
|
|
@@ -81,6 +82,14 @@ export async function getFastApiPaths(folder: WorkspaceFolder | undefined) { | |
| return fastApiPaths; | ||
| } | ||
|
|
||
| export function tryResolveFastApiArgs(folder: WorkspaceFolder, paths: readonly Uri[]): string[] | undefined { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Copilot generated: [verified] |
||
| if (paths.length !== 1) { | ||
| return undefined; | ||
| } | ||
| const relative = path.relative(folder.uri.fsPath, paths[0].fsPath); | ||
| return ['run', relative]; | ||
| } | ||
|
|
||
| export async function getFlaskPaths(folder: WorkspaceFolder | undefined) { | ||
| if (!folder) { | ||
| return []; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| 'use strict'; | ||
|
|
||
| import { expect } from 'chai'; | ||
| import * as path from 'path'; | ||
| import * as sinon from 'sinon'; | ||
| import { Uri, WorkspaceFolder } from 'vscode'; | ||
| import { DebuggerTypeName } from '../../../extension/constants'; | ||
| import { DynamicPythonDebugConfigurationService } from '../../../extension/debugger/configuration/dynamicdebugConfigurationService'; | ||
| import * as configurationUtils from '../../../extension/debugger/configuration/utils/configuration'; | ||
|
|
||
| suite('Debugging - Dynamic Debug Configuration Service', () => { | ||
| const folder: WorkspaceFolder = { uri: Uri.file('/work'), name: 'ws', index: 0 }; | ||
| let service: DynamicPythonDebugConfigurationService; | ||
| let getFastApiPathsStub: sinon.SinonStub; | ||
|
|
||
| setup(() => { | ||
| service = new DynamicPythonDebugConfigurationService(); | ||
| sinon.stub(configurationUtils, 'getDjangoPaths').resolves([]); | ||
| sinon.stub(configurationUtils, 'getFlaskPaths').resolves([]); | ||
| getFastApiPathsStub = sinon.stub(configurationUtils, 'getFastApiPaths'); | ||
| }); | ||
|
|
||
| teardown(() => { | ||
| sinon.restore(); | ||
| }); | ||
|
|
||
| const fastApiProviders = async () => { | ||
| const result = await service.provideDebugConfigurations(folder); | ||
| return (result ?? []).filter((c) => c.name?.includes('FastAPI')); | ||
| }; | ||
|
|
||
| const fileVariantConfig = { | ||
| name: 'Python Debugger: FastAPI File', | ||
| type: DebuggerTypeName, | ||
| request: 'launch', | ||
| module: 'fastapi', | ||
| args: ['run', '${file}'], | ||
| jinja: true, | ||
| }; | ||
|
|
||
| test('No FastAPI detected → no FastAPI configs offered', async () => { | ||
| getFastApiPathsStub.resolves([]); | ||
|
|
||
| const fastApi = await fastApiProviders(); | ||
| expect(fastApi).to.have.length(0); | ||
| }); | ||
|
|
||
| test('Single match at workspace root → project config uses resolved path, file variant uses ${file}', async () => { | ||
| getFastApiPathsStub.resolves([Uri.file('/work/main.py')]); | ||
|
|
||
| const fastApi = await fastApiProviders(); | ||
|
savannahostrowski marked this conversation as resolved.
|
||
| expect(fastApi).to.have.length(2); | ||
| expect(fastApi[0]).to.deep.equal({ | ||
| name: 'Python Debugger: FastAPI', | ||
| type: DebuggerTypeName, | ||
| request: 'launch', | ||
| module: 'fastapi', | ||
| args: ['run', 'main.py'], | ||
| jinja: true, | ||
| }); | ||
| expect(fastApi[1]).to.deep.equal(fileVariantConfig); | ||
| }); | ||
|
|
||
| test('Single match in subdirectory → project config passes path explicitly', async () => { | ||
| getFastApiPathsStub.resolves([Uri.file('/work/backend/app/main.py')]); | ||
|
|
||
| const fastApi = await fastApiProviders(); | ||
| expect(fastApi).to.have.length(2); | ||
| expect(fastApi[0]).to.deep.equal({ | ||
| name: 'Python Debugger: FastAPI', | ||
| type: DebuggerTypeName, | ||
| request: 'launch', | ||
| module: 'fastapi', | ||
| args: ['run', path.join('backend', 'app', 'main.py')], | ||
| jinja: true, | ||
| }); | ||
| expect(fastApi[1]).to.deep.equal(fileVariantConfig); | ||
| }); | ||
|
|
||
| test('Multiple matches → project config falls back to plain `fastapi run`', async () => { | ||
| getFastApiPathsStub.resolves([Uri.file('/work/svc-a/main.py'), Uri.file('/work/svc-b/main.py')]); | ||
|
|
||
| const fastApi = await fastApiProviders(); | ||
| expect(fastApi).to.have.length(2); | ||
| expect(fastApi[0]).to.deep.equal({ | ||
| name: 'Python Debugger: FastAPI', | ||
| type: DebuggerTypeName, | ||
| request: 'launch', | ||
| module: 'fastapi', | ||
| args: ['run'], | ||
| jinja: true, | ||
| }); | ||
| expect(fastApi[1]).to.deep.equal(fileVariantConfig); | ||
| }); | ||
| }); | ||
Uh oh!
There was an error while loading. Please reload this page.