Skip to content

Commit 0d9af97

Browse files
NGPetrovalehander92
authored andcommitted
fix: fix a lang detection bug and improve lang detection logic
(alexander): the bug was that when we had a `.` in a parent-parent folder of `program` on Nikola's machine, we were detecting the next part of the path as an extension, and also returning a false LangC default overally the logic was messy, refactored a bit according to Nikola's suggestions
1 parent 23c82f4 commit 0d9af97

File tree

1 file changed

+33
-23
lines changed

1 file changed

+33
-23
lines changed

src/ct/utilities/language_detection.nim

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -43,33 +43,43 @@ const WASM_LANGS = {
4343
proc detectLang*(program: string, lang: Lang, isWasm: bool = false): Lang =
4444
# TODO: under a debug print flag?
4545
# echo "detectLang ", program, " ", lang, " isWasm: ", isWasm
46+
if lang != LangUnknown:
47+
return lang
48+
49+
result = LangUnknown # by default
50+
4651
var possiblyExpandedPath = ""
4752
try:
4853
possiblyExpandedPath = expandFileName(program)
4954
except CatchableError:
5055
possiblyExpandedPath = program
5156

52-
if lang == LangUnknown:
53-
if "." in possiblyExpandedPath:
54-
let extension = rsplit(possiblyExpandedPath[1..^1], ".", 1)[1].toLowerAscii()
55-
if not isWasm:
56-
if LANGS.hasKey(extension):
57-
result = LANGS[extension] # TODO detectLangFromTrace(traceId)
58-
else:
59-
if WASM_LANGS.hasKey(extension):
60-
result = WASM_LANGS[extension]
61-
elif dirExists(program):
62-
result = detectFolderLang(program)
63-
else:
64-
let ctConfig = loadConfig(folder=getCurrentDir(), inTest=false)
65-
if ctConfig.rrBackend.enabled:
66-
let rawLang = execProcess(
67-
ctConfig.rrBackend.debugInfoToolPath,
68-
args = @["lang", program],
69-
options={}).strip
70-
result = toLang(rawLang)
71-
else:
72-
result = LangUnknown
73-
else:
74-
result = lang
57+
let filename = possiblyExpandedPath.extractFilename
58+
let isFolder = dirExists(program)
59+
60+
if isFolder:
61+
result = detectFolderLang(program)
62+
if result != LangUnknown:
63+
return result
64+
65+
if not isFolder and "." in filename:
66+
let extension = rsplit(filename[1..^1], ".", 1)[1].toLowerAscii()
67+
68+
if isWasm and WASM_LANGS.hasKey(extension):
69+
return WASM_LANGS[extension]
70+
71+
if LANGS.hasKey(extension):
72+
result = LANGS[extension] # TODO detectLangFromTrace(traceId)
73+
if result != LangUnknown:
74+
return result
75+
76+
# try with the rr-backend
77+
let ctConfig = loadConfig(folder=getCurrentDir(), inTest=false)
78+
if ctConfig.rrBackend.enabled:
79+
let rawLang = execProcess(
80+
ctConfig.rrBackend.debugInfoToolPath,
81+
args = @["lang", program],
82+
options={}).strip
83+
result = toLang(rawLang)
84+
7585
# echo "detectLang result ", result

0 commit comments

Comments
 (0)