Skip to content

Commit 12c859a

Browse files
authored
fix: lake: no lib files for exe template (#14366)
This PR fixes `lake new` and `lake init` to not emit library files on the `exe` template. It also fixes a related bug where the commands could sometimes overwrite library files for existing packages. These bugs were introduced in v4.10 with #2439. To prevent regressions, the `init` test has been updated to better validate the file structure produced by each template. Closes #11671
1 parent 49a89d6 commit 12c859a

2 files changed

Lines changed: 44 additions & 28 deletions

File tree

src/lake/Lake/CLI/Init.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ def initPkg
420420
let root := name
421421
let rootFile := Lean.modToFilePath dir root "lean"
422422
if tmp = .exe || (← rootFile.pathExists) then
423-
return (root, some rootFile)
423+
return (root, none)
424424
else
425425
let root := toUpperCamelCase name
426426
let rootFile := Lean.modToFilePath dir root "lean"

tests/lake/tests/init/test.sh

Lines changed: 43 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -34,60 +34,77 @@ done
3434

3535
# Test default (std) template
3636

37+
test_std() {
38+
local pkg=$1; local mod=$2; local exe=${3:-$1}
39+
test_exp -f $pkg/Main.lean
40+
test_exp -f $pkg/$mod.lean
41+
test_exp -f $pkg/$mod/Basic.lean
42+
test_run -d $pkg exe $exe
43+
test_exp -f $pkg/.lake/build/lib/lean/$mod.olean
44+
}
45+
3746
echo "# TEST: default template"
3847
test_run new hello .lean
39-
test_run -d hello exe hello
40-
test -f hello/.lake/build/lib/lean/Hello.olean
48+
test_std hello Hello
4149
rm -rf hello
4250
test_run new hello .toml
43-
test_run -d hello exe hello
44-
test -f hello/.lake/build/lib/lean/Hello.olean
51+
test_std hello Hello
4552
rm -rf hello
4653

4754
# Test exe template
4855

56+
test_exe() {
57+
local pkg=$1; local mod=${2:-$1}; local exe=${3:-$1}
58+
test_exp ! -d $pkg/$mod
59+
test_exp -f $pkg/Main.lean
60+
test_run -d $pkg exe $exe
61+
}
62+
4963
echo "# TEST: exe template"
5064
test_run new hello exe.lean
51-
test -f hello/Main.lean
52-
test_run -d hello exe hello
65+
test_exe hello Hello
5366
rm -rf hello
5467
test_run new hello exe.toml
55-
test -f hello/Main.lean
56-
test_run -d hello exe hello
68+
test_exe hello Hello
5769
rm -rf hello
5870

5971
# Test lib template
6072

73+
test_lib() {
74+
local pkg=$1; local mod=$2
75+
test_exp -f $pkg/$mod.lean
76+
test_exp -f $pkg/$mod/Basic.lean
77+
test_exp ! -f $pkg/Main.lean
78+
test_run -d $pkg build $mod
79+
test_exp -f $pkg/.lake/build/lib/lean/$mod.olean
80+
}
81+
6182
echo "# TEST: lib template"
6283
test_run new hello lib.lean
63-
test_run -d hello build Hello
64-
test -f hello/.lake/build/lib/lean/Hello.olean
84+
test_lib hello Hello
6585
rm -rf hello
6686
test_run new hello lib.toml
67-
test_run -d hello build Hello
68-
test -f hello/.lake/build/lib/lean/Hello.olean
87+
test_lib hello Hello
6988
rm -rf hello
7089

7190
# Test math & math-lax template
7291

7392
test_math_tmp () {
74-
tmp=$1; pkg=$2; mod=$3
93+
local tmp=$1; local pkg=$2; local mod=$3
7594
echo "# TEST: $tmp template"
7695
# Use `--offline` and remove the `require`,
7796
# since we do not wish to download mathlib during tests
7897
ELAN_TOOLCHAIN="v4.0.0-test" test_run new $pkg $tmp.lean --offline
7998
sed_i '/^require.*/{N;d;}' $pkg/lakefile.lean
8099
test_cmd_out "v4.0.0-test" cat $pkg/lean-toolchain
81-
test_run -d $pkg build $mod
82-
test -f $pkg/.lake/build/lib/lean/$mod.olean
100+
test_lib $pkg $mod
83101
rm -rf $pkg
84102
# Use `--offline` and remove the `require`,
85103
# since we do not wish to download mathlib during tests
86104
test_out "creating a new math package with a non-release Lean toolchain" \
87105
new $pkg $tmp.toml --offline
88106
sed_i '/^\[\[require\]\]/{N;N;N;d;}' $pkg/lakefile.toml
89-
test_run -d $pkg build $mod
90-
test -f $pkg/.lake/build/lib/lean/$mod.olean
107+
test_lib $pkg $mod
91108
}
92109

93110
test_math_tmp math-lax qed-lax QedLax
@@ -99,39 +116,38 @@ echo "# TEST: init ."
99116
mkdir hello
100117
pushd hello
101118
test_run init .
102-
test_run exe hello
119+
test_std . Hello hello
103120
popd
104121

105122
# Test creating packages with uppercase names
106123
# https://github.com/leanprover/lean4/issues/2540
107124

108125
echo "# TEST: Uppercase package names"
109126
test_run new HelloWorld
110-
test_run -d HelloWorld exe helloworld
127+
test_std HelloWorld HelloWorld helloworld
111128

112129
# Test creating multi-level packages with a `.`
113130

114131
echo "# TEST: Packages with a `.`"
115132
test_run new hello.world
116-
test_run -d hello-world exe hello-world
117-
test -f hello-world/Hello/World/Basic.lean
133+
test_std hello-world Hello/World
118134

119135
test_run new hello.exe exe
120-
test_run -d hello-exe exe hello-exe
136+
test_exe hello-exe Hello
121137

122138
# Test creating packages with a `-` (i.e., a non-identifier package name)
123139
# https://leanprover.zulipchat.com/#narrow/stream/270676-lean4/topic/lake.20new.20lean-data
124140

125141
echo "# TEST: Non-identifier package names"
126142
test_run new lean-data
127-
test_run -d lean-data exe lean-data
143+
test_std lean-data LeanData
128144

129145
# Test creating packages starting with digits (i.e., a non-identifier library name)
130146
# https://github.com/leanprover/lean4/issues/2865
131147

132148
echo "# TEST: Non-identifier library names"
133149
test_run new 123-hello
134-
test_run -d 123-hello exe 123-hello
150+
test_std 123-hello 123Hello
135151

136152
# Test creating packages with components that contain `.`s
137153
# https://github.com/leanprover/lean4/issues/2999
@@ -140,23 +156,23 @@ test_run -d 123-hello exe 123-hello
140156
if [ "$OSTYPE" != "cygwin" -a "$OSTYPE" != "msys" ]; then
141157
echo "# TEST: Escaped names"
142158
test_run new «A.B».«C.D»
143-
test_run -d A-B-C-D exe a-b-c-d
159+
test_std A-B-C-D A.B/C.D a-b-c-d
144160
fi
145161

146162
# Test creating packages with keyword names
147163
# https://github.com/leanprover/lake/issues/128
148164

149165
echo "# TEST: Keyword names"
150166
test_run new meta
151-
test_run -d meta exe meta
167+
test_std meta Meta
152168

153169
# Test `init` with name
154170

155171
echo "# TEST: init <name>"
156172
mkdir hello_world
157173
pushd hello_world
158174
test_run init hello_world exe
159-
test_run exe hello_world
175+
test_exe . HelloWorld hello_world
160176
popd
161177

162178
# Test bare `init` on existing package (should error)

0 commit comments

Comments
 (0)