Skip to content

Add dynamic FastAPI debug config #19505

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

Merged
Merged
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { IDynamicDebugConfigurationService } from '../types';
import { IFileSystem } from '../../../common/platform/types';
import { IPathUtils } from '../../../common/types';
import { DebuggerTypeName } from '../../constants';
import { asyncFilter } from '../../../common/utils/arrayUtils';

const workspaceFolderToken = '${workspaceFolder}';

Expand All @@ -31,19 +32,64 @@ export class DynamicPythonDebugConfigurationService implements IDynamicDebugConf
justMyCode: true,
});

const djangoManagePath = await this.fs.search(path.join(folder.uri.fsPath, '**/manage.py'));
if (djangoManagePath.length) {
const managePath = path.relative(folder.uri.fsPath, djangoManagePath[0]);
const djangoManagePath = await this.getDjangoPath(folder);
if (djangoManagePath) {
providers.push({
name: 'Dynamic Python: Django',
type: DebuggerTypeName,
request: 'launch',
program: `${workspaceFolderToken}${this.pathUtils.separator}${managePath}`,
program: `${workspaceFolderToken}${this.pathUtils.separator}${djangoManagePath}`,
args: ['runserver'],
django: true,
justMyCode: true,
});
}

let fastApiPath = await this.getFastApiPath(folder);
if (fastApiPath) {
fastApiPath = path
.relative(folder.uri.fsPath, fastApiPath)
.replaceAll(this.pathUtils.separator, '.')
.replace('.py', '');
providers.push({
name: 'Dynamic Python: FastAPI',
type: DebuggerTypeName,
request: 'launch',
module: 'uvicorn',
args: [`${fastApiPath}:app`],
jinja: true,
justMyCode: true,
});
}

return providers;
}

private async getDjangoPath(folder: WorkspaceFolder) {
const possiblePaths = await this.getPossiblePaths(folder, ['manage.py', '*/manage.py']);

return possiblePaths.length ? path.relative(folder.uri.fsPath, possiblePaths[0]) : null;
}

private async getFastApiPath(folder: WorkspaceFolder) {
const possiblePaths = await this.getPossiblePaths(folder, ['main.py', 'app.py', '*/main.py', '*/app.py']);
const regExpression = /app\s*=\s*FastAPI\(/;
const flaskPaths = await asyncFilter(possiblePaths, async (possiblePath) =>
Copy link

Choose a reason for hiding this comment

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

Fastapi paths

regExpression.exec((await this.fs.readFile(possiblePath)).toString()),
);

return flaskPaths.length ? flaskPaths[0] : null;
}

private async getPossiblePaths(folder: WorkspaceFolder, globPatterns: string[]): Promise<string[]> {
const foundPathsPromises = (await Promise.allSettled(
globPatterns.map(
async (pattern): Promise<string[]> => this.fs.search(path.join(folder.uri.fsPath, pattern)),
),
)) as { status: string; value: [] }[];
const results: string[] = [];
foundPathsPromises.forEach((result) => results.push(...result.value));

return results;
}
}