Skip to content
Open
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions src/common/errors/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import { commands, LogOutputChannel } from 'vscode';
import { Common } from '../localize';
import { showErrorMessage, showWarningMessage } from '../window.apis';

export function getErrorMessage(error: unknown): string {
return error instanceof Error ? error.message : String(error);
}

export function parseStack(ex: Error) {
if (ex.stack && Array.isArray(ex.stack)) {
const concatenated = { ...ex, stack: ex.stack.join('\n') };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// Licensed under the MIT License.

import { createHash } from 'crypto';
import { normalizePackageName } from '../managers/builtin/utils';
import { normalizePath } from './utils/pathUtils';
import { normalizePackageName } from '../../managers/builtin/utils';
import { normalizePath } from '../utils/pathUtils';

/** Length, in hex chars, of the cache key returned by {@link computeCacheKey}. 16 = 64 bits of SHA-256; fixed-length and filesystem-safe. */
export const CACHE_KEY_HEX_LENGTH = 16;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import * as crypto from 'crypto';
import * as fsapi from 'fs-extra';
import * as path from 'path';
import { Uri } from 'vscode';
import type { PythonEnvironment } from '../api';
import { INLINE_SCRIPT_MANAGER_ID } from './constants';
import { traceWarn } from './logging';
import { isFileNotFoundError } from './utils/filesystem';
import { normalizePath } from './utils/pathUtils';
import { isWindows } from './utils/platformUtils';
import { getVenvPythonPath } from './utils/virtualEnvironment';
import type { PythonEnvironment } from '../../api';
import { INLINE_SCRIPT_MANAGER_ID } from '../constants';
import { traceWarn } from '../logging';
import { isFileNotFoundError } from '../utils/filesystem';
import { normalizePath } from '../utils/pathUtils';
import { isWindows } from '../utils/platformUtils';
import { getVenvPythonPath } from '../utils/virtualEnvironment';

/** Bump this and {@link META_SCHEMA_VERSION} together for incompatible cache formats. */
export const INLINE_SCRIPT_CACHE_DIR_NAME = 'script-envs-v1';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

import { PythonEnvironment } from '../api';
import { matchesPythonVersion } from './inlineScriptMetadata';
import { traceWarn } from './logging';
import { compareReleaseSegments, parseReleaseSegments } from './utils/pep440Release';
import { PythonEnvironment } from '../../api';
import { traceWarn } from '../logging';
import { compareReleaseSegments, parseReleaseSegments } from '../utils/pep440Release';
import { matchesPythonVersion } from './metadata';

/**
* Pick the newest installed Python that can serve as a base interpreter for
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import * as tomljs from '@iarna/toml';
import * as fs from 'fs/promises';
import { Uri } from 'vscode';
import { traceVerbose, traceWarn } from './logging';
import { compareReleaseSegments, parseReleaseSegments } from './utils/pep440Release';
import { traceVerbose, traceWarn } from '../logging';
import { compareReleaseSegments, parseReleaseSegments } from '../utils/pep440Release';

/**
* Parsed and validated PEP 723 `script` metadata block.
Expand Down
4 changes: 2 additions & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ import {
} from './features/envCommands';
import { PythonEnvironmentManagers } from './features/envManagers';
import { EnvVarManager, PythonEnvVariableManager } from './features/execution/envVariableManager';
import { InlineScriptLazyDetector } from './features/inlineScriptLazyDetector';
import { InlineScriptLazyDetector } from './features/inlineScript/lazyDetector';
import {
applyInitialEnvironmentSelection,
registerInterpreterSettingsChangeListener,
Expand Down Expand Up @@ -98,7 +98,7 @@ import { ProjectItem, PythonEnvTreeItem } from './features/views/treeViewItems';
import { collectEnvironmentInfo, getEnvManagerAndPackageManagerConfigLevels, runPetInTerminalImpl } from './helpers';
import { EnvironmentManagers, ProjectCreators, PythonProjectManager } from './internal.api';
import { registerSystemPythonFeatures } from './managers/builtin/main';
import { registerInlineScriptFeatures } from './managers/builtin/inlineScriptMain';
import { registerInlineScriptFeatures } from './managers/builtin/inlineScript/main';
import { SysPythonManager } from './managers/builtin/sysPythonManager';
import {
createNativePythonFinder,
Expand Down
Comment thread
StellaHuang95 marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@

import * as path from 'path';
import { Disposable, TextDocument, TextDocumentChangeEvent, Uri } from 'vscode';
import { readInlineScriptMetadataFromFile } from '../common/inlineScriptMetadata';
import { traceVerbose, traceWarn } from '../common/logging';
import { EventNames } from '../common/telemetry/constants';
import { sendTelemetryEvent } from '../common/telemetry/sender';
import { readInlineScriptMetadataFromFile } from '../../common/inlineScript/metadata';
import { traceVerbose, traceWarn } from '../../common/logging';
import { EventNames } from '../../common/telemetry/constants';
import { sendTelemetryEvent } from '../../common/telemetry/sender';
import {
getOpenTextDocuments,
getWorkspaceFolder,
onDidChangeTextDocument,
onDidOpenTextDocument,
onDidSaveTextDocument,
} from '../common/workspace.apis';
} from '../../common/workspace.apis';

/**
* Silent on-open / on-save detector for `.py` files that declare
Expand Down
Comment thread
StellaHuang95 marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ import {
RefreshEnvironmentsScope,
ResolveEnvironmentContext,
SetEnvironmentScope,
} from '../../api';
import { computeCacheKey } from '../../common/inlineScriptCacheKey';
} from '../../../api';
import { getErrorMessage } from '../../../common/errors/utils';
import { computeCacheKey } from '../../../common/inlineScript/cacheKey';
import {
META_SCHEMA_VERSION,
getBaseInterpreterStatus,
Expand All @@ -29,21 +30,21 @@ import {
inspectMetaJson,
resolveCacheEntryPath,
writeMetaJson,
} from '../../common/inlineScriptCacheLayout';
import { pickCompatibleInterpreter } from '../../common/inlineScriptInterpreter';
} from '../../../common/inlineScript/cacheLayout';
import { pickCompatibleInterpreter } from '../../../common/inlineScript/interpreter';
import {
InlineScriptMetadata,
matchesPythonVersion,
readInlineScriptMetadataFromFile,
} from '../../common/inlineScriptMetadata';
import { CONDA_MANAGER_ID, PYENV_MANAGER_ID, SYSTEM_MANAGER_ID } from '../../common/constants';
import { acquireFileLock, AcquiredFileLock } from '../../common/lockfile.apis';
import { isFileNotFoundError } from '../../common/utils/filesystem';
import { normalizePath } from '../../common/utils/pathUtils';
import { compareReleaseSegments, parseReleaseSegments } from '../../common/utils/pep440Release';
import { getVenvPythonPath } from '../../common/utils/virtualEnvironment';
import { NativePythonFinder } from '../common/nativePythonFinder';
import { createWithProgress, resolveVenvPythonEnvironmentPath } from './venvUtils';
} from '../../../common/inlineScript/metadata';
import { CONDA_MANAGER_ID, PYENV_MANAGER_ID, SYSTEM_MANAGER_ID } from '../../../common/constants';
import { acquireFileLock, AcquiredFileLock } from '../../../common/lockfile.apis';
import { isFileNotFoundError } from '../../../common/utils/filesystem';
import { normalizePath } from '../../../common/utils/pathUtils';
import { compareReleaseSegments, parseReleaseSegments } from '../../../common/utils/pep440Release';
import { getVenvPythonPath } from '../../../common/utils/virtualEnvironment';
import { NativePythonFinder } from '../../common/nativePythonFinder';
import { createWithProgress, resolveVenvPythonEnvironmentPath } from '../venvUtils';

const BASE_INTERPRETER_MANAGER_IDS = new Set([
SYSTEM_MANAGER_ID,
Expand Down Expand Up @@ -160,7 +161,7 @@ export class InlineScriptEnvManager implements EnvironmentManager, Disposable {
}
}
} catch (error) {
this.log.error(`Failed to set up inline-script environment: ${this.errorMessage(error)}`);
this.log.error(`Failed to set up inline-script environment: ${getErrorMessage(error)}`);
return undefined;
}
}
Expand Down Expand Up @@ -230,7 +231,7 @@ export class InlineScriptEnvManager implements EnvironmentManager, Disposable {
return { environment, canonicalPath: await fs.realpath(executable) };
} catch (error) {
this.log.warn(
`Skipping base interpreter that cannot be resolved at ${executable}: ${this.errorMessage(error)}`,
`Skipping base interpreter that cannot be resolved at ${executable}: ${getErrorMessage(error)}`,
);
}
}
Expand Down Expand Up @@ -277,20 +278,20 @@ export class InlineScriptEnvManager implements EnvironmentManager, Disposable {
await lock.retain();
} catch (error) {
this.log.error(
`Failed to mark the inline-script cache lock as retained: ${this.errorMessage(error)}`,
`Failed to mark the inline-script cache lock as retained: ${getErrorMessage(error)}`,
);
}
}
return build.environment;
} catch (error) {
this.log.error(`Failed to create or reuse inline-script cache entry: ${this.errorMessage(error)}`);
this.log.error(`Failed to create or reuse inline-script cache entry: ${getErrorMessage(error)}`);
return undefined;
} finally {
if (lock) {
try {
await lock.release();
} catch (error) {
this.log.warn(`Failed to release inline-script cache lock: ${this.errorMessage(error)}`);
this.log.warn(`Failed to release inline-script cache lock: ${getErrorMessage(error)}`);
}
}
}
Expand All @@ -315,7 +316,7 @@ export class InlineScriptEnvManager implements EnvironmentManager, Disposable {
try {
resolvedEntry = await resolveCacheEntryPath(cacheRoot, envDir);
} catch (error) {
this.log.warn(`Failed to resolve inline-script cache entry: ${this.errorMessage(error)}`);
this.log.warn(`Failed to resolve inline-script cache entry: ${getErrorMessage(error)}`);
return { kind: 'uncertain' };
}
if (!resolvedEntry) {
Expand Down Expand Up @@ -369,7 +370,7 @@ export class InlineScriptEnvManager implements EnvironmentManager, Disposable {
try {
await writeMetaJson(envDir, { ...sidecar, lastUsedAt: new Date().toISOString() });
} catch (error) {
this.log.warn(`Failed to update inline-script cache metadata: ${this.errorMessage(error)}`);
this.log.warn(`Failed to update inline-script cache metadata: ${getErrorMessage(error)}`);
}
return { kind: 'reusable', environment };
}
Expand All @@ -394,7 +395,7 @@ export class InlineScriptEnvManager implements EnvironmentManager, Disposable {
false, // trackUvEnvironment
);
} catch (error) {
this.log.error(`Failed to build inline-script environment: ${this.errorMessage(error)}`);
this.log.error(`Failed to build inline-script environment: ${getErrorMessage(error)}`);
await this.removeCacheEntry(envDir);
return {};
}
Expand Down Expand Up @@ -429,7 +430,7 @@ export class InlineScriptEnvManager implements EnvironmentManager, Disposable {
lastUsedAt: new Date().toISOString(),
});
} catch (error) {
this.log.error(`Failed to record inline-script cache metadata: ${this.errorMessage(error)}`);
this.log.error(`Failed to record inline-script cache metadata: ${getErrorMessage(error)}`);
await this.removeCacheEntry(envDir);
return {};
}
Expand All @@ -442,7 +443,7 @@ export class InlineScriptEnvManager implements EnvironmentManager, Disposable {
await fs.remove(envDir.fsPath);
return true;
} catch (error) {
this.log.error(`Failed to remove incomplete inline-script environment: ${this.errorMessage(error)}`);
this.log.error(`Failed to remove incomplete inline-script environment: ${getErrorMessage(error)}`);
return false;
}
}
Expand All @@ -456,10 +457,6 @@ export class InlineScriptEnvManager implements EnvironmentManager, Disposable {
return compareReleaseSegments(actualRelease, expectedRelease) === 0;
}

private errorMessage(error: unknown): string {
return error instanceof Error ? error.message : String(error);
}

dispose(): void {
this._onDidChangeEnvironments.dispose();
this._onDidChangeEnvironment.dispose();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
// Licensed under the MIT License.

import { Disposable, LogOutputChannel, Uri } from 'vscode';
import { EnvironmentManager, PythonEnvironmentApi } from '../../api';
import { traceInfo, traceVerbose } from '../../common/logging';
import { getPythonApi } from '../../features/pythonApi';
import { isInlineScriptsFeatureEnabled } from '../../helpers';
import { NativePythonFinder } from '../common/nativePythonFinder';
import { InlineScriptEnvManager } from './inlineScriptEnvManager';
import { EnvironmentManager, PythonEnvironmentApi } from '../../../api';
import { traceInfo, traceVerbose } from '../../../common/logging';
import { getPythonApi } from '../../../features/pythonApi';
import { isInlineScriptsFeatureEnabled } from '../../../helpers';
import { NativePythonFinder } from '../../common/nativePythonFinder';
import { InlineScriptEnvManager } from './envManager';

/**
* Register the inline-script env manager when the internal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import {
computeCacheKey,
normalizeDependency,
normalizeInterpreterPath,
} from '../../common/inlineScriptCacheKey';
import * as platformUtils from '../../common/utils/platformUtils';
} from '../../../common/inlineScript/cacheKey';
import * as platformUtils from '../../../common/utils/platformUtils';

suite('inlineScriptCacheKey', () => {
let isWindowsStub: sinon.SinonStub;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import * as os from 'os';
import * as path from 'path';
import * as sinon from 'sinon';
import { Uri } from 'vscode';
import { PythonEnvironment } from '../../api';
import { PythonEnvironment } from '../../../api';
import {
CacheEntrySummary,
INLINE_SCRIPT_CACHE_DIR_NAME,
Expand All @@ -26,10 +26,10 @@ import {
selectStaleEntries,
verifyBaseInterpreterExists,
writeMetaJson,
} from '../../common/inlineScriptCacheLayout';
import * as logging from '../../common/logging';
import * as platformUtils from '../../common/utils/platformUtils';
import { getVenvPythonPath } from '../../common/utils/virtualEnvironment';
} from '../../../common/inlineScript/cacheLayout';
import * as logging from '../../../common/logging';
import * as platformUtils from '../../../common/utils/platformUtils';
import { getVenvPythonPath } from '../../../common/utils/virtualEnvironment';

function makeMeta(overrides: Partial<InlineScriptEnvMeta> = {}): InlineScriptEnvMeta {
return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import assert from 'assert';
import * as sinon from 'sinon';
import { Uri } from 'vscode';
import { PythonEnvironment } from '../../api';
import { extractLowerBoundVersion, pickCompatibleInterpreter } from '../../common/inlineScriptInterpreter';
import * as logging from '../../common/logging';
import { PythonEnvironment } from '../../../api';
import { extractLowerBoundVersion, pickCompatibleInterpreter } from '../../../common/inlineScript/interpreter';
import * as logging from '../../../common/logging';

function makeEnv(version: string, name = `Python ${version}`, error?: string): PythonEnvironment {
return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import {
matchesPythonVersion,
readInlineScriptMetadata,
readInlineScriptMetadataFromFile,
} from '../../common/inlineScriptMetadata';
import * as logging from '../../common/logging';
} from '../../../common/inlineScript/metadata';
import * as logging from '../../../common/logging';

// Helper to assemble a script body. Lines are joined with '\n' so each
// test controls line endings explicitly via `joiner` when needed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import assert from 'assert';
import * as path from 'path';
import * as sinon from 'sinon';
import { Disposable, TextDocument, TextDocumentChangeEvent, TextDocumentContentChangeEvent, Uri } from 'vscode';
import * as ism from '../../common/inlineScriptMetadata';
import { EventNames } from '../../common/telemetry/constants';
import * as telemetrySender from '../../common/telemetry/sender';
import * as wapi from '../../common/workspace.apis';
import { InlineScriptLazyDetector, shouldHandleUri } from '../../features/inlineScriptLazyDetector';
import * as ism from '../../../common/inlineScript/metadata';
import { EventNames } from '../../../common/telemetry/constants';
import * as telemetrySender from '../../../common/telemetry/sender';
import * as wapi from '../../../common/workspace.apis';
import { InlineScriptLazyDetector, shouldHandleUri } from '../../../features/inlineScript/lazyDetector';

// Build a minimal TextDocument stub. Only the `uri` field is read by
// the detector; the rest exists to satisfy the type.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ import * as os from 'os';
import * as path from 'path';
import * as sinon from 'sinon';
import { LogOutputChannel, Uri } from 'vscode';
import { EnvironmentManager, PythonEnvironment, PythonEnvironmentApi } from '../../../api';
import * as cacheKey from '../../../common/inlineScriptCacheKey';
import * as cacheLayout from '../../../common/inlineScriptCacheLayout';
import * as lockfileApis from '../../../common/lockfile.apis';
import * as metadataReader from '../../../common/inlineScriptMetadata';
import { isWindows } from '../../../common/utils/platformUtils';
import { getVenvPythonPath } from '../../../common/utils/virtualEnvironment';
import { InlineScriptEnvManager } from '../../../managers/builtin/inlineScriptEnvManager';
import * as venvUtils from '../../../managers/builtin/venvUtils';
import { NativePythonFinder } from '../../../managers/common/nativePythonFinder';
import { EnvironmentManager, PythonEnvironment, PythonEnvironmentApi } from '../../../../api';
import * as cacheKey from '../../../../common/inlineScript/cacheKey';
import * as cacheLayout from '../../../../common/inlineScript/cacheLayout';
import * as metadataReader from '../../../../common/inlineScript/metadata';
import * as lockfileApis from '../../../../common/lockfile.apis';
import { isWindows } from '../../../../common/utils/platformUtils';
import { getVenvPythonPath } from '../../../../common/utils/virtualEnvironment';
import { InlineScriptEnvManager } from '../../../../managers/builtin/inlineScript/envManager';
import * as venvUtils from '../../../../managers/builtin/venvUtils';
import { NativePythonFinder } from '../../../../managers/common/nativePythonFinder';

const CACHE_KEY = '0123456789abcdef';
const NOW = new Date('2026-07-21T12:00:00.000Z');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
import assert from 'assert';
import * as sinon from 'sinon';
import { Disposable, LogOutputChannel, Uri } from 'vscode';
import { EnvironmentManager, PythonEnvironmentApi } from '../../../api';
import * as pythonApi from '../../../features/pythonApi';
import * as helpers from '../../../helpers';
import { registerInlineScriptFeatures } from '../../../managers/builtin/inlineScriptMain';
import { NativePythonFinder } from '../../../managers/common/nativePythonFinder';
import { EnvironmentManager, PythonEnvironmentApi } from '../../../../api';
import * as pythonApi from '../../../../features/pythonApi';
import * as helpers from '../../../../helpers';
import { registerInlineScriptFeatures } from '../../../../managers/builtin/inlineScript/main';
import { NativePythonFinder } from '../../../../managers/common/nativePythonFinder';

function makeFakeLog(): LogOutputChannel {
return {
Expand Down
Loading