Use fastapi run for FastAPI debug configs, with file and project variants#1048
Use fastapi run for FastAPI debug configs, with file and project variants#1048savannahostrowski wants to merge 10 commits into
fastapi run for FastAPI debug configs, with file and project variants#1048Conversation
|
/azp run |
|
@microsoft-github-policy-service agree [company="FastAPI Labs"] |
|
Azure Pipelines could not run because the pipeline triggers exclude this branch/path. |
|
@microsoft-github-policy-service agree company="FastAPI Labs" |
|
So just closing the loop here and reconciling threads from #1045, since the PR only changes what the snippet flow writes when a user newly invokes "Add Configuration" and what the dynamic provider suggests in-memory for users without a launch.json, anyone with module: uvicorn already in their launch.json keeps that exact config and continues to work. I've also added tiered auto-detection so monorepo/nested layouts don't break out of the box: Auto-detection (applied in both flows):
The snippet flow also prompts on 0 matches (with no pre-fill). The dynamic provider doesn't surface FastAPI configs at all if 0 are detected. |
| export async function buildFastAPIWithFileLaunchDebugConfiguration( | ||
| _input: MultiStepInput<DebugConfigurationState>, | ||
| state: DebugConfigurationState, | ||
| ): Promise<void> { |
There was a problem hiding this comment.
Copilot generated:
The Architect notes the ['run', '${file}'] config literal now lives in two places (here and dynamicdebugConfigurationService.ts), and the ['run'] default is encoded both in tryResolveFastApiArgs and as ?? ['run'] in the dynamic caller. Hoist to a shared constant / small helper to prevent drift. Low priority.
[verified]
There was a problem hiding this comment.
I tried this but extracting ['run', '${file}'] into a readonly string[] constant required spreading at every call site ([...FASTAPI_RUN_FILE_ARGS]), which was longer than the inline literal.
| return fastApiPaths; | ||
| } | ||
|
|
||
| export function tryResolveFastApiArgs(folder: WorkspaceFolder, paths: readonly Uri[]): string[] | undefined { |
There was a problem hiding this comment.
Copilot generated:
Unresolved from prior review (Medium): path.relative emits OS-native separators, so a single subdir match persists "args": ["run", "backend\\app\\main.py"] on Windows. A launch.json shared to macOS/Linux then runs fastapi run backend\app\main.py, which treats backslashes literally and fails despite correct detection. The Skeptic notes the central fix actually broadened exposure here (root single-matches previously emitted separator-free ['run']). Normalize to POSIX in the persisted arg (e.g. relative.split(path.sep).join('/')) and confirm fastapi run accepts forward slashes on Windows.
[verified]
There was a problem hiding this comment.
@savannahostrowski what did you think of this idea? Does fastapi work with forward slashes on Windows?
| const appPyPath = path.join(folder.uri.fsPath, 'main.py'); | ||
| pathExistsStub.withArgs(appPyPath).resolves(false); | ||
| const file = await fastApiLaunch.getApplicationPath(folder); | ||
| const state = { config: {}, folder }; |
There was a problem hiding this comment.
Copilot generated:
The Skeptic notes the nested-match expectations use path.join('backend','app','main.py'), which produces the resolver's own OS-specific output on every platform — the separator assertion is tautological and will never catch the portability bug above. Once the persisted arg is normalized to POSIX, assert the literal 'backend/app/main.py' instead.
[verified]
There was a problem hiding this comment.
Of course that other change would affect this test if you modified it to use forward slashes.
46a83ea to
c8434ae
Compare
|
@rchiodo I addressed most of the comments you've left. The two outstanding are tied to the path-separator decision I called out in this thread. Happy to revisit if you'd prefer to normalize across all three providers, but didn't want to diverge from the existing pattern unilaterally. Let me know if there's anything else you'd like changed! |
|
@savannahostrowski can you merge/rebase on main? The tests fail because of an unrelated issue which I just submitted a fix for on the main branch. |
|
🔒 Automated review in progress — @rchiodo is auto-reviewing this PR. |
| module: 'uvicorn', | ||
| args: ['main:app', '--reload'], | ||
| module: 'fastapi', | ||
| args, |
There was a problem hiding this comment.
module: 'fastapi' runs python -m fastapi run …, which requires fastapi-cli (fastapi[standard]) or FastAPI ≥ 0.111. The previous module: 'uvicorn' worked with a bare uvicorn install, so environments that debugged fine before can now fail immediately with No module named fastapi.__main__. Consider detecting CLI availability or surfacing an actionable message, and document the minimum FastAPI/fastapi-cli requirement.
| if (paths.length !== 1) { | ||
| return undefined; | ||
| } | ||
| const relative = path.relative(folder.uri.fsPath, paths[0].fsPath); |
There was a problem hiding this comment.
path.relative yields OS-native separators (e.g. backend\app\main.py on Windows) that get baked into args and persisted to launch.json. A committed launch.json should be portable; consider normalizing to POSIX separators (fastapi run accepts / on Windows) so a shared config works cross-platform.
| } | ||
| const relative = path.relative(folder.uri.fsPath, paths[0].fsPath); | ||
| return ['run', relative]; | ||
| } |
There was a problem hiding this comment.
Multi-root workspace edge case: getFastApiPaths uses workspace-wide workspace.findFiles, so tryResolveFastApiArgs(folder, paths) can compute path.relative(folder, fileInAnotherFolder) → ..\otherFolder\main.py, which is then passed straight to fastapi run and points outside the folder. Consider filtering detected paths to the target folder, or treating cross-folder matches as "not a single match" and falling back to plain run.
| request: 'launch', | ||
| module: 'fastapi', | ||
| args: ['run', '${file}'], | ||
| jinja: true, |
There was a problem hiding this comment.
The "FastAPI File" (${file}) variant is only pushed inside if (fastApiPaths?.length), so a user whose app doesn't match the app\s*=\s*FastAPI\( regex (e.g. application = FastAPI(), a factory pattern, or app in __init__.py) gets neither config — even though fastapi run ${file} would work on their open file. The ${file} variant doesn't depend on project detection; consider offering it independently of the detection gate.
| } | ||
| const relative = path.relative(folder.uri.fsPath, paths[0].fsPath); | ||
| return ['run', relative]; | ||
| } |
There was a problem hiding this comment.
tryResolveFastApiArgs returns undefined for BOTH "0 matches" and ">1 matches", and the two callers resolve that ambiguity differently (dynamic provider → ?? ['run']; interactive builder → prompt). This is intentional and tested, but undocumented. Add a one-line comment stating it returns args only for an unambiguous single match and that callers decide how to handle 0-or-many, so a future maintainer doesn't "fix" it and break one path.
| const state = { config: {}, folder }; | ||
| getFastApiPathsStub.resolves([Uri.parse(path.join('one', 'two', 'backend', 'app', 'main.py'))]); | ||
|
|
||
| await fastApiLaunch.buildFastAPILaunchDebugConfiguration(instance(input), state); |
There was a problem hiding this comment.
The prefill computed at fastapiLaunch.ts (path.relative(workspaceRoot, fastApiPaths[0].fsPath)) is never asserted — the multi-match test stubs showInputBox but doesn't check the value (prefill) argument, so a wrong prefill would pass CI. Also untested: the telemetry fields autoDetectedFastAPIMainPyPath/manuallyEnteredAValue and the state.folder === undefined branch. Consider adding assertions for the prefill value and telemetry payload.
|
Approach looks reasonable. The main things worth aligning on before marking review-ready: the switch from |
rchiodo
left a comment
There was a problem hiding this comment.
Approved via Review Center.
A draft for #1045 - opening this to make the proposed approach a bit more concrete. Happy to adjust based on the open discussion before marking it review ready (there are some open questions to align on in the issue).
This updates the FastAPI debug configuration (both the
launch.jsonsnippet and the dynamic provider) to use thefastapiCLI instead of invokinguvicorndirectly:Debug FastAPI with current file - runs
fastapi run ${file}against the currently active file. This is optimal for single-file apps or quickly debugging a specific entry point.Debug FastAPI - runs plain
fastapi runand lets the CLI discover the app viapyproject.tomlor default file locations.This replaces the previous
uvicorn main:app --reloadconfig and its hand-rolledmain.pydetection.I have run the tests and manually tested as well.