mirror of
https://github.com/nim-lang/Nim.git
synced 2026-05-25 06:18:16 +00:00
fixes #25650 This pull request refactors and improves the dependency resolution logic in the Nim compiler, The most important changes are grouped below: ### Dependency Resolution Refactor * Replaced the `resolveFile` procedure with two more specialized procedures: `resolveImport` (which uses the compiler's module lookup rules for imports) and `resolveInclude` (which resolves includes relative to the including file or search paths). Updated all usages accordingly, improving clarity and correctness of dependency handling. [[1]](diffhunk://#diff-1203947eecb9ef641ce7ee029677f875eb983de050b82c65ca286517feed00e6L82-R94) [[2]](diffhunk://#diff-1203947eecb9ef641ce7ee029677f875eb983de050b82c65ca286517feed00e6L106-R103) [[3]](diffhunk://#diff-1203947eecb9ef641ce7ee029677f875eb983de050b82c65ca286517feed00e6L121-R118) * Removed the unused `strutils` import from `compiler/deps.nim` for cleaner dependencies. ### Testing Improvements * Added `import std/strbasics` to `tests/ic/tmiscs.nim` to ensure required symbols are available for tests. I tried to improve `resolveFile`, which is harder because either we need to add `lib/std` to search path and all of other nested directory to `--path` in `config/nim.cfg`. So I choose toi reuse `findModule` for imports
This commit is contained in:
@@ -10,10 +10,10 @@
|
||||
## Generate a .build.nif file for nifmake from a Nim project.
|
||||
## This enables incremental and parallel compilation using the `m` switch.
|
||||
|
||||
import std / [os, tables, sets, times, osproc, strutils]
|
||||
import std / [os, tables, sets, times, osproc]
|
||||
import options, msgs, lineinfos, pathutils
|
||||
|
||||
import "../dist/nimony/src/lib" / [nifstreams, nifcursors, bitabs, nifreader, nifbuilder]
|
||||
import "../dist/nimony/src/lib" / [nifstreams, bitabs, nifreader, nifbuilder]
|
||||
import "../dist/nimony/src/gear2" / modnames
|
||||
|
||||
type
|
||||
@@ -79,22 +79,19 @@ proc runNifler(c: DepContext; nimFile: string): bool =
|
||||
let exitCode = execShellCmd(cmd)
|
||||
result = exitCode == 0
|
||||
|
||||
proc resolveFile(c: DepContext; origin, toResolve: string): string =
|
||||
## Resolve an import path relative to origin file
|
||||
# Handle std/ prefix
|
||||
var path = toResolve
|
||||
if path.startsWith("std/"):
|
||||
path = path.substr(4)
|
||||
proc resolveImport(c: DepContext; origin, toResolve: string): string =
|
||||
## Resolve an import path using the compiler's normal module lookup rules.
|
||||
result = findModule(c.config, toResolve, origin).string
|
||||
|
||||
# Try relative to origin first
|
||||
proc resolveInclude(c: DepContext; origin, toResolve: string): string =
|
||||
## Resolve an include path relative to the including file or the search paths.
|
||||
let originDir = parentDir(origin)
|
||||
result = originDir / path.addFileExt("nim")
|
||||
result = originDir / toResolve.addFileExt("nim")
|
||||
if fileExists(result):
|
||||
return result
|
||||
|
||||
# Try search paths
|
||||
for searchPath in c.config.searchPaths:
|
||||
result = searchPath.string / path.addFileExt("nim")
|
||||
result = searchPath.string / toResolve.addFileExt("nim")
|
||||
if fileExists(result):
|
||||
return result
|
||||
|
||||
@@ -103,7 +100,7 @@ proc resolveFile(c: DepContext; origin, toResolve: string): string =
|
||||
proc traverseDeps(c: var DepContext; pair: FilePair; current: Node)
|
||||
|
||||
proc processInclude(c: var DepContext; includePath: string; current: Node) =
|
||||
let resolved = resolveFile(c, current.files[current.files.len - 1].nimFile, includePath)
|
||||
let resolved = resolveInclude(c, current.files[current.files.len - 1].nimFile, includePath)
|
||||
if resolved.len == 0 or not fileExists(resolved):
|
||||
return
|
||||
|
||||
@@ -118,7 +115,7 @@ proc processInclude(c: var DepContext; includePath: string; current: Node) =
|
||||
discard c.includeStack.pop()
|
||||
|
||||
proc processImport(c: var DepContext; importPath: string; current: Node) =
|
||||
let resolved = resolveFile(c, current.files[0].nimFile, importPath)
|
||||
let resolved = resolveImport(c, current.files[0].nimFile, importPath)
|
||||
if resolved.len == 0 or not fileExists(resolved):
|
||||
return
|
||||
|
||||
|
||||
Reference in New Issue
Block a user