-
Notifications
You must be signed in to change notification settings - Fork 208
Expand file tree
/
Copy pathinstall.nim
More file actions
623 lines (547 loc) · 29.2 KB
/
Copy pathinstall.nim
File metadata and controls
623 lines (547 loc) · 29.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
## Package install pipeline: downloading (via download.nim), copying into
## pkgcache/pkgs2, before/after hooks, bin symlinks and reverse-dep bookkeeping.
import std/[sequtils, sets, options, os, strutils, tables, strformat]
import chronos
import nimblesat, packageinfotypes, options, version, declarativeparser, packageinfo, common,
cli, tools, nimscriptexecutor, packagemetadatafile,
displaymessages, reversedeps, developfile, urls, download, sha1hashes,
versiondiscovery, nimresolution, build
type PkgDownloadEntry = object
name: string
ver: Version
pv: PkgTuple
vcsRevision: Sha1Hash
isRoot: bool
dlInfo: Option[PackageDownloadInfo] # resolved after collectDownloadEntries, before download
download: Future[void] # set during downloadPkgs; nil if cached or skipped
proc getPkgInfoFromSolved*(satResult: SATResult, solvedPkg: SolvedPackage, options: Options): PackageInfo =
for pkg in satResult.pkgs.toSeq:
if nameMatches(pkg, solvedPkg.pkgName, options):
return pkg
for pkg in satResult.pkgList.toSeq:
#For the pkg list we need to check the version as there may be multiple versions of the same package
if nameMatches(pkg, solvedPkg.pkgName, options) and pkg.basicInfo.version == solvedPkg.version:
return pkg
if solvedPkg.pkgName.isNim:
result = initPackageInfo()
result.basicInfo.name = solvedPkg.pkgName
result.basicInfo.version = solvedPkg.version
return
writeStackTrace()
raise newNimbleError[NimbleError]("Package not found in solution: " & $solvedPkg.pkgName & " " & $solvedPkg.version)
proc isInDevelopMode*(pkgInfo: PackageInfo, options: Options): bool =
if pkgInfo.developFileExists or
(not pkgInfo.myPath.startsWith(options.getPkgsDir) and pkgInfo.basicInfo.name != options.satResult.rootPackage.basicInfo.name):
return true
return false
proc displaySatisfiedMsg*(solvedPkgs: seq[SolvedPackage], pkgToInstall: seq[(string, Version)], options: Options) =
if options.verbosity == LowPriority:
for pkg in solvedPkgs:
if pkg.pkgName notin pkgToInstall.mapIt(it[0]):
for req in pkg.requirements:
displayInfo(pkgDepsAlreadySatisfiedMsg(req), MediumPriority)
proc displayDevelopDepsMsg*(satResult: SATResult, options: Options) =
## Surface develop-linked dependencies so users can confirm the link is active
for pkg in satResult.pkgs:
if pkg.basicInfo.name.isNim: continue
if pkg.basicInfo.name == satResult.rootPackage.basicInfo.name: continue
if pkg.isInDevelopMode(options):
let srcDir = pkg.myPath.parentDir
displayInfo(&"{pkg.basicInfo.name} [develop: {srcDir}]", HighPriority)
proc activateSolvedPkgFeatures*(satResult: SATResult, options: Options) =
for pkg in satResult.pkgs:
for pkgTuple, activeFeatures in pkg.activeFeatures:
let pkgWithFeature = satResult.getPkgInfoFromSolution(pkgTuple, options)
appendGloballyActiveFeatures(pkgWithFeature.basicInfo.name, activeFeatures)
proc addReverseDeps*(satResult: SATResult, options: Options) =
for solvedPkg in satResult.solvedPkgs:
if solvedPkg.pkgName.isNim or solvedPkg.pkgName.isFileURL: continue #Dont add fileUrl to reverse deps.
var reverseDepPkg = satResult.getPkgInfoFromSolved(solvedPkg, options)
# Check if THIS package (the one that depends on others) is a development package
if reverseDepPkg.isInDevelopMode(options):
reverseDepPkg.source = psDevelop
for dep in solvedPkg.deps:
if dep.pkgName.isNim: continue
try:
if dep.pkgName.isFileURL:
continue
let depPkg = satResult.getPkgInfoFromSolved(dep, options)
addRevDep(options.nimbleData, depPkg.basicInfo, reverseDepPkg)
except CatchableError:
# Skip packages that can't be found (e.g., installed during hook execution)
# This can happen when packages are installed recursively during hooks
displayInfo("Skipping reverse dependency for package not found in solution: " & $dep, MediumPriority)
proc executeHook(nimBin: Option[string], dir: string, options: var Options, action: ActionType, before: bool) =
let nimbleFile = findNimbleFile(dir, false, options).splitFile.name
let hook = VisitedHook(pkgName: nimbleFile, action: action, before: before)
if hook in options.visitedHooks:
return
options.visitedHooks.add(hook)
cd dir: # Make sure `execHook` executes the correct .nimble file.
if not execHook(nimBin, options, action, before):
if before:
raise nimbleError("Pre-hook prevented further execution.")
else:
raise nimbleError("Post-hook prevented further execution.")
proc packageExists(nimBin: Option[string], pkgInfo: PackageInfo, options: Options):
Option[PackageInfo] =
## Checks whether a package `pkgInfo` already exists in the Nimble cache. If a
## package already exists returns the `PackageInfo` of the package in the
## cache otherwise returns `none`. Raises a `NimbleError` in the case the
## package exists in the cache but it is not valid.
##
## Also checks for packages with the same name and checksum but different version
## to avoid storing the same content multiple times with different version labels.
let pkgDestDir = pkgInfo.getPkgDest(options)
if fileExists(pkgDestDir / packageMetaDataFileName):
var oldPkgInfo = initPackageInfo()
try:
oldPkgInfo = pkgDestDir.getPkgInfo(options, nimBin = nimBin)
except CatchableError as error:
raise nimbleError(&"The package inside \"{pkgDestDir}\" is invalid.",
details = error)
fillMetaData(oldPkgInfo, pkgDestDir, true, options)
return some(oldPkgInfo)
# Check if a package with the same name and checksum exists with a different version.
# This prevents storing the same content multiple times with different version labels.
if pkgInfo.basicInfo.checksum != notSetSha1Hash:
let pkgsDir = options.getPkgsDir()
let pkgNamePrefix = pkgInfo.basicInfo.name & "-"
let checksumSuffix = "-" & $pkgInfo.basicInfo.checksum
for kind, path in walkDir(pkgsDir):
if kind == pcDir:
let dirName = path.extractFilename
# Check if this is the same package (name matches) with same checksum
if dirName.startsWith(pkgNamePrefix) and dirName.endsWith(checksumSuffix):
if fileExists(path / packageMetaDataFileName):
var oldPkgInfo = initPackageInfo()
try:
oldPkgInfo = path.getPkgInfo(options, nimBin = nimBin)
except CatchableError:
continue # Skip invalid packages
fillMetaData(oldPkgInfo, path, true, options)
return some(oldPkgInfo)
return none[PackageInfo]()
proc copyInstallFiles(srcDir, destDir: string, pkgInfo: PackageInfo,
options: Options): HashSet[string] =
## Copies selected files from srcDir to destDir during installation.
## Skips dot directories (like .git) and tests unless explicitly in installDirs.
var copied: HashSet[string]
iterInstallFiles(srcDir, pkgInfo, options,
proc (file: string) =
let relPath = file.relativePath(srcDir).replace('\\', '/')
for part in relPath.split('/'):
if part.len > 0 and part[0] == '.':
if part notin pkgInfo.installDirs:
return
if part == "tests":
if part notin pkgInfo.installDirs:
return
createDir(changeRoot(srcDir, destDir, file.splitFile.dir))
let dest = changeRoot(srcDir, destDir, file)
copied.incl copyFileD(file, dest)
)
copied
proc installFromDirDownloadInfo(nimBin: Option[string], downloadDir: string, url: string, pv: PkgTuple, options: var Options): PackageInfo {.instrument.} =
## Installs a package from a download directory (pkgcache).
## flow: pkgcache -> buildtemp (build) -> pkgs2 (install minimum)
let dir = downloadDir
var pkgInfo = getPkgInfo(dir, options, nimBin = nimBin)
var depsOptions = options
depsOptions.depsOnly = false
# Handle version mismatch between git tag/lock file and .nimble file.
# Tag version takes precedence - if the nimble file has a different version,
# it's simply stale/wrong. Override it with the tag version.
if pv.ver.kind == verEq and pkgInfo.basicInfo.version != pv.ver.ver:
pkgInfo.basicInfo.version = pv.ver.ver
display("Installing", "$1@$2" %
[pkgInfo.basicInfo.name, $pkgInfo.basicInfo.version],
priority = MediumPriority)
let oldPkg = packageExists(nimBin, pkgInfo, options)
if oldPkg.isSome:
# In the case we already have the same package in the cache then only merge
# the new package special versions to the old one.
displayWarning(pkgAlreadyExistsInTheCacheMsg(pkgInfo), MediumPriority)
var oldPkg = oldPkg.get
# Add the requested version to specialVersions so this package can satisfy
# requirements for that version (important when same content has multiple version tags)
oldPkg.metaData.specialVersions.incl pkgInfo.basicInfo.version
oldPkg.metaData.specialVersions.incl pkgInfo.metaData.specialVersions
saveMetaData(oldPkg.metaData, oldPkg.getNimbleFileDir, changeRoots = false)
return oldPkg
let pkgDestDir = pkgInfo.getPkgDest(options)
# Fill package Meta data
pkgInfo.metaData.url = url
pkgInfo.source = psLocal # Not psDevelop — this is being installed, not developed
# Don't copy artifacts if project local deps mode and "installing" the top level package.
if not (options.localdeps and options.isInstallingTopLevel(dir)):
var filesInstalled: HashSet[string]
let hasBinaries = pkgInfo.bin.len > 0 and not pkgInfo.basicInfo.name.isNim
let hasPreInstallHook = pkgInfo.hasBeforeInstallHook and not pkgInfo.basicInfo.name.isNim
# Install pipeline: workDir → before-install hook → build → copy to pkgDestDir → after-install hook
# Optimization: skip buildtemp when we know it's safe (no binaries, no before-install hook, no submodules)
let hasSubmodules = not options.ignoreSubmodules and fileExists(downloadDir / ".gitmodules")
let canSkipBuildTemp = not hasBinaries and not hasPreInstallHook and not hasSubmodules
var workDir, buildTempDir: string
var workPkgInfo: PackageInfo
if canSkipBuildTemp:
# Optimized path: work directly from pkgcache
workDir = downloadDir
workPkgInfo = pkgInfo
else:
display("Info:", "Using buildtemp for " & pkgInfo.basicInfo.name &
" (binaries: " & $hasBinaries & ", before-install hook: " & $hasPreInstallHook &
", submodules: " & $hasSubmodules & ")",
priority = LowPriority)
buildTempDir = options.getPkgBuildTempDir(
pkgInfo.basicInfo.name,
pkgInfo.basicInfo.version.toDirectoryName,
$pkgInfo.basicInfo.checksum
)
# Clean up any existing temp dir from previous failed install
if dirExists(buildTempDir):
removeDir(buildTempDir)
createDir(buildTempDir)
# Copy ALL files and directories from pkgcache to temp build dir
let buildTempBase = options.getBuildTempDir()
let nimbleDirBase = options.getNimbleDir()
let buildTempIsInsideDownload = buildTempBase.len > 0 and
buildTempBase.startsWith(downloadDir & "/")
let nimbleDirIsInsideDownload = nimbleDirBase.len > 0 and
nimbleDirBase.startsWith(downloadDir & "/")
# Use yieldFilter to also yield directories (important for empty dirs like .git/refs/)
# and symlinks. Issue #1730: symlinked files (a shared source module a binary
# imports, a shared binary.nim.cfg, ...) were skipped here, so the build in
# buildtemp failed even though `nimble build` (which builds in place) worked.
for path in walkDirRec(downloadDir,
yieldFilter = {pcFile, pcDir, pcLinkToFile, pcLinkToDir}):
if buildTempIsInsideDownload and path.startsWith(buildTempBase):
continue
if nimbleDirIsInsideDownload and path.startsWith(nimbleDirBase):
continue
let relPath = path.substr(downloadDir.len)
if (DirSep & "nimbledeps" & DirSep) in relPath or
relPath.endsWith(DirSep & "nimbledeps"):
continue
if (DirSep & "tests" & DirSep) in relPath or
(DirSep & "testdata" & DirSep) in relPath:
continue
let destPath = changeRoot(downloadDir, buildTempDir, path)
if path.symlinkExists:
# Recreate the symlink so the build can resolve it (e.g. a shared
# source module or binary.nim.cfg). Fall back to copying the
# dereferenced content when the symlink can't be recreated (e.g.
# unprivileged Windows).
createDir(destPath.splitFile.dir)
try:
createSymlink(expandSymlink(path), destPath)
except OSError:
if path.fileExists:
discard copyFileD(path, destPath)
elif path.dirExists:
createDir(destPath)
else:
createDir(destPath.splitFile.dir)
discard copyFileD(path, destPath)
workPkgInfo = getPkgInfo(buildTempDir, options, nimBin = nimBin)
if pv.ver.kind == verEq and workPkgInfo.basicInfo.version != pv.ver.ver:
workPkgInfo.basicInfo.version = pv.ver.ver
workDir = buildTempDir
# Populate submodules in buildtemp
if hasSubmodules:
updateSubmodules(workDir)
# Run before-install hook (in buildtemp, before build)
executeHook(nimBin, workDir, options, actionInstall, before = true)
# Build binaries (only if there are any)
if hasBinaries:
let paths = getPathsAllPkgs(options, nimBin)
let flags = if options.action.typ in {actionInstall, actionPath, actionUninstall, actionDevelop}:
options.action.passNimFlags
else:
@[]
buildFromDir(workPkgInfo, paths, "-d:release" & flags, options, nimBin)
try:
createDir(pkgDestDir)
# For global installs, flatten srcDir so --nimblePath scanning finds modules
# at the package root (e.g. pkgs2/intops-xxx/intops.nim instead of .../src/intops.nim).
# For local installs (nimbledeps), keep srcDir structure because --path entries
# use getRealDir() which points to the srcDir subdirectory.
let isLocalInstall = dirExists(nimbledeps) or fileExists(developFileName)
let installSrcDir = if isLocalInstall: workPkgInfo.getNimbleFileDir()
else: workPkgInfo.getRealDir()
filesInstalled.incl copyInstallFiles(installSrcDir, pkgDestDir, workPkgInfo, options)
# When srcDir is flattened (global install), installDirs at the package root
# won't be found by copyInstallFiles (which starts from srcDir). Copy them separately.
if not isLocalInstall and workPkgInfo.srcDir.len > 0:
let realDir = workPkgInfo.getRealDir()
for dir in workPkgInfo.installDirs:
let srcDirPath = workDir / dir
if dirExists(srcDirPath) and not dirExists(realDir / dir):
let destDirPath = pkgDestDir / dir
createDir(destDirPath)
for path in walkDirRec(srcDirPath):
let relPath = path.relativePath(srcDirPath)
let dest = destDirPath / relPath
createDir(dest.splitFile.dir)
filesInstalled.incl copyFileD(path, dest)
# Copy the .nimble file
let nimbleFileDest = changeRoot(workPkgInfo.myPath.splitFile.dir, pkgDestDir, workPkgInfo.myPath)
filesInstalled.incl copyFileD(workPkgInfo.myPath, nimbleFileDest)
# Copy built binaries (only if there are any)
if hasBinaries:
for bin, src in workPkgInfo.bin:
let binDest = if dirExists(pkgDestDir / bin): bin & ".out" else: bin
let srcBin = workPkgInfo.getOutputDir(bin)
if fileExists(srcBin):
createDir((pkgDestDir / binDest).parentDir())
filesInstalled.incl copyFileD(srcBin, pkgDestDir / binDest)
pkgInfo.myPath = nimbleFileDest
pkgInfo.metaData.files = filesInstalled.toSeq
if pv.ver.kind == verSpecial:
pkgInfo.metadata.specialVersions.incl pv.ver.spe
saveMetaData(pkgInfo.metaData, pkgDestDir)
# Run after-install hook
executeHook(nimBin, pkgDestDir, options, actionInstall, before = false)
# Create bin symlinks (only if there are binaries)
if hasBinaries:
createBinSymlink(pkgInfo, options)
finally:
# Cleanup buildtemp if used
if not canSkipBuildTemp and dirExists(buildTempDir):
removeDir(buildTempDir)
else:
display("Warning:", "Skipped copy in project local deps mode", Warning)
pkgInfo.source = psInstalled
displaySuccess(pkgInstalledMsg(pkgInfo.basicInfo.name), MediumPriority)
pkgInfo
proc isRoot(pkgInfo: PackageInfo, satResult: SATResult): bool =
pkgInfo.basicInfo.name == satResult.rootPackage.basicInfo.name and pkgInfo.basicInfo.version == satResult.rootPackage.basicInfo.version
proc getVersionRangeFoPkgToInstall(satResult: SATResult, name: string, ver: Version): VersionRange =
if satResult.rootPackage.basicInfo.name == name and satResult.rootPackage.basicInfo.version == ver:
#It could be the case that we are installing a special version of a root package
if name == satResult.rootPackage.basicInfo.name and ver == satResult.rootPackage.basicInfo.version:
let specialVersion = satResult.rootPackage.getNimbleFileDir().lastPathPart().split("_")[^1]
if "#" in specialVersion:
return parseVersionRange(specialVersion)
return ver.toVersionRange()
proc collectDownloadEntries(satResult: SATResult, pkgsToInstall: seq[(string, Version)],
rootName: string, installedPkgs: HashSet[PackageInfo],
options: Options): seq[PkgDownloadEntry] =
for (name, ver) in pkgsToInstall:
let verRange = satResult.getVersionRangeFoPkgToInstall(name, ver)
let vcsRevision = if name in options.satResult.lockFileVcsRevisions:
options.satResult.lockFileVcsRevisions[name]
else:
notSetSha1Hash
var pv = (name: name, ver: verRange)
let isRootPkg = pv.name == rootName and
(rootName notin installedPkgs.mapIt(it.basicInfo.name) or satResult.rootPackage.hasLockFile(options))
if not isRootPkg and pv.name in options.satResult.normalizedRequirements:
pv.name = options.satResult.normalizedRequirements[pv.name]
result.add(PkgDownloadEntry(name: name, ver: ver, pv: pv, vcsRevision: vcsRevision,
isRoot: isRootPkg))
proc resolveDownloadInfo(entries: var seq[PkgDownloadEntry], options: Options) =
for i in 0 ..< entries.len:
if entries[i].isRoot:
continue
var pv = entries[i].pv
var dlInfo: PackageDownloadInfo
try:
dlInfo = getPackageDownloadInfo(pv, options, doPrompt = true, vcsRevision = entries[i].vcsRevision)
except CatchableError as e:
let url = getUrlFromPkgName(pv.name, options.satResult.pkgVersionTable, options)
if url != "":
pv.name = url
entries[i].pv = pv
dlInfo = getPackageDownloadInfo(pv, options, doPrompt = true, vcsRevision = entries[i].vcsRevision)
else:
raise e
entries[i].dlInfo = some(dlInfo)
proc doDownload(dlInfo: PackageDownloadInfo, options: Options, nimBin: Option[string]): Future[void] {.async.} =
discard await downloadFromDownloadInfoAsync(dlInfo, options, nimBin)
proc downloadPkgs(entries: var seq[PkgDownloadEntry], options: Options, nimBin: Option[string]) =
# Deduplicate by downloadDir so packages sharing a repo (different subdirs) only clone once
var started: Table[string, Future[void]]
for i in 0 ..< entries.len:
if entries[i].isRoot or entries[i].pv.name.isFileURL:
continue
let dlInfo = entries[i].dlInfo.get
if dirExists(dlInfo.downloadDir) and pkgDirHasNimble(dlInfo.downloadDir, options):
# Validate cached version matches expected version (issue #1692)
if not isCacheVersionValid(dlInfo.downloadDir, entries[i].pv.ver, options):
displayWarning(&"Cached version doesn't match expected {entries[i].pv.ver} for {entries[i].name}, re-downloading", HighPriority)
removeDir(dlInfo.downloadDir)
else:
continue
if dirExists(dlInfo.downloadDir) and not pkgDirHasNimble(dlInfo.downloadDir, options):
displayWarning(&"Cache directory is corrupted (no .nimble file found): {dlInfo.downloadDir}", HighPriority)
displayWarning("Removing corrupted cache and re-downloading...", HighPriority)
try:
removeDir(dlInfo.downloadDir)
except CatchableError as e:
displayWarning(&"Failed to remove corrupted cache: {e.msg}", HighPriority)
if dlInfo.downloadDir in started:
entries[i].download = started[dlInfo.downloadDir]
else:
var dlOptions = options
dlOptions.ignoreSubmodules = true
dlOptions.enableTarballs = false
entries[i].download = doDownload(dlInfo, dlOptions, nimBin)
started[dlInfo.downloadDir] = entries[i].download
if not options.parallelDiscovery:
waitFor entries[i].download
if options.parallelDiscovery:
var pending: seq[Future[void]] = @[]
for entry in entries:
if not entry.download.isNil:
pending.add entry.download
if pending.len > 0:
waitFor allFutures(pending)
var errors: seq[string] = @[]
for entry in entries:
if not entry.download.isNil and entry.download.failed:
errors.add("Failed to download " & entry.name & ": " & entry.download.error.msg)
if errors.len > 0:
raise nimbleError(errors.join("\n"))
proc installEntry(entry: PkgDownloadEntry, satResult: var SATResult,
options: var Options, nimBin: Option[string]): (PackageInfo, bool) =
var installedPkgInfo: PackageInfo
var wasNewlyInstalled = false
if entry.isRoot:
if satResult.rootPackage.developFileExists or options.localdeps:
satResult.rootPackage.source = psDevelop
installedPkgInfo = satResult.rootPackage
wasNewlyInstalled = true
else:
if satResult.rootPackage.basicInfo.name.isNim:
createBinSymlinkForNim(satResult.rootPackage, options)
installedPkgInfo = satResult.rootPackage
wasNewlyInstalled = true
else:
let tempPkgInfo = getPkgInfo(satResult.rootPackage.getNimbleFileDir(), options, nimBin = nimBin)
let oldPkg = packageExists(nimBin, tempPkgInfo, options)
installedPkgInfo = installFromDirDownloadInfo(nimBin, satResult.rootPackage.getNimbleFileDir(), satResult.rootPackage.metaData.url, entry.pv, options).toRequiresInfo(options, nimBin = nimBin)
wasNewlyInstalled = oldPkg.isNone
else:
let dlInfo = entry.dlInfo.get
var downloadDir = dlInfo.downloadDir / dlInfo.subdir
if entry.pv.name.isFileURL:
downloadDir = dlInfo.url.extractFilePathFromURL()
assert dirExists(downloadDir)
if entry.pv.name.isFileURL:
installedPkgInfo = getPackageFromFileUrl(dlInfo.url, options, nimBin = nimBin).toRequiresInfo(options, nimBin = nimBin)
else:
let tempPkgInfo = getPkgInfo(downloadDir, options, nimBin = nimBin)
let oldPkg = packageExists(nimBin, tempPkgInfo, options)
installedPkgInfo = installFromDirDownloadInfo(nimBin, downloadDir, dlInfo.url, entry.pv, options).toRequiresInfo(options, nimBin = nimBin)
wasNewlyInstalled = oldPkg.isNone
if installedPkgInfo.metadata.url == "" and entry.pv.name.isUrl:
installedPkgInfo.metadata.url = entry.pv.name
(installedPkgInfo, wasNewlyInstalled)
proc installPkgs*(satResult: var SATResult, options: var Options, nimBin: Option[string]) {.instrument.} =
# options.debugSATResult("installPkgs")
#At this point the packages are already downloaded.
#We still need to install them aka copy them from the cache to the nimbleDir + run preInstall and postInstall scripts
let isInRootDir = options.startDir == satResult.rootPackage.myPath.parentDir
var pkgsToInstall = satResult.pkgsToInstall
# Always filter out nim - it's handled separately through installNimFromBinariesDir
pkgsToInstall = pkgsToInstall.filterIt(not it[0].isNim)
#If we are not in the root folder, means user is installing a package globally so we need to install root
var installedPkgs = initHashSet[PackageInfo]()
# echo "isInRootDir ", isInRootDir, " startDir ", options.startDir, " rootDir ", satResult.rootPackage.myPath.parentDir
if options.action.typ == actionInstall and not options.depsOnly and
not (isInRootDir and options.action.packages.len > 0): #only install action install the root package: #skip root when in localdeps mode and in rootdir or when installing specific packages from root
pkgsToInstall.add((name: satResult.rootPackage.basicInfo.name, ver: satResult.rootPackage.basicInfo.version))
else:
#Root can be assumed as installed as the only global action one can do is install
installedPkgs.incl(satResult.rootPackage)
displaySatisfiedMsg(satResult.solvedPkgs, pkgsToInstall, options)
displayDevelopDepsMsg(satResult, options)
#If package is in develop mode, we dont need to install it.
var newlyInstalledPkgs = initHashSet[PackageInfo]()
let rootName = satResult.rootPackage.basicInfo.name
# options.debugSATResult()
# For develop, resolve pkgsToInstall from vendor packages instead of downloading.
# The SAT solver may flag vendor packages for installation when cached versions
# shadow them in processRequirements's hasVersion check.
if options.action.typ == actionDevelop:
let developPkgs = processDevelopDependencies(satResult.rootPackage, options, nimBin)
var remaining: seq[(string, Version)] = @[]
for (name, ver) in pkgsToInstall:
var found = false
for devPkg in developPkgs:
if cmpIgnoreCase(devPkg.basicInfo.name, name) == 0:
satResult.pkgs.incl(devPkg)
found = true
break
if not found:
remaining.add((name, ver))
pkgsToInstall = remaining
if isInRootDir and options.action.typ == actionInstall and not options.depsOnly:
executeHook(nimBin, getCurrentDir(), options, actionInstall, before = true)
var downloadEntries = collectDownloadEntries(satResult, pkgsToInstall, rootName, installedPkgs, options)
resolveDownloadInfo(downloadEntries, options)
downloadPkgs(downloadEntries, options, nimBin)
# === Phase 2: Install all packages sequentially ===
for i in 0 ..< downloadEntries.len:
let (installedPkgInfo, wasNewlyInstalled) = installEntry(downloadEntries[i], satResult, options, nimBin)
satResult.pkgs.incl(installedPkgInfo)
installedPkgs.incl(installedPkgInfo)
if wasNewlyInstalled:
newlyInstalledPkgs.incl(installedPkgInfo)
#we need to activate the features for the recently installed package
#so they are activated in the build step
options.satResult.activateSolvedPkgFeatures(options)
for pkg in installedPkgs:
var pkg = pkg
# fillMetaData(pkg, pkg.getRealDir(), false, options)
options.satResult.pkgs.incl pkg
#Only build root for this actions
let rootBuildActions = { actionInstall, actionBuild, actionRun }
# For build action, only build the root package
# For install action, only build newly installed packages
var pkgsToBuild = if options.action.typ == actionBuild:
installedPkgs.toSeq.filterIt(it.isRoot(options.satResult))
else:
# Only build packages that were newly installed in this session
newlyInstalledPkgs.toSeq
if options.action.typ == actionInstall and not options.thereIsNimbleFile and not options.depsOnly:
if not satResult.rootPackage.basicInfo.name.isNim:
#RootPackage shouldnt be in the pkgcache for global installs. We need to move it to the
#install dir.
let downloadDir = satResult.rootPackage.myPath.parentDir()
let pv = (name: satResult.rootPackage.basicInfo.name, ver: satResult.rootPackage.basicInfo.version.toVersionRange())
satResult.rootPackage = installFromDirDownloadInfo(nimBin, downloadDir, satResult.rootPackage.metaData.url, pv, options).toRequiresInfo(options, nimBin)
pkgsToBuild.add(satResult.rootPackage)
satResult.installedPkgs = installedPkgs.toSeq()
# Note: before-install and after-install hooks for installed packages now run
# inside installFromDirDownloadInfo (in buildtemp and install dir respectively).
# We only need to build packages that were NOT installed via installFromDirDownloadInfo:
# - Root package for actionBuild (built in current directory)
# - Develop mode packages (isLink = true)
for pkgToBuild in pkgsToBuild:
# Skip packages that were already built during install (not isLink)
# Only build root package in place or develop mode packages
if not pkgToBuild.isLink:
let isRoot = pkgToBuild.isRoot(options.satResult)
if not (isRoot and isInRootDir and options.action.typ == actionBuild):
# Package was installed via installFromDirDownloadInfo, already built
continue
if pkgToBuild.bin.len == 0:
if options.action.typ == actionBuild:
raise nimbleError(
"Nothing to build. Did you specify a module to build using the" &
" `bin` key in your .nimble file?")
else: #Skips building the package if it has no binaries
continue
# echo "Building package: ", pkgToBuild.basicInfo.name, " at ", pkgToBuild.myPath, " binaries: ", pkgToBuild.bin
let isRoot = pkgToBuild.isRoot(options.satResult) and isInRootDir
if isRoot and options.action.typ in rootBuildActions:
buildPkg(nimBin, pkgToBuild, isRoot, options)
satResult.buildPkgs.add(pkgToBuild)
elif pkgToBuild.isLink:
# Build develop mode packages
buildPkg(nimBin, pkgToBuild, false, options)
satResult.buildPkgs.add(pkgToBuild)
for pkg in satResult.installedPkgs.mitems:
satResult.pkgs.incl pkg