fix: leanpkg: actually detect import cycles
This commit is contained in:
parent
2091a09fa1
commit
27d52a352f
6 changed files with 19 additions and 3 deletions
|
|
@ -40,8 +40,8 @@ partial def buildModule (mod : Name) : BuildM Result := do
|
|||
let ctx ← read
|
||||
if ctx.parents.contains mod then
|
||||
-- cyclic import
|
||||
let cycle := ctx.parents.dropWhile (· != mod) ++ [mod]
|
||||
let cycle := cycle.reverse.map (s!" {·}")
|
||||
let cycle := mod :: (ctx.parents.partition (· != mod)).1 ++ [mod]
|
||||
let cycle := cycle.map (s!" {·}")
|
||||
throw <| IO.userError s!"import cycle detected:\n{"\n".intercalate cycle}"
|
||||
|
||||
if let some r := (← get).modTasks.find? mod then
|
||||
|
|
@ -54,7 +54,9 @@ partial def buildModule (mod : Name) : BuildM Result := do
|
|||
-- recursively build dependencies and calculate transitive `maxMTime`
|
||||
let (imports, _, _) ← Elab.parseImports (← IO.FS.readFile leanFile) leanFile.toString
|
||||
let localImports := imports.filter (·.module.getRoot == ctx.pkg)
|
||||
let deps ← localImports.mapM (buildModule ·.module)
|
||||
let deps ← localImports.mapM fun i =>
|
||||
withReader (fun ctx => { ctx with parents := mod :: ctx.parents }) <|
|
||||
buildModule i.module
|
||||
let depMTimes ← deps.mapM (·.maxMTime)
|
||||
let maxMTime := List.maximum? (leanMData.modified :: ctx.moreDepsMTime :: depMTimes) |>.get!
|
||||
|
||||
|
|
|
|||
|
|
@ -148,3 +148,10 @@ add_test(NAME leanpkgtest
|
|||
(cd ../a; leanpkg build lib)
|
||||
leanpkg build bin LINK_OPTS=../a/build/lib/libA.a
|
||||
./build/bin/B")
|
||||
|
||||
add_test(NAME leanpkgtest_cyclic
|
||||
WORKING_DIRECTORY "${LEAN_SOURCE_DIR}/../tests/leanpkg/cyclic"
|
||||
COMMAND bash -c "
|
||||
set -eu
|
||||
export PATH=${LEAN_BIN}:$PATH
|
||||
leanpkg build 2>&1 | grep 'import cycle'")
|
||||
|
|
|
|||
1
tests/leanpkg/cyclic/Cyclic.lean
Normal file
1
tests/leanpkg/cyclic/Cyclic.lean
Normal file
|
|
@ -0,0 +1 @@
|
|||
import Cyclic.A
|
||||
1
tests/leanpkg/cyclic/Cyclic/A.lean
Normal file
1
tests/leanpkg/cyclic/Cyclic/A.lean
Normal file
|
|
@ -0,0 +1 @@
|
|||
import Cyclic.A.B
|
||||
1
tests/leanpkg/cyclic/Cyclic/A/B.lean
Normal file
1
tests/leanpkg/cyclic/Cyclic/A/B.lean
Normal file
|
|
@ -0,0 +1 @@
|
|||
import Cyclic
|
||||
4
tests/leanpkg/cyclic/leanpkg.toml
Normal file
4
tests/leanpkg/cyclic/leanpkg.toml
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
[package]
|
||||
name = "cyclic"
|
||||
version = "0.1"
|
||||
lean_version = "leanprover/lean4:master"
|
||||
Loading…
Add table
Reference in a new issue