Skip to content

Commit 7efe468

Browse files
committed
fix: respect leanOptions in development mode
1 parent 3974b37 commit 7efe468

File tree

7 files changed

+74
-22
lines changed

7 files changed

+74
-22
lines changed

Projects/MathlibDemo/MathlibDemo.lean

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ import MathlibDemo.Bijection
66
import MathlibDemo.Logic
77
import MathlibDemo.Rational
88
import MathlibDemo.Ring
9+
import MathlibDemo.MathlibDemo

Projects/MathlibDemo/lake-manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"type": "git",
66
"subDir": null,
77
"scope": "",
8-
"rev": "d6bc0a15829bf478c4ab90b491dd5e7f000021b1",
8+
"rev": "81295dfbd8dc35fc1d1f399b7b61dd6cf6e05cb2",
99
"name": "mathlib",
1010
"manifestFile": "lake-manifest.json",
1111
"inputRev": "master",

Projects/README.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Adding Projects
22

3-
To add new projects, add any Lean project in the folder `Projects/`, e.g. `Projects/my-cool-project/`.
3+
To add new projects, add any Lean project in the folder `Projects/`, e.g. `Projects/MyCoolProject/`.
44
You can either build your Lean project manually or you include a script
5-
`Projects/my-cool-project/build.sh` for automatic builds.
5+
`Projects/MyCoolProject/build.sh` for automatic builds.
66
Usually a build script looks like this:
77

88
```
@@ -16,16 +16,16 @@ cd $(dirname $0)
1616
lake build
1717
```
1818

19-
A project added this way can then be accessed online with `https://your.url.com/#project=my-cool-project`.
19+
A project added this way can then be accessed online with `https://your.url.com/#project=MyCoolProject`.
2020
For the project to appear in the Settings, you need to update `client/config/config.json` by adding
21-
a new entry `{folder: "my-cool-project", name: "My Cool Project"}` to `projects`; here `folder` is the
21+
a new entry `{folder: "MyCoolProject", name: "My Cool Project"}` to `projects`; here `folder` is the
2222
folder name inside `Projects/` and `name` is the free-text display name.
2323

2424
If you want to add Examples, you should add them as valid Lean files to your project and then expand
2525
the config entry of your project in `config.json` as follows:
2626

2727
```
28-
{ "folder": "my-cool-project`",
28+
{ "folder": "MyCoolProject`",
2929
"name": "My Cool Project",
3030
"examples": [
3131
{ "file": "MyCustomProject/Demo1.lean",
@@ -35,6 +35,10 @@ the config entry of your project in `config.json` as follows:
3535
```
3636

3737
This will add an entry `My Cool Example` to the Example menu which loads
38-
the file from `Projects/my-cool-project/MyCustomProject/Demo1.lean`.
38+
the file from `Projects/MyCoolProject/MyCoolProject/Demo1.lean`.
3939

4040
You might want to look at the provided `MathlibDemo` project for comparison.
41+
42+
43+
**Important**: In order for lake to use any "leanOptions" specified in the projects lakefile, you must make sure there is a file `Projects/MyCoolProject/MyCoolProject.lean`
44+
where folder name and file name coincide.

client/src/App.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { LeanMonaco, LeanMonacoEditor, LeanMonacoOptions } from 'lean4monaco'
66
import LZString from 'lz-string'
77
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
88
import { faCode } from '@fortawesome/free-solid-svg-icons'
9+
import * as path from 'path'
910

1011
// Local imports
1112
import LeanLogo from './assets/logo.svg'
@@ -178,7 +179,7 @@ function App() {
178179
_leanMonaco.setInfoviewElement(infoviewRef.current!)
179180
;(async () => {
180181
await _leanMonaco.start(options)
181-
await leanMonacoEditor.start(editorRef.current!, `/project/${project}.lean`, code)
182+
await leanMonacoEditor.start(editorRef.current!, path.join(project, `${project}.lean`), code)
182183

183184
setEditor(leanMonacoEditor.editor)
184185
setLeanMonaco(_leanMonaco)

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

server/bubblewrap.sh

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,22 @@ ulimit -t 3600
77
LEAN_ROOT="$(cd $1 && lean --print-prefix)"
88
LEAN_PATH="$(cd $1 && lake env printenv LEAN_PATH)"
99

10+
PROJECT_NAME="$(basename $1)"
11+
1012
# # print commands as they are executed
1113
# set -x
1214

13-
if command -v bwrap >/dev/null 2>&1; then
15+
if ! command -v bwrap >/dev/null 2>&1; then
16+
echo "bwrap is not installed! You could try to run the development server instead."
17+
# # Could run without bubblewrap like this, but this might be an unwanted
18+
# # security risk.
19+
# (exec
20+
# cd $1
21+
# lake serve --
22+
# )
23+
else
1424
(exec bwrap\
15-
--ro-bind "$1" /project \
25+
--ro-bind "$1" "/$PROJECT_NAME" \
1626
--ro-bind "$LEAN_ROOT" /lean \
1727
--ro-bind /usr /usr \
1828
--dev /dev \
@@ -32,13 +42,7 @@ if command -v bwrap >/dev/null 2>&1; then
3242
--unshare-uts \
3343
--unshare-cgroup \
3444
--die-with-parent \
35-
--chdir "/project/" \
36-
lean --server
37-
)
38-
else
39-
echo "bwrap is not installed. Running without container." >&2
40-
(exec
41-
cd $1
45+
--chdir "/$PROJECT_NAME/" \
4246
lean --server
4347
)
4448
fi

server/index.mjs

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,18 @@ if (crtFile && keyFile) {
7474

7575
const wss = new WebSocketServer({ server })
7676

77+
// The path to the projects folder relative to the server
78+
let projectsBasePath = path.join(__dirname, '..', 'Projects')
79+
7780
function startServerProcess(project) {
78-
let projectPath = __dirname + `/../Projects/` + project
81+
let projectPath = path.join(projectsBasePath, project)
7982

8083
let serverProcess
8184
if (isDevelopment) {
8285
if (!isGithubAction) {
8386
console.warn("Running without Bubblewrap container!")
8487
}
85-
serverProcess = cp.spawn("lean", ["--server"], { cwd: projectPath })
88+
serverProcess = cp.spawn("lake", ["serve", "--"], { cwd: projectPath })
8689
} else {
8790
console.info("Running with Bubblewrap container.")
8891
serverProcess = cp.spawn("./bubblewrap.sh", [projectPath], { cwd: __dirname })
@@ -107,6 +110,40 @@ function startServerProcess(project) {
107110
return serverProcess
108111
}
109112

113+
/** Transform client URI to valid file on the server */
114+
function urisToFilenames(prefix, obj) {
115+
for (let key in obj) {
116+
if (obj.hasOwnProperty(key)) {
117+
if (key === 'uri') {
118+
obj[key] = obj[key].replace('file://', `file://${prefix}`)
119+
} else if (key === 'rootUri') {
120+
obj[key] = obj[key].replace('file://', `file://${prefix}`)
121+
} else if (key === 'rootPath') {
122+
obj[key] = path.join(prefix, obj[key])
123+
}
124+
if (typeof obj[key] === 'object' && obj[key] !== null) {
125+
urisToFilenames(prefix, obj[key]);
126+
}
127+
}
128+
}
129+
return obj;
130+
}
131+
132+
/** Transform server file back into client URI */
133+
function FilenamesToUri(prefix, obj) {
134+
for (let key in obj) {
135+
if (obj.hasOwnProperty(key)) {
136+
if (key === 'uri') {
137+
obj[key] = obj[key].replace(prefix, '')
138+
}
139+
if (typeof obj[key] === 'object' && obj[key] !== null) {
140+
FilenamesToUri(prefix, obj[key]);
141+
}
142+
}
143+
}
144+
return obj;
145+
}
146+
110147
wss.addListener("connection", function(ws, req) {
111148
const urlRegEx = /^\/websocket\/([\w.-]+)$/
112149
const reRes = urlRegEx.exec(req.url)
@@ -127,12 +164,17 @@ wss.addListener("connection", function(ws, req) {
127164
const socketConnection = jsonrpcserver.createConnection(reader, writer, () => ws.close())
128165
const serverConnection = jsonrpcserver.createProcessStreamConnection(ps)
129166
socketConnection.forward(serverConnection, message => {
167+
const prefix = isDevelopment ? projectsBasePath : "/"
168+
urisToFilenames(prefix, message)
169+
130170
if (isDevelopment && !isGithubAction) {
131171
console.log(`CLIENT: ${JSON.stringify(message)}`)
132172
}
133173
return message;
134174
})
135175
serverConnection.forward(socketConnection, message => {
176+
const prefix = isDevelopment ? projectsBasePath : "/"
177+
FilenamesToUri(prefix, message)
136178
if (isDevelopment && !isGithubAction) {
137179
console.log(`SERVER: ${JSON.stringify(message)}`)
138180
}

0 commit comments

Comments
 (0)