Skip to content

Commit 5b2a194

Browse files
committed
Add when isMainModule and better error handling to gen_import_suggestions
- Added back when isMainModule block to actually call generateSuggestions() - Improved error handling with better diagnostics - Added project setup to avoid package errors (still WIP) - Tool framework is ready but compiler API initialization needs more work
1 parent 7e40223 commit 5b2a194

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

tools/gen_import_suggestions.nim

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ proc setupCompilerForScanning(): ModuleGraph =
1616
conf.libpath = AbsoluteDir(nimRoot / "lib")
1717
conf.prefixDir = AbsoluteDir(nimRoot)
1818

19+
# Set a dummy project file to avoid package errors
20+
conf.projectFull = AbsoluteFile(nimRoot / "dummy.nim")
21+
conf.projectPath = AbsoluteDir(nimRoot)
22+
conf.projectName = "dummy"
23+
1924
# Minimize output
2025
conf.errorMax = 0
2126
conf.verbosity = 0
@@ -31,7 +36,7 @@ proc setupCompilerForScanning(): ModuleGraph =
3136
let systemFileIdx = fileInfoIdx(conf, conf.libpath / RelativeFile("system.nim"))
3237
discard result.compileModule(systemFileIdx, {sfSystemModule})
3338

34-
proc extractExportedSymbols(graph: ModuleGraph, moduleFile: AbsoluteFile): seq[string] =
39+
proc extractExportedSymbols(graph: ModuleGraph, moduleFile: AbsoluteFile, modulePath: string): seq[string] =
3540
result = @[]
3641

3742
try:
@@ -40,6 +45,8 @@ proc extractExportedSymbols(graph: ModuleGraph, moduleFile: AbsoluteFile): seq[s
4045
let moduleSym = graph.compileModule(fileIdx, {})
4146

4247
if moduleSym.isNil:
48+
when defined(debugSuggestions):
49+
echo " Skipped (nil module): ", modulePath
4350
return
4451

4552
# Iterate through module's exported symbols using modulegraphs.allSyms
@@ -58,9 +65,10 @@ proc extractExportedSymbols(graph: ModuleGraph, moduleFile: AbsoluteFile): seq[s
5865

5966
result.add name
6067

61-
except CatchableError:
68+
except CatchableError as e:
6269
# Module failed to compile - skip it
63-
discard
70+
when defined(debugSuggestions):
71+
echo " Skipped (error): ", modulePath, " - ", e.msg
6472

6573
proc getModulePath(filePath: string): string =
6674
# Convert "lib/std/os.nim" -> "std/os"
@@ -114,7 +122,7 @@ proc generateSuggestions*() =
114122
if moduleCount mod 20 == 0:
115123
echo " Processed ", moduleCount, " modules..."
116124

117-
let symbols = extractExportedSymbols(graph, absPath)
125+
let symbols = extractExportedSymbols(graph, absPath, modulePath)
118126

119127
for symName in symbols:
120128
inc totalSymbols
@@ -172,3 +180,6 @@ const importSuggestions* = {
172180
let fileSize = getFileSize("compiler/suggest_imports.nim")
173181
echo "Done! Generated compiler/suggest_imports.nim"
174182
echo " File size: ", (fileSize div 1024), " KB"
183+
184+
when isMainModule:
185+
generateSuggestions()

0 commit comments

Comments
 (0)