-
Notifications
You must be signed in to change notification settings - Fork 110
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 6 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,78 @@ | |
| '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[]; | ||
| 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]; | ||
| } | ||
|
|
||
| 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, | ||
| 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 |
|---|---|---|
|
|
@@ -5,57 +5,133 @@ | |
|
|
||
| import { expect } from 'chai'; | ||
| import * as path from 'path'; | ||
| import * as fs from 'fs-extra'; | ||
| import * as sinon from 'sinon'; | ||
| import { anything, instance, mock, when } from 'ts-mockito'; | ||
| import { Uri } from 'vscode'; | ||
| import { DebugConfigStrings } from '../../../../extension/common/utils/localize'; | ||
| import { DebuggerTypeName } from '../../../../extension/constants'; | ||
| import * as fastApiLaunch from '../../../../extension/debugger/configuration/providers/fastapiLaunch'; | ||
| import * as configurationUtils from '../../../../extension/debugger/configuration/utils/configuration'; | ||
| import { DebugConfigurationState } from '../../../../extension/debugger/types'; | ||
| import { MultiStepInput } from '../../../../extension/common/multiStepInput'; | ||
|
|
||
| suite('Debugging - Configuration Provider FastAPI', () => { | ||
| let input: MultiStepInput<DebugConfigurationState>; | ||
| let pathExistsStub: sinon.SinonStub; | ||
| let getFastApiPathsStub: sinon.SinonStub; | ||
|
|
||
| setup(() => { | ||
| input = mock<MultiStepInput<DebugConfigurationState>>(MultiStepInput); | ||
| pathExistsStub = sinon.stub(fs, 'pathExists'); | ||
| getFastApiPathsStub = sinon.stub(configurationUtils, 'getFastApiPaths'); | ||
| }); | ||
|
|
||
| teardown(() => { | ||
| sinon.restore(); | ||
| }); | ||
| test("getApplicationPath should return undefined if file doesn't exist", async () => { | ||
|
|
||
| test('Single match at workspace root → plain `fastapi run`', async () => { | ||
| const folder = { uri: Uri.parse(path.join('one', 'two')), name: '1', index: 0 }; | ||
| const appPyPath = path.join(folder.uri.fsPath, 'main.py'); | ||
| pathExistsStub.withArgs(appPyPath).resolves(false); | ||
| const file = await fastApiLaunch.getApplicationPath(folder); | ||
| const state = { config: {}, folder }; | ||
|
savannahostrowski marked this conversation as resolved.
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] |
||
| getFastApiPathsStub.resolves([Uri.parse(path.join('one', 'two', 'main.py'))]); | ||
|
|
||
| await fastApiLaunch.buildFastAPILaunchDebugConfiguration(instance(input), state); | ||
|
|
||
| expect(file).to.be.equal(undefined, 'Should return undefined'); | ||
| const config = { | ||
| name: DebugConfigStrings.fastapi.snippet.name, | ||
| type: DebuggerTypeName, | ||
| request: 'launch', | ||
| module: 'fastapi', | ||
| args: ['run'], | ||
| jinja: true, | ||
| }; | ||
|
|
||
| expect(state.config).to.be.deep.equal(config); | ||
| }); | ||
| test('getApplicationPath should find path', async () => { | ||
|
|
||
| test('Single match in subdirectory → passes path explicitly', async () => { | ||
| const folder = { uri: Uri.parse(path.join('one', 'two')), name: '1', index: 0 }; | ||
| const appPyPath = path.join(folder.uri.fsPath, 'main.py'); | ||
| pathExistsStub.withArgs(appPyPath).resolves(true); | ||
| const file = await fastApiLaunch.getApplicationPath(folder); | ||
| const state = { config: {}, folder }; | ||
| getFastApiPathsStub.resolves([Uri.parse(path.join('one', 'two', 'backend', 'app', 'main.py'))]); | ||
|
|
||
| expect(file).to.be.equal('main.py'); | ||
| await fastApiLaunch.buildFastAPILaunchDebugConfiguration(instance(input), state); | ||
|
|
||
| const config = { | ||
| name: DebugConfigStrings.fastapi.snippet.name, | ||
| type: DebuggerTypeName, | ||
| request: 'launch', | ||
| module: 'fastapi', | ||
| args: ['run', path.join('backend', 'app', 'main.py')], | ||
| jinja: true, | ||
| }; | ||
|
|
||
| expect(state.config).to.be.deep.equal(config); | ||
| }); | ||
| test('Launch JSON with selected app path', async () => { | ||
|
|
||
| test('No matches → prompts the user and uses the entered path', async () => { | ||
| const folder = { uri: Uri.parse(path.join('one', 'two')), name: '1', index: 0 }; | ||
| const state = { config: {}, folder }; | ||
| getFastApiPathsStub.resolves([]); | ||
| when(input.showInputBox(anything())).thenResolve('custom/main.py'); | ||
|
|
||
| await fastApiLaunch.buildFastAPILaunchDebugConfiguration(instance(input), state); | ||
|
|
||
| const config = { | ||
| name: DebugConfigStrings.fastapi.snippet.name, | ||
| type: DebuggerTypeName, | ||
| request: 'launch', | ||
| module: 'fastapi', | ||
| args: ['run', 'custom/main.py'], | ||
| jinja: true, | ||
| }; | ||
|
|
||
| when(input.showInputBox(anything())).thenResolve('main'); | ||
| expect(state.config).to.be.deep.equal(config); | ||
| }); | ||
|
|
||
| test('Multiple matches → prompts the user and uses the entered path', async () => { | ||
| const folder = { uri: Uri.parse(path.join('one', 'two')), name: '1', index: 0 }; | ||
| const state = { config: {}, folder }; | ||
| getFastApiPathsStub.resolves([ | ||
| Uri.parse(path.join('one', 'two', 'svc-a', 'main.py')), | ||
| Uri.parse(path.join('one', 'two', 'svc-b', 'main.py')), | ||
| ]); | ||
| when(input.showInputBox(anything())).thenResolve(path.join('svc-a', 'main.py')); | ||
|
|
||
| await fastApiLaunch.buildFastAPILaunchDebugConfiguration(instance(input), state); | ||
|
|
||
| const config = { | ||
| name: DebugConfigStrings.fastapi.snippet.name, | ||
| type: DebuggerTypeName, | ||
| request: 'launch', | ||
| module: 'uvicorn', | ||
| args: ['main:app', '--reload'], | ||
| module: 'fastapi', | ||
| args: ['run', path.join('svc-a', 'main.py')], | ||
| jinja: true, | ||
| }; | ||
|
|
||
| expect(state.config).to.be.deep.equal(config); | ||
| }); | ||
|
|
||
| test('User cancels prompt → config is not populated', async () => { | ||
| const folder = { uri: Uri.parse(path.join('one', 'two')), name: '1', index: 0 }; | ||
| const state = { config: {}, folder }; | ||
| getFastApiPathsStub.resolves([]); | ||
| when(input.showInputBox(anything())).thenResolve(undefined); | ||
|
|
||
| await fastApiLaunch.buildFastAPILaunchDebugConfiguration(instance(input), state); | ||
|
|
||
| expect(state.config).to.be.deep.equal({}); | ||
| }); | ||
|
|
||
| test('Launch JSON with file configuration', async () => { | ||
| const folder = { uri: Uri.parse(path.join('one', 'two')), name: '1', index: 0 }; | ||
| const state = { config: {}, folder }; | ||
|
|
||
| await fastApiLaunch.buildFastAPIWithFileLaunchDebugConfiguration(instance(input), state); | ||
|
|
||
| const config = { | ||
| name: DebugConfigStrings.fastapi.snippetFile.name, | ||
| type: DebuggerTypeName, | ||
| request: 'launch', | ||
| module: 'fastapi', | ||
| args: ['run', '${file}'], | ||
| jinja: true, | ||
| }; | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.