Skip to content

Commit e550b92

Browse files
Make syncVersions only sync versions of used tools
When syncVersions sync the versions of the provided tools, it should only change the version of a tool that has a defined version. Otherwise tools may get installed that were not meant to be installed. In particular this solves an issue where, when an explicit version for clangtidy is specified and the compiler is gcc, we would install llvm on top of gcc.
1 parent 77554aa commit e550b92

File tree

2 files changed

+24
-20
lines changed

2 files changed

+24
-20
lines changed

src/__tests__/main.test.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,22 @@ describe("syncVersion", () => {
2929
expect(syncVersions(parseArgs(["--llvm", "13.0.0", "--clangtidy", "true"]), llvmTools)).toBe(true)
3030
expect(syncVersions(parseArgs(["--llvm", "13.0.0", "--clangtidy", "12.0.0"]), llvmTools)).toBe(false)
3131

32-
const opts = parseArgs(["--llvm", "14.0.0", "--clangtidy", "true"])
33-
expect(syncVersions(opts, llvmTools)).toBe(true)
34-
expect(opts.llvm).toBe(opts.clangtidy)
32+
const opts1 = parseArgs(["--llvm", "14.0.0", "--clangtidy", "true"])
33+
expect(syncVersions(opts1, llvmTools)).toBe(true)
34+
expect(opts1.llvm).toBe(opts1.clangtidy)
35+
expect(opts1.clangformat).toBe(undefined)
36+
37+
const opts2 = parseArgs(["--clangtidy", "15.0.0", "--clangformat", "true"])
38+
expect(syncVersions(opts2, llvmTools)).toBe(true)
39+
expect(opts2.llvm).toBe(undefined)
40+
expect(opts2.clangtidy).toBe("15.0.0")
41+
expect(opts2.clangformat).toBe("15.0.0")
42+
43+
const opts3 = parseArgs(["--llvm", "true", "--clangformat", "true"])
44+
expect(syncVersions(opts3, llvmTools)).toBe(true)
45+
expect(opts3.llvm).toBe("true")
46+
expect(opts3.clangtidy).toBe(undefined)
47+
expect(opts3.clangformat).toBe("true")
3548
})
3649
})
3750

src/versions/versions.ts

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,25 +30,16 @@ export function isDefault(version: string | undefined, name: string) {
3030
}
3131

3232
export function syncVersions(opts: Opts, tools: Inputs[]): boolean {
33-
for (let i = 0; i < tools.length; i++) {
34-
// tools excluding i_tool
35-
const otherTools = tools.slice(0, i).concat(tools.slice(i + 1))
33+
const toolsInUse = tools.filter((tool) => opts[tool] !== undefined)
34+
const toolsNonDefaultVersion = toolsInUse.filter((tool) => !isDefault(opts[tool], tool))
3635

37-
const tool = tools[i]
36+
const targetVersion = toolsNonDefaultVersion.length ? opts[toolsNonDefaultVersion[0]] : "true"
3837

39-
if (!isDefault(opts[tool], tool)) {
40-
for (let i_other = 0; i_other < otherTools.length; i_other++) {
41-
const otherTool = otherTools[i_other]
42-
const useDefaultOtherTool = isDefault(opts[otherTool], otherTools[i_other])
43-
if (useDefaultOtherTool) {
44-
// use the same version if the other tool was requested with the default
45-
opts[otherTool] = opts[tool]
46-
} else if (opts[tool] !== opts[otherTools[i_other]]) {
47-
// error if different from the other given versions
48-
return false
49-
}
50-
}
51-
}
38+
// return false if any explicit versions don't match the target version
39+
if (toolsNonDefaultVersion.findIndex((tool) => opts[tool] !== targetVersion) !== -1) {
40+
return false
5241
}
42+
43+
toolsInUse.forEach((tool) => (opts[tool] = targetVersion))
5344
return true
5445
}

0 commit comments

Comments
 (0)