Skip to content

Commit ebd90dd

Browse files
committed
perf: parallelized checking of the file existence
1 parent 43c67b8 commit ebd90dd

File tree

16 files changed

+90
-68
lines changed

16 files changed

+90
-68
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
"mri": "^1.2.0",
6666
"msvc-dev-cmd": "github:aminya/msvc-dev-cmd#9f672c1",
6767
"numerous": "1.0.3",
68+
"path-exists": "^5.0.0",
6869
"patha": "^0.4.1",
6970
"quote-unquote": "^1.0.0",
7071
"semver": "7.3.8",

pnpm-lock.yaml

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/chocolatey/chocolatey.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* eslint-disable require-atomic-updates */
22
import execa from "execa"
3-
import { existsSync } from "fs"
3+
4+
import { pathExists } from "path-exists"
45
import { dirname } from "patha"
56
import which from "which"
67
import { addPath } from "../utils/env/addEnv"
@@ -63,7 +64,7 @@ export async function setupChocolatey(
6364
binDir = `${process.env.ChocolateyInstall ?? "C:/ProgramData/chocolatey"}/bin`
6465
}
6566

66-
if (existsSync(binDir)) {
67+
if (await pathExists(binDir)) {
6768
return { binDir }
6869
}
6970
return undefined

src/doxygen/doxygen.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ import { extractTar, extractZip } from "../utils/setup/extract"
99
import { notice } from "ci-log"
1010
import { setupGraphviz } from "../graphviz/graphviz"
1111
import { getVersion } from "../versions/versions"
12-
import { existsSync } from "fs"
12+
1313
import { isArch } from "../utils/env/isArch"
1414
import { hasDnf } from "../utils/env/hasDnf"
1515
import { setupDnfPack } from "../utils/setup/setupDnfPack"
1616
import { isUbuntu } from "../utils/env/isUbuntu"
17+
import { pathExists } from "path-exists"
1718

1819
/** Get the platform data for cmake */
1920
// eslint-disable-next-line @typescript-eslint/no-unused-vars
@@ -99,7 +100,7 @@ async function activateWinDoxygen() {
99100
"C:/Program Files/doxygen/bin",
100101
"C:/Program Files (x86)/doxygen",
101102
]) {
102-
if (existsSync(join(binDir, "doxygen.exe"))) {
103+
if (await pathExists(join(binDir, "doxygen.exe"))) {
103104
// eslint-disable-next-line no-await-in-loop
104105
await addPath(binDir)
105106
return binDir

src/gcc/gcc.ts

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { addPath, addEnv } from "../utils/env/addEnv"
2-
import { existsSync } from "fs"
2+
33
import { setupAptPack, updateAptAlternatives } from "../utils/setup/setupAptPack"
44
import { setupPacmanPack } from "../utils/setup/setupPacmanPack"
55
import { setupBrewPack } from "../utils/setup/setupBrewPack"
@@ -16,6 +16,7 @@ import { isArch } from "../utils/env/isArch"
1616
import { isUbuntu } from "../utils/env/isUbuntu"
1717
import { hasDnf } from "../utils/env/hasDnf"
1818
import { setupDnfPack } from "../utils/setup/setupDnfPack"
19+
import { pathExists } from "path-exists"
1920

2021
interface MingwInfo {
2122
releaseName: string
@@ -133,13 +134,13 @@ export async function setupGcc(version: string, setupDir: string, arch: string)
133134
async function setupChocoMingw(version: string, arch: string): Promise<InstallationInfo | undefined> {
134135
await setupChocoPack("mingw", version)
135136
let binDir: string | undefined
136-
if (arch === "x64" && existsSync("C:/tools/mingw64/bin")) {
137+
if (arch === "x64" && (await pathExists("C:/tools/mingw64/bin"))) {
137138
binDir = "C:/tools/mingw64/bin"
138139
await addPath(binDir)
139-
} else if (arch === "ia32" && existsSync("C:/tools/mingw32/bin")) {
140+
} else if (arch === "ia32" && (await pathExists("C:/tools/mingw32/bin"))) {
140141
binDir = "C:/tools/mingw32/bin"
141142
await addPath(binDir)
142-
} else if (existsSync(`${process.env.ChocolateyInstall ?? "C:/ProgramData/chocolatey"}/bin/g++.exe`)) {
143+
} else if (await pathExists(`${process.env.ChocolateyInstall ?? "C:/ProgramData/chocolatey"}/bin/g++.exe`)) {
143144
binDir = `${process.env.ChocolateyInstall ?? "C:/ProgramData/chocolatey"}/bin`
144145
}
145146
if (binDir !== undefined) {
@@ -171,35 +172,35 @@ async function activateGcc(version: string, binDir: string) {
171172
promises.push(addEnv("CC", `${binDir}/gcc-${majorVersion}`), addEnv("CXX", `${binDir}/g++-${majorVersion}`))
172173

173174
if (isUbuntu()) {
174-
updateAptAlternatives("cc", `${binDir}/gcc-${majorVersion}`)
175-
updateAptAlternatives("cxx", `${binDir}/g++-${majorVersion}`)
176-
updateAptAlternatives("gcc", `${binDir}/gcc-${majorVersion}`)
177-
updateAptAlternatives("g++", `${binDir}/g++-${majorVersion}`)
175+
await updateAptAlternatives("cc", `${binDir}/gcc-${majorVersion}`)
176+
await updateAptAlternatives("cxx", `${binDir}/g++-${majorVersion}`)
177+
await updateAptAlternatives("gcc", `${binDir}/gcc-${majorVersion}`)
178+
await updateAptAlternatives("g++", `${binDir}/g++-${majorVersion}`)
178179
}
179180
} else {
180181
promises.push(addEnv("CC", `${binDir}/gcc-${version}`), addEnv("CXX", `${binDir}/g++-${version}`))
181182

182183
if (isUbuntu()) {
183-
updateAptAlternatives("cc", `${binDir}/gcc-${version}`)
184-
updateAptAlternatives("cxx", `${binDir}/g++-${version}`)
185-
updateAptAlternatives("gcc", `${binDir}/gcc-${version}`)
186-
updateAptAlternatives("g++", `${binDir}/g++-${version}`)
184+
await updateAptAlternatives("cc", `${binDir}/gcc-${version}`)
185+
await updateAptAlternatives("cxx", `${binDir}/g++-${version}`)
186+
await updateAptAlternatives("gcc", `${binDir}/gcc-${version}`)
187+
await updateAptAlternatives("g++", `${binDir}/g++-${version}`)
187188
}
188189
}
189190
}
190191

191192
promises.push(setupMacOSSDK())
192193

193194
if (ciDetect() === "github-actions") {
194-
addGccLoggingMatcher()
195+
await addGccLoggingMatcher()
195196
}
196197

197198
await Promise.all(promises)
198199
}
199200

200-
function addGccLoggingMatcher() {
201+
async function addGccLoggingMatcher() {
201202
const matcherPath = join(__dirname, "gcc_matcher.json")
202-
if (!existsSync(matcherPath)) {
203+
if (!(await pathExists(matcherPath))) {
203204
return warning("the gcc_matcher.json file does not exist in the same folder as setup_cpp.js")
204205
}
205206
info(`::add-matcher::${matcherPath}`)

src/llvm/llvm.ts

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ import { setupMacOSSDK } from "../macos-sdk/macos-sdk"
77
import { addEnv } from "../utils/env/addEnv"
88
import { setupAptPack, updateAptAlternatives } from "../utils/setup/setupAptPack"
99
import { info, warning } from "ci-log"
10-
import { existsSync } from "fs"
10+
1111
import ciDetect from "@npmcli/ci-detect"
1212
import { setupGcc } from "../gcc/gcc"
1313
import { getVersion } from "../versions/versions"
1414
import { isUbuntu } from "../utils/env/isUbuntu"
1515
import { getLLVMPackageInfo } from "./llvm_url"
1616
import { ubuntuVersion } from "../utils/env/ubuntu_version"
17+
import { pathExists } from "path-exists"
1718

1819
export async function setupLLVM(version: string, setupDir: string, arch: string): Promise<InstallationInfo> {
1920
const installationInfo = await setupLLVMWithoutActivation(version, setupDir, arch)
@@ -94,41 +95,41 @@ export async function activateLLVM(directory: string, versionGiven: string) {
9495
// windows builds fail with llvm's CPATH
9596
if (process.platform !== "win32") {
9697
const llvmMajor = semverMajor(version)
97-
if (existsSync(`${directory}/lib/clang/${version}/include`)) {
98+
if (await pathExists(`${directory}/lib/clang/${version}/include`)) {
9899
promises.push(addEnv("CPATH", `${directory}/lib/clang/${version}/include`))
99-
} else if (existsSync(`${directory}/lib/clang/${llvmMajor}/include`)) {
100+
} else if (await pathExists(`${directory}/lib/clang/${llvmMajor}/include`)) {
100101
promises.push(addEnv("CPATH", `${directory}/lib/clang/${llvmMajor}/include`))
101102
}
102103
}
103104

104105
if (isUbuntu()) {
105-
updateAptAlternatives("cc", `${directory}/bin/clang`)
106-
updateAptAlternatives("cxx", `${directory}/bin/clang++`)
107-
updateAptAlternatives("clang", `${directory}/bin/clang`)
108-
updateAptAlternatives("clang++", `${directory}/bin/clang++`)
109-
updateAptAlternatives("lld", `${directory}/bin/lld`)
110-
updateAptAlternatives("ld.lld", `${directory}/bin/ld.lld`)
111-
updateAptAlternatives("llvm-ar", `${directory}/bin/llvm-ar`)
106+
await updateAptAlternatives("cc", `${directory}/bin/clang`)
107+
await updateAptAlternatives("cxx", `${directory}/bin/clang++`)
108+
await updateAptAlternatives("clang", `${directory}/bin/clang`)
109+
await updateAptAlternatives("clang++", `${directory}/bin/clang++`)
110+
await updateAptAlternatives("lld", `${directory}/bin/lld`)
111+
await updateAptAlternatives("ld.lld", `${directory}/bin/ld.lld`)
112+
await updateAptAlternatives("llvm-ar", `${directory}/bin/llvm-ar`)
112113
}
113114

114115
if (ciDetect() === "github-actions") {
115-
addLLVMLoggingMatcher()
116+
await addLLVMLoggingMatcher()
116117
}
117118

118119
await Promise.all(promises)
119120
}
120121

121122
/** Setup llvm tools (clang tidy, clang format, etc) without activating llvm and using it as the compiler */
122-
export function setupClangTools(version: string, setupDir: string, arch: string): Promise<InstallationInfo> {
123+
export async function setupClangTools(version: string, setupDir: string, arch: string): Promise<InstallationInfo> {
123124
if (ciDetect() === "github-actions") {
124-
addLLVMLoggingMatcher()
125+
await addLLVMLoggingMatcher()
125126
}
126127
return setupLLVMWithoutActivation(version, setupDir, arch)
127128
}
128129

129-
function addLLVMLoggingMatcher() {
130+
async function addLLVMLoggingMatcher() {
130131
const matcherPath = join(__dirname, "llvm_matcher.json")
131-
if (!existsSync(matcherPath)) {
132+
if (!(await pathExists(matcherPath))) {
132133
return warning("the llvm_matcher.json file does not exist in the same folder as setup_cpp.js")
133134
}
134135
info(`::add-matcher::${matcherPath}`)

src/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ export async function main(args: string[]): Promise<number> {
270270
info(`took ${timeFormatter.format(time1, time2) || "0 seconds"}`)
271271
}
272272

273-
finalizeCpprc()
273+
await finalizeCpprc()
274274

275275
if (successMessages.length === 0 && errorMessages.length === 0) {
276276
warning("setup_cpp was called without any arguments. Nothing to do.")

src/msvc/msvc.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ import { setupVCVarsall } from "../vcvarsall/vcvarsall"
55
import { vsversion_to_versionnumber, findVcvarsall } from "msvc-dev-cmd/lib.js"
66
import ciDetect from "@npmcli/ci-detect"
77
import { join } from "patha"
8-
import { existsSync } from "fs"
8+
99
import { error, info, warning } from "ci-log"
10+
import { pathExists } from "path-exists"
1011

1112
type MSVCVersion = "2022" | "17.0" | "2019" | "16.0" | "2017" | "15.0" | "2015" | "14.0" | "2013" | "12.0" | string
1213

@@ -66,13 +67,13 @@ export async function setupMSVC(
6667
await setupVCVarsall(version, VCTargetsPath, arch, toolset, sdk, uwp, spectre)
6768

6869
if (ciDetect() === "github-actions") {
69-
addMSVCLoggingMatcher()
70+
await addMSVCLoggingMatcher()
7071
}
7172
}
7273

73-
function addMSVCLoggingMatcher() {
74+
async function addMSVCLoggingMatcher() {
7475
const matcherPath = join(__dirname, "msvc_matcher.json")
75-
if (!existsSync(matcherPath)) {
76+
if (!(await pathExists(matcherPath))) {
7677
return warning("the msvc_matcher.json file does not exist in the same folder as setup_cpp.js")
7778
}
7879
info(`::add-matcher::${matcherPath}`)

src/python/actions_python.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { useCpythonVersion } from "setup-python/src/find-python"
22
import { findPyPyVersion } from "setup-python/src/find-pypy"
3-
import { existsSync } from "fs"
3+
44
import { info, warning } from "ci-log"
55
import { debug } from "@actions/core"
66
import { join } from "patha"
77
import ciDetect from "@npmcli/ci-detect"
88
import { isCacheFeatureAvailable, IS_MAC } from "setup-python/src/utils"
99
import { getCacheDistributor } from "setup-python/src/cache-distributions/cache-factory"
10+
import { pathExists } from "path-exists"
1011

1112
function isPyPyVersion(versionSpec: string) {
1213
return versionSpec.startsWith("pypy")
@@ -51,15 +52,15 @@ export async function setupActionsPython(version: string, _setupDir: string, arc
5152
}
5253

5354
if (ciDetect() === "github-actions") {
54-
addPythonLoggingMatcher()
55+
await addPythonLoggingMatcher()
5556
}
5657

5758
return undefined
5859
}
5960

60-
function addPythonLoggingMatcher() {
61+
async function addPythonLoggingMatcher() {
6162
const matcherPath = join(__dirname, "python_matcher.json")
62-
if (!existsSync(matcherPath)) {
63+
if (!(await pathExists(matcherPath))) {
6364
return warning("the python_matcher.json file does not exist in the same folder as setup_cpp.js")
6465
}
6566
info(`::add-matcher::${matcherPath}`)

src/utils/env/addEnv.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import { exportVariable, addPath as ghAddPath, info, setFailed } from "@actions/core"
22
import ciDetect from "@npmcli/ci-detect"
33
import { untildifyUser } from "untildify-user"
4-
import { appendFileSync, existsSync, readFileSync, writeFileSync } from "fs"
4+
import { appendFileSync, readFileSync, writeFileSync } from "fs"
55
import { error, warning } from "ci-log"
66
import { execPowershell } from "exec-powershell"
77
import { delimiter } from "path"
88
import escapeSpace from "escape-path-with-spaces"
99
import { giveUserAccess } from "user-access"
1010
import escapeQuote from "escape-quotes"
11+
import { pathExists } from "path-exists"
1112

1213
/**
1314
* Add an environment variable.
@@ -75,7 +76,7 @@ async function addEnvSystem(name: string, valGiven: string | undefined) {
7576
}
7677
case "linux":
7778
case "darwin": {
78-
setupCppInProfile()
79+
await setupCppInProfile()
7980
appendFileSync(cpprc_path, `\nexport ${name}="${val}"\n`)
8081
info(`${name}="${val}" was added to "${cpprc_path}`)
8182
return
@@ -99,7 +100,7 @@ async function addPathSystem(path: string) {
99100
}
100101
case "linux":
101102
case "darwin": {
102-
setupCppInProfile()
103+
await setupCppInProfile()
103104
appendFileSync(cpprc_path, `\nexport PATH="${path}:$PATH"\n`)
104105
info(`"${path}" was added to "${cpprc_path}"`)
105106
return
@@ -113,15 +114,15 @@ async function addPathSystem(path: string) {
113114
let setupCppInProfile_called = false
114115

115116
/// handles adding conditions to source .cpprc file from .bashrc and .profile
116-
export function setupCppInProfile() {
117+
export async function setupCppInProfile() {
117118
if (setupCppInProfile_called) {
118119
return
119120
}
120121

121122
// a variable that prevents source_cpprc from being called from .bashrc and .profile
122123
const source_cpprc_str = "# Automatically Generated by setup-cpp\nexport SOURCE_CPPRC=0"
123124

124-
if (existsSync(cpprc_path)) {
125+
if (await pathExists(cpprc_path)) {
125126
const cpprc_content = readFileSync(cpprc_path, "utf8")
126127
if (cpprc_content.includes(source_cpprc_str)) {
127128
// already executed setupCppInProfile
@@ -153,8 +154,8 @@ export function setupCppInProfile() {
153154
setupCppInProfile_called = true
154155
}
155156

156-
export function finalizeCpprc() {
157-
if (existsSync(cpprc_path)) {
157+
export async function finalizeCpprc() {
158+
if (await pathExists(cpprc_path)) {
158159
const entries = readFileSync(cpprc_path, "utf-8").split("\n")
159160

160161
const unique_entries = [...new Set(entries.reverse())].reverse() // remove duplicates, keeping the latest entry

0 commit comments

Comments
 (0)