-
Notifications
You must be signed in to change notification settings - Fork 61
feat: report diagnostics for malformed PEP 723 inline script metadata #1785
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
Stella Huang (StellaHuang95)
merged 2 commits into
microsoft:main
from
StellaHuang95:stellahuang-microsoft-pep723-inline-script-research
Sep 15, 2026
Merged
Changes from all commits
Commits
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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,229 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| import { | ||
| Diagnostic, | ||
| DiagnosticCollection, | ||
| DiagnosticSeverity, | ||
| Disposable, | ||
| languages, | ||
| Range, | ||
| TextDocument, | ||
| Uri, | ||
| } from 'vscode'; | ||
| import { | ||
| InlineScriptMetadataProblem, | ||
| parseInlineScriptMetadata, | ||
| sliceHeaderBytes, | ||
| } from '../../common/inlineScript/metadata'; | ||
| import { getInlineScriptRoutingKey } from '../../common/inlineScript/routingRegistry'; | ||
| import { InlineScriptStrings } from '../../common/localize'; | ||
| import { traceVerbose } from '../../common/logging'; | ||
| import { createSimpleDebounce, SimpleDebounce } from '../../common/utils/debounce'; | ||
| import { isSameOrParentPath } from '../../common/utils/pathUtils'; | ||
| import { | ||
| getOpenTextDocuments, | ||
| onDidChangeTextDocument, | ||
| onDidCloseTextDocument, | ||
| onDidDeleteFiles, | ||
| onDidOpenTextDocument, | ||
| onDidRenameFiles, | ||
| onDidSaveTextDocument, | ||
| } from '../../common/workspace.apis'; | ||
|
|
||
| const VALIDATION_DEBOUNCE_MS = 300; | ||
|
|
||
| const DIAGNOSTIC_COLLECTION_NAME = 'python-envs-inline-script'; | ||
|
|
||
| /** Publishes diagnostics for malformed PEP 723 inline script metadata, validating the live buffer. */ | ||
| export class InlineScriptDiagnosticsPublisher implements Disposable { | ||
| private readonly subscriptions: Disposable[] = []; | ||
| // `createSimpleDebounce` owns one timer, so a shared instance would let | ||
| // edits in one file cancel another's pending validation. | ||
| private readonly pending = new Map<string, { debounce: SimpleDebounce; document: TextDocument }>(); | ||
| private readonly published = new Map<string, Uri>(); | ||
| private disposed = false; | ||
|
|
||
| constructor(private readonly collection: DiagnosticCollection) {} | ||
|
|
||
| /** Documents open at activation are replayed via `setImmediate`, because `onLanguage:python` fires after editors are restored. */ | ||
| public activate(): void { | ||
| this.subscriptions.push( | ||
| onDidOpenTextDocument((doc) => this.validate(doc)), | ||
| onDidSaveTextDocument((doc) => this.validate(doc)), | ||
| onDidChangeTextDocument((e) => this.scheduleValidation(e.document)), | ||
| onDidCloseTextDocument((doc) => this.clear(doc.uri)), | ||
| onDidDeleteFiles((e) => e.files.forEach((uri) => this.clearTree(uri))), | ||
| onDidRenameFiles((e) => | ||
| e.files.forEach((file) => { | ||
| this.clearTree(file.oldUri); | ||
| this.revalidateIfOpen(file.newUri); | ||
| }), | ||
| ), | ||
| ); | ||
| const handle = setImmediate(() => this.replayOpenDocuments()); | ||
| this.subscriptions.push(new Disposable(() => clearImmediate(handle))); | ||
| } | ||
|
|
||
| public dispose(): void { | ||
| this.disposed = true; | ||
| this.subscriptions.forEach((s) => s.dispose()); | ||
| this.subscriptions.length = 0; | ||
| this.pending.forEach((entry) => entry.debounce.dispose()); | ||
| this.pending.clear(); | ||
| this.published.clear(); | ||
| this.collection.dispose(); | ||
| } | ||
|
|
||
| private replayOpenDocuments(): void { | ||
| if (this.disposed) { | ||
| return; | ||
| } | ||
| const docs = getOpenTextDocuments().filter((doc) => shouldValidateUri(doc.uri)); | ||
| traceVerbose(`inlineScriptDiagnostics: activation replay over ${docs.length} candidate .py document(s)`); | ||
| for (const doc of docs) { | ||
| this.validate(doc); | ||
| } | ||
| } | ||
|
|
||
| private scheduleValidation(document: TextDocument): void { | ||
| if (this.disposed || !shouldValidateUri(document.uri)) { | ||
| return; | ||
| } | ||
| const key = document.uri.toString(); | ||
| const existing = this.pending.get(key); | ||
| if (existing) { | ||
| existing.document = document; | ||
| existing.debounce.trigger(); | ||
| return; | ||
| } | ||
| const entry = { | ||
| document, | ||
| debounce: createSimpleDebounce(VALIDATION_DEBOUNCE_MS, () => { | ||
| const queued = this.pending.get(key); | ||
| this.pending.delete(key); | ||
| if (queued) { | ||
| this.validate(queued.document); | ||
| } | ||
| }), | ||
| }; | ||
| this.pending.set(key, entry); | ||
| entry.debounce.trigger(); | ||
| } | ||
|
|
||
| private validate(document: TextDocument): void { | ||
| if (this.disposed || !shouldValidateUri(document.uri)) { | ||
| return; | ||
| } | ||
| this.cancelPending(document.uri); | ||
|
|
||
| const uri = document.uri; | ||
| const result = parseInlineScriptMetadata(sliceHeaderBytes(document.getText()), uri.fsPath); | ||
| const problems = result.kind === 'none' ? [] : result.problems; | ||
| if (problems.length === 0) { | ||
| this.clear(uri); | ||
| return; | ||
| } | ||
|
|
||
| const diagnostics = problems.map((problem) => toDiagnostic(document, problem)); | ||
| this.collection.set(uri, diagnostics); | ||
| this.published.set(uri.toString(), uri); | ||
| traceVerbose( | ||
| `inlineScriptDiagnostics: published ${diagnostics.length} problem(s) for ${uri.fsPath}: ` + | ||
| problems.map((p) => p.code).join(', '), | ||
| ); | ||
| } | ||
|
|
||
| private revalidateIfOpen(uri: Uri): void { | ||
| if (this.disposed || !shouldValidateUri(uri)) { | ||
| return; | ||
| } | ||
| const key = uri.toString(); | ||
| const doc = getOpenTextDocuments().find((d) => d.uri.toString() === key); | ||
| if (doc) { | ||
| this.validate(doc); | ||
| } | ||
| } | ||
|
|
||
| private clear(uri: Uri): void { | ||
| this.cancelPending(uri); | ||
| const key = uri.toString(); | ||
| if (this.published.delete(key)) { | ||
| this.collection.delete(uri); | ||
| } | ||
| } | ||
|
|
||
| private clearTree(uri: Uri): void { | ||
| this.clear(uri); | ||
| if (uri.scheme !== 'file') { | ||
| return; | ||
| } | ||
| // Pending entries have never published, so `published` alone would leave a | ||
| // debounced validation to fire for a file that no longer exists. | ||
| const tracked = [ | ||
| ...this.published.values(), | ||
| ...Array.from(this.pending.values(), (entry) => entry.document.uri), | ||
| ]; | ||
| for (const candidate of tracked) { | ||
| if (candidate.scheme === 'file' && isSameOrParentPath(uri.fsPath, candidate.fsPath)) { | ||
| this.clear(candidate); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private cancelPending(uri: Uri): void { | ||
| const key = uri.toString(); | ||
| const entry = this.pending.get(key); | ||
| if (entry) { | ||
| entry.debounce.dispose(); | ||
| this.pending.delete(key); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** Whether inline script metadata is meaningful for `uri` — a local `.py` file. */ | ||
| export function shouldValidateUri(uri: Uri): boolean { | ||
| return getInlineScriptRoutingKey(uri) !== undefined; | ||
| } | ||
|
|
||
| function toDiagnostic(document: TextDocument, problem: InlineScriptMetadataProblem): Diagnostic { | ||
| const range = new Range( | ||
| document.positionAt(problem.sourceRange.start), | ||
| document.positionAt(problem.sourceRange.end), | ||
| ); | ||
| const diagnostic = new Diagnostic(range, messageFor(problem), severityFor(problem)); | ||
| diagnostic.source = InlineScriptStrings.diagnosticSource; | ||
| diagnostic.code = problem.code; | ||
| return diagnostic; | ||
| } | ||
|
|
||
| function messageFor(problem: InlineScriptMetadataProblem): string { | ||
| switch (problem.code) { | ||
| case 'unterminated-block': | ||
| return InlineScriptStrings.unterminatedBlock; | ||
| case 'multiple-blocks': | ||
| return InlineScriptStrings.multipleBlocks; | ||
| case 'invalid-content-line': | ||
| return InlineScriptStrings.invalidContentLine(problem.detail ?? ''); | ||
| case 'invalid-block-marker': | ||
| return InlineScriptStrings.invalidBlockMarker(problem.detail ?? ''); | ||
| case 'invalid-toml': | ||
| return InlineScriptStrings.invalidToml(problem.detail ?? ''); | ||
| case 'invalid-field-type': | ||
| return InlineScriptStrings.invalidFieldType(problem.detail ?? ''); | ||
| default: | ||
| return InlineScriptStrings.unterminatedBlock; | ||
| } | ||
| } | ||
|
|
||
| function severityFor(problem: InlineScriptMetadataProblem): DiagnosticSeverity { | ||
| return problem.severity === 'warning' ? DiagnosticSeverity.Warning : DiagnosticSeverity.Error; | ||
| } | ||
|
|
||
| export function registerInlineScriptDiagnostics(): Disposable { | ||
| const publisher = new InlineScriptDiagnosticsPublisher( | ||
| languages.createDiagnosticCollection(DIAGNOSTIC_COLLECTION_NAME), | ||
| ); | ||
| publisher.activate(); | ||
| return publisher; | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.