@@ -5,6 +5,22 @@ import * as fs from "fs";
55import * as child_process from "child_process" ;
66import * as settings from "./configSettings" ;
77
8+ const PathValidity : Map < string , boolean > = new Map < string , boolean > ( ) ;
9+ export function validatePath ( filePath : string ) : Promise < string > {
10+ if ( filePath . length === 0 ) {
11+ return Promise . resolve ( "" ) ;
12+ }
13+ if ( PathValidity . has ( filePath ) ) {
14+ return Promise . resolve ( PathValidity . get ( filePath ) ? filePath : "" ) ;
15+ }
16+ return new Promise < string > ( resolve => {
17+ fs . exists ( filePath , exists => {
18+ PathValidity . set ( filePath , exists ) ;
19+ return resolve ( exists ? filePath : "" ) ;
20+ } ) ;
21+ } ) ;
22+ }
23+
824let pythonInterpretterDirectory : string = null ;
925let previouslyIdentifiedPythonPath : string = null ;
1026
@@ -42,24 +58,77 @@ export function getPythonInterpreterDirectory(): Promise<string> {
4258 } ) ;
4359}
4460
61+ const IN_VALID_FILE_PATHS : Map < string , boolean > = new Map < string , boolean > ( ) ;
4562export function execPythonFile ( file : string , args : string [ ] , cwd : string , includeErrorAsResponse : boolean = false ) : Promise < string > {
46- return getPythonInterpreterDirectory ( ) . then ( pyPath => {
47- let options : any = { cwd : cwd } ;
48-
49- // If we have a fully qualitified path to the python executable directory, then we can use that
50- // as the base path for all other executables
51- if ( pyPath . length > 0 ) {
52- const EnvVariableName = process . platform === "win32" ? "Path" : "PATH" ;
53- options . env = mergeEnvVariables ( { EnvVariableName : pyPath + path . delimiter + process . env . PATH } ) ;
63+ // Whether to try executing the command without prefixing it with the python path
64+ let tryUsingCommandArg = false ;
65+ if ( file === settings . PythonSettings . getInstance ( ) . pythonPath ) {
66+ return execFileInternal ( file , args , cwd , includeErrorAsResponse ) ;
67+ }
68+
69+ let fullyQualifiedFilePromise = getPythonInterpreterDirectory ( ) . then ( pyPath => {
70+ let pythonIntepreterPath = pyPath ;
71+ let fullyQualifiedFile = file ;
72+ if ( pythonIntepreterPath . length === 0 || file . startsWith ( pyPath ) ) {
73+ return execFileInternal ( fullyQualifiedFile , args , cwd , includeErrorAsResponse , true ) ;
74+ }
75+
76+ // Qualify the command with the python path
77+ fullyQualifiedFile = path . join ( pythonIntepreterPath , file ) ;
78+
79+ // Check if we know whether this trow ENONE errors
80+ if ( IN_VALID_FILE_PATHS . has ( fullyQualifiedFile ) ) {
81+ return execFileInternal ( file , args , cwd , includeErrorAsResponse , true ) ;
5482 }
5583
56- return execFileInternal ( file , args , options , includeErrorAsResponse ) ;
84+ // It is possible this file doesn't exist, hence we initialize tryUsingCommandArg = true
85+ tryUsingCommandArg = true ;
86+
87+ if ( PathValidity . has ( fullyQualifiedFile ) ) {
88+ // If the file exists, then don't try again
89+ // If PathValidity value = false, that means we don't really know
90+ // Cuz we could have some command args suffixed in the file path (hopefully this will be fixed in a later build)
91+ if ( PathValidity . get ( fullyQualifiedFile ) ) {
92+ tryUsingCommandArg = false ;
93+ }
94+ return execFileInternal ( fullyQualifiedFile , args , cwd , includeErrorAsResponse , true ) ;
95+ }
96+
97+ return validatePath ( fullyQualifiedFile ) . then ( f => {
98+ // If the file exists, then don't bother trying again
99+ if ( f . length > 0 ) {
100+ tryUsingCommandArg = false ;
101+ }
102+ return execFileInternal ( fullyQualifiedFile , args , cwd , includeErrorAsResponse , true ) ;
103+ } ) ;
57104 } ) ;
105+
106+ if ( tryUsingCommandArg ) {
107+ return fullyQualifiedFilePromise . catch ( error => {
108+ if ( error && ( < any > error ) . code === "ENOENT" ) {
109+ // Re-execute the file, without the python path prefix
110+ // Only if we know that the previous one failed with ENOENT
111+ return execFileInternal ( file , args , cwd , includeErrorAsResponse , true ) ;
112+ }
113+ // return what ever error we got from the previous process
114+ Promise . reject ( error ) ;
115+ } ) ;
116+ }
117+ else {
118+ return fullyQualifiedFilePromise ;
119+ }
58120}
59121
60- function execFileInternal ( file : string , args : string [ ] , options : child_process . ExecFileOptions , includeErrorAsResponse : boolean ) : Promise < string > {
122+ function execFileInternal ( file : string , args : string [ ] , cwd : string , includeErrorAsResponse : boolean , rejectIfENOENTErrors : boolean = false ) : Promise < string > {
61123 return new Promise < string > ( ( resolve , reject ) => {
62- child_process . execFile ( file , args , options , ( error , stdout , stderr ) => {
124+ child_process . execFile ( file , args , { cwd : cwd } , ( error , stdout , stderr ) => {
125+ if ( error && ( < any > error ) . code === "ENOENT" && rejectIfENOENTErrors ) {
126+ if ( ! IN_VALID_FILE_PATHS . has ( file ) ) {
127+ IN_VALID_FILE_PATHS . set ( file , true ) ;
128+ }
129+ return reject ( error ) ;
130+ }
131+
63132 // pylint:
64133 // In the case of pylint we have some messages (such as config file not found and using default etc...) being returned in stderr
65134 // These error messages are useless when using pylint
0 commit comments