Skip to content

Commit a31e687

Browse files
authored
fix: lake: include module identifiers in trace (#12377)
This PR adds identifying information about a module available to `lean` (e.g., its name and package identifier) to the module's dependency trace. This ensures modules with different identification have different input hashes even if their source files and imports are identical.
1 parent 12ad957 commit a31e687

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

src/lake/Lake/Build/Module.lean

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,9 @@ private def Module.recBuildLean (mod : Module) : FetchM (Job ModuleOutputArtifac
744744
let srcTrace := leanJob.getTrace
745745
addTrace srcTrace
746746
addTrace <| traceOptions setup.options "options"
747+
addPureTrace setup.isModule "isModule"
748+
addPureTrace mod.name "Module.name"
749+
addPureTrace mod.pkg.id? "Package.id?"
747750
addPureTrace mod.leanArgs "Module.leanArgs"
748751
setTraceCaption s!"{mod.name.toString}:leanArts"
749752
let depTrace ← getTrace

src/lake/Lake/Build/Trace.lean

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ public def nil : Hash :=
109109

110110
public instance : NilTrace Hash := ⟨nil⟩
111111

112-
@[inline] public def ofNat (n : Nat) :=
112+
/-- Constructs a hash with with the value `n`. This is not equivalent to the hash of `n`. -/
113+
@[inline] public def ofNat (n : Nat) : Hash :=
113114
mk n.toUInt64
114115

115116
/-- Parse a hash from a JSON number. -/
@@ -142,12 +143,14 @@ public def ofHex? (s : String) : Option Hash :=
142143
public def hex (self : Hash) : String :=
143144
lpad (String.ofList <| Nat.toDigits 16 self.val.toNat) '0' 16
144145

146+
/-- Parse a hash from a string of decimal digits. -/
145147
public def ofDecimal? (s : String) : Option Hash :=
146148
s.toNat?.map ofNat
147149

148150
@[inline] public def ofString? (s : String) : Option Hash :=
149151
ofHex? s
150152

153+
/-- Laod a hash from a `.hash` file. -/
151154
public def load? (hashFile : FilePath) : BaseIO (Option Hash) :=
152155
ofString? <$> IO.FS.readFile hashFile |>.catchExceptions fun _ => pure none
153156

@@ -161,18 +164,25 @@ public instance : MixTrace Hash := ⟨mix⟩
161164

162165
public instance : ToString Hash := ⟨Hash.toString⟩
163166

164-
@[inline] public def ofString (str : String) :=
165-
mix nil <| mk <| hash str -- same as Name.mkSimple
167+
/-- Hash of a value using its `Hashable` instance. -/
168+
@[inline] public def ofHashable [Hashable α] (a : α) : Hash :=
169+
mix nil <| mk <| hash a -- same as Name.mkSimple
170+
171+
/-- Hash of a string (without line-ending normalization). -/
172+
@[inline] public def ofString (str : String) : Hash :=
173+
ofHashable str
166174

167175
/-- Hash of a line-ending normalized string. -/
168-
@[inline] public def ofText (str : String) :=
176+
@[inline] public def ofText (str : String) : Hash :=
169177
ofString str.crlfToLf
170178

179+
/-- Hash of a sequence of bytes. -/
171180
@[inline] public def ofByteArray (bytes : ByteArray) : Hash :=
172-
⟨hash bytes
181+
ofHashable bytes
173182

174-
@[inline] public def ofBool (b : Bool) :=
175-
mk (hash b)
183+
@[inline, deprecated ofHashable (since := "2026-02-06")]
184+
public def ofBool (b : Bool) : Hash :=
185+
ofHashable b
176186

177187
@[inline] public protected def toJson (self : Hash) : Json :=
178188
toJson self.hex
@@ -208,8 +218,7 @@ public instance [ComputeHash α m] : ComputeTrace α m Hash := ⟨ComputeHash.co
208218
@[inline] public def computeHash [ComputeHash α m] [MonadLiftT m n] (a : α) : n Hash :=
209219
liftM <| ComputeHash.computeHash a
210220

211-
public instance : ComputeHash Bool Id := ⟨Hash.ofBool⟩
212-
public instance : ComputeHash String Id := ⟨Hash.ofString⟩
221+
public instance [Hashable α] : ComputeHash α Id := ⟨Hash.ofHashable⟩
213222

214223
/--
215224
Compute the hash of a binary file.

0 commit comments

Comments
 (0)