44import * as fs from 'fs' ;
55import * as path from 'path' ;
66import * as crypto from 'crypto' ;
7- import * as os from 'os' ;
8- import { DebugSessionOptions , Disposable , GlobalEnvironmentVariableCollection , l10n , RelativePattern } from 'vscode' ;
7+ import {
8+ DebugSessionOptions ,
9+ Disposable ,
10+ GlobalEnvironmentVariableCollection ,
11+ l10n ,
12+ RelativePattern ,
13+ workspace ,
14+ } from 'vscode' ;
915import { createFileSystemWatcher , debugStartDebugging } from './utils' ;
10- import { traceError , traceLog , traceVerbose } from './common/log/logging' ;
16+ import { traceError , traceVerbose } from './common/log/logging' ;
1117
1218/**
1319 * Registers the configuration-less debugging setup for the extension.
@@ -30,21 +36,33 @@ export async function registerNoConfigDebug(
3036 const collection = envVarCollection ;
3137
3238 // create a temp directory for the noConfigDebugAdapterEndpoints
33- // file path format: tempDir/noConfigDebugAdapterEndpoints-<randomString>/debuggerAdapterEndpoint.txt
34- const randomSuffix = crypto . randomBytes ( 10 ) . toString ( 'hex' ) ;
35- const tempDirName = `noConfigDebugAdapterEndpoints-${ randomSuffix } ` ;
36- let tempDirPath = path . join ( os . tmpdir ( ) , tempDirName ) ;
37- try {
38- traceLog ( 'Attempting to use temp directory for noConfigDebugAdapterEndpoints, dir name:' , tempDirName ) ;
39- await fs . promises . mkdir ( tempDirPath , { recursive : true } ) ;
40- } catch ( error ) {
41- // Handle the error when accessing the temp directory
42- traceError ( 'Error accessing temp directory:' , error , ' Attempt to use extension root dir instead' ) ;
43- // Make new temp directory in extension root dird
44- tempDirPath = path . join ( extPath , '.temp' ) ;
45- await fs . promises . mkdir ( tempDirPath , { recursive : true } ) ;
39+ // file path format: extPath/.noConfigDebugAdapterEndpoints/endpoint-stableWorkspaceHash.txt
40+ let workspaceString = workspace . workspaceFile ?. fsPath ;
41+ if ( ! workspaceString ) {
42+ workspaceString = workspace . workspaceFolders ?. map ( ( e ) => e . uri . fsPath ) . join ( ';' ) ;
43+ }
44+ if ( ! workspaceString ) {
45+ traceError ( 'No workspace folder found' ) ;
46+ return Promise . resolve ( new Disposable ( ( ) => { } ) ) ;
47+ }
48+
49+ // create a stable hash for the workspace folder, reduce terminal variable churn
50+ const hash = crypto . createHash ( 'sha256' ) ;
51+ hash . update ( workspaceString . toString ( ) ) ;
52+ const stableWorkspaceHash = hash . digest ( 'hex' ) . slice ( 0 , 16 ) ;
53+
54+ const tempDirPath = path . join ( extPath , '.noConfigDebugAdapterEndpoints' ) ;
55+ const tempFilePath = path . join ( tempDirPath , `endpoint-${ stableWorkspaceHash } .txt` ) ;
56+
57+ // create the temp directory if it doesn't exist
58+ if ( ! fs . existsSync ( tempDirPath ) ) {
59+ fs . mkdirSync ( tempDirPath , { recursive : true } ) ;
60+ } else {
61+ // remove endpoint file in the temp directory if it exists
62+ if ( fs . existsSync ( tempFilePath ) ) {
63+ fs . unlinkSync ( tempFilePath ) ;
64+ }
4665 }
47- const tempFilePath = path . join ( tempDirPath , 'debuggerAdapterEndpoint.txt' ) ;
4866
4967 // Add env var for PYDEVD_DISABLE_FILE_VALIDATION to disable extra output in terminal when starting the debug session.
5068 collection . replace ( 'PYDEVD_DISABLE_FILE_VALIDATION' , '1' ) ;
0 commit comments