11import assert from "assert"
2- /* eslint-disable require-atomic-updates */
2+ import { homedir } from "os"
3+ import { parse as pathParse } from "path"
34import { getExecOutput } from "@actions/exec"
45import { GITHUB_ACTIONS } from "ci-info"
56import { info , warning } from "ci-log"
67import { execa } from "execa"
8+ import { readdir } from "fs/promises"
79import memoize from "micro-memoize"
810import { pathExists } from "path-exists"
911import { addExeExt , dirname , join } from "patha"
@@ -89,7 +91,10 @@ async function findOrSetupPython(version: string, setupDir: string, arch: string
8991 const { setupActionsPython } = await import ( "./actions_python" )
9092 await setupActionsPython ( version , setupDir , arch )
9193
92- foundPython = ( await findPython ( setupDir ) ) !
94+ foundPython = await findPython ( setupDir )
95+ if ( foundPython === undefined ) {
96+ throw new Error ( "Python binary could not be found" )
97+ }
9398 const binDir = dirname ( foundPython )
9499 installInfo = { bin : foundPython , installDir : binDir , binDir }
95100 } catch ( err ) {
@@ -103,7 +108,10 @@ async function findOrSetupPython(version: string, setupDir: string, arch: string
103108 }
104109
105110 if ( foundPython === undefined || installInfo . bin === undefined ) {
106- foundPython = ( await findPython ( setupDir ) ) !
111+ foundPython = await findPython ( setupDir )
112+ if ( foundPython === undefined ) {
113+ throw new Error ( "Python binary could not be found" )
114+ }
107115 installInfo . bin = foundPython
108116 }
109117
@@ -120,7 +128,10 @@ async function setupPythonSystem(setupDir: string, version: string) {
120128 await setupChocoPack ( "python3" , version )
121129 }
122130 // Adding the bin dir to the path
123- const bin = ( await findPython ( setupDir ) ) !
131+ const bin = await findPython ( setupDir )
132+ if ( bin === undefined ) {
133+ throw new Error ( "Python binary could not be found" )
134+ }
124135 const binDir = dirname ( bin )
125136 /** The directory which the tool is installed to */
126137 await addPath ( binDir )
@@ -166,6 +177,24 @@ async function findPython(binDir?: string) {
166177 return foundPython
167178 }
168179 }
180+
181+ // On Windows, search in C:\PythonXX
182+ if ( process . platform === "win32" ) {
183+ const rootDir = pathParse ( homedir ( ) ) . root
184+ // find all directories in rootDir using readdir
185+ const pythonDirs = ( await readdir ( rootDir ) ) . filter ( ( dir ) => dir . startsWith ( "Python" ) )
186+
187+ for ( const pythonDir of pythonDirs ) {
188+ for ( const pythonBin of [ "python3" , "python" ] ) {
189+ // eslint-disable-next-line no-await-in-loop
190+ const foundPython = await isPythonUpToDate ( pythonBin , join ( rootDir , pythonDir ) )
191+ if ( foundPython !== undefined ) {
192+ return foundPython
193+ }
194+ }
195+ }
196+ }
197+
169198 return undefined
170199}
171200
0 commit comments