From 60bb9c75ccac37b74fa203e49e043cd19073b1ce Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Tue, 21 Apr 2026 22:38:33 +0800 Subject: [PATCH] fixes #25650; nim ic import std/strbasics (#25760) 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 --- compiler/deps.nim | 25 +++++++++++-------------- tests/ic/tmiscs.nim | 1 + 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/compiler/deps.nim b/compiler/deps.nim index 6b891fac9f..18d6608fd0 100644 --- a/compiler/deps.nim +++ b/compiler/deps.nim @@ -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 diff --git a/tests/ic/tmiscs.nim b/tests/ic/tmiscs.nim index aabdd92601..e90ad05244 100644 --- a/tests/ic/tmiscs.nim +++ b/tests/ic/tmiscs.nim @@ -10,6 +10,7 @@ discard """ @[1, 2] ''' """ +import std/strbasics # Object variant / case object type