mirror of
https://github.com/l0ng-ai/openidea.git
synced 2026-11-16 05:21:06 -10:00
Compare commits
761
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f53f7c0208 | ||
|
|
3cc4c1c9b1 | ||
|
|
9c07e3e136 | ||
|
|
844611c7d5 | ||
|
|
c9c75fa3b7 | ||
|
|
32abc77a74 | ||
|
|
de107e145c | ||
|
|
9adf35b097 | ||
|
|
0e473c6dac | ||
|
|
b01f70390e | ||
|
|
efc1e607b8 | ||
|
|
f4469a2f53 | ||
|
|
708cca8e4f | ||
|
|
f5b4e5ad8c | ||
|
|
77984361b6 | ||
|
|
86ba75cbf4 | ||
|
|
d50348bc94 | ||
|
|
e8c6ff8f2f | ||
|
|
5150e8e81b | ||
|
|
88a2508bee | ||
|
|
61da1bc685 | ||
|
|
4f2fc3ecb1 | ||
|
|
5b0006fa42 | ||
|
|
d986b7408b | ||
|
|
6b2bb0bb6a | ||
|
|
2b353dfe53 | ||
|
|
ead2594d72 | ||
|
|
2cb573d5ad | ||
|
|
0360690c6a | ||
|
|
f179c970ba | ||
|
|
45d34ac638 | ||
|
|
7709897dbb | ||
|
|
da3b00518f | ||
|
|
87e4dcf42b | ||
|
|
c860649b13 | ||
|
|
07afe43d0d | ||
|
|
8ca2e089a3 | ||
|
|
41a8fbc591 | ||
|
|
3a0536eafc | ||
|
|
9cbdde526b | ||
|
|
2878eb8d87 | ||
|
|
2e764d74f8 | ||
|
|
92dd70f08b | ||
|
|
484bbfc704 | ||
|
|
0b097808f1 | ||
|
|
6cc4b3779c | ||
|
|
7d9efa1997 | ||
|
|
af2e45883d | ||
|
|
17e11b7904 | ||
|
|
22aa1edd61 | ||
|
|
07c862c785 | ||
|
|
b60dad7076 | ||
|
|
148e45745a | ||
|
|
3287061ea1 | ||
|
|
223beeb374 | ||
|
|
8b9ad2e9d1 | ||
|
|
a5306bade3 | ||
|
|
aa8822d9a3 | ||
|
|
5fca9c9ed0 | ||
|
|
83805734bf | ||
|
|
0b3d1d67d9 | ||
|
|
53a26b95a4 | ||
|
|
efa4fb3b43 | ||
|
|
6df0087038 | ||
|
|
bedd9d2fc9 | ||
|
|
696abb08d8 | ||
|
|
f99ae11c01 | ||
|
|
b0fbc5a76c | ||
|
|
273564687e | ||
|
|
5436b7202d | ||
|
|
9fdb067ec5 | ||
|
|
c8112f9010 | ||
|
|
0d376cae44 | ||
|
|
b5c86bc064 | ||
|
|
78e169f820 | ||
|
|
601f1ee10a | ||
|
|
ea65b8c074 | ||
|
|
10ddb4aa89 | ||
|
|
1259423fdc | ||
|
|
3fed53ced7 | ||
|
|
ad236cf1a1 | ||
|
|
3b06fc037d | ||
|
|
6d70458e22 | ||
|
|
bbd7c36c40 | ||
|
|
e586923308 | ||
|
|
2ddc849b88 | ||
|
|
41951e2895 | ||
|
|
feb261d172 | ||
|
|
3d4a1fd387 | ||
|
|
88e4b8f79b | ||
|
|
3c9ac70cdf | ||
|
|
cc72161f92 | ||
|
|
485638cba5 | ||
|
|
e968801415 | ||
|
|
7b6962001b | ||
|
|
8535fb7645 | ||
|
|
89eacbe640 | ||
|
|
c67b336a8a | ||
|
|
90cc707d00 | ||
|
|
779080781b | ||
|
|
ccf87e2476 | ||
|
|
76f0b3fc63 | ||
|
|
a5c14a4c21 | ||
|
|
cf444a31f7 | ||
|
|
c3763fb806 | ||
|
|
9a518e734b | ||
|
|
2913c12a19 | ||
|
|
3044518b88 | ||
|
|
778472854e | ||
|
|
1679d61a6a | ||
|
|
937008a6fe | ||
|
|
dc3d2050cb | ||
|
|
732baf8f1e | ||
|
|
9b6e5a5a62 | ||
|
|
8c2c701517 | ||
|
|
aeb689c632 | ||
|
|
80c25be389 | ||
|
|
acea7e6235 | ||
|
|
25197b3215 | ||
|
|
55786a7030 | ||
|
|
b7adbbb17c | ||
|
|
d1290c76ea | ||
|
|
0febd3e170 | ||
|
|
acee0d5edb | ||
|
|
a02a594010 | ||
|
|
6bb4a6375a | ||
|
|
f15c2c2b5e | ||
|
|
a83a1a54e5 | ||
|
|
0d499f3af6 | ||
|
|
e06c890ca4 | ||
|
|
6295cc2237 | ||
|
|
8349938481 | ||
|
|
cb2c4345d1 | ||
|
|
22f409ae76 | ||
|
|
b2dc59eb0f | ||
|
|
f5641f9c6f | ||
|
|
dcd4f150bd | ||
|
|
8caeb1305f | ||
|
|
7ec0a4fed2 | ||
|
|
733b6cff1d | ||
|
|
b0860ed1e2 | ||
|
|
48a13cb8fa | ||
|
|
c9488df786 | ||
|
|
4427ec2c07 | ||
|
|
cf71a61cdf | ||
|
|
f9af0e7bff | ||
|
|
6449b68bb0 | ||
|
|
5d2d814610 | ||
|
|
3f18a8a274 | ||
|
|
f8a8ecb662 | ||
|
|
e2b1a2d732 | ||
|
|
db5a306450 | ||
|
|
bad9429e79 | ||
|
|
c942374f8a | ||
|
|
611b27b974 | ||
|
|
71f948cc9f | ||
|
|
79a750362a | ||
|
|
f1c0d098ad | ||
|
|
d9c1a9ff22 | ||
|
|
e86011fc87 | ||
|
|
f80dd0aa49 | ||
|
|
832c397e0b | ||
|
|
0f91aac0d4 | ||
|
|
d4b2ade24d | ||
|
|
e5f642f1d1 | ||
|
|
b8a06863d9 | ||
|
|
b5d86b4532 | ||
|
|
d8814d18b0 | ||
|
|
7bd115bc97 | ||
|
|
74e66a5dae | ||
|
|
6c301a7b6b | ||
|
|
d171a946fe | ||
|
|
6ae167efc9 | ||
|
|
8432bf9997 | ||
|
|
59fba1a65d | ||
|
|
3bfd3134c0 | ||
|
|
573adbf9ab | ||
|
|
d2381096fb | ||
|
|
2c70c12bf2 | ||
|
|
3ef7fee3e2 | ||
|
|
deec6d05a0 | ||
|
|
4a98ce544b | ||
|
|
396a8d4b26 | ||
|
|
2889b301b8 | ||
|
|
e26e1d1d7d | ||
|
|
0af3bd00e7 | ||
|
|
951e81e065 | ||
|
|
023c0a6ee6 | ||
|
|
c5481e8fe4 | ||
|
|
dfacb45575 | ||
|
|
3363bb8a46 | ||
|
|
39f34ec22f | ||
|
|
34ae8acbf8 | ||
|
|
6bafcb2b5c | ||
|
|
a6ed65e6d1 | ||
|
|
009e61d27b | ||
|
|
a89c0dd229 | ||
|
|
ea644dcdb0 | ||
|
|
519c8ad8fc | ||
|
|
27fdbe71e5 | ||
|
|
d5cd5e7a55 | ||
|
|
c32a68c0b8 | ||
|
|
59cc84521f | ||
|
|
4faa37aaea | ||
|
|
aaf25d9f0d | ||
|
|
913ad6616c | ||
|
|
b80f0f9e39 | ||
|
|
460027fd4b | ||
|
|
33c5627bdb | ||
|
|
3a22a2d0f0 | ||
|
|
c169b8ab3c | ||
|
|
0dbce5d09e | ||
|
|
7fe6f5f58d | ||
|
|
abf904553d | ||
|
|
d425388f1a | ||
|
|
de123e2a21 | ||
|
|
d19dbec56a | ||
|
|
1a204b5f4b | ||
|
|
3ec7daf845 | ||
|
|
112ac05052 | ||
|
|
10b7340e2a | ||
|
|
6fa5b368e6 | ||
|
|
dd40273952 | ||
|
|
21bc81c6da | ||
|
|
1dc1e3cca1 | ||
|
|
4a82c22fdc | ||
|
|
010e7ba6b4 | ||
|
|
9984e40b83 | ||
|
|
36a6e6359d | ||
|
|
4f64326f89 | ||
|
|
b5c2d0dc5e | ||
|
|
c9dca15972 | ||
|
|
41b581eafd | ||
|
|
13e18af777 | ||
|
|
488e819b46 | ||
|
|
ad8df8fe00 | ||
|
|
19d6eab21a | ||
|
|
5c0befeff0 | ||
|
|
07bd8ded5e | ||
|
|
4ab85d5f49 | ||
|
|
3c91adfae2 | ||
|
|
5cfa6614a1 | ||
|
|
e875d97f7c | ||
|
|
31016ee684 | ||
|
|
6a93f1cce6 | ||
|
|
2b4e3c79f8 | ||
|
|
4e90d9fd5c | ||
|
|
ffc2f75d3b | ||
|
|
19587af8b1 | ||
|
|
b2c22ccec6 | ||
|
|
f795aa3232 | ||
|
|
9239b99f3a | ||
|
|
c5be2998ec | ||
|
|
b83df2dbda | ||
|
|
9f7a35f170 | ||
|
|
52b3e14a8c | ||
|
|
934e4ca0e7 | ||
|
|
a2809bda80 | ||
|
|
0fd8389b33 | ||
|
|
19c0ae07f2 | ||
|
|
5a5b53d86b | ||
|
|
ac7a81db1c | ||
|
|
9681e1a118 | ||
|
|
293972baaa | ||
|
|
b173a53004 | ||
|
|
f6e54f581b | ||
|
|
276a30849c | ||
|
|
63abedaadd | ||
|
|
c22a7fa112 | ||
|
|
a46f187428 | ||
|
|
ff826279f2 | ||
|
|
759fb3278d | ||
|
|
f7d19e1711 | ||
|
|
97d202ade0 | ||
|
|
f0f15366e4 | ||
|
|
fecbf8ed14 | ||
|
|
2d00f09393 | ||
|
|
f154bf8c83 | ||
|
|
d3720c0e11 | ||
|
|
ec862b3668 | ||
|
|
735830d642 | ||
|
|
65c0413dbb | ||
|
|
14bb7bcc6f | ||
|
|
8113d101ef | ||
|
|
5f7e05adc4 | ||
|
|
f2b85eca4c | ||
|
|
e41cf32a94 | ||
|
|
0cd19a8c0f | ||
|
|
fb76a6dab7 | ||
|
|
f4502ffffe | ||
|
|
2430c892ba | ||
|
|
8876d04cb8 | ||
|
|
eeb20f467c | ||
|
|
4ccff88ccf | ||
|
|
fdf29989ec | ||
|
|
3c3b6a6fd1 | ||
|
|
49bb6b2d76 | ||
|
|
632e564d58 | ||
|
|
08333bcff8 | ||
|
|
ae542b3044 | ||
|
|
4cb98ff162 | ||
|
|
cc8ccea888 | ||
|
|
75344c21ef | ||
|
|
91ac7f417c | ||
|
|
2fd43aa29f | ||
|
|
5b7d258adb | ||
|
|
fd1cebdf98 | ||
|
|
c9362247d0 | ||
|
|
8ab9c688ac | ||
|
|
ba0b3d9c7d | ||
|
|
96f8ee27dd | ||
|
|
5529db667c | ||
|
|
9c55c28d5b | ||
|
|
009773c0d4 | ||
|
|
ba0f4329ea | ||
|
|
4d88d44616 | ||
|
|
5e5d804b89 | ||
|
|
83b9744a36 | ||
|
|
4ab8959bac | ||
|
|
d715643ff2 | ||
|
|
a5214cae38 | ||
|
|
54dd3a167f | ||
|
|
0938a507da | ||
|
|
a05a40e0a3 | ||
|
|
3b978721e3 | ||
|
|
9793857024 | ||
|
|
8fb3b7b084 | ||
|
|
22fe1702be | ||
|
|
ad434af78c | ||
|
|
6d103df5cf | ||
|
|
e2d089a0c4 | ||
|
|
47fa0e6f79 | ||
|
|
921f1f2b2d | ||
|
|
88f73630a6 | ||
|
|
504ffac10a | ||
|
|
5ee558d1a7 | ||
|
|
c09fb7191a | ||
|
|
7ee80ed736 | ||
|
|
353768a15f | ||
|
|
5e969f0bad | ||
|
|
6f60f55a1d | ||
|
|
8e5ac1dc01 | ||
|
|
4fcf4555a1 | ||
|
|
40494f2920 | ||
|
|
c016007a2d | ||
|
|
69ce1ab13d | ||
|
|
fcc8583f36 | ||
|
|
b368f21eb6 | ||
|
|
929866bbc5 | ||
|
|
3c781296d6 | ||
|
|
4aed449dc0 | ||
|
|
55431f3965 | ||
|
|
d4b14a36c7 | ||
|
|
d2c565b19d | ||
|
|
01a60ef164 | ||
|
|
b0b768d25e | ||
|
|
3240bfc423 | ||
|
|
15182507cf | ||
|
|
38bd8fbd16 | ||
|
|
2859ec9106 | ||
|
|
a4b62ad27f | ||
|
|
1392e824bb | ||
|
|
6073f35eb6 | ||
|
|
b02964962b | ||
|
|
9ca8097f3c | ||
|
|
1b08d5de09 | ||
|
|
8faedce175 | ||
|
|
c8bdfa8edd | ||
|
|
4d22632609 | ||
|
|
7f61b7b8ab | ||
|
|
abe30914b5 | ||
|
|
1ae3edd5c8 | ||
|
|
52b8cf5e70 | ||
|
|
258ba189c6 | ||
|
|
f3d0417f4c | ||
|
|
291d1324c5 | ||
|
|
a6e137a6ae | ||
|
|
89f00a29f9 | ||
|
|
834f9e172c | ||
|
|
45badba915 | ||
|
|
71728a473a | ||
|
|
2acd5cf479 | ||
|
|
85f4ffb67c | ||
|
|
4348dab26e | ||
|
|
38d4dd8c16 | ||
|
|
58af3c55a5 | ||
|
|
a8643366c8 | ||
|
|
22685815cc | ||
|
|
e6b88b2701 | ||
|
|
c076dedefc | ||
|
|
3764d963d7 | ||
|
|
4d2bcbbc26 | ||
|
|
56d9c8908c | ||
|
|
0bd0c4e03b | ||
|
|
209fc796ce | ||
|
|
e7e89e46c2 | ||
|
|
c87ab87417 | ||
|
|
a9ad1f1bbb | ||
|
|
4c8caca6a5 | ||
|
|
2ec2e75d40 | ||
|
|
04d5d9bd58 | ||
|
|
9a415e6875 | ||
|
|
6f83f8cad0 | ||
|
|
b971c8b391 | ||
|
|
c333990404 | ||
|
|
70b953c77a | ||
|
|
cb18b88531 | ||
|
|
401d7a4105 | ||
|
|
46bd1f6fb8 | ||
|
|
85f755591b | ||
|
|
06b7513210 | ||
|
|
09aa03bb86 | ||
|
|
317988cfa7 | ||
|
|
7db2e98269 | ||
|
|
fdc8bc2d4c | ||
|
|
497d4766d7 | ||
|
|
7e11d0729b | ||
|
|
2e78c42fac | ||
|
|
1d0449d430 | ||
|
|
73f7fa0016 | ||
|
|
f3dfb384c9 | ||
|
|
b53cfe8cc2 | ||
|
|
7d146ef1b7 | ||
|
|
4bde38ef9d | ||
|
|
c6212293e1 | ||
|
|
b8ba1256d2 | ||
|
|
6fc6bf598c | ||
|
|
95235220b5 | ||
|
|
620fa247ef | ||
|
|
52e5a0c4af | ||
|
|
4acb265b90 | ||
|
|
b284c37588 | ||
|
|
771575057e | ||
|
|
563d2a75d4 | ||
|
|
3a64bba254 | ||
|
|
f4b4a0fa72 | ||
|
|
50bd134b65 | ||
|
|
8840a1e58a | ||
|
|
bf36cc0801 | ||
|
|
78c98d289f | ||
|
|
30b0fcb8fc | ||
|
|
586399deda | ||
|
|
e6e89860e6 | ||
|
|
d890c41352 | ||
|
|
c942b87abb | ||
|
|
cdecb5916c | ||
|
|
835c44615b | ||
|
|
df79c80aa8 | ||
|
|
73df48db7c | ||
|
|
6143011880 | ||
|
|
d1872c07ce | ||
|
|
cd79293fd7 | ||
|
|
86f7b747a5 | ||
|
|
2e5e9acd9f | ||
|
|
b5e9e27dad | ||
|
|
ff1bfeea9d | ||
|
|
14ee1a97f2 | ||
|
|
916c4243bf | ||
|
|
9c63f174e4 | ||
|
|
b463252bc5 | ||
|
|
2abdac4fdb | ||
|
|
721f4144c1 | ||
|
|
55057b8958 | ||
|
|
8abc8e7fbf | ||
|
|
68dd949df3 | ||
|
|
b98bddbbab | ||
|
|
21b05ab73a | ||
|
|
7d7de0b966 | ||
|
|
830f4aa802 | ||
|
|
c905f1428e | ||
|
|
a8036ce6fa | ||
|
|
28829ad756 | ||
|
|
d6c562049f | ||
|
|
100075166c | ||
|
|
d5332a2197 | ||
|
|
b0d20d7997 | ||
|
|
8df59cf5a7 | ||
|
|
3c68d2ea7f | ||
|
|
cfe1416dbd | ||
|
|
44fc0a2ebc | ||
|
|
e52cb2e426 | ||
|
|
519e90da9f | ||
|
|
9e73877f5e | ||
|
|
94a8761cc9 | ||
|
|
e66513668a | ||
|
|
565bdf170f | ||
|
|
5712a5373d | ||
|
|
2a981dd642 | ||
|
|
3580cbacd7 | ||
|
|
f91b87d790 | ||
|
|
74abf32704 | ||
|
|
4b1370742f | ||
|
|
6700f57f79 | ||
|
|
578219de3b | ||
|
|
afcdf57eb6 | ||
|
|
f145ea5d7b | ||
|
|
0c00f474e7 | ||
|
|
5cf01f682f | ||
|
|
fec4eb34b9 | ||
|
|
6454dbe169 | ||
|
|
8bdf623a28 | ||
|
|
bff6a12e2f | ||
|
|
ee1f15eba7 | ||
|
|
7ea4f36b6d | ||
|
|
ddfdbf1be6 | ||
|
|
da412f14be | ||
|
|
104986ead4 | ||
|
|
fdeaa31b7d | ||
|
|
4d5c640451 | ||
|
|
3e40295668 | ||
|
|
b43db63ff3 | ||
|
|
4dbb6920c8 | ||
|
|
10603b953b | ||
|
|
59a0a86257 | ||
|
|
f05066b18f | ||
|
|
4c15fe5a23 | ||
|
|
8f6a4b5b39 | ||
|
|
4f9dc0a56c | ||
|
|
67afcc9805 | ||
|
|
632eb9cbf7 | ||
|
|
88af3cb1eb | ||
|
|
59eb6446d4 | ||
|
|
51c14d3b03 | ||
|
|
c727c5b842 | ||
|
|
b296ac1ab7 | ||
|
|
8cce7ce390 | ||
|
|
1e41fa86ff | ||
|
|
65e0feb98f | ||
|
|
76faa39bc5 | ||
|
|
7f9bf3727f | ||
|
|
405343b7b7 | ||
|
|
cc29842e6b | ||
|
|
de0f738bb2 | ||
|
|
d781df542a | ||
|
|
abfc4cbade | ||
|
|
646e5f8364 | ||
|
|
a3b48b78fd | ||
|
|
5e4fa861bb | ||
|
|
068ce8e721 | ||
|
|
e5cc1ed712 | ||
|
|
abe6285629 | ||
|
|
66a829c24c | ||
|
|
04f3f9434f | ||
|
|
4330034403 | ||
|
|
de8afe31d2 | ||
|
|
579e5eee9b | ||
|
|
099ede3478 | ||
|
|
fb269afdfa | ||
|
|
435b00d893 | ||
|
|
14fe542617 | ||
|
|
513762147a | ||
|
|
489f55f52c | ||
|
|
55fcbf281a | ||
|
|
f8aa003b47 | ||
|
|
e27b7f2502 | ||
|
|
bc00302ef8 | ||
|
|
2e044c4a2b | ||
|
|
b1959e8fb5 | ||
|
|
ce67c7e129 | ||
|
|
3cc94e340b | ||
|
|
51793e02c5 | ||
|
|
1e0ce64b0c | ||
|
|
6e7b39023c | ||
|
|
c5d319c298 | ||
|
|
0780171704 | ||
|
|
0324a81ce4 | ||
|
|
7c51662e9e | ||
|
|
6b91fb396a | ||
|
|
4792647d06 | ||
|
|
0262812d9b | ||
|
|
33957ed34f | ||
|
|
de9c8db74e | ||
|
|
e1f41093ec | ||
|
|
53d5d66520 | ||
|
|
0eb68afd40 | ||
|
|
de2a631c17 | ||
|
|
59755adec4 | ||
|
|
5c7de8b6d7 | ||
|
|
ab41d9aa71 | ||
|
|
4ecd04af79 | ||
|
|
d09156ca8a | ||
|
|
1946dbd87c | ||
|
|
10d53e9956 | ||
|
|
780052d204 | ||
|
|
80a693c1bf | ||
|
|
7097fb46dd | ||
|
|
39d798e8c1 | ||
|
|
30a0b1d9fd | ||
|
|
41f54ceba9 | ||
|
|
d90b2b7bbe | ||
|
|
b2d506a929 | ||
|
|
81e3717d66 | ||
|
|
018b12f24f | ||
|
|
76b5d63c95 | ||
|
|
6fc6042a78 | ||
|
|
d01dc30f4f | ||
|
|
b8bad81497 | ||
|
|
63bb5d4976 | ||
|
|
49a2e084a8 | ||
|
|
42a65aba23 | ||
|
|
a812a03405 | ||
|
|
17204bbdca | ||
|
|
feef7db39d | ||
|
|
c54253fba2 | ||
|
|
ac54a064ef | ||
|
|
04dd202fab | ||
|
|
43b5c69ecd | ||
|
|
597d94bb42 | ||
|
|
ffbd80e857 | ||
|
|
f4ff2feefa | ||
|
|
a7af19d6b9 | ||
|
|
a2dd7e3939 | ||
|
|
3ed5157e03 | ||
|
|
68a9bf37e3 | ||
|
|
96711ff85a | ||
|
|
29bbaf3b78 | ||
|
|
b30657ec50 | ||
|
|
fd5f4b8639 | ||
|
|
0567bb88bf | ||
|
|
a7e968e869 | ||
|
|
e9a464cfef | ||
|
|
c23696c51f | ||
|
|
3e6e9ed255 | ||
|
|
cbc6b24c5f | ||
|
|
bb4f9ed180 | ||
|
|
f82808a82f | ||
|
|
1899483751 | ||
|
|
6d9bd6b43b | ||
|
|
2d4b25fc34 | ||
|
|
4c5df98bb8 | ||
|
|
46d288b456 | ||
|
|
7c5ef7a97b | ||
|
|
ae3973f11a | ||
|
|
e5efcfbf60 | ||
|
|
6f62917730 | ||
|
|
1b0fe7dcc6 | ||
|
|
7d1b54fae3 | ||
|
|
cb010fac2f | ||
|
|
684f52b3dd | ||
|
|
e8bc577e9d | ||
|
|
b1348ee03a | ||
|
|
e99d05d7a8 | ||
|
|
0e4cf1059a | ||
|
|
b4bcd8cce7 | ||
|
|
cb5e7081fa | ||
|
|
034b2b9f65 | ||
|
|
7ea3c59d05 | ||
|
|
6717b0948a | ||
|
|
8302cd1d6f | ||
|
|
c05670f4fd | ||
|
|
2f9658c367 | ||
|
|
40aa7e4899 | ||
|
|
fba1f02375 | ||
|
|
053ca43368 | ||
|
|
8fedc2efba | ||
|
|
2f2fa79cbe | ||
|
|
65d49619dd | ||
|
|
7f28161869 | ||
|
|
fc1493e1a4 | ||
|
|
efda6834ad | ||
|
|
e92e867019 | ||
|
|
8d8c7a481b | ||
|
|
0b3ca2fc9a | ||
|
|
2414773d61 | ||
|
|
e4181c36ea | ||
|
|
5063f6756e | ||
|
|
1a2f5308a0 | ||
|
|
cd04593d04 | ||
|
|
38241e7fae | ||
|
|
2e6d6a05b9 | ||
|
|
c73d858895 | ||
|
|
6e5486586c | ||
|
|
93642b3741 | ||
|
|
b06f2e27ea | ||
|
|
86f69b7fca | ||
|
|
fefa135f5a | ||
|
|
8fe2d35d62 | ||
|
|
21046c3047 | ||
|
|
017561a20b | ||
|
|
d3a68b2754 | ||
|
|
e98a560523 | ||
|
|
6983e8ec2f | ||
|
|
dc355602a9 | ||
|
|
924fa9259d | ||
|
|
24deeb0dbb | ||
|
|
0ce7f6e430 | ||
|
|
84fd0d0a60 | ||
|
|
96c99e0396 | ||
|
|
fd4838fc36 | ||
|
|
4873905884 | ||
|
|
6b37788971 | ||
|
|
44bf648bee | ||
|
|
a0c3249368 | ||
|
|
757412cb73 | ||
|
|
a497178b3e | ||
|
|
8b179992e8 | ||
|
|
85541b3663 | ||
|
|
0418a2ce01 | ||
|
|
c8639b38ba | ||
|
|
f04658612f | ||
|
|
d5cb379cae | ||
|
|
e3a818b607 | ||
|
|
0b1872937a | ||
|
|
f11cd361a7 | ||
|
|
7034fe1640 | ||
|
|
fc264d3f5c | ||
|
|
8e90341b3a | ||
|
|
3644956755 | ||
|
|
90e7e3162e | ||
|
|
833ae4c3c4 | ||
|
|
584baa3f71 | ||
|
|
98010e6513 | ||
|
|
a3640f655f | ||
|
|
8aaa7daf83 | ||
|
|
6a9a970df4 | ||
|
|
a309b7e6fa | ||
|
|
6971936f78 | ||
|
|
9d781a2f83 | ||
|
|
3260e5e224 | ||
|
|
ed575a00d9 | ||
|
|
2b41e61ce9 | ||
|
|
908740b3f2 | ||
|
|
e702e4da7b | ||
|
|
91023de207 | ||
|
|
02ee2880fe | ||
|
|
5a316e81d6 | ||
|
|
7c2ca4d061 | ||
|
|
8d09479a65 | ||
|
|
8fafde10b4 | ||
|
|
b1cd917a96 | ||
|
|
605d5f4985 | ||
|
|
7d52f058a7 | ||
|
|
c44558b4aa | ||
|
|
8455761181 | ||
|
|
dabf9a954e | ||
|
|
a3d1e57f3e | ||
|
|
0ff3f188de | ||
|
|
1654d22756 | ||
|
|
14d9cdff11 | ||
|
|
f7a507a5db | ||
|
|
c67e07a4f9 | ||
|
|
4e08893eae | ||
|
|
779070c3d7 | ||
|
|
4e86161c3c | ||
|
|
d7393b53f3 | ||
|
|
276a91973c | ||
|
|
fc75b15519 | ||
|
|
8798c2a542 | ||
|
|
8ea2787490 | ||
|
|
dc4e8403de | ||
|
|
4a0efbae25 | ||
|
|
cb3c0bafd7 | ||
|
|
71072dbe4b | ||
|
|
18b286e7e0 | ||
|
|
9474af4de1 | ||
|
|
8044d99ea0 | ||
|
|
0abaf6c777 | ||
|
|
0d9b08ac8c | ||
|
|
e54580265d | ||
|
|
4fa700b337 | ||
|
|
173d5edcdc |
No files matched your search
@@ -0,0 +1,83 @@
|
||||
---
|
||||
name: eval
|
||||
description: Run the code-intelligence evals (`pnpm eval`), pick the worst failing scenario, diagnose it from the attached timeline, fix it, prove the fix with before / after numbers measured on this machine without regressing other scenarios, and open one PR. Use for "跑 eval", "优化 Java 代码智能", "eval 失败了", an issue labelled `eval`, or any change to Java navigation / usages / gutter markers that needs numbers.
|
||||
---
|
||||
|
||||
# Eval loop: measure → fix → prove
|
||||
|
||||
`pnpm eval` drives the production workbench in Chromium against a release `openidea-server`
|
||||
with real language servers (Java / jdtls for now) and judges every scenario on what the user
|
||||
sees: time to feedback, to the first result, to the final one, and the results themselves vs
|
||||
`evals/<language>/golden/`. One machine, before and after: numbers from different machines or
|
||||
from the nightly host don't judge a fix.
|
||||
|
||||
## 0. Preconditions
|
||||
|
||||
- `jdtls` on PATH and `JAVA_HOME` pointing at a JDK of the pinned `JAVA` version (projects
|
||||
compile against it, and the JDK scenarios' results are in its sources; jdtls itself runs on
|
||||
a newer installed JDK when it needs one). Pinned versions: `.github/tool-versions.env`. A
|
||||
mismatch prints a warning and the report records both: say so in the PR when it applies.
|
||||
- First run builds `cargo build --release -p ide-host --bin openidea-server` and the perf
|
||||
bundle; later iterations of a frontend-only change can use `--no-build` after a
|
||||
`pnpm exec vite build --mode perf --outDir target/eval/dist`, a backend-only one after the
|
||||
cargo build alone.
|
||||
- Run evals one at a time on this machine, with nothing heavy (builds, other evals) beside them.
|
||||
|
||||
## 1. Baseline on the base commit
|
||||
|
||||
```sh
|
||||
git switch --detach origin/main # or the fix branch's merge base
|
||||
pnpm eval --save-baseline # target/eval/baseline.json, this machine only
|
||||
```
|
||||
|
||||
Read `target/eval/report.json`. Each verdict has `status`, `failures` (`kind`: `error`,
|
||||
`timeout`, `degraded`, `correctness`, `budget`, `golden`), `regressions`, `repro` (the command
|
||||
that reruns it) and, for failures, `diagnostics`: every command sent to the server in the
|
||||
slowest run's window (`cmd`, `start`, `ms`, `ok`, `bytes`, `items`), background tasks, server
|
||||
status changes and the server's log tail.
|
||||
|
||||
## 2. Pick one problem
|
||||
|
||||
Worst first: wrong or missing results and silent degradation, then timeouts, then the
|
||||
feedback budget (100 ms), then the slowest `completeMs`. One problem per PR. Say which
|
||||
scenario and metric, with its value vs budget.
|
||||
|
||||
## 3. Diagnose from the timeline
|
||||
|
||||
- Commands in the window ordered by `start`: what ran, what waited on what, what's serial
|
||||
that could overlap, which request returned many `items`/`bytes`, which failed.
|
||||
- `feedbackMs` far above 16 ms with a fast `firstResultMs`: the UI shows nothing while it
|
||||
waits (a hint or popup that only appears after a delay).
|
||||
- A task still running across the window: background work competing with the request.
|
||||
- For the frontend side rerun with `--trace` and open `target/eval/traces/<id>@<cond>.json`
|
||||
in Chrome's Performance panel.
|
||||
- Name the cause at class level (as in `issue-fix`): it usually affects sibling scenarios.
|
||||
|
||||
## 4. Fix, then rerun that scenario until it passes
|
||||
|
||||
```sh
|
||||
pnpm eval --only <scenario> --condition <warm|cold> # the verdict's `repro`
|
||||
```
|
||||
|
||||
Correctness is enforced, so a faster answer that drops, truncates or serves stale results
|
||||
fails. Don't trade a budget miss for a degraded answer.
|
||||
|
||||
## 5. Full suite, then PR
|
||||
|
||||
```sh
|
||||
pnpm eval # vs the baseline from step 1: no new failures or regressions
|
||||
```
|
||||
|
||||
PR per `issue-fix` §6, plus in **How it was verified** the before / after table from
|
||||
`target/eval/report.md` (both on this machine, commits named) and the tool versions line.
|
||||
|
||||
## Guard rails
|
||||
|
||||
- Goldens (`evals/*/golden/`) and budgets (`evals/budgets.json`) are human-owned. Don't edit
|
||||
them, and don't run `--update-golden`, to make a scenario pass. When a golden looks wrong
|
||||
(a tool bump, a deliberate behaviour change), stop and say so: a PR that touches them gets
|
||||
`needs-decision`.
|
||||
- Don't weaken a scenario's checks or timeouts in `evals/<language>/scenarios.ts` to pass.
|
||||
Adding a scenario (for a case the sweep or a user found) is fine; record its golden with
|
||||
`--update-golden --only <id>` and ask a person to review the file.
|
||||
- A flaky timing (passes on rerun) is not a regression: rerun once before acting on one.
|
||||
@@ -0,0 +1,7 @@
|
||||
# Link debug builds for the oldest system we support, so Launch Services
|
||||
# accepts the bundle on macOS 11. Without this a newer SDK leaves
|
||||
# LC_BUILD_VERSION.minos at 12.0 and the app is refused (-10825) before main
|
||||
# can load a newer WebKit. An explicitly exported MACOSX_DEPLOYMENT_TARGET
|
||||
# still wins: this is only the default for local builds and CI.
|
||||
[env]
|
||||
MACOSX_DEPLOYMENT_TARGET = "11.0"
|
||||
Symlink
+1
@@ -0,0 +1 @@
|
||||
../../.agents/skills/eval
|
||||
@@ -0,0 +1,2 @@
|
||||
custom:
|
||||
- https://github.com/l0ng-ai/openidea#sponsor
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 70 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 97 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 160 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 86 KiB |
@@ -0,0 +1,14 @@
|
||||
# Language server and tool versions for Language smoke (.github/workflows/language-smoke.yml)
|
||||
# and the code-intelligence evals (pnpm eval). Pinned so a release can't fail an unrelated
|
||||
# PR or move a golden result; bump them deliberately (and re-record goldens: pnpm eval --update-golden).
|
||||
VTSLS=0.3.0
|
||||
TYPESCRIPT=6.0.3
|
||||
BASEDPYRIGHT=1.40.2
|
||||
RUFF=0.17.3
|
||||
GOPLS=v0.24.0
|
||||
JDTLS=1.63.0-202610191322
|
||||
KOTLIN_LSP=271.2085.0
|
||||
GRADLE=8.14.3
|
||||
INTELEPHENSE=1.18.5
|
||||
# The JDK projects compile against (JAVA_HOME): library navigation opens its sources.
|
||||
JAVA=21
|
||||
@@ -98,10 +98,10 @@ jobs:
|
||||
# Test fixtures write LF files; keep Git from converting them on checkout.
|
||||
- run: git config --global core.autocrlf false
|
||||
- run: cargo nextest run --workspace --no-fail-fast
|
||||
# nextest ran export_bindings, which rewrites src/bindings.ts.
|
||||
- name: Bindings are up to date
|
||||
# nextest ran export_bindings, which rewrites src/bindings.ts and the command snapshot.
|
||||
- name: Bindings and command snapshot are up to date
|
||||
if: runner.os == 'Linux'
|
||||
run: git diff --exit-code src/bindings.ts
|
||||
run: git diff --exit-code src/bindings.ts src-tauri/tests/commands.snap
|
||||
|
||||
# The remote development server ships as a static Linux binary (release-build.yml):
|
||||
# keep that build working.
|
||||
@@ -208,11 +208,15 @@ jobs:
|
||||
- run: pnpm build
|
||||
|
||||
e2e:
|
||||
name: E2E
|
||||
name: E2E (${{ matrix.shard }}/2)
|
||||
needs: changes
|
||||
if: needs.changes.outputs.frontend == 'true'
|
||||
timeout-minutes: 30
|
||||
runs-on: ubuntu-24.04
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
shard: [1, 2]
|
||||
# Browsers and their system libraries are preinstalled; keep the tag in step with @playwright/test.
|
||||
container: mcr.microsoft.com/playwright:v1.64.1-noble
|
||||
steps:
|
||||
@@ -222,12 +226,12 @@ jobs:
|
||||
- uses: actions/setup-node@v5
|
||||
with: { node-version: 22, cache: pnpm }
|
||||
- run: pnpm install --frozen-lockfile
|
||||
- run: pnpm e2e
|
||||
- run: pnpm e2e --shard=${{ matrix.shard }}/2
|
||||
env:
|
||||
HOME: /root
|
||||
- uses: actions/upload-artifact@v6
|
||||
if: failure()
|
||||
with:
|
||||
name: playwright-report
|
||||
name: playwright-report-${{ matrix.shard }}
|
||||
path: playwright-report/
|
||||
retention-days: 7
|
||||
@@ -24,6 +24,7 @@ on:
|
||||
- "package.json"
|
||||
- "pnpm-lock.yaml"
|
||||
- ".github/workflows/language-smoke.yml"
|
||||
- ".github/tool-versions.env"
|
||||
pull_request:
|
||||
branches: [main]
|
||||
types: [opened, synchronize, reopened]
|
||||
@@ -38,16 +39,6 @@ concurrency:
|
||||
env:
|
||||
CARGO_TERM_COLOR: always
|
||||
CARGO_INCREMENTAL: 0
|
||||
# Pinned so a server release can't fail an unrelated PR; bump them deliberately.
|
||||
VTSLS: 0.3.0
|
||||
TYPESCRIPT: 6.0.3
|
||||
BASEDPYRIGHT: 1.40.2
|
||||
RUFF: 0.17.3
|
||||
GOPLS: v0.24.0
|
||||
JDTLS: 1.63.0-202610191322
|
||||
KOTLIN_LSP: 263.7084.0
|
||||
GRADLE: 8.14.3
|
||||
INTELEPHENSE: 1.18.5
|
||||
|
||||
jobs:
|
||||
smoke:
|
||||
@@ -61,6 +52,8 @@ jobs:
|
||||
timeout-minutes: 40
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
# Tool versions, shared with the evals (pnpm eval).
|
||||
- run: grep -E '^[A-Z0-9_]+=' .github/tool-versions.env >> "$GITHUB_ENV"
|
||||
- uses: dtolnay/rust-toolchain@stable
|
||||
with: { components: "rust-analyzer, rustfmt" }
|
||||
# One build cache for every language; main saves it.
|
||||
@@ -96,7 +89,7 @@ jobs:
|
||||
|
||||
- if: matrix.language == 'java' || matrix.language == 'kotlin'
|
||||
uses: actions/setup-java@v4
|
||||
with: { distribution: temurin, java-version: "21" }
|
||||
with: { distribution: temurin, java-version: "${{ env.JAVA }}" }
|
||||
- if: matrix.language == 'java'
|
||||
run: |
|
||||
mkdir -p "$RUNNER_TEMP/jdtls"
|
||||
@@ -126,7 +119,7 @@ jobs:
|
||||
- run: cargo test --locked -p ide-host --test language_smoke --no-run
|
||||
- run: cargo test --locked -p ide-host --test language_smoke -- --ignored --exact ${{ matrix.language }}
|
||||
- if: matrix.language == 'java'
|
||||
run: cargo test --locked -p ide-host --test project_smoke -- --ignored --exact maven_sync jdk
|
||||
run: cargo test --locked -p ide-host --test project_smoke -- --ignored --exact maven_sync jdk maven_launch_classpath_uses_the_runner_jdk_classifier
|
||||
- if: matrix.language == 'kotlin'
|
||||
run: cargo test --locked -p ide-host --test project_smoke -- --ignored --exact gradle_sync
|
||||
- if: matrix.language == 'python'
|
||||
|
||||
@@ -47,7 +47,7 @@ jobs:
|
||||
exit 0
|
||||
fi
|
||||
# No `nightly` tag yet (the first run): compare fails and the build goes on.
|
||||
status="$(gh api "repos/$GH_REPO/compare/nightly...$sha" --jq .status 2>/dev/null || echo ahead)"
|
||||
status="$(gh api "repos/$GH_REPO/compare/nightly...$sha" --jq .status 2>/dev/null)" || status=ahead
|
||||
if [ "$status" != ahead ] && [ "$FORCE" != true ]; then
|
||||
echo "::notice::$sha is not newer than the last nightly"
|
||||
exit 0
|
||||
|
||||
@@ -78,7 +78,7 @@ jobs:
|
||||
- uses: dtolnay/rust-toolchain@stable
|
||||
with: { targets: "${{ matrix.target }}" }
|
||||
- uses: Swatinem/rust-cache@v2
|
||||
with: { key: "${{ matrix.target }}" }
|
||||
with: { key: "${{ matrix.target }}", cache-on-failure: true }
|
||||
- name: Linux deps
|
||||
if: runner.os == 'Linux'
|
||||
run: |
|
||||
@@ -151,6 +151,13 @@ jobs:
|
||||
$hash = (Get-FileHash $asset -Algorithm SHA256).Hash.ToLower()
|
||||
"$hash $asset" | Out-File -Encoding ascii "$asset.sha256"
|
||||
gh release upload $env:TAG $asset "$asset.sha256" --clobber
|
||||
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
|
||||
if ($env:TAURI_SIGNING_PRIVATE_KEY) {
|
||||
pnpm exec tauri signer sign --app-version $version $asset
|
||||
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
|
||||
gh release upload $env:TAG "$asset.sig" --clobber
|
||||
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
|
||||
}
|
||||
|
||||
# The remote development server, per host platform it can run on. The desktop
|
||||
# app downloads the one for the host (ide-tools `server`) and installs it over SSH.
|
||||
@@ -174,7 +181,7 @@ jobs:
|
||||
- uses: dtolnay/rust-toolchain@stable
|
||||
with: { targets: "${{ matrix.target }}" }
|
||||
- uses: Swatinem/rust-cache@v2
|
||||
with: { key: "server-${{ matrix.target }}" }
|
||||
with: { key: "server-${{ matrix.target }}", cache-on-failure: true }
|
||||
- name: musl toolchain
|
||||
if: runner.os == 'Linux'
|
||||
run: sudo apt-get update && sudo apt-get install -y musl-tools
|
||||
@@ -233,6 +240,8 @@ jobs:
|
||||
[/_x64\.app\.tar\.gz$/, ["darwin-x86_64", "darwin-x86_64-app"]],
|
||||
...windows("aarch64", /_arm64_en-US\.msi$/, /_arm64-setup\.exe$/),
|
||||
...windows("x86_64", /_x64_en-US\.msi$/, /_x64-setup\.exe$/),
|
||||
[/_windows_arm64_portable\.zip$/, ["windows-aarch64-portable"]],
|
||||
[/_windows_x64_portable\.zip$/, ["windows-x86_64-portable"]],
|
||||
];
|
||||
const platforms = {};
|
||||
for (const sig of sigs) {
|
||||
@@ -251,7 +260,48 @@ jobs:
|
||||
core.setFailed(`latest.json would miss ${missing.join(", ")}`);
|
||||
return;
|
||||
}
|
||||
const manifest = { version: tag.replace(/^v/, ""), notes: "", pub_date: new Date().toISOString(), platforms };
|
||||
// The update's notes: the release body without its Download section. That
|
||||
// section only matters on the release page, and What's new follows it.
|
||||
// A translated section, "## 中文" added at the end of the draft before it is
|
||||
// published (it runs to the next heading of its level, or the end), becomes
|
||||
// `notes.zh-CN`: What's new shows it in that UI language and falls back to
|
||||
// `notes`, the English rest of the body. HTML comments are dropped.
|
||||
const { data: release } = await github.rest.repos.getRelease({ owner, repo, release_id });
|
||||
const languages = { "中文": "zh-CN" };
|
||||
const notesByLanguage = (body) => {
|
||||
const parts = { "": [] };
|
||||
let lang = "";
|
||||
let langLevel = 0;
|
||||
let skip = 0;
|
||||
for (const line of body.replace(/<!--[\s\S]*?-->/g, "").split(/\r?\n/)) {
|
||||
const heading = /^(#{1,6})\s+(.*)$/.exec(line);
|
||||
if (heading) {
|
||||
const level = heading[1].length;
|
||||
const title = heading[2].trim();
|
||||
if (skip && level <= skip) skip = 0;
|
||||
if (lang && level <= langLevel) lang = "";
|
||||
if (!skip && level <= 3 && /^download$/i.test(title)) {
|
||||
skip = level;
|
||||
continue;
|
||||
}
|
||||
if (!skip && !lang && level <= 2 && languages[title]) {
|
||||
lang = languages[title];
|
||||
langLevel = level;
|
||||
parts[lang] ??= [];
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (!skip) parts[lang].push(line);
|
||||
}
|
||||
return Object.fromEntries(Object.entries(parts).map(([key, lines]) => [key, lines.join("\n").trim()]));
|
||||
};
|
||||
const { "": notes, ...translated } = notesByLanguage(String(release.body ?? ""));
|
||||
const translations = Object.fromEntries(
|
||||
Object.entries(translated).filter(([, text]) => text).map(([lang, text]) => [`notes.${lang}`, text]),
|
||||
);
|
||||
const manifest = {
|
||||
version: tag.replace(/^v/, ""), notes, ...translations, pub_date: new Date().toISOString(), platforms,
|
||||
};
|
||||
const old = assets.find((a) => a.name === "latest.json");
|
||||
if (old) await github.rest.repos.deleteReleaseAsset({ owner, repo, asset_id: old.id });
|
||||
await github.rest.repos.uploadReleaseAsset({
|
||||
|
||||
@@ -40,6 +40,7 @@ CLAUDE.local.md
|
||||
.claude/skills/*
|
||||
!.claude/skills/verify
|
||||
!.claude/skills/issue-fix
|
||||
!.claude/skills/eval
|
||||
|
||||
# Local reference screenshots
|
||||
pycharm-screenshots/
|
||||
@@ -9,9 +9,11 @@
|
||||
"dist",
|
||||
"target",
|
||||
"src/bindings.ts",
|
||||
"evals/*/golden",
|
||||
"src/editor/core/cm/angular/parser*.ts",
|
||||
"src/editor/core/cm/jsp/parser*.ts",
|
||||
"src/editor/core/cm/astro/parser*.ts",
|
||||
"src/editor/core/cm/django/parser*.ts",
|
||||
"plugins/themes.md",
|
||||
"pnpm-lock.yaml",
|
||||
"**/*.toml",
|
||||
|
||||
@@ -24,8 +24,8 @@ Visual style: dark, low-contrast greys, one cool accent
|
||||
Plugin docs: `plugins/README.md`, `plugins/themes.md`.
|
||||
- `java/` — JDT LS extension bundles (`java-usages`, `kotlin-bridge`) and the patched
|
||||
Lombok agent, embedded into the app by `ide-sdk` / `ide-tools`.
|
||||
- `e2e/` — Playwright UI tests (`pnpm e2e`). `fixtures/` — sample projects per language
|
||||
for manual and automated testing.
|
||||
- `e2e/` — Playwright UI tests (`pnpm e2e`). `evals/` — code-intelligence evals (`pnpm eval`).
|
||||
`fixtures/` — sample projects per language for manual and automated testing.
|
||||
- `docs/` — the Mintlify documentation site (`docs.json` navigation, `en/` and `zh/` pages);
|
||||
preview with `npx mint dev` in `docs/`.
|
||||
|
||||
@@ -42,6 +42,7 @@ Visual style: dark, low-contrast greys, one cool accent
|
||||
shortcuts live in `src/platform/keymaps/`.
|
||||
- Styling: CSS Modules + tokens only (no hex literals in components). Inline SVG
|
||||
icons from `src/ui/icons.tsx`, never emoji. Real `<button>`/`<input>`; `aria-label` on icon buttons.
|
||||
`gap` uses the `--space-*` scale (`src/styles/tokens.css`, 4px grid) where a value is on it.
|
||||
- User-visible text goes through the text catalog: add every new string to both
|
||||
`src/platform/locales/text/en.json` and `zh.json` (`textCatalog.test.ts` enforces it).
|
||||
Other locales may lack entries; their UI shows the English text until a translator adds it.
|
||||
@@ -59,9 +60,12 @@ Unclear product decisions → `needs-decision` and stop.
|
||||
|
||||
## Skills
|
||||
|
||||
Do not use Impeccable or its related skills in this repository, including globally installed copies.
|
||||
|
||||
Shared agent skills live in `.agents/skills/` (`.claude/skills/` links to them):
|
||||
`verify` drives the running app through Tauri MCP and captures screenshot evidence;
|
||||
`issue-fix` takes an issue from reproduction to one PR.
|
||||
`issue-fix` takes an issue from reproduction to one PR;
|
||||
`eval` takes a failing code-intelligence eval to a fix with before / after numbers.
|
||||
|
||||
## Commands
|
||||
|
||||
@@ -81,6 +85,11 @@ Shared agent skills live in `.agents/skills/` (`.claude/skills/` links to them):
|
||||
`pnpm gen:bindings`.
|
||||
- `pnpm perf` (UI interaction latency / frames / leaks), `pnpm perf:rust` (criterion
|
||||
benches vs budgets). Budgets: `perf/budgets.json`.
|
||||
- `pnpm eval [--only <id>] [--runs n] [--save-baseline]` — Java code intelligence (navigation,
|
||||
usages, gutter markers) in the production workbench against a release `openidea-server` and
|
||||
jdtls: time to feedback / first result / complete, results vs `evals/<language>/golden/`.
|
||||
Report: `target/eval/report.json`. Goldens and `evals/budgets.json` are changed by people only.
|
||||
Tool versions: `.github/tool-versions.env` (shared with Language smoke).
|
||||
- `pnpm release:prepare <version>` — the release commit's changes: app version everywhere,
|
||||
`Cargo.lock`, and the plugin API snapshot when `wit/` moved to a new version.
|
||||
|
||||
|
||||
Generated
+362
-59
@@ -199,7 +199,7 @@ dependencies = [
|
||||
"wayland-backend",
|
||||
"wayland-client",
|
||||
"wayland-protocols",
|
||||
"zbus",
|
||||
"zbus 5.19.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -2637,7 +2637,6 @@ version = "1.4.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "337d46834ee672ab3e48caca2cb0c78cc174fb12b3a68d0d88f99a0519a5e36e"
|
||||
dependencies = [
|
||||
"generic-array 0.14.7",
|
||||
"rustversion",
|
||||
"typenum",
|
||||
]
|
||||
@@ -4145,8 +4144,9 @@ checksum = "3d3067d79b975e8844ca9eb072e16b31c3c1c36928edf9c6789548c524d0d954"
|
||||
|
||||
[[package]]
|
||||
name = "ide-agent"
|
||||
version = "0.1.41"
|
||||
version = "0.1.45"
|
||||
dependencies = [
|
||||
"blake3",
|
||||
"dunce",
|
||||
"encoding_rs",
|
||||
"ide-core",
|
||||
@@ -4169,7 +4169,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "ide-analysis"
|
||||
version = "0.1.41"
|
||||
version = "0.1.45"
|
||||
dependencies = [
|
||||
"ast-grep-core",
|
||||
"ide-core",
|
||||
@@ -4183,7 +4183,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "ide-android"
|
||||
version = "0.1.41"
|
||||
version = "0.1.45"
|
||||
dependencies = [
|
||||
"ide-core",
|
||||
"serde",
|
||||
@@ -4194,7 +4194,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "ide-build"
|
||||
version = "0.1.41"
|
||||
version = "0.1.45"
|
||||
dependencies = [
|
||||
"blake3",
|
||||
"chardetng",
|
||||
@@ -4212,7 +4212,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "ide-cli"
|
||||
version = "0.1.41"
|
||||
version = "0.1.45"
|
||||
dependencies = [
|
||||
"dirs 6.0.0",
|
||||
"serde",
|
||||
@@ -4226,17 +4226,18 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "ide-clipboard"
|
||||
version = "0.1.41"
|
||||
version = "0.1.45"
|
||||
dependencies = [
|
||||
"gtk",
|
||||
"objc2 0.6.5",
|
||||
"objc2-app-kit 0.3.2",
|
||||
"objc2-foundation 0.3.2",
|
||||
"png 0.18.1",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ide-core"
|
||||
version = "0.1.41"
|
||||
version = "0.1.45"
|
||||
dependencies = [
|
||||
"dunce",
|
||||
"encoding_rs",
|
||||
@@ -4253,7 +4254,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "ide-coverage"
|
||||
version = "0.1.41"
|
||||
version = "0.1.45"
|
||||
dependencies = [
|
||||
"ide-core",
|
||||
"ide-run",
|
||||
@@ -4267,40 +4268,53 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "ide-dap"
|
||||
version = "0.1.41"
|
||||
version = "0.1.45"
|
||||
dependencies = [
|
||||
"base64 0.22.1",
|
||||
"futures",
|
||||
"ide-core",
|
||||
"ide-heap",
|
||||
"ide-jdwp",
|
||||
"ide-jsonrpc",
|
||||
"ide-jvm-attach",
|
||||
"ide-jvm-inspect",
|
||||
"ide-language",
|
||||
"libc",
|
||||
"memchr",
|
||||
"parking_lot",
|
||||
"percent-encoding",
|
||||
"portable-pty",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"specta",
|
||||
"tempfile",
|
||||
"tokio",
|
||||
"tokio-tungstenite",
|
||||
"tracing",
|
||||
"tracing-subscriber",
|
||||
"windows-sys 0.61.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ide-db"
|
||||
version = "0.1.41"
|
||||
version = "0.1.45"
|
||||
dependencies = [
|
||||
"base64 0.22.1",
|
||||
"bb8",
|
||||
"blake3",
|
||||
"dirs 6.0.0",
|
||||
"dunce",
|
||||
"flate2",
|
||||
"futures",
|
||||
"ide-core",
|
||||
"ide-http",
|
||||
"ide-ssh",
|
||||
"notify",
|
||||
"open",
|
||||
"oracledb",
|
||||
"parking_lot",
|
||||
"reqwest",
|
||||
"rsa",
|
||||
"rustls",
|
||||
"serde",
|
||||
"serde_json",
|
||||
@@ -4314,11 +4328,12 @@ dependencies = [
|
||||
"toml_edit 0.25.17+spec-1.1.0",
|
||||
"tracing",
|
||||
"uuid",
|
||||
"zeroize",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ide-docker"
|
||||
version = "0.1.41"
|
||||
version = "0.1.45"
|
||||
dependencies = [
|
||||
"ide-core",
|
||||
"serde",
|
||||
@@ -4331,7 +4346,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "ide-extension"
|
||||
version = "0.1.41"
|
||||
version = "0.1.45"
|
||||
dependencies = [
|
||||
"base64 0.22.1",
|
||||
"blake3",
|
||||
@@ -4352,7 +4367,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "ide-fonts"
|
||||
version = "0.1.41"
|
||||
version = "0.1.45"
|
||||
dependencies = [
|
||||
"objc2-core-foundation",
|
||||
"objc2-core-text",
|
||||
@@ -4364,7 +4379,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "ide-github"
|
||||
version = "0.1.41"
|
||||
version = "0.1.45"
|
||||
dependencies = [
|
||||
"ide-core",
|
||||
"ide-vcs",
|
||||
@@ -4375,9 +4390,27 @@ dependencies = [
|
||||
"which",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ide-heap"
|
||||
version = "0.1.45"
|
||||
dependencies = [
|
||||
"criterion",
|
||||
"ide-core",
|
||||
"libc",
|
||||
"memmap2",
|
||||
"proptest",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"specta",
|
||||
"tempfile",
|
||||
"thiserror 2.0.21",
|
||||
"tracing",
|
||||
"uuid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ide-host"
|
||||
version = "0.1.41"
|
||||
version = "0.1.45"
|
||||
dependencies = [
|
||||
"base64 0.22.1",
|
||||
"blake3",
|
||||
@@ -4397,9 +4430,11 @@ dependencies = [
|
||||
"ide-db",
|
||||
"ide-docker",
|
||||
"ide-extension",
|
||||
"ide-heap",
|
||||
"ide-http",
|
||||
"ide-index",
|
||||
"ide-jsonrpc",
|
||||
"ide-jvm-attach",
|
||||
"ide-language",
|
||||
"ide-lsp",
|
||||
"ide-mcp",
|
||||
@@ -4407,6 +4442,7 @@ dependencies = [
|
||||
"ide-plugin",
|
||||
"ide-remote",
|
||||
"ide-run",
|
||||
"ide-scaffold",
|
||||
"ide-sdk",
|
||||
"ide-search",
|
||||
"ide-spell",
|
||||
@@ -4417,6 +4453,7 @@ dependencies = [
|
||||
"ide-tools",
|
||||
"ide-vcs",
|
||||
"ide-vfs",
|
||||
"ide-web",
|
||||
"ide-workspace",
|
||||
"ignore",
|
||||
"jsonschema",
|
||||
@@ -4433,6 +4470,7 @@ dependencies = [
|
||||
"specta-typescript",
|
||||
"tempfile",
|
||||
"tokio",
|
||||
"tokio-tungstenite",
|
||||
"toml 0.9.12+spec-1.1.0",
|
||||
"tracing",
|
||||
"tracing-subscriber",
|
||||
@@ -4443,7 +4481,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "ide-http"
|
||||
version = "0.1.41"
|
||||
version = "0.1.45"
|
||||
dependencies = [
|
||||
"ide-core",
|
||||
"ide-vfs",
|
||||
@@ -4462,7 +4500,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "ide-index"
|
||||
version = "0.1.41"
|
||||
version = "0.1.45"
|
||||
dependencies = [
|
||||
"blake3",
|
||||
"criterion",
|
||||
@@ -4482,9 +4520,21 @@ dependencies = [
|
||||
"tracing",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ide-jdwp"
|
||||
version = "0.1.45"
|
||||
dependencies = [
|
||||
"ide-jvm-inspect",
|
||||
"proptest",
|
||||
"tempfile",
|
||||
"thiserror 2.0.21",
|
||||
"tokio",
|
||||
"tracing",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ide-jsonrpc"
|
||||
version = "0.1.41"
|
||||
version = "0.1.45"
|
||||
dependencies = [
|
||||
"encoding_rs",
|
||||
"futures",
|
||||
@@ -4499,15 +4549,36 @@ dependencies = [
|
||||
"tracing",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ide-jvm-attach"
|
||||
version = "0.1.45"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"tempfile",
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ide-jvm-inspect"
|
||||
version = "0.1.45"
|
||||
dependencies = [
|
||||
"futures",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"specta",
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ide-language"
|
||||
version = "0.1.41"
|
||||
version = "0.1.45"
|
||||
dependencies = [
|
||||
"blake3",
|
||||
"criterion",
|
||||
"ide-core",
|
||||
"ide-extension",
|
||||
"ide-text",
|
||||
"memchr",
|
||||
"parking_lot",
|
||||
"regex-lite",
|
||||
"serde",
|
||||
@@ -4542,6 +4613,7 @@ dependencies = [
|
||||
"tree-sitter-nix",
|
||||
"tree-sitter-ocaml",
|
||||
"tree-sitter-php",
|
||||
"tree-sitter-prisma-io",
|
||||
"tree-sitter-proto",
|
||||
"tree-sitter-python",
|
||||
"tree-sitter-ruby",
|
||||
@@ -4557,13 +4629,15 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "ide-lsp"
|
||||
version = "0.1.41"
|
||||
version = "0.1.45"
|
||||
dependencies = [
|
||||
"dunce",
|
||||
"encoding_rs",
|
||||
"futures",
|
||||
"globset",
|
||||
"ide-core",
|
||||
"ide-jsonrpc",
|
||||
"ide-jvm-inspect",
|
||||
"ide-language",
|
||||
"ide-text",
|
||||
"ide-vfs",
|
||||
@@ -4583,7 +4657,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "ide-mcp"
|
||||
version = "0.1.41"
|
||||
version = "0.1.45"
|
||||
dependencies = [
|
||||
"bytes",
|
||||
"dunce",
|
||||
@@ -4602,7 +4676,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "ide-notebook"
|
||||
version = "0.1.41"
|
||||
version = "0.1.45"
|
||||
dependencies = [
|
||||
"futures",
|
||||
"ide-core",
|
||||
@@ -4625,7 +4699,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "ide-plugin"
|
||||
version = "0.1.41"
|
||||
version = "0.1.45"
|
||||
dependencies = [
|
||||
"blake3",
|
||||
"dunce",
|
||||
@@ -4648,9 +4722,10 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "ide-profiler"
|
||||
version = "0.1.41"
|
||||
version = "0.1.45"
|
||||
dependencies = [
|
||||
"ide-core",
|
||||
"ide-jvm-attach",
|
||||
"ide-run",
|
||||
"serde",
|
||||
"serde_json",
|
||||
@@ -4661,9 +4736,10 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "ide-remote"
|
||||
version = "0.1.41"
|
||||
version = "0.1.45"
|
||||
dependencies = [
|
||||
"dunce",
|
||||
"futures",
|
||||
"globset",
|
||||
"ide-core",
|
||||
"ide-run",
|
||||
@@ -4680,7 +4756,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "ide-run"
|
||||
version = "0.1.41"
|
||||
version = "0.1.45"
|
||||
dependencies = [
|
||||
"dunce",
|
||||
"encoding_rs",
|
||||
@@ -4702,11 +4778,28 @@ dependencies = [
|
||||
"zip",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ide-scaffold"
|
||||
version = "0.1.45"
|
||||
dependencies = [
|
||||
"ide-core",
|
||||
"ide-tools",
|
||||
"ide-vcs",
|
||||
"insta",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"specta",
|
||||
"tempfile",
|
||||
"toml 0.9.12+spec-1.1.0",
|
||||
"tracing",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ide-sdk"
|
||||
version = "0.1.41"
|
||||
version = "0.1.45"
|
||||
dependencies = [
|
||||
"dunce",
|
||||
"flate2",
|
||||
"ide-core",
|
||||
"ide-language",
|
||||
"ide-run",
|
||||
@@ -4716,17 +4809,20 @@ dependencies = [
|
||||
"serde",
|
||||
"serde_json",
|
||||
"specta",
|
||||
"tar",
|
||||
"tempfile",
|
||||
"toml 0.9.12+spec-1.1.0",
|
||||
"tracing",
|
||||
"url",
|
||||
"zip",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ide-search"
|
||||
version = "0.1.41"
|
||||
version = "0.1.45"
|
||||
dependencies = [
|
||||
"criterion",
|
||||
"encoding_rs",
|
||||
"globset",
|
||||
"grep-matcher",
|
||||
"grep-regex",
|
||||
@@ -4744,7 +4840,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "ide-spell"
|
||||
version = "0.1.41"
|
||||
version = "0.1.45"
|
||||
dependencies = [
|
||||
"ide-core",
|
||||
"parking_lot",
|
||||
@@ -4757,7 +4853,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "ide-ssh"
|
||||
version = "0.1.41"
|
||||
version = "0.1.45"
|
||||
dependencies = [
|
||||
"dirs 6.0.0",
|
||||
"flate2",
|
||||
@@ -4767,18 +4863,22 @@ dependencies = [
|
||||
"parking_lot",
|
||||
"russh",
|
||||
"russh-sftp",
|
||||
"rustls",
|
||||
"rustls-platform-verifier",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"specta",
|
||||
"suppaftp",
|
||||
"tempfile",
|
||||
"tokio",
|
||||
"tokio-rustls",
|
||||
"tracing",
|
||||
"uuid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ide-syntax"
|
||||
version = "0.1.41"
|
||||
version = "0.1.45"
|
||||
dependencies = [
|
||||
"ide-core",
|
||||
"ide-language",
|
||||
@@ -4793,7 +4893,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "ide-terminal"
|
||||
version = "0.1.41"
|
||||
version = "0.1.45"
|
||||
dependencies = [
|
||||
"ide-core",
|
||||
"libc",
|
||||
@@ -4806,7 +4906,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "ide-text"
|
||||
version = "0.1.41"
|
||||
version = "0.1.45"
|
||||
dependencies = [
|
||||
"criterion",
|
||||
"ide-core",
|
||||
@@ -4819,7 +4919,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "ide-tools"
|
||||
version = "0.1.41"
|
||||
version = "0.1.45"
|
||||
dependencies = [
|
||||
"flate2",
|
||||
"hex",
|
||||
@@ -4840,7 +4940,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "ide-vcs"
|
||||
version = "0.1.41"
|
||||
version = "0.1.45"
|
||||
dependencies = [
|
||||
"blake3",
|
||||
"dunce",
|
||||
@@ -4861,11 +4961,12 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "ide-vfs"
|
||||
version = "0.1.41"
|
||||
version = "0.1.45"
|
||||
dependencies = [
|
||||
"chardetng",
|
||||
"dunce",
|
||||
"encoding_rs",
|
||||
"flate2",
|
||||
"gix",
|
||||
"globset",
|
||||
"ide-core",
|
||||
@@ -4876,6 +4977,7 @@ dependencies = [
|
||||
"serde",
|
||||
"serde_json",
|
||||
"specta",
|
||||
"tar",
|
||||
"tempfile",
|
||||
"tracing",
|
||||
"trash",
|
||||
@@ -4885,11 +4987,11 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "ide-web"
|
||||
version = "0.1.41"
|
||||
version = "0.1.45"
|
||||
dependencies = [
|
||||
"dirs 6.0.0",
|
||||
"ide-core",
|
||||
"open",
|
||||
"parking_lot",
|
||||
"percent-encoding",
|
||||
"serde",
|
||||
"specta",
|
||||
@@ -4901,7 +5003,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "ide-workspace"
|
||||
version = "0.1.41"
|
||||
version = "0.1.45"
|
||||
dependencies = [
|
||||
"blake3",
|
||||
"dunce",
|
||||
@@ -5470,6 +5572,29 @@ dependencies = [
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "lazy-regex"
|
||||
version = "3.6.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f0a2cce8a22920a29fdcc8c9170aa5d29edd9fbcf8bea856e24c2cb1c2419f67"
|
||||
dependencies = [
|
||||
"lazy-regex-proc_macros",
|
||||
"once_cell",
|
||||
"regex",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "lazy-regex-proc_macros"
|
||||
version = "3.6.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6f1e4b1f1d8c71edb4ff2b2150ec4606445fd2ddf67ddc097655daf59286baa6"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"regex",
|
||||
"syn 3.0.9",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "lazy_static"
|
||||
version = "1.5.1"
|
||||
@@ -5597,6 +5722,20 @@ dependencies = [
|
||||
"serde_repr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "mac-notification-sys"
|
||||
version = "0.6.15"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "fd604973958ddcc11b561193c0fb96ba146506ef2f231ef2e7c35fd2cbc9beca"
|
||||
dependencies = [
|
||||
"cc",
|
||||
"log",
|
||||
"objc2 0.6.5",
|
||||
"objc2-foundation 0.3.2",
|
||||
"time",
|
||||
"uuid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "mach2"
|
||||
version = "0.6.0"
|
||||
@@ -5869,6 +6008,20 @@ dependencies = [
|
||||
"windows-sys 0.60.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "notify-rust"
|
||||
version = "4.19.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "57da5b5d3b7809bf71858da5a2818c3404ca4688c02c8abecdda0fcdec235df3"
|
||||
dependencies = [
|
||||
"futures-lite",
|
||||
"log",
|
||||
"mac-notification-sys",
|
||||
"serde",
|
||||
"tauri-winrt-notification",
|
||||
"zbus 6.3.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "notify-types"
|
||||
version = "2.1.0"
|
||||
@@ -6479,7 +6632,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "openidea"
|
||||
version = "0.1.41"
|
||||
version = "0.1.45"
|
||||
dependencies = [
|
||||
"base64 0.22.1",
|
||||
"blake3",
|
||||
@@ -6513,6 +6666,7 @@ dependencies = [
|
||||
"ide-profiler",
|
||||
"ide-remote",
|
||||
"ide-run",
|
||||
"ide-scaffold",
|
||||
"ide-sdk",
|
||||
"ide-search",
|
||||
"ide-spell",
|
||||
@@ -6540,6 +6694,7 @@ dependencies = [
|
||||
"tauri-build",
|
||||
"tauri-plugin-dialog",
|
||||
"tauri-plugin-mcp-bridge",
|
||||
"tauri-plugin-notification",
|
||||
"tauri-plugin-opener",
|
||||
"tauri-plugin-single-instance",
|
||||
"tauri-plugin-updater",
|
||||
@@ -6835,6 +6990,26 @@ dependencies = [
|
||||
"siphasher",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pin-project"
|
||||
version = "1.1.13"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2466b2336ed02bcdca6b294417127b90ec92038d1d5c4fbeac971a922e0e0924"
|
||||
dependencies = [
|
||||
"pin-project-internal",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pin-project-internal"
|
||||
version = "1.1.13"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c96395f0a926bc13b1c17622aaddda1ecb55d49c8f1bf9777e4d877800a43f8b"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.119",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pin-project-lite"
|
||||
version = "0.2.17"
|
||||
@@ -7583,9 +7758,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "russh"
|
||||
version = "0.64.1"
|
||||
version = "0.65.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ba61e87b9ec9a39a59a6bbed4c0b8ff7fb07073405b24a767a1f2c8560ac8f58"
|
||||
checksum = "94be3d86c71b9ed220a675c405e35c7a02565a009bf8da2e9cc13a700320a5c5"
|
||||
dependencies = [
|
||||
"aes",
|
||||
"bitflags 2.13.2",
|
||||
@@ -7719,9 +7894,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "rustix"
|
||||
version = "1.1.5"
|
||||
version = "1.1.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "891efababe418670775f199f0d233d84843c227a0949a883ce15b37c78d6629d"
|
||||
checksum = "e5e76742e5294153d4dde1207dbec5bdd81eef6fabdc0d2118babadabfd858aa"
|
||||
dependencies = [
|
||||
"bitflags 2.13.2",
|
||||
"errno",
|
||||
@@ -8007,7 +8182,7 @@ dependencies = [
|
||||
"once_cell",
|
||||
"serde",
|
||||
"sha2 0.11.1",
|
||||
"zbus",
|
||||
"zbus 5.19.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -8591,7 +8766,7 @@ checksum = "3a0219bd7d979d58245a4f41f695e1ac9f8befdffadd7f61f1bae9e39abc6620"
|
||||
[[package]]
|
||||
name = "sqlx"
|
||||
version = "0.9.0"
|
||||
source = "git+https://github.com/transact-rs/sqlx?rev=10462fa4414c6c7f1ed1d39bb31678e3b726878c#10462fa4414c6c7f1ed1d39bb31678e3b726878c"
|
||||
source = "git+https://github.com/l0ng-ai/sqlx?rev=9894985a1babc7e6bc0437da4fd43e3143184073#9894985a1babc7e6bc0437da4fd43e3143184073"
|
||||
dependencies = [
|
||||
"sqlx-core",
|
||||
"sqlx-macros",
|
||||
@@ -8603,7 +8778,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "sqlx-core"
|
||||
version = "0.9.0"
|
||||
source = "git+https://github.com/transact-rs/sqlx?rev=10462fa4414c6c7f1ed1d39bb31678e3b726878c#10462fa4414c6c7f1ed1d39bb31678e3b726878c"
|
||||
source = "git+https://github.com/l0ng-ai/sqlx?rev=9894985a1babc7e6bc0437da4fd43e3143184073#9894985a1babc7e6bc0437da4fd43e3143184073"
|
||||
dependencies = [
|
||||
"base64 0.22.1",
|
||||
"bytes",
|
||||
@@ -8638,7 +8813,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "sqlx-macros"
|
||||
version = "0.9.0"
|
||||
source = "git+https://github.com/transact-rs/sqlx?rev=10462fa4414c6c7f1ed1d39bb31678e3b726878c#10462fa4414c6c7f1ed1d39bb31678e3b726878c"
|
||||
source = "git+https://github.com/l0ng-ai/sqlx?rev=9894985a1babc7e6bc0437da4fd43e3143184073#9894985a1babc7e6bc0437da4fd43e3143184073"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
@@ -8650,7 +8825,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "sqlx-macros-core"
|
||||
version = "0.9.0"
|
||||
source = "git+https://github.com/transact-rs/sqlx?rev=10462fa4414c6c7f1ed1d39bb31678e3b726878c#10462fa4414c6c7f1ed1d39bb31678e3b726878c"
|
||||
source = "git+https://github.com/l0ng-ai/sqlx?rev=9894985a1babc7e6bc0437da4fd43e3143184073#9894985a1babc7e6bc0437da4fd43e3143184073"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"dotenvy",
|
||||
@@ -8674,7 +8849,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "sqlx-mysql"
|
||||
version = "0.9.0"
|
||||
source = "git+https://github.com/transact-rs/sqlx?rev=10462fa4414c6c7f1ed1d39bb31678e3b726878c#10462fa4414c6c7f1ed1d39bb31678e3b726878c"
|
||||
source = "git+https://github.com/l0ng-ai/sqlx?rev=9894985a1babc7e6bc0437da4fd43e3143184073#9894985a1babc7e6bc0437da4fd43e3143184073"
|
||||
dependencies = [
|
||||
"bitflags 2.13.2",
|
||||
"byteorder",
|
||||
@@ -8699,7 +8874,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "sqlx-postgres"
|
||||
version = "0.9.0"
|
||||
source = "git+https://github.com/transact-rs/sqlx?rev=10462fa4414c6c7f1ed1d39bb31678e3b726878c#10462fa4414c6c7f1ed1d39bb31678e3b726878c"
|
||||
source = "git+https://github.com/l0ng-ai/sqlx?rev=9894985a1babc7e6bc0437da4fd43e3143184073#9894985a1babc7e6bc0437da4fd43e3143184073"
|
||||
dependencies = [
|
||||
"atoi",
|
||||
"base64 0.22.1",
|
||||
@@ -8733,7 +8908,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "sqlx-sqlite"
|
||||
version = "0.9.0"
|
||||
source = "git+https://github.com/transact-rs/sqlx?rev=10462fa4414c6c7f1ed1d39bb31678e3b726878c#10462fa4414c6c7f1ed1d39bb31678e3b726878c"
|
||||
source = "git+https://github.com/l0ng-ai/sqlx?rev=9894985a1babc7e6bc0437da4fd43e3143184073#9894985a1babc7e6bc0437da4fd43e3143184073"
|
||||
dependencies = [
|
||||
"atoi",
|
||||
"flume",
|
||||
@@ -8883,6 +9058,23 @@ version = "2.6.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292"
|
||||
|
||||
[[package]]
|
||||
name = "suppaftp"
|
||||
version = "12.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2b58fcf921745cca6f0127c9f822069947da8c6dbeff52e9004750bcc448171f"
|
||||
dependencies = [
|
||||
"async-trait",
|
||||
"chrono",
|
||||
"lazy-regex",
|
||||
"log",
|
||||
"pin-project",
|
||||
"rustls-pki-types",
|
||||
"thiserror 2.0.21",
|
||||
"tokio",
|
||||
"tokio-rustls",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "swift-rs"
|
||||
version = "1.0.9"
|
||||
@@ -9266,6 +9458,26 @@ dependencies = [
|
||||
"windows-core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tauri-plugin-notification"
|
||||
version = "2.8.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "14a226bfb8189ec4ab2c1936e66330ad06c44d9f6c54a674338208491dee177f"
|
||||
dependencies = [
|
||||
"log",
|
||||
"notify-rust",
|
||||
"rand 0.10.3",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"serde_repr",
|
||||
"tauri",
|
||||
"tauri-plugin",
|
||||
"thiserror 2.0.21",
|
||||
"time",
|
||||
"url",
|
||||
"zbus 5.19.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tauri-plugin-opener"
|
||||
version = "2.7.2"
|
||||
@@ -9285,7 +9497,7 @@ dependencies = [
|
||||
"thiserror 2.0.21",
|
||||
"url",
|
||||
"windows",
|
||||
"zbus",
|
||||
"zbus 5.19.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -9301,7 +9513,7 @@ dependencies = [
|
||||
"tokio",
|
||||
"tracing",
|
||||
"windows-sys 0.61.2",
|
||||
"zbus",
|
||||
"zbus 5.19.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -9484,6 +9696,17 @@ dependencies = [
|
||||
"toml 1.1.8+spec-1.1.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tauri-winrt-notification"
|
||||
version = "0.8.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f37a6c354fd28fc9e322ed9bd47e3959576dad28c9d58ea1cf888cce1c7ccb36"
|
||||
dependencies = [
|
||||
"thiserror 2.0.21",
|
||||
"windows",
|
||||
"windows-version",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tempfile"
|
||||
version = "3.27.0"
|
||||
@@ -9491,7 +9714,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "32497e9a4c7b38532efcdebeef879707aa9f794296a4f0244f6f69e9bc8574bd"
|
||||
dependencies = [
|
||||
"fastrand",
|
||||
"getrandom 0.3.4",
|
||||
"getrandom 0.4.3",
|
||||
"once_cell",
|
||||
"rustix",
|
||||
"windows-sys 0.61.2",
|
||||
@@ -9723,7 +9946,10 @@ checksum = "d25a406cddcc431a75d3d9afc6a7c0f7428d4891dd973e4d54c56b46127bf857"
|
||||
dependencies = [
|
||||
"futures-util",
|
||||
"log",
|
||||
"rustls",
|
||||
"rustls-pki-types",
|
||||
"tokio",
|
||||
"tokio-rustls",
|
||||
"tungstenite",
|
||||
]
|
||||
|
||||
@@ -10289,6 +10515,16 @@ dependencies = [
|
||||
"tree-sitter-language",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tree-sitter-prisma-io"
|
||||
version = "1.6.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c35912d65a6adb07f781db46d843bf0464935e267da6ed192705516adafc7c24"
|
||||
dependencies = [
|
||||
"cc",
|
||||
"tree-sitter-language",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tree-sitter-proto"
|
||||
version = "0.6.0"
|
||||
@@ -10417,6 +10653,8 @@ dependencies = [
|
||||
"httparse",
|
||||
"log",
|
||||
"rand 0.9.5",
|
||||
"rustls",
|
||||
"rustls-pki-types",
|
||||
"sha1 0.10.7",
|
||||
"thiserror 2.0.21",
|
||||
"utf-8",
|
||||
@@ -11959,11 +12197,38 @@ dependencies = [
|
||||
"uuid",
|
||||
"windows-sys 0.61.2",
|
||||
"winnow 1.0.4",
|
||||
"zbus_macros",
|
||||
"zbus_macros 5.19.0",
|
||||
"zbus_names",
|
||||
"zvariant",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zbus"
|
||||
version = "6.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6b4aae740b23927ceabed962431abab2cdefb9f7f885e5bbab062da09bf580e4"
|
||||
dependencies = [
|
||||
"async-trait",
|
||||
"bitflags 2.13.2",
|
||||
"endi",
|
||||
"futures-core",
|
||||
"hex",
|
||||
"libc",
|
||||
"ordered-stream",
|
||||
"rustix",
|
||||
"serde",
|
||||
"serde_repr",
|
||||
"socket2",
|
||||
"uds_windows",
|
||||
"uuid",
|
||||
"windows-sys 0.61.2",
|
||||
"winnow 1.0.4",
|
||||
"zbus_macros 6.3.0",
|
||||
"zbus_utils",
|
||||
"zcheapstr",
|
||||
"zruntime",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zbus-secret-service-keyring-store"
|
||||
version = "1.0.1"
|
||||
@@ -11972,7 +12237,7 @@ checksum = "74801d001b9e7729adb4f1825b67b398185fed424749aa3d8bacf70417137d9a"
|
||||
dependencies = [
|
||||
"keyring-core",
|
||||
"secret-service",
|
||||
"zbus",
|
||||
"zbus 5.19.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -11990,6 +12255,18 @@ dependencies = [
|
||||
"zvariant_utils",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zbus_macros"
|
||||
version = "6.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "37c9dd4beaf145c16fcb2902862f50da86e54aed13dd368ddf64a1d17e6b8529"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 3.0.9",
|
||||
"zbus_utils",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zbus_names"
|
||||
version = "4.3.5"
|
||||
@@ -12001,6 +12278,19 @@ dependencies = [
|
||||
"zvariant",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zbus_utils"
|
||||
version = "6.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "af55e52da295311b368c45bcae221f020e28e97a2420360170ce389218e76ec7"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"serde",
|
||||
"syn 3.0.9",
|
||||
"winnow 1.0.4",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zcheapstr"
|
||||
version = "1.1.0"
|
||||
@@ -12157,6 +12447,19 @@ dependencies = [
|
||||
"simd-adler32",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zruntime"
|
||||
version = "0.2.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ce6a55d4f521760475a30f7b96779508fe8c40dc834478709454dd1d5f0ebc72"
|
||||
dependencies = [
|
||||
"futures-core",
|
||||
"futures-io",
|
||||
"rustix",
|
||||
"socket2",
|
||||
"windows-sys 0.61.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zune-core"
|
||||
version = "0.5.3"
|
||||
|
||||
+11
-2
@@ -5,7 +5,7 @@ members = ["crates/*", "src-tauri"]
|
||||
exclude = ["sdk", "plugins"]
|
||||
|
||||
[workspace.package]
|
||||
version = "0.1.41"
|
||||
version = "0.1.45"
|
||||
edition = "2024"
|
||||
rust-version = "1.96"
|
||||
license = "Apache-2.0"
|
||||
@@ -43,10 +43,15 @@ ide-ssh = { path = "crates/ide-ssh" }
|
||||
ide-remote = { path = "crates/ide-remote" }
|
||||
ide-host = { path = "crates/ide-host" }
|
||||
ide-mcp = { path = "crates/ide-mcp" }
|
||||
ide-scaffold = { path = "crates/ide-scaffold" }
|
||||
ide-notebook = { path = "crates/ide-notebook" }
|
||||
ide-extension = { path = "crates/ide-extension" }
|
||||
ide-plugin = { path = "crates/ide-plugin" }
|
||||
ide-cli = { path = "crates/ide-cli" }
|
||||
ide-jvm-attach = { path = "crates/ide-jvm-attach" }
|
||||
ide-jvm-inspect = { path = "crates/ide-jvm-inspect" }
|
||||
ide-jdwp = { path = "crates/ide-jdwp" }
|
||||
ide-heap = { path = "crates/ide-heap" }
|
||||
|
||||
# serialization & IPC typing
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
@@ -63,6 +68,7 @@ parking_lot = "0.12"
|
||||
rayon = "1"
|
||||
dashmap = "6"
|
||||
futures = "0.3"
|
||||
tokio-tungstenite = { version = "0.28", default-features = false, features = ["connect", "__rustls-tls"] }
|
||||
|
||||
# errors & diagnostics
|
||||
thiserror = "2"
|
||||
@@ -82,7 +88,10 @@ trash = "5"
|
||||
blake3 = "1"
|
||||
dunce = "1"
|
||||
dirs = "6"
|
||||
memmap2 = "0.9"
|
||||
zip = { version = "9.2.3", default-features = false, features = ["deflate"] }
|
||||
flate2 = "1"
|
||||
tar = "0.4"
|
||||
|
||||
# search, index, language
|
||||
grep-searcher = "0.1"
|
||||
@@ -104,7 +113,7 @@ reqwest = { version = "0.13", default-features = false, features = ["rustls-no-p
|
||||
rustls = { version = "0.23", default-features = false, features = ["ring", "std", "tls12", "logging"] }
|
||||
rustls-platform-verifier = "0.7"
|
||||
# Until SQLx releases its libsqlite3-sys 0.39 update (RUSTSEC-2026-0459).
|
||||
sqlx = { version = "0.9", git = "https://github.com/transact-rs/sqlx", rev = "10462fa4414c6c7f1ed1d39bb31678e3b726878c", default-features = false, features = ["runtime-tokio", "tls-rustls-ring-webpki", "sqlite", "postgres", "mysql"] }
|
||||
sqlx = { version = "0.9", git = "https://github.com/l0ng-ai/sqlx", rev = "9894985a1babc7e6bc0437da4fd43e3143184073", default-features = false, features = ["runtime-tokio", "tls-rustls-ring-webpki", "sqlite", "postgres", "mysql"] }
|
||||
tiberius = { version = "0.13", default-features = false, features = ["tds73", "rustls", "sql-browser-tokio"] }
|
||||
# Pure-Rust thin Oracle driver; pinned to the beta until it stabilizes.
|
||||
oracledb = "=26.0.0-beta.5"
|
||||
|
||||
@@ -9,6 +9,10 @@
|
||||
The IDE you know, rebuilt in Rust.
|
||||
</p>
|
||||
|
||||
<p align="center">
|
||||
English · <a href="README.zh-CN.md">简体中文</a> · <a href="README.tr.md">Türkçe</a>
|
||||
</p>
|
||||
|
||||
<p align="center">
|
||||
<a href="https://github.com/l0ng-ai/openidea/releases/latest"><b>Download</b></a>
|
||||
·
|
||||
@@ -64,7 +68,7 @@ the [official plugins](plugins/official). The plugin API is still a preview.
|
||||
- [Discord](https://discord.gg/2Zy4yn7gP) — questions, feedback and release news.
|
||||
- WeChat group — scan to join:
|
||||
|
||||
<img src=".github/assets/wechat-group.png" alt="WeChat group QR code" width="200">
|
||||
<img src=".github/assets/wechat-group-3.png" alt="WeChat group QR code" width="200">
|
||||
|
||||
- Contributing: see [CONTRIBUTING.md](CONTRIBUTING.md) and the [contributor docs](https://openidea.sh/en/contribute/setup).
|
||||
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
<p align="center">
|
||||
<picture>
|
||||
<source media="(prefers-color-scheme: dark)" srcset="src-tauri/icons/source/openidea-wordmark-dark.svg">
|
||||
<img src="src-tauri/icons/source/openidea-wordmark.svg" alt="OpenIDEA" height="56">
|
||||
</picture>
|
||||
</p>
|
||||
|
||||
<p align="center">
|
||||
Bildiğin IDE, Rust ile yeniden yazıldı.
|
||||
</p>
|
||||
|
||||
<p align="center">
|
||||
<a href="README.md">English</a> · <a href="README.zh-CN.md">简体中文</a> · Türkçe
|
||||
</p>
|
||||
|
||||
<p align="center">
|
||||
<a href="https://github.com/l0ng-ai/openidea/releases/latest"><b>İndir</b></a>
|
||||
·
|
||||
<a href="https://openidea.sh">Belgeler</a>
|
||||
·
|
||||
<a href="#performans-karşılaştırması">Performans</a>
|
||||
·
|
||||
<a href="#eklentiler">Eklentiler</a>
|
||||
·
|
||||
<a href="#topluluk">Topluluk</a>
|
||||
</p>
|
||||
|
||||
https://github.com/user-attachments/assets/234dd54c-1255-4b3a-b35c-533d78d737c0
|
||||
|
||||
OpenIDEA, IntelliJ IDEA'nın çalışma biçimini (kısayol tuşlarını, araç pencerelerini,
|
||||
her şeyi arama alışkanlığını) çekirdeği Rust ile yazılmış yerel bir uygulamaya taşır.
|
||||
|
||||
Editör, kod analizi, hata ayıklayıcı (debugger), Git, terminal, veritabanı ve HTTP istemcileri;
|
||||
60'tan fazla dil için. macOS, Windows ve Linux sürümleri var.
|
||||
|
||||
## Performans karşılaştırması
|
||||
|
||||
[freqtrade](https://github.com/freqtrade/freqtrade) projesini (Python, ~770 dosya)
|
||||
önbellekler hazırken açma, 5 çalıştırmanın ortancası:
|
||||
|
||||
| | OpenIDEA | IntelliJ IDEA 2026.2.3 |
|
||||
| ---------------------------- | ----------------------- | ---------------------- |
|
||||
| Projeyle pencerenin açılması | **0,9 sn** | 3,1 sn |
|
||||
| Hazır (CPU sakinleşti) | **1,1 sn** | 13,2 sn |
|
||||
| Bellek (USS, tüm işlemler) | **169 MB** | 2011 MB |
|
||||
| Hazır olana kadar CPU süresi | **2,4 sn** | 47,9 sn |
|
||||
| Boştayken CPU | Bir çekirdeğin **%4**'ü | Bir çekirdeğin %7'si |
|
||||
| Kapanma | **0,3 sn** | 5,9 sn |
|
||||
|
||||
`main` dalından derlenmiş bir OpenIDEA sürümü ve varsayılan ayar ve eklentilerle IDEA;
|
||||
Windows 11, 32 çekirdekli AMD, 62 GB RAM.
|
||||
IDEA, Python'u daha derinlemesine analiz eder; yani "hazır" iki tarafta aynı işi ifade etmez.
|
||||
İkisi de dışarıdan, aynı yöntemle ölçüldü: kendi makinende ve projelerinde denemek için
|
||||
[`perf/compare`](perf/compare) klasörüne bak.
|
||||
|
||||
## Eklentiler
|
||||
|
||||
Eklentiler; eylemler, kod tamamlama, hızlı düzeltmeler, araç pencereleri, özel editörler,
|
||||
temalar, dosya simgeleri ve diller ekler. Korumalı (sandbox) WebAssembly bileşenleri olarak çalışırlar;
|
||||
temalar, simge temaları ve diller için hiç kod yazmak gerekmez.
|
||||
|
||||
Eklentileri Ayarlar › Eklentiler (Settings › Plugins) bölümünden kur ya da
|
||||
`openidea-plugin new` ile kendininkini başlat. Ayrıntılar için [OpenIDEA eklentisi yazmak](https://openidea.sh/en/plugins) sayfasına ve
|
||||
[resmi eklentilere](plugins/official) bak. Eklenti API'si hâlâ önizleme aşamasında.
|
||||
|
||||
## Topluluk
|
||||
|
||||
- [Discord](https://discord.gg/2Zy4yn7gP): sorular, geri bildirim ve sürüm haberleri.
|
||||
- WeChat grubu, katılmak için kodu okut:
|
||||
|
||||
<img src=".github/assets/wechat-group-3.png" alt="WeChat grubu QR kodu" width="200">
|
||||
|
||||
- Katkıda bulunmak için: [CONTRIBUTING.md](CONTRIBUTING.md) ve [katkıcı belgeleri](https://openidea.sh/en/contribute/setup).
|
||||
|
||||
## Lisans
|
||||
|
||||
[Apache 2.0](LICENSE). Pakete dahil üçüncü taraf varlıklar [`THIRD_PARTY_LICENSES.md`](THIRD_PARTY_LICENSES.md) dosyasında listelenir.
|
||||
|
||||
<sub>JetBrains ile bağlantılı değildir. IntelliJ IDEA, JetBrains s.r.o.'nun ticari markasıdır.</sub>
|
||||
@@ -0,0 +1,88 @@
|
||||
<p align="center">
|
||||
<picture>
|
||||
<source media="(prefers-color-scheme: dark)" srcset="src-tauri/icons/source/openidea-wordmark-dark.svg">
|
||||
<img src="src-tauri/icons/source/openidea-wordmark.svg" alt="OpenIDEA" height="56">
|
||||
</picture>
|
||||
</p>
|
||||
|
||||
<p align="center">
|
||||
你熟悉的 IDE,用 Rust 重新打造。
|
||||
</p>
|
||||
|
||||
<p align="center">
|
||||
<a href="README.md">English</a> · 简体中文
|
||||
</p>
|
||||
|
||||
<p align="center">
|
||||
<a href="https://github.com/l0ng-ai/openidea/releases/latest"><b>下载</b></a>
|
||||
·
|
||||
<a href="https://openidea.sh/zh">文档</a>
|
||||
·
|
||||
<a href="#性能对比">性能对比</a>
|
||||
·
|
||||
<a href="#插件">插件</a>
|
||||
·
|
||||
<a href="#社区">社区</a>
|
||||
·
|
||||
<a href="#赞助">赞助</a>
|
||||
</p>
|
||||
|
||||
https://github.com/user-attachments/assets/234dd54c-1255-4b3a-b35c-533d78d737c0
|
||||
|
||||
OpenIDEA 把 IntelliJ IDEA 的使用方式(快捷键、工具窗口、随处搜索的习惯)带到一个以 Rust
|
||||
为核心的原生桌面应用里。
|
||||
|
||||
编辑器、代码洞察、调试器、Git、终端、数据库客户端和 HTTP 客户端,支持 60 多种语言。
|
||||
提供 macOS、Windows 和 Linux 版本。
|
||||
|
||||
## 性能对比
|
||||
|
||||
在缓存已预热的情况下打开 [freqtrade](https://github.com/freqtrade/freqtrade)(Python,约 770 个文件),
|
||||
取 5 次运行的中位数:
|
||||
|
||||
| | OpenIDEA | IntelliJ IDEA 2026.2.3 |
|
||||
| --------------------- | -------------- | ---------------------- |
|
||||
| 显示项目窗口 | **0.9 s** | 3.1 s |
|
||||
| 就绪(CPU 平稳) | **1.1 s** | 13.2 s |
|
||||
| 内存(USS,所有进程) | **169 MB** | 2011 MB |
|
||||
| 就绪前消耗的 CPU 时间 | **2.4 s** | 47.9 s |
|
||||
| 空闲 CPU | 单核的 **4 %** | 单核的 7 % |
|
||||
| 退出 | **0.3 s** | 5.9 s |
|
||||
|
||||
测试使用 `main` 分支的 OpenIDEA release 构建,以及默认设置和插件的 IDEA,环境为 Windows 11、
|
||||
32 核 AMD、62 GB 内存。IDEA 对 Python 的分析更深入,所以两边的“就绪”并不代表完成了同样多的工作。
|
||||
两者都以相同方式从外部测量:可以用 [`perf/compare`](perf/compare) 在自己的机器和项目上复现。
|
||||
|
||||
## 插件
|
||||
|
||||
插件可以添加操作、补全、快速修复、工具窗口、自定义编辑器、主题、文件图标和语言。插件以沙箱化的
|
||||
WebAssembly 组件运行;主题、图标主题和语言无需编写代码。
|
||||
|
||||
在“设置 › 插件”中安装,或用 `openidea-plugin new` 创建自己的插件:参见
|
||||
[编写 OpenIDEA 插件](https://openidea.sh/zh/plugins)和[官方插件](plugins/official)。插件 API 目前仍是预览版。
|
||||
|
||||
## 社区
|
||||
|
||||
- [Discord](https://discord.gg/2Zy4yn7gP):提问、反馈和版本发布消息。
|
||||
- 微信群,扫码加入:
|
||||
|
||||
<img src=".github/assets/wechat-group-3.png" alt="微信群二维码" width="200">
|
||||
|
||||
- 参与贡献:参见 [CONTRIBUTING.md](CONTRIBUTING.md) 和[贡献者文档](https://openidea.sh/zh/contribute/setup)。
|
||||
|
||||
## 赞助
|
||||
|
||||
如果 OpenIDEA 帮你节省了时间,可以通过支付宝或微信支付支持它的开发。
|
||||
|
||||
<table>
|
||||
<tr>
|
||||
<td align="center"><img src=".github/assets/sponsor-alipay.png" alt="支付宝收款码" width="200"><br>支付宝</td>
|
||||
<td align="center"><img src=".github/assets/sponsor-wechat.png" alt="微信支付收款码" width="200"><br>微信支付</td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
## 许可证
|
||||
|
||||
[Apache 2.0](LICENSE)。随附的第三方资源列在 [`THIRD_PARTY_LICENSES.md`](THIRD_PARTY_LICENSES.md) 中。
|
||||
|
||||
<sub>与 JetBrains 无关联。IntelliJ IDEA 是 JetBrains s.r.o. 的商标。</sub>
|
||||
@@ -1,11 +1,10 @@
|
||||
# Third-party licenses
|
||||
|
||||
## Flutter and Vite project icons
|
||||
## Project type icons
|
||||
|
||||
`src/ui/brands/flutter.svg` and `vite.svg` use the unmodified logo paths from
|
||||
`src/ui/brands/*.svg` use the unmodified logo paths from
|
||||
[Simple Icons](https://github.com/simple-icons/simple-icons), retrieved through
|
||||
Iconify (`simple-icons:flutter`, `simple-icons:vite`) and filled with the project
|
||||
icon colors. Licensed under [CC0 1.0 Universal](https://github.com/simple-icons/simple-icons/blob/develop/LICENSE.md).
|
||||
Iconify (`simple-icons:angular`, `simple-icons:astro`, `simple-icons:cplusplus`, `simple-icons:dart`, `simple-icons:django`, `simple-icons:dotnet`, `simple-icons:elixir`, `simple-icons:express`, `simple-icons:fastapi`, `simple-icons:flask`, `simple-icons:flutter`, `simple-icons:go`, `simple-icons:haskell`, `simple-icons:html5`, `simple-icons:kotlin`, `simple-icons:ktor`, `simple-icons:laravel`, `simple-icons:nestjs`, `simple-icons:nextdotjs`, `simple-icons:nodedotjs`, `simple-icons:nuxt`, `simple-icons:openjdk`, `simple-icons:phoenixframework`, `simple-icons:php`, `simple-icons:python`, `simple-icons:quarkus`, `simple-icons:ruby`, `simple-icons:rubyonrails`, `simple-icons:rust`, `simple-icons:scala`, `simple-icons:springboot`, `simple-icons:svelte`, `simple-icons:swift`, `simple-icons:symfony`, `simple-icons:vite`, `simple-icons:zig`) and filled with the brand colors (the standard icon grey for black logos). Licensed under [CC0 1.0 Universal](https://github.com/simple-icons/simple-icons/blob/develop/LICENSE.md).
|
||||
|
||||
## Project Lombok
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@ license.workspace = true
|
||||
|
||||
[dependencies]
|
||||
encoding_rs.workspace = true
|
||||
blake3.workspace = true
|
||||
ide-vfs.workspace = true
|
||||
ide-core.workspace = true
|
||||
ide-mcp.workspace = true
|
||||
@@ -24,3 +25,6 @@ dunce.workspace = true
|
||||
uuid.workspace = true
|
||||
tempfile.workspace = true
|
||||
which = "8"
|
||||
|
||||
[dev-dependencies]
|
||||
tokio = { workspace = true, features = ["test-util"] }
|
||||
@@ -0,0 +1,126 @@
|
||||
//! A short-lived ACP session to learn models, effort levels and other config options
|
||||
//! before the user's first task (Cursor and other ACP agents do not expose these in
|
||||
//! `initialize` alone).
|
||||
|
||||
use super::{Conn, Dialect};
|
||||
use crate::catalogs::Discovered;
|
||||
use crate::config;
|
||||
use crate::model::AgentConfigOption;
|
||||
use crate::provider::ProviderSpec;
|
||||
use crate::{Shared, client_info};
|
||||
use parking_lot::Mutex;
|
||||
use serde_json::{Value, json};
|
||||
use std::collections::BTreeMap;
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
|
||||
const DISCOVER_TIMEOUT: Duration = if cfg!(test) { Duration::from_secs(3) } else { Duration::from_secs(120) };
|
||||
|
||||
/// Opens `session/new`, reads `configOptions`, and records the `thought_level` choices of the
|
||||
/// session's model: they describe that model only, and other models may offer different ones.
|
||||
pub(crate) async fn run(shared: &Arc<Shared>, spec: &ProviderSpec) -> Result<Discovered, String> {
|
||||
let prefer_stored = crate::provider::prefer_stored_copilot_auth();
|
||||
match run_once(shared, spec, prefer_stored).await {
|
||||
Err(err)
|
||||
if !prefer_stored
|
||||
&& crate::provider::is_copilot_provider(&spec.id)
|
||||
&& crate::provider::has_unconfigured_copilot_ambient_token(&spec.env)
|
||||
&& super::auth_required(0, &err) =>
|
||||
{
|
||||
let retry = run_once(shared, spec, true).await;
|
||||
if retry.is_ok() {
|
||||
crate::provider::mark_prefer_stored_copilot_auth();
|
||||
}
|
||||
retry
|
||||
}
|
||||
other => other,
|
||||
}
|
||||
}
|
||||
|
||||
async fn run_once(shared: &Arc<Shared>, spec: &ProviderSpec, strip_ambient: bool) -> Result<Discovered, String> {
|
||||
let root = shared.paths.project_root.clone();
|
||||
let launch = spec.clone();
|
||||
let launch = tokio::task::spawn_blocking({
|
||||
let root = root.clone();
|
||||
move || launch.resolve_for_launch_with_copilot_env(&root, strip_ambient)
|
||||
})
|
||||
.await
|
||||
.map_err(|e| e.to_string())?;
|
||||
let cwd = dunce::canonicalize(&root).unwrap_or(root);
|
||||
let updates = Arc::new(DiscoveryUpdates { provider: spec.id.clone(), commands: Mutex::new(None) });
|
||||
let client = Dialect::wrap(updates.clone());
|
||||
let on_stderr: Box<dyn Fn(String) + Send + Sync> = Box::new(|_| {});
|
||||
let peer = (shared.connector)(&launch, &cwd, client.clone(), on_stderr)
|
||||
.map_err(|e| format!("Couldn't start {} (`{}`): {e}", launch.name, launch.command))?;
|
||||
let _stop = crate::catalogs::KillPeer(peer.clone());
|
||||
let conn = Conn::new(peer.clone(), client);
|
||||
let work = async {
|
||||
conn.initialize(&client_info()).await.map_err(|e| e.to_string())?;
|
||||
let cwd_s = cwd.to_string_lossy().into_owned();
|
||||
// No project MCP during discovery: Cursor can spend a long time spawning servers.
|
||||
let new: Value = conn
|
||||
.request("session/new", json!({ "cwd": cwd_s, "mcpServers": [] }), None)
|
||||
.await
|
||||
.map_err(|e| e.to_string())?;
|
||||
let mut options = config::options_of(&new);
|
||||
config::add_client_options(&launch.id, &mut options);
|
||||
let model = options.iter().find(|o| o.category.as_deref() == Some("model")).and_then(|o| o.current.clone());
|
||||
let levels = match (model, levels_for_model_from_slice(&options)) {
|
||||
(Some(model), Some(effort)) => BTreeMap::from([(model, effort)]),
|
||||
_ => BTreeMap::new(),
|
||||
};
|
||||
let commands = updates
|
||||
.commands
|
||||
.lock()
|
||||
.clone()
|
||||
.or_else(|| new.get("availableCommands").map(|_| config::commands_of(&new, &launch.id)));
|
||||
Ok::<_, String>(Discovered { config: options, commands, levels })
|
||||
};
|
||||
let result = tokio::time::timeout(DISCOVER_TIMEOUT, work).await;
|
||||
peer.kill();
|
||||
match result {
|
||||
Ok(result) => result,
|
||||
Err(_) => Err("the agent did not answer in time".into()),
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct DiscoveryUpdates {
|
||||
provider: String,
|
||||
commands: Mutex<Option<Vec<crate::AgentCommand>>>,
|
||||
}
|
||||
|
||||
impl ide_jsonrpc::Handler for DiscoveryUpdates {
|
||||
fn on_notification(&self, method: &str, params: Value) {
|
||||
if method == "session/update"
|
||||
&& let Some(update) = params.get("update")
|
||||
&& update["sessionUpdate"] == "available_commands_update"
|
||||
{
|
||||
// Commands are protocol data, not stderr (which may contain credentials).
|
||||
*self.commands.lock() = Some(config::commands_of(update, &self.provider));
|
||||
}
|
||||
}
|
||||
|
||||
fn on_request(&self, method: &str, _: Value) -> Result<Value, (i64, String)> {
|
||||
Err((ide_jsonrpc::codes::METHOD_NOT_FOUND, format!("unsupported discovery request {method}")))
|
||||
}
|
||||
}
|
||||
|
||||
fn levels_for_model_from_slice(options: &[AgentConfigOption]) -> Option<Vec<AgentConfigOption>> {
|
||||
let levels: Vec<_> = options
|
||||
.iter()
|
||||
.filter(|o| config::is_per_model_option(o))
|
||||
.cloned()
|
||||
.map(|mut o| {
|
||||
if config::is_context_option(&o.id) {
|
||||
if o.category.is_none() {
|
||||
o.category = Some("model_config".into());
|
||||
}
|
||||
} else if o.category.as_deref() != Some("thought_level") {
|
||||
o.category = Some("thought_level".into());
|
||||
}
|
||||
o
|
||||
})
|
||||
.collect();
|
||||
(!levels.is_empty()).then_some(levels)
|
||||
}
|
||||
@@ -107,6 +107,8 @@ fn manager(dir: &Path, agent: Arc<FakeV2Agent>) -> AgentManager {
|
||||
models_file: dir.join("models.json"),
|
||||
custom_agents_dir: None,
|
||||
home_dir: None,
|
||||
env_secrets: None,
|
||||
catalogs: Arc::default(),
|
||||
},
|
||||
tokio::runtime::Handle::current(),
|
||||
connector,
|
||||
@@ -153,6 +155,7 @@ async fn a_task_runs_on_a_v2_agent_and_resumes_its_session() {
|
||||
config: vec![AgentConfigChoice { id: "model".into(), value: "haiku".into() }],
|
||||
isolation: Isolation::Local,
|
||||
base: None,
|
||||
..Default::default()
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
@@ -14,8 +14,11 @@
|
||||
//! - Agent → client ([`Dialect`]): `session/update`s are normalized ([`normalize`]) and
|
||||
//! permission requests get the v1 `toolCall`.
|
||||
|
||||
pub(crate) mod discover;
|
||||
mod normalize;
|
||||
|
||||
pub(crate) use normalize::base64 as normalize_base64;
|
||||
|
||||
use ide_jsonrpc::{Handler, Peer, RpcError, codes};
|
||||
use normalize::Normalizer;
|
||||
use parking_lot::Mutex;
|
||||
@@ -57,6 +60,11 @@ fn client_capabilities() -> Value {
|
||||
"terminal": false,
|
||||
// Subagents as sessions of their own (ACP subagents RFD; v2 baseline).
|
||||
"subagents": {},
|
||||
// Forms the agent asks the user to fill in (`elicitation/create`); not URL mode.
|
||||
"elicitation": { "form": {} },
|
||||
// Cursor exposes model parameters (e.g. Composer `fast`) as separate config options
|
||||
// only when the client advertises this flag (undocumented Cursor ACP extension).
|
||||
"_meta": { "parameterizedModelPicker": true },
|
||||
})
|
||||
}
|
||||
|
||||
@@ -316,6 +324,13 @@ impl Handler for Dialect {
|
||||
}
|
||||
self.inner.on_request(method, params)
|
||||
}
|
||||
|
||||
fn on_request_raw(&self, method: &str, params: Value, raw: &[u8]) -> Result<Value, (i64, String, Option<Value>)> {
|
||||
if self.v2() && method == "session/request_permission" {
|
||||
return self.on_request_with_data(method, params);
|
||||
}
|
||||
self.inner.on_request_raw(method, params, raw)
|
||||
}
|
||||
}
|
||||
|
||||
/// A v2 permission request with the v1 `toolCall` (its subject's, titled with the request's title).
|
||||
|
||||
@@ -93,9 +93,87 @@ fn the_negotiating_request_carries_both_versions() {
|
||||
assert!(p["capabilities"].is_object());
|
||||
assert_eq!(p["clientCapabilities"]["subagents"], json!({}));
|
||||
assert_eq!(p["clientCapabilities"]["terminal"], false);
|
||||
assert_eq!(
|
||||
p["clientCapabilities"]["_meta"]["parameterizedModelPicker"], true,
|
||||
"Cursor and similar agents need parameterized model options (e.g. fast on/off)"
|
||||
);
|
||||
// Forms only: URL mode would open pages the agent names.
|
||||
assert_eq!(p["clientCapabilities"]["elicitation"], json!({ "form": {} }));
|
||||
// What we sent in the capture is what claude-agent-acp negotiated v2 and v1 from.
|
||||
let sent = &frames(TURN, "out")[0]["params"];
|
||||
assert_eq!(sent["clientCapabilities"], p["clientCapabilities"]);
|
||||
let sent = frames(TURN, "out")[0]["params"]["clientCapabilities"].clone();
|
||||
let mut expected = sent;
|
||||
expected["_meta"] = p["clientCapabilities"]["_meta"].clone();
|
||||
expected["elicitation"] = p["clientCapabilities"]["elicitation"].clone();
|
||||
assert_eq!(expected, p["clientCapabilities"]);
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn discovery_reads_effort_and_fast_from_session_new_then_stops_the_agent() {
|
||||
#[derive(Default)]
|
||||
struct DiscoveryAgent {
|
||||
peer: std::sync::OnceLock<Peer>,
|
||||
calls: parking_lot::Mutex<Vec<(String, Value)>>,
|
||||
}
|
||||
impl Handler for DiscoveryAgent {
|
||||
fn on_notification(&self, _: &str, _: Value) {}
|
||||
fn on_request(&self, method: &str, params: Value) -> Result<Value, (i64, String)> {
|
||||
self.calls.lock().push((method.to_owned(), params));
|
||||
match method {
|
||||
"initialize" => Ok(json!({
|
||||
"protocolVersion": 1,
|
||||
"agentCapabilities": { "loadSession": true }
|
||||
})),
|
||||
"session/new" => Ok(json!({
|
||||
"sessionId": "discover",
|
||||
"configOptions": [
|
||||
{"id": "model", "category": "model", "type": "select", "currentValue": "m1",
|
||||
"options": [{"value": "m1", "name": "One"}, {"value": "m2", "name": "Two"}]},
|
||||
{"id": "effort", "type": "select", "currentValue": "medium",
|
||||
"options": [{"value": "low"}, {"value": "medium"}, {"value": "high"}]},
|
||||
{"id": "fast", "type": "select", "currentValue": "false",
|
||||
"options": [{"value": "false", "name": "Off"}, {"value": "true", "name": "On"}]}
|
||||
]
|
||||
})),
|
||||
_ => Ok(json!({})),
|
||||
}
|
||||
}
|
||||
}
|
||||
let agent = Arc::new(DiscoveryAgent::default());
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let agent_for_connector = agent.clone();
|
||||
let connector: crate::Connector = Arc::new(move |_, _, client, _| {
|
||||
let (ca, cb) = tokio::io::duplex(1 << 20);
|
||||
let (car, caw) = tokio::io::split(ca);
|
||||
let (cbr, cbw) = tokio::io::split(cb);
|
||||
let agent_peer = Peer::connect_framed(cbr, cbw, agent_for_connector.clone(), Framing::Lines);
|
||||
let _ = agent_for_connector.peer.set(agent_peer);
|
||||
Ok(Peer::connect_framed(car, caw, client, Framing::Lines))
|
||||
});
|
||||
let mgr = crate::AgentManager::with_connector(
|
||||
crate::AgentPaths {
|
||||
project_root: tmp.path().to_path_buf(),
|
||||
store_dir: tmp.path().join("store"),
|
||||
worktrees_dir: tmp.path().join("worktrees"),
|
||||
models_file: tmp.path().join("models.json"),
|
||||
custom_agents_dir: None,
|
||||
home_dir: None,
|
||||
env_secrets: None,
|
||||
catalogs: Arc::default(),
|
||||
},
|
||||
tokio::runtime::Handle::current(),
|
||||
connector,
|
||||
);
|
||||
mgr.commands("cursor", None).await.unwrap();
|
||||
let calls = agent.calls.lock();
|
||||
assert_eq!(calls.iter().map(|(m, _)| m.as_str()).collect::<Vec<_>>(), ["initialize", "session/new"]);
|
||||
assert_eq!(calls[1].1["mcpServers"], json!([]));
|
||||
let cursor = mgr.providers().into_iter().find(|p| p.id == "cursor").unwrap();
|
||||
let fast = cursor.config.iter().find(|o| o.id == "fast").expect("fast option");
|
||||
assert_eq!(fast.category.as_deref(), Some("model_config"));
|
||||
assert_eq!(fast.current.as_deref(), Some("false"));
|
||||
// The effort option belongs to the session's model; m2 may offer other levels.
|
||||
assert_eq!(cursor.thought_levels.keys().collect::<Vec<_>>(), ["m1"]);
|
||||
assert!(cursor.thought_levels["m1"].iter().any(|o| o.category.as_deref() == Some("thought_level")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -0,0 +1,525 @@
|
||||
//! Model availability for new tasks. Sessions own selections; discovery owns catalogs.
|
||||
|
||||
use crate::{
|
||||
AgentCommand, AgentConfigOption, AgentEvent, AgentModelCatalog, Known, Shared, acp, config, native, provider,
|
||||
};
|
||||
use ide_core::{IdeError, IdeResult};
|
||||
use parking_lot::Mutex;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::{BTreeMap, HashMap};
|
||||
use std::path::PathBuf;
|
||||
use std::sync::{Arc, Weak};
|
||||
use std::time::Duration;
|
||||
use tokio::sync::watch;
|
||||
use tokio::time::Instant;
|
||||
|
||||
const FRESH_FOR: Duration = Duration::from_secs(15 * 60);
|
||||
const COOLDOWN: Duration = Duration::from_secs(30);
|
||||
|
||||
type Store = Arc<Mutex<HashMap<String, Known>>>;
|
||||
type Key = (PathBuf, PathBuf, String);
|
||||
|
||||
/// Backend-wide single-flight model lookups, scoped to project and effective launch configuration.
|
||||
#[derive(Default)]
|
||||
pub struct AgentCatalogs {
|
||||
state: Mutex<State>,
|
||||
#[cfg(test)]
|
||||
clock_ms: std::sync::atomic::AtomicU64,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct State {
|
||||
stores: HashMap<PathBuf, Store>,
|
||||
entries: HashMap<Key, Entry>,
|
||||
subscribers: Vec<Weak<Shared>>,
|
||||
generation: u64,
|
||||
}
|
||||
|
||||
struct Entry {
|
||||
// Launch credentials remain only in memory, never serialized or logged.
|
||||
context: LaunchContext,
|
||||
generation: u64,
|
||||
completed: Option<Instant>,
|
||||
successful: Option<Instant>,
|
||||
retry_at_ms: Option<f64>,
|
||||
error: Option<String>,
|
||||
flight: Option<watch::Receiver<bool>>,
|
||||
abort: Option<tokio::task::AbortHandle>,
|
||||
catalog: Option<CatalogData>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Default, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub(crate) struct CatalogData {
|
||||
pub config: Vec<AgentConfigOption>,
|
||||
pub thought_levels: BTreeMap<String, Vec<AgentConfigOption>>,
|
||||
pub refreshed_at_ms: Option<f64>,
|
||||
}
|
||||
|
||||
impl CatalogData {
|
||||
fn model_option(&self) -> Option<&AgentConfigOption> {
|
||||
self.config.iter().find(|o| o.category.as_deref() == Some("model"))
|
||||
}
|
||||
|
||||
fn lists(&self, model: &str) -> bool {
|
||||
self.model_option().is_some_and(|o| o.values.iter().any(|v| v.value == model))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub(crate) struct SavedCatalog {
|
||||
/// An opaque cryptographic digest, never raw launch arguments or credentials.
|
||||
fingerprint: String,
|
||||
#[serde(flatten)]
|
||||
catalog: CatalogData,
|
||||
}
|
||||
|
||||
#[derive(PartialEq)]
|
||||
struct LaunchContext {
|
||||
spec: provider::ProviderSpec,
|
||||
executable: PathBuf,
|
||||
modified: Option<std::time::SystemTime>,
|
||||
len: Option<u64>,
|
||||
}
|
||||
|
||||
impl LaunchContext {
|
||||
fn new(spec: provider::ProviderSpec, root: &std::path::Path) -> Self {
|
||||
let resolved = spec.clone().resolve_for_launch(root);
|
||||
let executable = dunce::canonicalize(&resolved.command).unwrap_or_else(|_| PathBuf::from(&resolved.command));
|
||||
let metadata = std::fs::metadata(&executable).ok();
|
||||
Self {
|
||||
spec,
|
||||
executable,
|
||||
modified: metadata.as_ref().and_then(|m| m.modified().ok()),
|
||||
len: metadata.as_ref().map(std::fs::Metadata::len),
|
||||
}
|
||||
}
|
||||
|
||||
fn fingerprint(&self) -> String {
|
||||
let mut hash = blake3::Hasher::new();
|
||||
fn field(hash: &mut blake3::Hasher, value: &[u8]) {
|
||||
hash.update(&(value.len() as u64).to_le_bytes());
|
||||
hash.update(value);
|
||||
}
|
||||
field(&mut hash, b"openidea-model-catalog-v1");
|
||||
field(
|
||||
&mut hash,
|
||||
match self.spec.driver {
|
||||
provider::Driver::Acp => b"acp",
|
||||
provider::Driver::Codex => b"codex",
|
||||
provider::Driver::Claude => b"claude",
|
||||
provider::Driver::Copilot => b"copilot",
|
||||
},
|
||||
);
|
||||
field(&mut hash, self.spec.command.as_bytes());
|
||||
hash.update(&(self.spec.args.len() as u64).to_le_bytes());
|
||||
for arg in &self.spec.args {
|
||||
field(&mut hash, arg.as_bytes());
|
||||
}
|
||||
hash.update(&(self.spec.env.len() as u64).to_le_bytes());
|
||||
for (name, value) in &self.spec.env {
|
||||
field(&mut hash, name.as_bytes());
|
||||
field(&mut hash, value.as_bytes());
|
||||
}
|
||||
field(&mut hash, self.executable.to_string_lossy().as_bytes());
|
||||
if let Some(modified) = self.modified.and_then(|t| t.duration_since(std::time::UNIX_EPOCH).ok()) {
|
||||
hash.update(&modified.as_nanos().to_le_bytes());
|
||||
}
|
||||
if let Some(len) = self.len {
|
||||
hash.update(&len.to_le_bytes());
|
||||
}
|
||||
hash.finalize().to_hex().to_string()
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) struct Discovered {
|
||||
pub config: Vec<AgentConfigOption>,
|
||||
pub commands: Option<Vec<AgentCommand>>,
|
||||
pub levels: BTreeMap<String, Vec<AgentConfigOption>>,
|
||||
}
|
||||
|
||||
pub(crate) struct KillPeer(pub ide_jsonrpc::Peer);
|
||||
|
||||
impl Drop for KillPeer {
|
||||
fn drop(&mut self) {
|
||||
self.0.kill();
|
||||
}
|
||||
}
|
||||
|
||||
impl AgentCatalogs {
|
||||
fn now(&self) -> Instant {
|
||||
let now = Instant::now();
|
||||
#[cfg(test)]
|
||||
let now = now + Duration::from_millis(self.clock_ms.load(std::sync::atomic::Ordering::Relaxed));
|
||||
now
|
||||
}
|
||||
|
||||
fn epoch_ms(&self) -> f64 {
|
||||
let now = crate::now_ms();
|
||||
#[cfg(test)]
|
||||
let now = now + self.clock_ms.load(std::sync::atomic::Ordering::Relaxed) as f64;
|
||||
now
|
||||
}
|
||||
|
||||
pub(crate) fn store(&self, path: &std::path::Path) -> Store {
|
||||
self.state
|
||||
.lock()
|
||||
.stores
|
||||
.entry(path.to_path_buf())
|
||||
.or_insert_with(|| {
|
||||
Arc::new(Mutex::new(
|
||||
std::fs::read(path).ok().and_then(|b| serde_json::from_slice(&b).ok()).unwrap_or_default(),
|
||||
))
|
||||
})
|
||||
.clone()
|
||||
}
|
||||
|
||||
pub(crate) fn subscribe(&self, shared: &Arc<Shared>) {
|
||||
let mut state = self.state.lock();
|
||||
state.subscribers.retain(|s| s.strong_count() > 0);
|
||||
state.subscribers.push(Arc::downgrade(shared));
|
||||
}
|
||||
|
||||
fn key(shared: &Shared, provider: &str) -> Key {
|
||||
(shared.paths.models_file.clone(), shared.paths.project_root.clone(), provider.to_owned())
|
||||
}
|
||||
|
||||
fn fallback(shared: &Shared, provider: &str) -> CatalogData {
|
||||
let cache = shared.known.lock();
|
||||
let Some(known) = cache.get(provider) else { return CatalogData::default() };
|
||||
known.catalogs.get(&shared.root_key()).map(|saved| saved.catalog.clone()).unwrap_or_else(|| CatalogData {
|
||||
config: known.config.clone(),
|
||||
thought_levels: known.thought_levels.clone(),
|
||||
refreshed_at_ms: None,
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) fn cached(&self, shared: &Shared, provider: &str) -> CatalogData {
|
||||
self.state
|
||||
.lock()
|
||||
.entries
|
||||
.get(&Self::key(shared, provider))
|
||||
.and_then(|e| e.catalog.clone())
|
||||
.unwrap_or_else(|| Self::fallback(shared, provider))
|
||||
}
|
||||
|
||||
pub(crate) fn evict_if_idle(&self, shared: &Shared, provider: &str) {
|
||||
let key = Self::key(shared, provider);
|
||||
let mut state = self.state.lock();
|
||||
if state.entries.get(&key).is_some_and(|e| e.flight.is_none()) {
|
||||
state.entries.remove(&key);
|
||||
}
|
||||
}
|
||||
|
||||
fn snapshot(&self, shared: &Shared, provider: &str, entry: &Entry) -> AgentModelCatalog {
|
||||
let catalog = entry.catalog.clone().unwrap_or_else(|| Self::fallback(shared, provider));
|
||||
AgentModelCatalog {
|
||||
provider: provider.to_owned(),
|
||||
config: shared.config_for_new_task(catalog.config, provider),
|
||||
thought_levels: catalog.thought_levels,
|
||||
refreshing: entry.flight.is_some(),
|
||||
refreshed_at_ms: catalog.refreshed_at_ms,
|
||||
retry_at_ms: entry
|
||||
.retry_at_ms
|
||||
.filter(|_| entry.completed.is_some_and(|at| self.now().duration_since(at) < COOLDOWN)),
|
||||
error: entry.error.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Start or join a lookup. No queued refresh can outlive the request that asked for it.
|
||||
pub(crate) fn request(
|
||||
self: &Arc<Self>,
|
||||
shared: &Arc<Shared>,
|
||||
spec: provider::ProviderSpec,
|
||||
force: bool,
|
||||
) -> (AgentModelCatalog, Option<watch::Receiver<bool>>) {
|
||||
let key = Self::key(shared, &spec.id);
|
||||
let context = LaunchContext::new(spec.clone(), &shared.paths.project_root);
|
||||
let now = self.now();
|
||||
let mut state = self.state.lock();
|
||||
let changed = state.entries.get(&key).is_none_or(|e| e.context != context);
|
||||
if changed {
|
||||
if let Some(abort) = state.entries.get_mut(&key).and_then(|entry| entry.abort.take()) {
|
||||
abort.abort();
|
||||
}
|
||||
state.generation += 1;
|
||||
let generation = state.generation;
|
||||
let saved =
|
||||
shared.known.lock().get(&spec.id).and_then(|known| known.catalogs.get(&shared.root_key())).cloned();
|
||||
let matching = saved.as_ref().is_some_and(|saved| saved.fingerprint == context.fingerprint());
|
||||
let age = saved
|
||||
.as_ref()
|
||||
.filter(|_| matching)
|
||||
.and_then(|saved| saved.catalog.refreshed_at_ms)
|
||||
.map(|at| self.epoch_ms() - at)
|
||||
.filter(|age| *age >= 0.0 && *age < FRESH_FOR.as_millis() as f64)
|
||||
.map(|age| Duration::from_secs_f64(age / 1000.0));
|
||||
let successful = age.and_then(|age| now.checked_sub(age));
|
||||
let completed = age.filter(|age| *age < COOLDOWN).and_then(|age| now.checked_sub(age));
|
||||
let retry_at_ms = completed
|
||||
.and_then(|_| saved.as_ref().and_then(|s| s.catalog.refreshed_at_ms))
|
||||
.map(|at| at + COOLDOWN.as_millis() as f64);
|
||||
let catalog = saved.map(|saved| {
|
||||
let mut catalog = saved.catalog;
|
||||
if !matching {
|
||||
catalog.refreshed_at_ms = None;
|
||||
}
|
||||
catalog
|
||||
});
|
||||
state.entries.insert(
|
||||
key.clone(),
|
||||
Entry {
|
||||
context,
|
||||
generation,
|
||||
completed,
|
||||
successful,
|
||||
retry_at_ms,
|
||||
catalog,
|
||||
error: None,
|
||||
flight: None,
|
||||
abort: None,
|
||||
},
|
||||
);
|
||||
}
|
||||
let entry = state.entries.get_mut(&key).expect("catalog entry");
|
||||
let cooling = entry.completed.is_some_and(|at| now.duration_since(at) < COOLDOWN);
|
||||
let fresh = entry.successful.is_some_and(|at| now.duration_since(at) < FRESH_FOR);
|
||||
let mut start = None;
|
||||
if entry.flight.is_none() && !cooling && (force || !fresh) {
|
||||
let (done, ready) = watch::channel(false);
|
||||
entry.flight = Some(ready);
|
||||
entry.error = None;
|
||||
entry.retry_at_ms = None;
|
||||
start = Some((entry.generation, done));
|
||||
}
|
||||
let snapshot = self.snapshot(shared, &spec.id, entry);
|
||||
let flight = entry.flight.clone();
|
||||
drop(state);
|
||||
if let Some((generation, done)) = start {
|
||||
self.broadcast(shared, &snapshot);
|
||||
let coordinator = self.clone();
|
||||
let shared = shared.clone();
|
||||
let flight_key = key.clone();
|
||||
let task = shared.runtime.clone().spawn(async move {
|
||||
let result = discover(&shared, spec).await;
|
||||
coordinator.complete(&shared, flight_key, generation, result);
|
||||
let _ = done.send(true);
|
||||
});
|
||||
// A subscriber can re-enter and change configuration during the start event.
|
||||
let mut state = self.state.lock();
|
||||
if let Some(entry) = state.entries.get_mut(&key).filter(|entry| entry.generation == generation) {
|
||||
if entry.flight.is_some() {
|
||||
entry.abort = Some(task.abort_handle());
|
||||
}
|
||||
} else {
|
||||
task.abort();
|
||||
}
|
||||
}
|
||||
(snapshot, flight)
|
||||
}
|
||||
|
||||
fn complete(&self, shared: &Shared, key: Key, generation: u64, result: Result<Discovered, String>) {
|
||||
let current = provider::load(shared.paths.custom_agents_dir.as_deref(), shared.env_secrets())
|
||||
.into_iter()
|
||||
.find(|spec| spec.id == key.2)
|
||||
.map(|spec| LaunchContext::new(spec, &shared.paths.project_root));
|
||||
let mut state = self.state.lock();
|
||||
let Some(entry) = state.entries.get_mut(&key).filter(|e| e.generation == generation) else { return };
|
||||
// Configuration may have changed without another refresh request arriving.
|
||||
if current.as_ref() != Some(&entry.context) {
|
||||
entry.flight = None;
|
||||
entry.abort = None;
|
||||
entry.completed = None;
|
||||
entry.retry_at_ms = None;
|
||||
entry.error = None;
|
||||
let snapshot = self.snapshot(shared, &key.2, entry);
|
||||
state.entries.remove(&key);
|
||||
drop(state);
|
||||
self.broadcast(shared, &snapshot);
|
||||
return;
|
||||
}
|
||||
let now = self.now();
|
||||
entry.completed = Some(now);
|
||||
entry.retry_at_ms = Some(self.epoch_ms() + COOLDOWN.as_millis() as f64);
|
||||
entry.flight = None;
|
||||
entry.abort = None;
|
||||
let success = match result {
|
||||
Ok(found) => {
|
||||
let mut cache = shared.known.lock();
|
||||
let known = cache.entry(key.2.clone()).or_default();
|
||||
let mut catalog = CatalogData {
|
||||
config: found.config,
|
||||
thought_levels: found.levels,
|
||||
refreshed_at_ms: Some(self.epoch_ms()),
|
||||
};
|
||||
// Efforts sessions reported for models discovery doesn't read stay while listed.
|
||||
for (model, levels) in entry.catalog.take().map(|old| old.thought_levels).unwrap_or_default() {
|
||||
if catalog.lists(&model) && !catalog.thought_levels.contains_key(&model) {
|
||||
catalog.thought_levels.insert(model, levels);
|
||||
}
|
||||
}
|
||||
known.config = catalog.config.clone();
|
||||
known.thought_levels = catalog.thought_levels.clone();
|
||||
if let Some(commands) = found.commands {
|
||||
known.commands.insert(shared.root_key(), commands);
|
||||
}
|
||||
known.authoritative = true;
|
||||
known.refreshed_at_ms = catalog.refreshed_at_ms;
|
||||
known.catalogs.insert(
|
||||
shared.root_key(),
|
||||
SavedCatalog { fingerprint: entry.context.fingerprint(), catalog: catalog.clone() },
|
||||
);
|
||||
entry.catalog = Some(catalog);
|
||||
shared.persist_known(&cache);
|
||||
entry.successful = Some(now);
|
||||
entry.error = None;
|
||||
true
|
||||
}
|
||||
Err(error) => {
|
||||
entry.error = Some(error);
|
||||
false
|
||||
}
|
||||
};
|
||||
let snapshot = self.snapshot(shared, &key.2, entry);
|
||||
drop(state);
|
||||
self.broadcast(shared, &snapshot);
|
||||
if success {
|
||||
for subscriber in self.subscribers(shared) {
|
||||
subscriber.emit(AgentEvent::Providers);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Records the efforts a session reported for a listed `model` other than the one discovery
|
||||
/// read: ACP discovery opens one session, so it knows only that model's efforts.
|
||||
pub(crate) fn learn_levels(&self, shared: &Shared, provider: &str, model: &str, levels: Vec<AgentConfigOption>) {
|
||||
let lacks = |catalog: &CatalogData| {
|
||||
catalog.lists(model)
|
||||
&& catalog.model_option().and_then(|o| o.current.as_deref()) != Some(model)
|
||||
&& catalog.thought_levels.get(model) != Some(&levels)
|
||||
};
|
||||
let key = Self::key(shared, provider);
|
||||
let mut state = self.state.lock();
|
||||
let mut learned = false;
|
||||
if let Some(catalog) = state.entries.get_mut(&key).and_then(|e| e.catalog.as_mut()).filter(|c| lacks(c)) {
|
||||
catalog.thought_levels.insert(model.to_owned(), levels.clone());
|
||||
learned = true;
|
||||
}
|
||||
let mut cache = shared.known.lock();
|
||||
if let Some(saved) = cache
|
||||
.get_mut(provider)
|
||||
.and_then(|known| known.catalogs.get_mut(&shared.root_key()))
|
||||
.filter(|saved| lacks(&saved.catalog))
|
||||
{
|
||||
saved.catalog.thought_levels.insert(model.to_owned(), levels);
|
||||
learned = true;
|
||||
}
|
||||
if !learned {
|
||||
return;
|
||||
}
|
||||
shared.persist_known(&cache);
|
||||
drop(cache);
|
||||
let snapshot = state.entries.get(&key).map(|entry| self.snapshot(shared, provider, entry));
|
||||
drop(state);
|
||||
if let Some(snapshot) = snapshot {
|
||||
self.broadcast(shared, &snapshot);
|
||||
}
|
||||
}
|
||||
|
||||
fn subscribers(&self, shared: &Shared) -> Vec<Arc<Shared>> {
|
||||
self.state
|
||||
.lock()
|
||||
.subscribers
|
||||
.iter()
|
||||
.filter_map(Weak::upgrade)
|
||||
.filter(|s| {
|
||||
s.paths.models_file == shared.paths.models_file && s.paths.project_root == shared.paths.project_root
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn broadcast(&self, shared: &Shared, catalog: &AgentModelCatalog) {
|
||||
for subscriber in self.subscribers(shared) {
|
||||
subscriber.emit(AgentEvent::ModelCatalog { catalog: catalog.clone() });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl crate::AgentManager {
|
||||
/// Return cached choices immediately and schedule discovery when eligible.
|
||||
pub fn refresh_models(&self, provider: &str, force: bool) -> IdeResult<AgentModelCatalog> {
|
||||
// Settings and environment secrets can change while this window is open.
|
||||
*self.shared.providers.write() =
|
||||
provider::load(self.shared.paths.custom_agents_dir.as_deref(), self.shared.env_secrets());
|
||||
let spec = self.shared.spec(provider).ok_or_else(|| IdeError::invalid(format!("unknown agent {provider}")))?;
|
||||
Ok(self.shared.paths.catalogs.request(&self.shared, spec, force).0)
|
||||
}
|
||||
}
|
||||
|
||||
async fn discover(shared: &Arc<Shared>, spec: provider::ProviderSpec) -> Result<Discovered, String> {
|
||||
match spec.driver {
|
||||
provider::Driver::Acp => acp::discover::run(shared, &spec).await,
|
||||
provider::Driver::Codex => native::codex::discover(spec, shared.paths.project_root.clone()).await,
|
||||
provider::Driver::Claude => {
|
||||
let found = native::claude::discover(spec.clone(), shared.paths.project_root.clone()).await?;
|
||||
Ok(Discovered {
|
||||
commands: Some(config::commands_of(
|
||||
&serde_json::json!({ "availableCommands": found.commands }),
|
||||
&spec.id,
|
||||
)),
|
||||
config: config::options_of(&serde_json::json!({ "configOptions": found.options })),
|
||||
levels: found
|
||||
.efforts
|
||||
.into_iter()
|
||||
.map(|(model, option)| {
|
||||
let levels = option
|
||||
.map(|o| config::options_of(&serde_json::json!({ "configOptions": [o] })))
|
||||
.unwrap_or_default();
|
||||
(model, levels)
|
||||
})
|
||||
.collect(),
|
||||
})
|
||||
}
|
||||
provider::Driver::Copilot => {
|
||||
if shared.uses_copilot_acp_fallback(&spec) {
|
||||
let acp_spec = native::copilot::acp_fallback_spec(&spec);
|
||||
return acp::discover::run(shared, &acp_spec).await;
|
||||
}
|
||||
match native::copilot::discover(spec.clone(), shared.paths.project_root.clone()).await {
|
||||
Ok(found) => Ok(Discovered {
|
||||
commands: Some(config::commands_of(
|
||||
&serde_json::json!({ "availableCommands": found.commands }),
|
||||
&spec.id,
|
||||
)),
|
||||
config: config::options_of(&serde_json::json!({ "configOptions": found.options })),
|
||||
levels: found
|
||||
.per_model
|
||||
.into_iter()
|
||||
.map(|(model, opts)| {
|
||||
let parsed = if opts.is_empty() {
|
||||
Vec::new()
|
||||
} else {
|
||||
config::options_of(&serde_json::json!({ "configOptions": opts }))
|
||||
};
|
||||
(model, parsed)
|
||||
})
|
||||
.collect(),
|
||||
}),
|
||||
Err(err) if err.allow_acp_fallback => {
|
||||
tracing::warn!(
|
||||
"Copilot headless catalog discovery failed ({}); falling back to `copilot --acp`",
|
||||
err.message
|
||||
);
|
||||
shared.mark_copilot_acp_fallback(&spec);
|
||||
let acp_spec = native::copilot::acp_fallback_spec(&spec);
|
||||
acp::discover::run(shared, &acp_spec).await
|
||||
}
|
||||
Err(err) => Err(err.message),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
@@ -0,0 +1,538 @@
|
||||
use super::*;
|
||||
use crate::{AgentManager, AgentPaths, Connector};
|
||||
use ide_jsonrpc::{Framing, Peer};
|
||||
use serde_json::{Value, json};
|
||||
use std::path::Path;
|
||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
|
||||
use tokio::sync::{mpsc, oneshot};
|
||||
|
||||
const PROVIDER: &str = "catalog-test";
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Pending {
|
||||
params: Value,
|
||||
reply: oneshot::Sender<Result<Value, String>>,
|
||||
}
|
||||
|
||||
struct Harness {
|
||||
connector: Connector,
|
||||
pending: mpsc::UnboundedReceiver<Pending>,
|
||||
launches: Arc<AtomicUsize>,
|
||||
live: Arc<AtomicUsize>,
|
||||
}
|
||||
|
||||
struct Live(Arc<AtomicUsize>);
|
||||
impl Drop for Live {
|
||||
fn drop(&mut self) {
|
||||
self.0.fetch_sub(1, Ordering::SeqCst);
|
||||
}
|
||||
}
|
||||
|
||||
impl Harness {
|
||||
fn new() -> Self {
|
||||
let (pending_tx, pending) = mpsc::unbounded_channel();
|
||||
let launches = Arc::new(AtomicUsize::new(0));
|
||||
let live = Arc::new(AtomicUsize::new(0));
|
||||
let connector: Connector = Arc::new({
|
||||
let launches = launches.clone();
|
||||
let live = live.clone();
|
||||
move |_, _, client, _| {
|
||||
launches.fetch_add(1, Ordering::SeqCst);
|
||||
live.fetch_add(1, Ordering::SeqCst);
|
||||
let guard = Live(live.clone());
|
||||
let pending = pending_tx.clone();
|
||||
let (client_io, server_io) = tokio::io::duplex(1 << 16);
|
||||
let (read, mut write) = tokio::io::split(server_io);
|
||||
tokio::spawn(async move {
|
||||
let _guard = guard;
|
||||
let mut lines = BufReader::new(read).lines();
|
||||
while let Some(line) = lines.next_line().await.unwrap() {
|
||||
let request: Value = serde_json::from_str(&line).unwrap();
|
||||
let id = &request["id"];
|
||||
if id.is_null() {
|
||||
continue;
|
||||
}
|
||||
let response = match request["method"].as_str().unwrap() {
|
||||
"initialize" => json!({ "id": id, "jsonrpc": "2.0", "result": {
|
||||
"protocolVersion": 1, "agentCapabilities": {}, "authMethods": [] } }),
|
||||
"session/new" => {
|
||||
let (reply, result) = oneshot::channel();
|
||||
pending.send(Pending { params: request["params"].clone(), reply }).unwrap();
|
||||
let result = tokio::select! {
|
||||
result = result => result.unwrap(),
|
||||
_ = lines.next_line() => break,
|
||||
};
|
||||
match result {
|
||||
Ok(result) => json!({ "id": id, "jsonrpc": "2.0", "result": result }),
|
||||
Err(error) => {
|
||||
json!({ "id": id, "jsonrpc": "2.0", "error": { "code": -32000, "message": error } })
|
||||
}
|
||||
}
|
||||
}
|
||||
method => panic!("discovery must not call {method}"),
|
||||
};
|
||||
if write.write_all(format!("{response}\n").as_bytes()).await.is_err() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
let (read, write) = tokio::io::split(client_io);
|
||||
Ok(Peer::connect_framed(read, write, client, Framing::Lines))
|
||||
}
|
||||
});
|
||||
Self { connector, pending, launches, live }
|
||||
}
|
||||
|
||||
async fn next(&mut self) -> Pending {
|
||||
tokio::time::timeout(Duration::from_secs(5), self.pending.recv()).await.unwrap().unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
fn configure(root: &Path, version: &str) {
|
||||
std::fs::create_dir_all(root.join("agents")).unwrap();
|
||||
let executable = root.join("catalog-test-bin");
|
||||
if !executable.exists() {
|
||||
std::fs::write(&executable, "first executable").unwrap();
|
||||
}
|
||||
std::fs::write(
|
||||
root.join("agents/catalog-test.toml"),
|
||||
format!(
|
||||
"name = \"Catalog Test\"\ncommand = {}\n[env]\nTOKEN = \"secret-{version}\"\n",
|
||||
serde_json::to_string(&executable.to_string_lossy()).unwrap()
|
||||
),
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
fn make_manager(root: &Path, catalogs: Arc<AgentCatalogs>, harness: &Harness) -> AgentManager {
|
||||
make_manager_at(root, &root.join("models.json"), catalogs, harness)
|
||||
}
|
||||
|
||||
fn make_manager_at(root: &Path, models_file: &Path, catalogs: Arc<AgentCatalogs>, harness: &Harness) -> AgentManager {
|
||||
AgentManager::with_connector(
|
||||
AgentPaths {
|
||||
project_root: root.to_path_buf(),
|
||||
store_dir: root.join("store"),
|
||||
worktrees_dir: root.join("worktrees"),
|
||||
models_file: models_file.to_path_buf(),
|
||||
catalogs,
|
||||
custom_agents_dir: Some(root.join("agents")),
|
||||
home_dir: Some(root.to_path_buf()),
|
||||
env_secrets: None,
|
||||
},
|
||||
tokio::runtime::Handle::current(),
|
||||
harness.connector.clone(),
|
||||
)
|
||||
}
|
||||
|
||||
fn events(manager: &AgentManager) -> mpsc::UnboundedReceiver<AgentEvent> {
|
||||
let (tx, rx) = mpsc::unbounded_channel();
|
||||
manager.subscribe(Arc::new(move |event| {
|
||||
let _ = tx.send(event);
|
||||
}));
|
||||
rx
|
||||
}
|
||||
|
||||
async fn completed(events: &mut mpsc::UnboundedReceiver<AgentEvent>) -> AgentModelCatalog {
|
||||
tokio::time::timeout(Duration::from_secs(5), async {
|
||||
loop {
|
||||
if let AgentEvent::ModelCatalog { catalog } = events.recv().await.unwrap()
|
||||
&& !catalog.refreshing
|
||||
{
|
||||
return catalog;
|
||||
}
|
||||
}
|
||||
})
|
||||
.await
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
fn response(models: &[&str], effort: &[&str]) -> Value {
|
||||
json!({ "sessionId": "discovery-only", "configOptions": [
|
||||
{ "id": "model", "type": "select", "category": "model", "currentValue": models.first(),
|
||||
"options": models.iter().map(|m| json!({ "value": m, "name": m })).collect::<Vec<_>>() },
|
||||
{ "id": "effort", "type": "select", "category": "thought_level", "currentValue": effort.first(),
|
||||
"options": effort.iter().map(|e| json!({ "value": e })).collect::<Vec<_>>() }
|
||||
] })
|
||||
}
|
||||
|
||||
fn model_values(catalog: &AgentModelCatalog) -> Vec<&str> {
|
||||
catalog
|
||||
.config
|
||||
.iter()
|
||||
.filter(|o| o.category.as_deref() == Some("model"))
|
||||
.flat_map(|o| o.values.iter().map(|v| v.value.as_str()))
|
||||
.collect()
|
||||
}
|
||||
|
||||
async fn stopped(harness: &Harness) {
|
||||
tokio::time::timeout(Duration::from_secs(5), async {
|
||||
while harness.live.load(Ordering::SeqCst) != 0 {
|
||||
tokio::task::yield_now().await;
|
||||
}
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
fn advance(catalogs: &AgentCatalogs, seconds: u64) {
|
||||
catalogs.clock_ms.fetch_add(seconds * 1000, Ordering::Relaxed);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn successful_refresh_replaces_models_and_efforts_and_old_sessions_cannot_revert_it() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
configure(tmp.path(), "one");
|
||||
let catalogs = Arc::new(AgentCatalogs::default());
|
||||
let mut harness = Harness::new();
|
||||
let manager = make_manager(tmp.path(), catalogs.clone(), &harness);
|
||||
let mut events = events(&manager);
|
||||
let old = config::options_of(&response(&["old", "retained"], &["low", "high"]));
|
||||
manager.shared.remember(PROVIDER, Some(&old), None, Some("old"));
|
||||
let cached = manager.refresh_models(PROVIDER, false).unwrap();
|
||||
assert!(cached.refreshing);
|
||||
assert_eq!(model_values(&cached), ["old", "retained"]);
|
||||
assert!(cached.refreshed_at_ms.is_none(), "session reports are not fresh discovery");
|
||||
let pending = harness.next().await;
|
||||
assert_eq!(pending.params["mcpServers"], json!([]));
|
||||
pending.reply.send(Ok(response(&["retained", "new"], &["medium"]))).unwrap();
|
||||
let fresh = completed(&mut events).await;
|
||||
assert_eq!(model_values(&fresh), ["retained", "new"]);
|
||||
// The session's efforts describe its own model, not "new".
|
||||
assert_eq!(fresh.thought_levels.keys().map(String::as_str).collect::<Vec<_>>(), ["retained"]);
|
||||
assert_eq!(fresh.thought_levels["retained"][0].values[0].value, "medium");
|
||||
assert!(fresh.refreshed_at_ms.is_some() && fresh.retry_at_ms.is_some());
|
||||
manager.shared.remember(PROVIDER, Some(&old), None, Some("old"));
|
||||
assert_eq!(manager.refresh_models(PROVIDER, false).unwrap().config, fresh.config);
|
||||
let provider = manager.providers().into_iter().find(|p| p.id == PROVIDER).unwrap();
|
||||
assert_eq!(provider.config, fresh.config);
|
||||
let persisted = std::fs::read_to_string(tmp.path().join("models.json")).unwrap();
|
||||
assert!(!persisted.contains("secret-one") && !persisted.contains("TOKEN"));
|
||||
let restored = make_manager(tmp.path(), Arc::default(), &harness);
|
||||
let restored_snapshot = restored.refresh_models(PROVIDER, false).unwrap();
|
||||
assert!(!restored_snapshot.refreshing, "matching persisted launch context remains fresh");
|
||||
assert_eq!(restored_snapshot.refreshed_at_ms, fresh.refreshed_at_ms);
|
||||
let provider = restored.providers().into_iter().find(|p| p.id == PROVIDER).unwrap();
|
||||
assert_eq!(provider.config, fresh.config);
|
||||
restored.shared.remember(PROVIDER, Some(&old), None, Some("old"));
|
||||
assert_eq!(restored.shared.known_config(PROVIDER), fresh.config);
|
||||
assert_eq!(restored.shared.known.lock()[PROVIDER].refreshed_at_ms, fresh.refreshed_at_ms);
|
||||
assert!(!tmp.path().join("store").exists(), "refresh never creates tasks");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_session_adds_the_efforts_of_a_model_discovery_did_not_read() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
configure(tmp.path(), "one");
|
||||
let mut harness = Harness::new();
|
||||
let catalogs = Arc::new(AgentCatalogs::default());
|
||||
let manager = make_manager(tmp.path(), catalogs.clone(), &harness);
|
||||
let mut events = events(&manager);
|
||||
manager.refresh_models(PROVIDER, false).unwrap();
|
||||
harness.next().await.reply.send(Ok(response(&["first", "second"], &["low", "high"]))).unwrap();
|
||||
completed(&mut events).await;
|
||||
// A session on "second" reports its own efforts; one on a model the catalog lacks, or on
|
||||
// "first" with other efforts, changes nothing.
|
||||
let second = config::options_of(&response(&["second"], &["off", "auto"]));
|
||||
manager.shared.remember(PROVIDER, Some(&second), None, Some("second"));
|
||||
manager.shared.remember(PROVIDER, Some(&config::options_of(&response(&["gone"], &["x"]))), None, Some("gone"));
|
||||
manager.shared.remember(PROVIDER, Some(&config::options_of(&response(&["first"], &["max"]))), None, Some("first"));
|
||||
let efforts = |catalog: &AgentModelCatalog, model: &str| -> Vec<String> {
|
||||
catalog.thought_levels[model][0].values.iter().map(|v| v.value.clone()).collect()
|
||||
};
|
||||
let catalog = manager.refresh_models(PROVIDER, false).unwrap();
|
||||
assert_eq!(catalog.thought_levels.keys().map(String::as_str).collect::<Vec<_>>(), ["first", "second"]);
|
||||
assert_eq!(efforts(&catalog, "first"), ["low", "high"]);
|
||||
assert_eq!(efforts(&catalog, "second"), ["off", "auto"]);
|
||||
let restored = make_manager(tmp.path(), Arc::default(), &harness).refresh_models(PROVIDER, false).unwrap();
|
||||
assert_eq!(efforts(&restored, "second"), ["off", "auto"], "persisted with the project's catalog");
|
||||
// Discovery again reads "first" only; "second" keeps what its session reported.
|
||||
advance(&catalogs, 60);
|
||||
while events.try_recv().is_ok() {}
|
||||
manager.refresh_models(PROVIDER, true).unwrap();
|
||||
harness.next().await.reply.send(Ok(response(&["first", "second"], &["medium"]))).unwrap();
|
||||
let fresh = completed(&mut events).await;
|
||||
assert_eq!(efforts(&fresh, "first"), ["medium"]);
|
||||
assert_eq!(efforts(&fresh, "second"), ["off", "auto"]);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn legacy_cache_loads_without_claiming_authoritative_freshness() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
configure(tmp.path(), "one");
|
||||
let options = config::options_of(&response(&["legacy"], &["low"]));
|
||||
std::fs::write(
|
||||
tmp.path().join("models.json"),
|
||||
serde_json::to_vec(&json!({
|
||||
(PROVIDER): { "config": options, "thoughtLevels": {}, "projectCommands": {} }
|
||||
}))
|
||||
.unwrap(),
|
||||
)
|
||||
.unwrap();
|
||||
let mut harness = Harness::new();
|
||||
let manager = make_manager(tmp.path(), Arc::default(), &harness);
|
||||
let mut events = events(&manager);
|
||||
let cached = manager.refresh_models(PROVIDER, false).unwrap();
|
||||
assert_eq!(model_values(&cached), ["legacy"]);
|
||||
assert!(cached.refreshing && cached.refreshed_at_ms.is_none());
|
||||
harness.next().await.reply.send(Err("Sign in required".into())).unwrap();
|
||||
let failed = completed(&mut events).await;
|
||||
assert_eq!(failed.config, cached.config);
|
||||
assert!(failed.error.as_deref().unwrap().contains("Sign in required"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn concurrent_windows_commands_and_manual_requests_share_one_flight_and_cooldown() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
configure(tmp.path(), "one");
|
||||
let catalogs = Arc::new(AgentCatalogs::default());
|
||||
let mut harness = Harness::new();
|
||||
let first = Arc::new(make_manager(tmp.path(), catalogs.clone(), &harness));
|
||||
let second = make_manager(tmp.path(), catalogs.clone(), &harness);
|
||||
let mut first_events = events(&first);
|
||||
let mut second_events = events(&second);
|
||||
assert!(first.refresh_models(PROVIDER, false).unwrap().refreshing);
|
||||
let pending = harness.next().await;
|
||||
for _ in 0..10 {
|
||||
assert!(second.refresh_models(PROVIDER, true).unwrap().refreshing);
|
||||
assert!(first.refresh_models(PROVIDER, true).unwrap().refreshing);
|
||||
}
|
||||
let commands = tokio::spawn({
|
||||
let first = first.clone();
|
||||
async move { first.commands(PROVIDER, None).await }
|
||||
});
|
||||
tokio::task::yield_now().await;
|
||||
assert_eq!(harness.launches.load(Ordering::SeqCst), 1);
|
||||
pending.reply.send(Ok(response(&["one"], &["low"]))).unwrap();
|
||||
let done = completed(&mut first_events).await;
|
||||
assert_eq!(completed(&mut second_events).await, done);
|
||||
commands.await.unwrap().unwrap();
|
||||
for force in [false, true, false, true] {
|
||||
assert!(!second.refresh_models(PROVIDER, force).unwrap().refreshing);
|
||||
}
|
||||
advance(&catalogs, 29);
|
||||
assert!(!first.refresh_models(PROVIDER, true).unwrap().refreshing);
|
||||
advance(&catalogs, 1);
|
||||
assert!(!first.refresh_models(PROVIDER, false).unwrap().refreshing, "automatic refresh remains fresh");
|
||||
assert!(first.refresh_models(PROVIDER, true).unwrap().refreshing);
|
||||
harness.next().await.reply.send(Ok(response(&["two"], &["high"]))).unwrap();
|
||||
let newer = completed(&mut first_events).await;
|
||||
assert_eq!(model_values(&newer), ["two"]);
|
||||
assert_eq!(completed(&mut second_events).await, newer);
|
||||
advance(&catalogs, 15 * 60 - 1);
|
||||
assert!(!second.refresh_models(PROVIDER, false).unwrap().refreshing);
|
||||
advance(&catalogs, 1);
|
||||
assert!(second.refresh_models(PROVIDER, false).unwrap().refreshing);
|
||||
harness.next().await.reply.send(Ok(response(&["three"], &["low"]))).unwrap();
|
||||
completed(&mut first_events).await;
|
||||
assert_eq!(harness.launches.load(Ordering::SeqCst), 3);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn failure_keeps_successful_choices_and_throttles_manual_and_auto_retries() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
configure(tmp.path(), "one");
|
||||
let catalogs = Arc::new(AgentCatalogs::default());
|
||||
let mut harness = Harness::new();
|
||||
let manager = make_manager(tmp.path(), catalogs.clone(), &harness);
|
||||
let mut events = events(&manager);
|
||||
manager.refresh_models(PROVIDER, true).unwrap();
|
||||
harness.next().await.reply.send(Ok(response(&["cached"], &["low"]))).unwrap();
|
||||
let success = completed(&mut events).await;
|
||||
let disk = std::fs::read(tmp.path().join("models.json")).unwrap();
|
||||
advance(&catalogs, 15 * 60);
|
||||
manager.refresh_models(PROVIDER, false).unwrap();
|
||||
harness.next().await.reply.send(Err("Authentication expired".into())).unwrap();
|
||||
let failed = completed(&mut events).await;
|
||||
assert_eq!(failed.config, success.config);
|
||||
assert_eq!(failed.thought_levels, success.thought_levels);
|
||||
assert_eq!(failed.refreshed_at_ms, success.refreshed_at_ms);
|
||||
assert!(failed.error.as_deref().unwrap().contains("Authentication expired"));
|
||||
assert_eq!(std::fs::read(tmp.path().join("models.json")).unwrap(), disk);
|
||||
assert!(!manager.refresh_models(PROVIDER, false).unwrap().refreshing);
|
||||
assert!(!manager.refresh_models(PROVIDER, true).unwrap().refreshing);
|
||||
advance(&catalogs, 29);
|
||||
assert!(!manager.refresh_models(PROVIDER, true).unwrap().refreshing);
|
||||
advance(&catalogs, 1);
|
||||
assert!(manager.refresh_models(PROVIDER, false).unwrap().refreshing);
|
||||
harness.next().await.reply.send(Ok(response(&["recovered"], &["high"]))).unwrap();
|
||||
let recovered = completed(&mut events).await;
|
||||
assert_eq!(model_values(&recovered), ["recovered"]);
|
||||
assert!(recovered.error.is_none());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn timeout_cleans_up_and_enters_the_same_failure_cooldown() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
configure(tmp.path(), "one");
|
||||
let catalogs = Arc::new(AgentCatalogs::default());
|
||||
let mut harness = Harness::new();
|
||||
let manager = make_manager(tmp.path(), catalogs.clone(), &harness);
|
||||
let mut events = events(&manager);
|
||||
manager.refresh_models(PROVIDER, false).unwrap();
|
||||
let pending = harness.next().await;
|
||||
tokio::time::pause();
|
||||
tokio::time::advance(Duration::from_secs(4)).await;
|
||||
let failed = completed(&mut events).await;
|
||||
assert!(failed.error.as_deref().unwrap().contains("time"));
|
||||
assert!(failed.retry_at_ms.is_some());
|
||||
assert!(!manager.refresh_models(PROVIDER, true).unwrap().refreshing);
|
||||
assert!(!manager.refresh_models(PROVIDER, false).unwrap().refreshing);
|
||||
tokio::time::resume();
|
||||
stopped(&harness).await;
|
||||
assert_eq!(harness.live.load(Ordering::SeqCst), 0, "timed-out connection must close");
|
||||
assert!(pending.reply.is_closed());
|
||||
advance(&catalogs, 30);
|
||||
assert!(manager.refresh_models(PROVIDER, false).unwrap().refreshing);
|
||||
harness.next().await.reply.send(Ok(response(&["retry"], &["low"]))).unwrap();
|
||||
completed(&mut events).await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn changed_effective_config_supersedes_old_completion_without_queueing() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
configure(tmp.path(), "one");
|
||||
let catalogs = Arc::new(AgentCatalogs::default());
|
||||
let mut harness = Harness::new();
|
||||
let first = make_manager(tmp.path(), catalogs.clone(), &harness);
|
||||
let second = make_manager(tmp.path(), catalogs.clone(), &harness);
|
||||
let mut events = events(&second);
|
||||
first.refresh_models(PROVIDER, false).unwrap();
|
||||
let obsolete = harness.next().await;
|
||||
configure(tmp.path(), "two");
|
||||
assert!(second.refresh_models(PROVIDER, false).unwrap().refreshing);
|
||||
let latest = harness.next().await;
|
||||
latest.reply.send(Ok(response(&["latest"], &["high"]))).unwrap();
|
||||
let fresh = completed(&mut events).await;
|
||||
let _ = obsolete.reply.send(Ok(response(&["obsolete"], &["low"])));
|
||||
stopped(&harness).await;
|
||||
let shown = first.refresh_models(PROVIDER, false).unwrap();
|
||||
assert_eq!(shown.config, fresh.config);
|
||||
assert_eq!(shown.refreshed_at_ms, fresh.refreshed_at_ms);
|
||||
assert!(!shown.refreshing);
|
||||
assert_eq!(harness.launches.load(Ordering::SeqCst), 2);
|
||||
assert!(!std::fs::read_to_string(tmp.path().join("models.json")).unwrap().contains("obsolete"));
|
||||
// A changed configuration also invalidates successful freshness/cooldown.
|
||||
configure(tmp.path(), "three");
|
||||
assert!(first.refresh_models(PROVIDER, false).unwrap().refreshing);
|
||||
harness.next().await.reply.send(Ok(response(&["new-config"], &["medium"]))).unwrap();
|
||||
completed(&mut events).await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn invalid_provider_is_a_request_error_and_does_not_launch() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let harness = Harness::new();
|
||||
let manager = make_manager(tmp.path(), Arc::default(), &harness);
|
||||
assert!(manager.refresh_models("missing-provider", true).is_err());
|
||||
assert_eq!(harness.launches.load(Ordering::SeqCst), 0);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn replacing_the_executable_at_the_same_path_invalidates_freshness_and_obsolete_responses() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
configure(tmp.path(), "unchanged-environment");
|
||||
let catalogs = Arc::new(AgentCatalogs::default());
|
||||
let mut harness = Harness::new();
|
||||
let manager = make_manager(tmp.path(), catalogs, &harness);
|
||||
let mut events = events(&manager);
|
||||
manager.refresh_models(PROVIDER, false).unwrap();
|
||||
let obsolete = harness.next().await;
|
||||
std::fs::write(tmp.path().join("catalog-test-bin"), "replacement executable with different size").unwrap();
|
||||
manager.refresh_models(PROVIDER, false).unwrap();
|
||||
let latest = harness.next().await;
|
||||
latest.reply.send(Ok(response(&["updated"], &["high"]))).unwrap();
|
||||
let fresh = completed(&mut events).await;
|
||||
let _ = obsolete.reply.send(Ok(response(&["obsolete"], &["low"])));
|
||||
stopped(&harness).await;
|
||||
assert_eq!(manager.refresh_models(PROVIDER, false).unwrap().config, fresh.config);
|
||||
std::fs::write(tmp.path().join("catalog-test-bin"), "another executable").unwrap();
|
||||
assert!(manager.refresh_models(PROVIDER, false).unwrap().refreshing);
|
||||
harness.next().await.reply.send(Ok(response(&["new-install"], &["medium"]))).unwrap();
|
||||
assert_eq!(model_values(&completed(&mut events).await), ["new-install"]);
|
||||
assert_eq!(harness.launches.load(Ordering::SeqCst), 3);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn project_catalogs_and_persisted_freshness_do_not_leak_between_contexts() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let a = tmp.path().join("project-a");
|
||||
let b = tmp.path().join("project-b");
|
||||
configure(&a, "account-a");
|
||||
configure(&b, "account-b");
|
||||
let models_file = tmp.path().join("shared-models.json");
|
||||
let catalogs = Arc::new(AgentCatalogs::default());
|
||||
let mut harness = Harness::new();
|
||||
let first = make_manager_at(&a, &models_file, catalogs.clone(), &harness);
|
||||
let second = make_manager_at(&b, &models_file, catalogs, &harness);
|
||||
let mut first_events = events(&first);
|
||||
let mut second_events = events(&second);
|
||||
first.refresh_models(PROVIDER, false).unwrap();
|
||||
harness.next().await.reply.send(Ok(response(&["account-a-model"], &["low"]))).unwrap();
|
||||
let a_catalog = completed(&mut first_events).await;
|
||||
second.refresh_models(PROVIDER, false).unwrap();
|
||||
harness.next().await.reply.send(Ok(response(&["account-b-model"], &["high"]))).unwrap();
|
||||
let b_catalog = completed(&mut second_events).await;
|
||||
assert_eq!(first.refresh_models(PROVIDER, false).unwrap().config, a_catalog.config);
|
||||
assert_eq!(second.refresh_models(PROVIDER, false).unwrap().config, b_catalog.config);
|
||||
assert_eq!(first.providers().into_iter().find(|p| p.id == PROVIDER).unwrap().config, a_catalog.config);
|
||||
for (root, expected) in [(&a, &a_catalog), (&b, &b_catalog)] {
|
||||
let restored = make_manager_at(root, &models_file, Arc::default(), &harness);
|
||||
let cached = restored.refresh_models(PROVIDER, false).unwrap();
|
||||
assert!(!cached.refreshing, "matching persisted context should retain freshness");
|
||||
assert_eq!(cached.config, expected.config);
|
||||
assert_eq!(cached.refreshed_at_ms, expected.refreshed_at_ms);
|
||||
}
|
||||
assert_eq!(harness.launches.load(Ordering::SeqCst), 2);
|
||||
configure(&a, "new-account");
|
||||
let changed = make_manager_at(&a, &models_file, Arc::default(), &harness);
|
||||
let mut events = events(&changed);
|
||||
let stale = changed.refresh_models(PROVIDER, false).unwrap();
|
||||
assert!(stale.refreshing, "a different launch environment cannot reuse persisted freshness");
|
||||
assert!(stale.refreshed_at_ms.is_none());
|
||||
assert_eq!(stale.config, a_catalog.config, "stale cached choices remain selectable");
|
||||
harness.next().await.reply.send(Ok(response(&["new-account-model"], &["medium"]))).unwrap();
|
||||
completed(&mut events).await;
|
||||
let persisted = std::fs::read_to_string(models_file).unwrap();
|
||||
assert!(!persisted.contains("secret-") && !persisted.contains("TOKEN"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn configuration_changed_without_another_request_also_discards_old_discovery() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
configure(tmp.path(), "before");
|
||||
let mut harness = Harness::new();
|
||||
let manager = make_manager(tmp.path(), Arc::default(), &harness);
|
||||
let mut events = events(&manager);
|
||||
manager.refresh_models(PROVIDER, false).unwrap();
|
||||
let obsolete = harness.next().await;
|
||||
configure(tmp.path(), "after");
|
||||
obsolete.reply.send(Ok(response(&["obsolete"], &["low"]))).unwrap();
|
||||
let discarded = completed(&mut events).await;
|
||||
assert!(model_values(&discarded).is_empty());
|
||||
assert!(discarded.refreshed_at_ms.is_none());
|
||||
assert!(!tmp.path().join("models.json").exists());
|
||||
manager.refresh_models(PROVIDER, false).unwrap();
|
||||
harness.next().await.reply.send(Ok(response(&["current"], &["high"]))).unwrap();
|
||||
assert_eq!(model_values(&completed(&mut events).await), ["current"]);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn successful_empty_catalog_removes_all_previous_models_and_efforts() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
configure(tmp.path(), "one");
|
||||
let catalogs = Arc::new(AgentCatalogs::default());
|
||||
let mut harness = Harness::new();
|
||||
let manager = make_manager(tmp.path(), catalogs.clone(), &harness);
|
||||
let mut events = events(&manager);
|
||||
manager.refresh_models(PROVIDER, true).unwrap();
|
||||
harness.next().await.reply.send(Ok(response(&["removed"], &["low"]))).unwrap();
|
||||
completed(&mut events).await;
|
||||
advance(&catalogs, 30);
|
||||
manager.refresh_models(PROVIDER, true).unwrap();
|
||||
harness.next().await.reply.send(Ok(response(&[], &[]))).unwrap();
|
||||
let empty = completed(&mut events).await;
|
||||
assert!(model_values(&empty).is_empty() && empty.thought_levels.is_empty());
|
||||
assert!(empty.refreshed_at_ms.is_some() && empty.error.is_none());
|
||||
}
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
use crate::model::{
|
||||
AgentCommand, AgentCommandSource, AgentConfigOption, AgentConfigValue, AgentConfigVia, AgentCost, AgentLimit,
|
||||
AgentUsage,
|
||||
AgentModelPricing, AgentUsage,
|
||||
};
|
||||
use serde_json::{Value, json};
|
||||
|
||||
@@ -20,17 +20,24 @@ pub(crate) fn add_client_options(provider: &str, config: &mut Vec<AgentConfigOpt
|
||||
description: Some("Tool approvals for this Cursor conversation".into()),
|
||||
category: Some("approval".into()),
|
||||
current: Some("ask".into()),
|
||||
default_value: Some("ask".into()),
|
||||
via: AgentConfigVia::Option,
|
||||
values: vec![
|
||||
AgentConfigValue {
|
||||
value: "ask".into(),
|
||||
name: "Ask for approval".into(),
|
||||
description: Some("Show permission requests from Cursor".into()),
|
||||
cost_category: None,
|
||||
usage_multiplier: None,
|
||||
pricing: None,
|
||||
},
|
||||
AgentConfigValue {
|
||||
value: "bypass".into(),
|
||||
name: "Bypass approvals".into(),
|
||||
description: Some("Automatically approve tool requests in this conversation".into()),
|
||||
cost_category: None,
|
||||
usage_multiplier: None,
|
||||
pricing: None,
|
||||
},
|
||||
],
|
||||
});
|
||||
@@ -42,9 +49,38 @@ pub(crate) fn bypass_approvals(config: &[AgentConfigOption]) -> bool {
|
||||
let selected = |category: &str| {
|
||||
config.iter().find(|o| o.category.as_deref() == Some(category)).and_then(|o| o.current.as_deref())
|
||||
};
|
||||
selected("mode").is_some_and(is_bypass_mode)
|
||||
|| selected("permissions").is_some_and(|v| matches!(v.to_ascii_lowercase().as_str(), "on" | "true"))
|
||||
|| selected("approval").is_some_and(|v| v.eq_ignore_ascii_case("bypass"))
|
||||
["mode", "permissions", "approval"]
|
||||
.into_iter()
|
||||
.any(|category| selected(category).zip(bypass_value(category)).is_some_and(|(value, bypass)| bypass(value)))
|
||||
}
|
||||
|
||||
/// For an option of `category` that can approve everything, what tells its approving values.
|
||||
fn bypass_value(category: &str) -> Option<fn(&str) -> bool> {
|
||||
match category {
|
||||
"mode" => Some(is_bypass_mode),
|
||||
"permissions" => Some(|v| matches!(v.to_ascii_lowercase().as_str(), "on" | "true")),
|
||||
"approval" => Some(|v| v.eq_ignore_ascii_case("bypass")),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Moves options that approve everything without asking to a value that asks: the agent's
|
||||
/// default or ask mode, else its first one that asks.
|
||||
pub(crate) fn ask_for_approvals(config: &mut [AgentConfigOption]) {
|
||||
for option in config {
|
||||
let Some(bypass) = option.category.as_deref().and_then(bypass_value) else { continue };
|
||||
if option.current.as_deref().is_some_and(|c| !bypass(c)) {
|
||||
continue;
|
||||
}
|
||||
let asks = |v: &&AgentConfigValue| !bypass(&v.value);
|
||||
let value = ["default", "ask"]
|
||||
.iter()
|
||||
.find_map(|preferred| option.values.iter().filter(asks).find(|v| v.value == *preferred))
|
||||
.or_else(|| option.values.iter().find(asks));
|
||||
if let Some(value) = value {
|
||||
option.current = Some(value.value.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The mode that approves everything without asking (`bypassPermissions`, `yolo`, `full-access`…), if the agent has one.
|
||||
@@ -89,6 +125,18 @@ pub(crate) fn options_of(result: &Value) -> Vec<AgentConfigOption> {
|
||||
out
|
||||
}
|
||||
|
||||
pub(crate) fn is_context_option(id: &str) -> bool {
|
||||
matches!(id, "context_tier" | "contextTier")
|
||||
}
|
||||
|
||||
/// Whether an option's available values and default belong to the selected model
|
||||
/// (`thought_level` and model context-tier options) rather than the provider as a whole.
|
||||
pub(crate) fn is_per_model_option(o: &AgentConfigOption) -> bool {
|
||||
matches!(o.category.as_deref(), Some("thought_level" | "context_tier"))
|
||||
|| matches!(o.id.as_str(), "effort" | "reasoning_effort" | "thinking" | "reasoning")
|
||||
|| is_context_option(&o.id)
|
||||
}
|
||||
|
||||
/// A `configOptions` array; only select options are kept (the UI shows them as dropdowns).
|
||||
pub(crate) fn parse_options(options: &Value) -> Vec<AgentConfigOption> {
|
||||
let Some(list) = options.as_array() else { return Vec::new() };
|
||||
@@ -104,11 +152,22 @@ pub(crate) fn parse_options(options: &Value) -> Vec<AgentConfigOption> {
|
||||
None => values.extend(value_of(v)),
|
||||
}
|
||||
}
|
||||
let current = str_of(o, "currentValue");
|
||||
let default_value = str_of(o, "defaultValue").or_else(|| current.clone());
|
||||
Some(AgentConfigOption {
|
||||
name: str_of(o, "name").unwrap_or_else(|| id.clone()),
|
||||
description: str_of(o, "description"),
|
||||
category: str_of(o, "category").or_else(|| matches!(id.as_str(), "model" | "mode").then(|| id.clone())),
|
||||
current: str_of(o, "currentValue"),
|
||||
category: str_of(o, "category").or_else(|| {
|
||||
matches!(id.as_str(), "model" | "mode")
|
||||
.then(|| id.clone())
|
||||
.or_else(|| {
|
||||
matches!(id.as_str(), "effort" | "reasoning_effort" | "thinking" | "reasoning")
|
||||
.then(|| "thought_level".into())
|
||||
})
|
||||
.or_else(|| (id == "fast" || is_context_option(&id)).then(|| "model_config".into()))
|
||||
}),
|
||||
current,
|
||||
default_value,
|
||||
values,
|
||||
via: AgentConfigVia::Option,
|
||||
id,
|
||||
@@ -118,9 +177,24 @@ pub(crate) fn parse_options(options: &Value) -> Vec<AgentConfigOption> {
|
||||
}
|
||||
|
||||
/// A `config_option_update`: its options replace the config-option ones; `modes` / `models`
|
||||
/// stand-ins stay.
|
||||
/// stand-ins stay, and initial `default_value`s survive same-model option edits for Reset.
|
||||
pub(crate) fn merge_update(config: &mut Vec<AgentConfigOption>, options: &Value) {
|
||||
let fresh = parse_options(options);
|
||||
let mut fresh = parse_options(options);
|
||||
let old_model = config.iter().find(|o| o.category.as_deref() == Some("model")).and_then(|o| o.current.as_deref());
|
||||
let new_model = fresh.iter().find(|o| o.category.as_deref() == Some("model")).and_then(|o| o.current.as_deref());
|
||||
let same_model = old_model == new_model;
|
||||
for f in &mut fresh {
|
||||
if f.default_value == f.current
|
||||
&& (same_model || !is_per_model_option(f))
|
||||
&& let Some(prev_default) = config
|
||||
.iter()
|
||||
.find(|p| p.id == f.id)
|
||||
.and_then(|p| p.default_value.as_ref())
|
||||
.filter(|def| f.values.iter().any(|v| v.value == **def))
|
||||
{
|
||||
f.default_value = Some(prev_default.clone());
|
||||
}
|
||||
}
|
||||
let covered = |o: &AgentConfigOption| fresh.iter().any(|f| f.category.is_some() && f.category == o.category);
|
||||
config.retain(|o| o.id == CURSOR_APPROVAL || (o.via != AgentConfigVia::Option && !covered(o)));
|
||||
let legacy = std::mem::take(config);
|
||||
@@ -214,16 +288,21 @@ fn legacy(
|
||||
Some(AgentConfigValue {
|
||||
name: str_of(m, "name").unwrap_or_else(|| value.clone()),
|
||||
description: str_of(m, "description"),
|
||||
cost_category: None,
|
||||
usage_multiplier: None,
|
||||
pricing: pricing_of(m),
|
||||
value,
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
let current = str_of(v, current);
|
||||
AgentConfigOption {
|
||||
id: id.to_owned(),
|
||||
name: name.to_owned(),
|
||||
description: None,
|
||||
category: Some(id.to_owned()),
|
||||
current: str_of(v, current),
|
||||
default_value: current.clone(),
|
||||
current,
|
||||
values,
|
||||
via,
|
||||
}
|
||||
@@ -231,13 +310,35 @@ fn legacy(
|
||||
|
||||
fn value_of(v: &Value) -> Option<AgentConfigValue> {
|
||||
let value = v.get("value")?.as_str()?.to_owned();
|
||||
let meta = v.get("_meta");
|
||||
let cost_category = meta
|
||||
.and_then(|m| m.get("copilotPriceCategory").or_else(|| m.get("priceCategory")))
|
||||
.and_then(Value::as_str)
|
||||
.map(|s| s.trim().to_ascii_lowercase())
|
||||
.filter(|s| !s.is_empty());
|
||||
let usage_multiplier = meta
|
||||
.and_then(|m| m.get("copilotUsage").or_else(|| m.get("usageMultiplier")))
|
||||
.and_then(Value::as_str)
|
||||
.map(str::trim)
|
||||
.filter(|s| !s.is_empty())
|
||||
.map(str::to_owned);
|
||||
Some(AgentConfigValue {
|
||||
name: str_of(v, "name").unwrap_or_else(|| value.clone()),
|
||||
description: str_of(v, "description"),
|
||||
cost_category,
|
||||
usage_multiplier,
|
||||
pricing: pricing_of(v),
|
||||
value,
|
||||
})
|
||||
}
|
||||
|
||||
fn pricing_of(v: &Value) -> Option<Box<AgentModelPricing>> {
|
||||
v.pointer("/_meta/pricing")
|
||||
.or_else(|| v.get("pricing"))
|
||||
.and_then(|p| serde_json::from_value(p.clone()).ok())
|
||||
.map(Box::new)
|
||||
}
|
||||
|
||||
fn str_of(v: &Value, key: &str) -> Option<String> {
|
||||
v.get(key).and_then(Value::as_str).map(str::to_owned)
|
||||
}
|
||||
@@ -333,6 +434,87 @@ mod tests {
|
||||
assert!(bypass_approvals(&url_mode));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn asking_for_approvals_keeps_a_mode_that_asks_and_leaves_one_that_does_not() {
|
||||
let mode = |current: &str, values: &[&str]| {
|
||||
options_of(&json!({"modes": {"currentModeId": current,
|
||||
"availableModes": values.iter().map(|v| json!({"id": v})).collect::<Vec<_>>()}}))
|
||||
};
|
||||
let codex = ["read-only", "ask", "auto", "full-access"];
|
||||
for (current, expected) in [("auto", "auto"), ("read-only", "read-only"), ("full-access", "ask")] {
|
||||
let mut c = mode(current, &codex);
|
||||
ask_for_approvals(&mut c);
|
||||
assert_eq!(c[0].current.as_deref(), Some(expected), "{current}");
|
||||
assert!(!bypass_approvals(&c));
|
||||
}
|
||||
let mut claude = mode("bypassPermissions", &["acceptEdits", "default", "plan", "bypassPermissions"]);
|
||||
ask_for_approvals(&mut claude);
|
||||
assert_eq!(claude[0].current.as_deref(), Some("default"));
|
||||
let mut other = mode("yolo", &["yolo", "code", "architect"]);
|
||||
ask_for_approvals(&mut other);
|
||||
assert_eq!(other[0].current.as_deref(), Some("code"));
|
||||
|
||||
let mut c = options_of(&json!({"configOptions": [
|
||||
{"id": "allow_all", "category": "permissions", "type": "select", "currentValue": "on",
|
||||
"options": [{"value": "on"}, {"value": "off"}]},
|
||||
{"id": "approval", "category": "approval", "type": "select", "currentValue": "bypass",
|
||||
"options": [{"value": "bypass"}, {"value": "ask"}]}
|
||||
]}));
|
||||
ask_for_approvals(&mut c);
|
||||
assert_eq!(ids(&c), [("allow_all", Some("off")), ("approval", Some("ask"))]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn effort_and_fast_categories_are_inferred_from_ids() {
|
||||
let mut c = options_of(&json!({
|
||||
"configOptions": [
|
||||
{"id": "model", "type": "select", "currentValue": "gemini-4-carbon",
|
||||
"options": [{"value": "gemini-4-carbon", "name": "Gemini 4 Carbon",
|
||||
"description": "Powerful · Medium cost",
|
||||
"_meta": {"copilotPriceCategory": "Medium", "copilotUsage": "1x"}}]},
|
||||
{"id": "effort", "type": "select", "currentValue": "medium",
|
||||
"options": [{"value": "low"}, {"value": "medium"}, {"value": "high"}]},
|
||||
{"id": "contextTier", "type": "select", "currentValue": "default",
|
||||
"options": [{"value": "default", "name": "200K"}, {"value": "long_context", "name": "1M"}]},
|
||||
{"id": "fast", "type": "select", "currentValue": "false",
|
||||
"options": [{"value": "false", "name": "Off"}, {"value": "true", "name": "On"}]}
|
||||
]
|
||||
}));
|
||||
assert_eq!(c.len(), 4);
|
||||
assert_eq!(c[0].values[0].cost_category.as_deref(), Some("medium"));
|
||||
assert_eq!(c[0].values[0].usage_multiplier.as_deref(), Some("1x"));
|
||||
assert_eq!(c[1].category.as_deref(), Some("thought_level"));
|
||||
assert_eq!(c[1].default_value.as_deref(), Some("medium"));
|
||||
assert_eq!(c[2].category.as_deref(), Some("model_config"));
|
||||
assert!(is_per_model_option(&c[1]) && is_per_model_option(&c[2]) && !is_per_model_option(&c[3]));
|
||||
|
||||
// Editing effort on the same model preserves its initial `default_value` for Reset.
|
||||
merge_update(
|
||||
&mut c,
|
||||
&json!([
|
||||
{"id": "model", "type": "select", "currentValue": "gemini-4-carbon",
|
||||
"options": [{"value": "gemini-4-carbon"}]},
|
||||
{"id": "effort", "type": "select", "currentValue": "high",
|
||||
"options": [{"value": "low"}, {"value": "medium"}, {"value": "high"}]}
|
||||
]),
|
||||
);
|
||||
let effort = c.iter().find(|o| o.id == "effort").unwrap();
|
||||
assert_eq!((effort.current.as_deref(), effort.default_value.as_deref()), (Some("high"), Some("medium")));
|
||||
|
||||
// Switching models refreshes per-model `default_value` to the new model's default.
|
||||
merge_update(
|
||||
&mut c,
|
||||
&json!([
|
||||
{"id": "model", "type": "select", "currentValue": "gpt-6-sol",
|
||||
"options": [{"value": "gemini-4-carbon"}, {"value": "gpt-6-sol"}]},
|
||||
{"id": "effort", "type": "select", "currentValue": "low",
|
||||
"options": [{"value": "low"}, {"value": "medium"}, {"value": "high"}]}
|
||||
]),
|
||||
);
|
||||
let effort = c.iter().find(|o| o.id == "effort").unwrap();
|
||||
assert_eq!((effort.current.as_deref(), effort.default_value.as_deref()), (Some("low"), Some("low")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn command_sources_in_updates_and_stored_tasks() {
|
||||
let update = json!({"availableCommands": [
|
||||
|
||||
@@ -0,0 +1,305 @@
|
||||
//! ACP elicitation in form mode: the agent's restricted JSON Schema as form fields, and the
|
||||
//! user's answer as the `elicitation/create` response.
|
||||
|
||||
use crate::model::{AgentForm, AgentFormField, AgentFormFieldKind, AgentFormOption, AgentPermissionOption};
|
||||
use serde::Deserialize;
|
||||
use serde::de::{Deserializer, IgnoredAny, MapAccess, Visitor};
|
||||
use serde_json::{Map, Value, json};
|
||||
|
||||
/// The reply that declines the form; a JSON object of field name to values accepts it, and
|
||||
/// anything else cancels it.
|
||||
const DECLINE: &str = "decline";
|
||||
|
||||
/// The form `elicitation/create` asks for; `Err` when it asks for something else.
|
||||
pub(crate) fn form_of(params: &Value) -> Result<AgentForm, String> {
|
||||
if params.get("mode").and_then(Value::as_str) != Some("form") {
|
||||
return Err("only form elicitation is supported".to_owned());
|
||||
}
|
||||
let schema = params.get("requestedSchema").ok_or("requestedSchema is missing")?;
|
||||
let required: Vec<&str> = schema
|
||||
.get("required")
|
||||
.and_then(Value::as_array)
|
||||
.map(|names| names.iter().filter_map(Value::as_str).collect())
|
||||
.unwrap_or_default();
|
||||
let properties = schema.get("properties").and_then(Value::as_object).ok_or("requestedSchema has no properties")?;
|
||||
let fields = properties
|
||||
.iter()
|
||||
.map(|(name, property)| field(name, property, required.contains(&name.as_str())))
|
||||
.collect::<Result<_, _>>()?;
|
||||
Ok(AgentForm { fields })
|
||||
}
|
||||
|
||||
/// Orders the fields as the request's schema lists them: `Value` keeps object keys sorted, and
|
||||
/// the agent's order is the one its form reads in.
|
||||
pub(crate) fn in_schema_order(form: &mut AgentForm, raw: &[u8]) {
|
||||
#[derive(Deserialize)]
|
||||
struct Request {
|
||||
params: Params,
|
||||
}
|
||||
#[derive(Deserialize)]
|
||||
struct Params {
|
||||
#[serde(rename = "requestedSchema")]
|
||||
schema: Schema,
|
||||
}
|
||||
#[derive(Deserialize)]
|
||||
struct Schema {
|
||||
properties: Keys,
|
||||
}
|
||||
struct Keys(Vec<String>);
|
||||
impl<'de> Deserialize<'de> for Keys {
|
||||
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
|
||||
struct Visit;
|
||||
impl<'de> Visitor<'de> for Visit {
|
||||
type Value = Keys;
|
||||
fn expecting(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
f.write_str("an object")
|
||||
}
|
||||
fn visit_map<A: MapAccess<'de>>(self, mut map: A) -> Result<Keys, A::Error> {
|
||||
let mut keys = Vec::new();
|
||||
while let Some((key, IgnoredAny)) = map.next_entry::<String, IgnoredAny>()? {
|
||||
keys.push(key);
|
||||
}
|
||||
Ok(Keys(keys))
|
||||
}
|
||||
}
|
||||
deserializer.deserialize_map(Visit)
|
||||
}
|
||||
}
|
||||
let Ok(request) = serde_json::from_slice::<Request>(raw) else { return };
|
||||
let order = request.params.schema.properties.0;
|
||||
form.fields.sort_by_key(|field| order.iter().position(|name| *name == field.name).unwrap_or(usize::MAX));
|
||||
}
|
||||
|
||||
fn field(name: &str, property: &Value, required: bool) -> Result<AgentFormField, String> {
|
||||
let text = |key: &str| property.get(key).and_then(Value::as_str).unwrap_or_default().trim().to_owned();
|
||||
let number = |key: &str| property.get(key).and_then(Value::as_f64);
|
||||
let unsupported = || format!("field {name} has an unsupported schema");
|
||||
let (kind, options, min, max) = match property.get("type").and_then(Value::as_str) {
|
||||
Some("string") => match choices(property) {
|
||||
Some(options) => (AgentFormFieldKind::Choice, options, None, None),
|
||||
None => (AgentFormFieldKind::Text, Vec::new(), number("minLength"), number("maxLength")),
|
||||
},
|
||||
Some("number") => (AgentFormFieldKind::Number, Vec::new(), number("minimum"), number("maximum")),
|
||||
Some("integer") => (AgentFormFieldKind::Integer, Vec::new(), number("minimum"), number("maximum")),
|
||||
Some("boolean") => (AgentFormFieldKind::Boolean, Vec::new(), None, None),
|
||||
Some("array") => {
|
||||
let options = property.get("items").and_then(choices).ok_or_else(unsupported)?;
|
||||
(AgentFormFieldKind::Choices, options, number("minItems"), number("maxItems"))
|
||||
}
|
||||
_ => return Err(unsupported()),
|
||||
};
|
||||
let default = match property.get("default") {
|
||||
Some(Value::Array(values)) => values.iter().filter_map(scalar).collect(),
|
||||
Some(value) => scalar(value).into_iter().collect(),
|
||||
None => Vec::new(),
|
||||
};
|
||||
let title = text("title");
|
||||
Ok(AgentFormField {
|
||||
name: name.to_owned(),
|
||||
title: if title.is_empty() { name.to_owned() } else { title },
|
||||
description: text("description"),
|
||||
required,
|
||||
kind,
|
||||
options,
|
||||
default,
|
||||
min,
|
||||
max,
|
||||
})
|
||||
}
|
||||
|
||||
/// The values of a choice: `enum` (titled by the legacy `enumNames`), or `oneOf` / `anyOf`
|
||||
/// of `{ const, title }`.
|
||||
fn choices(schema: &Value) -> Option<Vec<AgentFormOption>> {
|
||||
let option = |value: &str, label: Option<&str>, description: Option<&str>| AgentFormOption {
|
||||
value: value.to_owned(),
|
||||
label: label.filter(|l| !l.trim().is_empty()).unwrap_or(value).to_owned(),
|
||||
description: description.unwrap_or_default().trim().to_owned(),
|
||||
};
|
||||
if let Some(values) = schema.get("enum").and_then(Value::as_array) {
|
||||
let names = schema.get("enumNames").and_then(Value::as_array);
|
||||
return Some(
|
||||
values
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter_map(|(i, value)| {
|
||||
Some(option(value.as_str()?, names.and_then(|n| n.get(i)).and_then(Value::as_str), None))
|
||||
})
|
||||
.collect(),
|
||||
);
|
||||
}
|
||||
let listed = schema.get("oneOf").or_else(|| schema.get("anyOf"))?.as_array()?;
|
||||
Some(
|
||||
listed
|
||||
.iter()
|
||||
.filter_map(|o| {
|
||||
let text = |key: &str| o.get(key).and_then(Value::as_str);
|
||||
Some(option(text("const")?, text("title"), text("description")))
|
||||
})
|
||||
.collect(),
|
||||
)
|
||||
}
|
||||
|
||||
fn scalar(value: &Value) -> Option<String> {
|
||||
match value {
|
||||
Value::String(text) => Some(text.clone()),
|
||||
Value::Number(number) => Some(number.to_string()),
|
||||
Value::Bool(flag) => Some(flag.to_string()),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// A form is submitted from its own card; this option only names a declined form.
|
||||
pub(crate) fn options() -> Vec<AgentPermissionOption> {
|
||||
vec![AgentPermissionOption { id: DECLINE.to_owned(), name: "Decline".to_owned(), kind: "reject_once".to_owned() }]
|
||||
}
|
||||
|
||||
/// The `elicitation/create` response to the user's `reply`, and what the conversation
|
||||
/// records as chosen.
|
||||
pub(crate) fn answer(form: &AgentForm, reply: Option<&str>) -> (Value, String) {
|
||||
let values = match reply {
|
||||
Some(DECLINE) => return (json!({ "action": "decline" }), "Decline".to_owned()),
|
||||
Some(reply) => serde_json::from_str::<Map<String, Value>>(reply).ok(),
|
||||
None => None,
|
||||
};
|
||||
let Some(values) = values else { return (json!({ "action": "cancel" }), "Cancelled".to_owned()) };
|
||||
let mut content = Map::new();
|
||||
let mut shown = Vec::new();
|
||||
for field in &form.fields {
|
||||
let given: Vec<&str> = match values.get(&field.name) {
|
||||
Some(Value::Array(items)) => items.iter().filter_map(Value::as_str).collect(),
|
||||
Some(Value::String(item)) => vec![item.as_str()],
|
||||
_ => Vec::new(),
|
||||
};
|
||||
let given: Vec<&str> = given.into_iter().map(str::trim).filter(|v| !v.is_empty()).collect();
|
||||
let Some(&first) = given.first() else { continue };
|
||||
let value = match field.kind {
|
||||
AgentFormFieldKind::Text | AgentFormFieldKind::Choice => json!(first),
|
||||
AgentFormFieldKind::Number => match first.parse::<f64>() {
|
||||
Ok(number) => json!(number),
|
||||
Err(_) => continue,
|
||||
},
|
||||
AgentFormFieldKind::Integer => match first.parse::<i64>() {
|
||||
Ok(number) => json!(number),
|
||||
Err(_) => continue,
|
||||
},
|
||||
AgentFormFieldKind::Boolean => json!(first == "true"),
|
||||
AgentFormFieldKind::Choices => json!(given),
|
||||
};
|
||||
let label = |value: &str| {
|
||||
field.options.iter().find(|o| o.value == value).map_or_else(|| value.to_owned(), |o| o.label.clone())
|
||||
};
|
||||
let text = match field.kind {
|
||||
AgentFormFieldKind::Boolean => (if first == "true" { "yes" } else { "no" }).to_owned(),
|
||||
AgentFormFieldKind::Choices => given.iter().map(|v| label(v)).collect::<Vec<_>>().join(", "),
|
||||
_ => label(first),
|
||||
};
|
||||
shown.push(format!("{}: {text}", field.title));
|
||||
content.insert(field.name.clone(), value);
|
||||
}
|
||||
let chosen = if shown.is_empty() { "Submitted".to_owned() } else { shown.join("; ") };
|
||||
(json!({ "action": "accept", "content": content }), chosen)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
/// The form OMP's `ask` tool sends: a choice with a recommended default, then an
|
||||
/// optional typed answer beside it.
|
||||
fn ask() -> Value {
|
||||
json!({
|
||||
"sessionId": "s", "mode": "form", "message": "Which database?",
|
||||
"requestedSchema": { "type": "object", "properties": {
|
||||
"q0": { "type": "string", "title": "Which database?", "description": "Storage",
|
||||
"oneOf": [{ "const": "pg", "title": "Postgres", "description": "Relational" },
|
||||
{ "const": "sqlite", "title": "SQLite" }],
|
||||
"default": "pg" },
|
||||
"q0__other": { "type": "string", "title": "Other (type your own)" }
|
||||
} }
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fields_follow_the_schema_order() {
|
||||
let raw = br#"{ "jsonrpc": "2.0", "id": 3, "method": "elicitation/create", "params": { "mode": "form",
|
||||
"requestedSchema": { "type": "object", "properties": {
|
||||
"strategy": { "type": "string" }, "tests": { "type": "boolean" }, "note": { "type": "string" }
|
||||
} } } }"#;
|
||||
let params = serde_json::from_slice::<Value>(raw).unwrap()["params"].take();
|
||||
let mut form = form_of(¶ms).unwrap();
|
||||
in_schema_order(&mut form, raw);
|
||||
assert_eq!(form.fields.iter().map(|f| f.name.as_str()).collect::<Vec<_>>(), ["strategy", "tests", "note"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reads_choices_and_text_fields() {
|
||||
let form = form_of(&ask()).unwrap();
|
||||
let [choice, other] = form.fields.as_slice() else { panic!("{form:?}") };
|
||||
assert_eq!((choice.name.as_str(), choice.kind, choice.required), ("q0", AgentFormFieldKind::Choice, false));
|
||||
assert_eq!(choice.options[0].label, "Postgres");
|
||||
assert_eq!(choice.options[0].description, "Relational");
|
||||
assert_eq!(choice.default, ["pg"]);
|
||||
assert_eq!((other.kind, other.title.as_str()), (AgentFormFieldKind::Text, "Other (type your own)"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reads_every_restricted_schema_kind() {
|
||||
let form = form_of(&json!({ "mode": "form", "requestedSchema": {
|
||||
"type": "object", "required": ["count"],
|
||||
"properties": {
|
||||
"count": { "type": "integer", "minimum": 1, "maximum": 5 },
|
||||
"ratio": { "type": "number" },
|
||||
"ok": { "type": "boolean", "default": true },
|
||||
"size": { "type": "string", "enum": ["s", "l"], "enumNames": ["Small", "Large"] },
|
||||
"tags": { "type": "array", "items": { "anyOf": [{ "const": "a" }, { "const": "b" }] },
|
||||
"default": ["b"], "maxItems": 2 }
|
||||
}
|
||||
} }))
|
||||
.unwrap();
|
||||
let field = |name: &str| form.fields.iter().find(|f| f.name == name).unwrap();
|
||||
use AgentFormFieldKind::*;
|
||||
let kinds: Vec<_> = ["count", "ratio", "ok", "size", "tags"].map(|name| field(name).kind).into();
|
||||
assert_eq!(kinds, [Integer, Number, Boolean, Choice, Choices]);
|
||||
assert!(field("count").required && !field("ratio").required);
|
||||
assert_eq!((field("count").min, field("count").max), (Some(1.0), Some(5.0)));
|
||||
assert_eq!(field("ok").default, ["true"]);
|
||||
assert_eq!(field("size").options[1].label, "Large");
|
||||
assert_eq!((field("tags").default.as_slice(), field("tags").max), (["b".to_owned()].as_slice(), Some(2.0)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn url_mode_and_unsupported_fields_are_refused() {
|
||||
assert!(form_of(&json!({ "mode": "url", "url": "https://example.com" })).is_err());
|
||||
let nested = json!({ "mode": "form", "requestedSchema": { "type": "object", "properties": {
|
||||
"address": { "type": "object" }
|
||||
} } });
|
||||
assert!(form_of(&nested).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_answer_is_typed_by_its_field() {
|
||||
let form = form_of(&json!({ "mode": "form", "requestedSchema": { "type": "object", "properties": {
|
||||
"count": { "type": "integer" },
|
||||
"ok": { "type": "boolean" },
|
||||
"size": { "type": "string", "oneOf": [{ "const": "s", "title": "Small" }] },
|
||||
"tags": { "type": "array", "items": { "enum": ["a", "b"] } },
|
||||
"note": { "type": "string" }
|
||||
} } }))
|
||||
.unwrap();
|
||||
let reply = json!({ "count": ["3"], "ok": ["false"], "size": ["s"], "tags": ["a", "b"], "note": [" "] });
|
||||
let (response, chosen) = answer(&form, Some(&reply.to_string()));
|
||||
assert_eq!(
|
||||
response,
|
||||
json!({ "action": "accept", "content": { "count": 3, "ok": false, "size": "s", "tags": ["a", "b"] } })
|
||||
);
|
||||
assert_eq!(chosen, "count: 3; ok: no; size: Small; tags: a, b");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn declining_and_dismissing_send_no_content() {
|
||||
let form = form_of(&ask()).unwrap();
|
||||
assert_eq!(answer(&form, Some(DECLINE)), (json!({ "action": "decline" }), "Decline".to_owned()));
|
||||
assert_eq!(answer(&form, None).0, json!({ "action": "cancel" }));
|
||||
assert_eq!(answer(&form, Some("cancel")).0, json!({ "action": "cancel" }));
|
||||
}
|
||||
}
|
||||
@@ -3,11 +3,14 @@
|
||||
//!
|
||||
//! Only the native providers keep transcripts on disk in a readable form: Claude Code
|
||||
//! (`<config>/projects/*/<id>.jsonl`, see `native::claude::history`) and Codex
|
||||
//! (`<home>/.codex/sessions/**`, rollout files with a `session_meta` line).
|
||||
//! (`<home>/.codex/sessions/**`, rollout files with a `session_meta` line, named in
|
||||
//! `<home>/.codex/session_index.jsonl`).
|
||||
|
||||
use super::native::claude::history;
|
||||
use serde::Serialize;
|
||||
use specta::Type;
|
||||
use std::collections::HashMap;
|
||||
use std::io::BufRead;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
/// A session on this machine that no task holds yet.
|
||||
@@ -62,11 +65,28 @@ fn message_text(e: &serde_json::Value) -> Option<String> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Claude Code transcripts: `<config>/projects/*/<id>.jsonl`.
|
||||
pub fn claude_sessions(config_dir: &Path) -> Vec<ExternalSession> {
|
||||
/// Whether a session that ran in `cwd` belongs to the project at `root` (in it or below it).
|
||||
fn in_project(cwd: Option<&str>, root: &Path) -> bool {
|
||||
cwd.is_some_and(|cwd| Path::new(cwd).starts_with(root))
|
||||
}
|
||||
|
||||
/// Claude Code transcripts of the project at `root`: `<config>/projects/<escaped cwd>/<id>.jsonl`.
|
||||
/// Only the folders named after `root` or a folder below it are read, so other projects'
|
||||
/// transcripts (often gigabytes) are never opened.
|
||||
pub fn claude_sessions(config_dir: &Path, root: &Path) -> Vec<ExternalSession> {
|
||||
let mut out = Vec::new();
|
||||
let projects = config_dir.join("projects");
|
||||
let dirs: Vec<PathBuf> = std::fs::read_dir(&projects).into_iter().flatten().flatten().map(|d| d.path()).collect();
|
||||
let escaped = history::escape(root);
|
||||
let dirs: Vec<PathBuf> = std::fs::read_dir(&projects)
|
||||
.into_iter()
|
||||
.flatten()
|
||||
.flatten()
|
||||
.filter(|d| {
|
||||
let name = d.file_name().to_string_lossy().into_owned();
|
||||
name == escaped || name.starts_with(&format!("{escaped}-"))
|
||||
})
|
||||
.map(|d| d.path())
|
||||
.collect();
|
||||
for dir in dirs {
|
||||
let files: Vec<PathBuf> = std::fs::read_dir(&dir)
|
||||
.into_iter()
|
||||
@@ -93,6 +113,10 @@ pub fn claude_sessions(config_dir: &Path) -> Vec<ExternalSession> {
|
||||
.filter_map(|e| e.get("cwd").and_then(serde_json::Value::as_str))
|
||||
.next()
|
||||
.map(str::to_owned);
|
||||
// Escaping is lossy (`a/b-c` and `a/b/c` share a folder): the transcript decides.
|
||||
if !in_project(cwd.as_deref(), root) {
|
||||
continue;
|
||||
}
|
||||
out.push(ExternalSession {
|
||||
provider: "claude".to_owned(),
|
||||
session_id: id,
|
||||
@@ -110,7 +134,10 @@ fn codex_meta(line: &serde_json::Value) -> Option<(String, Option<String>)> {
|
||||
let payload =
|
||||
line.get("payload").filter(|p| p.get("type").and_then(serde_json::Value::as_str) == Some("session_meta"));
|
||||
let inner = payload.and_then(|p| p.get("payload")).or_else(|| {
|
||||
line.get("type").and_then(serde_json::Value::as_str).filter(|t| *t == "session_meta").and(Some(line))
|
||||
line.get("type")
|
||||
.and_then(serde_json::Value::as_str)
|
||||
.filter(|t| *t == "session_meta")
|
||||
.map(|_| line.get("payload").unwrap_or(line))
|
||||
})?;
|
||||
let id = inner.get("id").and_then(serde_json::Value::as_str)?.to_owned();
|
||||
let cwd = inner.get("cwd").and_then(serde_json::Value::as_str).map(str::to_owned);
|
||||
@@ -138,12 +165,13 @@ fn codex_user_text(line: &serde_json::Value) -> Option<String> {
|
||||
|
||||
/// One rollout file's session, or `None` when it names no `session_meta`.
|
||||
fn codex_file(path: &Path) -> Option<ExternalSession> {
|
||||
let text = std::fs::read_to_string(path).ok()?;
|
||||
let file = std::fs::File::open(path).ok()?;
|
||||
let mut id: Option<(String, Option<String>)> = None;
|
||||
let mut title: Option<String> = None;
|
||||
// `session_meta` is first, but cap the scan: giant rollouts must not stall listing.
|
||||
for line in text.lines().take(400) {
|
||||
let Ok(v) = serde_json::from_str::<serde_json::Value>(line) else { continue };
|
||||
for line in std::io::BufReader::new(file).lines().take(400) {
|
||||
let Ok(line) = line else { break };
|
||||
let Ok(v) = serde_json::from_str::<serde_json::Value>(&line) else { continue };
|
||||
if id.is_none()
|
||||
&& let Some(meta) = codex_meta(&v)
|
||||
{
|
||||
@@ -183,11 +211,39 @@ fn rollout_files(dir: &Path, depth: usize, out: &mut Vec<PathBuf>) {
|
||||
}
|
||||
}
|
||||
|
||||
/// Codex sessions: `<home>/.codex/sessions/**`.
|
||||
pub fn codex_sessions(codex_home: &Path) -> Vec<ExternalSession> {
|
||||
/// Thread names Codex keeps in `session_index.jsonl`, by thread id. A rename adds a line,
|
||||
/// so the last one wins.
|
||||
fn codex_names(codex_home: &Path) -> HashMap<String, String> {
|
||||
let Ok(file) = std::fs::File::open(codex_home.join("session_index.jsonl")) else { return HashMap::new() };
|
||||
let mut names = HashMap::new();
|
||||
for line in std::io::BufReader::new(file).lines() {
|
||||
let Ok(line) = line else { break };
|
||||
let Ok(v) = serde_json::from_str::<serde_json::Value>(&line) else { continue };
|
||||
let id = v.get("id").and_then(serde_json::Value::as_str);
|
||||
let name = v.get("thread_name").and_then(serde_json::Value::as_str).map(str::trim).filter(|n| !n.is_empty());
|
||||
if let (Some(id), Some(name)) = (id, name) {
|
||||
names.insert(id.to_owned(), name.to_owned());
|
||||
}
|
||||
}
|
||||
names
|
||||
}
|
||||
|
||||
/// Codex sessions of the project at `root`: `<home>/.codex/sessions/**`, titled by their
|
||||
/// thread names, else by their first prompts.
|
||||
pub fn codex_sessions(codex_home: &Path, root: &Path) -> Vec<ExternalSession> {
|
||||
let mut files = Vec::new();
|
||||
rollout_files(&codex_home.join("sessions"), 4, &mut files);
|
||||
files.iter().filter_map(|p| codex_file(p)).collect()
|
||||
let mut sessions: Vec<ExternalSession> =
|
||||
files.iter().filter_map(|p| codex_file(p)).filter(|s| in_project(s.cwd.as_deref(), root)).collect();
|
||||
if !sessions.is_empty() {
|
||||
let names = codex_names(codex_home);
|
||||
for s in &mut sessions {
|
||||
if let Some(name) = names.get(&s.session_id) {
|
||||
s.title = short(name);
|
||||
}
|
||||
}
|
||||
}
|
||||
sessions
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -222,7 +278,7 @@ mod tests {
|
||||
],
|
||||
);
|
||||
write(&tmp.path().join("projects/-work-app/empty.jsonl"), &[]);
|
||||
let sessions = claude_sessions(tmp.path());
|
||||
let sessions = claude_sessions(tmp.path(), Path::new("/work/app"));
|
||||
assert_eq!(sessions.len(), 1);
|
||||
let s = &sessions[0];
|
||||
assert_eq!((s.provider.as_str(), s.session_id.as_str()), ("claude", "abc123"));
|
||||
@@ -234,37 +290,91 @@ mod tests {
|
||||
#[test]
|
||||
fn claude_title_falls_back_to_the_first_prompt() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
write(&tmp.path().join("projects/-x/def456.jsonl"), &[claude_user("u1", "hello there")]);
|
||||
let sessions = claude_sessions(tmp.path());
|
||||
write(&tmp.path().join("projects/-work-app/def456.jsonl"), &[claude_user("u1", "hello there")]);
|
||||
let sessions = claude_sessions(tmp.path(), Path::new("/work/app"));
|
||||
assert_eq!(sessions.len(), 1);
|
||||
assert_eq!(sessions[0].title, "hello there");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn codex_meta_accepts_current_and_legacy_shapes() {
|
||||
let meta = json!({"id": "thread-1", "cwd": "/work/app"});
|
||||
for line in [
|
||||
json!({"type": "session_meta", "payload": meta}),
|
||||
json!({"payload": {"type": "session_meta", "payload": meta}}),
|
||||
json!({"type": "session_meta", "id": "thread-1", "cwd": "/work/app"}),
|
||||
] {
|
||||
assert_eq!(codex_meta(&line), Some(("thread-1".to_owned(), Some("/work/app".to_owned()))));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lists_codex_rollouts_with_meta() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
write(
|
||||
&tmp.path().join("sessions/2026/01/02/rollout-01.jsonl"),
|
||||
&[
|
||||
json!({"timestamp": "t", "payload": {"type": "session_meta",
|
||||
"payload": {"id": "thread-1", "cwd": "/work/app"}}}),
|
||||
json!({"timestamp": "t", "payload": {"type": "user_message",
|
||||
"payload": {"message": "add tests"}}}),
|
||||
json!({"timestamp": "t", "type": "session_meta",
|
||||
"payload": {"id": "thread-1", "cwd": "/work/app"}}),
|
||||
json!({"timestamp": "t", "type": "event_msg",
|
||||
"payload": {"type": "user_message", "message": "add tests"}}),
|
||||
],
|
||||
);
|
||||
write(&tmp.path().join("sessions/notes.jsonl"), &[json!({"nope": true})]);
|
||||
let sessions = codex_sessions(tmp.path());
|
||||
let sessions = codex_sessions(tmp.path(), Path::new("/work"));
|
||||
assert_eq!(sessions.len(), 1);
|
||||
let s = &sessions[0];
|
||||
assert_eq!((s.provider.as_str(), s.session_id.as_str()), ("codex", "thread-1"));
|
||||
assert_eq!(s.title, "add tests");
|
||||
assert_eq!(s.cwd.as_deref(), Some("/work/app"));
|
||||
|
||||
write(
|
||||
&tmp.path().join("session_index.jsonl"),
|
||||
&[
|
||||
json!({"id": "thread-1", "thread_name": "Add tests", "updated_at": "t1"}),
|
||||
json!({"id": "thread-2", "thread_name": "Other", "updated_at": "t1"}),
|
||||
json!({"id": "thread-1", "thread_name": "Add parser tests", "updated_at": "t2"}),
|
||||
],
|
||||
);
|
||||
assert_eq!(codex_sessions(tmp.path(), Path::new("/work"))[0].title, "Add parser tests");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn missing_directories_list_nothing() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
assert!(claude_sessions(&tmp.path().join("nope")).is_empty());
|
||||
assert!(codex_sessions(&tmp.path().join("nope")).is_empty());
|
||||
assert!(claude_sessions(&tmp.path().join("nope"), Path::new("/work")).is_empty());
|
||||
assert!(codex_sessions(&tmp.path().join("nope"), Path::new("/work")).is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn only_sessions_of_the_project_are_listed() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let user = |cwd: &str| {
|
||||
json!({"type": "user", "uuid": "u1", "parentUuid": null,
|
||||
"message": {"role": "user", "content": "hi"}, "cwd": cwd})
|
||||
};
|
||||
write(&tmp.path().join("projects/-work-app/in-root.jsonl"), &[user("/work/app")]);
|
||||
write(&tmp.path().join("projects/-work-app-web/below.jsonl"), &[user("/work/app/web")]);
|
||||
// `/work/app-old` escapes like a folder below `/work/app`; its transcript says otherwise.
|
||||
write(&tmp.path().join("projects/-work-app-old/sibling.jsonl"), &[user("/work/app-old")]);
|
||||
write(&tmp.path().join("projects/-other/elsewhere.jsonl"), &[user("/other")]);
|
||||
let mut ids: Vec<String> =
|
||||
claude_sessions(tmp.path(), Path::new("/work/app")).into_iter().map(|s| s.session_id).collect();
|
||||
ids.sort();
|
||||
assert_eq!(ids, ["below", "in-root"]);
|
||||
|
||||
let rollout = |name: &str, cwd: &str| {
|
||||
write(
|
||||
&tmp.path().join(format!("codex/sessions/2026/01/02/{name}.jsonl")),
|
||||
&[json!({"payload": {"type": "session_meta", "payload": {"id": name, "cwd": cwd}}})],
|
||||
)
|
||||
};
|
||||
rollout("mine", "/work/app/api");
|
||||
rollout("theirs", "/other");
|
||||
let codex: Vec<String> = codex_sessions(&tmp.path().join("codex"), Path::new("/work/app"))
|
||||
.into_iter()
|
||||
.map(|s| s.session_id)
|
||||
.collect();
|
||||
assert_eq!(codex, ["mine"]);
|
||||
}
|
||||
}
|
||||
+1132
-302
File diff suppressed because it is too large.
Load diff
+822
-64
@@ -1,13 +1,24 @@
|
||||
//! Model Context Protocol (MCP) server configurations stored in OpenIDEA's own settings
|
||||
//! (global `settings.toml` and project `.openidea/settings.toml`), passed to ACP agents in
|
||||
//! `session/new`, `session/load`, and `session/fork`, plus one-way import from `.mcp.json` /
|
||||
//! `.vscode/mcp.json`.
|
||||
//! `session/new`, `session/load`, and `session/fork`, plus discovery and one-way import from
|
||||
//! external CLI and workspace MCP configuration files.
|
||||
|
||||
use crate::acp::Negotiated;
|
||||
use crate::{AgentMcpServer, McpKeyValue, McpScope, McpServer, McpServerInput, McpTransport};
|
||||
use crate::{
|
||||
AgentMcpServer, McpDiscoveredServer, McpExternalSource, McpImportSelection, McpKeyValue, McpProbeResult, McpScope,
|
||||
McpServer, McpServerInput, McpToolSummary, McpTransport,
|
||||
};
|
||||
use ide_core::IpcPath;
|
||||
use serde_json::{Value, json};
|
||||
use std::collections::{BTreeMap, HashSet};
|
||||
use std::path::Path;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
/// Reserved server name for OpenIDEA's built-in IDE MCP server in per-session overrides.
|
||||
pub const BUILTIN_MCP_NAME: &str = "openidea";
|
||||
|
||||
const PROBE_TIMEOUT: Duration = Duration::from_secs(12);
|
||||
const PROBE_PROTOCOL_VERSION: &str = "2024-11-05";
|
||||
|
||||
/// Stable ID for a server in a given scope (`"user::<name>"` or `"workspace::<name>"`).
|
||||
pub fn server_id(scope: McpScope, name: &str) -> String {
|
||||
@@ -56,6 +67,7 @@ fn to_mcp_server(scope: McpScope, name: &str, cfg: &AgentMcpServer) -> McpServer
|
||||
url: cfg.url.clone(),
|
||||
headers: cfg.headers.iter().map(|(k, v)| McpKeyValue { name: k.clone(), value: v.clone() }).collect(),
|
||||
agents: cfg.agents.clone(),
|
||||
unapproved: false,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -75,8 +87,15 @@ fn matches_agent(agents: &[String], provider: &str) -> bool {
|
||||
}
|
||||
|
||||
/// Builds the ACP `mcpServers` array for `session/new`, `session/load`, and `session/fork`.
|
||||
/// Workspace entries take precedence over User entries with the same name.
|
||||
pub(crate) fn acp_mcp_servers(all: &[McpServer], provider: &str, init: &Negotiated) -> Vec<Value> {
|
||||
/// Workspace entries take precedence over User entries with the same name. Per-session
|
||||
/// `overrides` (`server_name` -> `enabled`) can enable a disabled server or disable an enabled one
|
||||
/// for this session only.
|
||||
pub(crate) fn acp_mcp_servers(
|
||||
all: &[McpServer],
|
||||
provider: &str,
|
||||
init: &Negotiated,
|
||||
overrides: &BTreeMap<String, bool>,
|
||||
) -> Vec<Value> {
|
||||
let supports_stdio = init.mcp_stdio();
|
||||
let supports_http = init.mcp("http");
|
||||
let supports_sse = init.mcp("sse");
|
||||
@@ -87,7 +106,16 @@ pub(crate) fn acp_mcp_servers(all: &[McpServer], provider: &str, init: &Negotiat
|
||||
all.iter().filter(|s| s.scope == McpScope::Workspace).chain(all.iter().filter(|s| s.scope == McpScope::User));
|
||||
|
||||
for srv in ordered {
|
||||
if !srv.enabled || !matches_agent(&srv.agents, provider) || !seen.insert(srv.name.clone()) {
|
||||
if srv.unapproved {
|
||||
continue;
|
||||
}
|
||||
let explicit = overrides.get(&srv.name).copied();
|
||||
let active = explicit.unwrap_or(srv.enabled);
|
||||
if !active || !seen.insert(srv.name.clone()) {
|
||||
continue;
|
||||
}
|
||||
// An explicit per-session enable (`Some(true)`) overrides the server's default agent filter.
|
||||
if explicit != Some(true) && !matches_agent(&srv.agents, provider) {
|
||||
continue;
|
||||
}
|
||||
match srv.transport {
|
||||
@@ -276,6 +304,306 @@ pub fn apply_import(workspace: &mut BTreeMap<String, AgentMcpServer>, workspace_
|
||||
added
|
||||
}
|
||||
|
||||
/// Parses a raw MCP JSON snippet pasted by the user and merges its servers into `user` or
|
||||
/// `workspace`. Supports `{"mcpServers": {...}}`, `{"servers": {...}}`, top-level server maps
|
||||
/// (`{"my-server": {"command": ...}}`), and single-server objects when `fallback_name` is given.
|
||||
pub fn apply_save_json(
|
||||
user: &mut BTreeMap<String, AgentMcpServer>,
|
||||
workspace: &mut BTreeMap<String, AgentMcpServer>,
|
||||
has_workspace: bool,
|
||||
scope: McpScope,
|
||||
raw_json: &str,
|
||||
fallback_name: Option<&str>,
|
||||
agents: Vec<String>,
|
||||
) -> Result<Vec<String>, String> {
|
||||
if scope == McpScope::Workspace && !has_workspace {
|
||||
return Err("Open a project to configure workspace MCP servers".into());
|
||||
}
|
||||
let parsed = parse_mcp_json_text(raw_json, fallback_name)?;
|
||||
if parsed.is_empty() {
|
||||
return Err("No valid MCP servers found in JSON".into());
|
||||
}
|
||||
let normalized_agents = normalize_agents_list(agents);
|
||||
let target = match scope {
|
||||
McpScope::User => user,
|
||||
McpScope::Workspace => workspace,
|
||||
};
|
||||
let mut saved_names = Vec::with_capacity(parsed.len());
|
||||
for (name, mut srv) in parsed {
|
||||
if srv.agents.is_empty() && !normalized_agents.is_empty() {
|
||||
srv.agents = normalized_agents.clone();
|
||||
}
|
||||
saved_names.push(name.clone());
|
||||
target.insert(name, srv);
|
||||
}
|
||||
Ok(saved_names)
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, Eq)]
|
||||
enum SourceFormat {
|
||||
Jsonc,
|
||||
CodexToml,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
struct SourceSpec {
|
||||
id: &'static str,
|
||||
label: &'static str,
|
||||
path: PathBuf,
|
||||
format: SourceFormat,
|
||||
suggested_scope: McpScope,
|
||||
native_provider: Option<&'static str>,
|
||||
claude_project_lookup: bool,
|
||||
}
|
||||
|
||||
fn external_source_specs(home_dir: Option<&Path>, workspace_root: Option<&Path>) -> Vec<SourceSpec> {
|
||||
let mut specs = Vec::new();
|
||||
if let Some(root) = workspace_root {
|
||||
specs.push(SourceSpec {
|
||||
id: "workspace-mcp",
|
||||
label: "Project (.mcp.json)",
|
||||
path: root.join(".mcp.json"),
|
||||
format: SourceFormat::Jsonc,
|
||||
suggested_scope: McpScope::Workspace,
|
||||
native_provider: Some("claude"),
|
||||
claude_project_lookup: false,
|
||||
});
|
||||
specs.push(SourceSpec {
|
||||
id: "vscode-mcp",
|
||||
label: "VS Code (.vscode/mcp.json)",
|
||||
path: root.join(".vscode").join("mcp.json"),
|
||||
format: SourceFormat::Jsonc,
|
||||
suggested_scope: McpScope::Workspace,
|
||||
native_provider: None,
|
||||
claude_project_lookup: false,
|
||||
});
|
||||
specs.push(SourceSpec {
|
||||
id: "cursor-project",
|
||||
label: "Cursor Project (.cursor/mcp.json)",
|
||||
path: root.join(".cursor").join("mcp.json"),
|
||||
format: SourceFormat::Jsonc,
|
||||
suggested_scope: McpScope::Workspace,
|
||||
native_provider: Some("cursor"),
|
||||
claude_project_lookup: false,
|
||||
});
|
||||
specs.push(SourceSpec {
|
||||
id: "codex-project",
|
||||
label: "Codex Project (.codex/config.toml)",
|
||||
path: root.join(".codex").join("config.toml"),
|
||||
format: SourceFormat::CodexToml,
|
||||
suggested_scope: McpScope::Workspace,
|
||||
native_provider: Some("codex"),
|
||||
claude_project_lookup: false,
|
||||
});
|
||||
specs.push(SourceSpec {
|
||||
id: "gemini-project",
|
||||
label: "Gemini Project (.gemini/settings.json)",
|
||||
path: root.join(".gemini").join("settings.json"),
|
||||
format: SourceFormat::Jsonc,
|
||||
suggested_scope: McpScope::Workspace,
|
||||
native_provider: Some("gemini"),
|
||||
claude_project_lookup: false,
|
||||
});
|
||||
}
|
||||
if let Some(home) = home_dir {
|
||||
specs.push(SourceSpec {
|
||||
id: "copilot-cli",
|
||||
label: "GitHub Copilot CLI",
|
||||
path: home.join(".copilot").join("mcp-config.json"),
|
||||
format: SourceFormat::Jsonc,
|
||||
suggested_scope: McpScope::User,
|
||||
native_provider: Some("copilot"),
|
||||
claude_project_lookup: false,
|
||||
});
|
||||
specs.push(SourceSpec {
|
||||
id: "claude-code",
|
||||
label: "Claude Code",
|
||||
path: home.join(".claude.json"),
|
||||
format: SourceFormat::Jsonc,
|
||||
suggested_scope: McpScope::User,
|
||||
native_provider: Some("claude"),
|
||||
claude_project_lookup: true,
|
||||
});
|
||||
specs.push(SourceSpec {
|
||||
id: "codex",
|
||||
label: "Codex",
|
||||
path: home.join(".codex").join("config.toml"),
|
||||
format: SourceFormat::CodexToml,
|
||||
suggested_scope: McpScope::User,
|
||||
native_provider: Some("codex"),
|
||||
claude_project_lookup: false,
|
||||
});
|
||||
specs.push(SourceSpec {
|
||||
id: "cursor",
|
||||
label: "Cursor",
|
||||
path: home.join(".cursor").join("mcp.json"),
|
||||
format: SourceFormat::Jsonc,
|
||||
suggested_scope: McpScope::User,
|
||||
native_provider: Some("cursor"),
|
||||
claude_project_lookup: false,
|
||||
});
|
||||
specs.push(SourceSpec {
|
||||
id: "gemini",
|
||||
label: "Gemini CLI",
|
||||
path: home.join(".gemini").join("settings.json"),
|
||||
format: SourceFormat::Jsonc,
|
||||
suggested_scope: McpScope::User,
|
||||
native_provider: Some("gemini"),
|
||||
claude_project_lookup: false,
|
||||
});
|
||||
specs.push(SourceSpec {
|
||||
id: "jetbrains-air",
|
||||
label: "JetBrains Air",
|
||||
path: home.join(".jetbrains").join("mcp").join("mcp.json"),
|
||||
format: SourceFormat::Jsonc,
|
||||
suggested_scope: McpScope::User,
|
||||
native_provider: None,
|
||||
claude_project_lookup: false,
|
||||
});
|
||||
specs.push(SourceSpec {
|
||||
id: "copilot-intellij",
|
||||
label: "GitHub Copilot (JetBrains)",
|
||||
path: home.join(".config").join("github-copilot").join("intellij").join("mcp.json"),
|
||||
format: SourceFormat::Jsonc,
|
||||
suggested_scope: McpScope::User,
|
||||
native_provider: None,
|
||||
claude_project_lookup: false,
|
||||
});
|
||||
}
|
||||
specs
|
||||
}
|
||||
|
||||
fn read_source_servers(spec: &SourceSpec, workspace_root: Option<&Path>) -> BTreeMap<String, AgentMcpServer> {
|
||||
let Ok(text) = std::fs::read_to_string(&spec.path) else {
|
||||
return BTreeMap::new();
|
||||
};
|
||||
if spec.format == SourceFormat::CodexToml {
|
||||
return parse_codex_toml_servers(&text);
|
||||
}
|
||||
let Ok(root) = parse_jsonc_value(&text) else {
|
||||
return BTreeMap::new();
|
||||
};
|
||||
let mut servers = extract_servers_from_root(&root, None);
|
||||
if spec.claude_project_lookup
|
||||
&& let Some(ws) = workspace_root
|
||||
&& let Some(projects) = root.get("projects").and_then(Value::as_object)
|
||||
{
|
||||
let ws_canon = dunce::canonicalize(ws).unwrap_or_else(|_| ws.to_path_buf());
|
||||
for (proj_key, proj_val) in projects {
|
||||
let matches = Path::new(proj_key) == ws || dunce::canonicalize(proj_key).is_ok_and(|c| c == ws_canon);
|
||||
if matches {
|
||||
for (name, srv) in extract_servers_from_root(proj_val, None) {
|
||||
servers.insert(name, srv);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
servers
|
||||
}
|
||||
|
||||
fn parse_codex_toml_servers(text: &str) -> BTreeMap<String, AgentMcpServer> {
|
||||
let Ok(doc) = toml::from_str::<toml::Value>(text) else {
|
||||
return BTreeMap::new();
|
||||
};
|
||||
let Ok(json_doc) = serde_json::to_value(doc) else {
|
||||
return BTreeMap::new();
|
||||
};
|
||||
let Some(table) = json_doc.get("mcp_servers").or_else(|| json_doc.get("mcpServers")).and_then(Value::as_object)
|
||||
else {
|
||||
return BTreeMap::new();
|
||||
};
|
||||
let mut out = BTreeMap::new();
|
||||
for (name, raw) in table {
|
||||
let name = name.trim();
|
||||
let Some(mut obj) = raw.as_object().cloned().filter(|_| !name.is_empty()) else {
|
||||
continue;
|
||||
};
|
||||
if !obj.contains_key("headers")
|
||||
&& let Some(http_headers) = obj.remove("http_headers")
|
||||
{
|
||||
obj.insert("headers".to_owned(), http_headers);
|
||||
}
|
||||
if let Some(srv) = parse_one_server_def(&obj) {
|
||||
out.insert(name.to_owned(), srv);
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// Discovers external MCP configuration files on disk (without exposing secret values).
|
||||
pub fn discover_external_sources(home_dir: Option<&Path>, workspace_root: Option<&Path>) -> Vec<McpExternalSource> {
|
||||
let mut out = Vec::new();
|
||||
for spec in external_source_specs(home_dir, workspace_root) {
|
||||
let map = read_source_servers(&spec, workspace_root);
|
||||
if map.is_empty() {
|
||||
continue;
|
||||
}
|
||||
let servers = map
|
||||
.into_iter()
|
||||
.map(|(name, cfg)| McpDiscoveredServer {
|
||||
name,
|
||||
transport: cfg.transport,
|
||||
enabled: cfg.enabled,
|
||||
command: cfg.command,
|
||||
args: cfg.args,
|
||||
url: cfg.url,
|
||||
env_keys: cfg.env.into_keys().collect(),
|
||||
header_keys: cfg.headers.into_keys().collect(),
|
||||
})
|
||||
.collect();
|
||||
out.push(McpExternalSource {
|
||||
id: spec.id.to_owned(),
|
||||
label: spec.label.to_owned(),
|
||||
path: IpcPath(spec.path),
|
||||
suggested_scope: spec.suggested_scope,
|
||||
native_provider: spec.native_provider.map(str::to_owned),
|
||||
servers,
|
||||
});
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// Imports selected `(source_id, name)` entries from discovered external MCP config files into
|
||||
/// `user` or `workspace` without modifying the external files. Returns imported server names.
|
||||
pub fn apply_import_selected(
|
||||
user: &mut BTreeMap<String, AgentMcpServer>,
|
||||
workspace: &mut BTreeMap<String, AgentMcpServer>,
|
||||
home_dir: Option<&Path>,
|
||||
workspace_root: Option<&Path>,
|
||||
scope: McpScope,
|
||||
selections: &[McpImportSelection],
|
||||
overwrite: bool,
|
||||
) -> Result<Vec<String>, String> {
|
||||
if scope == McpScope::Workspace && workspace_root.is_none() {
|
||||
return Err("Open a project to import workspace MCP servers".into());
|
||||
}
|
||||
let specs = external_source_specs(home_dir, workspace_root);
|
||||
let mut by_source: BTreeMap<&str, BTreeMap<String, AgentMcpServer>> = BTreeMap::new();
|
||||
for spec in &specs {
|
||||
if selections.iter().any(|s| s.source_id == spec.id) {
|
||||
by_source.insert(spec.id, read_source_servers(spec, workspace_root));
|
||||
}
|
||||
}
|
||||
let target = match scope {
|
||||
McpScope::User => user,
|
||||
McpScope::Workspace => workspace,
|
||||
};
|
||||
let mut imported = Vec::new();
|
||||
for sel in selections {
|
||||
let Some(source_map) = by_source.get(sel.source_id.as_str()) else {
|
||||
continue;
|
||||
};
|
||||
let Some(srv) = source_map.get(&sel.name) else {
|
||||
continue;
|
||||
};
|
||||
if overwrite || !target.contains_key(&sel.name) {
|
||||
target.insert(sel.name.clone(), srv.clone());
|
||||
imported.push(sel.name.clone());
|
||||
}
|
||||
}
|
||||
Ok(imported)
|
||||
}
|
||||
|
||||
fn normalize_agents_list(agents: Vec<String>) -> Vec<String> {
|
||||
let mut out = Vec::new();
|
||||
for a in agents {
|
||||
@@ -287,73 +615,376 @@ fn normalize_agents_list(agents: Vec<String>) -> Vec<String> {
|
||||
out
|
||||
}
|
||||
|
||||
fn read_external_file(path: &Path) -> BTreeMap<String, AgentMcpServer> {
|
||||
let Ok(bytes) = std::fs::read(path) else {
|
||||
return BTreeMap::new();
|
||||
};
|
||||
let Ok(root) = serde_json::from_slice::<Value>(&bytes) else {
|
||||
return BTreeMap::new();
|
||||
};
|
||||
let Some(servers_obj) = root.get("mcpServers").or_else(|| root.get("servers")).and_then(Value::as_object) else {
|
||||
return BTreeMap::new();
|
||||
/// Strips `//` line comments, `/* ... */` block comments, and trailing commas before `}` or `]`
|
||||
/// outside JSON string literals so JSONC config files parse cleanly with `serde_json`.
|
||||
pub(crate) fn strip_jsonc(input: &str) -> String {
|
||||
let bytes = input.as_bytes();
|
||||
let mut out = String::with_capacity(input.len());
|
||||
let mut i = 0;
|
||||
let mut in_str = false;
|
||||
while i < bytes.len() {
|
||||
if in_str {
|
||||
let ch = input[i..].chars().next().unwrap();
|
||||
out.push(ch);
|
||||
i += ch.len_utf8();
|
||||
if ch == '\\' && i < bytes.len() {
|
||||
let next = input[i..].chars().next().unwrap();
|
||||
out.push(next);
|
||||
i += next.len_utf8();
|
||||
} else if ch == '"' {
|
||||
in_str = false;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if bytes[i] == b'"' {
|
||||
in_str = true;
|
||||
out.push('"');
|
||||
i += 1;
|
||||
continue;
|
||||
}
|
||||
if bytes[i] == b'/' && i + 1 < bytes.len() && bytes[i + 1] == b'/' {
|
||||
i += 2;
|
||||
while i < bytes.len() && bytes[i] != b'\n' {
|
||||
i += 1;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if bytes[i] == b'/' && i + 1 < bytes.len() && bytes[i + 1] == b'*' {
|
||||
i += 2;
|
||||
while i + 1 < bytes.len() && !(bytes[i] == b'*' && bytes[i + 1] == b'/') {
|
||||
if bytes[i] == b'\n' {
|
||||
out.push('\n');
|
||||
i += 1;
|
||||
} else {
|
||||
let ch = input[i..].chars().next().unwrap();
|
||||
i += ch.len_utf8();
|
||||
}
|
||||
}
|
||||
i = (i + 2).min(bytes.len());
|
||||
continue;
|
||||
}
|
||||
let ch = input[i..].chars().next().unwrap();
|
||||
out.push(ch);
|
||||
i += ch.len_utf8();
|
||||
}
|
||||
remove_trailing_commas(&out)
|
||||
}
|
||||
|
||||
fn remove_trailing_commas(input: &str) -> String {
|
||||
let bytes = input.as_bytes();
|
||||
let mut out = String::with_capacity(input.len());
|
||||
let mut i = 0;
|
||||
let mut in_str = false;
|
||||
while i < bytes.len() {
|
||||
if in_str {
|
||||
let ch = input[i..].chars().next().unwrap();
|
||||
out.push(ch);
|
||||
i += ch.len_utf8();
|
||||
if ch == '\\' && i < bytes.len() {
|
||||
let next = input[i..].chars().next().unwrap();
|
||||
out.push(next);
|
||||
i += next.len_utf8();
|
||||
} else if ch == '"' {
|
||||
in_str = false;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if bytes[i] == b'"' {
|
||||
in_str = true;
|
||||
out.push('"');
|
||||
i += 1;
|
||||
continue;
|
||||
}
|
||||
if bytes[i] == b',' {
|
||||
let mut j = i + 1;
|
||||
while j < bytes.len() && bytes[j].is_ascii_whitespace() {
|
||||
j += 1;
|
||||
}
|
||||
if j < bytes.len() && (bytes[j] == b'}' || bytes[j] == b']') {
|
||||
i += 1;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
let ch = input[i..].chars().next().unwrap();
|
||||
out.push(ch);
|
||||
i += ch.len_utf8();
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
fn parse_jsonc_value(raw: &str) -> Result<Value, String> {
|
||||
let trimmed = raw.trim().trim_start_matches('\u{feff}');
|
||||
serde_json::from_str::<Value>(trimmed)
|
||||
.or_else(|_| serde_json::from_str::<Value>(&strip_jsonc(trimmed)))
|
||||
.map_err(|e| format!("Invalid JSON: {e}"))
|
||||
}
|
||||
|
||||
pub fn parse_mcp_json_text(raw: &str, fallback_name: Option<&str>) -> Result<BTreeMap<String, AgentMcpServer>, String> {
|
||||
let root = parse_jsonc_value(raw)?;
|
||||
Ok(extract_servers_from_root(&root, fallback_name))
|
||||
}
|
||||
|
||||
fn is_server_def_object(obj: &serde_json::Map<String, Value>) -> bool {
|
||||
obj.get("command").and_then(Value::as_str).is_some_and(|s| !s.trim().is_empty())
|
||||
|| obj.get("url").and_then(Value::as_str).is_some_and(|s| !s.trim().is_empty())
|
||||
|| obj.get("serverUrl").and_then(Value::as_str).is_some_and(|s| !s.trim().is_empty())
|
||||
}
|
||||
|
||||
fn parse_one_server_def(obj: &serde_json::Map<String, Value>) -> Option<AgentMcpServer> {
|
||||
let disabled = obj.get("disabled").and_then(Value::as_bool).unwrap_or(false)
|
||||
|| obj.get("enabled").and_then(Value::as_bool) == Some(false);
|
||||
let type_str = obj.get("type").and_then(Value::as_str).unwrap_or_default().to_ascii_lowercase();
|
||||
let url = obj
|
||||
.get("url")
|
||||
.or_else(|| obj.get("serverUrl"))
|
||||
.and_then(Value::as_str)
|
||||
.map(str::trim)
|
||||
.filter(|s| !s.is_empty())
|
||||
.map(str::to_owned);
|
||||
let command =
|
||||
obj.get("command").and_then(Value::as_str).map(str::trim).filter(|s| !s.is_empty()).map(str::to_owned);
|
||||
|
||||
let transport = match type_str.as_str() {
|
||||
"sse" => McpTransport::Sse,
|
||||
"http" | "streamable-http" | "streamable_http" => McpTransport::Http,
|
||||
"stdio" | "local" => McpTransport::Stdio,
|
||||
_ if url.is_some() && command.is_none() => McpTransport::Http,
|
||||
_ => McpTransport::Stdio,
|
||||
};
|
||||
|
||||
match transport {
|
||||
McpTransport::Stdio => command.as_ref()?,
|
||||
McpTransport::Http | McpTransport::Sse => url.as_ref()?,
|
||||
};
|
||||
|
||||
let args = obj
|
||||
.get("args")
|
||||
.and_then(Value::as_array)
|
||||
.map(|arr| arr.iter().filter_map(|v| v.as_str().map(str::to_owned)).collect())
|
||||
.unwrap_or_default();
|
||||
|
||||
let agents = obj
|
||||
.get("agents")
|
||||
.and_then(Value::as_array)
|
||||
.map(|arr| normalize_agents_list(arr.iter().filter_map(|v| v.as_str().map(str::to_owned)).collect()))
|
||||
.unwrap_or_default();
|
||||
|
||||
Some(AgentMcpServer {
|
||||
transport,
|
||||
enabled: !disabled,
|
||||
command,
|
||||
args,
|
||||
env: parse_key_values_map(obj.get("env")),
|
||||
url,
|
||||
headers: parse_key_values_map(obj.get("headers")),
|
||||
agents,
|
||||
})
|
||||
}
|
||||
|
||||
fn extract_servers_from_root(root: &Value, fallback_name: Option<&str>) -> BTreeMap<String, AgentMcpServer> {
|
||||
let mut out = BTreeMap::new();
|
||||
for (name, def) in servers_obj {
|
||||
let name = name.trim();
|
||||
let Some(obj) = def.as_object().filter(|_| !name.is_empty()) else {
|
||||
continue;
|
||||
};
|
||||
let disabled = obj.get("disabled").and_then(Value::as_bool).unwrap_or(false)
|
||||
|| obj.get("enabled").and_then(Value::as_bool) == Some(false);
|
||||
let type_str = obj.get("type").and_then(Value::as_str).unwrap_or_default().to_ascii_lowercase();
|
||||
let url = obj
|
||||
.get("url")
|
||||
.or_else(|| obj.get("serverUrl"))
|
||||
let Some(root_obj) = root.as_object() else {
|
||||
return out;
|
||||
};
|
||||
|
||||
// Single server definition object pasted directly (`{"command": "npx", ...}`).
|
||||
if is_server_def_object(root_obj) {
|
||||
let name = root_obj
|
||||
.get("name")
|
||||
.and_then(Value::as_str)
|
||||
.map(str::trim)
|
||||
.filter(|s| !s.is_empty())
|
||||
.map(str::to_owned);
|
||||
let command =
|
||||
obj.get("command").and_then(Value::as_str).map(str::trim).filter(|s| !s.is_empty()).map(str::to_owned);
|
||||
|
||||
let transport = match type_str.as_str() {
|
||||
"sse" => McpTransport::Sse,
|
||||
"http" | "streamable-http" | "streamable_http" => McpTransport::Http,
|
||||
"stdio" | "local" => McpTransport::Stdio,
|
||||
_ if url.is_some() && command.is_none() => McpTransport::Http,
|
||||
_ => McpTransport::Stdio,
|
||||
};
|
||||
|
||||
match transport {
|
||||
McpTransport::Stdio if command.is_none() => continue,
|
||||
McpTransport::Http | McpTransport::Sse if url.is_none() => continue,
|
||||
_ => {}
|
||||
.or_else(|| fallback_name.map(str::trim).filter(|s| !s.is_empty()));
|
||||
if let (Some(name), Some(srv)) = (name, parse_one_server_def(root_obj)) {
|
||||
out.insert(name.to_owned(), srv);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
let args = obj
|
||||
.get("args")
|
||||
.and_then(Value::as_array)
|
||||
.map(|arr| arr.iter().filter_map(|v| v.as_str().map(str::to_owned)).collect())
|
||||
.unwrap_or_default();
|
||||
let servers_obj =
|
||||
root_obj.get("mcpServers").or_else(|| root_obj.get("servers")).and_then(Value::as_object).unwrap_or(root_obj);
|
||||
|
||||
out.insert(
|
||||
name.to_owned(),
|
||||
AgentMcpServer {
|
||||
transport,
|
||||
enabled: !disabled,
|
||||
command,
|
||||
args,
|
||||
env: parse_key_values_map(obj.get("env")),
|
||||
url,
|
||||
headers: parse_key_values_map(obj.get("headers")),
|
||||
agents: Vec::new(),
|
||||
},
|
||||
);
|
||||
for (name, def) in servers_obj {
|
||||
let name = name.trim();
|
||||
if name.is_empty() || matches!(name, "$schema" | "inputs" | "version") {
|
||||
continue;
|
||||
}
|
||||
let Some(obj) = def.as_object() else {
|
||||
continue;
|
||||
};
|
||||
if !is_server_def_object(obj) {
|
||||
continue;
|
||||
}
|
||||
if let Some(srv) = parse_one_server_def(obj) {
|
||||
out.insert(name.to_owned(), srv);
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
fn read_external_file(path: &Path) -> BTreeMap<String, AgentMcpServer> {
|
||||
let Ok(text) = std::fs::read_to_string(path) else {
|
||||
return BTreeMap::new();
|
||||
};
|
||||
let Ok(root) = parse_jsonc_value(&text) else {
|
||||
return BTreeMap::new();
|
||||
};
|
||||
extract_servers_from_root(&root, None)
|
||||
}
|
||||
|
||||
/// Probes a stdio MCP server by launching its command, performing the MCP `initialize` handshake,
|
||||
/// and requesting `tools/list`. For HTTP/SSE servers, use `ide-host`'s HTTP probe helper.
|
||||
pub async fn probe_stdio_server(server: &AgentMcpServer, cwd: &Path) -> McpProbeResult {
|
||||
let started = Instant::now();
|
||||
let elapsed_ms = || u32::try_from(started.elapsed().as_millis()).unwrap_or(u32::MAX);
|
||||
let Some(raw_cmd) = server.command.as_deref().map(str::trim).filter(|s| !s.is_empty()) else {
|
||||
return McpProbeResult {
|
||||
ok: false,
|
||||
server_name: None,
|
||||
server_version: None,
|
||||
protocol_version: None,
|
||||
tools: Vec::new(),
|
||||
duration_ms: elapsed_ms(),
|
||||
error: Some("Command is empty".into()),
|
||||
};
|
||||
};
|
||||
|
||||
let mut env: Vec<(String, String)> = ide_core::process::shell_env()
|
||||
.map(|(k, v)| (k.to_string_lossy().into_owned(), v.to_string_lossy().into_owned()))
|
||||
.collect();
|
||||
for (k, v) in &server.env {
|
||||
if let Some((_, existing)) = env.iter_mut().find(|(ek, _)| ek == k) {
|
||||
*existing = v.clone();
|
||||
} else {
|
||||
env.push((k.clone(), v.clone()));
|
||||
}
|
||||
}
|
||||
let search_path = env
|
||||
.iter()
|
||||
.find(|(k, _)| k.eq_ignore_ascii_case("PATH"))
|
||||
.map(|(_, v)| std::ffi::OsString::from(v))
|
||||
.or_else(|| ide_core::process::host_var_os("PATH"))
|
||||
.unwrap_or_default();
|
||||
let program = ide_core::process::find_executable(raw_cmd, &search_path)
|
||||
.map(|p| p.to_string_lossy().into_owned())
|
||||
.unwrap_or_else(|| raw_cmd.to_owned());
|
||||
|
||||
let stderr_lines = std::sync::Arc::new(parking_lot::Mutex::new(Vec::<String>::new()));
|
||||
let stderr_capture = stderr_lines.clone();
|
||||
let on_stderr: Box<dyn Fn(String) + Send + Sync> = Box::new(move |line| {
|
||||
let mut guard = stderr_capture.lock();
|
||||
if guard.len() < 12 && !line.trim().is_empty() {
|
||||
guard.push(line);
|
||||
}
|
||||
});
|
||||
|
||||
let peer = match ide_jsonrpc::Peer::spawn_framed(
|
||||
&program,
|
||||
&server.args,
|
||||
cwd,
|
||||
&env,
|
||||
std::sync::Arc::new(ide_jsonrpc::NoopHandler),
|
||||
on_stderr,
|
||||
ide_jsonrpc::Framing::Lines,
|
||||
) {
|
||||
Ok(peer) => peer,
|
||||
Err(e) => {
|
||||
return McpProbeResult {
|
||||
ok: false,
|
||||
server_name: None,
|
||||
server_version: None,
|
||||
protocol_version: None,
|
||||
tools: Vec::new(),
|
||||
duration_ms: elapsed_ms(),
|
||||
error: Some(format!("Failed to launch `{raw_cmd}`: {e}")),
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
let outcome = tokio::time::timeout(PROBE_TIMEOUT, async {
|
||||
let init: Value = peer
|
||||
.request(
|
||||
"initialize",
|
||||
json!({
|
||||
"protocolVersion": PROBE_PROTOCOL_VERSION,
|
||||
"capabilities": {},
|
||||
"clientInfo": crate::client_info(),
|
||||
}),
|
||||
None,
|
||||
)
|
||||
.await
|
||||
.map_err(|e| e.to_string())?;
|
||||
let _ = peer.notify("notifications/initialized", json!({}));
|
||||
let tools_reply: Value = peer.request("tools/list", json!({}), None).await.unwrap_or_else(|_| json!({}));
|
||||
Ok::<(Value, Value), String>((init, tools_reply))
|
||||
})
|
||||
.await;
|
||||
|
||||
peer.kill();
|
||||
|
||||
match outcome {
|
||||
Ok(Ok((init, tools_reply))) => parse_probe_payloads(&init, &tools_reply, elapsed_ms()),
|
||||
Ok(Err(err)) => {
|
||||
let tail = stderr_lines.lock().join("\n");
|
||||
let error = if tail.is_empty() { err } else { format!("{err}\n{tail}") };
|
||||
McpProbeResult {
|
||||
ok: false,
|
||||
server_name: None,
|
||||
server_version: None,
|
||||
protocol_version: None,
|
||||
tools: Vec::new(),
|
||||
duration_ms: elapsed_ms(),
|
||||
error: Some(error),
|
||||
}
|
||||
}
|
||||
Err(_) => {
|
||||
let tail = stderr_lines.lock().join("\n");
|
||||
let error = if tail.is_empty() {
|
||||
format!("Timed out after {}s waiting for MCP handshake", PROBE_TIMEOUT.as_secs())
|
||||
} else {
|
||||
format!("Timed out after {}s waiting for MCP handshake:\n{tail}", PROBE_TIMEOUT.as_secs())
|
||||
};
|
||||
McpProbeResult {
|
||||
ok: false,
|
||||
server_name: None,
|
||||
server_version: None,
|
||||
protocol_version: None,
|
||||
tools: Vec::new(),
|
||||
duration_ms: elapsed_ms(),
|
||||
error: Some(error),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse_probe_payloads(init: &Value, tools_reply: &Value, duration_ms: u32) -> McpProbeResult {
|
||||
let server_name = init.pointer("/serverInfo/name").and_then(Value::as_str).map(str::to_owned);
|
||||
let server_version = init.pointer("/serverInfo/version").and_then(Value::as_str).map(str::to_owned);
|
||||
let protocol_version = init.get("protocolVersion").and_then(Value::as_str).map(str::to_owned);
|
||||
let tools = tools_reply
|
||||
.get("tools")
|
||||
.and_then(Value::as_array)
|
||||
.map(|arr| {
|
||||
arr.iter()
|
||||
.filter_map(|t| {
|
||||
let name = t.get("name")?.as_str()?.trim();
|
||||
if name.is_empty() {
|
||||
return None;
|
||||
}
|
||||
let description = t
|
||||
.get("description")
|
||||
.and_then(Value::as_str)
|
||||
.map(str::trim)
|
||||
.filter(|s| !s.is_empty())
|
||||
.map(str::to_owned);
|
||||
Some(McpToolSummary { name: name.to_owned(), description })
|
||||
})
|
||||
.collect()
|
||||
})
|
||||
.unwrap_or_default();
|
||||
McpProbeResult { ok: true, server_name, server_version, protocol_version, tools, duration_ms, error: None }
|
||||
}
|
||||
|
||||
fn parse_key_values_map(val: Option<&Value>) -> BTreeMap<String, String> {
|
||||
match val {
|
||||
Some(Value::Object(map)) => map
|
||||
@@ -457,22 +1088,29 @@ mod tests {
|
||||
let init_http = Negotiated::from_initialize(&json!({
|
||||
"agentCapabilities": { "mcpCapabilities": { "http": true, "sse": false } }
|
||||
}));
|
||||
let claude_servers = acp_mcp_servers(&list, "claude-acp", &init_http);
|
||||
let empty_overrides = BTreeMap::new();
|
||||
let claude_servers = acp_mcp_servers(&list, "claude-acp", &init_http, &empty_overrides);
|
||||
assert_eq!(claude_servers.len(), 2);
|
||||
assert_eq!(claude_servers[0]["name"], "shared");
|
||||
assert_eq!(claude_servers[0]["args"], json!(["-y", "workspace-pkg"]));
|
||||
assert_eq!(claude_servers[1]["name"], "claude-http");
|
||||
assert_eq!(claude_servers[1]["type"], "http");
|
||||
|
||||
let codex_servers = acp_mcp_servers(&list, "codex", &init_http);
|
||||
let codex_servers = acp_mcp_servers(&list, "codex", &init_http, &empty_overrides);
|
||||
assert_eq!(codex_servers.len(), 1);
|
||||
assert_eq!(codex_servers[0]["name"], "shared");
|
||||
|
||||
// Per-session overrides can disable an enabled server or enable an agent-scoped server.
|
||||
let overrides = BTreeMap::from([("shared".to_owned(), false), ("claude-http".to_owned(), true)]);
|
||||
let codex_overridden = acp_mcp_servers(&list, "codex", &init_http, &overrides);
|
||||
assert_eq!(codex_overridden.len(), 1);
|
||||
assert_eq!(codex_overridden[0]["name"], "claude-http");
|
||||
|
||||
// Disable workspace `shared` and remove user `claude-http`.
|
||||
apply_set_enabled(&mut user, &mut workspace, "workspace::shared", false).unwrap();
|
||||
apply_remove(&mut user, &mut workspace, "user::claude-http").unwrap();
|
||||
let list = servers_from_settings(&user, &workspace);
|
||||
let after = acp_mcp_servers(&list, "claude", &init_http);
|
||||
let after = acp_mcp_servers(&list, "claude", &init_http, &empty_overrides);
|
||||
assert_eq!(after.len(), 1);
|
||||
assert_eq!(after[0]["args"], json!(["-y", "user-pkg"]));
|
||||
}
|
||||
@@ -504,4 +1142,124 @@ mod tests {
|
||||
assert_eq!(std::fs::read_to_string(&mcp_json).unwrap(), raw_mcp);
|
||||
assert_eq!(std::fs::read_to_string(&vscode_mcp).unwrap(), raw_vscode);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn discovers_cli_sources_top_level_maps_and_jsonc_and_imports_selected() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let home = tmp.path().join("home");
|
||||
let proj = tmp.path().join("project");
|
||||
std::fs::create_dir_all(home.join(".copilot")).unwrap();
|
||||
std::fs::create_dir_all(home.join(".codex")).unwrap();
|
||||
std::fs::create_dir_all(home.join(".gemini")).unwrap();
|
||||
std::fs::create_dir_all(home.join(".jetbrains").join("mcp")).unwrap();
|
||||
std::fs::create_dir_all(home.join(".config").join("github-copilot").join("intellij")).unwrap();
|
||||
std::fs::create_dir_all(&proj).unwrap();
|
||||
|
||||
std::fs::write(
|
||||
home.join(".copilot").join("mcp-config.json"),
|
||||
r#"{"mcpServers":{"github":{"type":"http","url":"https://api.githubcopilot.com/mcp/","headers":{"Authorization":"Bearer tok"}},"slack":{"type":"local","command":"npx","args":["-y","slack-mcp"],"env":{"SLACK_TOKEN":"xoxb"}}}}"#,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
// Codex `~/.codex/config.toml` `[mcp_servers]` format.
|
||||
std::fs::write(
|
||||
home.join(".codex").join("config.toml"),
|
||||
"[mcp_servers.postgres]\ncommand = \"npx\"\nargs = [\"-y\", \"@modelcontextprotocol/server-postgres\"]\n\n[mcp_servers.remote_api]\nurl = \"https://mcp.example.com/sse\"\ntype = \"sse\"\nhttp_headers = { Authorization = \"Bearer codex-tok\" }\n",
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
// Gemini `~/.gemini/settings.json` with unrelated top-level settings alongside `mcpServers`.
|
||||
std::fs::write(
|
||||
home.join(".gemini").join("settings.json"),
|
||||
r#"{"theme":"Default","mcpServers":{"fetch":{"command":"uvx","args":["mcp-server-fetch"]}}}"#,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
// JetBrains Air top-level server map (not nested under `mcpServers`).
|
||||
std::fs::write(
|
||||
home.join(".jetbrains").join("mcp").join("mcp.json"),
|
||||
r#"{"linear":{"type":"stdio","command":"npx","args":["-y","mcp-remote","https://mcp.linear.app/mcp"]}}"#,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
// JSONC comments + trailing commas.
|
||||
std::fs::write(
|
||||
home.join(".config").join("github-copilot").join("intellij").join("mcp.json"),
|
||||
"// comment with https://url/inside\n{\n /* block comment */\n \"servers\": {\n \"docs\": { \"command\": \"uvx\", \"args\": [\"docs-mcp\",], },\n },\n}\n",
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let sources = discover_external_sources(Some(&home), Some(&proj));
|
||||
assert_eq!(sources.len(), 5);
|
||||
let copilot = sources.iter().find(|s| s.id == "copilot-cli").unwrap();
|
||||
assert_eq!(copilot.native_provider.as_deref(), Some("copilot"));
|
||||
assert_eq!(copilot.servers.len(), 2);
|
||||
let gh_srv = copilot.servers.iter().find(|s| s.name == "github").unwrap();
|
||||
assert_eq!(gh_srv.header_keys, vec!["Authorization".to_string()]);
|
||||
|
||||
let codex = sources.iter().find(|s| s.id == "codex").unwrap();
|
||||
assert_eq!(codex.native_provider.as_deref(), Some("codex"));
|
||||
assert_eq!(codex.servers.len(), 2);
|
||||
|
||||
let gemini = sources.iter().find(|s| s.id == "gemini").unwrap();
|
||||
assert_eq!(gemini.servers[0].name, "fetch");
|
||||
|
||||
let air = sources.iter().find(|s| s.id == "jetbrains-air").unwrap();
|
||||
assert_eq!(air.servers[0].name, "linear");
|
||||
|
||||
let ij = sources.iter().find(|s| s.id == "copilot-intellij").unwrap();
|
||||
assert_eq!(ij.servers[0].name, "docs");
|
||||
|
||||
let mut user = BTreeMap::new();
|
||||
let mut workspace = BTreeMap::new();
|
||||
let imported = apply_import_selected(
|
||||
&mut user,
|
||||
&mut workspace,
|
||||
Some(&home),
|
||||
Some(&proj),
|
||||
McpScope::User,
|
||||
&[
|
||||
McpImportSelection { source_id: "copilot-cli".into(), name: "github".into() },
|
||||
McpImportSelection { source_id: "jetbrains-air".into(), name: "linear".into() },
|
||||
],
|
||||
false,
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(imported, vec!["github".to_string(), "linear".to_string()]);
|
||||
assert_eq!(user["github"].headers.get("Authorization").map(String::as_str), Some("Bearer tok"));
|
||||
assert_eq!(user["linear"].command.as_deref(), Some("npx"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn apply_save_json_accepts_mcp_servers_map_and_single_server_object() {
|
||||
let mut user = BTreeMap::new();
|
||||
let mut workspace = BTreeMap::new();
|
||||
|
||||
let names = apply_save_json(
|
||||
&mut user,
|
||||
&mut workspace,
|
||||
true,
|
||||
McpScope::User,
|
||||
r#"{"mcpServers":{"playwright":{"command":"npx","args":["@playwright/mcp@latest"]}}}"#,
|
||||
None,
|
||||
vec!["copilot".into(), "claude".into()],
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(names, vec!["playwright".to_string()]);
|
||||
assert_eq!(user["playwright"].agents, vec!["copilot".to_string(), "claude".to_string()]);
|
||||
|
||||
let single = apply_save_json(
|
||||
&mut user,
|
||||
&mut workspace,
|
||||
true,
|
||||
McpScope::Workspace,
|
||||
r#"{"type":"sse","serverUrl":"https://sse.example.com/events"}"#,
|
||||
Some("events-sse"),
|
||||
Vec::new(),
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(single, vec!["events-sse".to_string()]);
|
||||
assert_eq!(workspace["events-sse"].transport, McpTransport::Sse);
|
||||
assert_eq!(workspace["events-sse"].url.as_deref(), Some("https://sse.example.com/events"));
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,15 @@ use serde::{Deserialize, Serialize};
|
||||
use specta::Type;
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
/// Why a task needs the user's attention (native / in-app notify).
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize, Type)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub enum AgentAttentionReason {
|
||||
Finished,
|
||||
Input,
|
||||
Failed,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize, Type)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub enum AgentStatus {
|
||||
@@ -36,14 +45,18 @@ pub struct AgentCapabilities {
|
||||
pub steer: bool,
|
||||
/// It reports its background tasks and stops them one by one.
|
||||
pub background_tasks: bool,
|
||||
/// The task runs read-only: it asks before anything, and requests that would change
|
||||
/// the project are denied (a temporary side conversation).
|
||||
pub read_only: bool,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize, Type)]
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize, Type)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub enum Isolation {
|
||||
/// A new git worktree on its own branch.
|
||||
Worktree,
|
||||
/// The project's own checkout.
|
||||
#[default]
|
||||
Local,
|
||||
}
|
||||
|
||||
@@ -95,6 +108,9 @@ pub struct AgentTask {
|
||||
// Boxed so that `AgentEvent::Task` stays within clippy's `large_enum_variant` bound on Windows.
|
||||
#[serde(default)]
|
||||
pub background: Box<Vec<AgentBackgroundTask>>,
|
||||
/// Per-session MCP server enable/disable overrides (`server_name` -> `enabled`), including `"openidea"`.
|
||||
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
|
||||
pub mcp_overrides: BTreeMap<String, bool>,
|
||||
}
|
||||
|
||||
/// A task the agent runs in the background while the conversation goes on.
|
||||
@@ -120,6 +136,40 @@ pub struct AgentRetry {
|
||||
pub error: Option<String>,
|
||||
}
|
||||
|
||||
/// Token pricing (Credits per 1M tokens) and context window capacity for one tier.
|
||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Type)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct AgentTokenPricing {
|
||||
pub input: f64,
|
||||
pub output: f64,
|
||||
#[serde(default)]
|
||||
#[specta(optional)]
|
||||
pub cache_read: Option<f64>,
|
||||
#[serde(default)]
|
||||
#[specta(optional)]
|
||||
pub cache_write: Option<f64>,
|
||||
#[serde(default)]
|
||||
#[specta(optional)]
|
||||
pub context_tokens: Option<u32>,
|
||||
}
|
||||
|
||||
/// Pricing and context-tier metadata attached to a model [`AgentConfigValue`].
|
||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Type)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct AgentModelPricing {
|
||||
/// `"low"`, `"medium"`, `"high"`, or `"very_high"`.
|
||||
#[serde(default)]
|
||||
#[specta(optional)]
|
||||
pub cost_tier: Option<String>,
|
||||
/// Request-based billing multiplier (e.g. `1.0`, `0.33`, `3.0`), when applicable.
|
||||
#[serde(default)]
|
||||
#[specta(optional)]
|
||||
pub multiplier: Option<f64>,
|
||||
/// Token pricing keyed by context tier (`"default"`, `"long_context"`).
|
||||
#[serde(default)]
|
||||
pub tiers: BTreeMap<String, AgentTokenPricing>,
|
||||
}
|
||||
|
||||
/// One value of a select-type session setting.
|
||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Type)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
@@ -127,6 +177,18 @@ pub struct AgentConfigValue {
|
||||
pub value: String,
|
||||
pub name: String,
|
||||
pub description: Option<String>,
|
||||
/// Relative cost category from ACP `_meta` (`copilotPriceCategory`, e.g. `low`, `medium`, `high`, `very_high`).
|
||||
#[serde(default)]
|
||||
#[specta(optional)]
|
||||
pub cost_category: Option<String>,
|
||||
/// Usage multiplier from ACP `_meta` (`copilotUsage`, e.g. `1x`, `0x`, `3x`).
|
||||
#[serde(default)]
|
||||
#[specta(optional)]
|
||||
pub usage_multiplier: Option<String>,
|
||||
/// Detailed per-tier token pricing from `_meta.pricing` when available.
|
||||
#[serde(default)]
|
||||
#[specta(optional)]
|
||||
pub pricing: Option<Box<AgentModelPricing>>,
|
||||
}
|
||||
|
||||
/// How a setting is changed.
|
||||
@@ -152,6 +214,10 @@ pub struct AgentConfigOption {
|
||||
/// ACP category: `model`, `mode`, `thought_level`, or agent-specific.
|
||||
pub category: Option<String>,
|
||||
pub current: Option<String>,
|
||||
/// Initial default value reported by the agent, preserved across session updates for Reset.
|
||||
#[serde(default)]
|
||||
#[specta(optional)]
|
||||
pub default_value: Option<String>,
|
||||
pub values: Vec<AgentConfigValue>,
|
||||
pub via: AgentConfigVia,
|
||||
}
|
||||
@@ -313,6 +379,59 @@ pub struct AgentQuestionOption {
|
||||
pub preview: Option<String>,
|
||||
}
|
||||
|
||||
/// A form the agent asks the user to fill in (ACP `elicitation/create`, form mode).
|
||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Type)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct AgentForm {
|
||||
pub fields: Vec<AgentFormField>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Type)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct AgentFormField {
|
||||
/// The property name the answer is keyed by.
|
||||
pub name: String,
|
||||
pub title: String,
|
||||
#[serde(default)]
|
||||
pub description: String,
|
||||
#[serde(default)]
|
||||
pub required: bool,
|
||||
pub kind: AgentFormFieldKind,
|
||||
/// The choices of a `choice` or `choices` field.
|
||||
#[serde(default)]
|
||||
pub options: Vec<AgentFormOption>,
|
||||
/// The initial answer: option values, a text, a number, or `true` / `false`.
|
||||
#[serde(default)]
|
||||
pub default: Vec<String>,
|
||||
/// Bounds: the number of a `number` / `integer`, the length of a `text`, the count of `choices`.
|
||||
#[serde(default)]
|
||||
pub min: Option<f64>,
|
||||
#[serde(default)]
|
||||
pub max: Option<f64>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize, Type)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub enum AgentFormFieldKind {
|
||||
Text,
|
||||
Number,
|
||||
Integer,
|
||||
Boolean,
|
||||
/// One of `options`.
|
||||
Choice,
|
||||
/// Any of `options`.
|
||||
Choices,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Type)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct AgentFormOption {
|
||||
pub value: String,
|
||||
pub label: String,
|
||||
#[serde(default)]
|
||||
pub description: String,
|
||||
}
|
||||
|
||||
/// One item of a task's conversation.
|
||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Type)]
|
||||
#[serde(tag = "type", rename_all = "camelCase", rename_all_fields = "camelCase")]
|
||||
@@ -384,11 +503,19 @@ pub enum AgentEntry {
|
||||
Permission {
|
||||
id: u32,
|
||||
title: String,
|
||||
/// The shell command the request asks to run, from its structured input; `None` for
|
||||
/// any other request, whatever its title says.
|
||||
#[serde(default)]
|
||||
command: Option<String>,
|
||||
options: Vec<AgentPermissionOption>,
|
||||
/// Set when the request asks `AskUserQuestion`'s questions; answered with a JSON object
|
||||
/// of question to answer instead of an option.
|
||||
#[serde(default)]
|
||||
questions: Vec<AgentQuestion>,
|
||||
/// Set when the agent asks for a form (ACP elicitation); answered with a JSON object of
|
||||
/// field name to its values, `decline`, or nothing to cancel.
|
||||
#[serde(default)]
|
||||
form: Option<AgentForm>,
|
||||
/// Name of the chosen option, or `None` while pending. Cancelled requests read "Cancelled".
|
||||
chosen: Option<String>,
|
||||
},
|
||||
@@ -469,7 +596,7 @@ pub struct AgentProbe {
|
||||
pub error: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize, Type)]
|
||||
#[derive(Clone, Debug, Default, Serialize, Deserialize, Type)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct NewAgentTask {
|
||||
/// Validated by the host; defaults to the window project root.
|
||||
@@ -482,9 +609,13 @@ pub struct NewAgentTask {
|
||||
pub isolation: Isolation,
|
||||
/// Branch to start the worktree from; the current branch when `None`.
|
||||
pub base: Option<String>,
|
||||
/// Per-session MCP server enable/disable overrides (`server_name` -> `enabled`).
|
||||
#[serde(default)]
|
||||
pub mcp_overrides: BTreeMap<String, bool>,
|
||||
}
|
||||
|
||||
/// A file the task changed, relative to [`AgentTask::base_commit`].
|
||||
/// A file the task changed: against [`AgentTask::base_commit`], or against `HEAD` in the
|
||||
/// grouped view.
|
||||
#[derive(Clone, Debug, Serialize, Deserialize, Type)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct AgentChange {
|
||||
@@ -493,6 +624,57 @@ pub struct AgentChange {
|
||||
pub kind: ide_vcs::ChangeKind,
|
||||
pub added: u32,
|
||||
pub removed: u32,
|
||||
/// Path before a rename; the left side of its diff reads this path.
|
||||
pub old_path: Option<String>,
|
||||
}
|
||||
|
||||
/// A commit made while the task ran, with the files it changed against its first parent.
|
||||
#[derive(Clone, Debug, Serialize, Deserialize, Type)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct AgentCommit {
|
||||
pub id: String,
|
||||
pub short_id: String,
|
||||
pub summary: String,
|
||||
pub message: String,
|
||||
pub time_ms: f64,
|
||||
/// First parent, or `None` for a root commit.
|
||||
pub parent: Option<String>,
|
||||
pub files: Vec<AgentChange>,
|
||||
}
|
||||
|
||||
/// Which comparison a rollback of the task's changes uses.
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize, Type)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub enum AgentChangesView {
|
||||
/// Everything since the commit the task started from.
|
||||
#[default]
|
||||
SinceStart,
|
||||
/// The uncommitted files against `HEAD`; commits made since the start stay.
|
||||
Group,
|
||||
}
|
||||
|
||||
/// A task's uncommitted files and the commits made since it started.
|
||||
#[derive(Clone, Debug, Default, Serialize, Deserialize, Type)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct AgentChanges {
|
||||
/// Files changed against `HEAD`, less those changed before the task started and untouched since.
|
||||
pub uncommitted: Vec<AgentChange>,
|
||||
/// Commits from the task's start (exclusive) to `HEAD` (inclusive), newest first. Empty
|
||||
/// for tasks working in the project's own checkout.
|
||||
pub commits: Vec<AgentCommit>,
|
||||
}
|
||||
|
||||
/// Authoritative choices for new tasks, independent of any running session's settings.
|
||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Type)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct AgentModelCatalog {
|
||||
pub provider: String,
|
||||
pub config: Vec<AgentConfigOption>,
|
||||
pub thought_levels: BTreeMap<String, Vec<AgentConfigOption>>,
|
||||
pub refreshing: bool,
|
||||
pub refreshed_at_ms: Option<f64>,
|
||||
pub retry_at_ms: Option<f64>,
|
||||
pub error: Option<String>,
|
||||
}
|
||||
|
||||
/// Streamed to the window that subscribed (`AgentManager::subscribe`).
|
||||
@@ -501,7 +683,7 @@ pub struct AgentChange {
|
||||
pub enum AgentEvent {
|
||||
/// A task was created or its metadata changed.
|
||||
Task {
|
||||
task: AgentTask,
|
||||
task: Box<AgentTask>,
|
||||
},
|
||||
Removed {
|
||||
id: String,
|
||||
@@ -532,8 +714,18 @@ pub enum AgentEvent {
|
||||
provider: String,
|
||||
limits: Vec<AgentLimit>,
|
||||
},
|
||||
/// A model lookup started or completed, including unsuccessful lookups.
|
||||
ModelCatalog {
|
||||
catalog: AgentModelCatalog,
|
||||
},
|
||||
/// The installed or configured agent providers changed.
|
||||
Providers,
|
||||
/// The task needs attention once (finished turn, explicit input, or failure).
|
||||
Attention {
|
||||
task: String,
|
||||
event_id: String,
|
||||
reason: AgentAttentionReason,
|
||||
},
|
||||
}
|
||||
|
||||
pub use ide_core::settings::{AgentMcpServer, McpTransport};
|
||||
@@ -571,6 +763,9 @@ pub struct McpServer {
|
||||
/// Agent provider ids this server is enabled for; empty means enabled for all agents.
|
||||
#[serde(default)]
|
||||
pub agents: Vec<String>,
|
||||
/// True when this Workspace server was added outside OpenIDEA and has not been approved yet.
|
||||
#[serde(default)]
|
||||
pub unapproved: bool,
|
||||
}
|
||||
|
||||
/// Payload for creating or updating a user or workspace MCP server.
|
||||
@@ -602,3 +797,112 @@ pub struct McpServerInput {
|
||||
fn default_true() -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
/// An external MCP configuration file discovered on disk (user CLI config or workspace file).
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Type)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct McpExternalSource {
|
||||
/// Stable source identifier (e.g. `"copilot-cli"`, `"jetbrains-air"`, `"claude-code"`, `"cursor"`, `"workspace-mcp"`, `"vscode-mcp"`).
|
||||
pub id: String,
|
||||
/// Human-readable source label (e.g. `"GitHub Copilot CLI"`).
|
||||
pub label: String,
|
||||
/// Absolute path of the discovered configuration file.
|
||||
pub path: IpcPath,
|
||||
/// Suggested target scope when importing this source into OpenIDEA settings.
|
||||
pub suggested_scope: McpScope,
|
||||
/// Agent provider ID this external CLI reads natively (`Some("copilot")`, `Some("claude")`, `Some("cursor")`, or `None`).
|
||||
pub native_provider: Option<String>,
|
||||
/// Servers found inside this external configuration file (values omitted for safety; imported directly on the host).
|
||||
pub servers: Vec<McpDiscoveredServer>,
|
||||
}
|
||||
|
||||
/// Summary of one server discovered inside an external MCP configuration file.
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Type)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct McpDiscoveredServer {
|
||||
pub name: String,
|
||||
pub transport: McpTransport,
|
||||
pub enabled: bool,
|
||||
pub command: Option<String>,
|
||||
pub args: Vec<String>,
|
||||
pub url: Option<String>,
|
||||
/// Environment variable names declared on this server (values stay on the host).
|
||||
pub env_keys: Vec<String>,
|
||||
/// HTTP header names declared on this server (values stay on the host).
|
||||
pub header_keys: Vec<String>,
|
||||
}
|
||||
|
||||
/// Selection of one server from an external MCP configuration source to import.
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Type)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct McpImportSelection {
|
||||
pub source_id: String,
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
/// One tool advertised by an MCP server during `tools/list`.
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Type)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct McpToolSummary {
|
||||
pub name: String,
|
||||
pub description: Option<String>,
|
||||
}
|
||||
|
||||
/// Result of testing/probing an MCP server (`initialize` + `tools/list`).
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Type)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct McpProbeResult {
|
||||
pub ok: bool,
|
||||
pub server_name: Option<String>,
|
||||
pub server_version: Option<String>,
|
||||
pub protocol_version: Option<String>,
|
||||
pub tools: Vec<McpToolSummary>,
|
||||
pub duration_ms: u32,
|
||||
pub error: Option<String>,
|
||||
}
|
||||
|
||||
/// Payload for creating a custom ACP agent (always the ACP driver); the id
|
||||
/// becomes the `<config>/agents/<id>.toml` file name.
|
||||
#[derive(Clone, Debug, Deserialize, Type)]
|
||||
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
||||
pub struct CustomAgentInput {
|
||||
pub id: String,
|
||||
pub name: String,
|
||||
pub command: String,
|
||||
#[serde(default)]
|
||||
pub args: Vec<String>,
|
||||
#[serde(default)]
|
||||
pub env: BTreeMap<String, String>,
|
||||
}
|
||||
|
||||
/// What importing one entry of a JetBrains `acp.json` file did.
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Type)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct AcpImportOutcome {
|
||||
/// The `agent_servers` key the entry was imported from.
|
||||
pub name: String,
|
||||
/// The id its agent file would get (also when skipped).
|
||||
pub id: String,
|
||||
pub saved: bool,
|
||||
/// Why the entry was skipped; empty when it was saved.
|
||||
#[serde(default)]
|
||||
pub reason: String,
|
||||
}
|
||||
|
||||
/// What accepting a worktree task's changes copied into the local checkout.
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Type)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct AgentAcceptOutcome {
|
||||
/// Relative paths copied into the checkout (uncommitted), including ones already identical.
|
||||
pub accepted: Vec<String>,
|
||||
/// Files left alone because the checkout has its own edits to them.
|
||||
pub skipped: Vec<AgentAcceptSkipped>,
|
||||
}
|
||||
|
||||
/// A file accept left alone, with why.
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Type)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct AgentAcceptSkipped {
|
||||
pub path: String,
|
||||
pub reason: String,
|
||||
}
|
||||
@@ -31,7 +31,7 @@ pub(super) fn transcript(config_dir: &Path, cwd: &Path, id: &str) -> PathBuf {
|
||||
}
|
||||
|
||||
/// The project directory name Claude Code uses for `cwd`.
|
||||
pub(super) fn escape(cwd: &Path) -> String {
|
||||
pub(crate) fn escape(cwd: &Path) -> String {
|
||||
cwd.to_string_lossy().chars().map(|c| if c.is_ascii_alphanumeric() { c } else { '-' }).collect()
|
||||
}
|
||||
|
||||
@@ -158,6 +158,42 @@ pub(crate) fn title(entries: &[Value]) -> Option<String> {
|
||||
.map(str::to_owned)
|
||||
}
|
||||
|
||||
/// Claude Code titles a session from the last 1000 characters of its text.
|
||||
const TITLE_SOURCE_CHARS: usize = 1000;
|
||||
/// Below this Claude Code gives no title without asking the model.
|
||||
const TITLE_SOURCE_MIN_CHARS: usize = 10;
|
||||
|
||||
/// The conversation's own text to title the session after: the prompts and the replies,
|
||||
/// without the files attached to the prompts. `None` while there is too little.
|
||||
pub(super) fn title_source(entries: &[Value]) -> Option<String> {
|
||||
let mut text = String::new();
|
||||
for e in chain(entries, None) {
|
||||
if !(is_turn_start(e) || kind(e) == "assistant") {
|
||||
continue;
|
||||
}
|
||||
let blocks = match e.pointer("/message/content") {
|
||||
Some(Value::String(s)) => vec![s.as_str()],
|
||||
Some(Value::Array(blocks)) => blocks
|
||||
.iter()
|
||||
.filter(|b| b.get("type").and_then(Value::as_str) == Some("text"))
|
||||
.filter_map(|b| b.get("text").and_then(Value::as_str))
|
||||
.filter(|t| !t.starts_with("\n<context ref="))
|
||||
.collect(),
|
||||
_ => Vec::new(),
|
||||
};
|
||||
for block in blocks {
|
||||
text.push_str(block);
|
||||
text.push('\n');
|
||||
}
|
||||
}
|
||||
let text = text.trim();
|
||||
let len = text.chars().count();
|
||||
if len < TITLE_SOURCE_MIN_CHARS {
|
||||
return None;
|
||||
}
|
||||
Some(text.chars().skip(len.saturating_sub(TITLE_SOURCE_CHARS)).collect())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@@ -219,6 +255,29 @@ mod tests {
|
||||
assert_eq!(cut(&c, 1), Ok(None));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn titles_come_from_prompts_and_replies() {
|
||||
let mut prompt = user("u1", None, "Fix the login form");
|
||||
prompt["message"]["content"]
|
||||
.as_array_mut()
|
||||
.unwrap()
|
||||
.push(json!({"type": "text", "text": "\n<context ref=\"file:///p/a.rs\">\nfn a() {}\n</context>"}));
|
||||
let entries = vec![
|
||||
prompt,
|
||||
assistant(
|
||||
"a1",
|
||||
"u1",
|
||||
json!([{"type": "thinking", "thinking": "hm"}, {"type": "text", "text": "Done."},
|
||||
{"type": "tool_use", "id": "t1", "name": "Bash", "input": {}}]),
|
||||
),
|
||||
json!({"type": "user", "uuid": "r1", "parentUuid": "a1", "message": {"content": [{"type": "tool_result", "tool_use_id": "t1", "content": "out"}]}}),
|
||||
];
|
||||
assert_eq!(title_source(&entries).as_deref(), Some("Fix the login form\nDone."));
|
||||
assert_eq!(title_source(&[user("u1", None, "hi")]), None);
|
||||
let long = "x".repeat(TITLE_SOURCE_CHARS + 5);
|
||||
assert_eq!(title_source(&[user("u1", None, &long)]).map(|t| t.chars().count()), Some(TITLE_SOURCE_CHARS));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn project_directory_names() {
|
||||
assert_eq!(escape(Path::new("/private/tmp/a.b/c_d")), "-private-tmp-a-b-c-d");
|
||||
|
||||
@@ -85,6 +85,8 @@ pub(crate) async fn discover(spec: ProviderSpec, cwd: PathBuf) -> Result<Discove
|
||||
let (spec, cwd) = spec.await.map_err(|e| e.to_string())?;
|
||||
let mut args = spec.args;
|
||||
args.extend(Start::New { id: uuid::Uuid::new_v4().to_string() }.args());
|
||||
// Metadata discovery must not launch project-configured MCP servers.
|
||||
args.extend(["--strict-mcp-config".into(), "--mcp-config".into(), "{\"mcpServers\":{}}".into()]);
|
||||
let launch = Launch { program: spec.command, args, cwd, env: spec.env };
|
||||
let mut io = cli::Process.launch(&launch, Arc::new(|_| {})).map_err(|e| e.to_string())?;
|
||||
let _stop = io.kill.clone().drop_guard();
|
||||
@@ -191,6 +193,8 @@ struct State {
|
||||
limit_resets_ms: Option<f64>,
|
||||
terminal_only: Vec<String>,
|
||||
titled: bool,
|
||||
/// A title is being read or generated.
|
||||
titling: bool,
|
||||
}
|
||||
|
||||
struct Turn {
|
||||
@@ -365,6 +369,7 @@ impl Shim {
|
||||
limit_resets_ms: None,
|
||||
terminal_only: Vec::new(),
|
||||
titled: false,
|
||||
titling: false,
|
||||
}),
|
||||
cwd,
|
||||
starting: tokio::sync::Mutex::new(()),
|
||||
@@ -583,11 +588,12 @@ impl Shim {
|
||||
impl super::Shim for Shim {
|
||||
fn attach(&self, client: Peer) {
|
||||
let _ = self.client.set(client.clone());
|
||||
// The client went away: so do the processes.
|
||||
let weak = self.me.clone();
|
||||
// The client went away: so do the processes. Held until then: the connection drops
|
||||
// its handler (this shim) as it closes, and the sessions would go without stopping.
|
||||
let me = self.me.upgrade();
|
||||
tokio::spawn(async move {
|
||||
client.closed().await;
|
||||
if let Some(me) = weak.upgrade() {
|
||||
if let Some(me) = me {
|
||||
me.closed.store(true, Ordering::Release);
|
||||
let sessions: Vec<Arc<Session>> = me.sessions.lock().drain().map(|(_, s)| s).collect();
|
||||
for s in sessions {
|
||||
@@ -598,6 +604,15 @@ impl super::Shim for Shim {
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for Session {
|
||||
/// A session dropped without being stopped (the shim went away) takes its process along.
|
||||
fn drop(&mut self) {
|
||||
if let Some(p) = self.st.get_mut().proc.take() {
|
||||
p.kill.cancel();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Session {
|
||||
fn shim(&self) -> Option<Arc<Shim>> {
|
||||
self.shim.upgrade()
|
||||
@@ -1048,7 +1063,8 @@ impl Session {
|
||||
} else if matches!(st.start, Start::New { .. }) {
|
||||
st.start = Start::Resume { id: self.id.clone(), at: None };
|
||||
}
|
||||
read_title = !st.titled;
|
||||
read_title = !st.titled && !st.titling;
|
||||
st.titling |= read_title;
|
||||
if let Some(turn) = st.turn.as_ref()
|
||||
&& belongs(turn, &msg)
|
||||
{
|
||||
@@ -1073,13 +1089,29 @@ impl Session {
|
||||
}
|
||||
}
|
||||
|
||||
/// Sends the title Claude Code gave the session, once it has one.
|
||||
/// Sends the session's title once it has one: the one in its transcript, or else one
|
||||
/// Claude Code is asked for, as it titles no session run the way the Agent SDK runs it.
|
||||
/// Without a title the next turn tries again.
|
||||
fn read_title(self: &Arc<Self>) {
|
||||
let me = self.clone();
|
||||
tokio::task::spawn_blocking(move || {
|
||||
let Some(title) = chain_title(&me) else { return };
|
||||
me.st.lock().titled = true;
|
||||
if let Some(shim) = me.shim() {
|
||||
tokio::spawn(async move {
|
||||
let reader = me.clone();
|
||||
let entries = tokio::task::spawn_blocking(move || transcript_entries(&reader)).await.ok().flatten();
|
||||
let mut title = entries.as_deref().and_then(history::title);
|
||||
if title.is_none()
|
||||
&& let Some(source) = entries.as_deref().and_then(history::title_source)
|
||||
{
|
||||
title = me.generate_title(source).await;
|
||||
}
|
||||
{
|
||||
let mut st = me.st.lock();
|
||||
st.titling = false;
|
||||
if title.is_none() {
|
||||
return;
|
||||
}
|
||||
st.titled = true;
|
||||
}
|
||||
if let (Some(title), Some(shim)) = (title, me.shim()) {
|
||||
shim.notify(vec![Update {
|
||||
session: me.id.clone(),
|
||||
update: json!({ "sessionUpdate": "session_info_update", "title": title }),
|
||||
@@ -1088,6 +1120,19 @@ impl Session {
|
||||
});
|
||||
}
|
||||
|
||||
/// A title Claude Code generates from `source`, also written to the transcript.
|
||||
async fn generate_title(&self, source: String) -> Option<String> {
|
||||
let proc = self.st.lock().proc.clone()?;
|
||||
let request = json!({ "subtype": "generate_session_title", "description": source, "persist": true });
|
||||
match proc.control(request).await {
|
||||
Ok(r) => r.get("title").and_then(Value::as_str).map(str::trim).filter(|t| !t.is_empty()).map(str::to_owned),
|
||||
Err(e) => {
|
||||
tracing::debug!("generating a claude session title: {e}");
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn on_cli_request(self: &Arc<Self>, proc: &Arc<Proc>, msg: Value) {
|
||||
let id = msg.get("request_id").and_then(Value::as_str).unwrap_or_default().to_owned();
|
||||
let request = msg.get("request").cloned().unwrap_or_default();
|
||||
@@ -1417,9 +1462,13 @@ fn strings(v: Option<&Value>) -> Vec<String> {
|
||||
|
||||
/// The title in a session's transcript (or that of the session it continues).
|
||||
fn chain_title(session: &Session) -> Option<String> {
|
||||
history::title(&transcript_entries(session)?)
|
||||
}
|
||||
|
||||
fn transcript_entries(session: &Session) -> Option<Vec<Value>> {
|
||||
let shim = session.shim()?;
|
||||
let path = shim.transcript(&session.cwd, &session.id).filter(|p| p.exists())?;
|
||||
history::title(&history::read(&path).ok()?)
|
||||
history::read(&path).ok()
|
||||
}
|
||||
|
||||
fn start_json(start: &Start) -> Value {
|
||||
|
||||
@@ -311,6 +311,8 @@ struct Replay {
|
||||
sent: Arc<Mutex<Vec<Vec<Value>>>>,
|
||||
launched: Mutex<Vec<Vec<String>>>,
|
||||
errors: Arc<Mutex<Vec<String>>>,
|
||||
/// Each process's kill token.
|
||||
kills: Mutex<Vec<CancellationToken>>,
|
||||
}
|
||||
|
||||
impl Cli for Replay {
|
||||
@@ -331,6 +333,7 @@ impl Cli for Replay {
|
||||
let (shim_in, cli_in) = tokio::io::duplex(1 << 20);
|
||||
let (mut cli_out, shim_out) = tokio::io::duplex(1 << 20);
|
||||
let kill = CancellationToken::new();
|
||||
self.kills.lock().push(kill.clone());
|
||||
let (sent, errors, token) = (self.sent.clone(), self.errors.clone(), kill.clone());
|
||||
sent.lock().push(Vec::new());
|
||||
tokio::spawn(async move {
|
||||
@@ -421,14 +424,18 @@ impl Replayed {
|
||||
}
|
||||
}
|
||||
|
||||
async fn replay(name: &str) -> Replayed {
|
||||
let processes = load_fixture(name);
|
||||
let cli = Arc::new(Replay {
|
||||
fn replay_cli(processes: Vec<Vec<Frame>>) -> Arc<Replay> {
|
||||
Arc::new(Replay {
|
||||
processes: Mutex::new(processes.into()),
|
||||
sent: Arc::default(),
|
||||
launched: Mutex::default(),
|
||||
errors: Arc::default(),
|
||||
});
|
||||
kills: Mutex::default(),
|
||||
})
|
||||
}
|
||||
|
||||
async fn replay(name: &str) -> Replayed {
|
||||
let cli = replay_cli(load_fixture(name));
|
||||
let (config, cwd) = (tempfile::tempdir().unwrap(), tempfile::tempdir().unwrap());
|
||||
let cwd_path = std::fs::canonicalize(cwd.path()).unwrap();
|
||||
let h =
|
||||
@@ -461,6 +468,69 @@ async fn replay_simple() {
|
||||
assert_eq!(user["message"]["content"][0]["text"], "Reply with exactly: hello there");
|
||||
}
|
||||
|
||||
/// The app closing the connection (an idle task released, archived or deleted) stops the
|
||||
/// CLI processes of its sessions, idle ones included.
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn closing_the_connection_stops_the_processes() {
|
||||
let r = replay("simple").await;
|
||||
assert!(r.cli.kills.lock().iter().all(|k| !k.is_cancelled()));
|
||||
r.h.client.kill();
|
||||
let kills = r.cli.kills.lock().clone();
|
||||
tokio::time::timeout(Duration::from_secs(5), async {
|
||||
for k in kills {
|
||||
k.cancelled().await;
|
||||
}
|
||||
})
|
||||
.await
|
||||
.expect("every process was stopped");
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn sessions_without_a_title_get_one_generated() {
|
||||
let mut processes = load_fixture("simple");
|
||||
processes[0].extend([
|
||||
Frame::Shim(json!({"type": "control_request", "request_id": "req_3",
|
||||
"request": {"subtype": "generate_session_title"}})),
|
||||
Frame::Cli(json!({"type": "control_response",
|
||||
"response": {"subtype": "success", "request_id": "req_3", "response": {"title": "Say hello"}}})),
|
||||
]);
|
||||
let cli = replay_cli(processes);
|
||||
let (config, cwd) = (tempfile::tempdir().unwrap(), tempfile::tempdir().unwrap());
|
||||
let cwd_path = std::fs::canonicalize(cwd.path()).unwrap();
|
||||
let h = Harness::connect(
|
||||
cli.clone(),
|
||||
config.path().into(),
|
||||
config.path().join("pending"),
|
||||
cwd_path.clone(),
|
||||
Vec::new(),
|
||||
)
|
||||
.await;
|
||||
let session = h.new_session().await;
|
||||
// The transcript Claude Code writes, without the title it gives sessions in a terminal.
|
||||
let dir = config.path().join("projects").join(history::escape(&cwd_path));
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let prompt = json!({"type": "user", "uuid": "u1", "parentUuid": null,
|
||||
"message": {"role": "user", "content": "Reply with exactly: hello there"}});
|
||||
std::fs::write(dir.join(format!("{session}.jsonl")), format!("{prompt}\n")).unwrap();
|
||||
|
||||
h.prompt(&session, "Reply with exactly: hello there").await.unwrap();
|
||||
let mut titles = Vec::new();
|
||||
for _ in 0..100 {
|
||||
titles = h.updates(&session, "session_info_update");
|
||||
if !titles.is_empty() {
|
||||
break;
|
||||
}
|
||||
tokio::time::sleep(Duration::from_millis(20)).await;
|
||||
}
|
||||
assert!(cli.errors.lock().is_empty(), "{:?}", cli.errors.lock());
|
||||
assert_eq!(titles.iter().map(|t| t["title"].clone()).collect::<Vec<_>>(), [json!("Say hello")]);
|
||||
let sent = cli.sent.lock()[0].clone();
|
||||
let request =
|
||||
sent.iter().find(|m| m.pointer("/request/subtype") == Some(&json!("generate_session_title"))).unwrap();
|
||||
assert_eq!(request["request"]["description"], "Reply with exactly: hello there");
|
||||
assert_eq!(request["request"]["persist"], true);
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn replay_multi_turn() {
|
||||
let r = replay("multi_turn").await;
|
||||
@@ -727,6 +797,7 @@ async fn a_stop_ahead_of_its_prompt_cancels_it() {
|
||||
sent: Arc::default(),
|
||||
launched: Mutex::default(),
|
||||
errors: Arc::default(),
|
||||
kills: Mutex::default(),
|
||||
});
|
||||
let (dir, cwd) = (tempfile::tempdir().unwrap(), tempfile::tempdir().unwrap());
|
||||
let h = Harness::connect(cli.clone(), dir.path().into(), dir.path().join("p"), cwd.path().into(), Vec::new()).await;
|
||||
@@ -770,6 +841,7 @@ async fn a_failed_turn_reports_its_failure() {
|
||||
sent: Arc::default(),
|
||||
launched: Mutex::default(),
|
||||
errors: Arc::default(),
|
||||
kills: Mutex::default(),
|
||||
});
|
||||
let (dir, cwd) = (tempfile::tempdir().unwrap(), tempfile::tempdir().unwrap());
|
||||
let h =
|
||||
|
||||
@@ -28,6 +28,9 @@ const OUTPUT_EVERY: Duration = Duration::from_millis(200);
|
||||
const EARLY_CANCEL: Duration = Duration::from_secs(1);
|
||||
/// How long the client's connection outlives codex.
|
||||
const EXIT_GRACE: Duration = Duration::from_millis(100);
|
||||
/// What a thread's name is asked for with, after its first turn.
|
||||
const NAME_PROMPT: &str = "Name this conversation for a list of tasks: a short title of at most six words, in the \
|
||||
language the user writes in. Reply with the title only, without quotes or a closing period.";
|
||||
|
||||
pub(crate) fn connect(
|
||||
spec: &ProviderSpec,
|
||||
@@ -41,6 +44,92 @@ pub(crate) fn connect(
|
||||
})
|
||||
}
|
||||
|
||||
/// A model lookup uses app-server directly: no thread, task, prompt or project MCP.
|
||||
pub(crate) async fn discover(
|
||||
spec: ProviderSpec,
|
||||
cwd: std::path::PathBuf,
|
||||
) -> Result<crate::catalogs::Discovered, String> {
|
||||
let spec = tokio::task::spawn_blocking({
|
||||
let cwd = cwd.clone();
|
||||
move || spec.resolve_for_launch(&cwd)
|
||||
})
|
||||
.await
|
||||
.map_err(|e| e.to_string())?;
|
||||
let peer = Peer::spawn_framed(
|
||||
&spec.command,
|
||||
&spec.args,
|
||||
&cwd,
|
||||
&spec.env,
|
||||
Arc::new(ide_jsonrpc::NoopHandler),
|
||||
Box::new(|_| {}),
|
||||
Framing::Lines,
|
||||
)
|
||||
.map_err(|e| e.to_string())?;
|
||||
let _stop = crate::catalogs::KillPeer(peer.clone());
|
||||
tokio::time::timeout(Duration::from_secs(30), async {
|
||||
let _: Value = peer.request("initialize", initialize_params(), None).await.map_err(|e| e.to_string())?;
|
||||
peer.notify("initialized", Value::Null).map_err(|e| e.to_string())?;
|
||||
let mut models = list_models(&peer).await.map_err(|e| e.message)?;
|
||||
models.retain(|model| !model.hidden);
|
||||
let mut settings = Settings::of(&json!({}), &models);
|
||||
if settings.model.is_empty()
|
||||
&& let Some(model) = models.first()
|
||||
{
|
||||
settings.model = model.id.clone();
|
||||
settings.fit_effort(&models);
|
||||
}
|
||||
let options = translate::config_options(&models, &settings);
|
||||
let config = crate::config::options_of(&json!({ "configOptions": options }));
|
||||
let levels = models
|
||||
.iter()
|
||||
.map(|model| {
|
||||
let mut settings = settings.clone();
|
||||
settings.model = model.id.clone();
|
||||
settings.fit_effort(&models);
|
||||
let options = translate::config_options(&models, &settings);
|
||||
let levels = crate::config::options_of(&json!({ "configOptions": options }))
|
||||
.into_iter()
|
||||
.filter(|o| o.category.as_deref() == Some("thought_level"))
|
||||
.collect();
|
||||
(model.id.clone(), levels)
|
||||
})
|
||||
.collect();
|
||||
Ok(crate::catalogs::Discovered { config, commands: None, levels })
|
||||
})
|
||||
.await
|
||||
.map_err(|_| "Codex model discovery timed out".to_owned())?
|
||||
}
|
||||
|
||||
fn initialize_params() -> Value {
|
||||
json!({
|
||||
"clientInfo": { "name": "openidea", "title": "OpenIDEA", "version": env!("CARGO_PKG_VERSION") },
|
||||
"capabilities": { "experimentalApi": true, "optOutNotificationMethods": OPT_OUT },
|
||||
})
|
||||
}
|
||||
|
||||
async fn list_models(peer: &Peer) -> Result<Vec<ModelInfo>, ResponseError> {
|
||||
let mut all = Vec::new();
|
||||
let mut cursor = Value::Null;
|
||||
let mut seen = HashSet::new();
|
||||
loop {
|
||||
let page: Value = peer.request("model/list", json!({ "cursor": cursor }), None).await.map_err(from_codex)?;
|
||||
let Some(data) = page.get("data").and_then(Value::as_array) else {
|
||||
return Err(error(codes::INTERNAL_ERROR, "Codex returned an invalid model list"));
|
||||
};
|
||||
let models = translate::models_of(&page);
|
||||
if models.len() != data.len() {
|
||||
return Err(error(codes::INTERNAL_ERROR, "Codex returned an invalid model list"));
|
||||
}
|
||||
all.extend(models);
|
||||
match page.get("nextCursor") {
|
||||
None | Some(Value::Null) => return Ok(all),
|
||||
Some(Value::String(next)) if next.is_empty() => return Ok(all),
|
||||
Some(Value::String(next)) if seen.insert(next.clone()) => cursor = json!(next),
|
||||
_ => return Err(error(codes::INTERNAL_ERROR, "Codex returned an invalid model-list cursor")),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Connects `client` to a shim driving the codex connection `connect_codex` opens (it
|
||||
/// gets the handler for what codex sends). Each side's end closes the other.
|
||||
fn attach(
|
||||
@@ -126,6 +215,10 @@ struct Thread {
|
||||
early_cancel: Option<Instant>,
|
||||
/// The latest agent message (a subagent's answer).
|
||||
answer: String,
|
||||
/// The thread has a name.
|
||||
named: bool,
|
||||
/// A name is being asked for.
|
||||
naming: bool,
|
||||
}
|
||||
|
||||
struct PendingPrompt {
|
||||
@@ -243,15 +336,7 @@ impl CodexShim {
|
||||
}
|
||||
|
||||
async fn initialize(&self) -> Result<Value, ResponseError> {
|
||||
let info = self
|
||||
.call(
|
||||
"initialize",
|
||||
json!({
|
||||
"clientInfo": { "name": "openidea", "title": "OpenIDEA", "version": env!("CARGO_PKG_VERSION") },
|
||||
"capabilities": { "experimentalApi": true, "optOutNotificationMethods": OPT_OUT },
|
||||
}),
|
||||
)
|
||||
.await?;
|
||||
let info = self.call("initialize", initialize_params()).await?;
|
||||
self.codex().notify("initialized", Value::Null).map_err(from_codex)?;
|
||||
self.state.lock().initialized = true;
|
||||
// `<client>/<codex version> (<platform>) …`
|
||||
@@ -275,32 +360,15 @@ impl CodexShim {
|
||||
}))
|
||||
}
|
||||
|
||||
/// The models codex offers; empty when it can't list them.
|
||||
async fn models(&self) -> Arc<Vec<ModelInfo>> {
|
||||
/// Cache only complete, successful listings; a failed page must never become availability.
|
||||
async fn models(&self) -> Result<Arc<Vec<ModelInfo>>, ResponseError> {
|
||||
let mut cached = self.models.lock().await;
|
||||
if let Some(m) = cached.as_ref() {
|
||||
return m.clone();
|
||||
if let Some(models) = cached.as_ref() {
|
||||
return Ok(models.clone());
|
||||
}
|
||||
let mut all = Vec::new();
|
||||
let mut cursor = Value::Null;
|
||||
loop {
|
||||
match self.call("model/list", json!({ "cursor": cursor })).await {
|
||||
Ok(page) => {
|
||||
all.extend(translate::models_of(&page));
|
||||
match page.get("nextCursor").filter(|c| c.is_string()) {
|
||||
Some(next) if *next != cursor => cursor = next.clone(),
|
||||
_ => break,
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
tracing::warn!("codex: listing models failed: {}", e.message);
|
||||
return Arc::new(all);
|
||||
}
|
||||
}
|
||||
}
|
||||
let models = Arc::new(all);
|
||||
let models = Arc::new(list_models(self.codex()).await?);
|
||||
*cached = Some(models.clone());
|
||||
models
|
||||
Ok(models)
|
||||
}
|
||||
|
||||
async fn publish_skills(&self, session: &str) {
|
||||
@@ -329,11 +397,12 @@ impl CodexShim {
|
||||
t.parent = None;
|
||||
t.settings = Some(settings);
|
||||
t.cwd = cwd;
|
||||
t.named = thread.get("name").and_then(Value::as_str).is_some_and(|n| !n.trim().is_empty());
|
||||
(id, options)
|
||||
}
|
||||
|
||||
async fn new_session(&self, params: &Value) -> Result<Value, ResponseError> {
|
||||
let models = self.models().await;
|
||||
let models = self.models().await?;
|
||||
let mut request = json!({ "cwd": params.get("cwd") });
|
||||
if let Some(config) = translate::mcp_config(params) {
|
||||
request["config"] = config;
|
||||
@@ -350,7 +419,7 @@ impl CodexShim {
|
||||
/// Resumes the thread and replays its history before answering.
|
||||
async fn load_session(&self, params: &Value) -> Result<Value, ResponseError> {
|
||||
let id = str_param(params, "sessionId")?;
|
||||
let models = self.models().await;
|
||||
let models = self.models().await?;
|
||||
let mut request = json!({ "threadId": id, "cwd": params.get("cwd") });
|
||||
if let Some(config) = translate::mcp_config(params) {
|
||||
request["config"] = config;
|
||||
@@ -368,7 +437,7 @@ impl CodexShim {
|
||||
|
||||
async fn fork_session(&self, params: &Value) -> Result<Value, ResponseError> {
|
||||
let source = str_param(params, "sessionId")?;
|
||||
let models = self.models().await;
|
||||
let models = self.models().await?;
|
||||
let settings = self.state.lock().threads.get(source).and_then(|t| t.settings.clone());
|
||||
let mut request = json!({ "threadId": source, "cwd": params.get("cwd") });
|
||||
if let Some(config) = translate::mcp_config(params) {
|
||||
@@ -389,7 +458,7 @@ impl CodexShim {
|
||||
let session = str_param(params, "sessionId")?;
|
||||
let option = str_param(params, "configId")?;
|
||||
let value = str_param(params, "value")?;
|
||||
let models = self.models().await;
|
||||
let models = self.models().await?;
|
||||
let mut state = self.state.lock();
|
||||
let settings = state
|
||||
.threads
|
||||
@@ -406,7 +475,7 @@ impl CodexShim {
|
||||
if input.is_empty() {
|
||||
return Err(invalid("empty prompt"));
|
||||
}
|
||||
let models = self.models().await;
|
||||
let models = self.models().await?;
|
||||
let (done, ended) = oneshot::channel();
|
||||
let settings = {
|
||||
let mut state = self.state.lock();
|
||||
@@ -625,8 +694,9 @@ impl CodexShim {
|
||||
Some(translate::plan(params))
|
||||
}),
|
||||
"thread/tokenUsage/updated" => self.with_thread(thread, |_, _| translate::usage(params)),
|
||||
"thread/name/updated" => self.with_thread(thread, |_, _| {
|
||||
"thread/name/updated" => self.with_thread(thread, |t, _| {
|
||||
let title = params.get("threadName").and_then(Value::as_str)?;
|
||||
t.named = true;
|
||||
Some(json!({ "sessionUpdate": "session_info_update", "title": title }))
|
||||
}),
|
||||
"thread/started" => self.thread_started(params.get("thread").unwrap_or(&Value::Null)),
|
||||
@@ -812,9 +882,10 @@ impl CodexShim {
|
||||
}
|
||||
}
|
||||
|
||||
fn turn_completed(&self, thread: &str, turn: &Value) {
|
||||
fn turn_completed(self: &Arc<Self>, thread: &str, turn: &Value) {
|
||||
let id = turn.get("id").and_then(Value::as_str).unwrap_or_default();
|
||||
let (parent, answer) = {
|
||||
let completed = turn.get("status").and_then(Value::as_str) == Some("completed");
|
||||
let (parent, answer, name) = {
|
||||
let mut state = self.state.lock();
|
||||
let Some(t) = state.threads.get_mut(thread) else { return };
|
||||
if t.turn.as_deref().is_none_or(|r| r == id) {
|
||||
@@ -830,8 +901,15 @@ impl CodexShim {
|
||||
let _ = p.done.send((turn.clone(), error.clone()));
|
||||
}
|
||||
let answer = std::mem::take(&mut t.answer);
|
||||
(t.parent.clone(), (answer, error))
|
||||
let name =
|
||||
completed && !id.is_empty() && t.parent.is_none() && t.settings.is_some() && !t.named && !t.naming;
|
||||
t.naming |= name;
|
||||
(t.parent.clone(), (answer, error), name)
|
||||
};
|
||||
if name {
|
||||
let (shim, thread, turn) = (self.clone(), thread.to_owned(), id.to_owned());
|
||||
self.runtime.spawn(async move { shim.name_thread(&thread, &turn).await });
|
||||
}
|
||||
let Some(parent) = parent else { return };
|
||||
let (answer, error) = answer;
|
||||
let mut state_of = match turn.get("status").and_then(Value::as_str) {
|
||||
@@ -853,6 +931,28 @@ impl CodexShim {
|
||||
}
|
||||
}
|
||||
|
||||
/// Names a thread from what it holds after `turn`, as codex leaves naming to its
|
||||
/// clients; `thread/name/updated` brings the name back. Unnamed, the next turn tries again.
|
||||
async fn name_thread(&self, thread: &str, turn: &str) {
|
||||
let sample = json!({ "threadId": thread, "expectedTurnId": turn, "prompt": NAME_PROMPT, "effort": "low" });
|
||||
let named = match self.call("thread/sample", sample).await {
|
||||
Ok(r) => match r.get("text").and_then(Value::as_str).and_then(translate::thread_name) {
|
||||
Some(name) => {
|
||||
self.call("thread/name/set", json!({ "threadId": thread, "name": name })).await.map(|_| ())
|
||||
}
|
||||
None => Err(error(codes::INTERNAL_ERROR, "codex gave no name")),
|
||||
},
|
||||
Err(e) => Err(e),
|
||||
};
|
||||
if let Err(e) = &named {
|
||||
tracing::debug!("naming codex thread {thread}: {}", e.message);
|
||||
}
|
||||
if let Some(t) = self.state.lock().threads.get_mut(thread) {
|
||||
t.naming = false;
|
||||
t.named |= named.is_ok();
|
||||
}
|
||||
}
|
||||
|
||||
/// Requests from codex: approvals and questions, asked as ACP permission requests.
|
||||
fn on_codex_request(&self, method: &str, params: &Value) -> Result<Value, (i64, String)> {
|
||||
let thread = params.get("threadId").and_then(Value::as_str).unwrap_or_default().to_owned();
|
||||
@@ -869,13 +969,14 @@ impl CodexShim {
|
||||
"item/commandExecution/requestApproval" => {
|
||||
let choices = translate::decisions(params, true);
|
||||
let title = translate::command_approval_title(params, item.as_ref());
|
||||
let call = tool_call_of(
|
||||
&item_id,
|
||||
&title,
|
||||
"execute",
|
||||
reason,
|
||||
Some(json!({ "command": params.get("command"), "cwd": params.get("cwd") })),
|
||||
);
|
||||
let mut input = json!({
|
||||
"command": translate::approval_command(params, item.as_ref()),
|
||||
"cwd": params.get("cwd"),
|
||||
});
|
||||
if let Some(network) = params.get("networkApprovalContext").filter(|c| !c.is_null()) {
|
||||
input["networkApprovalContext"] = network.clone();
|
||||
}
|
||||
let call = tool_call_of(&item_id, &title, "execute", reason, Some(input));
|
||||
Ok(self.ask(&thread, call, &choices, decline))
|
||||
}
|
||||
"item/fileChange/requestApproval" => {
|
||||
|
||||
@@ -11,6 +11,155 @@ use std::collections::VecDeque;
|
||||
use std::path::PathBuf;
|
||||
use tokio::io::{AsyncBufReadExt, AsyncRead, AsyncWrite, AsyncWriteExt, BufReader};
|
||||
|
||||
struct ModelPages {
|
||||
pages: Mutex<VecDeque<Result<Value, (i64, String)>>>,
|
||||
cursors: Mutex<Vec<Value>>,
|
||||
}
|
||||
|
||||
impl Handler for ModelPages {
|
||||
fn on_notification(&self, _: &str, _: Value) {}
|
||||
|
||||
fn on_request(&self, method: &str, params: Value) -> Result<Value, (i64, String)> {
|
||||
assert_eq!(method, "model/list", "listing models must never start a thread or turn");
|
||||
self.cursors.lock().push(params["cursor"].clone());
|
||||
self.pages.lock().pop_front().expect("another expected page")
|
||||
}
|
||||
}
|
||||
|
||||
fn model_peer(pages: Arc<ModelPages>) -> (Peer, Peer) {
|
||||
let (ours, theirs) = tokio::io::duplex(1 << 16);
|
||||
let (read, write) = tokio::io::split(theirs);
|
||||
let server = Peer::connect_framed(read, write, pages, Framing::Lines);
|
||||
let (read, write) = tokio::io::split(ours);
|
||||
(Peer::connect_framed(read, write, Arc::new(ide_jsonrpc::NoopHandler), Framing::Lines), server)
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn model_pagination_failure_does_not_cache_an_incomplete_catalog() {
|
||||
let first = json!({ "data": [{ "id": "first" }], "nextCursor": "next" });
|
||||
let pages = Arc::new(ModelPages {
|
||||
pages: Mutex::new(VecDeque::from([
|
||||
Ok(first.clone()),
|
||||
Err((-32000, "authentication expired".into())),
|
||||
Ok(first),
|
||||
Ok(json!({ "data": [{ "id": "second" }], "nextCursor": null })),
|
||||
])),
|
||||
cursors: Mutex::default(),
|
||||
});
|
||||
let (peer, server) = model_peer(pages.clone());
|
||||
let shim = CodexShim {
|
||||
runtime: tokio::runtime::Handle::current(),
|
||||
codex: OnceLock::from(peer.clone()),
|
||||
client: OnceLock::new(),
|
||||
state: Mutex::default(),
|
||||
models: tokio::sync::Mutex::new(None),
|
||||
skills: tokio::sync::Mutex::new(()),
|
||||
};
|
||||
assert_eq!(shim.models().await.unwrap_err().message, "authentication expired");
|
||||
let models = shim.models().await.unwrap();
|
||||
assert_eq!(models.iter().map(|m| m.id.as_str()).collect::<Vec<_>>(), ["first", "second"]);
|
||||
assert_eq!(shim.models().await.unwrap().len(), 2);
|
||||
assert_eq!(*pages.cursors.lock(), [Value::Null, json!("next"), Value::Null, json!("next")]);
|
||||
peer.kill();
|
||||
server.kill();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn repeated_or_invalid_model_cursors_and_malformed_pages_are_failures() {
|
||||
for pages in [
|
||||
vec![json!({ "data": [], "nextCursor": "a" }), json!({ "data": [], "nextCursor": "a" })],
|
||||
vec![
|
||||
json!({ "data": [], "nextCursor": "a" }),
|
||||
json!({ "data": [], "nextCursor": "b" }),
|
||||
json!({ "data": [], "nextCursor": "a" }),
|
||||
],
|
||||
vec![json!({ "data": [], "nextCursor": 7 })],
|
||||
vec![json!({ "nextCursor": null })],
|
||||
vec![json!({ "data": [{}], "nextCursor": null })],
|
||||
] {
|
||||
let pages =
|
||||
Arc::new(ModelPages { pages: Mutex::new(pages.into_iter().map(Ok).collect()), cursors: Mutex::default() });
|
||||
let (peer, server) = model_peer(pages);
|
||||
assert!(list_models(&peer).await.is_err());
|
||||
peer.kill();
|
||||
server.kill();
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn native_discovery_only_initializes_and_pages_models_then_stops_the_process() {
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let script = tmp.path().join("fake-codex");
|
||||
std::fs::write(
|
||||
&script,
|
||||
r#"#!/usr/bin/env python3
|
||||
import json, os, pathlib, sys
|
||||
pathlib.Path('pid').write_text(str(os.getpid()))
|
||||
for line in sys.stdin:
|
||||
request = json.loads(line)
|
||||
with pathlib.Path('requests').open('a') as f:
|
||||
f.write(json.dumps(request) + '\n')
|
||||
method = request['method']
|
||||
if method == 'initialized':
|
||||
continue
|
||||
if method == 'initialize':
|
||||
result = {}
|
||||
elif method == 'model/list':
|
||||
cursor = request['params']['cursor']
|
||||
model = 'one' if cursor is None else 'two'
|
||||
result = {'data': [{'id': model, 'isDefault': cursor is None,
|
||||
'defaultReasoningEffort': 'low', 'supportedReasoningEfforts': [{'reasoningEffort': 'low'}]}],
|
||||
'nextCursor': 'second-page' if cursor is None else None}
|
||||
else:
|
||||
raise RuntimeError('must not create a thread or prompt: ' + method)
|
||||
print(json.dumps({'jsonrpc': '2.0', 'id': request['id'], 'result': result}), flush=True)
|
||||
"#,
|
||||
)
|
||||
.unwrap();
|
||||
std::fs::set_permissions(&script, std::fs::Permissions::from_mode(0o755)).unwrap();
|
||||
let spec = ProviderSpec {
|
||||
id: "codex".into(),
|
||||
name: "Codex".into(),
|
||||
driver: crate::provider::Driver::Codex,
|
||||
command: script.to_string_lossy().into_owned(),
|
||||
args: Vec::new(),
|
||||
env: Vec::new(),
|
||||
login_hint: String::new(),
|
||||
source: crate::AgentSource::Builtin,
|
||||
};
|
||||
let found = discover(spec, tmp.path().to_path_buf()).await.unwrap();
|
||||
let models: Vec<_> = found
|
||||
.config
|
||||
.iter()
|
||||
.filter(|o| o.category.as_deref() == Some("model"))
|
||||
.flat_map(|o| o.values.iter().map(|v| v.value.as_str()))
|
||||
.collect();
|
||||
assert_eq!(models, ["one", "two"]);
|
||||
assert_eq!(found.levels.keys().map(String::as_str).collect::<Vec<_>>(), ["one", "two"]);
|
||||
let requests = std::fs::read_to_string(tmp.path().join("requests")).unwrap();
|
||||
let requests: Vec<Value> = requests.lines().map(|line| serde_json::from_str(line).unwrap()).collect();
|
||||
assert_eq!(
|
||||
requests.iter().map(|r| r["method"].as_str().unwrap()).collect::<Vec<_>>(),
|
||||
["initialize", "initialized", "model/list", "model/list"]
|
||||
);
|
||||
assert_eq!(requests[2]["params"]["cursor"], Value::Null);
|
||||
assert_eq!(requests[3]["params"]["cursor"], "second-page");
|
||||
let pid = std::fs::read_to_string(tmp.path().join("pid")).unwrap();
|
||||
tokio::time::timeout(Duration::from_secs(5), async {
|
||||
loop {
|
||||
let alive = std::process::Command::new("kill").args(["-0", pid.trim()]).output().unwrap().status.success();
|
||||
if !alive {
|
||||
break;
|
||||
}
|
||||
tokio::time::sleep(Duration::from_millis(10)).await;
|
||||
}
|
||||
})
|
||||
.await
|
||||
.expect("discovery must kill and reap its child");
|
||||
}
|
||||
|
||||
// ---- Pure translations -------------------------------------------------------------
|
||||
|
||||
fn models() -> Vec<ModelInfo> {
|
||||
@@ -535,8 +684,10 @@ async fn play<R: AsyncRead + Unpin, W: AsyncWrite + Unpin>(
|
||||
.unwrap()
|
||||
.unwrap_or_else(|| panic!("the shim closed before {frame}"));
|
||||
let actual: Value = serde_json::from_str(&line).unwrap();
|
||||
// Older recordings predate skill discovery; play an unsupported server.
|
||||
if actual["method"] == "skills/list" && frame["method"] != "skills/list" {
|
||||
// Older recordings predate skill discovery and thread naming; play an
|
||||
// unsupported server.
|
||||
let method = actual["method"].as_str().unwrap_or_default();
|
||||
if matches!(method, "skills/list" | "thread/sample") && frame["method"] != method {
|
||||
let reply = json!({ "id": actual["id"], "error": { "code": -32601, "message": "unsupported" } });
|
||||
write.write_all(format!("{reply}\n").as_bytes()).await.unwrap();
|
||||
continue;
|
||||
@@ -574,7 +725,14 @@ async fn play<R: AsyncRead + Unpin, W: AsyncWrite + Unpin>(
|
||||
/// Runs `scenario` against its transcript; returns the ACP results, the client and
|
||||
/// the frames the shim sent.
|
||||
async fn replay(scenario: Scenario) -> (Results, Arc<Client>, Vec<Value>, Value) {
|
||||
let t = load(scenario.file());
|
||||
let (results, client, sent, meta, _codex) = replay_transcript(scenario, load(scenario.file())).await;
|
||||
(results, client, sent, meta)
|
||||
}
|
||||
|
||||
/// Both connections of a replay, which stay open while it is held.
|
||||
type CodexEnd = Box<dyn std::any::Any>;
|
||||
|
||||
async fn replay_transcript(scenario: Scenario, t: Transcript) -> (Results, Arc<Client>, Vec<Value>, Value, CodexEnd) {
|
||||
let (ours, theirs) = tokio::io::duplex(1 << 20);
|
||||
let (tr, tw) = tokio::io::split(theirs);
|
||||
let client = Arc::new(Client::default());
|
||||
@@ -590,8 +748,8 @@ async fn replay(scenario: Scenario) -> (Results, Arc<Client>, Vec<Value>, Value)
|
||||
.await
|
||||
.expect("the scenario finished");
|
||||
// Codex stays up until the scenario is over.
|
||||
let (sent, _read, _write) = player.await.unwrap();
|
||||
(results, client, sent, meta)
|
||||
let (sent, read, write) = player.await.unwrap();
|
||||
(results, client, sent, meta, Box::new((acp, read, write)))
|
||||
}
|
||||
|
||||
fn sent<'a>(frames: &'a [Value], method: &str) -> Vec<&'a Value> {
|
||||
@@ -801,6 +959,42 @@ async fn a_simple_turn_streams_reasoning_text_and_usage() {
|
||||
assert_eq!(info["title"], "Say hi");
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn an_unnamed_thread_is_named_after_its_first_turn() {
|
||||
let mut t = load(Scenario::Simple.file());
|
||||
t.frames.retain(|(_, f)| f["method"] != "thread/name/updated");
|
||||
let thread = "01a18300-0000-7000-8000-000000000001";
|
||||
t.frames.extend([
|
||||
(true, json!({"id": 90, "method": "thread/sample"})),
|
||||
(false, json!({"id": 90, "result": {"responseId": "r", "sourceTurnId": thread, "text": "\"Say hi.\"\n"}})),
|
||||
(true, json!({"id": 91, "method": "thread/name/set"})),
|
||||
(false, json!({"id": 91, "result": {}})),
|
||||
(false, json!({"method": "thread/name/updated", "params": {"threadId": thread, "threadName": "Say hi"}})),
|
||||
]);
|
||||
let (results, client, frames, _, _codex) = replay_transcript(Scenario::Simple, t).await;
|
||||
let session = session_of(&results, "new");
|
||||
let sample = &sent(&frames, "thread/sample")[0]["params"];
|
||||
assert_eq!((sample["threadId"].as_str(), sample["expectedTurnId"].as_str()), (Some(thread), Some(thread)));
|
||||
assert_eq!(sent(&frames, "thread/name/set")[0]["params"], json!({"threadId": thread, "name": "Say hi"}));
|
||||
let mut info = Vec::new();
|
||||
for _ in 0..100 {
|
||||
info = client.of(&session).into_iter().filter(|u| u["sessionUpdate"] == "session_info_update").collect();
|
||||
if !info.is_empty() {
|
||||
break;
|
||||
}
|
||||
tokio::time::sleep(Duration::from_millis(20)).await;
|
||||
}
|
||||
assert_eq!(info.iter().map(|u| u["title"].clone()).collect::<Vec<_>>(), [json!("Say hi")]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn thread_names_are_the_first_line_of_the_reply() {
|
||||
assert_eq!(thread_name(" \n“修复登录表单。”\nmore").as_deref(), Some("修复登录表单"));
|
||||
assert_eq!(thread_name("**Fix the login form.**").as_deref(), Some("Fix the login form"));
|
||||
assert_eq!(thread_name(" \n\"\" "), None);
|
||||
assert_eq!(thread_name(&"x".repeat(100)).map(|n| n.chars().count()), Some(80));
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn settings_apply_to_the_next_turn() {
|
||||
let (results, client, frames, _) = replay(Scenario::MultiTurn).await;
|
||||
|
||||
@@ -612,6 +612,16 @@ pub(super) fn usage(params: &Value) -> Option<Value> {
|
||||
Some(json!({ "sessionUpdate": "usage_update", "used": used, "size": size }))
|
||||
}
|
||||
|
||||
/// A thread name from the model's reply to the naming prompt: its first line, without
|
||||
/// quotes or a closing period. `None` when nothing is left.
|
||||
pub(super) fn thread_name(reply: &str) -> Option<String> {
|
||||
const MAX_NAME: usize = 80;
|
||||
let line = reply.lines().map(str::trim).find(|l| !l.is_empty())?;
|
||||
let line = line.trim_matches(|c: char| matches!(c, '"' | '\'' | '`' | '“' | '”' | '「' | '」' | '*'));
|
||||
let line = line.trim_end_matches(['.', '。']).trim();
|
||||
(!line.is_empty()).then(|| line.chars().take(MAX_NAME).collect())
|
||||
}
|
||||
|
||||
/// How a failed turn is reported.
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub(super) enum Failure {
|
||||
@@ -757,19 +767,23 @@ pub(super) fn answer_of(choices: &[Choice], outcome: &Value, decline: Value) ->
|
||||
choices.iter().find(|c| selected && c.option["optionId"].as_str() == chosen).map_or(decline, |c| c.answer.clone())
|
||||
}
|
||||
|
||||
/// The command a command approval is about: the request's own, else its item's.
|
||||
pub(super) fn approval_command<'a>(params: &'a Value, item: Option<&'a Value>) -> Option<&'a str> {
|
||||
// Approval payloads can describe a different command from the item's earlier summary.
|
||||
let item = item.filter(|i| i.get("id") == params.get("itemId"));
|
||||
params
|
||||
.get("command")
|
||||
.and_then(Value::as_str)
|
||||
.filter(|c| !c.is_empty())
|
||||
.or_else(|| item.and_then(|i| i.get("command")).and_then(Value::as_str).filter(|c| !c.is_empty()))
|
||||
}
|
||||
|
||||
/// The ACP title of a command approval.
|
||||
pub(super) fn command_approval_title(params: &Value, item: Option<&Value>) -> String {
|
||||
if let Some(host) = params.pointer("/networkApprovalContext/host").and_then(Value::as_str) {
|
||||
return format!("Allow network access to {host}");
|
||||
}
|
||||
// Approval payloads can describe a different command from the item's earlier summary.
|
||||
let item = item.filter(|i| i.get("id") == params.get("itemId"));
|
||||
let command = params
|
||||
.get("command")
|
||||
.and_then(Value::as_str)
|
||||
.filter(|c| !c.is_empty())
|
||||
.or_else(|| item.and_then(|i| i.get("command")).and_then(Value::as_str).filter(|c| !c.is_empty()));
|
||||
match command {
|
||||
match approval_command(params, item) {
|
||||
Some(command) => format!("Run {}", crate::shell_script(command).unwrap_or_else(|| command.to_owned())),
|
||||
None => "Run a command".to_owned(),
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,841 @@
|
||||
//! Model catalog, effort levels, context-window tiers, and token pricing from
|
||||
//! `copilot --headless --stdio` (`models.list` + `models.listTokenPriceUnits`).
|
||||
|
||||
use crate::model::{AgentModelPricing, AgentTokenPricing};
|
||||
use serde_json::{Value, json};
|
||||
use std::collections::{BTreeMap, HashMap};
|
||||
|
||||
pub(super) const MODEL: &str = "model";
|
||||
pub(super) const EFFORT: &str = "reasoning_effort";
|
||||
pub(super) const CONTEXT_TIER: &str = "context_tier";
|
||||
pub(super) const MODE: &str = "mode";
|
||||
pub(super) const ALLOW_ALL: &str = "allow_all";
|
||||
|
||||
const BATCH_TOKENS: f64 = 1_000_000.0;
|
||||
const NANO_AIU_PER_CREDIT: f64 = 1_000_000_000.0;
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub(super) enum Mode {
|
||||
Agent,
|
||||
Plan,
|
||||
Autopilot,
|
||||
}
|
||||
|
||||
impl Mode {
|
||||
pub const ALL: [Mode; 3] = [Mode::Agent, Mode::Plan, Mode::Autopilot];
|
||||
|
||||
pub fn id(self) -> &'static str {
|
||||
match self {
|
||||
Mode::Agent => "agent",
|
||||
Mode::Plan => "plan",
|
||||
Mode::Autopilot => "autopilot",
|
||||
}
|
||||
}
|
||||
|
||||
pub fn wire(self) -> &'static str {
|
||||
match self {
|
||||
Mode::Agent => "interactive",
|
||||
Mode::Plan => "plan",
|
||||
Mode::Autopilot => "autopilot",
|
||||
}
|
||||
}
|
||||
|
||||
pub fn name(self) -> &'static str {
|
||||
match self {
|
||||
Mode::Agent => "Agent",
|
||||
Mode::Plan => "Plan",
|
||||
Mode::Autopilot => "Autopilot",
|
||||
}
|
||||
}
|
||||
|
||||
pub fn description(self) -> &'static str {
|
||||
match self {
|
||||
Mode::Agent => "Default agent mode for conversational interactions",
|
||||
Mode::Plan => "Plan mode for creating and executing multi-step plans",
|
||||
Mode::Autopilot => "Autonomous mode that enables allow-all and runs until task completion",
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse(value: &str) -> Option<Mode> {
|
||||
let short = value.rsplit(['#', '/']).next().unwrap_or(value);
|
||||
match short {
|
||||
"agent" | "interactive" => Some(Mode::Agent),
|
||||
"plan" => Some(Mode::Plan),
|
||||
"autopilot" => Some(Mode::Autopilot),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub(super) struct ContextTierChoice {
|
||||
pub id: &'static str,
|
||||
pub label: String,
|
||||
pub tokens: u32,
|
||||
pub description: &'static str,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub(super) struct ModelEntry {
|
||||
pub id: String,
|
||||
pub name: String,
|
||||
pub description: Option<String>,
|
||||
pub is_default: bool,
|
||||
pub default_effort: Option<String>,
|
||||
pub efforts: Vec<String>,
|
||||
pub context_tiers: Vec<ContextTierChoice>,
|
||||
pub default_context_tokens: Option<u32>,
|
||||
pub max_context_tokens: Option<u32>,
|
||||
pub pricing: Option<AgentModelPricing>,
|
||||
}
|
||||
|
||||
impl ModelEntry {
|
||||
/// Copilot's default reasoning effort for this model: user setting (`effortLevel`) when
|
||||
/// supported, otherwise the model's `defaultReasoningEffort`, `"medium"`, or first listed.
|
||||
pub fn default_effort<'a>(&'a self, user_default: Option<&str>) -> Option<&'a str> {
|
||||
if let Some(wanted) = user_default
|
||||
&& let Some(found) = self.efforts.iter().find(|e| e.as_str() == wanted)
|
||||
{
|
||||
return Some(found.as_str());
|
||||
}
|
||||
if let Some(model_def) = self.default_effort.as_deref()
|
||||
&& let Some(found) = self.efforts.iter().find(|e| e.as_str() == model_def)
|
||||
{
|
||||
return Some(found.as_str());
|
||||
}
|
||||
self.efforts.iter().find(|e| e.as_str() == "medium").or_else(|| self.efforts.first()).map(String::as_str)
|
||||
}
|
||||
|
||||
/// Copilot's default context-window tier for this model: user setting (`contextTier`) when
|
||||
/// supported, otherwise `"default"` (`200K`), or first listed.
|
||||
pub fn default_context_tier(&self, user_default: Option<&str>) -> Option<&'static str> {
|
||||
if let Some(wanted) = user_default
|
||||
&& let Some(found) = self.context_tiers.iter().find(|t| t.id == wanted)
|
||||
{
|
||||
return Some(found.id);
|
||||
}
|
||||
self.context_tiers.iter().find(|t| t.id == "default").or_else(|| self.context_tiers.first()).map(|t| t.id)
|
||||
}
|
||||
|
||||
/// Active context window capacity in tokens for `tier`.
|
||||
pub fn context_tokens(&self, tier: Option<&str>) -> Option<u32> {
|
||||
if let Some(tier) = tier
|
||||
&& let Some(found) = self.context_tiers.iter().find(|t| t.id == tier)
|
||||
{
|
||||
return Some(found.tokens);
|
||||
}
|
||||
self.context_tiers
|
||||
.iter()
|
||||
.find(|t| t.id == "default")
|
||||
.or_else(|| self.context_tiers.first())
|
||||
.map(|t| t.tokens)
|
||||
.or(self.default_context_tokens)
|
||||
.or(self.max_context_tokens)
|
||||
}
|
||||
}
|
||||
|
||||
/// Copilot's own configured defaults from `user.settings.get` (`~/.copilot/config.json`)
|
||||
/// and optional CLI launch flags (`--model`, `--effort`, `--context`, `--allow-all`).
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq)]
|
||||
pub(super) struct UserDefaults {
|
||||
pub model: Option<String>,
|
||||
pub effort: Option<String>,
|
||||
pub context_tier: Option<String>,
|
||||
pub mode: Option<Mode>,
|
||||
pub allow_all: Option<bool>,
|
||||
}
|
||||
|
||||
impl UserDefaults {
|
||||
pub fn from_rpc_and_args(settings_rpc: Option<&Value>, args: &[String]) -> Self {
|
||||
let mut out = Self::default();
|
||||
if let Some(res) = settings_rpc {
|
||||
out.model = setting_str(res, "model");
|
||||
out.effort = setting_str(res, "effortLevel").or_else(|| setting_str(res, "reasoningEffort"));
|
||||
out.context_tier = setting_str(res, "contextTier");
|
||||
out.mode = setting_str(res, "defaultMode").and_then(|m| Mode::parse(&m));
|
||||
out.allow_all = setting_str(res, "defaultPermissionMode").and_then(|p| match p.as_str() {
|
||||
"allow-all" | "allow_all" | "bypass" | "autopilot" => Some(true),
|
||||
"manual" => Some(false),
|
||||
_ => None,
|
||||
});
|
||||
}
|
||||
let mut iter = args.iter().peekable();
|
||||
while let Some(arg) = iter.next() {
|
||||
let s = arg.as_str();
|
||||
if s == "--allow-all" || s == "--yolo" {
|
||||
out.allow_all = Some(true);
|
||||
} else if let Some(v) = flag_val(s, "--model", &mut iter) {
|
||||
out.model = Some(v);
|
||||
} else if let Some(v) =
|
||||
flag_val(s, "--effort", &mut iter).or_else(|| flag_val(s, "--reasoning-effort", &mut iter))
|
||||
{
|
||||
out.effort = Some(v);
|
||||
} else if let Some(v) = flag_val(s, "--context", &mut iter) {
|
||||
out.context_tier = Some(v);
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
}
|
||||
|
||||
fn setting_str(res: &Value, key: &str) -> Option<String> {
|
||||
let settings = res.get("settings");
|
||||
let raw = settings
|
||||
.and_then(|s| s.get(key))
|
||||
.and_then(|entry| entry.get("value").or(Some(entry)))
|
||||
.or_else(|| res.get(key))?;
|
||||
raw.as_str().map(str::trim).filter(|s| !s.is_empty()).map(str::to_owned)
|
||||
}
|
||||
|
||||
fn flag_val<'a>(
|
||||
arg: &'a str,
|
||||
flag: &str,
|
||||
iter: &mut std::iter::Peekable<std::slice::Iter<'a, String>>,
|
||||
) -> Option<String> {
|
||||
if arg == flag {
|
||||
return iter.next().map(|v| v.trim()).filter(|v| !v.is_empty()).map(str::to_owned);
|
||||
}
|
||||
let prefix = format!("{flag}=");
|
||||
arg.strip_prefix(&prefix).map(str::trim).filter(|v| !v.is_empty()).map(str::to_owned)
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub(super) struct Settings {
|
||||
pub model: String,
|
||||
pub effort: Option<String>,
|
||||
pub context_tier: Option<String>,
|
||||
pub mode: Mode,
|
||||
pub allow_all: bool,
|
||||
defaults: UserDefaults,
|
||||
/// Explicit per-session user overrides; `None` means track Copilot's default for each model.
|
||||
explicit_effort: Option<String>,
|
||||
explicit_context_tier: Option<String>,
|
||||
}
|
||||
|
||||
impl Settings {
|
||||
pub fn new(models: &[ModelEntry], defaults: &UserDefaults) -> Self {
|
||||
let model = defaults
|
||||
.model
|
||||
.as_deref()
|
||||
.filter(|id| models.iter().any(|m| m.id == *id))
|
||||
.or_else(|| models.iter().find(|m| m.is_default && m.id != "auto").map(|m| m.id.as_str()))
|
||||
.or_else(|| models.iter().find(|m| m.id != "auto").map(|m| m.id.as_str()))
|
||||
.or_else(|| models.first().map(|m| m.id.as_str()))
|
||||
.unwrap_or("auto")
|
||||
.to_owned();
|
||||
let mode = defaults.mode.unwrap_or(Mode::Agent);
|
||||
let allow_all = defaults.allow_all.unwrap_or(false) || mode == Mode::Autopilot;
|
||||
let mut s = Self {
|
||||
model,
|
||||
effort: None,
|
||||
context_tier: None,
|
||||
mode,
|
||||
allow_all,
|
||||
defaults: defaults.clone(),
|
||||
explicit_effort: None,
|
||||
explicit_context_tier: None,
|
||||
};
|
||||
s.fit_to_model(models);
|
||||
s
|
||||
}
|
||||
|
||||
pub fn fit_to_model(&mut self, models: &[ModelEntry]) {
|
||||
let Some(m) = models.iter().find(|m| m.id == self.model) else {
|
||||
self.effort = None;
|
||||
self.context_tier = None;
|
||||
return;
|
||||
};
|
||||
self.effort = if m.efforts.is_empty() {
|
||||
None
|
||||
} else if let Some(wanted) = self.explicit_effort.as_deref().filter(|w| m.efforts.iter().any(|e| e == *w)) {
|
||||
Some(wanted.to_owned())
|
||||
} else {
|
||||
m.default_effort(self.defaults.effort.as_deref()).map(str::to_owned)
|
||||
};
|
||||
|
||||
self.context_tier = if m.context_tiers.is_empty() {
|
||||
None
|
||||
} else if let Some(wanted) =
|
||||
self.explicit_context_tier.as_deref().filter(|w| m.context_tiers.iter().any(|t| t.id == *w))
|
||||
{
|
||||
Some(wanted.to_owned())
|
||||
} else {
|
||||
m.default_context_tier(self.defaults.context_tier.as_deref()).map(str::to_owned)
|
||||
};
|
||||
}
|
||||
|
||||
pub fn set(&mut self, models: &[ModelEntry], option: &str, value: &str) -> Result<(), String> {
|
||||
match option {
|
||||
MODEL => {
|
||||
if !models.is_empty() && !models.iter().any(|m| m.id == value) && value != self.model {
|
||||
return Err(format!("unknown model {value}"));
|
||||
}
|
||||
self.model = value.to_owned();
|
||||
self.fit_to_model(models);
|
||||
}
|
||||
EFFORT => {
|
||||
let m = models.iter().find(|m| m.id == self.model);
|
||||
if !m.is_some_and(|m| m.efforts.iter().any(|e| e == value)) {
|
||||
return Err(format!("{} has no reasoning effort {value}", self.model));
|
||||
}
|
||||
self.effort = Some(value.to_owned());
|
||||
self.explicit_effort = Some(value.to_owned());
|
||||
}
|
||||
CONTEXT_TIER => {
|
||||
let m = models.iter().find(|m| m.id == self.model);
|
||||
if !m.is_some_and(|m| m.context_tiers.iter().any(|t| t.id == value)) {
|
||||
return Err(format!("{} has no context tier {value}", self.model));
|
||||
}
|
||||
self.context_tier = Some(value.to_owned());
|
||||
self.explicit_context_tier = Some(value.to_owned());
|
||||
}
|
||||
MODE => {
|
||||
let mode = Mode::parse(value).ok_or_else(|| format!("unknown mode {value}"))?;
|
||||
self.mode = mode;
|
||||
if mode == Mode::Autopilot {
|
||||
self.allow_all = true;
|
||||
}
|
||||
}
|
||||
ALLOW_ALL => match value {
|
||||
"on" | "true" => self.allow_all = true,
|
||||
"off" | "false" => self.allow_all = false,
|
||||
_ => return Err(format!("unknown allow_all value {value}")),
|
||||
},
|
||||
_ => return Err(format!("unknown setting {option}")),
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn active_context_tokens(&self, models: &[ModelEntry]) -> Option<u32> {
|
||||
models.iter().find(|m| m.id == self.model).and_then(|m| m.context_tokens(self.context_tier.as_deref()))
|
||||
}
|
||||
}
|
||||
|
||||
/// Parses `models.list` + optional `models.listTokenPriceUnits` into catalog entries,
|
||||
/// prepending `"auto"` when not already in the list.
|
||||
pub(super) fn parse_catalog(list: &Value, price_units: Option<&Value>) -> Vec<ModelEntry> {
|
||||
let units = parse_price_units(price_units);
|
||||
let raw = list
|
||||
.get("models")
|
||||
.or_else(|| list.get("data"))
|
||||
.and_then(Value::as_array)
|
||||
.map(Vec::as_slice)
|
||||
.unwrap_or_default();
|
||||
let mut out = Vec::new();
|
||||
if !raw.iter().any(|m| str_of(m, "id") == Some("auto")) && !raw.is_empty() {
|
||||
out.push(ModelEntry {
|
||||
id: "auto".into(),
|
||||
name: "Auto".into(),
|
||||
description: Some("Let Copilot pick the best model".into()),
|
||||
is_default: false,
|
||||
default_effort: None,
|
||||
efforts: Vec::new(),
|
||||
context_tiers: Vec::new(),
|
||||
default_context_tokens: None,
|
||||
max_context_tokens: None,
|
||||
pricing: None,
|
||||
});
|
||||
}
|
||||
for m in raw {
|
||||
if let Some(entry) = parse_model(m, &units) {
|
||||
out.push(entry);
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
fn parse_model(m: &Value, units: &HashMap<String, PriceUnit>) -> Option<ModelEntry> {
|
||||
let id = str_of(m, "id")?.to_owned();
|
||||
if m.get("model_picker_enabled").and_then(Value::as_bool) == Some(false)
|
||||
|| m.get("modelPickerEnabled").and_then(Value::as_bool) == Some(false)
|
||||
{
|
||||
return None;
|
||||
}
|
||||
let name = str_of(m, "name")
|
||||
.or_else(|| str_of(m, "label"))
|
||||
.or_else(|| str_of(m, "displayName"))
|
||||
.filter(|s| !s.is_empty())
|
||||
.unwrap_or(&id)
|
||||
.to_owned();
|
||||
|
||||
let mut efforts = parse_efforts(m);
|
||||
efforts.sort_by_key(|e| effort_rank(e));
|
||||
efforts.dedup();
|
||||
|
||||
let limits = m.pointer("/capabilities/limits");
|
||||
let max_prompt =
|
||||
limits.and_then(|l| u32_of(l, "max_prompt_tokens").or_else(|| u32_of(l, "maxPromptTokens"))).filter(|&n| n > 0);
|
||||
let max_window = limits
|
||||
.and_then(|l| u32_of(l, "max_context_window_tokens").or_else(|| u32_of(l, "maxContextWindowTokens")))
|
||||
.filter(|&n| n > 0);
|
||||
|
||||
let billing = m.get("billing");
|
||||
let multiplier = billing.and_then(|b| f64_of(b, "multiplier"));
|
||||
let raw_category = str_of(m, "modelPickerPriceCategory")
|
||||
.or_else(|| str_of(m, "model_picker_price_category"))
|
||||
.or_else(|| str_of(m, "priceCategory"))
|
||||
.or_else(|| str_of(m, "price_category"))
|
||||
.or_else(|| billing.and_then(|b| str_of(b, "priceCategory").or_else(|| str_of(b, "price_category"))));
|
||||
let mut cost_tier = raw_category.and_then(normalize_cost_tier).map(str::to_owned);
|
||||
|
||||
let token_prices = billing.and_then(|b| b.get("token_prices").or_else(|| b.get("tokenPrices")));
|
||||
let unit = units.get(&id).copied().unwrap_or(PriceUnit::Credits);
|
||||
let (mut tiers, default_max, long_max) = parse_token_prices(token_prices, max_prompt, max_window, unit);
|
||||
|
||||
let mut context_tiers = Vec::new();
|
||||
if let (Some(def_tokens), Some(long_tokens)) = (default_max, long_max)
|
||||
&& def_tokens < long_tokens
|
||||
{
|
||||
context_tiers.push(ContextTierChoice {
|
||||
id: "default",
|
||||
label: format_tokens(def_tokens),
|
||||
tokens: def_tokens,
|
||||
description: "Default recommended context size",
|
||||
});
|
||||
context_tiers.push(ContextTierChoice {
|
||||
id: "long_context",
|
||||
label: format_tokens(long_tokens),
|
||||
tokens: long_tokens,
|
||||
description: "Maximum context window for longer sessions",
|
||||
});
|
||||
}
|
||||
|
||||
if cost_tier.is_none()
|
||||
&& let Some(def) = tiers.get("default")
|
||||
{
|
||||
cost_tier = Some(infer_cost_tier(def.output).to_owned());
|
||||
}
|
||||
if let Some(def_tokens) = default_max.or(max_window).or(max_prompt)
|
||||
&& let Some(def) = tiers.get_mut("default")
|
||||
&& def.context_tokens.is_none()
|
||||
{
|
||||
def.context_tokens = Some(def_tokens);
|
||||
}
|
||||
if let Some(long_tokens) = long_max
|
||||
&& let Some(long) = tiers.get_mut("long_context")
|
||||
{
|
||||
long.context_tokens = Some(long_tokens);
|
||||
}
|
||||
|
||||
let pricing = (!tiers.is_empty() || cost_tier.is_some() || multiplier.is_some()).then_some(AgentModelPricing {
|
||||
cost_tier,
|
||||
multiplier,
|
||||
tiers,
|
||||
});
|
||||
|
||||
let description = multiplier
|
||||
.map(|mul| {
|
||||
let clean = round6(mul);
|
||||
if (clean - clean.round()).abs() < 1e-6 {
|
||||
format!("{}x", clean.round() as i64)
|
||||
} else {
|
||||
format!("{clean}x")
|
||||
}
|
||||
})
|
||||
.or_else(|| str_of(m, "description").filter(|d| !d.is_empty() && *d != name).map(str::to_owned))
|
||||
.or_else(|| (id == "auto").then(|| "Let Copilot pick the best model".to_owned()));
|
||||
|
||||
let is_default = m
|
||||
.get("is_chat_default")
|
||||
.or_else(|| m.get("isChatDefault"))
|
||||
.or_else(|| m.get("isDefault"))
|
||||
.and_then(Value::as_bool)
|
||||
.unwrap_or(false);
|
||||
let default_effort = str_of(m, "defaultReasoningEffort")
|
||||
.or_else(|| str_of(m, "default_reasoning_effort"))
|
||||
.map(str::trim)
|
||||
.filter(|s| !s.is_empty())
|
||||
.map(str::to_owned);
|
||||
|
||||
Some(ModelEntry {
|
||||
id,
|
||||
name,
|
||||
description,
|
||||
is_default,
|
||||
default_effort,
|
||||
efforts,
|
||||
context_tiers,
|
||||
default_context_tokens: default_max.or(max_window).or(max_prompt),
|
||||
max_context_tokens: long_max.or(default_max).or(max_window).or(max_prompt),
|
||||
pricing,
|
||||
})
|
||||
}
|
||||
|
||||
fn parse_efforts(m: &Value) -> Vec<String> {
|
||||
let arr = m
|
||||
.get("supportedReasoningEfforts")
|
||||
.or_else(|| m.get("supported_reasoning_efforts"))
|
||||
.or_else(|| m.pointer("/capabilities/supports/reasoning_effort"))
|
||||
.or_else(|| m.pointer("/capabilities/supports/reasoningEffort"));
|
||||
arr.and_then(Value::as_array)
|
||||
.into_iter()
|
||||
.flatten()
|
||||
.filter_map(|v| v.as_str().filter(|s| !s.is_empty()).map(str::to_owned))
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
enum PriceUnit {
|
||||
Credits,
|
||||
NanoAiu,
|
||||
}
|
||||
|
||||
fn parse_price_units(raw: Option<&Value>) -> HashMap<String, PriceUnit> {
|
||||
let mut out = HashMap::new();
|
||||
let Some(raw) = raw else { return out };
|
||||
let unit_of = |s: &str| {
|
||||
if s.eq_ignore_ascii_case("nano_aiu") || s.eq_ignore_ascii_case("nanoaiu") || s.contains("nano") {
|
||||
Some(PriceUnit::NanoAiu)
|
||||
} else if s.to_ascii_lowercase().contains("credit") || s.eq_ignore_ascii_case("aic") {
|
||||
Some(PriceUnit::Credits)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
};
|
||||
if let Some(map) =
|
||||
raw.get("units").or_else(|| raw.get("priceUnits")).or_else(|| raw.get("models")).and_then(Value::as_object)
|
||||
{
|
||||
for (k, v) in map {
|
||||
let u = v
|
||||
.as_str()
|
||||
.and_then(unit_of)
|
||||
.or_else(|| v.get("unit").or_else(|| v.get("priceUnit")).and_then(Value::as_str).and_then(unit_of));
|
||||
if let Some(u) = u {
|
||||
out.insert(k.clone(), u);
|
||||
}
|
||||
}
|
||||
}
|
||||
for key in ["units", "priceUnits", "models"] {
|
||||
if let Some(arr) = raw.get(key).and_then(Value::as_array) {
|
||||
for item in arr {
|
||||
let id = str_of(item, "modelId").or_else(|| str_of(item, "id"));
|
||||
let u = str_of(item, "unit").or_else(|| str_of(item, "priceUnit")).and_then(unit_of);
|
||||
if let (Some(id), Some(u)) = (id, u) {
|
||||
out.insert(id.to_owned(), u);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
fn parse_token_prices(
|
||||
tp: Option<&Value>,
|
||||
max_prompt: Option<u32>,
|
||||
max_window: Option<u32>,
|
||||
unit: PriceUnit,
|
||||
) -> (BTreeMap<String, AgentTokenPricing>, Option<u32>, Option<u32>) {
|
||||
let mut tiers = BTreeMap::new();
|
||||
let Some(tp) = tp else {
|
||||
return (tiers, None, max_window.or(max_prompt));
|
||||
};
|
||||
let batch =
|
||||
f64_of(tp, "batch_size").or_else(|| f64_of(tp, "batchSize")).filter(|&b| b > 0.0).unwrap_or(BATCH_TOKENS);
|
||||
let batch_scale = BATCH_TOKENS / batch;
|
||||
let unit_scale = match unit {
|
||||
PriceUnit::Credits => 1.0,
|
||||
PriceUnit::NanoAiu => 1.0 / NANO_AIU_PER_CREDIT,
|
||||
};
|
||||
let scale = batch_scale * unit_scale;
|
||||
|
||||
let parse_tier = |node: &Value| -> Option<(AgentTokenPricing, Option<u32>)> {
|
||||
let input = f64_of(node, "input_price").or_else(|| f64_of(node, "inputPrice"))?;
|
||||
let output = f64_of(node, "output_price").or_else(|| f64_of(node, "outputPrice"))?;
|
||||
// Guard against raw nano-AIU numbers even if `listTokenPriceUnits` was unavailable.
|
||||
let effective = if unit == PriceUnit::Credits && (input >= 10_000_000.0 || output >= 10_000_000.0) {
|
||||
scale / NANO_AIU_PER_CREDIT
|
||||
} else {
|
||||
scale
|
||||
};
|
||||
let cache_read = f64_of(node, "cache_read_price")
|
||||
.or_else(|| f64_of(node, "cacheReadPrice"))
|
||||
.or_else(|| f64_of(node, "cache_price"))
|
||||
.or_else(|| f64_of(node, "cachePrice"))
|
||||
.map(|v| round6(v * effective));
|
||||
let cache_write = f64_of(node, "cache_write_price")
|
||||
.or_else(|| f64_of(node, "cacheWritePrice"))
|
||||
.map(|v| round6(v * effective));
|
||||
let ctx = u32_of(node, "max_prompt_tokens")
|
||||
.or_else(|| u32_of(node, "maxPromptTokens"))
|
||||
.or_else(|| u32_of(node, "context_max"))
|
||||
.or_else(|| u32_of(node, "contextMax"))
|
||||
.filter(|&n| n > 0);
|
||||
Some((
|
||||
AgentTokenPricing {
|
||||
input: round6(input * effective),
|
||||
output: round6(output * effective),
|
||||
cache_read,
|
||||
cache_write,
|
||||
context_tokens: ctx,
|
||||
},
|
||||
ctx,
|
||||
))
|
||||
};
|
||||
|
||||
// Both CAPI `/models` (`token_prices.default` + `token_prices.long_context`) and
|
||||
// `copilot --headless` `models.list` (top-level `tokenPrices` + `tokenPrices.longContext`)
|
||||
// share the same per-tier fields.
|
||||
let def_node = tp.get("default").unwrap_or(tp);
|
||||
let mut def_max = None;
|
||||
if let Some((def_pricing, ctx)) = parse_tier(def_node) {
|
||||
def_max = ctx;
|
||||
tiers.insert("default".into(), def_pricing);
|
||||
}
|
||||
|
||||
let long_node = tp.get("longContext").or_else(|| tp.get("long_context"));
|
||||
let mut long_max = None;
|
||||
if let Some(long_node) = long_node
|
||||
&& let Some((mut long_pricing, ctx)) = parse_tier(long_node)
|
||||
{
|
||||
// Use the full context window capacity (`1_000_000`) when available so models whose
|
||||
// `max_prompt_tokens` deduct `max_output_tokens` (e.g. `872_000` = `1M - 128K` on
|
||||
// GPT-6 Sol, `936_000` = `1M - 64K` on Gemini 4 Carbon) display cleanly as `1M`.
|
||||
let cap = match (ctx, max_window) {
|
||||
(Some(c), Some(w)) if w >= c => Some(w),
|
||||
(Some(c), _) => Some(c),
|
||||
(None, w) => w.or(max_prompt),
|
||||
};
|
||||
long_max = cap;
|
||||
long_pricing.context_tokens = cap;
|
||||
tiers.insert("long_context".into(), long_pricing);
|
||||
} else if let (Some(d), Some(p)) = (def_max, max_prompt)
|
||||
&& d < p
|
||||
&& let Some(def) = tiers.get("default").cloned()
|
||||
{
|
||||
let cap = max_window.filter(|&w| w >= p).unwrap_or(p);
|
||||
long_max = Some(cap);
|
||||
tiers.insert("long_context".into(), AgentTokenPricing { context_tokens: Some(cap), ..def });
|
||||
}
|
||||
|
||||
(tiers, def_max, long_max)
|
||||
}
|
||||
|
||||
fn normalize_cost_tier(raw: &str) -> Option<&'static str> {
|
||||
match raw.trim().to_ascii_lowercase().as_str() {
|
||||
"lightweight" | "low" => Some("low"),
|
||||
"versatile" | "medium" | "standard" => Some("medium"),
|
||||
"powerful" | "high" | "premium" => Some("high"),
|
||||
"very_high" | "veryhigh" => Some("very_high"),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn infer_cost_tier(output_credits_per_1m: f64) -> &'static str {
|
||||
if output_credits_per_1m < 300.0 {
|
||||
"low"
|
||||
} else if output_credits_per_1m <= 1500.0 {
|
||||
"medium"
|
||||
} else {
|
||||
"high"
|
||||
}
|
||||
}
|
||||
|
||||
/// Formats token capacity matching VS Code (`200000` → `"200K"`, `1000000` → `"1M"`).
|
||||
pub(super) fn format_tokens(tokens: u32) -> String {
|
||||
if tokens >= 1_000_000 {
|
||||
let m = (f64::from(tokens) / 1_000_000.0 * 10.0).floor() / 10.0;
|
||||
if (m - m.round()).abs() < 1e-6 { format!("{}M", m.round() as u32) } else { format!("{m:.1}M") }
|
||||
} else if tokens > 900_000 {
|
||||
"1M".into()
|
||||
} else if tokens >= 1_000 {
|
||||
format!("{}K", (f64::from(tokens) / 1_000.0).round() as u32)
|
||||
} else {
|
||||
tokens.to_string()
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn effort_rank(effort: &str) -> u8 {
|
||||
match effort {
|
||||
"none" => 0,
|
||||
"minimal" => 1,
|
||||
"low" => 2,
|
||||
"medium" => 3,
|
||||
"high" => 4,
|
||||
"xhigh" | "extra_high" => 5,
|
||||
"max" => 6,
|
||||
_ => 3,
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn effort_label(effort: &str) -> (&'static str, &'static str) {
|
||||
match effort {
|
||||
"none" => ("None", "No additional reasoning; fastest responses."),
|
||||
"minimal" => ("Minimal", "Least reasoning; fastest responses."),
|
||||
"low" => ("Low", "Minimal reasoning before responding."),
|
||||
"medium" => ("Medium", "Balanced reasoning and speed."),
|
||||
"high" => ("High", "More thorough reasoning; slower responses."),
|
||||
"xhigh" => ("Extra High", "Extensive reasoning for the hardest problems."),
|
||||
"max" => ("Max", "Maximum reasoning; slowest but most thorough."),
|
||||
_ => ("Default", ""),
|
||||
}
|
||||
}
|
||||
|
||||
/// ACP `configOptions` for the active session settings.
|
||||
pub(super) fn config_options(models: &[ModelEntry], s: &Settings) -> Value {
|
||||
let mut model_values: Vec<Value> = models.iter().map(model_option_value).collect();
|
||||
if !s.model.is_empty() && !models.iter().any(|m| m.id == s.model) {
|
||||
model_values.push(json!({ "value": s.model, "name": s.model }));
|
||||
}
|
||||
let mut out = Vec::new();
|
||||
if !model_values.is_empty() {
|
||||
out.push(json!({
|
||||
"id": MODEL,
|
||||
"name": "Model",
|
||||
"category": "model",
|
||||
"type": "select",
|
||||
"currentValue": s.model,
|
||||
"options": model_values,
|
||||
}));
|
||||
}
|
||||
if let Some(m) = models.iter().find(|m| m.id == s.model) {
|
||||
if let (Some(opt), Some(current)) = (effort_option_for(m, s.effort.as_deref()), s.effort.as_deref()) {
|
||||
let mut o = opt;
|
||||
o["currentValue"] = json!(current);
|
||||
out.push(o);
|
||||
}
|
||||
if let (Some(opt), Some(current)) =
|
||||
(context_tier_option_for(m, s.context_tier.as_deref()), s.context_tier.as_deref())
|
||||
{
|
||||
let mut o = opt;
|
||||
o["currentValue"] = json!(current);
|
||||
out.push(o);
|
||||
}
|
||||
}
|
||||
out.push(json!({
|
||||
"id": MODE,
|
||||
"name": "Mode",
|
||||
"category": "mode",
|
||||
"type": "select",
|
||||
"currentValue": s.mode.id(),
|
||||
"options": Mode::ALL.iter().map(|m| json!({
|
||||
"value": m.id(),
|
||||
"name": m.name(),
|
||||
"description": m.description(),
|
||||
})).collect::<Vec<_>>(),
|
||||
}));
|
||||
out.push(json!({
|
||||
"id": ALLOW_ALL,
|
||||
"name": "Allow All",
|
||||
"category": "permissions",
|
||||
"type": "select",
|
||||
"currentValue": if s.allow_all { "on" } else { "off" },
|
||||
"options": [
|
||||
{ "value": "on", "name": "On", "description": "Automatically approve all tool, path, and URL requests" },
|
||||
{ "value": "off", "name": "Off", "description": "Require approval for tool, path, and URL requests" },
|
||||
],
|
||||
}));
|
||||
Value::Array(out)
|
||||
}
|
||||
|
||||
/// Per-model options (`[reasoning_effort?, context_tier?]`) for every model in `models`,
|
||||
/// each defaulted to Copilot's own defaults (respecting `user.settings.get` / CLI flags).
|
||||
pub(super) fn per_model_options(models: &[ModelEntry], defaults: &UserDefaults) -> Vec<(String, Vec<Value>)> {
|
||||
models
|
||||
.iter()
|
||||
.map(|m| {
|
||||
let mut opts = Vec::new();
|
||||
if let Some(effort) = effort_option_for(m, m.default_effort(defaults.effort.as_deref())) {
|
||||
opts.push(effort);
|
||||
}
|
||||
if let Some(ctx) = context_tier_option_for(m, m.default_context_tier(defaults.context_tier.as_deref())) {
|
||||
opts.push(ctx);
|
||||
}
|
||||
(m.id.clone(), opts)
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn model_option_value(m: &ModelEntry) -> Value {
|
||||
let mut v = json!({ "value": m.id, "name": m.name });
|
||||
if let Some(d) = &m.description {
|
||||
v["description"] = json!(d);
|
||||
}
|
||||
if let Some(pricing) = &m.pricing {
|
||||
let mut meta = serde_json::Map::new();
|
||||
meta.insert("pricing".into(), json!(pricing));
|
||||
if let Some(cat) = &pricing.cost_tier {
|
||||
meta.insert("copilotPriceCategory".into(), json!(cat));
|
||||
}
|
||||
v["_meta"] = Value::Object(meta);
|
||||
}
|
||||
v
|
||||
}
|
||||
|
||||
fn effort_option_for(m: &ModelEntry, current: Option<&str>) -> Option<Value> {
|
||||
if m.efforts.len() <= 1 && m.efforts.is_empty() {
|
||||
return None;
|
||||
}
|
||||
let default = current.filter(|c| m.efforts.iter().any(|e| e == *c)).or_else(|| m.default_effort(None))?;
|
||||
let options: Vec<Value> = m
|
||||
.efforts
|
||||
.iter()
|
||||
.map(|e| {
|
||||
let (label, desc) = effort_label(e);
|
||||
let name = if label == "Default" { capitalize(e) } else { label.to_owned() };
|
||||
let mut item = json!({ "value": e, "name": name });
|
||||
if !desc.is_empty() {
|
||||
item["description"] = json!(desc);
|
||||
}
|
||||
item
|
||||
})
|
||||
.collect();
|
||||
Some(json!({
|
||||
"id": EFFORT,
|
||||
"name": "Effort Level",
|
||||
"description": "How much reasoning the model performs before responding. Higher effort can improve quality at the cost of speed.",
|
||||
"category": "thought_level",
|
||||
"type": "select",
|
||||
"currentValue": default,
|
||||
"options": options,
|
||||
}))
|
||||
}
|
||||
|
||||
fn context_tier_option_for(m: &ModelEntry, current: Option<&str>) -> Option<Value> {
|
||||
if m.context_tiers.len() < 2 {
|
||||
return None;
|
||||
}
|
||||
let default =
|
||||
current.filter(|c| m.context_tiers.iter().any(|t| t.id == *c)).or_else(|| m.default_context_tier(None))?;
|
||||
let options: Vec<Value> = m
|
||||
.context_tiers
|
||||
.iter()
|
||||
.map(|t| {
|
||||
json!({
|
||||
"value": t.id,
|
||||
"name": t.label,
|
||||
"description": t.description,
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
Some(json!({
|
||||
"id": CONTEXT_TIER,
|
||||
"name": "Context Size",
|
||||
"description": "Context window size for this model.",
|
||||
"category": "model_config",
|
||||
"type": "select",
|
||||
"currentValue": default,
|
||||
"options": options,
|
||||
}))
|
||||
}
|
||||
|
||||
fn capitalize(s: &str) -> String {
|
||||
let mut chars = s.chars();
|
||||
chars.next().map(|c| c.to_uppercase().chain(chars).collect()).unwrap_or_default()
|
||||
}
|
||||
|
||||
fn round6(n: f64) -> f64 {
|
||||
let r = (n * 1_000_000.0).round() / 1_000_000.0;
|
||||
if r == 0.0 { 0.0 } else { r }
|
||||
}
|
||||
|
||||
fn str_of<'a>(v: &'a Value, key: &str) -> Option<&'a str> {
|
||||
v.get(key).and_then(Value::as_str)
|
||||
}
|
||||
|
||||
fn f64_of(v: &Value, key: &str) -> Option<f64> {
|
||||
v.get(key).and_then(Value::as_f64).filter(|n| n.is_finite())
|
||||
}
|
||||
|
||||
fn u32_of(v: &Value, key: &str) -> Option<u32> {
|
||||
v.get(key).and_then(Value::as_u64).and_then(|n| u32::try_from(n).ok())
|
||||
}
|
||||
@@ -0,0 +1,540 @@
|
||||
use super::models::{self, CONTEXT_TIER, EFFORT, MODEL, Mode, Settings, UserDefaults};
|
||||
use super::{acp_args, acp_fallback_spec, attach, headless_args};
|
||||
use crate::config;
|
||||
use crate::provider::Driver;
|
||||
use ide_jsonrpc::{Framing, Handler, Peer, codes};
|
||||
use parking_lot::Mutex;
|
||||
use serde_json::{Value, json};
|
||||
use std::sync::{Arc, OnceLock};
|
||||
|
||||
fn sample_models_list() -> Value {
|
||||
json!({
|
||||
"models": [
|
||||
{
|
||||
"id": "gemini-4-carbon",
|
||||
"name": "Gemini 4 Carbon",
|
||||
"priceCategory": "versatile",
|
||||
"supportedReasoningEfforts": ["low", "medium", "high"],
|
||||
"defaultReasoningEffort": "medium",
|
||||
"capabilities": {
|
||||
"supports": { "vision": true, "reasoningEffort": true },
|
||||
"limits": {
|
||||
"max_context_window_tokens": 1_000_000,
|
||||
"max_prompt_tokens": 1_000_000,
|
||||
"max_output_tokens": 64_000
|
||||
}
|
||||
},
|
||||
"billing": {
|
||||
"is_premium": true,
|
||||
"multiplier": 1.0,
|
||||
"token_prices": {
|
||||
"batch_size": 1_000_000,
|
||||
"preferred_tier": "default",
|
||||
"default": {
|
||||
"input_price": 100.0,
|
||||
"output_price": 500.0,
|
||||
"cache_read_price": 10.0,
|
||||
"cache_write_price": 0.0,
|
||||
"max_prompt_tokens": 200_000
|
||||
},
|
||||
"long_context": {
|
||||
"input_price": 200.0,
|
||||
"output_price": 1000.0,
|
||||
"cache_read_price": 10.0,
|
||||
"cache_write_price": 0.0,
|
||||
"max_prompt_tokens": 1_000_000
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "gpt-5.6-sol",
|
||||
"name": "GPT-5.6 Sol",
|
||||
"priceCategory": "powerful",
|
||||
"supportedReasoningEfforts": ["none", "low", "medium", "high", "xhigh", "max"],
|
||||
"defaultReasoningEffort": "medium",
|
||||
"capabilities": {
|
||||
"supports": { "vision": true },
|
||||
"limits": {
|
||||
"max_context_window_tokens": 1_000_000,
|
||||
"max_prompt_tokens": 1_000_000
|
||||
}
|
||||
},
|
||||
"billing": {
|
||||
"multiplier": 2.0,
|
||||
"token_prices": {
|
||||
"batch_size": 1_000_000,
|
||||
"default": {
|
||||
"input_price": 250_000_000_000.0,
|
||||
"output_price": 1_500_000_000_000.0,
|
||||
"cache_read_price": 25_000_000_000.0,
|
||||
"cache_write_price": 0.0,
|
||||
"max_prompt_tokens": 200_000
|
||||
},
|
||||
"long_context": {
|
||||
"input_price": 500_000_000_000.0,
|
||||
"output_price": 3_000_000_000_000.0,
|
||||
"cache_read_price": 50_000_000_000.0,
|
||||
"cache_write_price": 0.0,
|
||||
"max_prompt_tokens": 1_000_000
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "mai-experimental-internal",
|
||||
"name": "MAI Experimental",
|
||||
"priceCategory": "lightweight",
|
||||
"supportedReasoningEfforts": ["low", "medium"],
|
||||
"capabilities": {
|
||||
"limits": {
|
||||
"max_context_window_tokens": 131_072,
|
||||
"max_prompt_tokens": 131_072
|
||||
}
|
||||
},
|
||||
"billing": {
|
||||
"multiplier": 0.33
|
||||
}
|
||||
}
|
||||
]
|
||||
})
|
||||
}
|
||||
|
||||
fn sample_price_units() -> Value {
|
||||
json!({
|
||||
"units": {
|
||||
"gemini-4-carbon": "ai_credits",
|
||||
"gpt-5.6-sol": "nano_aiu"
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parses_tiered_context_windows_pricing_and_uses_copilots_own_defaults() {
|
||||
let catalog = models::parse_catalog(&sample_models_list(), Some(&sample_price_units()));
|
||||
assert_eq!(
|
||||
catalog.iter().map(|m| m.id.as_str()).collect::<Vec<_>>(),
|
||||
["auto", "gemini-4-carbon", "gpt-5.6-sol", "mai-experimental-internal"]
|
||||
);
|
||||
|
||||
let gemini = catalog.iter().find(|m| m.id == "gemini-4-carbon").unwrap();
|
||||
assert_eq!(gemini.default_effort(None), Some("medium"));
|
||||
assert_eq!(gemini.default_context_tier(None), Some("default"));
|
||||
assert_eq!(
|
||||
gemini.context_tiers.iter().map(|t| (t.id, t.label.as_str(), t.tokens)).collect::<Vec<_>>(),
|
||||
[("default", "200K", 200_000), ("long_context", "1M", 1_000_000)]
|
||||
);
|
||||
let pricing = gemini.pricing.as_ref().unwrap();
|
||||
assert_eq!(pricing.cost_tier.as_deref(), Some("medium"));
|
||||
let long_p = &pricing.tiers["long_context"];
|
||||
assert_eq!(
|
||||
(long_p.input, long_p.output, long_p.cache_read, long_p.cache_write),
|
||||
(200.0, 1000.0, Some(10.0), Some(0.0))
|
||||
);
|
||||
let def_p = &pricing.tiers["default"];
|
||||
assert_eq!((def_p.input, def_p.output), (100.0, 500.0));
|
||||
|
||||
// Nano-AIU conversion (10^9 nano-AIU = 1 AI Credit).
|
||||
let sol = catalog.iter().find(|m| m.id == "gpt-5.6-sol").unwrap();
|
||||
assert_eq!(sol.default_effort(None), Some("medium"));
|
||||
assert_eq!(sol.default_context_tier(None), Some("default"));
|
||||
let sol_long = &sol.pricing.as_ref().unwrap().tiers["long_context"];
|
||||
assert_eq!((sol_long.input, sol_long.output, sol_long.cache_read), (500.0, 3000.0, Some(50.0)));
|
||||
|
||||
// Single-window model has no multi-tier `context_tier` toggle.
|
||||
let mai = catalog.iter().find(|m| m.id == "mai-experimental-internal").unwrap();
|
||||
assert!(mai.context_tiers.is_empty());
|
||||
assert_eq!(mai.default_effort(None), Some("medium"));
|
||||
|
||||
// Initial settings pick Copilot's own defaults (`medium` effort + `default` 200K context).
|
||||
let mut s = Settings::new(&catalog, &UserDefaults::default());
|
||||
assert_eq!(s.model, "gemini-4-carbon");
|
||||
assert_eq!(s.effort.as_deref(), Some("medium"));
|
||||
assert_eq!(s.context_tier.as_deref(), Some("default"));
|
||||
assert_eq!(s.active_context_tokens(&catalog), Some(200_000));
|
||||
|
||||
// Switching to GPT-5.6 Sol without an explicit override stays on Copilot's defaults (`medium` + `default`).
|
||||
s.set(&catalog, MODEL, "gpt-5.6-sol").unwrap();
|
||||
assert_eq!(s.effort.as_deref(), Some("medium"));
|
||||
assert_eq!(s.context_tier.as_deref(), Some("default"));
|
||||
|
||||
// Explicit session overrides (`xhigh` + `long_context`) stick across models that support them,
|
||||
// and fall back to Copilot's default when switching to a model that does not.
|
||||
s.set(&catalog, EFFORT, "xhigh").unwrap();
|
||||
s.set(&catalog, CONTEXT_TIER, "long_context").unwrap();
|
||||
assert_eq!(s.active_context_tokens(&catalog), Some(1_000_000));
|
||||
s.set(&catalog, MODEL, "mai-experimental-internal").unwrap();
|
||||
assert_eq!(s.effort.as_deref(), Some("medium"));
|
||||
assert_eq!(s.context_tier, None);
|
||||
|
||||
// User settings from `user.settings.get` (1.0.112+ `{ value, default, isDefault }` shape) and CLI flags are honored.
|
||||
let rpc_settings = json!({
|
||||
"settings": {
|
||||
"model": { "value": "gpt-5.6-sol", "default": null, "isDefault": false },
|
||||
"effortLevel": { "value": "high", "default": null, "isDefault": false },
|
||||
"contextTier": { "value": "long_context", "default": null, "isDefault": false },
|
||||
"defaultMode": { "value": "plan", "default": "interactive", "isDefault": false },
|
||||
}
|
||||
});
|
||||
let user_defs = UserDefaults::from_rpc_and_args(Some(&rpc_settings), &[]);
|
||||
let configured = Settings::new(&catalog, &user_defs);
|
||||
assert_eq!(configured.model, "gpt-5.6-sol");
|
||||
assert_eq!(configured.effort.as_deref(), Some("high"));
|
||||
assert_eq!(configured.context_tier.as_deref(), Some("long_context"));
|
||||
assert_eq!(configured.mode, Mode::Plan);
|
||||
|
||||
// Per-model discovery options preserve pricing on `model` values and emit Copilot's defaults.
|
||||
let raw = models::config_options(&catalog, &Settings::new(&catalog, &UserDefaults::default()));
|
||||
let parsed = config::parse_options(&raw);
|
||||
let model_opt = parsed.iter().find(|o| o.id == MODEL).unwrap();
|
||||
let gem_val = model_opt.values.iter().find(|v| v.value == "gemini-4-carbon").unwrap();
|
||||
assert_eq!(gem_val.pricing.as_ref().unwrap().cost_tier.as_deref(), Some("medium"));
|
||||
assert_eq!(parsed.iter().find(|o| o.id == EFFORT).unwrap().current.as_deref(), Some("medium"));
|
||||
assert_eq!(parsed.iter().find(|o| o.id == CONTEXT_TIER).unwrap().current.as_deref(), Some("default"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parses_headless_sdk_models_list_flat_token_prices_and_single_tier_models() {
|
||||
let sdk_list = json!({
|
||||
"models": [
|
||||
{ "id": "auto", "name": "Auto" },
|
||||
{
|
||||
"id": "gemini-4-carbon",
|
||||
"name": "Gemini 4 Carbon",
|
||||
"modelPickerCategory": "powerful",
|
||||
"modelPickerPriceCategory": "medium",
|
||||
"supportedReasoningEfforts": ["low", "medium", "high"],
|
||||
"capabilities": {
|
||||
"limits": {
|
||||
"max_context_window_tokens": 1_000_000,
|
||||
"max_output_tokens": 64_000,
|
||||
"max_prompt_tokens": 936_000
|
||||
}
|
||||
},
|
||||
"billing": {
|
||||
"tokenPrices": {
|
||||
"inputPrice": 200,
|
||||
"outputPrice": 1000,
|
||||
"cachePrice": 10,
|
||||
"cacheReadPrice": 10,
|
||||
"cacheWritePrice": 0,
|
||||
"contextMax": 200_000,
|
||||
"maxPromptTokens": 200_000,
|
||||
"batchSize": 1_000_000,
|
||||
"longContext": {
|
||||
"inputPrice": 200,
|
||||
"outputPrice": 1000,
|
||||
"cachePrice": 10,
|
||||
"cacheReadPrice": 10,
|
||||
"cacheWritePrice": 0,
|
||||
"contextMax": 936_000,
|
||||
"maxPromptTokens": 936_000
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "gpt-6-sol",
|
||||
"name": "GPT-6 Sol",
|
||||
"modelPickerPriceCategory": "medium",
|
||||
"supportedReasoningEfforts": ["none", "low", "medium", "high", "xhigh", "max"],
|
||||
"capabilities": {
|
||||
"limits": {
|
||||
"max_context_window_tokens": 1_000_000,
|
||||
"max_output_tokens": 128_000,
|
||||
"max_prompt_tokens": 872_000
|
||||
}
|
||||
},
|
||||
"billing": {
|
||||
"tokenPrices": {
|
||||
"inputPrice": 250,
|
||||
"outputPrice": 1250,
|
||||
"contextMax": 272_000,
|
||||
"maxPromptTokens": 272_000,
|
||||
"batchSize": 1_000_000,
|
||||
"longContext": {
|
||||
"inputPrice": 500,
|
||||
"outputPrice": 2500,
|
||||
"contextMax": 872_000,
|
||||
"maxPromptTokens": 872_000
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "gpt-5.3-codex",
|
||||
"name": "GPT-5.3-Codex",
|
||||
"modelPickerPriceCategory": "medium",
|
||||
"supportedReasoningEfforts": ["low", "medium", "high", "xhigh"],
|
||||
"capabilities": {
|
||||
"limits": {
|
||||
"max_context_window_tokens": 400_000,
|
||||
"max_output_tokens": 128_000,
|
||||
"max_prompt_tokens": 272_000
|
||||
}
|
||||
},
|
||||
"billing": {
|
||||
"tokenPrices": {
|
||||
"inputPrice": 150,
|
||||
"outputPrice": 600,
|
||||
"contextMax": 272_000,
|
||||
"maxPromptTokens": 272_000,
|
||||
"batchSize": 1_000_000
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
});
|
||||
let units = json!({
|
||||
"units": [
|
||||
{ "modelId": "gemini-4-carbon", "unit": "ai_credits" },
|
||||
{ "modelId": "gpt-6-sol", "unit": "ai_credits" },
|
||||
{ "modelId": "gpt-5.3-codex", "unit": "ai_credits" }
|
||||
]
|
||||
});
|
||||
|
||||
let catalog = models::parse_catalog(&sdk_list, Some(&units));
|
||||
let carbon = catalog.iter().find(|m| m.id == "gemini-4-carbon").unwrap();
|
||||
assert_eq!(
|
||||
carbon.context_tiers.iter().map(|t| (t.id, t.label.as_str())).collect::<Vec<_>>(),
|
||||
[("default", "200K"), ("long_context", "1M")]
|
||||
);
|
||||
let cp = carbon.pricing.as_ref().unwrap();
|
||||
assert_eq!(cp.cost_tier.as_deref(), Some("medium"));
|
||||
assert_eq!((cp.tiers["long_context"].input, cp.tiers["long_context"].output), (200.0, 1000.0));
|
||||
|
||||
let sol = catalog.iter().find(|m| m.id == "gpt-6-sol").unwrap();
|
||||
assert_eq!(
|
||||
sol.context_tiers.iter().map(|t| (t.id, t.label.as_str())).collect::<Vec<_>>(),
|
||||
[("default", "272K"), ("long_context", "1M")]
|
||||
);
|
||||
|
||||
let codex = catalog.iter().find(|m| m.id == "gpt-5.3-codex").unwrap();
|
||||
assert!(codex.context_tiers.is_empty(), "single-tier model must not get a synthetic long_context tier");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn headless_and_acp_args_roundtrip_while_preserving_user_flags() {
|
||||
assert_eq!(headless_args(&["--acp".into()]), vec!["--headless", "--no-auto-update", "--stdio"]);
|
||||
assert_eq!(
|
||||
headless_args(&["--log-level".into(), "debug".into(), "--headless".into(), "--stdio".into()]),
|
||||
vec!["--log-level", "debug", "--headless", "--stdio", "--no-auto-update"]
|
||||
);
|
||||
assert_eq!(
|
||||
acp_args(&[
|
||||
"--headless".into(),
|
||||
"--no-auto-update".into(),
|
||||
"--stdio".into(),
|
||||
"--context".into(),
|
||||
"long_context".into()
|
||||
]),
|
||||
vec!["--acp", "--context", "long_context"]
|
||||
);
|
||||
let spec = crate::provider::builtin_spec("copilot").unwrap();
|
||||
assert_eq!(spec.driver, Driver::Copilot);
|
||||
let fallback = acp_fallback_spec(&spec);
|
||||
assert_eq!(fallback.driver, Driver::Acp);
|
||||
assert_eq!(fallback.args, vec!["--acp"]);
|
||||
}
|
||||
|
||||
struct FakeCopilotServer {
|
||||
peer: OnceLock<Peer>,
|
||||
calls: Mutex<Vec<(String, Value)>>,
|
||||
}
|
||||
|
||||
impl Handler for FakeCopilotServer {
|
||||
fn on_notification(&self, _method: &str, _params: Value) {}
|
||||
|
||||
fn on_request(&self, method: &str, params: Value) -> Result<Value, (i64, String)> {
|
||||
self.calls.lock().push((method.to_owned(), params.clone()));
|
||||
match method {
|
||||
"connect" => Ok(json!({ "ok": true, "protocolVersion": 3, "version": "1.0.112" })),
|
||||
"models.list" => Ok(sample_models_list()),
|
||||
"models.listTokenPriceUnits" => Ok(sample_price_units()),
|
||||
"user.settings.get" => Ok(json!({ "settings": { "model": "gemini-4-carbon" } })),
|
||||
"commands.list" => Ok(json!({ "commands": [{ "name": "compact", "description": "Compact context" }] })),
|
||||
"skills.discover" => Ok(json!({ "skills": [] })),
|
||||
"session.create" => Ok(json!({ "sessionId": "cop-sess-1" })),
|
||||
"session.model.switchTo" => Ok(json!({ "modelId": params["modelId"] })),
|
||||
"session.mode.set" => Ok(json!({})),
|
||||
"session.permissions.setApproveAll" => {
|
||||
assert!(params.get("enabled").and_then(Value::as_bool).is_some());
|
||||
Ok(json!({ "success": true }))
|
||||
}
|
||||
"session.send" => {
|
||||
if let Some(peer) = self.peer.get().cloned() {
|
||||
let sid = params["sessionId"].as_str().unwrap_or("cop-sess-1").to_owned();
|
||||
std::thread::spawn(move || {
|
||||
let _ = peer.notify(
|
||||
"session.event",
|
||||
json!({
|
||||
"sessionId": sid,
|
||||
"event": {
|
||||
"id": "e1",
|
||||
"type": "session.usage_info",
|
||||
"data": { "currentTokens": 42_000, "tokenLimit": 1_000_000 }
|
||||
}
|
||||
}),
|
||||
);
|
||||
let _ = peer.notify(
|
||||
"session.event",
|
||||
json!({
|
||||
"sessionId": sid,
|
||||
"event": {
|
||||
"id": "e2",
|
||||
"type": "assistant.message_delta",
|
||||
"data": { "messageId": "m1", "deltaContent": "Hello from 1M context!" }
|
||||
}
|
||||
}),
|
||||
);
|
||||
let _ = peer.notify(
|
||||
"session.event",
|
||||
json!({
|
||||
"sessionId": sid,
|
||||
"event": {
|
||||
"id": "e3",
|
||||
"type": "session.idle",
|
||||
"data": { "aborted": false, "mode": "interactive" }
|
||||
}
|
||||
}),
|
||||
);
|
||||
});
|
||||
}
|
||||
Ok(json!({ "messageId": "msg-1" }))
|
||||
}
|
||||
_ => Err((codes::METHOD_NOT_FOUND, format!("unexpected {method}"))),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct CollectUpdates(Mutex<Vec<Value>>);
|
||||
|
||||
impl Handler for CollectUpdates {
|
||||
fn on_notification(&self, method: &str, params: Value) {
|
||||
if method == "session/update" {
|
||||
self.0.lock().push(params["update"].clone());
|
||||
}
|
||||
}
|
||||
|
||||
fn on_request(&self, method: &str, _params: Value) -> Result<Value, (i64, String)> {
|
||||
Err((codes::METHOD_NOT_FOUND, format!("unexpected {method}")))
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn shim_creates_session_with_copilot_defaults_switches_tiers_and_streams_turns() {
|
||||
let server = Arc::new(FakeCopilotServer { peer: OnceLock::new(), calls: Mutex::new(Vec::new()) });
|
||||
let updates = Arc::new(CollectUpdates(Mutex::new(Vec::new())));
|
||||
let server_clone = server.clone();
|
||||
let client = attach(updates.clone(), move |events| {
|
||||
let (a, b) = tokio::io::duplex(1 << 16);
|
||||
let (ar, aw) = tokio::io::split(a);
|
||||
let (br, bw) = tokio::io::split(b);
|
||||
let srv_peer = Peer::connect_framed(br, bw, server_clone.clone(), Framing::ContentLength);
|
||||
let _ = server_clone.peer.set(srv_peer);
|
||||
Ok(Peer::connect_framed(ar, aw, events, Framing::ContentLength))
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
let init: Value = client.request("initialize", json!({}), None).await.unwrap();
|
||||
assert_eq!(init["agentInfo"]["version"], "1.0.112");
|
||||
assert_eq!(init["agentCapabilities"]["_meta"]["openidea"]["steer"], true);
|
||||
|
||||
let created: Value = client.request("session/new", json!({ "cwd": "/tmp", "mcpServers": [] }), None).await.unwrap();
|
||||
assert_eq!(created["sessionId"], "cop-sess-1");
|
||||
|
||||
// Verify `session.create` sent `"trustWorkingDirectory": "session"`, `gemini-4-carbon`,
|
||||
// and Copilot's own defaults (`reasoningEffort: "medium"` and `contextTier: "default"`).
|
||||
{
|
||||
let calls = server.calls.lock();
|
||||
let (_, create_params) = calls.iter().find(|(m, _)| m == "session.create").unwrap();
|
||||
assert_eq!(create_params["trustWorkingDirectory"], "session");
|
||||
assert_eq!(create_params["model"], "gemini-4-carbon");
|
||||
assert_eq!(create_params["reasoningEffort"], "medium");
|
||||
assert_eq!(create_params["contextTier"], "default");
|
||||
}
|
||||
|
||||
// Switch Context Size up to 1M (`long_context`) and Effort to `high` live via `session/set_config_option`.
|
||||
let _: Value = client
|
||||
.request(
|
||||
"session/set_config_option",
|
||||
json!({
|
||||
"sessionId": "cop-sess-1",
|
||||
"configId": "reasoning_effort",
|
||||
"value": "high"
|
||||
}),
|
||||
None,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
let switched_1m: Value = client
|
||||
.request(
|
||||
"session/set_config_option",
|
||||
json!({
|
||||
"sessionId": "cop-sess-1",
|
||||
"configId": "context_tier",
|
||||
"value": "long_context"
|
||||
}),
|
||||
None,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
let opts_1m = config::options_of(&switched_1m);
|
||||
assert_eq!(opts_1m.iter().find(|o| o.id == "context_tier").unwrap().current.as_deref(), Some("long_context"));
|
||||
|
||||
// Run a turn and verify 1M usage_update + streamed message chunk.
|
||||
let prompt_res: Value = client
|
||||
.request(
|
||||
"session/prompt",
|
||||
json!({
|
||||
"sessionId": "cop-sess-1",
|
||||
"prompt": [{ "type": "text", "text": "Explain architecture" }]
|
||||
}),
|
||||
None,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(prompt_res["stopReason"], "end_turn");
|
||||
{
|
||||
let seen = updates.0.lock();
|
||||
assert!(
|
||||
seen.iter().any(|u| u["sessionUpdate"] == "usage_update" && u["size"] == 1_000_000 && u["used"] == 42_000)
|
||||
);
|
||||
assert!(
|
||||
seen.iter().any(
|
||||
|u| u["sessionUpdate"] == "agent_message_chunk" && u["content"]["text"] == "Hello from 1M context!"
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
// Switch Context Size back down to 200K (`default`) live via `session/set_config_option`.
|
||||
let switched: Value = client
|
||||
.request(
|
||||
"session/set_config_option",
|
||||
json!({
|
||||
"sessionId": "cop-sess-1",
|
||||
"configId": "context_tier",
|
||||
"value": "default"
|
||||
}),
|
||||
None,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
let opts = config::options_of(&switched);
|
||||
assert_eq!(opts.iter().find(|o| o.id == "context_tier").unwrap().current.as_deref(), Some("default"));
|
||||
{
|
||||
let calls = server.calls.lock();
|
||||
let (_, sw_params) = calls.iter().rfind(|(m, _)| m == "session.model.switchTo").unwrap();
|
||||
assert_eq!(sw_params["modelId"], "gemini-4-carbon");
|
||||
assert_eq!(sw_params["reasoningEffort"], "high");
|
||||
assert_eq!(sw_params["contextTier"], "default");
|
||||
}
|
||||
// Switching context_tier immediately emits an updated `usage_update` with `size: 200_000`.
|
||||
assert!(
|
||||
updates
|
||||
.0
|
||||
.lock()
|
||||
.iter()
|
||||
.any(|u| u["sessionUpdate"] == "usage_update" && u["size"] == 200_000 && u["used"] == 42_000)
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,583 @@
|
||||
//! Pure translations between Copilot headless JSON-RPC payloads (`session.event`,
|
||||
//! permissions, slash commands, MCP servers) and ACP v1.
|
||||
|
||||
use crate::LIMITS_UPDATE;
|
||||
use serde_json::{Map, Value, json};
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::path::Path;
|
||||
|
||||
const MAX_OUTPUT: usize = 64 * 1024;
|
||||
|
||||
#[derive(Default)]
|
||||
pub(super) struct EventTranslator {
|
||||
/// Assistant message IDs that already streamed deltas, so final `assistant.message` won't duplicate them.
|
||||
streamed_messages: HashSet<String>,
|
||||
/// Tool call metadata captured on `tool.execution_start`, keyed by `toolCallId`.
|
||||
tools: HashMap<String, ToolInfo>,
|
||||
/// Subagents started in this session (`toolCallId` → subagent session id).
|
||||
subagents: HashMap<String, String>,
|
||||
/// Last reported prompt token count, used when updating context size.
|
||||
pub last_used_tokens: Option<u32>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
struct ToolInfo {
|
||||
kind: &'static str,
|
||||
title: String,
|
||||
locations: Vec<Value>,
|
||||
output: String,
|
||||
diffs: Vec<Value>,
|
||||
}
|
||||
|
||||
/// An ACP notification `(session_id, update)` produced from a Copilot `session.event`.
|
||||
pub(super) struct Translated {
|
||||
pub updates: Vec<(String, Value)>,
|
||||
pub terminal: Option<TurnTerminal>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub(super) enum TurnTerminal {
|
||||
Idle { aborted: bool },
|
||||
Error { message: String, data: Option<Value> },
|
||||
}
|
||||
|
||||
impl EventTranslator {
|
||||
pub fn reset_turn(&mut self) {
|
||||
self.streamed_messages.clear();
|
||||
}
|
||||
|
||||
pub fn translate(&mut self, main_session: &str, event: &Value, fallback_limit: Option<u32>) -> Translated {
|
||||
let mut out = Translated { updates: Vec::new(), terminal: None };
|
||||
let event_type = str_of(event, "type").unwrap_or_default();
|
||||
let data = event.get("data").unwrap_or(&Value::Null);
|
||||
let agent_id = str_of(event, "agentId").filter(|s| !s.is_empty());
|
||||
let target_session =
|
||||
agent_id.map(|id| format!("{main_session}:sub:{id}")).unwrap_or_else(|| main_session.to_owned());
|
||||
|
||||
match event_type {
|
||||
"assistant.reasoning_delta" => {
|
||||
let delta = str_of(data, "deltaContent").or_else(|| str_of(data, "content")).unwrap_or_default();
|
||||
if !delta.is_empty() {
|
||||
out.updates.push((
|
||||
target_session,
|
||||
json!({ "sessionUpdate": "agent_thought_chunk", "content": { "type": "text", "text": delta } }),
|
||||
));
|
||||
}
|
||||
}
|
||||
"assistant.message_delta" => {
|
||||
if data.get("parentToolCallId").and_then(Value::as_str).is_some() && agent_id.is_none() {
|
||||
return out;
|
||||
}
|
||||
if let Some(mid) = str_of(data, "messageId") {
|
||||
self.streamed_messages.insert(mid.to_owned());
|
||||
}
|
||||
let delta = str_of(data, "deltaContent").unwrap_or_default();
|
||||
if !delta.is_empty() {
|
||||
out.updates.push((
|
||||
target_session,
|
||||
json!({ "sessionUpdate": "agent_message_chunk", "content": { "type": "text", "text": delta } }),
|
||||
));
|
||||
}
|
||||
}
|
||||
"assistant.message" => {
|
||||
if data.get("parentToolCallId").and_then(Value::as_str).is_some() && agent_id.is_none() {
|
||||
return out;
|
||||
}
|
||||
let already = str_of(data, "messageId").is_some_and(|mid| self.streamed_messages.contains(mid));
|
||||
if !already && let Some(content) = str_of(data, "content").filter(|c| !c.is_empty()) {
|
||||
out.updates.push((
|
||||
target_session,
|
||||
json!({ "sessionUpdate": "agent_message_chunk", "content": { "type": "text", "text": content } }),
|
||||
));
|
||||
}
|
||||
}
|
||||
"tool.execution_start" => {
|
||||
let tool_name = str_of(data, "toolName").unwrap_or_default();
|
||||
if matches!(tool_name, "report_intent" | "tool_search_tool" | "tool_search_tool_regex") {
|
||||
return out;
|
||||
}
|
||||
let Some(call_id) = str_of(data, "toolCallId").filter(|s| !s.is_empty()) else { return out };
|
||||
let args = data.get("arguments").unwrap_or(&Value::Null);
|
||||
if tool_name == "task" && agent_id.is_none() {
|
||||
let sub_id = format!("{main_session}:task:{call_id}");
|
||||
self.subagents.insert(call_id.to_owned(), sub_id.clone());
|
||||
let title = str_of(args, "name").or_else(|| str_of(args, "agent_type")).unwrap_or("Subagent");
|
||||
let desc = str_of(args, "description");
|
||||
let prompt = str_of(args, "prompt");
|
||||
out.updates.push((
|
||||
main_session.to_owned(),
|
||||
json!({
|
||||
"sessionUpdate": "subagent_update",
|
||||
"subagentId": sub_id,
|
||||
"title": title,
|
||||
"description": desc,
|
||||
"prompt": prompt,
|
||||
"status": "running",
|
||||
"cancellable": false,
|
||||
}),
|
||||
));
|
||||
return out;
|
||||
}
|
||||
let info = describe_tool(tool_name, args);
|
||||
let mut update = json!({
|
||||
"sessionUpdate": "tool_call",
|
||||
"toolCallId": call_id,
|
||||
"title": info.title,
|
||||
"kind": info.kind,
|
||||
"status": "in_progress",
|
||||
});
|
||||
if !info.locations.is_empty() {
|
||||
update["locations"] = Value::Array(info.locations.clone());
|
||||
}
|
||||
if !info.diffs.is_empty() {
|
||||
update["content"] = Value::Array(info.diffs.clone());
|
||||
}
|
||||
self.tools.insert(call_id.to_owned(), info);
|
||||
out.updates.push((target_session, update));
|
||||
}
|
||||
"tool.execution_partial_result" | "tool.shell_output" => {
|
||||
let Some(call_id) = str_of(data, "toolCallId") else { return out };
|
||||
let chunk = str_of(data, "partialOutput")
|
||||
.or_else(|| str_of(data, "output"))
|
||||
.or_else(|| str_of(data, "chunk"))
|
||||
.unwrap_or_default();
|
||||
if chunk.is_empty() {
|
||||
return out;
|
||||
}
|
||||
if let Some(info) = self.tools.get_mut(call_id) {
|
||||
info.output.push_str(chunk);
|
||||
trim_tail(&mut info.output);
|
||||
let content = tool_content(&info.output, &info.diffs);
|
||||
out.updates.push((
|
||||
target_session,
|
||||
json!({
|
||||
"sessionUpdate": "tool_call_update",
|
||||
"toolCallId": call_id,
|
||||
"content": content,
|
||||
}),
|
||||
));
|
||||
}
|
||||
}
|
||||
"tool.execution_complete" => {
|
||||
let Some(call_id) = str_of(data, "toolCallId") else { return out };
|
||||
let success = data.get("success").and_then(Value::as_bool).unwrap_or(true);
|
||||
let result_text = extract_tool_result_text(data);
|
||||
if let Some(sub_id) = self.subagents.remove(call_id) {
|
||||
out.updates.push((
|
||||
main_session.to_owned(),
|
||||
json!({
|
||||
"sessionUpdate": "subagent_update",
|
||||
"subagentId": sub_id,
|
||||
"status": if success { "done" } else { "failed" },
|
||||
"reply": result_text,
|
||||
"error": (!success).then(|| result_text.clone()).flatten(),
|
||||
}),
|
||||
));
|
||||
return out;
|
||||
}
|
||||
let Some(mut info) = self.tools.remove(call_id) else { return out };
|
||||
if info.output.is_empty()
|
||||
&& let Some(t) = result_text
|
||||
&& info.kind != "edit"
|
||||
{
|
||||
info.output = t;
|
||||
trim_tail(&mut info.output);
|
||||
}
|
||||
let mut update = json!({
|
||||
"sessionUpdate": "tool_call_update",
|
||||
"toolCallId": call_id,
|
||||
"status": if success { "completed" } else { "failed" },
|
||||
});
|
||||
let content = tool_content(&info.output, &info.diffs);
|
||||
if !content.is_empty() {
|
||||
update["content"] = Value::Array(content);
|
||||
}
|
||||
out.updates.push((target_session, update));
|
||||
}
|
||||
"subagent.started" => {
|
||||
if let Some(call_id) = str_of(data, "toolCallId") {
|
||||
let sub_id = self
|
||||
.subagents
|
||||
.entry(call_id.to_owned())
|
||||
.or_insert_with(|| format!("{main_session}:task:{call_id}"))
|
||||
.clone();
|
||||
let title = str_of(data, "agentDisplayName").or_else(|| str_of(data, "agentName"));
|
||||
let desc = str_of(data, "agentDescription");
|
||||
out.updates.push((
|
||||
main_session.to_owned(),
|
||||
json!({
|
||||
"sessionUpdate": "subagent_update",
|
||||
"subagentId": sub_id,
|
||||
"title": title,
|
||||
"description": desc,
|
||||
"status": "running",
|
||||
}),
|
||||
));
|
||||
}
|
||||
}
|
||||
"subagent.failed" => {
|
||||
if let Some(call_id) = str_of(data, "toolCallId")
|
||||
&& let Some(sub_id) = self.subagents.get(call_id).cloned()
|
||||
{
|
||||
out.updates.push((
|
||||
main_session.to_owned(),
|
||||
json!({
|
||||
"sessionUpdate": "subagent_update",
|
||||
"subagentId": sub_id,
|
||||
"status": "failed",
|
||||
"error": str_of(data, "error").or_else(|| str_of(data, "message")),
|
||||
}),
|
||||
));
|
||||
}
|
||||
}
|
||||
"session.title_changed" => {
|
||||
if let Some(title) = str_of(data, "title").map(str::trim).filter(|t| !t.is_empty()) {
|
||||
out.updates.push((
|
||||
main_session.to_owned(),
|
||||
json!({ "sessionUpdate": "session_info_update", "title": title }),
|
||||
));
|
||||
}
|
||||
}
|
||||
"session.usage_info" => {
|
||||
let used = data.get("currentTokens").and_then(Value::as_u64).and_then(|n| u32::try_from(n).ok());
|
||||
let limit = data
|
||||
.get("tokenLimit")
|
||||
.and_then(Value::as_u64)
|
||||
.and_then(|n| u32::try_from(n).ok())
|
||||
.filter(|&n| n > 0)
|
||||
.or(fallback_limit);
|
||||
if let (Some(used), Some(size)) = (used, limit) {
|
||||
self.last_used_tokens = Some(used);
|
||||
out.updates.push((
|
||||
main_session.to_owned(),
|
||||
json!({ "sessionUpdate": "usage_update", "used": used, "size": size }),
|
||||
));
|
||||
}
|
||||
}
|
||||
"assistant.usage" => {
|
||||
if agent_id.is_none() {
|
||||
let used = data.get("inputTokens").and_then(Value::as_u64).and_then(|n| u32::try_from(n).ok());
|
||||
if let (Some(used), Some(size)) = (used, fallback_limit.filter(|&s| s > 0)) {
|
||||
self.last_used_tokens = Some(used);
|
||||
out.updates.push((
|
||||
main_session.to_owned(),
|
||||
json!({ "sessionUpdate": "usage_update", "used": used, "size": size }),
|
||||
));
|
||||
}
|
||||
}
|
||||
if let Some(limits) = quota_limits(data.get("quotaSnapshots")) {
|
||||
out.updates.push((main_session.to_owned(), limits));
|
||||
}
|
||||
}
|
||||
"session.idle" if agent_id.is_none() => {
|
||||
let mode = str_of(data, "mode").unwrap_or_default();
|
||||
if mode != "autopilot" {
|
||||
let aborted = data.get("aborted").and_then(Value::as_bool).unwrap_or(false);
|
||||
out.terminal = Some(TurnTerminal::Idle { aborted });
|
||||
}
|
||||
}
|
||||
"session.autopilot_stopped" if agent_id.is_none() => {
|
||||
let reason = str_of(data, "reason").unwrap_or_default();
|
||||
if reason == "error" {
|
||||
out.terminal = Some(TurnTerminal::Error { message: "Autopilot request failed".into(), data: None });
|
||||
} else {
|
||||
out.terminal = Some(TurnTerminal::Idle { aborted: reason == "aborted" });
|
||||
}
|
||||
}
|
||||
"session.error" if agent_id.is_none() => {
|
||||
let message = str_of(data, "message").unwrap_or("Copilot reported an error.").to_owned();
|
||||
out.terminal = Some(TurnTerminal::Error { message, data: Some(data.clone()) });
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
out
|
||||
}
|
||||
}
|
||||
|
||||
fn describe_tool(name: &str, args: &Value) -> ToolInfo {
|
||||
let mut locations = Vec::new();
|
||||
let mut diffs = Vec::new();
|
||||
if let Some(p) = str_of(args, "path").or_else(|| str_of(args, "filePath")).filter(|p| !p.is_empty()) {
|
||||
locations.push(json!({ "path": p }));
|
||||
}
|
||||
let (kind, title) = match name {
|
||||
"bash" | "powershell" | "shell" => {
|
||||
let cmd = str_of(args, "command").or_else(|| str_of(args, "description")).unwrap_or("Run command");
|
||||
("execute", cmd.to_owned())
|
||||
}
|
||||
"view" | "read_file" => {
|
||||
let label = str_of(args, "path")
|
||||
.and_then(|p| Path::new(p).file_name().and_then(|n| n.to_str()))
|
||||
.unwrap_or("Read file");
|
||||
("read", format!("Read {label}"))
|
||||
}
|
||||
"grep" | "glob" | "semantic_search" => {
|
||||
let pat = str_of(args, "pattern").or_else(|| str_of(args, "query")).unwrap_or(name);
|
||||
("search", format!("Search {pat}"))
|
||||
}
|
||||
"edit" => {
|
||||
let path = str_of(args, "path").unwrap_or_default();
|
||||
let old = str_of(args, "old_str").unwrap_or_default();
|
||||
let new = str_of(args, "new_str").unwrap_or_default();
|
||||
if !path.is_empty() {
|
||||
diffs.push(json!({ "type": "diff", "path": path, "oldText": old, "newText": new }));
|
||||
}
|
||||
let label = Path::new(path).file_name().and_then(|n| n.to_str()).unwrap_or("file");
|
||||
("edit", format!("Edit {label}"))
|
||||
}
|
||||
"create" => {
|
||||
let path = str_of(args, "path").unwrap_or_default();
|
||||
let new = str_of(args, "file_text").unwrap_or_default();
|
||||
if !path.is_empty() {
|
||||
diffs.push(json!({ "type": "diff", "path": path, "oldText": "", "newText": new }));
|
||||
}
|
||||
let label = Path::new(path).file_name().and_then(|n| n.to_str()).unwrap_or("file");
|
||||
("edit", format!("Create {label}"))
|
||||
}
|
||||
"web_fetch" => {
|
||||
let url = str_of(args, "url").unwrap_or("URL");
|
||||
("fetch", format!("Fetch {url}"))
|
||||
}
|
||||
_ => ("other", str_of(args, "description").unwrap_or(name).to_owned()),
|
||||
};
|
||||
ToolInfo { kind, title, locations, output: String::new(), diffs }
|
||||
}
|
||||
|
||||
fn extract_tool_result_text(data: &Value) -> Option<String> {
|
||||
if let Some(err) = data.pointer("/error/message").and_then(Value::as_str).filter(|s| !s.is_empty()) {
|
||||
return Some(err.to_owned());
|
||||
}
|
||||
let result = data.get("result")?;
|
||||
if let Some(s) = result.as_str() {
|
||||
return (!s.is_empty()).then(|| s.to_owned());
|
||||
}
|
||||
str_of(result, "content")
|
||||
.or_else(|| str_of(result, "detailedContent"))
|
||||
.or_else(|| str_of(result, "textResultForLlm"))
|
||||
.filter(|s| !s.is_empty())
|
||||
.map(str::to_owned)
|
||||
}
|
||||
|
||||
fn tool_content(output: &str, diffs: &[Value]) -> Vec<Value> {
|
||||
let mut out = diffs.to_vec();
|
||||
if !output.is_empty() {
|
||||
out.push(json!({ "type": "content", "content": { "type": "text", "text": output } }));
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
fn quota_limits(snapshots: Option<&Value>) -> Option<Value> {
|
||||
let map = snapshots?.as_object()?;
|
||||
let mut limits = Vec::new();
|
||||
for (window, snap) in map {
|
||||
let used_pct = snap
|
||||
.get("percentageUsed")
|
||||
.and_then(Value::as_f64)
|
||||
.or_else(|| snap.get("remainingPercentage").and_then(Value::as_f64).map(|rem| 100.0 - rem));
|
||||
let Some(used_pct) = used_pct.filter(|p| p.is_finite()) else { continue };
|
||||
let resets_ms = snap.get("resetsAt").and_then(Value::as_f64);
|
||||
let mut entry = json!({
|
||||
"window": window,
|
||||
"usedPercent": used_pct.clamp(0.0, 100.0),
|
||||
});
|
||||
if let Some(ms) = resets_ms {
|
||||
entry["resetsMs"] = json!(ms);
|
||||
}
|
||||
limits.push(entry);
|
||||
}
|
||||
(!limits.is_empty()).then(|| json!({ "sessionUpdate": LIMITS_UPDATE, "limits": limits }))
|
||||
}
|
||||
|
||||
/// Builds an ACP `session/request_permission` payload from Copilot's `permission.requested` data.
|
||||
pub(super) fn permission_request(session_id: &str, perm: &Value) -> Value {
|
||||
let kind = str_of(perm, "kind").unwrap_or("other");
|
||||
let tool_call_id = str_of(perm, "toolCallId").unwrap_or("permission");
|
||||
let (acp_kind, title, raw_input) = match kind {
|
||||
"shell" => {
|
||||
let cmd = str_of(perm, "fullCommandText").or_else(|| str_of(perm, "command")).unwrap_or_default();
|
||||
let title = str_of(perm, "intention")
|
||||
.filter(|s| !s.is_empty())
|
||||
.map(str::to_owned)
|
||||
.unwrap_or_else(|| if cmd.is_empty() { "Run shell command".into() } else { format!("Run {cmd}") });
|
||||
("execute", title, json!({ "command": cmd }))
|
||||
}
|
||||
"write" => {
|
||||
let path = str_of(perm, "fileName").or_else(|| str_of(perm, "path")).unwrap_or("file");
|
||||
("edit", format!("Edit {path}"), json!({ "path": path }))
|
||||
}
|
||||
"read" => {
|
||||
let path = str_of(perm, "path").unwrap_or("file");
|
||||
("read", format!("Read {path}"), json!({ "path": path }))
|
||||
}
|
||||
"url" => {
|
||||
let url = str_of(perm, "url").unwrap_or("URL");
|
||||
("fetch", format!("Allow network access to {url}"), json!({ "url": url }))
|
||||
}
|
||||
"mcp" => {
|
||||
let server = str_of(perm, "serverName").unwrap_or("MCP");
|
||||
let tool = str_of(perm, "toolName").unwrap_or("tool");
|
||||
("other", format!("Run {server}: {tool}"), perm.clone())
|
||||
}
|
||||
_ => {
|
||||
let t = str_of(perm, "intention").or_else(|| str_of(perm, "message")).unwrap_or("Allow action");
|
||||
("other", t.to_owned(), perm.clone())
|
||||
}
|
||||
};
|
||||
json!({
|
||||
"sessionId": session_id,
|
||||
"toolCall": {
|
||||
"toolCallId": tool_call_id,
|
||||
"kind": acp_kind,
|
||||
"title": title,
|
||||
"rawInput": raw_input,
|
||||
},
|
||||
"options": [
|
||||
{ "optionId": "approve-once", "name": "Allow once", "kind": "allow_once" },
|
||||
{ "optionId": "approved", "name": "Always allow", "kind": "allow_always" },
|
||||
{ "optionId": "denied", "name": "Reject", "kind": "reject_once" },
|
||||
],
|
||||
})
|
||||
}
|
||||
|
||||
/// Builds an ACP `session/request_permission` question card for Copilot's `userInput.request`.
|
||||
pub(super) fn user_input_permission_request(session_id: &str, req: &Value) -> Value {
|
||||
let question = str_of(req, "question").unwrap_or("Provide input");
|
||||
let choices: Vec<Value> = req
|
||||
.get("choices")
|
||||
.and_then(Value::as_array)
|
||||
.into_iter()
|
||||
.flatten()
|
||||
.filter_map(|c| c.as_str().map(|label| json!({ "label": label, "description": "" })))
|
||||
.collect();
|
||||
json!({
|
||||
"sessionId": session_id,
|
||||
"toolCall": {
|
||||
"toolCallId": str_of(req, "toolCallId").unwrap_or("ask_user"),
|
||||
"kind": "other",
|
||||
"title": question,
|
||||
"_meta": {
|
||||
"questions": [{
|
||||
"header": "Question",
|
||||
"question": question,
|
||||
"multiSelect": false,
|
||||
"options": choices,
|
||||
}]
|
||||
}
|
||||
},
|
||||
"options": [
|
||||
{ "optionId": "submit", "name": "Submit", "kind": "allow_once" },
|
||||
{ "optionId": "cancel", "name": "Cancel", "kind": "reject_once" },
|
||||
],
|
||||
})
|
||||
}
|
||||
|
||||
/// Converts ACP `mcpServers` array into Copilot's `mcpServers` object map.
|
||||
pub(super) fn wire_mcp_servers(acp_servers: Option<&Value>) -> Option<Value> {
|
||||
let list = acp_servers?.as_array()?;
|
||||
let mut out = Map::new();
|
||||
for item in list {
|
||||
let Some(name) = str_of(item, "name").filter(|n| !n.is_empty()) else { continue };
|
||||
let transport = str_of(item, "type").unwrap_or("stdio");
|
||||
match transport {
|
||||
"http" | "sse" => {
|
||||
let Some(url) = str_of(item, "url").filter(|u| !u.is_empty()) else { continue };
|
||||
let mut headers = Map::new();
|
||||
for kv in item.get("headers").and_then(Value::as_array).into_iter().flatten() {
|
||||
if let (Some(k), Some(v)) = (str_of(kv, "name"), str_of(kv, "value")) {
|
||||
headers.insert(k.to_owned(), json!(v));
|
||||
}
|
||||
}
|
||||
out.insert(
|
||||
name.to_owned(),
|
||||
json!({
|
||||
"type": transport,
|
||||
"url": url,
|
||||
"headers": headers,
|
||||
"tools": ["*"],
|
||||
}),
|
||||
);
|
||||
}
|
||||
_ => {
|
||||
let Some(cmd) = str_of(item, "command").filter(|c| !c.is_empty()) else { continue };
|
||||
let args: Vec<Value> = item.get("args").and_then(Value::as_array).cloned().unwrap_or_default();
|
||||
let mut env = Map::new();
|
||||
for kv in item.get("env").and_then(Value::as_array).into_iter().flatten() {
|
||||
if let (Some(k), Some(v)) = (str_of(kv, "name"), str_of(kv, "value")) {
|
||||
env.insert(k.to_owned(), json!(v));
|
||||
}
|
||||
}
|
||||
out.insert(
|
||||
name.to_owned(),
|
||||
json!({
|
||||
"type": "local",
|
||||
"command": cmd,
|
||||
"args": args,
|
||||
"env": env,
|
||||
"tools": ["*"],
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
(!out.is_empty()).then_some(Value::Object(out))
|
||||
}
|
||||
|
||||
/// Converts `commands.list` + `skills.discover` results into ACP `availableCommands`.
|
||||
pub(super) fn available_commands(commands_res: Option<&Value>, skills_res: Option<&Value>) -> Vec<Value> {
|
||||
let mut seen = HashSet::new();
|
||||
let mut out = Vec::new();
|
||||
for cmd in commands_res.and_then(|v| v.get("commands")).and_then(Value::as_array).into_iter().flatten() {
|
||||
let Some(name) = str_of(cmd, "name").map(|n| n.trim_start_matches('/')).filter(|n| !n.is_empty()) else {
|
||||
continue;
|
||||
};
|
||||
if !seen.insert(name.to_owned()) {
|
||||
continue;
|
||||
}
|
||||
let hint = cmd.pointer("/input/hint").and_then(Value::as_str).filter(|h| !h.is_empty());
|
||||
out.push(json!({
|
||||
"name": name,
|
||||
"description": str_of(cmd, "description").unwrap_or_default(),
|
||||
"source": "command",
|
||||
"input": hint.map(|h| json!({ "hint": h })),
|
||||
}));
|
||||
}
|
||||
for skill in skills_res.and_then(|v| v.get("skills")).and_then(Value::as_array).into_iter().flatten() {
|
||||
if skill.get("enabled").and_then(Value::as_bool) == Some(false)
|
||||
|| skill.get("userInvocable").and_then(Value::as_bool) == Some(false)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
let Some(name) = str_of(skill, "commandName")
|
||||
.or_else(|| str_of(skill, "name"))
|
||||
.map(|n| n.trim_start_matches('/'))
|
||||
.filter(|n| !n.is_empty())
|
||||
else {
|
||||
continue;
|
||||
};
|
||||
if !seen.insert(name.to_owned()) {
|
||||
continue;
|
||||
}
|
||||
let hint = str_of(skill, "argumentHint").filter(|h| !h.is_empty());
|
||||
out.push(json!({
|
||||
"name": name,
|
||||
"description": str_of(skill, "description").unwrap_or_default(),
|
||||
"source": "skill",
|
||||
"input": hint.map(|h| json!({ "hint": h })),
|
||||
}));
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
fn trim_tail(text: &mut String) {
|
||||
if text.len() > MAX_OUTPUT {
|
||||
let mut cut = text.len() - MAX_OUTPUT;
|
||||
while !text.is_char_boundary(cut) {
|
||||
cut += 1;
|
||||
}
|
||||
text.replace_range(..cut, "");
|
||||
}
|
||||
}
|
||||
|
||||
fn str_of<'a>(v: &'a Value, key: &str) -> Option<&'a str> {
|
||||
v.get(key).and_then(Value::as_str)
|
||||
}
|
||||
@@ -13,6 +13,7 @@
|
||||
|
||||
pub(crate) mod claude;
|
||||
pub(crate) mod codex;
|
||||
pub(crate) mod copilot;
|
||||
|
||||
use ide_jsonrpc::{Framing, Handler, Peer};
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
//! `<config>/agents/<id>.toml` (written by hand, or by installing one from the ACP
|
||||
//! Registry, see `registry`).
|
||||
|
||||
use crate::{AgentCommandReadOnlyReason, AgentProvider, AgentSource};
|
||||
use crate::{AcpImportOutcome, AgentCommandReadOnlyReason, AgentProvider, AgentSource, CustomAgentInput, EnvSecrets};
|
||||
use ide_core::{IdeError, IdeResult};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::Value;
|
||||
@@ -20,6 +20,8 @@ pub(crate) enum Driver {
|
||||
Codex,
|
||||
/// `claude` in stream-json mode.
|
||||
Claude,
|
||||
/// `copilot --headless --no-auto-update --stdio`.
|
||||
Copilot,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
@@ -55,10 +57,18 @@ const BUILTIN: &[(&str, &str, Driver, &[&str], &str)] = &[
|
||||
"Run `codex login` in a terminal.",
|
||||
),
|
||||
("gemini", "Gemini CLI", Driver::Acp, &["gemini", "--experimental-acp"], "Run `gemini` in a terminal and sign in."),
|
||||
("copilot", "GitHub Copilot", Driver::Acp, &["copilot", "--acp"], "Run `copilot` in a terminal and sign in."),
|
||||
(
|
||||
"copilot",
|
||||
"GitHub Copilot",
|
||||
Driver::Copilot,
|
||||
&["copilot", "--headless", "--no-auto-update", "--stdio"],
|
||||
"Run `copilot` in a terminal and sign in.",
|
||||
),
|
||||
("cursor", "Cursor", Driver::Acp, &["cursor-agent", "acp"], "Run `cursor-agent login` in a terminal."),
|
||||
("opencode", "OpenCode", Driver::Acp, &["opencode", "acp"], "Run `opencode auth login` in a terminal."),
|
||||
("qwen", "Qwen Code", Driver::Acp, &["qwen", "--experimental-acp"], "Run `qwen` in a terminal and sign in."),
|
||||
("qoder", "Qoder", Driver::Acp, &["qodercli", "--acp"], "Run `qodercli login` in a terminal."),
|
||||
("qodercn", "Qoder CN", Driver::Acp, &["qoderclicn", "--acp"], "Run `qoderclicn login` in a terminal."),
|
||||
("kimi", "Kimi CLI", Driver::Acp, &["kimi", "acp"], "Run `kimi` in a terminal and sign in."),
|
||||
("goose", "Goose", Driver::Acp, &["goose", "acp"], "Run `goose configure` in a terminal."),
|
||||
("auggie", "Auggie", Driver::Acp, &["auggie", "--acp"], "Run `auggie login` in a terminal."),
|
||||
@@ -118,10 +128,18 @@ impl ProviderSpec {
|
||||
}
|
||||
|
||||
/// Resolve once before connecting, so authentication guidance names that same install.
|
||||
pub(crate) fn resolve_for_launch(mut self, cwd: &Path) -> Self {
|
||||
pub(crate) fn resolve_for_launch(self, cwd: &Path) -> Self {
|
||||
let strip = prefer_stored_copilot_auth();
|
||||
self.resolve_for_launch_with_copilot_env(cwd, strip)
|
||||
}
|
||||
|
||||
pub(crate) fn resolve_for_launch_with_copilot_env(mut self, cwd: &Path, strip_ambient_tokens: bool) -> Self {
|
||||
if self.driver != Driver::Acp {
|
||||
self.command = crate::native::newest_install(&self.command);
|
||||
}
|
||||
if is_copilot_provider(&self.id) {
|
||||
self.env = sanitized_copilot_env(&self.env, strip_ambient_tokens);
|
||||
}
|
||||
let program = ide_jsonrpc::resolve_spawn_program(&self.command, &self.env);
|
||||
self.command =
|
||||
if program.components().count() > 1 && !program.is_absolute() { cwd.join(program) } else { program }
|
||||
@@ -131,6 +149,69 @@ impl ProviderSpec {
|
||||
}
|
||||
}
|
||||
|
||||
const AMBIENT_COPILOT_AUTH_VARS: [&str; 3] = ["COPILOT_GITHUB_TOKEN", "GH_TOKEN", "GITHUB_TOKEN"];
|
||||
static PREFER_STORED_COPILOT_AUTH: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
|
||||
|
||||
pub(crate) fn is_copilot_provider(id: &str) -> bool {
|
||||
id == "copilot" || id.starts_with("copilot-")
|
||||
}
|
||||
|
||||
pub(crate) fn prefer_stored_copilot_auth() -> bool {
|
||||
PREFER_STORED_COPILOT_AUTH.load(std::sync::atomic::Ordering::Acquire)
|
||||
}
|
||||
|
||||
pub(crate) fn mark_prefer_stored_copilot_auth() {
|
||||
PREFER_STORED_COPILOT_AUTH.store(true, std::sync::atomic::Ordering::Release);
|
||||
}
|
||||
|
||||
pub(crate) fn has_unconfigured_copilot_ambient_token(spec_env: &[(String, String)]) -> bool {
|
||||
AMBIENT_COPILOT_AUTH_VARS.iter().any(|&key| {
|
||||
!spec_env.iter().any(|(k, v)| k == key && !v.is_empty())
|
||||
&& ambient_copilot_env_value(key).is_some_and(|v| !v.is_empty())
|
||||
})
|
||||
}
|
||||
|
||||
fn ambient_copilot_env_value(key: &str) -> Option<String> {
|
||||
ide_core::process::shell_env()
|
||||
.find(|(k, _)| *k == key)
|
||||
.map(|(_, v)| v.to_string_lossy().into_owned())
|
||||
.or_else(|| ide_core::process::host_var_os(key).map(|v| v.to_string_lossy().into_owned()))
|
||||
}
|
||||
|
||||
/// Builds child environment overrides for `copilot`:
|
||||
/// - Always clears ambient classic PATs (`ghp_…`), which Copilot CLI unconditionally rejects
|
||||
/// (`"Classic PATs are not supported"`) instead of falling back to Keychain / `gh` login.
|
||||
/// - When `strip_ambient_tokens` is true, clears unconfigured ambient `GITHUB_TOKEN` / `GH_TOKEN` /
|
||||
/// `COPILOT_GITHUB_TOKEN` so Copilot uses its stored login (`~/.copilot` or `gh auth` keyring),
|
||||
/// while preserving any token explicitly configured in OpenIDEA's agent `spec_env`.
|
||||
pub(crate) fn sanitized_copilot_env(
|
||||
spec_env: &[(String, String)],
|
||||
strip_ambient_tokens: bool,
|
||||
) -> Vec<(String, String)> {
|
||||
sanitize_copilot_env_with(spec_env, strip_ambient_tokens, ambient_copilot_env_value)
|
||||
}
|
||||
|
||||
pub(crate) fn sanitize_copilot_env_with(
|
||||
spec_env: &[(String, String)],
|
||||
strip_ambient_tokens: bool,
|
||||
lookup: impl Fn(&str) -> Option<String>,
|
||||
) -> Vec<(String, String)> {
|
||||
let mut out = Vec::with_capacity(spec_env.len() + AMBIENT_COPILOT_AUTH_VARS.len());
|
||||
for key in AMBIENT_COPILOT_AUTH_VARS {
|
||||
let explicit = spec_env.iter().any(|(k, v)| k == key && !v.is_empty());
|
||||
if explicit {
|
||||
continue;
|
||||
}
|
||||
if let Some(val) = lookup(key).filter(|v| !v.is_empty())
|
||||
&& (strip_ambient_tokens || val.trim().starts_with("ghp_"))
|
||||
{
|
||||
out.push((key.to_owned(), String::new()));
|
||||
}
|
||||
}
|
||||
out.extend(spec_env.iter().cloned());
|
||||
out
|
||||
}
|
||||
|
||||
fn render_login_hint(spec: &ProviderSpec, program: &Path, on_path: Option<&Path>, windows: bool) -> String {
|
||||
let Some(builtin) = builtin_spec(&spec.id) else { return spec.login_hint.clone() };
|
||||
let prefix = format!("Run `{}", builtin.command);
|
||||
@@ -169,10 +250,13 @@ fn render_login_command<'a>(program: &Path, args: impl IntoIterator<Item = &'a s
|
||||
}
|
||||
|
||||
/// Whether `id` names a built-in agent that needs no registry install (native drivers
|
||||
/// and `npx -y` adapters). CLI built-ins (`copilot`, `gemini`, …) can be installed and
|
||||
/// updated from the ACP Registry into `<config>/agents/<id>.toml`.
|
||||
/// other than `copilot`, and `npx -y` adapters). CLI built-ins (`copilot`, `gemini`, …)
|
||||
/// can be installed and updated from the ACP Registry into `<config>/agents/<id>.toml`.
|
||||
pub(crate) fn is_builtin(id: &str) -> bool {
|
||||
BUILTIN.iter().any(|&(bid, _, driver, cmd, _)| bid == id && (driver != Driver::Acp || cmd.first() == Some(&"npx")))
|
||||
id != "copilot"
|
||||
&& BUILTIN
|
||||
.iter()
|
||||
.any(|&(bid, _, driver, cmd, _)| bid == id && (driver != Driver::Acp || cmd.first() == Some(&"npx")))
|
||||
}
|
||||
|
||||
/// Display name of a built-in agent, when `id` is one.
|
||||
@@ -229,23 +313,40 @@ pub(crate) fn describe(spec: &ProviderSpec) -> AgentProvider {
|
||||
|
||||
/// Overrides built-in `id`'s command through its agent file, preserving other
|
||||
/// customizations. `None` restores the built-in command and removes a redundant file.
|
||||
pub(crate) fn set_builtin_command(custom_dir: &Path, id: &str, command: Option<&str>) -> IdeResult<()> {
|
||||
update_builtin(custom_dir, id, |agent, builtin| agent.command = command.unwrap_or(&builtin.command).to_owned())
|
||||
pub(crate) fn set_builtin_command(
|
||||
custom_dir: &Path,
|
||||
secrets: Option<&dyn EnvSecrets>,
|
||||
id: &str,
|
||||
command: Option<&str>,
|
||||
) -> IdeResult<()> {
|
||||
update_builtin(custom_dir, secrets, id, |agent, builtin| {
|
||||
agent.command = command.unwrap_or(&builtin.command).to_owned()
|
||||
})
|
||||
}
|
||||
|
||||
/// The environment built-in `id` starts with, through its agent file (empty clears it).
|
||||
pub(crate) fn set_builtin_env(custom_dir: &Path, id: &str, env: BTreeMap<String, String>) -> IdeResult<()> {
|
||||
update_builtin(custom_dir, id, |agent, _| agent.env = env)
|
||||
pub(crate) fn set_builtin_env(
|
||||
custom_dir: &Path,
|
||||
secrets: Option<&dyn EnvSecrets>,
|
||||
id: &str,
|
||||
env: BTreeMap<String, String>,
|
||||
) -> IdeResult<()> {
|
||||
update_builtin(custom_dir, secrets, id, |agent, _| agent.env = env)
|
||||
}
|
||||
|
||||
/// Changes built-in `id`'s agent file; a file left with only the built-in's values is removed.
|
||||
fn update_builtin(custom_dir: &Path, id: &str, change: impl FnOnce(&mut CustomAgent, &ProviderSpec)) -> IdeResult<()> {
|
||||
fn update_builtin(
|
||||
custom_dir: &Path,
|
||||
secrets: Option<&dyn EnvSecrets>,
|
||||
id: &str,
|
||||
change: impl FnOnce(&mut CustomAgent, &ProviderSpec),
|
||||
) -> IdeResult<()> {
|
||||
let Some(builtin) = builtin_spec(id) else {
|
||||
return Err(IdeError::invalid(format!("{id} is not a built-in agent")));
|
||||
};
|
||||
let file = custom_dir.join(format!("{id}.toml"));
|
||||
let mut agent = if file.try_exists()? {
|
||||
CustomAgent::read(&file).map_err(IdeError::invalid)?
|
||||
CustomAgent::read_with(&file, id, secrets).map_err(IdeError::invalid)?
|
||||
} else {
|
||||
CustomAgent {
|
||||
name: Some(builtin.name.clone()),
|
||||
@@ -270,14 +371,145 @@ fn update_builtin(custom_dir: &Path, id: &str, change: impl FnOnce(&mut CustomAg
|
||||
Err(e) if e.kind() != std::io::ErrorKind::NotFound => return Err(e.into()),
|
||||
_ => {}
|
||||
}
|
||||
forget_env(secrets, id, &[]);
|
||||
return Ok(());
|
||||
}
|
||||
agent.write(&file)?;
|
||||
Ok(())
|
||||
agent.write_with(&file, id, secrets)
|
||||
}
|
||||
|
||||
/// Saves a new custom agent; refuses built-in and ACP Registry ids, taken ids
|
||||
/// and an empty command. The id is also the agent file's name, so it must be
|
||||
/// a safe file stem.
|
||||
pub(crate) fn save_custom(
|
||||
custom_dir: &Path,
|
||||
secrets: Option<&dyn EnvSecrets>,
|
||||
input: &CustomAgentInput,
|
||||
) -> IdeResult<()> {
|
||||
let id = validated_custom_id(&input.id)?;
|
||||
if builtin_spec(&id).is_some() {
|
||||
return Err(IdeError::invalid(format!("{id} is a built-in agent; change its command instead")));
|
||||
}
|
||||
if input.command.trim().is_empty() {
|
||||
return Err(IdeError::invalid("the agent command must not be empty"));
|
||||
}
|
||||
let file = custom_dir.join(format!("{id}.toml"));
|
||||
if file.try_exists()? {
|
||||
if CustomAgent::read(&file).is_ok_and(|a| a.registry.is_some()) {
|
||||
return Err(IdeError::invalid(format!(
|
||||
"{id} is installed from the ACP Registry; uninstall it there to replace it"
|
||||
)));
|
||||
}
|
||||
return Err(IdeError::invalid(format!("there is already a custom agent '{id}'")));
|
||||
}
|
||||
CustomAgent {
|
||||
name: Some(input.name.clone()),
|
||||
driver: Driver::Acp,
|
||||
command: input.command.clone(),
|
||||
args: input.args.clone(),
|
||||
env: input.env.clone(),
|
||||
registry: None,
|
||||
}
|
||||
.write_with(&file, &id, secrets)
|
||||
}
|
||||
|
||||
/// Removes a custom agent's file; refuses built-in, ACP Registry and missing ids.
|
||||
pub(crate) fn remove_custom(custom_dir: &Path, secrets: Option<&dyn EnvSecrets>, id: &str) -> IdeResult<()> {
|
||||
let id = validated_custom_id(id)?;
|
||||
let id = id.as_str();
|
||||
if builtin_spec(id).is_some() {
|
||||
return Err(IdeError::invalid(format!("{id} is a built-in agent and cannot be removed")));
|
||||
}
|
||||
let file = custom_dir.join(format!("{id}.toml"));
|
||||
let agent =
|
||||
CustomAgent::read(&file).map_err(|_| IdeError::not_found(format!("there is no custom agent '{id}'")))?;
|
||||
if agent.registry.is_some() {
|
||||
return Err(IdeError::invalid(format!("{id} is installed from the ACP Registry; uninstall it there")));
|
||||
}
|
||||
match std::fs::remove_file(&file) {
|
||||
Err(e) if e.kind() != std::io::ErrorKind::NotFound => Err(e.into()),
|
||||
_ => {
|
||||
forget_env(secrets, id, &[]);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Imports a JetBrains `acp.json` file's `agent_servers` entries as custom
|
||||
/// agents, one outcome per entry; entries that collide, lack a command or
|
||||
/// have no usable name are skipped with a reason.
|
||||
pub(crate) fn import_acp(
|
||||
custom_dir: &Path,
|
||||
secrets: Option<&dyn EnvSecrets>,
|
||||
text: &str,
|
||||
) -> IdeResult<Vec<AcpImportOutcome>> {
|
||||
#[derive(Deserialize)]
|
||||
struct AcpJson {
|
||||
#[serde(default)]
|
||||
agent_servers: BTreeMap<String, AcpServer>,
|
||||
}
|
||||
#[derive(Deserialize)]
|
||||
struct AcpServer {
|
||||
#[serde(default)]
|
||||
command: String,
|
||||
#[serde(default)]
|
||||
args: Vec<String>,
|
||||
#[serde(default)]
|
||||
env: BTreeMap<String, String>,
|
||||
}
|
||||
let parsed: AcpJson =
|
||||
serde_json::from_str(text).map_err(|e| IdeError::invalid(format!("not an acp.json file: {e}")))?;
|
||||
let mut outcomes = Vec::with_capacity(parsed.agent_servers.len());
|
||||
for (name, server) in parsed.agent_servers {
|
||||
let id = slugify_id(&name);
|
||||
let mut outcome = AcpImportOutcome { name, id: id.clone(), saved: false, reason: String::new() };
|
||||
if id.is_empty() {
|
||||
outcome.reason = "the name has no usable characters for an id".to_owned();
|
||||
} else if builtin_spec(&id).is_some() {
|
||||
outcome.reason = format!("{id} is a built-in agent");
|
||||
} else if server.command.trim().is_empty() {
|
||||
outcome.reason = "the entry has no command".to_owned();
|
||||
} else if custom_dir.join(format!("{id}.toml")).try_exists()? {
|
||||
outcome.reason = format!("there is already a custom agent '{id}'");
|
||||
} else {
|
||||
CustomAgent {
|
||||
name: Some(outcome.name.clone()),
|
||||
driver: Driver::Acp,
|
||||
command: server.command,
|
||||
args: server.args,
|
||||
env: server.env,
|
||||
registry: None,
|
||||
}
|
||||
.write_with(&custom_dir.join(format!("{id}.toml")), &id, secrets)?;
|
||||
outcome.saved = true;
|
||||
}
|
||||
outcomes.push(outcome);
|
||||
}
|
||||
Ok(outcomes)
|
||||
}
|
||||
|
||||
/// A custom agent id doubles as a file stem: letters, digits, `-` and `_` only.
|
||||
fn validated_custom_id(id: &str) -> IdeResult<String> {
|
||||
if !id.is_empty() && id.chars().all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '_') {
|
||||
return Ok(id.to_owned());
|
||||
}
|
||||
Err(IdeError::invalid(format!("'{id}' is not a usable agent id (letters, digits, '-' and '_' only)")))
|
||||
}
|
||||
|
||||
/// Turns an `acp.json` server name into a file-stem id (`"My Agent!"` → `"my-agent"`).
|
||||
fn slugify_id(name: &str) -> String {
|
||||
let mut id = String::with_capacity(name.len());
|
||||
for c in name.chars() {
|
||||
if c.is_ascii_alphanumeric() {
|
||||
id.push(c.to_ascii_lowercase());
|
||||
} else if !id.ends_with('-') {
|
||||
id.push('-');
|
||||
}
|
||||
}
|
||||
id.trim_matches('-').to_owned()
|
||||
}
|
||||
|
||||
/// An agent file, `<config>/agents/<id>.toml`.
|
||||
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub(crate) struct CustomAgent {
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
@@ -324,11 +556,67 @@ impl CustomAgent {
|
||||
std::fs::write(&tmp, text)?;
|
||||
std::fs::rename(&tmp, path)
|
||||
}
|
||||
|
||||
/// Reads agent `id`'s file with its environment values filled in from `secrets`; values
|
||||
/// still written in the file (by hand, by an older version) move there first. A registry
|
||||
/// install keeps the environment its registry entry gives it in the file.
|
||||
pub(crate) fn read_with(path: &Path, id: &str, secrets: Option<&dyn EnvSecrets>) -> Result<Self, String> {
|
||||
let mut agent = Self::read(path)?;
|
||||
let Some(secrets) = secrets.filter(|_| agent.registry.is_none()) else {
|
||||
return Ok(agent);
|
||||
};
|
||||
let written = agent.env.values().any(|v| !v.is_empty());
|
||||
for (name, value) in agent.env.iter_mut() {
|
||||
if value.is_empty()
|
||||
&& let Some(saved) = secrets.get(&env_key(id, name))
|
||||
{
|
||||
*value = saved;
|
||||
}
|
||||
}
|
||||
if written && let Err(e) = agent.write_with(path, id, Some(secrets)) {
|
||||
tracing::warn!("agent {id}: could not move environment values out of {}: {}", path.display(), e.message);
|
||||
}
|
||||
Ok(agent)
|
||||
}
|
||||
|
||||
/// Writes agent `id`'s file with the environment's names only, its values saved in `secrets`.
|
||||
pub(crate) fn write_with(&self, path: &Path, id: &str, secrets: Option<&dyn EnvSecrets>) -> IdeResult<()> {
|
||||
let Some(secrets) = secrets.filter(|_| self.registry.is_none()) else {
|
||||
return Ok(self.write(path)?);
|
||||
};
|
||||
let mut keys = Vec::with_capacity(self.env.len());
|
||||
for (name, value) in &self.env {
|
||||
let key = env_key(id, name);
|
||||
if value.is_empty() {
|
||||
secrets.delete(&key);
|
||||
} else if secrets.get(&key).as_deref() != Some(value.as_str()) {
|
||||
secrets.set(&key, value)?;
|
||||
}
|
||||
keys.push(key);
|
||||
}
|
||||
forget_env(Some(secrets), id, &keys);
|
||||
let names = self.env.keys().map(|name| (name.clone(), String::new())).collect();
|
||||
Ok(Self { env: names, ..self.clone() }.write(path)?)
|
||||
}
|
||||
}
|
||||
|
||||
fn env_key(id: &str, name: &str) -> String {
|
||||
format!("agent/{id}/env/{}", name.replace('%', "%25").replace('/', "%2F"))
|
||||
}
|
||||
|
||||
/// Forgets agent `id`'s saved environment values, except `keep`.
|
||||
fn forget_env(secrets: Option<&dyn EnvSecrets>, id: &str, keep: &[String]) {
|
||||
let Some(secrets) = secrets else { return };
|
||||
for key in secrets.keys(&format!("agent/{id}/env/")) {
|
||||
if !keep.contains(&key) {
|
||||
secrets.delete(&key);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Built-in agents, then the `*.toml` agents of `custom_dir`; a custom agent with a
|
||||
/// built-in id replaces it.
|
||||
pub(crate) fn load(custom_dir: Option<&Path>) -> Vec<ProviderSpec> {
|
||||
pub(crate) fn load(custom_dir: Option<&Path>, secrets: Option<&dyn EnvSecrets>) -> Vec<ProviderSpec> {
|
||||
let mut specs = builtins();
|
||||
let mut files: Vec<PathBuf> = custom_dir
|
||||
.and_then(|d| std::fs::read_dir(d).ok())
|
||||
@@ -341,7 +629,11 @@ pub(crate) fn load(custom_dir: Option<&Path>) -> Vec<ProviderSpec> {
|
||||
files.sort();
|
||||
for path in files {
|
||||
let Some(id) = path.file_stem().and_then(|s| s.to_str()).map(str::to_owned) else { continue };
|
||||
let agent = match CustomAgent::read(&path) {
|
||||
let explicit_driver = std::fs::read_to_string(&path)
|
||||
.ok()
|
||||
.and_then(|t| toml::from_str::<toml::Table>(&t).ok())
|
||||
.is_some_and(|t| t.contains_key("driver"));
|
||||
let agent = match CustomAgent::read_with(&path, &id, secrets) {
|
||||
Ok(a) => a,
|
||||
Err(e) => {
|
||||
tracing::warn!("skipping agent {}: {e}", path.display());
|
||||
@@ -366,6 +658,14 @@ pub(crate) fn load(custom_dir: Option<&Path>) -> Vec<ProviderSpec> {
|
||||
match specs.iter_mut().find(|s| s.id == spec.id) {
|
||||
Some(existing) => {
|
||||
spec.login_hint = existing.login_hint.clone();
|
||||
if !explicit_driver && existing.driver == Driver::Copilot && spec.driver == Driver::Acp {
|
||||
// Registry installs (`github-copilot-cli`) record `args = ["--acp"]` with no
|
||||
// `driver`; upgrade them in place to the native headless driver (which
|
||||
// automatically falls back to `--acp`) while keeping the installed executable
|
||||
// path and registry version tracking. Explicit `driver = "acp"` stays ACP.
|
||||
spec.driver = Driver::Copilot;
|
||||
spec.args = crate::native::copilot::headless_args(&spec.args);
|
||||
}
|
||||
if spec.source == AgentSource::Custom {
|
||||
spec.source = AgentSource::Builtin;
|
||||
}
|
||||
@@ -409,6 +709,45 @@ mod tests {
|
||||
use super::*;
|
||||
use serde_json::json;
|
||||
|
||||
#[test]
|
||||
fn qoder_global_and_cn_editions_are_both_built_in() {
|
||||
let info = |id: &str| describe(&load(None, None).into_iter().find(|s| s.id == id).unwrap());
|
||||
let (global, cn) = (info("qoder"), info("qodercn"));
|
||||
assert_eq!((global.name.as_str(), global.command.as_str()), ("Qoder", "qodercli --acp"));
|
||||
assert_eq!((cn.name.as_str(), cn.command.as_str()), ("Qoder CN", "qoderclicn --acp"));
|
||||
assert_eq!(global.login_hint.as_deref(), Some("Run `qodercli login` in a terminal."));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn qodercn_is_available_without_a_file_and_preserves_existing_customizations() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let qodercn = load(None, None).into_iter().find(|s| s.id == "qodercn").unwrap();
|
||||
assert_eq!(qodercn.driver, Driver::Acp);
|
||||
assert!(!is_unlisted(&qodercn));
|
||||
let info = describe(&qodercn);
|
||||
assert_eq!(info.name, "Qoder CN");
|
||||
assert_eq!(info.command, "qoderclicn --acp");
|
||||
assert_eq!(info.source, AgentSource::Builtin);
|
||||
assert!(info.command_editable);
|
||||
assert_eq!(info.login_hint.as_deref(), Some("Run `qoderclicn login` in a terminal."));
|
||||
|
||||
std::fs::write(
|
||||
dir.path().join("qodercn.toml"),
|
||||
"command = \"/custom/qoderclicn\"\nargs = [\"--acp\"]\n[env]\nK = \"v\"\n",
|
||||
)
|
||||
.unwrap();
|
||||
let specs = load(Some(dir.path()), None);
|
||||
assert_eq!(specs.iter().filter(|s| s.id == "qodercn").count(), 1);
|
||||
let custom = specs.iter().find(|s| s.id == "qodercn").unwrap();
|
||||
assert_eq!(custom.command, "/custom/qoderclicn");
|
||||
assert_eq!(custom.env, [("K".into(), "v".into())]);
|
||||
set_builtin_command(dir.path(), None, "qodercn", None).unwrap();
|
||||
let reset = load(Some(dir.path()), None).into_iter().find(|s| s.id == "qodercn").unwrap();
|
||||
assert_eq!(reset.command, "qoderclicn");
|
||||
assert_eq!(reset.args, ["--acp"]);
|
||||
assert_eq!(reset.env, custom.env);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn login_hints_cover_all_builtin_commands_and_preserve_separate_login_tools() {
|
||||
for spec in builtins() {
|
||||
@@ -518,7 +857,7 @@ mod tests {
|
||||
#[test]
|
||||
fn launch_metadata_distinguishes_defaults_overrides_and_read_only_agents() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let list = || load(Some(dir.path())).iter().map(describe).collect::<Vec<_>>();
|
||||
let list = || load(Some(dir.path()), None).iter().map(describe).collect::<Vec<_>>();
|
||||
let defaults = list();
|
||||
let codex = defaults.iter().find(|p| p.id == "codex").unwrap();
|
||||
assert_eq!(codex.program, "codex");
|
||||
@@ -538,14 +877,14 @@ mod tests {
|
||||
p.id.ends_with("-acp").then_some(AgentCommandReadOnlyReason::PackageRunner)
|
||||
);
|
||||
}
|
||||
set_builtin_command(dir.path(), "codex", Some("/path with spaces/codex")).unwrap();
|
||||
set_builtin_command(dir.path(), None, "codex", Some("/path with spaces/codex")).unwrap();
|
||||
let overridden = list().into_iter().find(|p| p.id == "codex").unwrap();
|
||||
assert_eq!(overridden.program, "/path with spaces/codex");
|
||||
assert_eq!(overridden.args, codex.args);
|
||||
assert_eq!(overridden.default_program, codex.default_program);
|
||||
assert!(overridden.command_overridden && overridden.command_editable);
|
||||
assert_eq!(overridden.source, AgentSource::Builtin);
|
||||
set_builtin_command(dir.path(), "codex", Some("codex")).unwrap();
|
||||
set_builtin_command(dir.path(), None, "codex", Some("codex")).unwrap();
|
||||
assert!(!list().iter().find(|p| p.id == "codex").unwrap().command_overridden);
|
||||
std::fs::write(
|
||||
dir.path().join("gemini.toml"),
|
||||
@@ -570,7 +909,7 @@ mod tests {
|
||||
write("mine.toml", "name = \"Mine\"\ncommand = \"/opt/mine\"\nargs = [\"acp\"]\n[env]\nK = \"v\"\n");
|
||||
write("gemini.toml", "command = \"gemini\"\nargs = [\"--acp\"]\n");
|
||||
write("broken.toml", "nope = 1\n");
|
||||
let specs = load(Some(dir.path()));
|
||||
let specs = load(Some(dir.path()), None);
|
||||
let mine = specs.iter().find(|s| s.id == "mine").unwrap();
|
||||
assert_eq!((mine.name.as_str(), mine.command.as_str()), ("Mine", "/opt/mine"));
|
||||
assert_eq!(mine.env, vec![("K".to_owned(), "v".to_owned())]);
|
||||
@@ -586,34 +925,35 @@ mod tests {
|
||||
fn set_builtin_env_writes_and_clears_the_environment() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let env = BTreeMap::from([("CLAUDE_CODE_ENTRYPOINT".to_owned(), "openidea".to_owned())]);
|
||||
set_builtin_env(dir.path(), "claude", env.clone()).unwrap();
|
||||
let claude = load(Some(dir.path())).into_iter().find(|s| s.id == "claude").unwrap();
|
||||
set_builtin_env(dir.path(), None, "claude", env.clone()).unwrap();
|
||||
let claude = load(Some(dir.path()), None).into_iter().find(|s| s.id == "claude").unwrap();
|
||||
assert_eq!(claude.env, vec![("CLAUDE_CODE_ENTRYPOINT".to_owned(), "openidea".to_owned())]);
|
||||
assert_eq!(describe(&claude).env, env);
|
||||
// The command override and the environment are kept independently.
|
||||
set_builtin_command(dir.path(), "claude", Some("/opt/claude")).unwrap();
|
||||
set_builtin_command(dir.path(), "claude", None).unwrap();
|
||||
set_builtin_command(dir.path(), None, "claude", Some("/opt/claude")).unwrap();
|
||||
set_builtin_command(dir.path(), None, "claude", None).unwrap();
|
||||
assert!(dir.path().join("claude.toml").is_file());
|
||||
set_builtin_env(dir.path(), "claude", BTreeMap::new()).unwrap();
|
||||
set_builtin_env(dir.path(), None, "claude", BTreeMap::new()).unwrap();
|
||||
assert!(!dir.path().join("claude.toml").exists());
|
||||
set_builtin_env(dir.path(), "not-a-builtin", env).unwrap_err();
|
||||
set_builtin_env(dir.path(), None, "not-a-builtin", env).unwrap_err();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn acp_adapters_of_native_agents_are_unlisted_until_customized() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let unlisted = |dir: &Path| load(Some(dir)).into_iter().filter(is_unlisted).map(|s| s.id).collect::<Vec<_>>();
|
||||
let unlisted =
|
||||
|dir: &Path| load(Some(dir), None).into_iter().filter(is_unlisted).map(|s| s.id).collect::<Vec<_>>();
|
||||
assert_eq!(unlisted(dir.path()), ["claude-acp", "codex-acp"]);
|
||||
set_builtin_env(dir.path(), "codex-acp", BTreeMap::from([("A".to_owned(), "1".to_owned())])).unwrap();
|
||||
set_builtin_env(dir.path(), None, "codex-acp", BTreeMap::from([("A".to_owned(), "1".to_owned())])).unwrap();
|
||||
assert_eq!(unlisted(dir.path()), ["claude-acp"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn set_builtin_command_overrides_and_clears_a_builtin() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
set_builtin_command(dir.path(), "not-a-builtin", Some("/x")).unwrap_err();
|
||||
set_builtin_command(dir.path(), "claude", Some("/opt/claude")).unwrap();
|
||||
let claude = load(Some(dir.path())).into_iter().find(|s| s.id == "claude").unwrap();
|
||||
set_builtin_command(dir.path(), None, "not-a-builtin", Some("/x")).unwrap_err();
|
||||
set_builtin_command(dir.path(), None, "claude", Some("/opt/claude")).unwrap();
|
||||
let claude = load(Some(dir.path()), None).into_iter().find(|s| s.id == "claude").unwrap();
|
||||
assert_eq!((claude.command.as_str(), claude.driver), ("/opt/claude", Driver::Claude));
|
||||
assert!(claude.args.is_empty());
|
||||
// A registry install is neither overwritten nor removed by a command override.
|
||||
@@ -628,14 +968,14 @@ mod tests {
|
||||
}
|
||||
.write(&path)
|
||||
.unwrap();
|
||||
set_builtin_command(dir.path(), "codex", Some("/x")).unwrap_err();
|
||||
set_builtin_command(dir.path(), "codex", None).unwrap_err();
|
||||
set_builtin_command(dir.path(), None, "codex", Some("/x")).unwrap_err();
|
||||
set_builtin_command(dir.path(), None, "codex", None).unwrap_err();
|
||||
assert!(path.exists());
|
||||
// Clearing (twice) returns the built-in to auto-detection.
|
||||
set_builtin_command(dir.path(), "claude", None).unwrap();
|
||||
set_builtin_command(dir.path(), "claude", None).unwrap();
|
||||
set_builtin_command(dir.path(), None, "claude", None).unwrap();
|
||||
set_builtin_command(dir.path(), None, "claude", None).unwrap();
|
||||
assert!(!dir.path().join("claude.toml").exists());
|
||||
assert_eq!(load(Some(dir.path())).iter().find(|s| s.id == "claude").unwrap().command, "claude");
|
||||
assert_eq!(load(Some(dir.path()), None).iter().find(|s| s.id == "claude").unwrap().command, "claude");
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -663,11 +1003,11 @@ mod tests {
|
||||
}
|
||||
agent.write(&path).unwrap();
|
||||
|
||||
set_builtin_command(dir.path(), "claude", Some("/new/claude")).unwrap();
|
||||
set_builtin_command(dir.path(), None, "claude", Some("/new/claude")).unwrap();
|
||||
agent.command = "/new/claude".into();
|
||||
assert_eq!(CustomAgent::read(&path).unwrap(), agent, "{field}");
|
||||
|
||||
set_builtin_command(dir.path(), "claude", None).unwrap();
|
||||
set_builtin_command(dir.path(), None, "claude", None).unwrap();
|
||||
agent.command = "claude".into();
|
||||
assert_eq!(CustomAgent::read(&path).unwrap(), agent, "{field}");
|
||||
}
|
||||
@@ -683,11 +1023,11 @@ mod tests {
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
set_builtin_command(dir.path(), "codex", None).unwrap();
|
||||
set_builtin_command(dir.path(), None, "codex", None).unwrap();
|
||||
|
||||
assert!(!path.exists());
|
||||
assert_eq!(
|
||||
load(Some(dir.path())).into_iter().find(|s| s.id == "codex").unwrap(),
|
||||
load(Some(dir.path()), None).into_iter().find(|s| s.id == "codex").unwrap(),
|
||||
builtin_spec("codex").unwrap()
|
||||
);
|
||||
}
|
||||
@@ -708,8 +1048,170 @@ mod tests {
|
||||
let text = std::fs::read_to_string(&path).unwrap();
|
||||
assert!(text.contains("[registry]") && !text.contains("driver"), "{text}");
|
||||
assert_eq!(CustomAgent::read(&path).unwrap(), agent);
|
||||
let spec = load(Some(path.parent().unwrap())).into_iter().find(|s| s.id == "amp-acp").unwrap();
|
||||
let spec = load(Some(path.parent().unwrap()), None).into_iter().find(|s| s.id == "amp-acp").unwrap();
|
||||
assert_eq!(spec.source, AgentSource::Registry { version: "0.10.0".into(), update: None });
|
||||
assert!(toml::from_str::<CustomAgent>("command = \"x\"\n[registry]\nid = \"x\"\n").is_err());
|
||||
}
|
||||
|
||||
fn custom_input(id: &str) -> CustomAgentInput {
|
||||
CustomAgentInput {
|
||||
id: id.into(),
|
||||
name: "Mine".into(),
|
||||
command: "/opt/mine".into(),
|
||||
args: vec!["acp".into()],
|
||||
env: BTreeMap::from([("K".to_owned(), "v".to_owned())]),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn custom_agents_save_list_and_remove() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
save_custom(dir.path(), None, &custom_input("mine")).unwrap();
|
||||
let mine = load(Some(dir.path()), None).into_iter().find(|s| s.id == "mine").unwrap();
|
||||
assert_eq!((mine.name.as_str(), mine.command.as_str()), ("Mine", "/opt/mine"));
|
||||
assert_eq!(mine.env, vec![("K".to_owned(), "v".to_owned())]);
|
||||
assert_eq!(mine.source, AgentSource::Custom);
|
||||
remove_custom(dir.path(), None, "mine").unwrap();
|
||||
assert!(!load(Some(dir.path()), None).iter().any(|s| s.id == "mine"));
|
||||
remove_custom(dir.path(), None, "mine").unwrap_err();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn custom_agents_refuse_builtin_registry_and_bad_ids() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
save_custom(dir.path(), None, &custom_input("claude")).unwrap_err();
|
||||
save_custom(dir.path(), None, &custom_input("../evil")).unwrap_err();
|
||||
save_custom(dir.path(), None, &custom_input("")).unwrap_err();
|
||||
let mut empty_command = custom_input("mine");
|
||||
empty_command.command = " ".into();
|
||||
save_custom(dir.path(), None, &empty_command).unwrap_err();
|
||||
save_custom(dir.path(), None, &custom_input("mine")).unwrap();
|
||||
save_custom(dir.path(), None, &custom_input("mine")).unwrap_err();
|
||||
remove_custom(dir.path(), None, "claude").unwrap_err();
|
||||
// An id that isn't a plain file stem never reaches outside the agents directory.
|
||||
let outside = dir.path().join("outside.toml");
|
||||
std::fs::write(&outside, "command = \"/bin/sh\"\n").unwrap();
|
||||
let agents = dir.path().join("agents");
|
||||
std::fs::create_dir(&agents).unwrap();
|
||||
remove_custom(&agents, None, "../outside").unwrap_err();
|
||||
assert!(outside.exists());
|
||||
std::fs::write(
|
||||
dir.path().join("amp-acp.toml"),
|
||||
"command = \"/registry/amp\"\n[registry]\nid = \"amp-acp\"\nversion = \"1.0\"\n",
|
||||
)
|
||||
.unwrap();
|
||||
save_custom(dir.path(), None, &custom_input("amp-acp")).unwrap_err();
|
||||
remove_custom(dir.path(), None, "amp-acp").unwrap_err();
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct MemorySecrets(parking_lot::Mutex<BTreeMap<String, String>>);
|
||||
|
||||
impl EnvSecrets for MemorySecrets {
|
||||
fn get(&self, key: &str) -> Option<String> {
|
||||
self.0.lock().get(key).cloned()
|
||||
}
|
||||
fn set(&self, key: &str, value: &str) -> IdeResult<()> {
|
||||
self.0.lock().insert(key.to_owned(), value.to_owned());
|
||||
Ok(())
|
||||
}
|
||||
fn delete(&self, key: &str) {
|
||||
self.0.lock().remove(key);
|
||||
}
|
||||
fn keys(&self, prefix: &str) -> Vec<String> {
|
||||
self.0.lock().keys().filter(|k| k.starts_with(prefix)).cloned().collect()
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn environment_values_go_to_the_secrets_and_the_files_keep_the_names() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let secrets = MemorySecrets::default();
|
||||
let s = Some(&secrets as &dyn EnvSecrets);
|
||||
let env = BTreeMap::from([("API_KEY".to_owned(), "v-91f".to_owned()), ("A/B".to_owned(), String::new())]);
|
||||
set_builtin_env(dir.path(), s, "claude", env.clone()).unwrap();
|
||||
save_custom(dir.path(), s, &CustomAgentInput { env: env.clone(), ..custom_input("mine") }).unwrap();
|
||||
for id in ["claude", "mine"] {
|
||||
let text = std::fs::read_to_string(dir.path().join(format!("{id}.toml"))).unwrap();
|
||||
assert!(text.contains("API_KEY") && !text.contains("v-91f"), "{text}");
|
||||
let spec = load(Some(dir.path()), s).into_iter().find(|p| p.id == id).unwrap();
|
||||
assert_eq!(describe(&spec).env, env);
|
||||
}
|
||||
assert_eq!(secrets.keys("agent/"), ["agent/claude/env/API_KEY", "agent/mine/env/API_KEY"]);
|
||||
|
||||
set_builtin_command(dir.path(), s, "claude", Some("/opt/claude")).unwrap();
|
||||
assert_eq!(secrets.get("agent/claude/env/API_KEY").as_deref(), Some("v-91f"));
|
||||
set_builtin_env(dir.path(), s, "claude", BTreeMap::from([("OTHER".to_owned(), "x".to_owned())])).unwrap();
|
||||
assert_eq!(secrets.keys("agent/claude/"), ["agent/claude/env/OTHER"]);
|
||||
set_builtin_command(dir.path(), s, "claude", None).unwrap();
|
||||
set_builtin_env(dir.path(), s, "claude", BTreeMap::new()).unwrap();
|
||||
remove_custom(dir.path(), s, "mine").unwrap();
|
||||
assert_eq!(secrets.keys("agent/"), Vec::<String>::new());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn values_written_in_agent_files_move_to_the_secrets_unless_installed_from_the_registry() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let secrets = MemorySecrets::default();
|
||||
let s = Some(&secrets as &dyn EnvSecrets);
|
||||
std::fs::write(dir.path().join("mine.toml"), "command = \"/opt/mine\"\n[env]\nK = \"v-91f\"\nS = \"\"\n")
|
||||
.unwrap();
|
||||
secrets.set("agent/mine/env/S", "saved").unwrap();
|
||||
let registry =
|
||||
"command = \"/registry/amp\"\n[env]\nR = \"1\"\n[registry]\nid = \"amp-acp\"\nversion = \"1.0\"\n";
|
||||
std::fs::write(dir.path().join("amp-acp.toml"), registry).unwrap();
|
||||
let specs = load(Some(dir.path()), s);
|
||||
assert_eq!(
|
||||
specs.iter().find(|p| p.id == "mine").unwrap().env,
|
||||
[("K".to_owned(), "v-91f".to_owned()), ("S".to_owned(), "saved".to_owned())]
|
||||
);
|
||||
assert!(!std::fs::read_to_string(dir.path().join("mine.toml")).unwrap().contains("v-91f"));
|
||||
assert_eq!(secrets.keys("agent/"), ["agent/mine/env/K", "agent/mine/env/S"]);
|
||||
assert_eq!(std::fs::read_to_string(dir.path().join("amp-acp.toml")).unwrap(), registry);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn acp_json_imports_servers_and_skips_collisions() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
save_custom(dir.path(), None, &custom_input("taken")).unwrap();
|
||||
let outcomes = import_acp(
|
||||
dir.path(),
|
||||
None,
|
||||
r#"{"agent_servers": {
|
||||
"My Agent!": {"command": "/opt/agent", "args": ["--acp"], "env": {"K": "v"}},
|
||||
"taken": {"command": "/opt/taken"},
|
||||
"claude": {"command": "/opt/claude"},
|
||||
"!!!": {"command": "/opt/noname"},
|
||||
"silent": {"args": []}
|
||||
}}"#,
|
||||
)
|
||||
.unwrap();
|
||||
let saved: Vec<&str> = outcomes.iter().filter(|o| o.saved).map(|o| o.id.as_str()).collect();
|
||||
assert_eq!(saved, ["my-agent"]);
|
||||
assert!(outcomes.iter().filter(|o| !o.saved).all(|o| !o.reason.is_empty()));
|
||||
let mine = load(Some(dir.path()), None).into_iter().find(|s| s.id == "my-agent").unwrap();
|
||||
assert_eq!((mine.name.as_str(), mine.command.as_str()), ("My Agent!", "/opt/agent"));
|
||||
assert_eq!(mine.source, AgentSource::Custom);
|
||||
import_acp(dir.path(), None, "not json").unwrap_err();
|
||||
assert!(import_acp(dir.path(), None, "{}").unwrap().is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sanitizes_classic_pats_and_ambient_tokens_while_preserving_explicit_agent_env() {
|
||||
let lookup = |k: &str| match k {
|
||||
"GITHUB_TOKEN" => Some("ghp_classic_pat_123".into()),
|
||||
"GH_TOKEN" => Some("gho_oauth_token_456".into()),
|
||||
_ => None,
|
||||
};
|
||||
assert_eq!(sanitize_copilot_env_with(&[], false, lookup), vec![("GITHUB_TOKEN".into(), String::new())]);
|
||||
assert_eq!(
|
||||
sanitize_copilot_env_with(&[], true, lookup),
|
||||
vec![("GH_TOKEN".into(), String::new()), ("GITHUB_TOKEN".into(), String::new())]
|
||||
);
|
||||
let explicit = vec![("GH_TOKEN".into(), "github_pat_custom".into())];
|
||||
assert_eq!(
|
||||
sanitize_copilot_env_with(&explicit, true, lookup),
|
||||
vec![("GITHUB_TOKEN".into(), String::new()), ("GH_TOKEN".into(), "github_pat_custom".into())]
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -826,7 +826,7 @@ mod tests {
|
||||
assert!(!shown.iter().any(|a| a.id == "cline"));
|
||||
|
||||
write_atomic(&p.cache_file, INDEX.as_bytes()).unwrap();
|
||||
let mut providers: Vec<AgentProvider> = provider::load(Some(&p.agents_dir))
|
||||
let mut providers: Vec<AgentProvider> = provider::load(Some(&p.agents_dir), None)
|
||||
.into_iter()
|
||||
.filter(|s| s.id == "amp-acp")
|
||||
.map(|s| provider::describe(&s))
|
||||
@@ -918,7 +918,7 @@ mod tests {
|
||||
assert_eq!(agent.command, v1.to_string_lossy());
|
||||
assert_eq!((agent.args.as_slice(), agent.env["A"].as_str()), (&["acp".to_owned()][..], "1"));
|
||||
assert_eq!(agent.registry, Some(RegistryRef { id: "demo".into(), version: "1.0.0".into() }));
|
||||
let spec = provider::load(Some(&p.agents_dir)).into_iter().find(|s| s.id == "demo").unwrap();
|
||||
let spec = provider::load(Some(&p.agents_dir), None).into_iter().find(|s| s.id == "demo").unwrap();
|
||||
assert_eq!(spec.source, AgentSource::Registry { version: "1.0.0".into(), update: None });
|
||||
assert!(provider::installed(&spec.command));
|
||||
|
||||
@@ -991,7 +991,7 @@ mod tests {
|
||||
assert_eq!((shown[0].installed.as_deref(), shown[0].update), (Some("1.0.104"), true));
|
||||
|
||||
write_atomic(&p.cache_file, index.as_bytes()).unwrap();
|
||||
let spec = provider::load(Some(&p.agents_dir)).into_iter().find(|s| s.id == "copilot").unwrap();
|
||||
let spec = provider::load(Some(&p.agents_dir), None).into_iter().find(|s| s.id == "copilot").unwrap();
|
||||
assert_eq!(spec.name, "GitHub Copilot");
|
||||
assert_eq!(spec.login_hint, "Run `copilot` in a terminal and sign in.");
|
||||
assert_eq!(spec.source, AgentSource::Registry { version: "1.0.104".into(), update: None });
|
||||
@@ -1004,7 +1004,7 @@ mod tests {
|
||||
);
|
||||
|
||||
remove(&p, "github-copilot-cli").unwrap();
|
||||
let fallback = provider::load(Some(&p.agents_dir)).into_iter().find(|s| s.id == "copilot").unwrap();
|
||||
let fallback = provider::load(Some(&p.agents_dir), None).into_iter().find(|s| s.id == "copilot").unwrap();
|
||||
assert_eq!(fallback.source, AgentSource::Builtin);
|
||||
}
|
||||
}
|
||||
@@ -24,6 +24,8 @@ fn paths(dir: &Path) -> AgentPaths {
|
||||
models_file: dir.join("models.json"),
|
||||
custom_agents_dir: None,
|
||||
home_dir: None,
|
||||
env_secrets: None,
|
||||
catalogs: Arc::default(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -62,6 +64,7 @@ async fn create(mgr: &AgentManager, provider: &str, prompt: &str) -> String {
|
||||
config: config(provider),
|
||||
isolation: Isolation::Local,
|
||||
base: None,
|
||||
..Default::default()
|
||||
};
|
||||
mgr.create(req).await.unwrap().id
|
||||
}
|
||||
|
||||
@@ -0,0 +1,600 @@
|
||||
//! Command execution security policy (Whitelist / Blacklist) for coding agents.
|
||||
|
||||
use ide_core::settings::AgentSecuritySettings;
|
||||
use serde_json::Value;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub enum PolicyDecision {
|
||||
/// Command matched a blacklist pattern (`pattern`).
|
||||
Blocked(String),
|
||||
/// Command matched the whitelist and no blacklist pattern.
|
||||
Allowed,
|
||||
/// Neither matched; prompt the user as usual.
|
||||
AskUser,
|
||||
}
|
||||
|
||||
/// The shell command an ACP `session/request_permission` asks to run: only a tool call of kind
|
||||
/// `execute`, and only its structured `rawInput` command. A title is free text the agent
|
||||
/// writes, so it never names the command.
|
||||
pub fn extract_command(params: &Value) -> Option<String> {
|
||||
let tool = params.get("toolCall").or_else(|| params.pointer("/subject/toolCall"))?;
|
||||
if tool.get("kind").and_then(Value::as_str) != Some("execute") {
|
||||
return None;
|
||||
}
|
||||
let raw_cmd = tool.pointer("/rawInput/command").or_else(|| tool.pointer("/rawInput/cmd"));
|
||||
raw_cmd.and_then(command_from_value)
|
||||
}
|
||||
|
||||
/// Whether allowing the request grants more than running its command, such as network access
|
||||
/// for it: the command policy may block such a request but never approves it.
|
||||
pub fn widens_access(params: &Value) -> bool {
|
||||
let tool = params.get("toolCall").or_else(|| params.pointer("/subject/toolCall"));
|
||||
tool.and_then(|t| t.pointer("/rawInput/networkApprovalContext")).is_some_and(|c| !c.is_null())
|
||||
}
|
||||
|
||||
fn command_from_value(val: &Value) -> Option<String> {
|
||||
match val {
|
||||
Value::String(s) => {
|
||||
let trimmed = s.trim();
|
||||
(!trimmed.is_empty()).then(|| trimmed.to_owned())
|
||||
}
|
||||
Value::Array(items) => {
|
||||
let words: Vec<&str> = items.iter().filter_map(Value::as_str).collect();
|
||||
if words.is_empty() {
|
||||
return None;
|
||||
}
|
||||
let cmd = match words.as_slice() {
|
||||
[_, flag, script] if flag.starts_with('-') && flag.ends_with('c') => (*script).trim().to_owned(),
|
||||
_ => words.join(" ").trim().to_owned(),
|
||||
};
|
||||
(!cmd.is_empty()).then_some(cmd)
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Checks whether a single pattern matches `command` using prefix + glob rules:
|
||||
/// - If `pattern` contains `*` or `?`, matches using glob wildcards (where `*` matches
|
||||
/// any sequence of characters). A trailing ` *` in the pattern also matches the bare
|
||||
/// command without arguments (e.g. `git status *` matches `git status`).
|
||||
/// - Otherwise, matches when `command` equals `pattern` or starts with `pattern` followed
|
||||
/// by whitespace (prefix match at word boundary).
|
||||
pub fn matches_command(pattern: &str, command: &str) -> bool {
|
||||
let pat = pattern.trim();
|
||||
let cmd = command.trim();
|
||||
if pat.is_empty() || cmd.is_empty() {
|
||||
return false;
|
||||
}
|
||||
if pat.contains('*') || pat.contains('?') {
|
||||
if let Some(stem) = pat.strip_suffix(" *")
|
||||
&& cmd == stem.trim()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
wildcard_match(pat.as_bytes(), cmd.as_bytes())
|
||||
} else {
|
||||
cmd == pat || cmd.strip_prefix(pat).is_some_and(|rest| rest.starts_with(char::is_whitespace))
|
||||
}
|
||||
}
|
||||
|
||||
fn wildcard_match(pat: &[u8], text: &[u8]) -> bool {
|
||||
let (mut p, mut t) = (0, 0);
|
||||
let (mut star_p, mut star_t) = (None, 0);
|
||||
while t < text.len() {
|
||||
if p < pat.len() && (pat[p] == b'?' || pat[p] == text[t]) {
|
||||
p += 1;
|
||||
t += 1;
|
||||
} else if p < pat.len() && pat[p] == b'*' {
|
||||
star_p = Some(p);
|
||||
p += 1;
|
||||
star_t = t;
|
||||
} else if let Some(sp) = star_p {
|
||||
p = sp + 1;
|
||||
star_t += 1;
|
||||
t = star_t;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
while p < pat.len() && pat[p] == b'*' {
|
||||
p += 1;
|
||||
}
|
||||
p == pat.len()
|
||||
}
|
||||
|
||||
/// Matches an allowed command entry against a sub-command.
|
||||
/// An allow entry is a whole command or a prefix only up to a word boundary:
|
||||
/// e.g. `git status` matches `git status` and `git status -s`, but NOT `git statusx`.
|
||||
/// Any accidental trailing `*` in the allow entry is stripped so `git status*` behaves
|
||||
/// cleanly as a prefix at word boundaries instead of a glob wildcard.
|
||||
pub fn matches_allow_entry(entry: &str, command: &str) -> bool {
|
||||
let mut entry = entry.trim();
|
||||
if let Some(stripped) = entry.strip_suffix('*') {
|
||||
entry = stripped.trim_end();
|
||||
}
|
||||
let cmd = command.trim();
|
||||
if entry.is_empty() || cmd.is_empty() {
|
||||
return false;
|
||||
}
|
||||
cmd == entry || cmd.strip_prefix(entry).is_some_and(|rest| rest.starts_with(char::is_whitespace))
|
||||
}
|
||||
|
||||
/// Checks whether a word is an environment variable assignment (`IDENTIFIER=...`).
|
||||
fn is_env_assignment_word(word: &str) -> bool {
|
||||
let Some((name, _)) = word.split_once('=') else {
|
||||
return false;
|
||||
};
|
||||
if name.is_empty() {
|
||||
return false;
|
||||
}
|
||||
let mut chars = name.chars();
|
||||
match chars.next() {
|
||||
Some(c) if c.is_ascii_alphabetic() || c == '_' => {}
|
||||
_ => return false,
|
||||
}
|
||||
chars.all(|c| c.is_ascii_alphanumeric() || c == '_')
|
||||
}
|
||||
|
||||
/// Returns true if the sub-command starts with an environment variable assignment,
|
||||
/// e.g. `FOO=bar cmd` or `PORT=3000`.
|
||||
fn has_leading_env_assignment(cmd: &str) -> bool {
|
||||
let trimmed = cmd.trim_start();
|
||||
let bytes = trimmed.as_bytes();
|
||||
let mut i = 0;
|
||||
let mut quote: Option<u8> = None;
|
||||
while i < bytes.len() {
|
||||
let b = bytes[i];
|
||||
if b == b'\\' && quote != Some(b'\'') && i + 1 < bytes.len() {
|
||||
i += 2;
|
||||
continue;
|
||||
}
|
||||
if let Some(q) = quote {
|
||||
if b == q {
|
||||
quote = None;
|
||||
}
|
||||
i += 1;
|
||||
continue;
|
||||
}
|
||||
if b == b'\'' || b == b'"' {
|
||||
quote = Some(b);
|
||||
i += 1;
|
||||
continue;
|
||||
}
|
||||
if b.is_ascii_whitespace() {
|
||||
break;
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
let first_word = &trimmed[..i];
|
||||
is_env_assignment_word(first_word)
|
||||
}
|
||||
|
||||
/// Checks whether `command` contains shell constructs that must never be auto-approved:
|
||||
/// - Command substitution: `$(...)` or backticks
|
||||
/// - Redirection: `>`, `>>`, `<`
|
||||
/// - Here-docs: `<<`
|
||||
/// - Subshells: `(...)`
|
||||
/// - Leading environment assignments: `VAR=val cmd`
|
||||
pub fn has_unsafe_shell_syntax(command: &str) -> bool {
|
||||
let cmd = command.trim();
|
||||
if cmd.is_empty() {
|
||||
return false;
|
||||
}
|
||||
|
||||
if has_leading_env_assignment(cmd) {
|
||||
return true;
|
||||
}
|
||||
|
||||
let bytes = cmd.as_bytes();
|
||||
let mut i = 0;
|
||||
let mut quote: Option<u8> = None;
|
||||
|
||||
while i < bytes.len() {
|
||||
let b = bytes[i];
|
||||
|
||||
if b == b'\\' && quote != Some(b'\'') && i + 1 < bytes.len() {
|
||||
i += 2;
|
||||
continue;
|
||||
}
|
||||
|
||||
if let Some(q) = quote {
|
||||
if b == q {
|
||||
quote = None;
|
||||
i += 1;
|
||||
continue;
|
||||
}
|
||||
// Inside double quotes: $(...) and `...` are evaluated by the shell.
|
||||
if q == b'"' {
|
||||
if b == b'`' {
|
||||
return true;
|
||||
}
|
||||
if b == b'$' && i + 1 < bytes.len() && bytes[i + 1] == b'(' {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
i += 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
match b {
|
||||
b'\'' | b'"' => {
|
||||
quote = Some(b);
|
||||
i += 1;
|
||||
}
|
||||
b'`' => return true,
|
||||
b'$' if i + 1 < bytes.len() && bytes[i + 1] == b'(' => return true,
|
||||
b'<' | b'>' => return true,
|
||||
b'(' | b')' => return true,
|
||||
_ => i += 1,
|
||||
}
|
||||
}
|
||||
|
||||
false
|
||||
}
|
||||
|
||||
/// Splits a shell command into sub-commands separated by `;`, `&&`, `||`, `|`, or `\n`
|
||||
/// (outside single/double quotes) so compound commands cannot bypass policy checks.
|
||||
fn split_subcommands(command: &str) -> Vec<&str> {
|
||||
let bytes = command.as_bytes();
|
||||
let mut out = Vec::new();
|
||||
let mut start = 0;
|
||||
let mut i = 0;
|
||||
let mut quote: Option<u8> = None;
|
||||
|
||||
while i < bytes.len() {
|
||||
let b = bytes[i];
|
||||
if let Some(q) = quote {
|
||||
if b == b'\\' && q == b'"' && i + 1 < bytes.len() {
|
||||
i += 2;
|
||||
continue;
|
||||
}
|
||||
if b == q {
|
||||
quote = None;
|
||||
}
|
||||
i += 1;
|
||||
continue;
|
||||
}
|
||||
match b {
|
||||
b'\'' | b'"' => {
|
||||
quote = Some(b);
|
||||
i += 1;
|
||||
}
|
||||
b'\\' if i + 1 < bytes.len() => {
|
||||
i += 2;
|
||||
}
|
||||
b';' | b'\n' => {
|
||||
let seg = command[start..i].trim();
|
||||
if !seg.is_empty() {
|
||||
out.push(seg);
|
||||
}
|
||||
i += 1;
|
||||
start = i;
|
||||
}
|
||||
b'&' | b'|' => {
|
||||
let seg = command[start..i].trim();
|
||||
if !seg.is_empty() {
|
||||
out.push(seg);
|
||||
}
|
||||
if i + 1 < bytes.len() && bytes[i + 1] == b {
|
||||
i += 2;
|
||||
} else {
|
||||
i += 1;
|
||||
}
|
||||
start = i;
|
||||
}
|
||||
_ => i += 1,
|
||||
}
|
||||
}
|
||||
let tail = command[start..].trim();
|
||||
if !tail.is_empty() {
|
||||
out.push(tail);
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// Whether every shell an agent may run `command` in (a POSIX shell, PowerShell, cmd.exe) reads
|
||||
/// it as the one command the policy sees. Outside double quotes no separator or redirection may
|
||||
/// remain after splitting (cmd.exe takes single quotes literally); `\` may only stand in a
|
||||
/// path (it escapes only in POSIX shells); no `^` may escape a quote (cmd.exe); no `%` (cmd.exe
|
||||
/// expands it before parsing); no carriage return; no typographic quotes (PowerShell ends
|
||||
/// strings at them).
|
||||
fn reads_alike_in_every_shell(command: &str) -> bool {
|
||||
let chars: Vec<char> = command.chars().collect();
|
||||
let mut in_double = false;
|
||||
for (i, &c) in chars.iter().enumerate() {
|
||||
let next = chars.get(i + 1).copied();
|
||||
match c {
|
||||
'"' => in_double = !in_double,
|
||||
'\\' if !next.is_none_or(|n| n.is_ascii_alphanumeric() || matches!(n, '\\' | '.' | '_' | '-')) => {
|
||||
return false;
|
||||
}
|
||||
'^' if matches!(next, Some('"' | '\'' | '\n' | '\r')) => return false,
|
||||
'%' | '\r' | '\u{2018}'..='\u{201f}' => return false,
|
||||
';' | '&' | '|' | '<' | '>' | '\n' if !in_double => return false,
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
/// A command as blocked patterns are compared with it: lower case, single spaces, and the
|
||||
/// program without its folder or `.exe` (`/bin/rm` and `RM.EXE` are `rm`).
|
||||
fn normalized_for_blocking(command: &str) -> String {
|
||||
let mut words = command.split_whitespace().map(str::to_lowercase);
|
||||
let Some(program) = words.next() else { return String::new() };
|
||||
let program = program.rsplit(['/', '\\']).next().unwrap_or(&program);
|
||||
let program = program.strip_suffix(".exe").unwrap_or(program);
|
||||
std::iter::once(program.to_owned()).chain(words).collect::<Vec<_>>().join(" ")
|
||||
}
|
||||
|
||||
/// Evaluates `command` against `settings`:
|
||||
/// 1. Blacklist takes precedence: if the full command or any sub-command matches a
|
||||
/// `blocked_commands` entry, returns [`PolicyDecision::Blocked`].
|
||||
/// 2. Whitelist: allowed_commands cannot auto-approve when empty.
|
||||
/// 3. Every sub-command in a chain must be safe (no command substitution, redirection,
|
||||
/// subshell, or env assignments) and match at least one `allowed_commands` entry
|
||||
/// exactly or up to word boundary; otherwise falls back to [`PolicyDecision::AskUser`].
|
||||
pub fn evaluate_command(command: &str, settings: &AgentSecuritySettings) -> PolicyDecision {
|
||||
let cmd = command.trim();
|
||||
if cmd.is_empty() {
|
||||
return PolicyDecision::AskUser;
|
||||
}
|
||||
let subcommands = split_subcommands(cmd);
|
||||
|
||||
let normalized: Vec<String> =
|
||||
std::iter::once(cmd).chain(subcommands.iter().copied()).map(normalized_for_blocking).collect();
|
||||
for pat in &settings.blocked_commands {
|
||||
let pat = pat.trim();
|
||||
if pat.is_empty() {
|
||||
continue;
|
||||
}
|
||||
let normalized_pat = normalized_for_blocking(pat);
|
||||
if matches_command(pat, cmd)
|
||||
|| subcommands.iter().any(|sub| matches_command(pat, sub))
|
||||
|| normalized.iter().any(|c| matches_command(&normalized_pat, c))
|
||||
{
|
||||
return PolicyDecision::Blocked(pat.to_owned());
|
||||
}
|
||||
}
|
||||
|
||||
if settings.allowed_commands.is_empty() || subcommands.is_empty() {
|
||||
return PolicyDecision::AskUser;
|
||||
}
|
||||
|
||||
let all_allowed = subcommands.iter().all(|sub| {
|
||||
let s = sub.trim();
|
||||
if s.is_empty() || has_unsafe_shell_syntax(s) || !reads_alike_in_every_shell(s) {
|
||||
return false;
|
||||
}
|
||||
settings.allowed_commands.iter().any(|entry| matches_allow_entry(entry, s))
|
||||
});
|
||||
|
||||
if all_allowed {
|
||||
return PolicyDecision::Allowed;
|
||||
}
|
||||
|
||||
PolicyDecision::AskUser
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use serde_json::json;
|
||||
|
||||
#[test]
|
||||
fn prefix_and_glob_matching() {
|
||||
assert!(matches_command("git status", "git status"));
|
||||
assert!(matches_command("git status", "git status -s"));
|
||||
assert!(!matches_command("git status", "git statusbar"));
|
||||
assert!(matches_command("git status*", "git status"));
|
||||
assert!(matches_command("git status*", "git status --short"));
|
||||
assert!(matches_command("npm test *", "npm test"));
|
||||
assert!(matches_command("npm test *", "npm test unit"));
|
||||
assert!(matches_command("rm -rf *", "rm -rf /tmp/foo"));
|
||||
assert!(matches_command("rm -rf *", "rm -rf /"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn blacklist_takes_precedence_and_catches_chained_commands() {
|
||||
let settings = AgentSecuritySettings::default();
|
||||
assert_eq!(evaluate_command("rm -rf /tmp/build", &settings), PolicyDecision::Blocked("rm -rf *".into()));
|
||||
assert_eq!(evaluate_command("git status && rm -rf /", &settings), PolicyDecision::Blocked("rm -rf *".into()));
|
||||
assert_eq!(evaluate_command(":(){ :|:& };:", &settings), PolicyDecision::Blocked(":(){ :|:& };:".into()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn allow_entry_matching_word_boundary() {
|
||||
assert!(matches_allow_entry("git status", "git status"));
|
||||
assert!(matches_allow_entry("git status", "git status -s"));
|
||||
assert!(!matches_allow_entry("git status", "git statusbar"));
|
||||
assert!(!matches_allow_entry("git status", "git statusx"));
|
||||
// Trailing asterisk in allow entry normalizes cleanly to word boundary prefix
|
||||
assert!(matches_allow_entry("git status*", "git status"));
|
||||
assert!(matches_allow_entry("git status*", "git status --short"));
|
||||
assert!(!matches_allow_entry("git status*", "git statusbar"));
|
||||
assert!(matches_allow_entry("ls", "ls"));
|
||||
assert!(matches_allow_entry("ls", "ls -la"));
|
||||
assert!(!matches_allow_entry("ls", "lsof"));
|
||||
assert!(matches_allow_entry("cargo check", "cargo check"));
|
||||
assert!(matches_allow_entry("cargo check", "cargo check --workspace"));
|
||||
assert!(!matches_allow_entry("cargo check", "cargo checkmate"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unsafe_shell_syntax_detection() {
|
||||
// Command substitutions: $(...) and `...`
|
||||
assert!(has_unsafe_shell_syntax("echo $(whoami)"));
|
||||
assert!(has_unsafe_shell_syntax("echo \"user: $(whoami)\""));
|
||||
assert!(has_unsafe_shell_syntax("echo `whoami`"));
|
||||
assert!(has_unsafe_shell_syntax("echo \"`whoami`\""));
|
||||
|
||||
// Redirections: >, >>, <
|
||||
assert!(has_unsafe_shell_syntax("git status > /tmp/out"));
|
||||
assert!(has_unsafe_shell_syntax("git status >> /tmp/out"));
|
||||
assert!(has_unsafe_shell_syntax("cat < /tmp/in"));
|
||||
assert!(has_unsafe_shell_syntax("cat << 'EOF'"));
|
||||
|
||||
// Subshells: (...)
|
||||
assert!(has_unsafe_shell_syntax("(cd /tmp && ls)"));
|
||||
assert!(has_unsafe_shell_syntax("git status && (exit 1)"));
|
||||
|
||||
// Environment assignments: VAR=val ...
|
||||
assert!(has_unsafe_shell_syntax("FOO=bar git status"));
|
||||
assert!(has_unsafe_shell_syntax("PORT=3000 npm test"));
|
||||
assert!(has_unsafe_shell_syntax("_SECRET=123 cargo test"));
|
||||
assert!(has_unsafe_shell_syntax("A=\"b c\" git status"));
|
||||
|
||||
// Safe constructs with characters inside quotes or command flags
|
||||
assert!(!has_unsafe_shell_syntax("git commit -m \"fix <bug> and (issue)\""));
|
||||
assert!(!has_unsafe_shell_syntax("git commit -m 'fix <bug>'"));
|
||||
assert!(!has_unsafe_shell_syntax("cargo test --test=unit"));
|
||||
assert!(!has_unsafe_shell_syntax("git status -s"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn default_settings_has_no_auto_approval_and_blocks_dangerous_defaults() {
|
||||
let settings = AgentSecuritySettings::default();
|
||||
assert!(settings.allowed_commands.is_empty(), "allowed_commands must start empty");
|
||||
// Safe commands ask user by default
|
||||
assert_eq!(evaluate_command("git status", &settings), PolicyDecision::AskUser);
|
||||
assert_eq!(evaluate_command("ls", &settings), PolicyDecision::AskUser);
|
||||
assert_eq!(evaluate_command("cargo check", &settings), PolicyDecision::AskUser);
|
||||
// Blacklist patterns are still blocked by default
|
||||
assert_eq!(evaluate_command("rm -rf /", &settings), PolicyDecision::Blocked("rm -rf *".into()));
|
||||
assert_eq!(evaluate_command("rm -rf /tmp/test", &settings), PolicyDecision::Blocked("rm -rf *".into()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn whitelist_allows_safe_single_and_chained_commands_only() {
|
||||
let settings = AgentSecuritySettings {
|
||||
allowed_commands: vec!["git status".into(), "git diff".into(), "cargo test".into()],
|
||||
blocked_commands: vec!["rm -rf *".into()],
|
||||
};
|
||||
assert_eq!(evaluate_command("git status -s", &settings), PolicyDecision::Allowed);
|
||||
assert_eq!(evaluate_command("git diff HEAD~1", &settings), PolicyDecision::Allowed);
|
||||
assert_eq!(evaluate_command("git status && cargo test", &settings), PolicyDecision::Allowed);
|
||||
|
||||
// Word boundary: git statusx must not match git status
|
||||
assert_eq!(evaluate_command("git statusx", &settings), PolicyDecision::AskUser);
|
||||
|
||||
// Unsafe syntax (redirection, command substitution, env assignment) falls back to AskUser
|
||||
assert_eq!(evaluate_command("git status > out.txt", &settings), PolicyDecision::AskUser);
|
||||
assert_eq!(evaluate_command("git status $(whoami)", &settings), PolicyDecision::AskUser);
|
||||
assert_eq!(evaluate_command("FOO=bar git status", &settings), PolicyDecision::AskUser);
|
||||
|
||||
// Chaining an unapproved command must still ask the user.
|
||||
assert_eq!(evaluate_command("git status && curl https://example.com", &settings), PolicyDecision::AskUser);
|
||||
assert_eq!(evaluate_command("cargo publish", &settings), PolicyDecision::AskUser);
|
||||
|
||||
// Blacklist still takes precedence over whitelist
|
||||
assert_eq!(evaluate_command("git status && rm -rf /", &settings), PolicyDecision::Blocked("rm -rf *".into()));
|
||||
}
|
||||
|
||||
/// The command may run in PowerShell or cmd.exe as well as a POSIX shell: what any of them
|
||||
/// would split or expand differently from the policy's reading is never auto-approved.
|
||||
#[test]
|
||||
fn what_another_shell_reads_as_two_commands_is_not_auto_approved() {
|
||||
let settings = AgentSecuritySettings {
|
||||
allowed_commands: vec!["git status".into(), "git commit".into()],
|
||||
blocked_commands: vec![],
|
||||
};
|
||||
for cmd in [
|
||||
// `\` escapes only in POSIX shells.
|
||||
"git status \\; calc.exe",
|
||||
"git status \\& calc.exe",
|
||||
"git status \\| calc.exe",
|
||||
"git status \\\\\\; calc.exe",
|
||||
"git status \"\\\" ; calc.exe ; \\\"\"",
|
||||
// cmd.exe: single quotes don't quote, `^` escapes a quote, `%` expands first.
|
||||
"git status '&' calc.exe",
|
||||
"git status ';' calc.exe",
|
||||
"git status '>' out.txt",
|
||||
"git status ^\" & calc.exe ^\"",
|
||||
"git status %COMSPEC%",
|
||||
// PowerShell also closes a string at a typographic quote.
|
||||
"git status \"a \u{201d} ; calc.exe ; \u{201c} b\"",
|
||||
"git status \u{2018}x\u{2019}",
|
||||
// A carriage return ends the command line too.
|
||||
"git status\r calc.exe",
|
||||
] {
|
||||
assert_eq!(evaluate_command(cmd, &settings), PolicyDecision::AskUser, "{cmd:?}");
|
||||
}
|
||||
// Text every shell reads alike stays approvable.
|
||||
for cmd in [
|
||||
"git status -s",
|
||||
"git status && git status",
|
||||
"git commit -m \"fix <bug> & (issue); done\"",
|
||||
"git status src\\main.rs C:\\work\\repo",
|
||||
"git status HEAD^",
|
||||
] {
|
||||
assert_eq!(evaluate_command(cmd, &settings), PolicyDecision::Allowed, "{cmd:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn blocked_patterns_ignore_case_spacing_and_the_programs_folder() {
|
||||
let settings = AgentSecuritySettings::default();
|
||||
for cmd in
|
||||
["rm -rf /", "RM -RF /", "/bin/rm -rf /", "Rm -Rf /tmp/x", "C:\\Windows\\System32\\shutdown.exe /s"]
|
||||
{
|
||||
assert!(matches!(evaluate_command(cmd, &settings), PolicyDecision::Blocked(_)), "{cmd:?}");
|
||||
}
|
||||
assert_eq!(evaluate_command("git status", &settings), PolicyDecision::AskUser);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn extracts_command_from_v1_v2_and_codex_payloads() {
|
||||
let claude = json!({
|
||||
"toolCall": {
|
||||
"title": "git status",
|
||||
"kind": "execute",
|
||||
"rawInput": { "command": "git status --short" }
|
||||
}
|
||||
});
|
||||
assert_eq!(extract_command(&claude).as_deref(), Some("git status --short"));
|
||||
|
||||
let codex = json!({
|
||||
"toolCall": {
|
||||
"title": "Run cargo test",
|
||||
"kind": "execute",
|
||||
"rawInput": { "command": ["/bin/zsh", "-lc", "cargo test --workspace"] }
|
||||
}
|
||||
});
|
||||
assert_eq!(extract_command(&codex).as_deref(), Some("cargo test --workspace"));
|
||||
|
||||
let write = json!({
|
||||
"toolCall": {
|
||||
"title": "Write foo.txt",
|
||||
"kind": "edit",
|
||||
"rawInput": { "file_path": "foo.txt" }
|
||||
}
|
||||
});
|
||||
assert_eq!(extract_command(&write), None);
|
||||
}
|
||||
|
||||
/// Only a tool call of kind `execute` runs a command, and its command is in `rawInput`:
|
||||
/// a title says what the agent wants it to say.
|
||||
#[test]
|
||||
fn a_request_is_a_command_only_by_its_kind_and_input() {
|
||||
let escalation = json!({ "sessionId": "t", "toolCall": {
|
||||
"toolCallId": "i1", "title": "Run git status", "kind": "other", "status": "pending",
|
||||
"rawInput": { "fileSystem": { "write": ["C:\\"] }, "network": { "enabled": true } } },
|
||||
"options": [ { "optionId": "turn", "name": "Allow for this turn", "kind": "allow_once" },
|
||||
{ "optionId": "decline", "name": "Decline", "kind": "reject_once" } ] });
|
||||
assert_eq!(extract_command(&escalation), None);
|
||||
let titled_only = json!({ "toolCall": { "title": "Run git status", "kind": "execute" } });
|
||||
assert_eq!(extract_command(&titled_only), None);
|
||||
let named_only = json!({ "toolCall": { "title": "git status", "name": "Bash" } });
|
||||
assert_eq!(extract_command(&named_only), None);
|
||||
let mislabelled = json!({ "toolCall": { "title": "Run git status", "kind": "edit",
|
||||
"rawInput": { "command": "git status" } } });
|
||||
assert_eq!(extract_command(&mislabelled), None);
|
||||
let network = json!({ "toolCall": { "title": "Allow network access to example.com", "kind": "execute",
|
||||
"rawInput": { "command": "curl https://example.com", "networkApprovalContext": { "host": "example.com" } } } });
|
||||
assert_eq!(extract_command(&network).as_deref(), Some("curl https://example.com"));
|
||||
assert!(widens_access(&network));
|
||||
let plain = json!({ "toolCall": { "title": "Run git status", "kind": "execute",
|
||||
"rawInput": { "command": "git status" } } });
|
||||
assert!(!widens_access(&plain));
|
||||
}
|
||||
}
|
||||
+1434
-66
File diff suppressed because it is too large.
Load diff
@@ -438,8 +438,10 @@ mod tests {
|
||||
t.push(AgentEntry::Permission {
|
||||
id: 1,
|
||||
title: "Run?".into(),
|
||||
command: None,
|
||||
options: vec![],
|
||||
questions: vec![],
|
||||
form: None,
|
||||
chosen: None,
|
||||
});
|
||||
assert_eq!(t.end_turn(None), vec![2, 3]);
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
//! Task worktrees and the changes made in them.
|
||||
|
||||
use crate::model::AgentChange;
|
||||
use crate::model::{AgentChange, AgentChanges, AgentCommit};
|
||||
use ide_core::{IdeError, IdeResult, IpcPath};
|
||||
use ide_vcs::{ChangeKind, Repo};
|
||||
use std::collections::BTreeMap;
|
||||
use ide_vcs::{ChangeKind, LogQuery, Repo};
|
||||
use std::collections::{BTreeMap, BTreeSet};
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::time::{Duration, UNIX_EPOCH};
|
||||
|
||||
@@ -14,8 +14,18 @@ pub(crate) struct Created {
|
||||
pub base_commit: String,
|
||||
}
|
||||
|
||||
/// The repository of `path`, free to execute: the host runs every agent command that
|
||||
/// reaches a repository only in a trusted project.
|
||||
fn discover(path: &Path) -> IdeResult<Option<Repo>> {
|
||||
let repo = Repo::discover(path)?;
|
||||
if let Some(repo) = &repo {
|
||||
repo.set_execution_allowed(true);
|
||||
}
|
||||
Ok(repo)
|
||||
}
|
||||
|
||||
fn repo_at(path: &Path) -> IdeResult<Repo> {
|
||||
Repo::discover(path)?.ok_or_else(|| IdeError::unavailable(format!("{} is not in a git repository", path.display())))
|
||||
discover(path)?.ok_or_else(|| IdeError::unavailable(format!("{} is not in a git repository", path.display())))
|
||||
}
|
||||
|
||||
/// The current branch (or commit when detached) and its commit.
|
||||
@@ -25,6 +35,14 @@ pub(crate) fn head_of(path: &Path) -> IdeResult<(String, String)> {
|
||||
Ok((info.head.unwrap_or_else(|| commit.clone()), commit))
|
||||
}
|
||||
|
||||
/// `HEAD`'s commit in `cwd`, `None` outside a repository or before its first commit.
|
||||
pub(crate) fn head_commit(cwd: &Path) -> IdeResult<Option<String>> {
|
||||
match discover(cwd)? {
|
||||
Some(repo) => Ok(repo.head()?.1),
|
||||
None => Ok(None),
|
||||
}
|
||||
}
|
||||
|
||||
/// `git worktree add -b agent/<slug> <dir>/<slug> <base>`.
|
||||
pub(crate) fn create(project_root: &Path, worktrees_dir: &Path, title: &str, base: Option<&str>) -> IdeResult<Created> {
|
||||
let repo = repo_at(project_root)?;
|
||||
@@ -91,28 +109,428 @@ fn retrying(attempts: u32, pause: Duration, mut op: impl FnMut() -> std::io::Res
|
||||
/// Files under `cwd` that differ from `base` (untracked files included), with line counts.
|
||||
/// Outside a repository there is nothing to compare against: no changes.
|
||||
pub(crate) fn changes(cwd: &Path, base: &str) -> IdeResult<Vec<AgentChange>> {
|
||||
let Some(repo) = Repo::discover(cwd)? else { return Ok(Vec::new()) };
|
||||
let Some(repo) = discover(cwd)? else { return Ok(Vec::new()) };
|
||||
let root = repo.root().to_path_buf();
|
||||
let stats = repo.numstat_with_worktree(base)?;
|
||||
let stat = |rel: &str| stats.iter().find(|(p, ..)| p == rel).map_or((0, 0), |&(_, a, r)| (a, r));
|
||||
let stats: std::collections::HashMap<String, (u32, u32)> =
|
||||
repo.numstat_with_worktree(base)?.into_iter().map(|(p, a, r)| (p, (a, r))).collect();
|
||||
let stat = |rel: &str| stats.get(rel).copied().unwrap_or((0, 0));
|
||||
let mut out: Vec<AgentChange> = repo
|
||||
.diff_with_worktree(base)?
|
||||
.into_iter()
|
||||
.map(|f| {
|
||||
let (added, removed) = stat(&f.path);
|
||||
AgentChange { path: IpcPath(root.join(&f.path)), rel_path: f.path, kind: f.kind, added, removed }
|
||||
AgentChange {
|
||||
path: IpcPath(root.join(&f.path)),
|
||||
rel_path: f.path,
|
||||
kind: f.kind,
|
||||
added,
|
||||
removed,
|
||||
old_path: f.old_path,
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
let tracked: std::collections::HashSet<String> = out.iter().map(|c| c.rel_path.clone()).collect();
|
||||
for s in repo.status()? {
|
||||
if s.unstaged == Some(ChangeKind::Untracked) && !out.iter().any(|c| c.rel_path == s.rel_path) {
|
||||
let added = std::fs::read_to_string(&*s.path).map_or(0, |t| t.lines().count() as u32);
|
||||
out.push(AgentChange { path: s.path, rel_path: s.rel_path, kind: ChangeKind::Added, added, removed: 0 });
|
||||
if s.unstaged == Some(ChangeKind::Untracked) && !tracked.contains(&s.rel_path) {
|
||||
out.push(added(s.path, s.rel_path));
|
||||
}
|
||||
}
|
||||
out.sort_by(|a, b| a.rel_path.cmp(&b.rel_path));
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
/// Files renamed since `rev`, by their new repository-relative path: the old path, as a deletion
|
||||
/// to roll back along with the rename.
|
||||
pub(crate) fn rename_sources(cwd: &Path, rev: &str) -> IdeResult<BTreeMap<String, AgentChange>> {
|
||||
let Some(repo) = discover(cwd)? else { return Ok(BTreeMap::new()) };
|
||||
let root = repo.root().to_path_buf();
|
||||
Ok(repo
|
||||
.diff_with_worktree(rev)?
|
||||
.into_iter()
|
||||
.filter_map(|f| {
|
||||
let old = f.old_path.filter(|old| *old != f.path)?;
|
||||
let source = AgentChange {
|
||||
path: IpcPath(root.join(&old)),
|
||||
rel_path: old,
|
||||
kind: ChangeKind::Deleted,
|
||||
added: 0,
|
||||
removed: 0,
|
||||
old_path: None,
|
||||
};
|
||||
Some((f.path, source))
|
||||
})
|
||||
.collect())
|
||||
}
|
||||
|
||||
fn added(path: IpcPath, rel_path: String) -> AgentChange {
|
||||
let added = std::fs::read_to_string(&*path).map_or(0, |t| t.lines().count() as u32);
|
||||
AgentChange { path, rel_path, kind: ChangeKind::Added, added, removed: 0, old_path: None }
|
||||
}
|
||||
|
||||
/// The commits from `base` (exclusive) to `head` (inclusive) in `cwd`, newest first, each
|
||||
/// with the files it changed against its first parent and their line counts.
|
||||
pub(crate) fn commits(cwd: &Path, base: &str, head: &str) -> IdeResult<Vec<AgentCommit>> {
|
||||
if base == head {
|
||||
return Ok(Vec::new());
|
||||
}
|
||||
let Some(repo) = discover(cwd)? else { return Ok(Vec::new()) };
|
||||
let root = repo.root().to_path_buf();
|
||||
let query = LogQuery { range: Some(format!("{base}..{head}")), ..LogQuery::default() };
|
||||
let mut out = Vec::new();
|
||||
for commit in repo.log(&query)? {
|
||||
let parent = commit.parents.first().cloned();
|
||||
// A root commit has no parent to count lines against; its files still show.
|
||||
let stats: std::collections::HashMap<String, (u32, u32)> = match &parent {
|
||||
Some(parent) => repo
|
||||
.diff_numstat(parent, &commit.id, &[])?
|
||||
.into_iter()
|
||||
.map(|s| (s.path, (s.added, s.removed)))
|
||||
.collect(),
|
||||
None => std::collections::HashMap::new(),
|
||||
};
|
||||
let mut files: Vec<AgentChange> = repo
|
||||
.commit_files(&commit.id)?
|
||||
.into_iter()
|
||||
.map(|f| {
|
||||
let (added, removed) = stats.get(&f.path).copied().unwrap_or((0, 0));
|
||||
AgentChange {
|
||||
path: IpcPath(root.join(&f.path)),
|
||||
rel_path: f.path,
|
||||
kind: f.kind,
|
||||
added,
|
||||
removed,
|
||||
old_path: f.old_path,
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
files.sort_by(|a, b| a.rel_path.cmp(&b.rel_path));
|
||||
out.push(AgentCommit {
|
||||
id: commit.id,
|
||||
short_id: commit.short_id,
|
||||
summary: commit.summary,
|
||||
message: commit.message,
|
||||
time_ms: commit.time_ms,
|
||||
parent,
|
||||
files,
|
||||
});
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
/// Whether two paths name one file (a case-insensitive disk resolving both spellings).
|
||||
fn same_file(a: &Path, b: &Path) -> bool {
|
||||
match (std::fs::canonicalize(a), std::fs::canonicalize(b)) {
|
||||
(Ok(a), Ok(b)) => a == b,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
/// The bytes of `path` (`Some(None)`: no file there), or `None` when it is there but can't be read.
|
||||
fn existing(path: &Path) -> Option<Option<Vec<u8>>> {
|
||||
match std::fs::read(path) {
|
||||
Ok(bytes) => Some(Some(bytes)),
|
||||
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Some(None),
|
||||
Err(_) => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn crlf_to_lf(bytes: &[u8]) -> Vec<u8> {
|
||||
let mut out = Vec::with_capacity(bytes.len());
|
||||
for (i, &b) in bytes.iter().enumerate() {
|
||||
if !(b == b'\r' && bytes.get(i + 1) == Some(&b'\n')) {
|
||||
out.push(b);
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// Where a task works and what its changes are measured against.
|
||||
pub(crate) enum TaskBase {
|
||||
/// A worktree of its own: everything since the commit it was created from, the agent's
|
||||
/// own commits included.
|
||||
Worktree { cwd: PathBuf, base: String },
|
||||
/// The project's checkout, where the user works and commits too. `start` is the commit it
|
||||
/// started from (`None` in a repository without commits, or an imported session).
|
||||
Local { cwd: PathBuf, start: Option<String> },
|
||||
}
|
||||
|
||||
impl TaskBase {
|
||||
pub(crate) fn cwd(&self) -> &Path {
|
||||
match self {
|
||||
Self::Worktree { cwd, .. } | Self::Local { cwd, .. } => cwd,
|
||||
}
|
||||
}
|
||||
|
||||
/// The files the task changed, less those changed before it started and untouched since.
|
||||
pub(crate) fn changes(&self, before: &BTreeMap<String, Stamp>) -> IdeResult<Vec<AgentChange>> {
|
||||
let (cwd, start) = match self {
|
||||
Self::Worktree { cwd, base } => return Ok(without_untouched(changes(cwd, base)?, before)),
|
||||
Self::Local { cwd, start } => (cwd, start.as_deref()),
|
||||
};
|
||||
let Some(repo) = discover(cwd)? else { return Ok(Vec::new()) };
|
||||
// What is uncommitted, as the Commit tool window lists it: commits made since the task
|
||||
// started drop out.
|
||||
let head = repo.branch_info()?.head_commit;
|
||||
let listed = match &head {
|
||||
Some(head) => changes(cwd, head)?,
|
||||
None => {
|
||||
let mut all: Vec<AgentChange> = repo
|
||||
.status()?
|
||||
.into_iter()
|
||||
.filter(|s| s.path.is_file())
|
||||
.map(|s| added(s.path, s.rel_path))
|
||||
.collect();
|
||||
all.sort_by(|a, b| a.rel_path.cmp(&b.rel_path));
|
||||
all
|
||||
}
|
||||
};
|
||||
let listed = without_untouched(listed, before);
|
||||
let Some(start) = start.filter(|s| head.as_deref() != Some(*s)) else { return Ok(listed) };
|
||||
// HEAD moved (a reset, a checkout, a pull): a file that is as it was when the task
|
||||
// started is the user's doing, not the task's. Git compares tracked files, with its
|
||||
// line-ending and other filters: the bytes on disk and in the commit differ under
|
||||
// `core.autocrlf`.
|
||||
let since_start: std::collections::HashSet<String> = repo
|
||||
.diff_with_worktree(start)?
|
||||
.into_iter()
|
||||
.flat_map(|f| [Some(f.path), f.old_path].into_iter().flatten())
|
||||
.collect();
|
||||
let untracked: std::collections::HashSet<String> = repo
|
||||
.status()?
|
||||
.into_iter()
|
||||
.filter(|s| s.unstaged == Some(ChangeKind::Untracked))
|
||||
.map(|s| s.rel_path)
|
||||
.collect();
|
||||
Ok(listed
|
||||
.into_iter()
|
||||
.filter(|c| {
|
||||
if before.contains_key(&c.rel_path) {
|
||||
return true;
|
||||
}
|
||||
let Ok(Some(at_start)) = repo.file_bytes_at(&c.path.0, Some(start)) else { return true };
|
||||
if !untracked.contains(&c.rel_path) {
|
||||
return since_start.contains(&c.rel_path);
|
||||
}
|
||||
// Untracked since (a mixed reset): git doesn't diff it, so compare the bytes,
|
||||
// CRLF on disk matching LF committed.
|
||||
!std::fs::read(&c.path.0).is_ok_and(|now| now == at_start || crlf_to_lf(&now) == at_start)
|
||||
})
|
||||
.collect())
|
||||
}
|
||||
|
||||
/// The task's uncommitted changes and, for a worktree task, the commits made since it
|
||||
/// started (newest first). Both read the same `HEAD`, so a commit landing between the two
|
||||
/// reads cannot put a file in both lists.
|
||||
pub(crate) fn grouped_changes(&self, before: &BTreeMap<String, Stamp>) -> IdeResult<AgentChanges> {
|
||||
let cwd = self.cwd();
|
||||
let Some(repo) = discover(cwd)? else { return Ok(AgentChanges::default()) };
|
||||
let head = repo.head()?.1;
|
||||
let uncommitted = match &head {
|
||||
Some(head) => without_untouched(changes(cwd, head)?, before),
|
||||
None => {
|
||||
// No commits yet: every file is new, as in `changes` for a local task.
|
||||
let mut all: Vec<AgentChange> = repo
|
||||
.status()?
|
||||
.into_iter()
|
||||
.filter(|s| s.path.is_file())
|
||||
.map(|s| added(s.path, s.rel_path))
|
||||
.collect();
|
||||
all.sort_by(|a, b| a.rel_path.cmp(&b.rel_path));
|
||||
without_untouched(all, before)
|
||||
}
|
||||
};
|
||||
let commits = match (self, &head) {
|
||||
(Self::Worktree { cwd, base }, Some(head)) => commits(cwd, base, head)?,
|
||||
_ => Vec::new(),
|
||||
};
|
||||
Ok(AgentChanges { uncommitted, commits })
|
||||
}
|
||||
|
||||
/// The revision the task's changed files go back to on rollback, and are compared with:
|
||||
/// the commit the task started from, or the current `HEAD` once the user committed on top
|
||||
/// of it, so those commits stay. `None` while the repository has no commits.
|
||||
pub(crate) fn rev(&self) -> IdeResult<Option<String>> {
|
||||
let (cwd, start) = match self {
|
||||
Self::Worktree { base, .. } => return Ok(Some(base.clone())),
|
||||
Self::Local { cwd, start } => (cwd, start.as_deref()),
|
||||
};
|
||||
let Some(repo) = discover(cwd)? else { return Ok(None) };
|
||||
let head = repo.branch_info()?.head_commit;
|
||||
// A start commit git no longer knows (garbage-collected) can't be gone back to.
|
||||
let start = start.filter(|s| repo.merge_base(s, s).ok().flatten().is_some());
|
||||
Ok(match (start, head) {
|
||||
(Some(start), Some(head)) if start != head && repo.merge_base(start, &head)?.as_deref() != Some(start) => {
|
||||
Some(start.to_owned())
|
||||
}
|
||||
(start, head) => head.or_else(|| start.map(str::to_owned)),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// One side of an Accept (the task's worktree or the checkout): where its files really are, and
|
||||
/// git's own folders for it.
|
||||
struct Confined {
|
||||
root: PathBuf,
|
||||
metadata: Vec<PathBuf>,
|
||||
}
|
||||
|
||||
impl Confined {
|
||||
fn new(root: &Path, repo: Option<&Repo>) -> IdeResult<Self> {
|
||||
let metadata = match repo {
|
||||
Some(repo) => repo.metadata_dirs()?.iter().map(|d| ide_core::real_path::resolve_real(d)).collect(),
|
||||
None => Vec::new(),
|
||||
};
|
||||
Ok(Self { root: dunce::canonicalize(root)?, metadata })
|
||||
}
|
||||
|
||||
/// Whether `rel` may be read or written here: it doesn't name git's metadata under any
|
||||
/// spelling (case, trailing dots or spaces, stream suffixes, short names), stays in the tree
|
||||
/// once links are followed, and isn't a link itself.
|
||||
fn allows(&self, rel: &str) -> bool {
|
||||
use ide_core::real_path::{names_git_dir, reaches_git_dir, resolve_real};
|
||||
let path = self.root.join(rel);
|
||||
if names_git_dir(Path::new(rel)) || path.symlink_metadata().is_ok_and(|m| m.file_type().is_symlink()) {
|
||||
return false;
|
||||
}
|
||||
let real = resolve_real(&path);
|
||||
real.starts_with(&self.root)
|
||||
&& !reaches_git_dir(&self.root, &real)
|
||||
&& !self.metadata.iter().any(|dir| real.starts_with(dir))
|
||||
}
|
||||
}
|
||||
|
||||
/// Copies the task's `changes` (against `base`) from the worktree `cwd` into the
|
||||
/// local checkout `root`, uncommitted: the user reviews and commits them with the
|
||||
/// VCS tools. Files the checkout changed itself are left alone and reported, so
|
||||
/// accepting never destroys the user's own edits. Returns accepted and skipped paths.
|
||||
pub(crate) fn accept(
|
||||
root: &Path,
|
||||
cwd: &Path,
|
||||
base: &str,
|
||||
changes: &[AgentChange],
|
||||
) -> IdeResult<crate::AgentAcceptOutcome> {
|
||||
let repo = repo_at(cwd)?;
|
||||
// Change paths are repository-relative, and the task's project may be a folder inside it.
|
||||
let local = discover(root)?;
|
||||
let local_root = local.as_ref().map_or_else(|| root.to_path_buf(), |r| r.root().to_path_buf());
|
||||
// Paths come from what the agent left in its worktree: neither side may lead into git's own
|
||||
// files or out through a link.
|
||||
let task_side = Confined::new(repo.root(), Some(&repo))?;
|
||||
let checkout_side = Confined::new(&local_root, local.as_ref())?;
|
||||
let copyable = |rel: &str| task_side.allows(rel) && checkout_side.allows(rel);
|
||||
let renamed_from: BTreeMap<String, String> =
|
||||
repo.diff_with_worktree(base)?.into_iter().filter_map(|f| Some((f.path, f.old_path?))).collect();
|
||||
// The task's version of one path of the checkout (`Some(None)`: absent there), or `None` when
|
||||
// the checkout changed it itself.
|
||||
let target = |rel: &str| -> IdeResult<Option<Option<Vec<u8>>>> {
|
||||
let in_task = repo.root().join(rel);
|
||||
let current = std::fs::read(&in_task).ok();
|
||||
// A file there that can't be read is the checkout's own: never written over.
|
||||
let Some(local_bytes) = existing(&local_root.join(rel)) else { return Ok(None) };
|
||||
if local_bytes == current {
|
||||
return Ok(Some(current));
|
||||
}
|
||||
let base_bytes = repo.file_bytes_at(&in_task, Some(base))?;
|
||||
// The checkout may hold the committed LF text with CRLF line ends (`core.autocrlf`).
|
||||
let unchanged = match (&local_bytes, &base_bytes) {
|
||||
(Some(now), Some(at_base)) => now == at_base || crlf_to_lf(now) == *at_base,
|
||||
(now, at_base) => now == at_base,
|
||||
};
|
||||
Ok(unchanged.then_some(current))
|
||||
};
|
||||
// Puts `bytes` (`None`: nothing) at `rel` of the checkout.
|
||||
let write = |rel: &str, bytes: Option<Vec<u8>>| -> IdeResult<()> {
|
||||
let local = local_root.join(rel);
|
||||
if std::fs::read(&local).ok() == bytes {
|
||||
return Ok(());
|
||||
}
|
||||
match bytes {
|
||||
Some(bytes) => {
|
||||
if let Some(parent) = local.parent() {
|
||||
std::fs::create_dir_all(parent)?;
|
||||
}
|
||||
std::fs::write(&local, bytes)?;
|
||||
}
|
||||
None => match std::fs::remove_file(&local) {
|
||||
Err(e) if e.kind() != std::io::ErrorKind::NotFound => return Err(e.into()),
|
||||
_ => {}
|
||||
},
|
||||
}
|
||||
Ok(())
|
||||
};
|
||||
// Brings one path of the checkout to the task's version, unless the checkout changed it itself.
|
||||
let apply = |rel: &str| -> IdeResult<bool> {
|
||||
let Some(bytes) = target(rel)? else { return Ok(false) };
|
||||
write(rel, bytes)?;
|
||||
Ok(true)
|
||||
};
|
||||
let mut accepted = Vec::with_capacity(changes.len());
|
||||
let mut skipped = Vec::new();
|
||||
let skip = |path: &str| crate::AgentAcceptSkipped {
|
||||
path: path.to_owned(),
|
||||
reason: "the checkout has changes of its own to it; accept it by hand".to_owned(),
|
||||
};
|
||||
let refuse = |path: &str| crate::AgentAcceptSkipped {
|
||||
path: path.to_owned(),
|
||||
reason: "it leads into git's own files or through a link, so it is not copied".to_owned(),
|
||||
};
|
||||
for change in changes {
|
||||
let old = renamed_from.get(&change.rel_path);
|
||||
if !copyable(&change.rel_path) {
|
||||
skipped.push(refuse(&change.rel_path));
|
||||
continue;
|
||||
}
|
||||
if let Some(old) = old.filter(|old| !copyable(old)) {
|
||||
skipped.push(refuse(old));
|
||||
continue;
|
||||
}
|
||||
// A case-only rename (`a.txt` → `A.txt`). Where the disk resolves both
|
||||
// spellings to one file it is renamed in place and `apply` then refreshes
|
||||
// the content. On a case-sensitive disk the two names are different files:
|
||||
// both are checked before either changes — the old one must be the file the
|
||||
// checkout had, the new one absent or already the task's — then the task's
|
||||
// version lands at the new name before the old one goes.
|
||||
let case_only =
|
||||
old.is_some_and(|o| o != &change.rel_path && o.to_lowercase() == change.rel_path.to_lowercase());
|
||||
if case_only {
|
||||
let old = old.expect("case_only implies a rename source");
|
||||
let (old_path, new_path) = (local_root.join(old), local_root.join(&change.rel_path));
|
||||
if same_file(&old_path, &new_path) {
|
||||
std::fs::rename(&old_path, &new_path)?;
|
||||
} else {
|
||||
let Some(old_target) = target(old)? else {
|
||||
skipped.push(skip(old));
|
||||
continue;
|
||||
};
|
||||
let task_bytes = std::fs::read(repo.root().join(&change.rel_path)).ok();
|
||||
let at_new = existing(&new_path);
|
||||
if at_new.as_ref().is_none_or(|at_new| at_new.is_some() && *at_new != task_bytes) {
|
||||
skipped.push(skip(&change.rel_path));
|
||||
continue;
|
||||
}
|
||||
write(&change.rel_path, task_bytes)?;
|
||||
write(old, old_target)?;
|
||||
accepted.push(change.rel_path.clone());
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if !apply(&change.rel_path)? {
|
||||
skipped.push(skip(&change.rel_path));
|
||||
continue;
|
||||
}
|
||||
accepted.push(change.rel_path.clone());
|
||||
// A rename also takes the file away from its old path (a case-only rename
|
||||
// is handled above).
|
||||
if !case_only
|
||||
&& let Some(old) = old
|
||||
&& !apply(old)?
|
||||
{
|
||||
skipped.push(skip(old));
|
||||
}
|
||||
}
|
||||
Ok(crate::AgentAcceptOutcome { accepted, skipped })
|
||||
}
|
||||
|
||||
/// Size and modification time of a file, `None` once it is gone.
|
||||
pub(crate) type Stamp = Option<(u64, u128)>;
|
||||
|
||||
@@ -124,7 +542,29 @@ pub(crate) fn stamp(path: &Path) -> Stamp {
|
||||
|
||||
/// The files already changed in `cwd` against `HEAD`, stamped so later edits to them show.
|
||||
pub(crate) fn snapshot(cwd: &Path) -> IdeResult<BTreeMap<String, Stamp>> {
|
||||
Ok(changes(cwd, "HEAD")?.into_iter().map(|c| (c.rel_path, stamp(&c.path))).collect())
|
||||
// Paths and stamps only: no diff and no line counts, which read every untracked file
|
||||
// and held up starting a task in a checkout with many of them.
|
||||
let Some(repo) = Repo::discover(cwd)? else { return Ok(BTreeMap::new()) };
|
||||
Ok(repo.status()?.into_iter().map(|s| (s.rel_path, stamp(&s.path))).collect())
|
||||
}
|
||||
|
||||
/// The files whose state changed between two snapshots of a checkout: edited, new, deleted,
|
||||
/// or back to as committed.
|
||||
pub(crate) fn changed_between(start: &BTreeMap<String, Stamp>, end: &BTreeMap<String, Stamp>) -> Vec<String> {
|
||||
let mut changed: Vec<String> =
|
||||
end.iter().filter(|(path, stamp)| start.get(*path) != Some(*stamp)).map(|(path, _)| path.clone()).collect();
|
||||
changed.extend(start.keys().filter(|path| !end.contains_key(*path)).cloned());
|
||||
changed
|
||||
}
|
||||
|
||||
/// The changes among `changes` that the task made, when it records the files its turns
|
||||
/// changed (`None`: every change counts).
|
||||
pub(crate) fn made_by_task(changes: Vec<AgentChange>, touched: Option<&BTreeSet<String>>) -> Vec<AgentChange> {
|
||||
let Some(touched) = touched else { return changes };
|
||||
changes
|
||||
.into_iter()
|
||||
.filter(|c| touched.contains(&c.rel_path) || c.old_path.as_ref().is_some_and(|old| touched.contains(old)))
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Drops the changes that were there before the task and haven't been touched since.
|
||||
@@ -161,11 +601,11 @@ pub(crate) fn save_before(cwd: &Path, before: &BTreeMap<String, Stamp>, dir: &Pa
|
||||
}
|
||||
|
||||
/// Rolls `change` back: to the copy saved when the task started when the file had changes
|
||||
/// then (`before`, `saved` holding the copies), otherwise to `base` (removed when `base`
|
||||
/// lacks it).
|
||||
/// then (`before`, `saved` holding the copies), otherwise to `rev` (removed when `rev` lacks
|
||||
/// it, or when there is no `rev`: the repository had no commits).
|
||||
pub(crate) fn restore(
|
||||
cwd: &Path,
|
||||
base: &str,
|
||||
rev: Option<&str>,
|
||||
change: &AgentChange,
|
||||
before: &BTreeMap<String, Stamp>,
|
||||
saved: Option<&Path>,
|
||||
@@ -183,10 +623,13 @@ pub(crate) fn restore(
|
||||
})?;
|
||||
Some(std::fs::read(copy)?)
|
||||
}
|
||||
None => {
|
||||
let repo = Repo::discover(cwd)?.ok_or_else(|| IdeError::invalid("not in a git repository"))?;
|
||||
repo.file_bytes_at(path, Some(base))?
|
||||
}
|
||||
None => match rev {
|
||||
Some(rev) => {
|
||||
let repo = Repo::discover(cwd)?.ok_or_else(|| IdeError::invalid("not in a git repository"))?;
|
||||
repo.file_bytes_at(path, Some(rev))?
|
||||
}
|
||||
None => None,
|
||||
},
|
||||
};
|
||||
match content {
|
||||
Some(bytes) => {
|
||||
@@ -204,7 +647,7 @@ pub(crate) fn restore(
|
||||
}
|
||||
|
||||
pub(crate) fn file_at(cwd: &Path, path: &Path, rev: &str) -> IdeResult<Option<String>> {
|
||||
let Some(repo) = Repo::discover(cwd)? else { return Ok(None) };
|
||||
let Some(repo) = discover(cwd)? else { return Ok(None) };
|
||||
let encoding = match ide_vfs::load_text(path) {
|
||||
Ok(loaded) => encoding_rs::Encoding::for_label(loaded.encoding.as_bytes()).unwrap_or(encoding_rs::UTF_8),
|
||||
Err(_) => ide_vfs::detect_encoding(&repo.file_bytes_at(path, Some(rev))?.unwrap_or_default(), None),
|
||||
@@ -313,7 +756,18 @@ mod tests {
|
||||
|
||||
fn git(dir: &Path, args: &[&str]) {
|
||||
let ok = std::process::Command::new("git")
|
||||
.args(["-c", "user.name=t", "-c", "user.email=t@t", "-c", "commit.gpgsign=false"])
|
||||
.args([
|
||||
"-c",
|
||||
"user.name=t",
|
||||
"-c",
|
||||
"user.email=t@t",
|
||||
"-c",
|
||||
"commit.gpgsign=false",
|
||||
// Hermetic line endings: a developer or CI runner with
|
||||
// `core.autocrlf=true` would otherwise check the fixtures out as CRLF.
|
||||
"-c",
|
||||
"core.autocrlf=false",
|
||||
])
|
||||
.args(args)
|
||||
.current_dir(dir)
|
||||
.status()
|
||||
@@ -361,6 +815,88 @@ mod tests {
|
||||
assert!(!created.path.exists());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn commits_since_the_base_are_listed_with_their_files() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let repo = tmp.path().join("repo");
|
||||
std::fs::create_dir(&repo).unwrap();
|
||||
git(&repo, &["init", "-q", "-b", "main"]);
|
||||
std::fs::write(repo.join("a.txt"), "one\n").unwrap();
|
||||
git(&repo, &["add", "."]);
|
||||
git(&repo, &["commit", "-q", "-m", "init"]);
|
||||
|
||||
let created = create(&repo, &tmp.path().join("wt"), "Edit A", None).unwrap();
|
||||
assert!(commits(&created.path, &created.base_commit, &created.base_commit).unwrap().is_empty());
|
||||
|
||||
std::fs::write(created.path.join("a.txt"), "one\ntwo\n").unwrap();
|
||||
git(&created.path, &["add", "."]);
|
||||
git(&created.path, &["commit", "-q", "-m", "change a"]);
|
||||
std::fs::write(created.path.join("b.txt"), "b\n").unwrap();
|
||||
git(&created.path, &["add", "."]);
|
||||
git(&created.path, &["commit", "-q", "-m", "add b"]);
|
||||
|
||||
let head = head_of(&created.path).unwrap().1;
|
||||
let log = commits(&created.path, &created.base_commit, &head).unwrap();
|
||||
assert_eq!(log.iter().map(|c| c.summary.as_str()).collect::<Vec<_>>(), ["add b", "change a"]);
|
||||
assert_eq!(log[0].files.iter().map(|f| f.rel_path.as_str()).collect::<Vec<_>>(), ["b.txt"]);
|
||||
assert_eq!((log[0].files[0].added, log[0].files[0].removed), (1, 0));
|
||||
assert_eq!(log[1].files.iter().map(|f| f.rel_path.as_str()).collect::<Vec<_>>(), ["a.txt"]);
|
||||
assert_eq!((log[1].files[0].added, log[1].files[0].removed), (1, 0));
|
||||
assert!(log.iter().all(|c| c.parent.is_some() && !c.message.is_empty()));
|
||||
|
||||
// Nothing is uncommitted after the commits; editing a committed file brings it back.
|
||||
let at = TaskBase::Worktree { cwd: created.path.clone(), base: created.base_commit.clone() };
|
||||
let grouped = at.grouped_changes(&BTreeMap::new()).unwrap();
|
||||
assert!(grouped.uncommitted.is_empty());
|
||||
assert_eq!(grouped.commits.len(), 2);
|
||||
std::fs::write(created.path.join("a.txt"), "one\ntwo\nthree\n").unwrap();
|
||||
let grouped = at.grouped_changes(&BTreeMap::new()).unwrap();
|
||||
assert_eq!(grouped.uncommitted.iter().map(|c| c.rel_path.as_str()).collect::<Vec<_>>(), ["a.txt"]);
|
||||
assert_eq!(grouped.commits.len(), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_rename_in_a_commit_keeps_its_old_path() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let repo = tmp.path().join("repo");
|
||||
std::fs::create_dir(&repo).unwrap();
|
||||
git(&repo, &["init", "-q", "-b", "main"]);
|
||||
std::fs::write(repo.join("a.txt"), "one\ntwo\nthree\nfour\nfive\n").unwrap();
|
||||
git(&repo, &["add", "."]);
|
||||
git(&repo, &["commit", "-q", "-m", "init"]);
|
||||
let base = head_of(&repo).unwrap().1;
|
||||
|
||||
std::fs::rename(repo.join("a.txt"), repo.join("b.txt")).unwrap();
|
||||
std::fs::write(repo.join("b.txt"), "one\ntwo\nthree\nfour\nFIVE\n").unwrap();
|
||||
git(&repo, &["add", "."]);
|
||||
git(&repo, &["commit", "-q", "-m", "rename a"]);
|
||||
|
||||
let head = head_of(&repo).unwrap().1;
|
||||
let log = commits(&repo, &base, &head).unwrap();
|
||||
assert_eq!(log.len(), 1);
|
||||
assert_eq!(log[0].files.len(), 1);
|
||||
assert_eq!(log[0].files[0].rel_path, "b.txt");
|
||||
assert_eq!(log[0].files[0].old_path.as_deref(), Some("a.txt"));
|
||||
assert_eq!(log[0].files[0].kind, ChangeKind::Renamed);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_local_task_has_no_commits() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let repo = tmp.path().join("repo");
|
||||
std::fs::create_dir(&repo).unwrap();
|
||||
git(&repo, &["init", "-q", "-b", "main"]);
|
||||
std::fs::write(repo.join("a.txt"), "one\n").unwrap();
|
||||
git(&repo, &["add", "."]);
|
||||
git(&repo, &["commit", "-q", "-m", "init"]);
|
||||
std::fs::write(repo.join("a.txt"), "one\ntwo\n").unwrap();
|
||||
|
||||
let at = TaskBase::Local { cwd: repo.clone(), start: None };
|
||||
let grouped = at.grouped_changes(&BTreeMap::new()).unwrap();
|
||||
assert!(grouped.commits.is_empty());
|
||||
assert_eq!(grouped.uncommitted.iter().map(|c| c.rel_path.as_str()).collect::<Vec<_>>(), ["a.txt"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_broken_worktree_registration_still_leaves_a_deletable_folder() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
@@ -391,4 +927,296 @@ mod tests {
|
||||
assert!(remove(&repo, &tmp.path().join("wt"), &elsewhere).is_err());
|
||||
assert!(elsewhere.join("keep.txt").exists());
|
||||
}
|
||||
|
||||
fn accept_repo() -> (tempfile::TempDir, PathBuf, Created) {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let repo = tmp.path().join("repo");
|
||||
std::fs::create_dir(&repo).unwrap();
|
||||
git(&repo, &["init", "-q", "-b", "main"]);
|
||||
// Hermetic line endings: a machine with `core.autocrlf=true` would check
|
||||
// the fixtures and the worktree out as CRLF.
|
||||
git(&repo, &["config", "core.autocrlf", "false"]);
|
||||
std::fs::write(repo.join("a.txt"), "one\ntwo\n").unwrap();
|
||||
std::fs::write(repo.join("gone.txt"), "bye\n").unwrap();
|
||||
git(&repo, &["add", "."]);
|
||||
git(&repo, &["commit", "-q", "-m", "init"]);
|
||||
let created = create(&repo, &tmp.path().join("wt"), "Edit A", None).unwrap();
|
||||
(tmp, repo, created)
|
||||
}
|
||||
|
||||
fn listed(cwd: &Path, base: &str) -> Vec<AgentChange> {
|
||||
without_untouched(changes(cwd, base).unwrap(), &BTreeMap::new())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn accept_moves_a_renamed_file_and_maps_paths_from_a_project_folder() {
|
||||
let (_tmp, repo, created) = accept_repo();
|
||||
std::fs::create_dir(created.path.join("app")).unwrap();
|
||||
git(&created.path, &["mv", "a.txt", "app/moved.txt"]);
|
||||
std::fs::write(created.path.join("app/new.txt"), "n\n").unwrap();
|
||||
git(&created.path, &["add", "."]);
|
||||
git(&created.path, &["-c", "user.name=t", "-c", "user.email=t@t", "commit", "-q", "-m", "move"]);
|
||||
|
||||
// The task's project is the repository's `app` folder.
|
||||
let project = repo.join("app");
|
||||
std::fs::create_dir(&project).unwrap();
|
||||
let changes = listed(&created.path, &created.base_commit);
|
||||
let outcome = accept(&project, &created.path, &created.base_commit, &changes).unwrap();
|
||||
assert!(outcome.skipped.is_empty(), "{:?}", outcome.skipped);
|
||||
assert_eq!(std::fs::read_to_string(repo.join("app/moved.txt")).unwrap(), "one\ntwo\n");
|
||||
assert_eq!(std::fs::read_to_string(repo.join("app/new.txt")).unwrap(), "n\n");
|
||||
assert!(!repo.join("a.txt").exists(), "the rename's old path is gone");
|
||||
assert!(!repo.join("app/app").exists());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn accept_treats_a_crlf_checkout_of_the_base_as_unchanged() {
|
||||
let (_tmp, repo, created) = accept_repo();
|
||||
// The checkout holds the committed text with CRLF line ends, as core.autocrlf leaves it.
|
||||
std::fs::write(repo.join("a.txt"), "one\r\ntwo\r\n").unwrap();
|
||||
std::fs::write(created.path.join("a.txt"), "one\nTWO\n").unwrap();
|
||||
git(&created.path, &["mv", "gone.txt", "kept.txt"]);
|
||||
std::fs::write(repo.join("gone.txt"), "bye\r\n").unwrap();
|
||||
let changes = listed(&created.path, &created.base_commit);
|
||||
let outcome = accept(&repo, &created.path, &created.base_commit, &changes).unwrap();
|
||||
assert!(outcome.skipped.is_empty(), "{:?}", outcome.skipped);
|
||||
assert_eq!(std::fs::read_to_string(repo.join("a.txt")).unwrap(), "one\nTWO\n");
|
||||
assert!(repo.join("kept.txt").exists() && !repo.join("gone.txt").exists());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn accept_applies_a_case_only_rename() {
|
||||
let (_tmp, repo, created) = accept_repo();
|
||||
git(&created.path, &["mv", "a.txt", "A.txt"]);
|
||||
let changes = listed(&created.path, &created.base_commit);
|
||||
let outcome = accept(&repo, &created.path, &created.base_commit, &changes).unwrap();
|
||||
assert!(outcome.skipped.is_empty(), "{:?}", outcome.skipped);
|
||||
let names: Vec<_> = std::fs::read_dir(&repo)
|
||||
.unwrap()
|
||||
.map(|e| e.unwrap().file_name().to_string_lossy().into_owned())
|
||||
.filter(|n| n.eq_ignore_ascii_case("a.txt"))
|
||||
.collect();
|
||||
assert_eq!(names, ["A.txt"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_case_only_rename_keeps_a_file_the_checkout_has_at_the_new_name() {
|
||||
let (_tmp, repo, created) = accept_repo();
|
||||
std::fs::write(repo.join("A.txt"), "mine\n").unwrap();
|
||||
if std::fs::read_to_string(repo.join("a.txt")).unwrap() == "mine\n" {
|
||||
eprintln!("this disk doesn't tell a.txt from A.txt; nothing to check");
|
||||
return;
|
||||
}
|
||||
git(&created.path, &["mv", "a.txt", "A.txt"]);
|
||||
let changes = listed(&created.path, &created.base_commit);
|
||||
let outcome = accept(&repo, &created.path, &created.base_commit, &changes).unwrap();
|
||||
assert_eq!(outcome.skipped.iter().map(|s| s.path.as_str()).collect::<Vec<_>>(), ["A.txt"]);
|
||||
assert_eq!(std::fs::read_to_string(repo.join("A.txt")).unwrap(), "mine\n");
|
||||
assert_eq!(std::fs::read_to_string(repo.join("a.txt")).unwrap(), "one\ntwo\n");
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn accept_leaves_a_checkout_file_it_cannot_read() {
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
let (_tmp, repo, created) = accept_repo();
|
||||
std::fs::write(created.path.join("new.txt"), "task\n").unwrap();
|
||||
std::fs::write(repo.join("new.txt"), "mine\n").unwrap();
|
||||
std::fs::set_permissions(repo.join("new.txt"), std::fs::Permissions::from_mode(0o200)).unwrap();
|
||||
if std::fs::read(repo.join("new.txt")).is_ok() {
|
||||
eprintln!("running as a user that reads any file; nothing to check");
|
||||
return;
|
||||
}
|
||||
let changes = listed(&created.path, &created.base_commit);
|
||||
let outcome = accept(&repo, &created.path, &created.base_commit, &changes).unwrap();
|
||||
std::fs::set_permissions(repo.join("new.txt"), std::fs::Permissions::from_mode(0o644)).unwrap();
|
||||
assert_eq!(outcome.skipped.iter().map(|s| s.path.as_str()).collect::<Vec<_>>(), ["new.txt"]);
|
||||
assert_eq!(std::fs::read_to_string(repo.join("new.txt")).unwrap(), "mine\n");
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
#[test]
|
||||
fn accept_does_not_write_into_the_checkouts_git_folder_through_its_short_name() {
|
||||
let (_tmp, repo, created) = accept_repo();
|
||||
if !repo.join("GIT~1").join("HEAD").is_file() {
|
||||
eprintln!("8.3 short names are disabled on this volume; nothing to check");
|
||||
return;
|
||||
}
|
||||
// Written in the worktree under the name the checkout's `.git` folder also has.
|
||||
let wt = &created.path;
|
||||
std::fs::rename(wt.join(".git"), wt.join("gitfile")).unwrap();
|
||||
std::fs::create_dir_all(wt.join("GIT~1").join("hooks")).unwrap();
|
||||
std::fs::write(wt.join("GIT~1").join("hooks").join("pre-commit"), "#!/bin/sh\nexit 0\n").unwrap();
|
||||
std::fs::rename(wt.join("gitfile"), wt.join(".git")).unwrap();
|
||||
std::fs::write(wt.join("a.txt"), "one\nTWO\n").unwrap();
|
||||
|
||||
let changes = listed(wt, &created.base_commit);
|
||||
assert!(changes.iter().any(|c| c.rel_path == "GIT~1/hooks/pre-commit"), "control: git lists it");
|
||||
let outcome = accept(&repo, wt, &created.base_commit, &changes).unwrap();
|
||||
assert!(!repo.join(".git").join("hooks").join("pre-commit").exists(), "{outcome:?}");
|
||||
assert_eq!(outcome.accepted, ["a.txt"]);
|
||||
assert_eq!(outcome.skipped.iter().map(|s| s.path.as_str()).collect::<Vec<_>>(), ["GIT~1/hooks/pre-commit"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn accept_refuses_paths_that_lead_into_git_metadata_under_any_spelling() {
|
||||
let (tmp, repo, _created) = accept_repo();
|
||||
let checkout = Confined::new(&repo, Repo::discover(&repo).unwrap().as_ref()).unwrap();
|
||||
for rel in [
|
||||
".git/hooks/pre-commit",
|
||||
".GIT/hooks/pre-commit",
|
||||
".git./hooks/pre-commit",
|
||||
".git /hooks/pre-commit",
|
||||
".git::$INDEX_ALLOCATION/hooks/pre-commit",
|
||||
"sub/.git/config",
|
||||
"../outside.txt",
|
||||
] {
|
||||
assert!(!checkout.allows(rel), "{rel}");
|
||||
}
|
||||
assert!(checkout.allows("src/new.txt"));
|
||||
assert!(checkout.allows(".github/workflows/ci.yml"));
|
||||
|
||||
// git's folder under another name inside the tree (`--separate-git-dir`).
|
||||
let separate = tmp.path().join("separate");
|
||||
std::fs::create_dir(&separate).unwrap();
|
||||
git(&separate, &["init", "-q", "--separate-git-dir", "meta", "-b", "main"]);
|
||||
let side = Confined::new(&separate, Repo::discover(&separate).unwrap().as_ref()).unwrap();
|
||||
assert!(!side.allows("meta/hooks/pre-commit"));
|
||||
assert!(side.allows("metadata.txt"));
|
||||
}
|
||||
|
||||
/// A link the task left in its worktree, or a linked folder in the checkout: neither is followed.
|
||||
#[test]
|
||||
fn accept_does_not_follow_links_on_either_side() {
|
||||
let (tmp, repo, created) = accept_repo();
|
||||
let outside = tmp.path().join("outside");
|
||||
std::fs::create_dir(&outside).unwrap();
|
||||
std::fs::write(outside.join("secret.txt"), "secret\n").unwrap();
|
||||
// A folder link in the checkout (a junction on Windows, which needs no privilege).
|
||||
#[cfg(unix)]
|
||||
std::os::unix::fs::symlink(&outside, repo.join("linked")).unwrap();
|
||||
#[cfg(windows)]
|
||||
assert!(
|
||||
std::process::Command::new("cmd")
|
||||
.args(["/c", "mklink", "/J"])
|
||||
.arg(repo.join("linked"))
|
||||
.arg(&outside)
|
||||
.output()
|
||||
.unwrap()
|
||||
.status
|
||||
.success()
|
||||
);
|
||||
std::fs::create_dir(created.path.join("linked")).unwrap();
|
||||
std::fs::write(created.path.join("linked/new.txt"), "x\n").unwrap();
|
||||
std::fs::write(created.path.join("a.txt"), "one\nTWO\n").unwrap();
|
||||
// A file link in the worktree, where the platform lets this user create one.
|
||||
#[cfg(unix)]
|
||||
let file_link = std::os::unix::fs::symlink(outside.join("secret.txt"), created.path.join("copied.txt"));
|
||||
#[cfg(windows)]
|
||||
let file_link = std::os::windows::fs::symlink_file(outside.join("secret.txt"), created.path.join("copied.txt"));
|
||||
if let Err(e) = &file_link {
|
||||
eprintln!("no file link ({e}); checking the folder link only");
|
||||
}
|
||||
|
||||
let changes: Vec<_> = listed(&created.path, &created.base_commit);
|
||||
let outcome = accept(&repo, &created.path, &created.base_commit, &changes).unwrap();
|
||||
assert!(!outside.join("new.txt").exists(), "{outcome:?}");
|
||||
assert!(!repo.join("copied.txt").exists(), "{outcome:?}");
|
||||
assert!(outcome.accepted.contains(&"a.txt".to_owned()));
|
||||
let refused: Vec<_> = outcome.skipped.iter().map(|s| s.path.as_str()).collect();
|
||||
if file_link.is_ok() {
|
||||
assert_eq!(refused, ["copied.txt", "linked/new.txt"]);
|
||||
} else {
|
||||
assert_eq!(refused, ["linked/new.txt"]);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn accept_copies_new_modified_and_deleted_files_into_the_checkout() {
|
||||
let (_tmp, repo, created) = accept_repo();
|
||||
std::fs::write(created.path.join("a.txt"), "one\nTWO\n").unwrap();
|
||||
std::fs::write(created.path.join("new.txt"), "x\n").unwrap();
|
||||
std::fs::remove_file(created.path.join("gone.txt")).unwrap();
|
||||
|
||||
let outcome =
|
||||
accept(&repo, &created.path, &created.base_commit, &listed(&created.path, &created.base_commit)).unwrap();
|
||||
assert!(outcome.skipped.is_empty());
|
||||
assert_eq!(outcome.accepted, ["a.txt", "gone.txt", "new.txt"]);
|
||||
assert_eq!(std::fs::read_to_string(repo.join("a.txt")).unwrap(), "one\nTWO\n");
|
||||
assert_eq!(std::fs::read_to_string(repo.join("new.txt")).unwrap(), "x\n");
|
||||
assert!(!repo.join("gone.txt").exists());
|
||||
|
||||
// Accepting again changes nothing: the checkout already matches.
|
||||
let again =
|
||||
accept(&repo, &created.path, &created.base_commit, &listed(&created.path, &created.base_commit)).unwrap();
|
||||
assert!(again.skipped.is_empty());
|
||||
assert_eq!(again.accepted, ["a.txt", "gone.txt", "new.txt"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_local_task_goes_back_to_its_start_unless_the_user_committed_on_top_of_it() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let repo = tmp.path().join("repo");
|
||||
std::fs::create_dir(&repo).unwrap();
|
||||
git(&repo, &["init", "-q", "-b", "main"]);
|
||||
let local = |start: Option<&str>| TaskBase::Local { cwd: repo.clone(), start: start.map(str::to_owned) };
|
||||
let head = || head_of(&repo).unwrap().1;
|
||||
let commit = |name: &str| {
|
||||
std::fs::write(repo.join(name), "x\n").unwrap();
|
||||
git(&repo, &["add", "."]);
|
||||
git(&repo, &["commit", "-q", "-m", name]);
|
||||
head_of(&repo).unwrap().1
|
||||
};
|
||||
|
||||
assert_eq!(local(None).rev().unwrap(), None, "no commits yet");
|
||||
let first = commit("a.txt");
|
||||
assert_eq!(local(None).rev().unwrap(), Some(first.clone()), "an imported session: HEAD");
|
||||
let start = commit("b.txt");
|
||||
assert_eq!(local(Some(&start)).rev().unwrap(), Some(start.clone()), "HEAD where it started");
|
||||
let ahead = commit("c.txt");
|
||||
assert_eq!(local(Some(&start)).rev().unwrap(), Some(ahead), "committed on top: HEAD");
|
||||
git(&repo, &["reset", "-q", "--soft", &first]);
|
||||
assert_eq!(local(Some(&start)).rev().unwrap(), Some(start.clone()), "moved back: the start");
|
||||
git(&repo, &["checkout", "-q", "-b", "side"]);
|
||||
commit("d.txt");
|
||||
assert_eq!(local(Some(&start)).rev().unwrap(), Some(start.clone()), "another branch: the start");
|
||||
let gone = "0123456789abcdef0123456789abcdef01234567";
|
||||
assert_eq!(local(Some(gone)).rev().unwrap(), Some(head()), "an unknown start: HEAD");
|
||||
let worktree = TaskBase::Worktree { cwd: repo.clone(), base: start.clone() };
|
||||
assert_eq!(worktree.rev().unwrap(), Some(start));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn accept_leaves_files_the_checkout_changed_itself() {
|
||||
let (_tmp, repo, created) = accept_repo();
|
||||
std::fs::write(created.path.join("a.txt"), "one\nTWO\n").unwrap();
|
||||
std::fs::write(created.path.join("gone.txt"), "bye\nbye\n").unwrap();
|
||||
std::fs::write(repo.join("a.txt"), "one\nMINE\n").unwrap();
|
||||
|
||||
let outcome =
|
||||
accept(&repo, &created.path, &created.base_commit, &listed(&created.path, &created.base_commit)).unwrap();
|
||||
assert_eq!(outcome.accepted, ["gone.txt"]);
|
||||
assert_eq!(outcome.skipped.len(), 1);
|
||||
assert_eq!(outcome.skipped[0].path, "a.txt");
|
||||
assert!(!outcome.skipped[0].reason.is_empty());
|
||||
// The user's own edit survives; the other file comes over.
|
||||
assert_eq!(std::fs::read_to_string(repo.join("a.txt")).unwrap(), "one\nMINE\n");
|
||||
assert_eq!(std::fs::read_to_string(repo.join("gone.txt")).unwrap(), "bye\nbye\n");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_turn_changed_the_files_whose_state_moved() {
|
||||
let start: BTreeMap<String, Stamp> =
|
||||
[("edited", Some((1, 1))), ("same", Some((2, 2))), ("reverted", Some((3, 3)))]
|
||||
.into_iter()
|
||||
.map(|(p, s)| (p.to_owned(), s))
|
||||
.collect();
|
||||
let end: BTreeMap<String, Stamp> = [("edited", Some((1, 9))), ("same", Some((2, 2))), ("new", Some((4, 4)))]
|
||||
.into_iter()
|
||||
.map(|(p, s)| (p.to_owned(), s))
|
||||
.collect();
|
||||
let mut changed = changed_between(&start, &end);
|
||||
changed.sort();
|
||||
assert_eq!(changed, ["edited", "new", "reverted"]);
|
||||
}
|
||||
}
|
||||
@@ -79,23 +79,26 @@ fn scan(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// `None` means an input cannot be resolved safely: build normally without reuse.
|
||||
/// All filesystem work belongs on the caller's blocking thread.
|
||||
pub fn launch_fingerprint(
|
||||
/// One planned step as fingerprinted: resolved program, args, cwd and env.
|
||||
type StepFingerprint = (PathBuf, Vec<String>, PathBuf, BTreeMap<String, String>);
|
||||
|
||||
/// Environment, planned steps and input paths shared by every Maven build
|
||||
/// fingerprint: the filtered process environment plus the wrapper, settings
|
||||
/// and JDK files the planned invocation reads.
|
||||
struct Collected {
|
||||
inputs: Vec<PathBuf>,
|
||||
environment: BTreeMap<String, String>,
|
||||
steps: Vec<StepFingerprint>,
|
||||
inherited_hash: u64,
|
||||
}
|
||||
|
||||
fn collect(
|
||||
root: &Path,
|
||||
model: &BuildModel,
|
||||
module: &Path,
|
||||
test: bool,
|
||||
projects: &[&maven::MavenProject],
|
||||
plan: &BuildPlan,
|
||||
local_repo: Option<PathBuf>,
|
||||
cancel: &CancellationToken,
|
||||
) -> IdeResult<Option<LaunchFingerprint>> {
|
||||
let Some(m) = &model.maven else { return Ok(None) };
|
||||
let projects = maven_launch_projects(root, m, module, test);
|
||||
if projects.is_empty() {
|
||||
return Ok(None);
|
||||
}
|
||||
let Some(mut inputs) = maven::launch_inputs(root, &projects, local_repo) else { return Ok(None) };
|
||||
let plan = launch_plan(root, model, module, test, BuildKind::Build)?;
|
||||
) -> IdeResult<Option<Collected>> {
|
||||
let Some(mut inputs) = maven::launch_inputs(root, projects, local_repo) else { return Ok(None) };
|
||||
let mut environment: BTreeMap<String, String> = std::env::vars()
|
||||
.map(|(key, value)| (key.to_ascii_uppercase(), value))
|
||||
.filter(|(key, _)| {
|
||||
@@ -165,26 +168,111 @@ pub fn launch_fingerprint(
|
||||
Path::new(home).join(if cfg!(windows) { "bin/java.exe" } else { "bin/java" }),
|
||||
]);
|
||||
}
|
||||
let steps: Vec<_> =
|
||||
plan.steps.iter().map(|s| (process::program_path(&s.program, &s.env), &s.args, &s.cwd, &s.env)).collect();
|
||||
let steps = plan
|
||||
.steps
|
||||
.iter()
|
||||
.map(|s| (process::program_path(&s.program, &s.env), s.args.clone(), s.cwd.clone(), s.env.clone()))
|
||||
.collect();
|
||||
// Profiles and plugins can read arbitrary environment variables. Keep a signature
|
||||
// of the inherited environment without persisting unrelated credentials.
|
||||
let inherited: BTreeMap<_, _> = std::env::vars_os().collect();
|
||||
let mut inherited_hash = std::hash::DefaultHasher::new();
|
||||
inherited.hash(&mut inherited_hash);
|
||||
let parameters =
|
||||
serde_json::to_string(&(root, module, test, steps, environment, inherited_hash.finish(), projects))
|
||||
.map_err(|e| IdeError::internal(e.to_string()))?;
|
||||
Ok(Some(Collected { inputs, environment, steps, inherited_hash: inherited_hash.finish() }))
|
||||
}
|
||||
|
||||
/// Scans every input, storing project inputs relative to the project.
|
||||
fn scan_all(
|
||||
root: &Path,
|
||||
inputs: Vec<PathBuf>,
|
||||
cancel: &CancellationToken,
|
||||
) -> IdeResult<BTreeMap<PathBuf, Option<Stamp>>> {
|
||||
let mut files = BTreeMap::new();
|
||||
for path in inputs {
|
||||
scan(root, &path, &mut files, &mut HashSet::new(), cancel)?;
|
||||
}
|
||||
// Store project inputs relative to the project, including each file's relative path.
|
||||
let files = files.into_iter().map(|(p, stamp)| (p.strip_prefix(root).unwrap_or(&p).to_path_buf(), stamp)).collect();
|
||||
Ok(Some(LaunchFingerprint { version: 2, parameters, files }))
|
||||
Ok(files.into_iter().map(|(p, stamp)| (p.strip_prefix(root).unwrap_or(&p).to_path_buf(), stamp)).collect())
|
||||
}
|
||||
|
||||
/// `None` means an input cannot be resolved safely: build normally without reuse.
|
||||
/// All filesystem work belongs on the caller's blocking thread.
|
||||
pub fn launch_fingerprint(
|
||||
root: &Path,
|
||||
model: &BuildModel,
|
||||
module: &Path,
|
||||
test: bool,
|
||||
local_repo: Option<PathBuf>,
|
||||
cancel: &CancellationToken,
|
||||
) -> IdeResult<Option<LaunchFingerprint>> {
|
||||
let Some(m) = &model.maven else { return Ok(None) };
|
||||
let projects = maven_launch_projects(root, m, module, test);
|
||||
if projects.is_empty() {
|
||||
return Ok(None);
|
||||
}
|
||||
let plan = launch_plan(root, model, module, test, BuildKind::Build)?;
|
||||
let Some(collected) = collect(root, &projects, &plan, local_repo)? else { return Ok(None) };
|
||||
let Collected { inputs, environment, steps, inherited_hash } = collected;
|
||||
let parameters = serde_json::to_string(&(root, module, test, steps, environment, inherited_hash, projects))
|
||||
.map_err(|e| IdeError::internal(e.to_string()))?;
|
||||
Ok(Some(LaunchFingerprint { version: 2, parameters, files: scan_all(root, inputs, cancel)? }))
|
||||
}
|
||||
|
||||
/// Inputs fingerprint of a menu build (`Build Project` / `Build Module`): the
|
||||
/// built modules' sources, resources and POM chain plus the exact planned
|
||||
/// steps, so an unchanged project skips Maven entirely (#2476). Only `Build`
|
||||
/// callers reuse it, never `Rebuild` or `Artifacts`; `None` disables reuse.
|
||||
/// All filesystem work belongs on the caller's blocking thread.
|
||||
pub fn menu_fingerprint(
|
||||
root: &Path,
|
||||
model: &BuildModel,
|
||||
file: Option<&Path>,
|
||||
cancel: &CancellationToken,
|
||||
) -> IdeResult<Option<LaunchFingerprint>> {
|
||||
let plan = plan(root, model, BuildKind::Build, file, None)?;
|
||||
if plan.system != BuildSystem::Maven {
|
||||
return Ok(None);
|
||||
}
|
||||
let Some(m) = &model.maven else { return Ok(None) };
|
||||
let projects: Vec<&maven::MavenProject> = match file {
|
||||
Some(file) => {
|
||||
// The innermost linked project owning the file, as in single-file compilation.
|
||||
let rel = rel_str(root, file);
|
||||
let module = m
|
||||
.projects
|
||||
.iter()
|
||||
.filter(|p| rel == p.dir || rel.starts_with(&format!("{}/", p.dir)) || p.dir.is_empty())
|
||||
.max_by_key(|p| p.dir.len());
|
||||
match module {
|
||||
Some(project) if project.packaging != "pom" => {
|
||||
maven_launch_projects(root, m, &subdir(root, &project.dir), true)
|
||||
}
|
||||
_ => return Ok(None),
|
||||
}
|
||||
}
|
||||
// A project build walks every linked project.
|
||||
None => m.projects.iter().collect(),
|
||||
};
|
||||
if projects.is_empty() {
|
||||
return Ok(None);
|
||||
}
|
||||
let Some(collected) = collect(root, &projects, &plan, None)? else { return Ok(None) };
|
||||
let Collected { inputs, environment, steps, inherited_hash } = collected;
|
||||
let scope = file.map(|f| rel_str(root, f));
|
||||
let parameters = serde_json::to_string(&("menu-build", scope, steps, environment, inherited_hash))
|
||||
.map_err(|e| IdeError::internal(e.to_string()))?;
|
||||
Ok(Some(LaunchFingerprint { version: 2, parameters, files: scan_all(root, inputs, cancel)? }))
|
||||
}
|
||||
|
||||
impl LaunchFingerprint {
|
||||
/// Folders (relative to the project) of the Maven projects whose POM is an input.
|
||||
pub(super) fn project_dirs(&self) -> impl Iterator<Item = &Path> {
|
||||
self.files
|
||||
.keys()
|
||||
.filter(|p| p.is_relative() && p.file_name().is_some_and(|n| n == "pom.xml"))
|
||||
.map(|pom| pom.parent().unwrap_or(Path::new("")))
|
||||
}
|
||||
|
||||
/// Relative paths whose content changed while `(length, mtime)` stayed the same
|
||||
/// compared to `previous` (timestamp-preserving external edits, coarse-granularity
|
||||
/// filesystems). Timestamp-incremental builders (Maven, the Kotlin plugin) would
|
||||
|
||||
@@ -0,0 +1,253 @@
|
||||
//! Resources a Gradle build changes while copying them (filters, `expand`, renames, extra
|
||||
//! sources). The Java language server copies them as they are into Buildship's
|
||||
//! `bin/<source set>` folders, which launches run from; IntelliJ IDEA runs Gradle's copy.
|
||||
//! Before a launch, Gradle processes them and its output replaces those plain copies, so
|
||||
//! each resource is on the classpath once, as the build produces it.
|
||||
use super::{BuildPlan, BuildStep, BuildSystem, Format, run, subdir};
|
||||
use crate::BuildModel;
|
||||
use crate::gradle::{self, GradleModel, ReportedProject};
|
||||
use ide_core::{CancellationToken, IdeError, IdeResult, Severity};
|
||||
use std::fs;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
/// A source set whose resources Gradle processes, with the folder the launch reads them from.
|
||||
#[derive(Debug, PartialEq)]
|
||||
struct Target {
|
||||
/// Task path from the root build (`:app:processResources`).
|
||||
task: String,
|
||||
/// Where the task writes the resources.
|
||||
output: PathBuf,
|
||||
/// The Java language server's output folder of the source set, on the launch classpath.
|
||||
jdtls: PathBuf,
|
||||
}
|
||||
|
||||
fn targets(reported: &[ReportedProject], class_paths: &[String]) -> Vec<Target> {
|
||||
// buildSrc is built before the build, never addressable as `:buildSrc:<task>`.
|
||||
let addressable = |p: &&ReportedProject| !p.build.as_deref().is_some_and(|b| b.ends_with(":buildSrc"));
|
||||
let sets: Vec<_> = reported
|
||||
.iter()
|
||||
.filter(addressable)
|
||||
.flat_map(|p| p.source_sets.iter().filter_map(move |s| Some((p, s, s.processed.as_ref()?))))
|
||||
.collect();
|
||||
if sets.is_empty() {
|
||||
return Vec::new();
|
||||
}
|
||||
// Canonical, as Gradle reports its folders: jdtls may spell them differently.
|
||||
let entries: Vec<PathBuf> = class_paths.iter().filter_map(|p| dunce::canonicalize(p).ok()).collect();
|
||||
sets.into_iter()
|
||||
.filter_map(|(p, set, resources)| {
|
||||
// Buildship's output folder of the source set.
|
||||
let jdtls = dunce::canonicalize(Path::new(&p.dir).join("bin").join(&set.name)).ok()?;
|
||||
let project = if p.path == ":" { "" } else { p.path.as_str() };
|
||||
entries.contains(&jdtls).then(|| Target {
|
||||
task: format!("{project}:{}", resources.task),
|
||||
output: PathBuf::from(&resources.dir),
|
||||
jdtls,
|
||||
})
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Before a JVM launch from the Java language server's output folders on `class_paths`:
|
||||
/// runs the Gradle tasks that process resources of those folders' source sets (see the
|
||||
/// module docs) and copies their output over the plain copies. `cache_dir` holds the
|
||||
/// linked builds' last syncs. Returns the tasks run, none for builds that copy their
|
||||
/// resources as they are.
|
||||
pub fn process_launch_resources(
|
||||
root: &Path,
|
||||
model: &BuildModel,
|
||||
cache_dir: &Path,
|
||||
class_paths: &[String],
|
||||
cancel: &CancellationToken,
|
||||
) -> IdeResult<Vec<String>> {
|
||||
// A composite also reports the builds it includes: the deepest build owning a folder runs its task.
|
||||
let mut builds: Vec<&GradleModel> = model.gradle.iter().collect();
|
||||
builds.sort_by_key(|g| std::cmp::Reverse(subdir(root, &g.dir).components().count()));
|
||||
let mut done: Vec<PathBuf> = Vec::new();
|
||||
let mut tasks = Vec::new();
|
||||
for g in builds {
|
||||
let reported = crate::sync::reported_gradle_projects(&gradle::cache_dir(cache_dir, &g.dir));
|
||||
let targets: Vec<Target> =
|
||||
targets(&reported, class_paths).into_iter().filter(|t| !done.contains(&t.jdtls)).collect();
|
||||
if targets.is_empty() {
|
||||
continue;
|
||||
}
|
||||
let mut step = BuildStep::new(&g.command, &["--console=plain"], &subdir(root, &g.dir), Format::Gradle);
|
||||
step.args.extend(targets.iter().map(|t| t.task.clone()));
|
||||
step.env = g.env.clone();
|
||||
let plan = BuildPlan { system: BuildSystem::Gradle, title: "Process resources".into(), steps: vec![step] };
|
||||
let result = run(root, &plan, cancel, &|_| {})?;
|
||||
if !result.success {
|
||||
let errors: Vec<&str> = result
|
||||
.messages
|
||||
.iter()
|
||||
.filter(|m| m.severity == Severity::Error)
|
||||
.take(5)
|
||||
.map(|m| m.text.as_str())
|
||||
.collect();
|
||||
return Err(IdeError::internal(errors.join("\n")));
|
||||
}
|
||||
for t in targets {
|
||||
if cancel.is_cancelled() {
|
||||
return Err(IdeError::cancelled());
|
||||
}
|
||||
overlay(&t.output, &t.output, &t.jdtls)?;
|
||||
done.push(t.jdtls);
|
||||
tasks.push(t.task);
|
||||
}
|
||||
}
|
||||
Ok(tasks)
|
||||
}
|
||||
|
||||
/// Copies the files under `dir` to the same place under `to` (relative to `base`) where
|
||||
/// they differ, so unchanged resources are not rewritten on every launch.
|
||||
fn overlay(base: &Path, dir: &Path, to: &Path) -> IdeResult<()> {
|
||||
let entries = match fs::read_dir(dir) {
|
||||
// No resources: the task had nothing to copy.
|
||||
Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(()),
|
||||
entries => entries?,
|
||||
};
|
||||
for entry in entries {
|
||||
let entry = entry?;
|
||||
let path = entry.path();
|
||||
let kind = entry.file_type()?;
|
||||
if kind.is_dir() {
|
||||
overlay(base, &path, to)?;
|
||||
} else if kind.is_file() {
|
||||
let dest = to.join(path.strip_prefix(base).map_err(|e| IdeError::internal(e.to_string()))?);
|
||||
if !same_content(&path, &dest) {
|
||||
if let Some(parent) = dest.parent() {
|
||||
fs::create_dir_all(parent)?;
|
||||
}
|
||||
fs::copy(&path, &dest)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn same_content(a: &Path, b: &Path) -> bool {
|
||||
match (fs::metadata(a), fs::metadata(b)) {
|
||||
(Ok(m), Ok(n)) if m.len() == n.len() => matches!((fs::read(a), fs::read(b)), (Ok(x), Ok(y)) if x == y),
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::gradle::{ProcessedResources, ReportedSourceSet};
|
||||
|
||||
fn write(path: &Path, text: &str) {
|
||||
fs::create_dir_all(path.parent().unwrap()).unwrap();
|
||||
fs::write(path, text).unwrap();
|
||||
}
|
||||
|
||||
fn set(name: &str, processed: Option<&Path>) -> ReportedSourceSet {
|
||||
ReportedSourceSet {
|
||||
name: name.into(),
|
||||
test: name == "test",
|
||||
processed: processed.map(|dir| ProcessedResources {
|
||||
task: if name == "main" { "processResources".into() } else { "processTestResources".into() },
|
||||
dir: dir.to_string_lossy().into_owned(),
|
||||
}),
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
fn project(path: &str, dir: &Path, sets: Vec<ReportedSourceSet>) -> ReportedProject {
|
||||
ReportedProject {
|
||||
path: path.into(),
|
||||
dir: dir.to_string_lossy().into_owned(),
|
||||
source_sets: sets,
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
/// Only processed source sets whose jdtls output is on the classpath: the launched
|
||||
/// module, upstream modules of the build, tests only for a test classpath; never buildSrc.
|
||||
#[test]
|
||||
fn targets_are_processed_sets_on_the_classpath() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let root = dunce::canonicalize(tmp.path()).unwrap();
|
||||
let (app, lib, plain, src) = (root.join("app"), root.join("lib"), root.join("plain"), root.join("buildSrc"));
|
||||
for dir in [&root, &app, &lib, &plain, &src] {
|
||||
for set in ["main", "test"] {
|
||||
fs::create_dir_all(dir.join("bin").join(set)).unwrap();
|
||||
}
|
||||
}
|
||||
let out = |dir: &Path, set: &str| dir.join("build/resources").join(set);
|
||||
let mut build_src = project(":buildSrc", &src, vec![set("main", Some(&out(&src, "main")))]);
|
||||
build_src.build = Some(":buildSrc".into());
|
||||
let reported = [
|
||||
project(":", &root, vec![set("main", Some(&out(&root, "main")))]),
|
||||
project(":app", &app, vec![set("main", Some(&out(&app, "main"))), set("test", Some(&out(&app, "test")))]),
|
||||
project(":lib", &lib, vec![set("main", Some(&out(&lib, "main")))]),
|
||||
project(":plain", &plain, vec![set("main", None)]),
|
||||
build_src,
|
||||
];
|
||||
let entry = |dir: &Path, set: &str| dir.join("bin").join(set).to_string_lossy().into_owned();
|
||||
let runtime = [
|
||||
entry(&app, "main"),
|
||||
// Spelled differently from Gradle's canonical folders.
|
||||
format!("{}/", entry(&lib, "main")),
|
||||
entry(&plain, "main"),
|
||||
entry(&src, "main"),
|
||||
"/m2/spring-core.jar".into(),
|
||||
];
|
||||
let tasks = |cp: &[String]| targets(&reported, cp).into_iter().map(|t| t.task).collect::<Vec<_>>();
|
||||
assert_eq!(tasks(&runtime), [":app:processResources", ":lib:processResources"]);
|
||||
let mut test = runtime.to_vec();
|
||||
test.insert(0, entry(&app, "test"));
|
||||
assert_eq!(tasks(&test), [":app:processResources", ":app:processTestResources", ":lib:processResources"]);
|
||||
assert_eq!(tasks(&[entry(&root, "main")]), [":processResources"], "the root project");
|
||||
let app_targets = targets(&reported, &runtime);
|
||||
let t = &app_targets[0];
|
||||
assert_eq!(
|
||||
(t.output.as_path(), t.jdtls.as_path()),
|
||||
(out(&app, "main").as_path(), app.join("bin/main").as_path())
|
||||
);
|
||||
|
||||
let unprocessed = [project(":app", &app, vec![set("main", None)])];
|
||||
assert!(targets(&unprocessed, &runtime).is_empty());
|
||||
}
|
||||
|
||||
/// The processed copy replaces jdtls's plain one (the reported `@version@` case), new
|
||||
/// files are added, identical ones are left alone and files Gradle doesn't produce stay.
|
||||
#[test]
|
||||
fn overlay_replaces_plain_copies_with_gradle_output() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let (output, jdtls) = (tmp.path().join("build/resources/main"), tmp.path().join("bin/main"));
|
||||
write(&output.join("application.yaml"), "spring:\n application:\n version: 0.0.1\n");
|
||||
write(&output.join("static/extra.txt"), "from an extra source");
|
||||
write(&output.join("migrations/V1__init.sql"), "create table t (id int);");
|
||||
write(&jdtls.join("application.yaml"), "spring:\n application:\n version: @version@\n");
|
||||
write(&jdtls.join("migrations/V1__init.sql"), "create table t (id int);");
|
||||
write(&jdtls.join("com/example/App.class"), "bytecode");
|
||||
let unchanged = fs::metadata(jdtls.join("migrations/V1__init.sql")).unwrap().modified().unwrap();
|
||||
std::thread::sleep(std::time::Duration::from_millis(20));
|
||||
|
||||
overlay(&output, &output, &jdtls).unwrap();
|
||||
let read = |rel: &str| fs::read_to_string(jdtls.join(rel)).unwrap();
|
||||
assert_eq!(read("application.yaml"), "spring:\n application:\n version: 0.0.1\n");
|
||||
assert_eq!(read("static/extra.txt"), "from an extra source");
|
||||
assert_eq!(read("com/example/App.class"), "bytecode");
|
||||
let modified = fs::metadata(jdtls.join("migrations/V1__init.sql")).unwrap().modified().unwrap();
|
||||
assert_eq!(modified, unchanged, "identical files are not rewritten");
|
||||
|
||||
overlay(&tmp.path().join("missing"), &tmp.path().join("missing"), &jdtls).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn builds_without_processed_resources_run_nothing() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
// Running this command would fail the call.
|
||||
let missing = tmp.path().join("missing-gradle").to_string_lossy().into_owned();
|
||||
let model =
|
||||
BuildModel { gradle: vec![GradleModel { command: missing, ..Default::default() }], ..Default::default() };
|
||||
let class_paths = [tmp.path().join("bin/main").to_string_lossy().into_owned()];
|
||||
let tasks =
|
||||
process_launch_resources(tmp.path(), &model, tmp.path(), &class_paths, &CancellationToken::new()).unwrap();
|
||||
assert!(tasks.is_empty());
|
||||
}
|
||||
}
|
||||
@@ -4,9 +4,23 @@
|
||||
//! for the Build tool window, the Problems view and the editor.
|
||||
|
||||
mod fingerprint;
|
||||
mod launch_resources;
|
||||
mod parse;
|
||||
mod single;
|
||||
mod structure;
|
||||
mod up_to_date;
|
||||
|
||||
pub use fingerprint::{LaunchFingerprint, launch_fingerprint};
|
||||
pub use fingerprint::{LaunchFingerprint, launch_fingerprint, menu_fingerprint};
|
||||
pub use launch_resources::process_launch_resources;
|
||||
pub use single::{
|
||||
SingleFileTarget, compile_file_plan, find_javac, javac_step, module_classpath, single_file_classpath,
|
||||
single_file_target,
|
||||
};
|
||||
pub use structure::{
|
||||
StructureJdk, StructureRoots, artifact_file, artifact_output, language_release, structure_outputs, structure_plan,
|
||||
structure_war_plan, structure_web_artifacts,
|
||||
};
|
||||
pub use up_to_date::{menu_up_to_date, record_menu_build};
|
||||
|
||||
use crate::process::{self, Stream};
|
||||
use crate::{BuildModel, maven};
|
||||
@@ -44,6 +58,10 @@ pub enum BuildSystem {
|
||||
TypeScript,
|
||||
#[serde(rename = "cmake")]
|
||||
CMake,
|
||||
#[serde(rename = "dotnet")]
|
||||
Dotnet,
|
||||
/// A Java project without a build tool, compiled from its Project Structure.
|
||||
Javac,
|
||||
}
|
||||
|
||||
/// One command of a build.
|
||||
@@ -54,6 +72,10 @@ pub struct BuildStep {
|
||||
pub cwd: PathBuf,
|
||||
pub env: BTreeMap<String, String>,
|
||||
format: Format,
|
||||
/// Folders created before the command runs (a compiler's output).
|
||||
mkdirs: Vec<PathBuf>,
|
||||
/// Work done in the IDE instead of a command, with its console title.
|
||||
action: Option<(structure::Action, String)>,
|
||||
}
|
||||
|
||||
impl BuildStep {
|
||||
@@ -64,16 +86,26 @@ impl BuildStep {
|
||||
cwd: cwd.to_path_buf(),
|
||||
env: BTreeMap::new(),
|
||||
format,
|
||||
mkdirs: Vec::new(),
|
||||
action: None,
|
||||
}
|
||||
}
|
||||
|
||||
fn action(action: structure::Action, root: &Path) -> Self {
|
||||
let title = action.display(root);
|
||||
BuildStep { action: Some((action, title)), ..Self::new("", &[], root, Format::Gradle) }
|
||||
}
|
||||
|
||||
/// A CMake configure/build invocation, parsed as compiler output in the Build view.
|
||||
pub fn cmake(program: &str, args: Vec<String>, cwd: PathBuf, env: BTreeMap<String, String>) -> Self {
|
||||
Self { program: program.into(), args, cwd, env, format: Format::Gcc }
|
||||
Self { program: program.into(), args, cwd, env, format: Format::Gcc, mkdirs: Vec::new(), action: None }
|
||||
}
|
||||
|
||||
/// The command line as echoed in the console.
|
||||
pub fn display(&self) -> String {
|
||||
if let Some((_, title)) = &self.action {
|
||||
return title.clone();
|
||||
}
|
||||
let program = Path::new(&self.program).file_name().map_or(self.program.clone(), |n| n.to_string_lossy().into());
|
||||
std::iter::once(program)
|
||||
.chain(self.args.iter().map(|a| if a.contains(' ') { format!("\"{a}\"") } else { a.clone() }))
|
||||
@@ -156,6 +188,8 @@ fn detect_dir(dir: &Path, model: &BuildModel, root: &Path) -> Option<BuildSystem
|
||||
|| has(dir, &["build.gradle", "build.gradle.kts", "settings.gradle", "settings.gradle.kts"])
|
||||
{
|
||||
Some(BuildSystem::Gradle)
|
||||
} else if dotnet_target(dir).is_some() {
|
||||
Some(BuildSystem::Dotnet)
|
||||
} else if has(dir, &["Cargo.toml"]) {
|
||||
Some(BuildSystem::Cargo)
|
||||
} else if has(dir, &["go.mod", "go.work"]) {
|
||||
@@ -216,7 +250,7 @@ pub fn plan(
|
||||
.or((!model.gradle.is_empty()).then_some(BuildSystem::Gradle))
|
||||
.or_else(|| model.maven.as_ref().filter(|m| !m.projects.is_empty()).map(|_| BuildSystem::Maven))
|
||||
.ok_or_else(|| {
|
||||
IdeError::not_found("no supported build system (Maven, Gradle, Cargo, Go, TypeScript, CMake) found")
|
||||
IdeError::not_found("no supported build system (Maven, Gradle, .NET, Cargo, Go, TypeScript, CMake) found")
|
||||
})?;
|
||||
let module = module_dir.as_deref().filter(|d| *d != root);
|
||||
let title = match module {
|
||||
@@ -235,6 +269,9 @@ pub fn plan(
|
||||
BuildSystem::Go => go_steps(file.and_then(Path::parent).unwrap_or(&dir), kind)?,
|
||||
BuildSystem::TypeScript => ts_steps(&dir, kind),
|
||||
BuildSystem::CMake => cmake_steps(&dir, kind, cmake.and_then(<[_]>::first)),
|
||||
BuildSystem::Dotnet => dotnet_steps(&dir, kind)?,
|
||||
// Never detected from a folder: it comes from the Project Structure (`structure_plan`).
|
||||
BuildSystem::Javac => return Err(IdeError::invalid("no build tool to run")),
|
||||
};
|
||||
Ok(BuildPlan { system, title, steps })
|
||||
}
|
||||
@@ -258,6 +295,11 @@ pub(super) fn maven_steps(
|
||||
BuildKind::Rebuild => &["clean", "test-compile"],
|
||||
BuildKind::Artifacts => &["package", "-DskipTests"],
|
||||
};
|
||||
// The IDE's compile builds are not coverage runs. JaCoCo's offline `instrument`
|
||||
// rewrites target/classes in `process-classes`; `jacoco.skip` prevents that in this
|
||||
// build. It does not restore classes instrumented by an earlier build: Rebuild's
|
||||
// `clean` removes those, while an incremental Build can still reuse them.
|
||||
let skip_jacoco = matches!(kind, BuildKind::Build | BuildKind::Rebuild);
|
||||
let tops: Vec<&maven::MavenProject> = m.projects.iter().filter(|p| !p.aggregated).collect();
|
||||
if tops.is_empty() {
|
||||
return Err(IdeError::not_found("no Maven project found"));
|
||||
@@ -270,6 +312,9 @@ pub(super) fn maven_steps(
|
||||
Format::Maven,
|
||||
);
|
||||
s.args.extend(p.options.iter().cloned());
|
||||
if skip_jacoco {
|
||||
s.args.push("-Djacoco.skip=true".into());
|
||||
}
|
||||
s.args.extend(extra.iter().cloned());
|
||||
s.args.extend(goals.iter().map(|g| (*g).to_owned()));
|
||||
s.env = p.env.clone();
|
||||
@@ -434,6 +479,33 @@ fn has_build_script(dir: &Path) -> bool {
|
||||
.is_some_and(|v| v.pointer("/scripts/build").is_some_and(serde_json::Value::is_string))
|
||||
}
|
||||
|
||||
/// The solution (`.sln` / `.slnx`) or project (`.csproj` / `.fsproj` / `.vbproj`) file `dir`
|
||||
/// builds: a solution first, then a project; the first by name when there are several.
|
||||
fn dotnet_target(dir: &Path) -> Option<PathBuf> {
|
||||
let mut files: Vec<PathBuf> =
|
||||
std::fs::read_dir(dir).ok()?.flatten().map(|e| e.path()).filter(|p| p.is_file()).collect();
|
||||
files.sort();
|
||||
let ext = |p: &Path| p.extension().map(|e| e.to_string_lossy().to_ascii_lowercase()).unwrap_or_default();
|
||||
let solution = files.iter().find(|p| matches!(ext(p).as_str(), "sln" | "slnx"));
|
||||
solution.or_else(|| files.iter().find(|p| matches!(ext(p).as_str(), "csproj" | "fsproj" | "vbproj"))).cloned()
|
||||
}
|
||||
|
||||
/// `dotnet build` of the directory's solution or project; Rebuild skips incremental builds,
|
||||
/// Build Artifacts publishes. The summary that repeats every message is left out.
|
||||
fn dotnet_steps(dir: &Path, kind: BuildKind) -> IdeResult<Vec<BuildStep>> {
|
||||
let target = dotnet_target(dir).ok_or_else(|| IdeError::not_found("no .NET solution or project found"))?;
|
||||
let target = target.file_name().map_or_else(String::new, |n| n.to_string_lossy().into_owned());
|
||||
let verb = if kind == BuildKind::Artifacts { "publish" } else { "build" };
|
||||
let mut step = BuildStep::new("dotnet", &[verb, &target, "-nologo", "-clp:NoSummary"], dir, Format::Msbuild);
|
||||
if kind == BuildKind::Rebuild {
|
||||
step.args.push("--no-incremental".into());
|
||||
}
|
||||
// The classic console logger prints the canonical `file(line,col): error` lines; the
|
||||
// terminal logger (.NET 8+) only appears on a terminal, but rules it out for older SDKs too.
|
||||
step.env.insert("MSBUILDTERMINALLOGGER".into(), "off".into());
|
||||
Ok(vec![step])
|
||||
}
|
||||
|
||||
fn ts_steps(dir: &Path, kind: BuildKind) -> Vec<BuildStep> {
|
||||
let script = has_build_script(dir);
|
||||
if script && (kind == BuildKind::Artifacts || !dir.join("tsconfig.json").is_file()) {
|
||||
@@ -601,6 +673,36 @@ pub fn run(
|
||||
let command = step.display();
|
||||
on_event(BuildEvent::Step { command: command.clone(), cwd: step.cwd.clone().into() });
|
||||
on_event(BuildEvent::Progress { message: command.clone(), fraction: None });
|
||||
if let Some((action, _)) = &step.action {
|
||||
match action.run(cancel) {
|
||||
Ok(text) => {
|
||||
if !text.is_empty() {
|
||||
on_event(BuildEvent::Output { text: format!("{text}\n"), stderr: false });
|
||||
}
|
||||
continue;
|
||||
}
|
||||
Err(e) if e.kind == ide_core::error::ErrorKind::Cancelled => return Err(e),
|
||||
Err(e) => {
|
||||
let m = BuildMessage {
|
||||
severity: Severity::Error,
|
||||
text: e.message.clone(),
|
||||
path: None,
|
||||
range: None,
|
||||
code: None,
|
||||
};
|
||||
on_event(BuildEvent::Output { text: format!("{}\n", e.message), stderr: true });
|
||||
on_event(BuildEvent::Message { message: m.clone() });
|
||||
messages.push(m);
|
||||
success = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
for dir in &step.mkdirs {
|
||||
if let Err(e) = std::fs::create_dir_all(dir) {
|
||||
tracing::debug!(dir = %dir.display(), %e, "cannot create the build output folder");
|
||||
}
|
||||
}
|
||||
let mut parser = OutputParser::new(step.format);
|
||||
let mut emit = |raws: Vec<RawMessage>, messages: &mut Vec<BuildMessage>| {
|
||||
for raw in raws {
|
||||
@@ -1033,6 +1135,22 @@ mod tests {
|
||||
std::fs::write(p, text).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn maven_compile_steps_skip_jacoco_but_artifacts_do_not() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let root = tmp.path().join("demo");
|
||||
write(&root, "pom.xml", "<project><artifactId>demo</artifactId></project>");
|
||||
let model = crate::load(&root, &tmp.path().join("cache"), None);
|
||||
for (kind, expected) in [(BuildKind::Build, true), (BuildKind::Rebuild, true), (BuildKind::Artifacts, false)] {
|
||||
let plan = plan(&root, &model, kind, None, None).unwrap();
|
||||
assert_eq!(
|
||||
plan.steps[0].args.iter().any(|arg| arg == "-Djacoco.skip=true"),
|
||||
expected,
|
||||
"{kind:?}: {plan:?}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn plans_per_build_system() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
@@ -1049,7 +1167,10 @@ mod tests {
|
||||
assert_eq!(p.system, BuildSystem::Maven);
|
||||
assert_eq!(p.title, "demo");
|
||||
assert_eq!(p.steps.len(), 1);
|
||||
assert_eq!(p.steps[0].args, ["-B", "-Dstyle.color=never", maven::LOCAL_REPO_TRACKING_ARG, "test-compile"]);
|
||||
assert_eq!(
|
||||
p.steps[0].args,
|
||||
["-B", "-Dstyle.color=never", maven::LOCAL_REPO_TRACKING_ARG, "-Djacoco.skip=true", "test-compile"]
|
||||
);
|
||||
let p = plan(&root, &model, BuildKind::Rebuild, Some(&root.join("core/src/main/java/A.java")), None).unwrap();
|
||||
assert_eq!(
|
||||
p.steps[0].args,
|
||||
@@ -1057,6 +1178,7 @@ mod tests {
|
||||
"-B",
|
||||
"-Dstyle.color=never",
|
||||
maven::LOCAL_REPO_TRACKING_ARG,
|
||||
"-Djacoco.skip=true",
|
||||
"-pl",
|
||||
"core",
|
||||
"-am",
|
||||
@@ -1104,6 +1226,24 @@ mod tests {
|
||||
("pnpm", &["run".to_owned(), "build".to_owned()][..])
|
||||
);
|
||||
|
||||
// .NET: the solution, else the project; a module build is the file's project. An ASP.NET
|
||||
// project's package.json (its front end) doesn't make it a TypeScript build.
|
||||
let net = tmp.path().join("net");
|
||||
write(&net, "App.sln", "");
|
||||
write(&net, "src/Api/Api.csproj", "<Project/>");
|
||||
write(&net, "src/Api/package.json", "{}");
|
||||
write(&net, "src/Api/Program.cs", "");
|
||||
let p = plan(&net, &BuildModel::default(), BuildKind::Build, None, None).unwrap();
|
||||
assert_eq!(p.system, BuildSystem::Dotnet);
|
||||
assert_eq!(p.steps[0].args, ["build", "App.sln", "-nologo", "-clp:NoSummary"]);
|
||||
assert_eq!(p.steps[0].env["MSBUILDTERMINALLOGGER"], "off");
|
||||
let p = plan(&net, &BuildModel::default(), BuildKind::Rebuild, Some(&net.join("src/Api/Program.cs")), None)
|
||||
.unwrap();
|
||||
assert_eq!(p.steps[0].args, ["build", "Api.csproj", "-nologo", "-clp:NoSummary", "--no-incremental"]);
|
||||
assert_eq!(p.steps[0].cwd, net.join("src/Api"));
|
||||
let p = plan(&net, &BuildModel::default(), BuildKind::Artifacts, None, None).unwrap();
|
||||
assert_eq!(p.steps[0].args[0], "publish");
|
||||
|
||||
let cm = tmp.path().join("cm");
|
||||
write(&cm, "CMakeLists.txt", "project(app)");
|
||||
let args = |p: &BuildPlan| p.steps.iter().map(|s| s.args.join(" ")).collect::<Vec<_>>();
|
||||
@@ -1345,6 +1485,7 @@ mod tests {
|
||||
maven::LOCAL_REPO_TRACKING_ARG,
|
||||
"-s",
|
||||
"settings.xml",
|
||||
"-Djacoco.skip=true",
|
||||
"-pl",
|
||||
"app",
|
||||
"-am",
|
||||
@@ -1361,6 +1502,7 @@ mod tests {
|
||||
maven::LOCAL_REPO_TRACKING_ARG,
|
||||
"-s",
|
||||
"settings.xml",
|
||||
"-Djacoco.skip=true",
|
||||
"-pl",
|
||||
"app",
|
||||
"-am",
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
//! [`super::resolve`] turns them into IDE coordinates.
|
||||
|
||||
use ide_core::Severity;
|
||||
pub(crate) use ide_core::output_encoding::strip_ansi;
|
||||
use serde_json::Value;
|
||||
|
||||
/// Output dialect of a build step.
|
||||
@@ -24,6 +25,8 @@ pub(crate) enum Format {
|
||||
Go,
|
||||
/// GCC / Clang (through `cmake --build`): `src/a.cpp:12:5: error: msg`.
|
||||
Gcc,
|
||||
/// MSBuild (`dotnet build`): `C:\p\A.cs(12,5): error CS1002: msg [C:\p\App.csproj]`.
|
||||
Msbuild,
|
||||
}
|
||||
|
||||
/// What a printed column counts.
|
||||
@@ -98,32 +101,6 @@ pub(crate) struct OutputParser {
|
||||
pending: Option<Pending>,
|
||||
}
|
||||
|
||||
/// Removes ANSI escape sequences (colour, cursor movement).
|
||||
pub(crate) fn strip_ansi(s: &str) -> String {
|
||||
if !s.contains('\u{1b}') {
|
||||
return s.to_owned();
|
||||
}
|
||||
let mut out = String::with_capacity(s.len());
|
||||
let mut chars = s.chars().peekable();
|
||||
while let Some(c) = chars.next() {
|
||||
if c != '\u{1b}' {
|
||||
out.push(c);
|
||||
continue;
|
||||
}
|
||||
if chars.peek() == Some(&'[') {
|
||||
chars.next();
|
||||
for c in chars.by_ref() {
|
||||
if c.is_ascii_alphabetic() || c == '~' {
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
chars.next();
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
fn num(s: &str) -> Option<u32> {
|
||||
s.trim().parse().ok().filter(|n| *n > 0)
|
||||
}
|
||||
@@ -169,6 +146,13 @@ fn bracket_pos(s: &str) -> Option<(u32, Option<u32>, &str)> {
|
||||
Some((line, col, rest.trim_start()))
|
||||
}
|
||||
|
||||
/// `/C:/p/A.java` → `C:/p/A.java`: Maven and file URIs put a `/` before the drive letter on Windows.
|
||||
/// Joined as is, it would become the drive-relative `C:p/A.java`.
|
||||
fn drive_path(p: &str) -> &str {
|
||||
let b = p.as_bytes();
|
||||
if b.len() > 2 && b[0] == b'/' && b[1].is_ascii_alphabetic() && b[2] == b':' { &p[1..] } else { p }
|
||||
}
|
||||
|
||||
/// `file:///p/A.kt` → `/p/A.kt` (percent-decoded).
|
||||
fn from_file_uri(s: &str) -> String {
|
||||
let Some(rest) = s.strip_prefix("file://") else { return s.to_owned() };
|
||||
@@ -187,9 +171,7 @@ fn from_file_uri(s: &str) -> String {
|
||||
out.push(bytes[i]);
|
||||
i += 1;
|
||||
}
|
||||
let path = String::from_utf8_lossy(&out).into_owned();
|
||||
// `file:///C:/x` on Windows.
|
||||
if path.len() > 2 && path.as_bytes()[0] == b'/' && path.as_bytes()[2] == b':' { path[1..].to_owned() } else { path }
|
||||
drive_path(&String::from_utf8_lossy(&out)).to_owned()
|
||||
}
|
||||
|
||||
/// Kotlin: `file:///p/A.kt:12:5 msg` or `/p/A.kt: (12, 5): msg`, after the `e: ` / `w: ` prefix.
|
||||
@@ -247,7 +229,7 @@ fn maven_line(line: &str) -> Option<RawMessage> {
|
||||
&& looks_like_source_path(path)
|
||||
&& let Some((ln, col, msg)) = bracket_pos(&format!("[{pos}"))
|
||||
{
|
||||
return Some(RawMessage::new(severity, msg, ColUnit::Char).at(path, ln, col));
|
||||
return Some(RawMessage::new(severity, msg, ColUnit::Char).at(drive_path(path), ln, col));
|
||||
}
|
||||
if let Some(m) = kotlin_line(rest) {
|
||||
return Some(m);
|
||||
@@ -288,6 +270,43 @@ fn tsc_line(line: &str) -> Option<RawMessage> {
|
||||
Some(m)
|
||||
}
|
||||
|
||||
/// MSBuild's canonical form: `path(12,5): error CS1002: msg [project]`, `path(12,5,12,9): …`,
|
||||
/// or `path : error NU1101: msg [project]` for a project-level message (no position).
|
||||
/// The path may contain spaces (`C:\Users\Jane Doe\…`); "error" / "warning" are never localized.
|
||||
fn msbuild_line(line: &str) -> Option<RawMessage> {
|
||||
let line = line.trim();
|
||||
// The project the message came from, appended by the console logger.
|
||||
let line = match line.rfind(" [") {
|
||||
Some(i) if line.ends_with(']') => &line[..i],
|
||||
_ => line,
|
||||
};
|
||||
let (head, severity, rest) =
|
||||
[(" error ", Severity::Error), (" warning ", Severity::Warning)].into_iter().find_map(|(word, severity)| {
|
||||
let i = line.find(&format!(":{word}"))?;
|
||||
Some((&line[..i], severity, &line[i + word.len() + 1..]))
|
||||
})?;
|
||||
let (code, msg) = rest.split_once(':')?;
|
||||
let code = code.trim();
|
||||
if code.is_empty() || code.contains(' ') {
|
||||
return None;
|
||||
}
|
||||
let head = head.trim_end();
|
||||
let (path, pos) = match head.strip_suffix(')').and_then(|h| h.rsplit_once('(')) {
|
||||
Some((path, pos)) => (path, pos.split(',').map(num).collect::<Vec<_>>()),
|
||||
None => (head, Vec::new()),
|
||||
};
|
||||
let at = |i: usize| pos.get(i).copied().flatten();
|
||||
// `MSBUILD : error MSB1009` and the like name no file.
|
||||
let file = path.rsplit(['/', '\\']).next().is_some_and(|n| n.contains('.'));
|
||||
let mut m = RawMessage::new(severity, msg.trim(), ColUnit::Utf16);
|
||||
if file {
|
||||
m.path = Some(path.trim().to_owned());
|
||||
(m.line, m.col, m.end_line, m.end_col) = (at(0), at(1), at(2), at(3));
|
||||
}
|
||||
m.code = Some(code.to_owned());
|
||||
Some(m)
|
||||
}
|
||||
|
||||
/// go: `./a.go:12:5: msg` (errors only; `go vet` style `a.go:12: msg` too).
|
||||
fn go_line(line: &str) -> Option<RawMessage> {
|
||||
let (path, ln, col, rest) = path_line_col(line)?;
|
||||
@@ -424,6 +443,7 @@ impl OutputParser {
|
||||
Format::Tsc => tsc_line(&line),
|
||||
Format::Go => go_line(&line),
|
||||
Format::Gcc => gcc_line(&line),
|
||||
Format::Msbuild => msbuild_line(&line),
|
||||
Format::Cargo => None,
|
||||
};
|
||||
if let Some(msg) = parsed {
|
||||
@@ -481,6 +501,20 @@ mod tests {
|
||||
assert_eq!(out[1].unit, ColUnit::Char);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn maven_windows_paths_drop_the_slash_before_the_drive() {
|
||||
let out = parse(
|
||||
Format::Maven,
|
||||
"[ERROR] /C:/dev/p/src/main/java/a/A.java:[18,41] package a.b does not exist\n\
|
||||
[ERROR] C:\\dev\\p\\B.java:[3,8] cannot find symbol\n\
|
||||
[ERROR] file:///C:/dev/p/K.kt:4:9 Unresolved reference 'x'.\n",
|
||||
);
|
||||
assert_eq!(out.len(), 3, "{out:?}");
|
||||
assert_eq!(pos(&out[0]), (Severity::Error, "C:/dev/p/src/main/java/a/A.java", 18, Some(41)));
|
||||
assert_eq!(pos(&out[1]), (Severity::Error, "C:\\dev\\p\\B.java", 3, Some(8)));
|
||||
assert_eq!(pos(&out[2]), (Severity::Error, "C:/dev/p/K.kt", 4, Some(9)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn maven_detail_lines_with_level_prefix_and_kotlin() {
|
||||
let out = parse(
|
||||
@@ -557,6 +591,27 @@ mod tests {
|
||||
assert!(plain.messages.is_empty() && plain.console.is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn msbuild_canonical_messages() {
|
||||
let out = parse(
|
||||
Format::Msbuild,
|
||||
" Determining projects to restore...\n\
|
||||
C:\\Users\\Jane Doe\\App\\Program.cs(12,5): error CS1002: ; expected [C:\\Users\\Jane Doe\\App\\App.csproj]\n\
|
||||
/p/App/Models/User.cs(3,8,3,12): warning CS0168: The variable 'e' is declared but never used [/p/App/App.csproj]\n\
|
||||
/p/App/App.csproj : error NU1101: Unable to find package Foo. [/p/App/App.csproj]\n\
|
||||
MSBUILD : error MSB1009: Project file does not exist.\n\
|
||||
Build FAILED.\n",
|
||||
);
|
||||
assert_eq!(out.len(), 4, "{out:?}");
|
||||
assert_eq!(pos(&out[0]), (Severity::Error, "C:\\Users\\Jane Doe\\App\\Program.cs", 12, Some(5)));
|
||||
assert_eq!((out[0].code.as_deref(), out[0].text.as_str()), (Some("CS1002"), "; expected"));
|
||||
assert_eq!(pos(&out[1]), (Severity::Warning, "/p/App/Models/User.cs", 3, Some(8)));
|
||||
assert_eq!((out[1].end_line, out[1].end_col), (Some(3), Some(12)));
|
||||
assert_eq!((out[2].path.as_deref(), out[2].line), (Some("/p/App/App.csproj"), None));
|
||||
assert_eq!(out[2].text, "Unable to find package Foo.");
|
||||
assert_eq!((out[3].path.as_deref(), out[3].code.as_deref()), (None, Some("MSB1009")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tsc_plain_and_pretty() {
|
||||
let out = parse(
|
||||
|
||||
@@ -0,0 +1,538 @@
|
||||
//! Single-file Java compilation with `javac` for the Compile File action (#2476).
|
||||
//!
|
||||
//! A module build (`-pl … -am`) still walks every upstream module, and packaging
|
||||
//! walks the whole reactor; compiling one file only needs that file's module
|
||||
//! outputs plus its dependency classpath, which is much faster to assemble.
|
||||
use super::*;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fs;
|
||||
use std::process::Command as ProcCommand;
|
||||
|
||||
/// Output file of the `dependency:build-classpath` probe, relative to each module.
|
||||
const CP_FILE: &str = "target/ide-compile-cp.txt";
|
||||
|
||||
/// The resolved dependency classpath for one module and scope, tied to the same
|
||||
/// inputs as its launch build so POM, JDK, settings and profile changes invalidate it.
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
struct CachedClasspath {
|
||||
fingerprint: LaunchFingerprint,
|
||||
classpath: String,
|
||||
}
|
||||
|
||||
/// A `.java` source and the module build that compiles it.
|
||||
pub struct SingleFileTarget {
|
||||
/// Absolute module directory.
|
||||
pub module: PathBuf,
|
||||
/// The source is under `src/test`: compile to test classes with the test classpath.
|
||||
pub test: bool,
|
||||
/// Absolute `target/classes` (or `target/test-classes`).
|
||||
pub out_dir: PathBuf,
|
||||
/// Reactor root building the module (owns the Maven command and options).
|
||||
pub top: maven::MavenProject,
|
||||
/// `-pl` value of the module relative to the reactor root; empty when the
|
||||
/// module is the root itself.
|
||||
pub pl: String,
|
||||
/// `maven.compiler.release` (or equivalent) of the module's project.
|
||||
pub java_version: Option<u32>,
|
||||
}
|
||||
|
||||
/// Locates the linked Maven module owning a `.java` source file.
|
||||
pub fn single_file_target(root: &Path, model: &BuildModel, file: &Path) -> IdeResult<SingleFileTarget> {
|
||||
if file.extension().is_none_or(|ext| ext != "java") {
|
||||
return Err(IdeError::invalid("only Java sources can be compiled as a single file"));
|
||||
}
|
||||
if !file.is_file() {
|
||||
return Err(IdeError::not_found("no Java source found for single-file compilation"));
|
||||
}
|
||||
let Some(m) = &model.maven else {
|
||||
return Err(IdeError::not_found("no linked Maven project found for module"));
|
||||
};
|
||||
// The innermost project whose directory contains the file owns it; the
|
||||
// aggregator root (`""`) is a prefix of everything, so take the longest match.
|
||||
let rel = rel_str(root, file);
|
||||
let project = m
|
||||
.projects
|
||||
.iter()
|
||||
.filter(|p| rel == p.dir || rel.starts_with(&format!("{}/", p.dir)) || p.dir.is_empty())
|
||||
.max_by_key(|p| p.dir.len())
|
||||
.ok_or_else(|| IdeError::not_found("no linked Maven project found for module"))?;
|
||||
if project.packaging == "pom" {
|
||||
// An aggregator owns no sources itself: a file that only matches it
|
||||
// belongs to no linked module.
|
||||
return Err(IdeError::not_found("no linked Maven project found for module"));
|
||||
}
|
||||
let module = subdir(root, &project.dir);
|
||||
let top = maven_reactor(m, &project.dir)
|
||||
.ok_or_else(|| IdeError::not_found("no linked Maven project found for module"))?;
|
||||
let top_dir = subdir(root, &top.dir);
|
||||
let pl = module.strip_prefix(&top_dir).map(|r| r.to_string_lossy().replace('\\', "/")).unwrap_or_default();
|
||||
// Test sources see the test classpath and compile into test classes.
|
||||
let parts: Vec<&str> = rel.split('/').collect();
|
||||
let test = parts.windows(2).any(|w| w == ["src", "test"]);
|
||||
let out_dir = module.join("target").join(if test { "test-classes" } else { "classes" });
|
||||
Ok(SingleFileTarget { module, test, out_dir, top: top.clone(), pl, java_version: project.java_version })
|
||||
}
|
||||
|
||||
/// The Maven reactor target for a launch module whose source file is not known yet.
|
||||
fn module_target(root: &Path, model: &BuildModel, module: &Path, test: bool) -> IdeResult<SingleFileTarget> {
|
||||
let Some(m) = &model.maven else {
|
||||
return Err(IdeError::not_found("no linked Maven project found for module"));
|
||||
};
|
||||
let module = if module.is_absolute() { module.to_path_buf() } else { root.join(module) };
|
||||
let project = m
|
||||
.projects
|
||||
.iter()
|
||||
.find(|p| root.join(&p.dir) == module)
|
||||
.ok_or_else(|| IdeError::not_found("no linked Maven project found for module"))?;
|
||||
if project.packaging == "pom" {
|
||||
return Err(IdeError::not_found("the launch module is a Maven aggregator without sources"));
|
||||
}
|
||||
let top = maven_reactor(m, &project.dir)
|
||||
.ok_or_else(|| IdeError::not_found("no linked Maven reactor found for module"))?;
|
||||
let top_dir = subdir(root, &top.dir);
|
||||
let pl = module.strip_prefix(&top_dir).map(|r| r.to_string_lossy().replace('\\', "/")).unwrap_or_default();
|
||||
let out_dir = module.join("target").join(if test { "test-classes" } else { "classes" });
|
||||
Ok(SingleFileTarget { module, test, out_dir, top: top.clone(), pl, java_version: project.java_version })
|
||||
}
|
||||
|
||||
/// `javac` for the module: its `JAVA_HOME`, else `PATH`.
|
||||
pub fn find_javac(env: &BTreeMap<String, String>) -> String {
|
||||
let home = env.get("JAVA_HOME").map(Path::new);
|
||||
let exe = if cfg!(windows) { "javac.exe" } else { "javac" };
|
||||
if let Some(javac) = home.map(|h| h.join("bin").join(exe)).filter(|p| p.is_file()) {
|
||||
return javac.to_string_lossy().into_owned();
|
||||
}
|
||||
exe.to_owned()
|
||||
}
|
||||
|
||||
/// Dependency classpath of the module: existing reactor class directories plus
|
||||
/// `mvn dependency:build-classpath` (which resolves without compiling anything,
|
||||
/// so `-am` stays cheap). Entries are platform-separated (`;` on Windows).
|
||||
pub fn single_file_classpath(root: &Path, model: &BuildModel, target: &SingleFileTarget) -> IdeResult<String> {
|
||||
classpath(root, model, target, "test", false)
|
||||
}
|
||||
|
||||
/// Runtime (or test) dependency classpath for a launch module.
|
||||
///
|
||||
/// The Java language server runs on its own JDK, so its Maven model can select a
|
||||
/// different JDK profile and dependency classifier than the project launch. Use the
|
||||
/// same Maven process that builds the module instead.
|
||||
pub fn module_classpath(
|
||||
root: &Path,
|
||||
model: &BuildModel,
|
||||
module: &Path,
|
||||
test: bool,
|
||||
local_repo: Option<PathBuf>,
|
||||
cache: &Path,
|
||||
cancel: &ide_core::CancellationToken,
|
||||
) -> IdeResult<String> {
|
||||
let target = module_target(root, model, module, test)?;
|
||||
let fingerprint = launch_fingerprint(root, model, &target.module, test, local_repo, cancel)?;
|
||||
let cache_path = classpath_cache_path(root, cache, &target.module, test);
|
||||
if let (Some(fingerprint), Some(path)) = (&fingerprint, &cache_path)
|
||||
&& let Ok(bytes) = fs::read(path)
|
||||
&& let Ok(cached) = serde_json::from_slice::<CachedClasspath>(&bytes)
|
||||
&& cached.fingerprint == *fingerprint
|
||||
&& classpath_entries_exist(root, &cached.classpath)
|
||||
{
|
||||
return Ok(cached.classpath);
|
||||
}
|
||||
let classpath = classpath(root, model, &target, if test { "test" } else { "runtime" }, true)?;
|
||||
if cancel.is_cancelled() {
|
||||
return Err(IdeError::cancelled());
|
||||
}
|
||||
if let (Some(fingerprint), Some(path)) = (fingerprint, cache_path) {
|
||||
let cached = CachedClasspath { fingerprint, classpath: classpath.clone() };
|
||||
if let Err(error) = cache_classpath(&path, &cached) {
|
||||
tracing::debug!(path = %path.display(), %error, "Cannot cache the Java launch classpath");
|
||||
}
|
||||
}
|
||||
Ok(classpath)
|
||||
}
|
||||
|
||||
/// One cache file per module and scope. Mirroring the module path keeps nested
|
||||
/// modules and their different dependency scopes from overwriting each other.
|
||||
fn classpath_cache_path(root: &Path, cache: &Path, module: &Path, test: bool) -> Option<PathBuf> {
|
||||
let relative = module.strip_prefix(root).ok()?;
|
||||
let mut path = cache.join("java-launch/classpaths");
|
||||
path.push(if relative.as_os_str().is_empty() { Path::new("_root") } else { relative });
|
||||
path.push(if test { "test.json" } else { "runtime.json" });
|
||||
Some(path)
|
||||
}
|
||||
|
||||
/// Best-effort cache persistence. A failed write only costs one later Maven
|
||||
/// probe, so the launch still succeeds even when the cache directory is unusable.
|
||||
fn cache_classpath(path: &Path, cached: &CachedClasspath) -> IdeResult<()> {
|
||||
if let Some(parent) = path.parent() {
|
||||
fs::create_dir_all(parent)?;
|
||||
}
|
||||
fs::write(path, serde_json::to_vec(cached).map_err(|e| IdeError::internal(e.to_string()))?)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// A cache entry stays valid while every file exists. Maven also emits reactor
|
||||
/// output directories for modules with no classes, which may legitimately never be
|
||||
/// created; the launch snapshot already supplies the module's classes in that case.
|
||||
fn classpath_entries_exist(root: &Path, classpath: &str) -> bool {
|
||||
// Temp directories on macOS are commonly reached through `/var`, while Maven
|
||||
// reports the canonical `/private/var` path. Compare against both spellings.
|
||||
let canonical_root = dunce::canonicalize(root).unwrap_or_else(|_| root.to_path_buf());
|
||||
std::env::split_paths(classpath).all(|entry| {
|
||||
entry.exists()
|
||||
|| ((entry.starts_with(root) || entry.starts_with(&canonical_root))
|
||||
&& (entry.ends_with(Path::new("target/classes")) || entry.ends_with(Path::new("target/test-classes"))))
|
||||
})
|
||||
}
|
||||
|
||||
fn classpath(
|
||||
root: &Path,
|
||||
model: &BuildModel,
|
||||
target: &SingleFileTarget,
|
||||
scope: &str,
|
||||
compile_first: bool,
|
||||
) -> IdeResult<String> {
|
||||
let mut entries: Vec<String> = Vec::new();
|
||||
// The module's own classes first: the file almost always uses classes next to it.
|
||||
let own: &[&str] = if target.test { &["target/test-classes", "target/classes"] } else { &["target/classes"] };
|
||||
for out in own {
|
||||
let path = target.module.join(out);
|
||||
if path.is_dir() {
|
||||
entries.push(path.to_string_lossy().into_owned());
|
||||
}
|
||||
}
|
||||
if let Some(m) = &model.maven {
|
||||
// Upstream reactor modules first: their freshly built classes shadow
|
||||
// installed snapshots of the same artifacts.
|
||||
for project in maven_launch_projects(root, m, &target.module, target.test) {
|
||||
let dir = subdir(root, &project.dir);
|
||||
if dir == target.module {
|
||||
continue;
|
||||
}
|
||||
for classes in ["target/classes", "target/test-classes"] {
|
||||
let path = dir.join(classes);
|
||||
if path.is_dir() {
|
||||
entries.push(path.to_string_lossy().into_owned());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
let probe = CP_FILE;
|
||||
let mut args = vec!["-B", "-Dstyle.color=never", maven::LOCAL_REPO_TRACKING_ARG];
|
||||
args.extend(target.top.options.iter().map(String::as_str));
|
||||
if !target.pl.is_empty() {
|
||||
args.extend(["-pl", target.pl.as_str(), "-am"]);
|
||||
}
|
||||
let output_arg = format!("-Dmdep.outputFile={probe}");
|
||||
let scope_arg = format!("-Dmdep.includeScope={scope}");
|
||||
// A launch build may only have reached `compile`. Running `compile` in this
|
||||
// same Maven session lets the dependency plugin resolve Reactor siblings from
|
||||
// their target/classes without paying for package-phase plugins.
|
||||
if compile_first {
|
||||
args.push("compile");
|
||||
}
|
||||
args.extend(["dependency:build-classpath", output_arg.as_str(), scope_arg.as_str(), "-q"]);
|
||||
let top_dir = subdir(root, &target.top.dir);
|
||||
let status = ProcCommand::new(&target.top.command)
|
||||
.args(&args)
|
||||
.envs(&target.top.env)
|
||||
.current_dir(&top_dir)
|
||||
.output()
|
||||
.map_err(|e| IdeError::internal(format!("cannot resolve the compile classpath: {e}")))?;
|
||||
if !status.status.success() {
|
||||
let stdout = String::from_utf8_lossy(&status.stdout);
|
||||
let stderr = String::from_utf8_lossy(&status.stderr);
|
||||
// Maven reports reactor/dependency failures on stdout, not only stderr.
|
||||
let tail: Vec<&str> = stdout.lines().chain(stderr.lines()).filter(|l| !l.trim().is_empty()).collect();
|
||||
let tail = tail.iter().rev().take(5).rev().cloned().collect::<Vec<_>>().join("\n");
|
||||
return Err(IdeError::internal(format!("cannot resolve the compile classpath:\n{tail}")));
|
||||
}
|
||||
// With `-am` every visited module writes its own file; only this module's counts.
|
||||
let cp_file = target.module.join(probe);
|
||||
let content = fs::read_to_string(&cp_file).map_err(|e| {
|
||||
IdeError::internal(format!("cannot resolve the compile classpath: {} ({e})", cp_file.display()))
|
||||
})?;
|
||||
let separator = if cfg!(windows) { ';' } else { ':' };
|
||||
entries.extend(content.split([separator, '\n', '\r']).map(str::trim).filter(|e| !e.is_empty()).map(str::to_owned));
|
||||
entries.dedup();
|
||||
Ok(entries.join(&separator.to_string()))
|
||||
}
|
||||
|
||||
/// The `javac` step compiling `file` into `target`'s output directory.
|
||||
pub fn javac_step(target: &SingleFileTarget, file: &Path, classpath: &str, javac: &str) -> IdeResult<BuildStep> {
|
||||
fs::create_dir_all(&target.out_dir)
|
||||
.map_err(|e| IdeError::internal(format!("cannot create {} ({e})", target.out_dir.display())))?;
|
||||
let mut step = BuildStep::new(javac, &[], &target.module, Format::Gradle);
|
||||
// Raw `javac` prints `path:line: error: …`, the same form Gradle forwards.
|
||||
step.args.push("-encoding".into());
|
||||
step.args.push("UTF-8".into());
|
||||
step.args.push("-d".into());
|
||||
step.args.push(target.out_dir.to_string_lossy().into_owned());
|
||||
step.args.push("-cp".into());
|
||||
step.args.push(classpath.to_owned());
|
||||
// Classes of the module that were never built (or are older than their source) are read
|
||||
// from its sources; only `file` is written, so a test compile never puts main classes
|
||||
// into test-classes.
|
||||
let sources = source_roots(target);
|
||||
if !sources.is_empty() {
|
||||
let separator = if cfg!(windows) { ";" } else { ":" };
|
||||
step.args.push("-sourcepath".into());
|
||||
step.args.push(sources.iter().map(|p| p.to_string_lossy()).collect::<Vec<_>>().join(separator));
|
||||
step.args.push("-implicit:none".into());
|
||||
}
|
||||
if let Some(version) = target.java_version {
|
||||
step.args.push("--release".into());
|
||||
step.args.push(version.to_string());
|
||||
}
|
||||
step.args.push(file.to_string_lossy().into_owned());
|
||||
step.env = target.top.env.clone();
|
||||
Ok(step)
|
||||
}
|
||||
|
||||
/// The module's Java source folders that exist, generated ones included; test sources see
|
||||
/// the main ones too.
|
||||
fn source_roots(target: &SingleFileTarget) -> Vec<PathBuf> {
|
||||
let scopes: &[(&str, &str)] = if target.test {
|
||||
&[("src/test/java", "target/generated-test-sources"), ("src/main/java", "target/generated-sources")]
|
||||
} else {
|
||||
&[("src/main/java", "target/generated-sources")]
|
||||
};
|
||||
let mut roots = Vec::new();
|
||||
for (src, generated) in scopes {
|
||||
roots.push(target.module.join(src));
|
||||
let generated = target.module.join(generated);
|
||||
let mut dirs: Vec<PathBuf> =
|
||||
fs::read_dir(&generated).into_iter().flatten().flatten().map(|d| d.path()).filter(|p| p.is_dir()).collect();
|
||||
dirs.sort();
|
||||
roots.extend(dirs);
|
||||
}
|
||||
roots.retain(|p| p.is_dir());
|
||||
roots
|
||||
}
|
||||
|
||||
/// Resolves the classpath and plans the `javac` invocation for one Java source.
|
||||
pub fn compile_file_plan(root: &Path, model: &BuildModel, file: &Path) -> IdeResult<BuildPlan> {
|
||||
let target = single_file_target(root, model, file)?;
|
||||
let classpath = single_file_classpath(root, model, &target)?;
|
||||
let javac = find_javac(&target.top.env);
|
||||
let name = file.file_name().map(|n| n.to_string_lossy().into_owned()).unwrap_or_default();
|
||||
Ok(BuildPlan {
|
||||
system: BuildSystem::Maven,
|
||||
title: format!("Compile {name}"),
|
||||
steps: vec![javac_step(&target, file, &classpath, &javac)?],
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn write(root: &Path, rel: &str, value: &str) {
|
||||
let path = root.join(rel);
|
||||
fs::create_dir_all(path.parent().unwrap()).unwrap();
|
||||
fs::write(path, value).unwrap();
|
||||
}
|
||||
|
||||
fn fixture(root: &Path) -> BuildModel {
|
||||
write(
|
||||
root,
|
||||
"pom.xml",
|
||||
r#"<project><groupId>demo</groupId><artifactId>parent</artifactId><version>1</version>
|
||||
<packaging>pom</packaging><modules><module>lib</module><module>app</module></modules></project>"#,
|
||||
);
|
||||
write(
|
||||
root,
|
||||
"lib/pom.xml",
|
||||
r#"<project><parent><groupId>demo</groupId><artifactId>parent</artifactId><version>1</version></parent>
|
||||
<artifactId>lib</artifactId></project>"#,
|
||||
);
|
||||
write(
|
||||
root,
|
||||
"app/pom.xml",
|
||||
r#"<project><parent><groupId>demo</groupId><artifactId>parent</artifactId><version>1</version></parent>
|
||||
<artifactId>app</artifactId><dependencies><dependency><groupId>demo</groupId><artifactId>lib</artifactId><version>1</version></dependency></dependencies></project>"#,
|
||||
);
|
||||
write(root, "app/src/main/java/demo/Main.java", "class Main {}");
|
||||
write(root, "app/src/test/java/demo/MainTest.java", "class MainTest {}");
|
||||
write(root, "lib/target/classes/Lib.class", "built");
|
||||
crate::load(root, &root.join("cache"), None)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn target_finds_the_owning_module_and_the_test_scope() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let root = dir.path();
|
||||
let model = fixture(root);
|
||||
let main = single_file_target(root, &model, &root.join("app/src/main/java/demo/Main.java")).unwrap();
|
||||
assert_eq!(main.module, root.join("app"));
|
||||
assert!(!main.test);
|
||||
assert_eq!(main.out_dir, root.join("app/target/classes"));
|
||||
let test = single_file_target(root, &model, &root.join("app/src/test/java/demo/MainTest.java")).unwrap();
|
||||
assert!(test.test);
|
||||
assert_eq!(test.out_dir, root.join("app/target/test-classes"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn target_rejects_non_java_missing_and_unlinked_files() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let root = dir.path();
|
||||
let model = fixture(root);
|
||||
write(root, "app/pom.xml", "<project/>");
|
||||
assert!(single_file_target(root, &model, &root.join("app/pom.xml")).is_err());
|
||||
assert!(single_file_target(root, &model, &root.join("app/src/main/java/demo/Missing.java")).is_err());
|
||||
write(root, "other/Main.java", "class Main {}");
|
||||
assert!(single_file_target(root, &model, &root.join("other/Main.java")).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn javac_prefers_the_module_jdk_and_scopes_test_sources() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let root = dir.path();
|
||||
let model = fixture(root);
|
||||
let file = root.join("app/src/test/java/demo/MainTest.java");
|
||||
let target = single_file_target(root, &model, &file).unwrap();
|
||||
let jdk = root.join("jdk");
|
||||
fs::create_dir_all(jdk.join("bin")).unwrap();
|
||||
let exe = if cfg!(windows) { "javac.exe" } else { "javac" };
|
||||
fs::write(jdk.join("bin").join(exe), "fake").unwrap();
|
||||
let mut target = target;
|
||||
target.top.env.insert("JAVA_HOME".into(), jdk.to_string_lossy().into_owned());
|
||||
let step = javac_step(&target, &file, "/repo/a.jar", &find_javac(&target.top.env)).unwrap();
|
||||
assert!(step.program.ends_with(exe));
|
||||
assert!(target.out_dir.is_dir());
|
||||
let args = step.args.join(" ");
|
||||
assert!(args.contains("-encoding UTF-8"), "{args}");
|
||||
assert!(args.contains("-d ") && args.contains("test-classes"), "{args}");
|
||||
assert!(args.contains("-cp /repo/a.jar"), "{args}");
|
||||
assert!(args.contains("MainTest.java"), "{args}");
|
||||
// Test sources resolve their own and the main sources, without writing them.
|
||||
let sourcepath = step.args.iter().position(|a| a == "-sourcepath").map(|i| step.args[i + 1].clone());
|
||||
let roots = sourcepath.expect("a -sourcepath");
|
||||
assert!(roots.contains("src/test/java") && roots.contains("src/main/java"), "{roots}");
|
||||
assert!(step.args.iter().any(|a| a == "-implicit:none"), "{args}");
|
||||
// No `JAVA_HOME`: plain `javac` from `PATH`.
|
||||
assert_eq!(find_javac(&BTreeMap::new()), exe);
|
||||
}
|
||||
|
||||
/// The classpath probe runs the project's Maven command with `-pl … -am`
|
||||
/// `dependency:build-classpath`; the fake `mvnw` below only records its
|
||||
/// arguments and writes a canned classpath file for the `-pl` module.
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn classpath_probe_writes_one_file_per_module_and_reads_the_compiled_one() {
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let root = dir.path();
|
||||
fixture(root);
|
||||
let script = root.join("mvnw");
|
||||
fs::write(
|
||||
&script,
|
||||
"#!/bin/sh\nprintf '%s\\n' \"$@\" > args.txt\npl='.'\nwhile [ $# -gt 0 ]; do\nif [ \"$1\" = \"-pl\" ]; then pl=\"$2\"; shift 2; else shift; fi\ndone\nmkdir -p \"$pl/target\"\nprintf '/repo/a.jar:/repo/b.jar' > \"$pl/target/ide-compile-cp.txt\"\n",
|
||||
)
|
||||
.unwrap();
|
||||
fs::set_permissions(&script, fs::Permissions::from_mode(0o755)).unwrap();
|
||||
let model = crate::load(root, &root.join("cache"), None);
|
||||
let file = root.join("app/src/main/java/demo/Main.java");
|
||||
let target = single_file_target(root, &model, &file).unwrap();
|
||||
assert!(target.top.command.ends_with("mvnw"));
|
||||
assert_eq!(target.pl, "app");
|
||||
let cp = single_file_classpath(root, &model, &target).unwrap();
|
||||
let separator = ':';
|
||||
// Reactor class directories first, so fresh outputs shadow installed snapshots.
|
||||
let lib = root.join("lib/target/classes").to_string_lossy().into_owned();
|
||||
assert!(cp.starts_with(&lib), "{cp}");
|
||||
// The module's own classes come first once they exist.
|
||||
fs::create_dir_all(root.join("app/target/classes")).unwrap();
|
||||
let cp = single_file_classpath(root, &model, &target).unwrap();
|
||||
let own = root.join("app/target/classes").to_string_lossy().into_owned();
|
||||
assert!(cp.starts_with(&format!("{own}:{lib}")), "{cp}");
|
||||
assert!(cp.contains("/repo/a.jar"), "{cp}");
|
||||
assert!(cp.contains("/repo/b.jar"), "{cp}");
|
||||
assert!(cp.split(separator).count() >= 3, "{cp}");
|
||||
let args = fs::read_to_string(root.join("args.txt")).unwrap();
|
||||
assert!(!args.lines().any(|a| a == "compile" || a == "package"), "{args}");
|
||||
}
|
||||
|
||||
/// A launch uses runtime dependencies, while tests additionally see test-scope ones.
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn launch_module_classpath_uses_the_matching_dependency_scope() {
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let root = dir.path();
|
||||
fixture(root);
|
||||
let script = root.join("mvnw");
|
||||
fs::write(
|
||||
&script,
|
||||
"#!/bin/sh\nprintf '%s\\n' \"$@\" > args.txt\npl='.'\nwhile [ $# -gt 0 ]; do\nif [ \"$1\" = \"-pl\" ]; then pl=\"$2\"; shift 2; else shift; fi\ndone\nmkdir -p \"$pl/target\"\nprintf '/repo/runtime.jar:/repo/test.jar' > \"$pl/target/ide-compile-cp.txt\"\n",
|
||||
)
|
||||
.unwrap();
|
||||
fs::set_permissions(&script, fs::Permissions::from_mode(0o755)).unwrap();
|
||||
let model = crate::load(root, &root.join("cache"), None);
|
||||
|
||||
let cache = root.join("cache");
|
||||
let runtime =
|
||||
module_classpath(root, &model, &root.join("app"), false, None, &cache, &CancellationToken::new()).unwrap();
|
||||
assert!(runtime.contains("/repo/runtime.jar"), "{runtime}");
|
||||
let args = fs::read_to_string(root.join("args.txt")).unwrap();
|
||||
assert!(args.lines().any(|a| a == "compile"), "{args}");
|
||||
assert!(!args.lines().any(|a| a == "package"), "{args}");
|
||||
assert!(args.lines().any(|a| a == "-Dmdep.includeScope=runtime"), "{args}");
|
||||
|
||||
let test =
|
||||
module_classpath(root, &model, &root.join("app"), true, None, &cache, &CancellationToken::new()).unwrap();
|
||||
assert!(test.contains("/repo/test.jar"), "{test}");
|
||||
let args = fs::read_to_string(root.join("args.txt")).unwrap();
|
||||
assert!(args.lines().any(|a| a == "-Dmdep.includeScope=test"), "{args}");
|
||||
}
|
||||
|
||||
/// The launch classpath follows the Maven runner JDK and is reused until that
|
||||
/// build input changes; the probe does not run packaging plugins.
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn launch_classpath_cache_follows_the_runner_jdk() {
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let root = dir.path();
|
||||
fixture(root);
|
||||
let script = root.join("mvnw");
|
||||
// `dependency:build-classpath` reports reactor outputs as absolute paths. Keep
|
||||
// the fake probe faithful so the cache's existence check sees the output.
|
||||
fs::write(
|
||||
&script,
|
||||
"#!/bin/sh\nprintf x >> calls.txt\npl='.'\nwhile [ $# -gt 0 ]; do\nif [ \"$1\" = \"-pl\" ]; then pl=\"$2\"; shift 2; else shift; fi\ndone\nmkdir -p \"$pl/target/classes\" \"$JAVA_HOME/classpath\"\n: > \"$JAVA_HOME/classpath/jdk.jar\"\nprintf '%s/classpath/jdk.jar:%s/%s/target/classes:%s/empty/target/classes' \"$JAVA_HOME\" \"$(pwd)\" \"$pl\" \"$(pwd)\" > \"$pl/target/ide-compile-cp.txt\"\n",
|
||||
)
|
||||
.unwrap();
|
||||
fs::set_permissions(&script, fs::Permissions::from_mode(0o755)).unwrap();
|
||||
let mut model = crate::load(root, &root.join("cache"), None);
|
||||
for (name, version) in [("jdk21", "21"), ("jdk8", "8")] {
|
||||
let home = root.join(name);
|
||||
fs::create_dir_all(home.join("bin")).unwrap();
|
||||
fs::write(home.join("release"), format!("JAVA_VERSION=\"{version}\"\n")).unwrap();
|
||||
fs::write(home.join("bin/java"), "").unwrap();
|
||||
}
|
||||
let cache = root.join("classpath-cache");
|
||||
let cancel = CancellationToken::new();
|
||||
let jdk21 = root.join("jdk21");
|
||||
for project in &mut model.maven.as_mut().unwrap().projects {
|
||||
project.env.insert("JAVA_HOME".into(), jdk21.to_string_lossy().into_owned());
|
||||
}
|
||||
let first = module_classpath(root, &model, &root.join("app"), false, None, &cache, &cancel).unwrap();
|
||||
assert!(first.contains("jdk21/classpath/jdk.jar"), "{first}");
|
||||
assert!(!first.contains("jdk8/classpath/jdk.jar"), "{first}");
|
||||
let second = module_classpath(root, &model, &root.join("app"), false, None, &cache, &cancel).unwrap();
|
||||
assert_eq!(second, first);
|
||||
assert_eq!(fs::read_to_string(root.join("calls.txt")).unwrap(), "x");
|
||||
|
||||
let jdk8 = root.join("jdk8");
|
||||
for project in &mut model.maven.as_mut().unwrap().projects {
|
||||
project.env.insert("JAVA_HOME".into(), jdk8.to_string_lossy().into_owned());
|
||||
}
|
||||
let switched = module_classpath(root, &model, &root.join("app"), false, None, &cache, &cancel).unwrap();
|
||||
assert!(switched.contains("jdk8/classpath/jdk.jar"), "{switched}");
|
||||
assert!(!switched.contains("jdk21/classpath/jdk.jar"), "{switched}");
|
||||
assert_eq!(fs::read_to_string(root.join("calls.txt")).unwrap(), "xx");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,819 @@
|
||||
//! A Java project without a build tool, built from its Project Structure: `javac` compiles
|
||||
//! the source roots to the module's output folder against the project libraries and the
|
||||
//! Web facet's `WEB-INF/lib`, and the resources are copied next to the classes; tests the
|
||||
//! same way into the test output, which also sees the production classes.
|
||||
use super::*;
|
||||
use ide_core::settings::{
|
||||
ArtifactConfig, ArtifactKind, DependencyScope, ModuleStructureSettings, ProjectStructureSettings,
|
||||
};
|
||||
use std::fs;
|
||||
|
||||
/// The roots of the project as the IDE resolved them (absolute folders).
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub struct StructureRoots {
|
||||
pub sources: Vec<PathBuf>,
|
||||
pub tests: Vec<PathBuf>,
|
||||
pub resources: Vec<PathBuf>,
|
||||
pub test_resources: Vec<PathBuf>,
|
||||
pub excluded: Vec<PathBuf>,
|
||||
}
|
||||
|
||||
/// The JDK compiling the project.
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct StructureJdk {
|
||||
pub home: PathBuf,
|
||||
pub major: u32,
|
||||
}
|
||||
|
||||
/// Where the project's classes go: `<compiler output>/production/<module>` and
|
||||
/// `<compiler output>/test/<module>` (`out` by default), unless the module names its own.
|
||||
pub fn structure_outputs(root: &Path, ps: &ProjectStructureSettings) -> (PathBuf, PathBuf) {
|
||||
let module = root_module(root, ps);
|
||||
let name = module_name(root);
|
||||
let base = resolve_dir(root, &ps.compiler_output).unwrap_or_else(|| root.join("out"));
|
||||
let main = module
|
||||
.and_then(|m| resolve_dir(root, &m.compiler_output))
|
||||
.unwrap_or_else(|| base.join("production").join(&name));
|
||||
let test = module
|
||||
.and_then(|m| resolve_dir(root, &m.test_compiler_output))
|
||||
.unwrap_or_else(|| base.join("test").join(&name));
|
||||
(main, test)
|
||||
}
|
||||
|
||||
fn module_name(root: &Path) -> String {
|
||||
root.file_name().map_or_else(|| "project".to_owned(), |n| n.to_string_lossy().into_owned())
|
||||
}
|
||||
|
||||
fn root_module<'a>(root: &Path, ps: &'a ProjectStructureSettings) -> Option<&'a ModuleStructureSettings> {
|
||||
ps.modules.get(".").or_else(|| ps.module_settings(root, root))
|
||||
}
|
||||
|
||||
fn resolve_dir(root: &Path, value: &str) -> Option<PathBuf> {
|
||||
let value = value.trim();
|
||||
(!value.is_empty()).then(|| root.join(value))
|
||||
}
|
||||
|
||||
/// `.java` files below `dir`, not inside an excluded folder or another root of `skip`.
|
||||
fn java_files(dir: &Path, excluded: &[PathBuf], out: &mut Vec<PathBuf>) {
|
||||
let Ok(entries) = fs::read_dir(dir) else { return };
|
||||
let mut entries: Vec<_> = entries.flatten().collect();
|
||||
entries.sort_by_key(|e| e.file_name());
|
||||
for e in entries {
|
||||
let path = e.path();
|
||||
match e.file_type() {
|
||||
Ok(t) if t.is_dir() => {
|
||||
if !excluded.iter().any(|x| path.starts_with(x)) {
|
||||
java_files(&path, excluded, out);
|
||||
}
|
||||
}
|
||||
Ok(t) if t.is_file() && path.extension().is_some_and(|x| x == "java") => out.push(path),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Folders and archives of the libraries a module compiles against, for `scope`.
|
||||
fn library_entries(root: &Path, ps: &ProjectStructureSettings, test: bool) -> Vec<String> {
|
||||
let wanted = |scope: DependencyScope| match scope {
|
||||
DependencyScope::Compile | DependencyScope::Provided => true,
|
||||
DependencyScope::Test => test,
|
||||
DependencyScope::Runtime => false,
|
||||
};
|
||||
let mut out: Vec<String> = Vec::new();
|
||||
let mut add = |entry: String| {
|
||||
if !out.contains(&entry) {
|
||||
out.push(entry);
|
||||
}
|
||||
};
|
||||
let module_deps = root_module(root, ps).map(|m| m.dependencies.as_slice()).unwrap_or_default();
|
||||
for lib in &ps.libraries {
|
||||
// A library a module dependency names takes that dependency's scope.
|
||||
let scope =
|
||||
module_deps.iter().find(|d| d.kind == "library" && d.target == lib.name).map_or(lib.scope, |d| d.scope);
|
||||
if wanted(scope) {
|
||||
for class in &lib.classes {
|
||||
add(class_entry(root, class));
|
||||
}
|
||||
}
|
||||
}
|
||||
for dep in module_deps.iter().filter(|d| d.kind == "library" && wanted(d.scope)) {
|
||||
if !ps.libraries.iter().any(|l| l.name == dep.target) && !dep.target.trim().is_empty() {
|
||||
add(class_entry(root, &dep.target));
|
||||
}
|
||||
}
|
||||
for facet in ps.facets.iter().filter(|f| f.kind == "web") {
|
||||
let lib = web_root(root, facet).join("WEB-INF").join("lib");
|
||||
if lib.is_dir() {
|
||||
add(format!("{}{}*", lib.to_string_lossy(), std::path::MAIN_SEPARATOR));
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// The web resource folder of a Web facet: its own, else the folder holding `WEB-INF/web.xml`.
|
||||
fn web_root(root: &Path, facet: &ide_core::settings::FacetConfig) -> PathBuf {
|
||||
let dir = facet.web_root.trim();
|
||||
let dir = if dir.is_empty() {
|
||||
facet.descriptor.trim().replace('\\', "/").strip_suffix("/WEB-INF/web.xml").unwrap_or("").to_owned()
|
||||
} else {
|
||||
dir.to_owned()
|
||||
};
|
||||
root.join(dir)
|
||||
}
|
||||
|
||||
/// The class archives and folders of a library path (`lib/*.jar` lists the folder's jars).
|
||||
fn library_files(root: &Path, value: &str) -> Vec<PathBuf> {
|
||||
let entry = class_entry(root, value);
|
||||
match entry.strip_suffix('*') {
|
||||
Some(dir) => {
|
||||
let mut jars: Vec<PathBuf> = fs::read_dir(dir)
|
||||
.map(|d| d.flatten().map(|e| e.path()).filter(|p| p.extension().is_some_and(|x| x == "jar")).collect())
|
||||
.unwrap_or_default();
|
||||
jars.sort();
|
||||
jars
|
||||
}
|
||||
None => vec![PathBuf::from(entry)],
|
||||
}
|
||||
}
|
||||
|
||||
/// A classpath entry for a library path: a folder of jars (`lib/*`) stays a wildcard javac expands.
|
||||
fn class_entry(root: &Path, value: &str) -> String {
|
||||
let path = root.join(value.trim());
|
||||
let text = path.to_string_lossy().into_owned();
|
||||
if text.ends_with("/*.jar") || text.ends_with("\\*.jar") {
|
||||
return text[..text.len() - 4].to_owned();
|
||||
}
|
||||
text
|
||||
}
|
||||
|
||||
fn classpath(entries: &[String]) -> String {
|
||||
entries.join(if cfg!(windows) { ";" } else { ":" })
|
||||
}
|
||||
|
||||
/// `--release N` from JDK 9 on; older compilers only know `-source` / `-target`.
|
||||
fn release_args(level: Option<u32>, jdk: Option<&StructureJdk>) -> Vec<String> {
|
||||
let Some(level) = level else { return Vec::new() };
|
||||
if jdk.is_none_or(|j| j.major >= 9) {
|
||||
vec!["--release".into(), level.to_string()]
|
||||
} else {
|
||||
vec!["-source".into(), format!("1.{level}"), "-target".into(), format!("1.{level}")]
|
||||
}
|
||||
}
|
||||
|
||||
/// `"17"`, `"1.8"`, `"JDK_17"` → the release number.
|
||||
pub fn language_release(level: &str) -> Option<u32> {
|
||||
let digits = level.trim().trim_start_matches("JDK_").trim_start_matches("jdk").replace('_', ".");
|
||||
let digits = digits.strip_prefix("1.").unwrap_or(&digits);
|
||||
digits.split(['.', ' ']).next()?.parse().ok().filter(|n| *n > 0)
|
||||
}
|
||||
|
||||
/// The Build / Rebuild plan of a project without a build tool, or `None` when it has no
|
||||
/// Java sources under its source roots. `language_level`: the project's, unless the module
|
||||
/// overrides it. Writes the javac argument files below `cache`.
|
||||
pub fn structure_plan(
|
||||
root: &Path,
|
||||
ps: &ProjectStructureSettings,
|
||||
roots: &StructureRoots,
|
||||
jdk: Option<&StructureJdk>,
|
||||
language_level: &str,
|
||||
kind: BuildKind,
|
||||
cache: &Path,
|
||||
) -> IdeResult<Option<BuildPlan>> {
|
||||
let mut main_sources = Vec::new();
|
||||
for dir in &roots.sources {
|
||||
java_files(dir, &roots.excluded, &mut main_sources);
|
||||
}
|
||||
let mut test_sources = Vec::new();
|
||||
for dir in &roots.tests {
|
||||
java_files(dir, &roots.excluded, &mut test_sources);
|
||||
}
|
||||
if main_sources.is_empty() && test_sources.is_empty() {
|
||||
return Ok(None);
|
||||
}
|
||||
let (main_out, test_out) = structure_outputs(root, ps);
|
||||
let module = root_module(root, ps);
|
||||
let level = module
|
||||
.map(|m| m.language_level.as_str())
|
||||
.filter(|l| !l.trim().is_empty())
|
||||
.or(Some(language_level))
|
||||
.and_then(language_release);
|
||||
let env: BTreeMap<String, String> = jdk
|
||||
.map(|j| BTreeMap::from([("JAVA_HOME".to_owned(), j.home.to_string_lossy().into_owned())]))
|
||||
.unwrap_or_default();
|
||||
let javac = find_javac(&env);
|
||||
let args_dir = cache.join("javac");
|
||||
fs::create_dir_all(&args_dir)
|
||||
.map_err(|e| IdeError::internal(format!("cannot create {} ({e})", args_dir.display())))?;
|
||||
|
||||
let mut steps = Vec::new();
|
||||
if kind == BuildKind::Rebuild {
|
||||
steps.push(BuildStep::action(Action::Clean(vec![main_out.clone(), test_out.clone()]), root));
|
||||
}
|
||||
let main_cp = library_entries(root, ps, false);
|
||||
let compile = |name: &str, sources: &[PathBuf], out: &Path, cp: &[String]| -> IdeResult<BuildStep> {
|
||||
let args_file = args_dir.join(format!("{name}.args"));
|
||||
// One quoted path per line: the sources outgrow a command line quickly.
|
||||
let text: String = sources
|
||||
.iter()
|
||||
.map(|p| format!("\"{}\"\n", p.to_string_lossy().replace('\\', "\\\\").replace('"', "\\\"")))
|
||||
.collect();
|
||||
fs::write(&args_file, text)
|
||||
.map_err(|e| IdeError::internal(format!("cannot write {} ({e})", args_file.display())))?;
|
||||
let mut step = BuildStep::new(&javac, &[], root, Format::Gradle);
|
||||
step.args.extend(["-encoding".into(), "UTF-8".into(), "-g".into()]);
|
||||
step.args.extend(["-d".into(), out.to_string_lossy().into_owned()]);
|
||||
if !cp.is_empty() {
|
||||
step.args.extend(["-cp".into(), classpath(cp)]);
|
||||
}
|
||||
step.args.extend(release_args(level, jdk));
|
||||
step.args.push(format!("@{}", args_file.to_string_lossy()));
|
||||
step.env = env.clone();
|
||||
step.mkdirs = vec![out.to_path_buf()];
|
||||
Ok(step)
|
||||
};
|
||||
if !main_sources.is_empty() {
|
||||
steps.push(compile("production", &main_sources, &main_out, &main_cp)?);
|
||||
}
|
||||
steps.push(BuildStep::action(
|
||||
Action::CopyResources {
|
||||
from: roots.sources.iter().chain(&roots.resources).cloned().collect(),
|
||||
to: main_out.clone(),
|
||||
},
|
||||
root,
|
||||
));
|
||||
// Artifacts hold production classes only.
|
||||
if !test_sources.is_empty() && kind != BuildKind::Artifacts {
|
||||
let mut test_cp = vec![main_out.to_string_lossy().into_owned()];
|
||||
test_cp.extend(library_entries(root, ps, true));
|
||||
steps.push(compile("test", &test_sources, &test_out, &test_cp)?);
|
||||
steps.push(BuildStep::action(
|
||||
Action::CopyResources {
|
||||
from: roots.tests.iter().chain(&roots.test_resources).cloned().collect(),
|
||||
to: test_out.clone(),
|
||||
},
|
||||
root,
|
||||
));
|
||||
}
|
||||
if kind == BuildKind::Artifacts {
|
||||
if ps.artifacts.is_empty() {
|
||||
return Err(IdeError::invalid("No artifacts are configured: add one in Project Structure › Artifacts"));
|
||||
}
|
||||
for artifact in &ps.artifacts {
|
||||
steps.extend(artifact_steps(root, ps, artifact, &main_out, None, cache));
|
||||
}
|
||||
}
|
||||
Ok(Some(BuildPlan { system: BuildSystem::Javac, title: module_name(root), steps }))
|
||||
}
|
||||
|
||||
/// Where an artifact goes: its output folder, else `out/artifacts/<name>`.
|
||||
pub fn artifact_output(root: &Path, artifact: &ArtifactConfig) -> PathBuf {
|
||||
resolve_dir(root, &artifact.output_directory)
|
||||
.unwrap_or_else(|| root.join("out").join("artifacts").join(file_name(&artifact.name)))
|
||||
}
|
||||
|
||||
/// An artifact name as a file name (`shop:war exploded` → `shop_war_exploded`).
|
||||
fn file_name(name: &str) -> String {
|
||||
name.chars().map(|c| if c.is_alphanumeric() || c == '-' || c == '.' { c } else { '_' }).collect()
|
||||
}
|
||||
|
||||
/// The archive a `jar` / `war` artifact writes (`<output>/<name>.war`), or its folder (exploded).
|
||||
pub fn artifact_file(root: &Path, artifact: &ArtifactConfig) -> PathBuf {
|
||||
let out = artifact_output(root, artifact);
|
||||
let base = artifact.name.split(':').next().filter(|n| !n.trim().is_empty()).unwrap_or(&artifact.name);
|
||||
match artifact.kind {
|
||||
ArtifactKind::Jar => out.join(format!("{}.jar", file_name(base))),
|
||||
ArtifactKind::War => out.join(format!("{}.war", file_name(base))),
|
||||
ArtifactKind::WarExploded => out,
|
||||
}
|
||||
}
|
||||
|
||||
/// The steps laying out one artifact from the module output `classes`; `war`: write a
|
||||
/// WAR there instead of the artifact's own output (an application server's deployment).
|
||||
fn artifact_steps(
|
||||
root: &Path,
|
||||
ps: &ProjectStructureSettings,
|
||||
artifact: &ArtifactConfig,
|
||||
classes: &Path,
|
||||
war: Option<&Path>,
|
||||
cache: &Path,
|
||||
) -> Vec<BuildStep> {
|
||||
let libraries: Vec<PathBuf> = ps
|
||||
.libraries
|
||||
.iter()
|
||||
.filter(|l| artifact.include_libraries.contains(&l.name))
|
||||
.flat_map(|l| l.classes.iter().flat_map(|c| library_files(root, c)))
|
||||
.collect();
|
||||
let facet = ps
|
||||
.facets
|
||||
.iter()
|
||||
.filter(|f| f.kind == "web")
|
||||
.find(|f| f.module == artifact.module || f.module.is_empty() && artifact.module == ".")
|
||||
.or_else(|| ps.facets.iter().find(|f| f.kind == "web"));
|
||||
let staging = cache.join("artifacts").join(file_name(&artifact.name));
|
||||
let mut steps = Vec::new();
|
||||
match (artifact.kind, war) {
|
||||
(ArtifactKind::Jar, None) => {
|
||||
let out = artifact_output(root, artifact);
|
||||
let mut manifest = String::from("Manifest-Version: 1.0\r\n");
|
||||
if !artifact.main_class.trim().is_empty() {
|
||||
manifest.push_str(&format!("Main-Class: {}\r\n", artifact.main_class.trim()));
|
||||
}
|
||||
let names: Vec<String> =
|
||||
libraries.iter().filter_map(|l| l.file_name()).map(|n| n.to_string_lossy().into_owned()).collect();
|
||||
if !names.is_empty() {
|
||||
manifest.push_str(&format!("Class-Path: {}\r\n", names.join(" ")));
|
||||
}
|
||||
steps.push(BuildStep::action(
|
||||
Action::Layout { into: staging.clone(), entries: vec![(classes.to_path_buf(), PathBuf::new())] },
|
||||
root,
|
||||
));
|
||||
// The jar's Class-Path names the libraries next to it.
|
||||
steps.push(BuildStep::action(
|
||||
Action::Layout {
|
||||
into: out.clone(),
|
||||
entries: libraries.iter().map(|l| (l.clone(), file_of(l))).collect(),
|
||||
},
|
||||
root,
|
||||
));
|
||||
steps.push(BuildStep::action(
|
||||
Action::Archive { from: staging, to: artifact_file(root, artifact), manifest: Some(manifest) },
|
||||
root,
|
||||
));
|
||||
}
|
||||
(kind, war) => {
|
||||
let exploded = match (kind, war) {
|
||||
(ArtifactKind::WarExploded, None) => artifact_output(root, artifact),
|
||||
_ => staging.clone(),
|
||||
};
|
||||
let mut entries = Vec::new();
|
||||
if let Some(facet) = facet {
|
||||
entries.push((web_root(root, facet), PathBuf::new()));
|
||||
}
|
||||
entries.push((classes.to_path_buf(), PathBuf::from("WEB-INF/classes")));
|
||||
for lib in &libraries {
|
||||
entries.push((lib.clone(), Path::new("WEB-INF/lib").join(file_of(lib))));
|
||||
}
|
||||
steps.push(BuildStep::action(Action::Layout { into: exploded.clone(), entries }, root));
|
||||
let archive = war
|
||||
.map(Path::to_path_buf)
|
||||
.or_else(|| (kind != ArtifactKind::WarExploded).then(|| artifact_file(root, artifact)));
|
||||
if let Some(to) = archive {
|
||||
steps.push(BuildStep::action(Action::Archive { from: exploded, to, manifest: None }, root));
|
||||
}
|
||||
}
|
||||
}
|
||||
steps
|
||||
}
|
||||
|
||||
fn file_of(path: &Path) -> PathBuf {
|
||||
path.file_name().map(PathBuf::from).unwrap_or_default()
|
||||
}
|
||||
|
||||
/// The WAR artifacts of a project without a build tool: an application server deploys them.
|
||||
pub fn structure_web_artifacts(ps: &ProjectStructureSettings) -> Vec<&ArtifactConfig> {
|
||||
ps.artifacts.iter().filter(|a| matches!(a.kind, ArtifactKind::War | ArtifactKind::WarExploded)).collect()
|
||||
}
|
||||
|
||||
/// A build of `artifact` into the WAR `war` (Tomcat's deployment), compiling first.
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub fn structure_war_plan(
|
||||
root: &Path,
|
||||
ps: &ProjectStructureSettings,
|
||||
roots: &StructureRoots,
|
||||
jdk: Option<&StructureJdk>,
|
||||
language_level: &str,
|
||||
artifact: &ArtifactConfig,
|
||||
war: &Path,
|
||||
cache: &Path,
|
||||
) -> IdeResult<BuildPlan> {
|
||||
let mut plan = structure_plan(root, ps, roots, jdk, language_level, BuildKind::Build, cache)?
|
||||
.unwrap_or_else(|| BuildPlan { system: BuildSystem::Javac, title: module_name(root), steps: Vec::new() });
|
||||
let (main_out, _) = structure_outputs(root, ps);
|
||||
// Tests are not part of a deployment.
|
||||
plan.steps.retain(|s| !s.args.iter().any(|a| a.ends_with("test.args")));
|
||||
plan.steps
|
||||
.retain(|s| !matches!(&s.action, Some((Action::CopyResources { to, .. }, _)) if !to.starts_with(&main_out)));
|
||||
plan.steps.extend(artifact_steps(root, ps, artifact, &main_out, Some(war), cache));
|
||||
plan.title = artifact.name.clone();
|
||||
Ok(plan)
|
||||
}
|
||||
|
||||
/// Work a build does in the IDE itself rather than through a tool.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub(crate) enum Action {
|
||||
/// Deletes these output folders (Rebuild).
|
||||
Clean(Vec<PathBuf>),
|
||||
/// Copies the files of these folders that are not Java sources into `to`, keeping their
|
||||
/// relative paths; files that are already up to date are left alone.
|
||||
CopyResources { from: Vec<PathBuf>, to: PathBuf },
|
||||
/// Lays out an artifact in `into`: each folder's content or file goes to its relative
|
||||
/// place (`""` = the top). What is no longer laid out is removed.
|
||||
Layout { into: PathBuf, entries: Vec<(PathBuf, PathBuf)> },
|
||||
/// Zips the folder `from` into the archive `to`, with this manifest first.
|
||||
Archive { from: PathBuf, to: PathBuf, manifest: Option<String> },
|
||||
}
|
||||
|
||||
impl Action {
|
||||
pub(crate) fn display(&self, root: &Path) -> String {
|
||||
let rel = |p: &Path| p.strip_prefix(root).unwrap_or(p).to_string_lossy().replace('\\', "/");
|
||||
match self {
|
||||
Action::Clean(dirs) => format!("Clean {}", dirs.iter().map(|d| rel(d)).collect::<Vec<_>>().join(", ")),
|
||||
Action::CopyResources { to, .. } => format!("Copy resources to {}", rel(to)),
|
||||
Action::Layout { into, .. } => format!("Lay out {}", rel(into)),
|
||||
Action::Archive { to, .. } => format!("Pack {}", rel(to)),
|
||||
}
|
||||
}
|
||||
|
||||
/// Runs the action; the text is a summary for the console.
|
||||
pub(crate) fn run(&self, cancel: &CancellationToken) -> IdeResult<String> {
|
||||
match self {
|
||||
Action::Clean(dirs) => {
|
||||
for dir in dirs.iter().filter(|d| d.exists()) {
|
||||
fs::remove_dir_all(dir)
|
||||
.map_err(|e| IdeError::internal(format!("cannot delete {} ({e})", dir.display())))?;
|
||||
}
|
||||
Ok(String::new())
|
||||
}
|
||||
Action::CopyResources { from, to } => {
|
||||
let mut copied = 0;
|
||||
for dir in from {
|
||||
copied += copy_resources(dir, dir, to, cancel)?;
|
||||
}
|
||||
Ok(format!("{copied} resource file(s) copied"))
|
||||
}
|
||||
Action::Layout { into, entries } => {
|
||||
// A fresh layout: files dropped from the sources must not linger in the artifact.
|
||||
if entries.iter().any(|(from, _)| from.starts_with(into)) {
|
||||
return Err(IdeError::invalid(format!(
|
||||
"The artifact output folder {} holds files the artifact is made of",
|
||||
into.display()
|
||||
)));
|
||||
}
|
||||
if into.exists() {
|
||||
fs::remove_dir_all(into)
|
||||
.map_err(|e| IdeError::internal(format!("cannot delete {} ({e})", into.display())))?;
|
||||
}
|
||||
let mut copied = 0;
|
||||
for (from, to) in entries {
|
||||
let target = into.join(to);
|
||||
if from.is_dir() {
|
||||
copied += copy_tree(from, &target, cancel)?;
|
||||
} else if from.is_file() {
|
||||
copy_file(from, &target)?;
|
||||
copied += 1;
|
||||
}
|
||||
}
|
||||
Ok(format!("{copied} file(s) laid out"))
|
||||
}
|
||||
Action::Archive { from, to, manifest } => {
|
||||
archive(from, to, manifest.as_deref(), cancel)?;
|
||||
Ok(format!("Wrote {}", to.display()))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn copy_file(from: &Path, to: &Path) -> IdeResult<()> {
|
||||
if let Some(parent) = to.parent() {
|
||||
fs::create_dir_all(parent)
|
||||
.map_err(|e| IdeError::internal(format!("cannot create {} ({e})", parent.display())))?;
|
||||
}
|
||||
fs::copy(from, to).map_err(|e| IdeError::internal(format!("cannot copy {} ({e})", from.display())))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn copy_tree(from: &Path, to: &Path, cancel: &CancellationToken) -> IdeResult<usize> {
|
||||
if cancel.is_cancelled() {
|
||||
return Err(IdeError::cancelled());
|
||||
}
|
||||
let Ok(entries) = fs::read_dir(from) else { return Ok(0) };
|
||||
let mut copied = 0;
|
||||
for e in entries.flatten() {
|
||||
let Ok(t) = e.file_type() else { continue };
|
||||
let target = to.join(e.file_name());
|
||||
if t.is_dir() {
|
||||
copied += copy_tree(&e.path(), &target, cancel)?;
|
||||
} else if t.is_file() {
|
||||
copy_file(&e.path(), &target)?;
|
||||
copied += 1;
|
||||
}
|
||||
}
|
||||
Ok(copied)
|
||||
}
|
||||
|
||||
fn archive(from: &Path, to: &Path, manifest: Option<&str>, cancel: &CancellationToken) -> IdeResult<()> {
|
||||
use std::io::Write;
|
||||
let err = |e: &dyn std::fmt::Display| IdeError::internal(format!("cannot write {} ({e})", to.display()));
|
||||
if let Some(parent) = to.parent() {
|
||||
fs::create_dir_all(parent).map_err(|e| err(&e))?;
|
||||
}
|
||||
let mut zip = zip::ZipWriter::new(fs::File::create(to).map_err(|e| err(&e))?);
|
||||
let options = zip::write::SimpleFileOptions::default();
|
||||
if let Some(manifest) = manifest {
|
||||
zip.add_directory("META-INF/", options).map_err(|e| err(&e))?;
|
||||
zip.start_file("META-INF/MANIFEST.MF", options).map_err(|e| err(&e))?;
|
||||
zip.write_all(manifest.as_bytes()).map_err(|e| err(&e))?;
|
||||
}
|
||||
let mut stack = vec![from.to_path_buf()];
|
||||
while let Some(dir) = stack.pop() {
|
||||
if cancel.is_cancelled() {
|
||||
return Err(IdeError::cancelled());
|
||||
}
|
||||
let Ok(entries) = fs::read_dir(&dir) else { continue };
|
||||
let mut entries: Vec<_> = entries.flatten().collect();
|
||||
entries.sort_by_key(|e| e.file_name());
|
||||
for e in entries {
|
||||
let path = e.path();
|
||||
let name = path.strip_prefix(from).unwrap_or(&path).to_string_lossy().replace('\\', "/");
|
||||
if manifest.is_some() && name.eq_ignore_ascii_case("META-INF/MANIFEST.MF") {
|
||||
continue;
|
||||
}
|
||||
match e.file_type() {
|
||||
Ok(t) if t.is_dir() => {
|
||||
if !(manifest.is_some() && name == "META-INF") {
|
||||
zip.add_directory(format!("{name}/"), options).map_err(|e| err(&e))?;
|
||||
}
|
||||
stack.push(path);
|
||||
}
|
||||
Ok(t) if t.is_file() => {
|
||||
zip.start_file(name, options).map_err(|e| err(&e))?;
|
||||
let mut file = fs::File::open(&path).map_err(|e| err(&e))?;
|
||||
std::io::copy(&mut file, &mut zip).map_err(|e| err(&e))?;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
zip.finish().map_err(|e| err(&e))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn copy_resources(base: &Path, dir: &Path, to: &Path, cancel: &CancellationToken) -> IdeResult<usize> {
|
||||
if cancel.is_cancelled() {
|
||||
return Err(IdeError::cancelled());
|
||||
}
|
||||
let Ok(entries) = fs::read_dir(dir) else { return Ok(0) };
|
||||
let mut copied = 0;
|
||||
for e in entries.flatten() {
|
||||
let path = e.path();
|
||||
let Ok(t) = e.file_type() else { continue };
|
||||
if t.is_dir() {
|
||||
// The output folder can sit inside a source root (`src` holding `classes`).
|
||||
if path != to && !path.starts_with(to) {
|
||||
copied += copy_resources(base, &path, to, cancel)?;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if !t.is_file() || path.extension().is_some_and(|x| x == "java") {
|
||||
continue;
|
||||
}
|
||||
let target = to.join(path.strip_prefix(base).unwrap_or(&path));
|
||||
let newer =
|
||||
match (fs::metadata(&path).and_then(|m| m.modified()), fs::metadata(&target).and_then(|m| m.modified())) {
|
||||
(Ok(source), Ok(existing)) => source > existing,
|
||||
_ => true,
|
||||
};
|
||||
if newer {
|
||||
if let Some(parent) = target.parent() {
|
||||
fs::create_dir_all(parent)
|
||||
.map_err(|e| IdeError::internal(format!("cannot create {} ({e})", parent.display())))?;
|
||||
}
|
||||
fs::copy(&path, &target)
|
||||
.map_err(|e| IdeError::internal(format!("cannot copy {} ({e})", path.display())))?;
|
||||
copied += 1;
|
||||
}
|
||||
}
|
||||
Ok(copied)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use ide_core::settings::{FacetConfig, ModuleDependency, ProjectLibrary};
|
||||
|
||||
fn write(path: &Path, text: &str) {
|
||||
fs::create_dir_all(path.parent().unwrap()).unwrap();
|
||||
fs::write(path, text).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reads_language_levels() {
|
||||
assert_eq!(language_release("17"), Some(17));
|
||||
assert_eq!(language_release("1.8"), Some(8));
|
||||
assert_eq!(language_release("JDK_11"), Some(11));
|
||||
assert_eq!(language_release(""), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn plans_javac_over_source_roots_with_libraries_and_web_lib() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let root = tmp.path().join("shop");
|
||||
write(&root.join("src/com/a/A.java"), "package com.a; class A {}");
|
||||
write(&root.join("src/com/a/app.properties"), "x=1");
|
||||
write(&root.join("test/com/a/ATest.java"), "package com.a; class ATest {}");
|
||||
write(&root.join("WebRoot/WEB-INF/web.xml"), "<web-app/>");
|
||||
write(&root.join("WebRoot/WEB-INF/lib/x.jar"), "");
|
||||
let ps = ProjectStructureSettings {
|
||||
libraries: vec![
|
||||
ProjectLibrary { name: "tools".into(), classes: vec!["lib/*.jar".into()], ..Default::default() },
|
||||
ProjectLibrary {
|
||||
name: "junit".into(),
|
||||
classes: vec!["junit.jar".into()],
|
||||
scope: DependencyScope::Test,
|
||||
..Default::default()
|
||||
},
|
||||
],
|
||||
facets: vec![FacetConfig {
|
||||
kind: "web".into(),
|
||||
module: ".".into(),
|
||||
descriptor: "WebRoot/WEB-INF/web.xml".into(),
|
||||
web_root: String::new(),
|
||||
}],
|
||||
..Default::default()
|
||||
};
|
||||
let roots =
|
||||
StructureRoots { sources: vec![root.join("src")], tests: vec![root.join("test")], ..Default::default() };
|
||||
let jdk = StructureJdk { home: PathBuf::from("/jdk"), major: 21 };
|
||||
let plan = structure_plan(&root, &ps, &roots, Some(&jdk), "17", BuildKind::Rebuild, &tmp.path().join("cache"))
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
assert_eq!(plan.system, BuildSystem::Javac);
|
||||
let shown: Vec<String> = plan.steps.iter().map(BuildStep::display).collect();
|
||||
assert_eq!(shown[0], "Clean out/production/shop, out/test/shop");
|
||||
let javac = &plan.steps[1];
|
||||
let cp = |s: &BuildStep| s.args[s.args.iter().position(|a| a == "-cp").unwrap() + 1].clone();
|
||||
let sep = if cfg!(windows) { ";" } else { ":" };
|
||||
let main_cp = cp(javac);
|
||||
let entries: Vec<&str> = main_cp.split(sep).collect();
|
||||
assert_eq!(entries.len(), 2, "{main_cp}");
|
||||
assert!(Path::new(entries[0]).ends_with("lib/*"));
|
||||
assert!(Path::new(entries[1]).ends_with("WEB-INF/lib/*"));
|
||||
assert!(javac.args.windows(2).any(|w| w == ["--release", "17"]));
|
||||
let args = fs::read_to_string(tmp.path().join("cache/javac/production.args")).unwrap();
|
||||
assert!(args.contains("A.java") && !args.contains("ATest.java"));
|
||||
assert_eq!(shown[2], "Copy resources to out/production/shop");
|
||||
let test = &plan.steps[3];
|
||||
let test_cp = cp(test);
|
||||
assert_eq!(Path::new(test_cp.split(sep).next().unwrap()), root.join("out/production/shop"));
|
||||
assert!(test_cp.contains("junit.jar"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_module_dependency_gives_its_library_a_scope_and_jdk_8_gets_source_and_target() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let root = tmp.path().join("p");
|
||||
write(&root.join("src/A.java"), "class A {}");
|
||||
let mut ps = ProjectStructureSettings {
|
||||
libraries: vec![ProjectLibrary {
|
||||
name: "junit".into(),
|
||||
classes: vec!["junit.jar".into()],
|
||||
..Default::default()
|
||||
}],
|
||||
compiler_output: "build/classes".into(),
|
||||
..Default::default()
|
||||
};
|
||||
ps.modules.insert(
|
||||
".".into(),
|
||||
ModuleStructureSettings {
|
||||
language_level: "1.8".into(),
|
||||
dependencies: vec![ModuleDependency {
|
||||
kind: "library".into(),
|
||||
target: "junit".into(),
|
||||
scope: DependencyScope::Test,
|
||||
exported: false,
|
||||
}],
|
||||
..Default::default()
|
||||
},
|
||||
);
|
||||
let roots = StructureRoots { sources: vec![root.join("src")], ..Default::default() };
|
||||
let jdk = StructureJdk { home: PathBuf::from("/jdk8"), major: 8 };
|
||||
let plan = structure_plan(&root, &ps, &roots, Some(&jdk), "21", BuildKind::Build, &tmp.path().join("c"))
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
let javac = &plan.steps[0];
|
||||
assert!(!javac.args.iter().any(|a| a == "-cp"), "{:?}", javac.args);
|
||||
assert!(javac.args.windows(4).any(|w| w == ["-source", "1.8", "-target", "1.8"]));
|
||||
assert!(
|
||||
javac.args.windows(2).any(|w| w[0] == "-d" && Path::new(&w[1]).ends_with("build/classes/production/p"))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_plan_without_java_sources() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let roots = StructureRoots { sources: vec![tmp.path().join("src")], ..Default::default() };
|
||||
let plan =
|
||||
structure_plan(tmp.path(), &Default::default(), &roots, None, "", BuildKind::Build, &tmp.path().join("c"));
|
||||
assert!(plan.unwrap().is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn copies_resources_but_not_sources_and_skips_up_to_date_files() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let src = tmp.path().join("src");
|
||||
write(&src.join("a/A.java"), "class A {}");
|
||||
write(&src.join("a/x.xml"), "<x/>");
|
||||
let out = tmp.path().join("out");
|
||||
let action = Action::CopyResources { from: vec![src], to: out.clone() };
|
||||
let cancel = CancellationToken::new();
|
||||
assert_eq!(action.run(&cancel).unwrap(), "1 resource file(s) copied");
|
||||
assert!(out.join("a/x.xml").is_file() && !out.join("a/A.java").exists());
|
||||
assert_eq!(action.run(&cancel).unwrap(), "0 resource file(s) copied");
|
||||
Action::Clean(vec![out.clone()]).run(&cancel).unwrap();
|
||||
assert!(!out.exists());
|
||||
}
|
||||
|
||||
fn entries(archive: &Path) -> Vec<String> {
|
||||
let mut zip = zip::ZipArchive::new(fs::File::open(archive).unwrap()).unwrap();
|
||||
(0..zip.len()).map(|i| zip.by_index(i).unwrap().name().unwrap().into_owned()).collect()
|
||||
}
|
||||
|
||||
fn run_plan(plan: &BuildPlan) {
|
||||
let cancel = CancellationToken::new();
|
||||
for step in &plan.steps {
|
||||
if let Some((action, _)) = &step.action {
|
||||
action.run(&cancel).unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lays_out_war_jar_and_exploded_artifacts() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let root = tmp.path().join("shop");
|
||||
write(&root.join("WebRoot/index.jsp"), "hi");
|
||||
write(&root.join("WebRoot/WEB-INF/web.xml"), "<web-app/>");
|
||||
write(&root.join("WebRoot/WEB-INF/lib/inside.jar"), "");
|
||||
write(&root.join("lib/extra.jar"), "");
|
||||
let classes = root.join("out/production/shop");
|
||||
write(&classes.join("com/a/A.class"), "");
|
||||
let artifact = |name: &str, kind| ArtifactConfig {
|
||||
name: name.into(),
|
||||
kind,
|
||||
module: ".".into(),
|
||||
include_libraries: vec!["extra".into()],
|
||||
main_class: "com.a.A".into(),
|
||||
..Default::default()
|
||||
};
|
||||
let ps = ProjectStructureSettings {
|
||||
libraries: vec![ProjectLibrary {
|
||||
name: "extra".into(),
|
||||
classes: vec!["lib/*.jar".into()],
|
||||
..Default::default()
|
||||
}],
|
||||
facets: vec![FacetConfig {
|
||||
kind: "web".into(),
|
||||
module: ".".into(),
|
||||
descriptor: "WebRoot/WEB-INF/web.xml".into(),
|
||||
web_root: String::new(),
|
||||
}],
|
||||
artifacts: vec![
|
||||
artifact("shop:war", ArtifactKind::War),
|
||||
artifact("shop:war exploded", ArtifactKind::WarExploded),
|
||||
artifact("shop:jar", ArtifactKind::Jar),
|
||||
],
|
||||
..Default::default()
|
||||
};
|
||||
let cache = tmp.path().join("cache");
|
||||
for a in &ps.artifacts {
|
||||
let plan = BuildPlan {
|
||||
system: BuildSystem::Javac,
|
||||
title: String::new(),
|
||||
steps: artifact_steps(&root, &ps, a, &classes, None, &cache),
|
||||
};
|
||||
run_plan(&plan);
|
||||
}
|
||||
let war = entries(&root.join("out/artifacts/shop_war/shop.war"));
|
||||
for name in [
|
||||
"index.jsp",
|
||||
"WEB-INF/web.xml",
|
||||
"WEB-INF/lib/inside.jar",
|
||||
"WEB-INF/lib/extra.jar",
|
||||
"WEB-INF/classes/com/a/A.class",
|
||||
] {
|
||||
assert!(war.iter().any(|e| e == name), "{name} in {war:?}");
|
||||
}
|
||||
let exploded = root.join("out/artifacts/shop_war_exploded");
|
||||
assert!(exploded.join("WEB-INF/classes/com/a/A.class").is_file());
|
||||
assert!(exploded.join("WEB-INF/lib/extra.jar").is_file());
|
||||
let jar_dir = root.join("out/artifacts/shop_jar");
|
||||
let jar = entries(&jar_dir.join("shop.jar"));
|
||||
assert_eq!(jar[..2], ["META-INF/".to_owned(), "META-INF/MANIFEST.MF".to_owned()]);
|
||||
assert!(jar.iter().any(|e| e == "com/a/A.class"));
|
||||
assert!(jar_dir.join("extra.jar").is_file());
|
||||
let mut zip = zip::ZipArchive::new(fs::File::open(jar_dir.join("shop.jar")).unwrap()).unwrap();
|
||||
let mut manifest = String::new();
|
||||
std::io::Read::read_to_string(&mut zip.by_name("META-INF/MANIFEST.MF").unwrap(), &mut manifest).unwrap();
|
||||
assert!(manifest.contains("Main-Class: com.a.A") && manifest.contains("Class-Path: extra.jar"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn never_lays_out_over_its_own_sources() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let web = tmp.path().join("WebRoot");
|
||||
write(&web.join("a.jsp"), "");
|
||||
let action = Action::Layout { into: tmp.path().to_path_buf(), entries: vec![(web.clone(), PathBuf::new())] };
|
||||
assert!(action.run(&CancellationToken::new()).is_err());
|
||||
assert!(web.join("a.jsp").is_file());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,253 @@
|
||||
//! Up-to-date checks for menu builds (`Build Project` / `Build Module`, #2476).
|
||||
//!
|
||||
//! Menu builds have no fingerprint record on the launch path, so a second
|
||||
//! click always pays a full offline Maven run. This module stores one JSON
|
||||
//! record per scope (project, or one module file's build) after a successful
|
||||
//! build and compares it before the next build; on a match the build is
|
||||
//! skipped entirely. Like launch reuse, timestamp-preserving external edits
|
||||
//! are re-stamped first so Maven actually recompiles them on the next run.
|
||||
|
||||
use super::*;
|
||||
use std::fs;
|
||||
|
||||
/// Record of the last successful menu build of one scope.
|
||||
#[derive(serde::Serialize, serde::Deserialize)]
|
||||
struct Record {
|
||||
fingerprint: LaunchFingerprint,
|
||||
/// Output folders of the built projects that existed after the build, relative to the
|
||||
/// project. Deleting one outside the IDE (`mvn clean` in a terminal) leaves every input
|
||||
/// unchanged, so a build is skipped only while they are all still there.
|
||||
#[serde(default)]
|
||||
outputs: Vec<PathBuf>,
|
||||
}
|
||||
|
||||
/// The build's output folders that exist now, relative to `root`.
|
||||
fn existing_outputs(root: &Path, fingerprint: &LaunchFingerprint) -> Vec<PathBuf> {
|
||||
fingerprint
|
||||
.project_dirs()
|
||||
.flat_map(|dir| ["target/classes", "target/test-classes"].map(|out| dir.join(out)))
|
||||
.filter(|rel| root.join(rel).is_dir())
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn record_path(cache: &Path, root: &Path, file: Option<&Path>) -> PathBuf {
|
||||
let base = cache.join("menu-build").join("records");
|
||||
match file {
|
||||
Some(f) => base.join(f.strip_prefix(root).unwrap_or(f)).with_extension("json"),
|
||||
None => base.join("project.json"),
|
||||
}
|
||||
}
|
||||
|
||||
/// Re-stamps files whose content changed without advancing mtime (git
|
||||
/// checkout, external sync): timestamp-incremental Maven would otherwise skip
|
||||
/// them as "unchanged" and the rebuild would compile nothing.
|
||||
fn restamp(root: &Path, edits: &[PathBuf]) {
|
||||
let now = std::time::SystemTime::now();
|
||||
for rel in edits {
|
||||
let path = root.join(rel);
|
||||
let _ = fs::File::options().write(true).open(&path).and_then(|f| f.set_modified(now)).or_else(|_| {
|
||||
fs::OpenOptions::new()
|
||||
.create(true)
|
||||
.truncate(false)
|
||||
.write(true)
|
||||
.open(&path)
|
||||
.and_then(|f| f.set_modified(now))
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether the last successful menu build of this scope is still fresh: its
|
||||
/// record exists and the fingerprint matches. On a mismatch caused by
|
||||
/// timestamp-preserving external edits the files are re-stamped first, so the
|
||||
/// rebuild Maven runs actually recompiles them.
|
||||
pub fn menu_up_to_date(
|
||||
cache: &Path,
|
||||
root: &Path,
|
||||
model: &BuildModel,
|
||||
file: Option<&Path>,
|
||||
cancel: &CancellationToken,
|
||||
) -> bool {
|
||||
let current = match menu_fingerprint(root, model, file, cancel) {
|
||||
Ok(Some(current)) => current,
|
||||
_ => return false,
|
||||
};
|
||||
let bytes = match fs::read(record_path(cache, root, file)) {
|
||||
Ok(bytes) => bytes,
|
||||
_ => return false,
|
||||
};
|
||||
let record: Record = match serde_json::from_slice(&bytes) {
|
||||
Ok(record) => record,
|
||||
_ => return false,
|
||||
};
|
||||
if record.fingerprint != current {
|
||||
restamp(root, &record.fingerprint.mtime_preserved_edits(¤t));
|
||||
return false;
|
||||
}
|
||||
record.outputs.iter().all(|out| root.join(out).is_dir())
|
||||
}
|
||||
|
||||
/// Records a successful menu build as the baseline for future up-to-date
|
||||
/// checks. Failures are swallowed: a missing record only costs one Maven run.
|
||||
pub fn record_menu_build(
|
||||
cache: &Path,
|
||||
root: &Path,
|
||||
model: &BuildModel,
|
||||
file: Option<&Path>,
|
||||
cancel: &CancellationToken,
|
||||
) {
|
||||
let current = match menu_fingerprint(root, model, file, cancel) {
|
||||
Ok(Some(current)) => current,
|
||||
_ => return,
|
||||
};
|
||||
// A torn write fails to parse later and is treated as "no record", so a
|
||||
// plain write is enough.
|
||||
let path = record_path(cache, root, file);
|
||||
if path.parent().is_some_and(|p| fs::create_dir_all(p).is_err()) {
|
||||
return;
|
||||
}
|
||||
let outputs = existing_outputs(root, ¤t);
|
||||
let bytes = match serde_json::to_vec(&Record { fingerprint: current, outputs }) {
|
||||
Ok(bytes) => bytes,
|
||||
_ => return,
|
||||
};
|
||||
let _ = fs::write(&path, bytes);
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn write(root: &Path, rel: &str, value: &str) {
|
||||
let path = root.join(rel);
|
||||
fs::create_dir_all(path.parent().unwrap()).unwrap();
|
||||
fs::write(path, value).unwrap();
|
||||
}
|
||||
|
||||
fn fixture(root: &Path) -> BuildModel {
|
||||
maven_two_module(root, "int x;");
|
||||
BuildModel { maven: Some(maven::load(root)), ..Default::default() }
|
||||
}
|
||||
|
||||
fn maven_two_module(root: &Path, extra_app: &str) {
|
||||
write(
|
||||
root,
|
||||
"pom.xml",
|
||||
r#"<project><modelVersion>4.0.0</modelVersion><groupId>t</groupId><artifactId>root</artifactId><version>1</version><packaging>pom</packaging><modules><module>app</module><module>lib</module></modules></project>"#,
|
||||
);
|
||||
write(
|
||||
root,
|
||||
"app/pom.xml",
|
||||
r#"<project><modelVersion>4.0.0</modelVersion><parent><groupId>t</groupId><artifactId>root</artifactId><version>1</version></parent><artifactId>app</artifactId><dependencies><dependency><groupId>t</groupId><artifactId>lib</artifactId><version>1</version></dependency></dependencies></project>"#,
|
||||
);
|
||||
write(
|
||||
root,
|
||||
"lib/pom.xml",
|
||||
r#"<project><modelVersion>4.0.0</modelVersion><parent><groupId>t</groupId><artifactId>root</artifactId><version>1</version></parent><artifactId>lib</artifactId></project>"#,
|
||||
);
|
||||
write(root, "app/src/main/java/t/A.java", &format!("package t; public class A {{ {extra_app} }}"));
|
||||
write(root, "lib/src/main/java/t/L.java", "package t; public class L {}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn record_then_skip_without_changes() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let root = dir.path();
|
||||
let model = fixture(root);
|
||||
let cache = root.join("cache");
|
||||
let file = root.join("app/src/main/java/t/A.java");
|
||||
let cancel = CancellationToken::new();
|
||||
|
||||
assert!(!menu_up_to_date(&cache, root, &model, Some(&file), &cancel));
|
||||
record_menu_build(&cache, root, &model, Some(&file), &cancel);
|
||||
assert!(menu_up_to_date(&cache, root, &model, Some(&file), &cancel));
|
||||
// The project scope is independent of the module scope.
|
||||
assert!(!menu_up_to_date(&cache, root, &model, None, &cancel));
|
||||
record_menu_build(&cache, root, &model, None, &cancel);
|
||||
assert!(menu_up_to_date(&cache, root, &model, None, &cancel));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deleted_outputs_invalidate_the_record() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let root = dir.path();
|
||||
let model = fixture(root);
|
||||
let cache = root.join("cache");
|
||||
let cancel = CancellationToken::new();
|
||||
for out in ["app/target/classes/t", "lib/target/classes/t", "app/target/test-classes"] {
|
||||
fs::create_dir_all(root.join(out)).unwrap();
|
||||
}
|
||||
record_menu_build(&cache, root, &model, None, &cancel);
|
||||
assert!(menu_up_to_date(&cache, root, &model, None, &cancel));
|
||||
// `mvn clean` outside the IDE: no input changed, but the classes are gone.
|
||||
fs::remove_dir_all(root.join("lib/target")).unwrap();
|
||||
assert!(!menu_up_to_date(&cache, root, &model, None, &cancel));
|
||||
fs::create_dir_all(root.join("lib/target/classes")).unwrap();
|
||||
assert!(menu_up_to_date(&cache, root, &model, None, &cancel));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn edit_invalidates_and_rerecord_restores_skip() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let root = dir.path();
|
||||
let model = fixture(root);
|
||||
let cache = root.join("cache");
|
||||
let file = root.join("app/src/main/java/t/A.java");
|
||||
let cancel = CancellationToken::new();
|
||||
|
||||
record_menu_build(&cache, root, &model, Some(&file), &cancel);
|
||||
assert!(menu_up_to_date(&cache, root, &model, Some(&file), &cancel));
|
||||
write(root, "app/src/main/java/t/A.java", "package t; public class A { int y; }");
|
||||
assert!(!menu_up_to_date(&cache, root, &model, Some(&file), &cancel));
|
||||
record_menu_build(&cache, root, &model, Some(&file), &cancel);
|
||||
assert!(menu_up_to_date(&cache, root, &model, Some(&file), &cancel));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mtime_preserved_edit_forces_rebuild_and_restamps() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let root = dir.path();
|
||||
let model = fixture(root);
|
||||
let cache = root.join("cache");
|
||||
let rel = "app/src/main/java/t/A.java";
|
||||
let file = root.join(rel);
|
||||
let cancel = CancellationToken::new();
|
||||
|
||||
record_menu_build(&cache, root, &model, Some(&file), &cancel);
|
||||
let before = fs::metadata(&file).unwrap().modified().unwrap();
|
||||
// Same length, same mtime, different content: an external edit that
|
||||
// preserved the timestamp.
|
||||
write(root, rel, "package t; public class A { int z; }");
|
||||
fs::File::options().write(true).open(&file).and_then(|f| f.set_modified(before)).unwrap();
|
||||
assert!(!menu_up_to_date(&cache, root, &model, Some(&file), &cancel));
|
||||
assert!(fs::metadata(&file).unwrap().modified().unwrap() > before);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn corrupt_record_never_skips() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let root = dir.path();
|
||||
let model = fixture(root);
|
||||
let cache = root.join("cache");
|
||||
let file = root.join("app/src/main/java/t/A.java");
|
||||
let cancel = CancellationToken::new();
|
||||
|
||||
fs::create_dir_all(cache.join("menu-build").join("records").join("app/src/main/java/t")).unwrap();
|
||||
fs::write(cache.join("menu-build").join("records").join("app/src/main/java/t/A.json"), b"{not json").unwrap();
|
||||
assert!(!menu_up_to_date(&cache, root, &model, Some(&file), &cancel));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unsupported_builds_never_skip() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let root = dir.path();
|
||||
let model = fixture(root);
|
||||
write(root, "other/Main.java", "public class Main {}");
|
||||
let cache = root.join("cache");
|
||||
let cancel = CancellationToken::new();
|
||||
|
||||
// A file outside any non-aggregator module has no fingerprint.
|
||||
let stray = root.join("other/Main.java");
|
||||
record_menu_build(&cache, root, &model, Some(&stray), &cancel);
|
||||
assert!(!menu_up_to_date(&cache, root, &model, Some(&stray), &cancel));
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,24 @@ def canon = { File f -> f.canonicalPath }
|
||||
// the identity path (`:lib:core`, `:buildSrc`) tells them apart and addresses their tasks
|
||||
// from the root build. Internal API, hence the fallback.
|
||||
def identity = { Project p -> try { p.identityPath.toString() } catch (ignored) { p.path } }
|
||||
// The task copying a source set's resources and its output folder, when it changes them on the
|
||||
// way (filters, `expand`, renames, includes / excludes, extra sources): jdtls copies resources as
|
||||
// they are, so launches take them from Gradle. Internal API, hence the fallback.
|
||||
def processedResources = { Project p, s ->
|
||||
try {
|
||||
def task = p.tasks.findByName(s.processResourcesTaskName)
|
||||
if (task == null) return null
|
||||
def specs = 0
|
||||
def changes = task.mainSpec.sourcePaths.size() > 1
|
||||
task.rootSpec.buildRootResolver().walk { r ->
|
||||
specs++
|
||||
changes |= !r.allCopyActions.isEmpty() || !r.allIncludes.isEmpty() || !r.allExcludes.isEmpty() ||
|
||||
!r.allIncludeSpecs.isEmpty() || !r.allExcludeSpecs.isEmpty() || r.destPath.segments.length > 0
|
||||
}
|
||||
// The root and main specs exist for every task; more come from `from(…) { … }`.
|
||||
(changes || specs > 2) ? [task: task.name, dir: canon(task.destinationDir)] : null
|
||||
} catch (ignored) { null }
|
||||
}
|
||||
|
||||
def describe = { Project p ->
|
||||
def m = [path: identity(p), dir: canon(p.projectDir), buildDir: canon(p.layout.buildDirectory.get().asFile)]
|
||||
@@ -26,7 +44,10 @@ def describe = { Project p ->
|
||||
def test = s.name == 'test' || s.name == 'testFixtures' || suites.contains(s.name) ||
|
||||
s.name.endsWith('Test') || s.name.endsWith('Tests') ||
|
||||
(!src.isEmpty() && src.every { ideaTests.contains(it) })
|
||||
[name: s.name, test: test, src: src, res: res]
|
||||
def set = [name: s.name, test: test, src: src, res: res]
|
||||
def processed = processedResources(p, s)
|
||||
if (processed != null) set.processed = processed
|
||||
set
|
||||
}
|
||||
}
|
||||
try {
|
||||
|
||||
@@ -204,7 +204,7 @@ pub fn parse_settings(text: &str) -> Settings {
|
||||
}
|
||||
|
||||
/// `a/b/../c` → `a/c`, without touching the filesystem.
|
||||
fn normalize(p: &Path) -> PathBuf {
|
||||
pub(crate) fn normalize(p: &Path) -> PathBuf {
|
||||
let mut out = PathBuf::new();
|
||||
for c in p.components() {
|
||||
match c {
|
||||
@@ -335,6 +335,19 @@ pub struct ReportedSourceSet {
|
||||
/// Absolute resource folders.
|
||||
#[serde(default)]
|
||||
pub res: Vec<String>,
|
||||
/// Set when Gradle changes the resources while copying them (filters, `expand`, renames,
|
||||
/// includes / excludes, extra sources), which the Java language server's copy does not.
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub processed: Option<ProcessedResources>,
|
||||
}
|
||||
|
||||
/// The task that copies a source set's resources, and where it writes them.
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct ProcessedResources {
|
||||
/// Task name in its project (`processResources`).
|
||||
pub task: String,
|
||||
/// Absolute output folder.
|
||||
pub dir: String,
|
||||
}
|
||||
|
||||
const MODEL_LINE: &str = "@openidea-model\t";
|
||||
@@ -1450,7 +1463,7 @@ dependencies {
|
||||
model.projects.push(project);
|
||||
}
|
||||
assert!(build_files(root, &model).contains(&root.join("included/deps.gradle")));
|
||||
let model = crate::BuildModel { maven: None, gradle: vec![model] };
|
||||
let model = crate::BuildModel { maven: None, gradle: vec![model], sbt: Vec::new() };
|
||||
assert_eq!(crate::dependency_coordinates(root, &model)[0].artifact_id, "included");
|
||||
}
|
||||
|
||||
|
||||
@@ -536,6 +536,7 @@ mod tests {
|
||||
crate::maven::LOCAL_REPO_TRACKING_ARG,
|
||||
"-o",
|
||||
"-Peditor",
|
||||
"-Djacoco.skip=true",
|
||||
"test-compile"
|
||||
]
|
||||
);
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
//! Build tool models for the Maven and Gradle tool windows, and Build › Build
|
||||
//! Build tool models for the Maven, Gradle and sbt tool windows, and Build › Build
|
||||
//! Project ([`compile`]) for Maven, Gradle, Cargo, Go and TypeScript projects.
|
||||
//!
|
||||
//! [`load`] reads the project's build files (instant, no build tool involved)
|
||||
@@ -13,6 +13,7 @@ pub mod kotlin_bridge;
|
||||
pub mod maven;
|
||||
pub mod pom_edit;
|
||||
mod process;
|
||||
pub mod sbt;
|
||||
mod sync;
|
||||
pub mod web;
|
||||
mod xml;
|
||||
@@ -20,6 +21,7 @@ mod xml;
|
||||
pub use gradle::{GradleModel, GradleProject, GradleTask};
|
||||
pub use gradle_deps::{GradleDependency, gradle_dependencies};
|
||||
pub use maven::{MavenDependency, MavenModel, MavenPlugin, MavenProfile, MavenProject, Omission};
|
||||
pub use sbt::{SbtBuild, SbtProject};
|
||||
pub use sync::{
|
||||
SyncOptions, SyncProgress, maven_profiles, save_maven_profiles, sync_gradle, sync_gradle_model, sync_maven,
|
||||
};
|
||||
@@ -42,6 +44,9 @@ pub struct BuildModel {
|
||||
pub maven: Option<MavenModel>,
|
||||
#[serde(default)]
|
||||
pub gradle: Vec<GradleModel>,
|
||||
/// sbt builds, read from their `build.sbt` (sbt is not started).
|
||||
#[serde(default)]
|
||||
pub sbt: Vec<SbtBuild>,
|
||||
}
|
||||
|
||||
impl BuildModel {
|
||||
@@ -96,7 +101,7 @@ pub fn load_linked(
|
||||
g
|
||||
})
|
||||
.collect();
|
||||
BuildModel { maven, gradle }
|
||||
BuildModel { maven, gradle, sbt: sbt::load(root) }
|
||||
}
|
||||
|
||||
/// Gradle project folders outside `root` as of the last sync (`includeFlat` siblings):
|
||||
@@ -122,6 +127,14 @@ pub fn gradle_external_roots(
|
||||
gradle::external_roots(root, dirs.iter().map(String::as_str))
|
||||
}
|
||||
|
||||
/// Directories of Maven POMs linked from outside `root`, outermost only.
|
||||
/// Reads just those explicit paths (no POM parse and no directory walk).
|
||||
pub fn maven_external_roots(root: &Path, linked: &[String]) -> Vec<PathBuf> {
|
||||
let dirs = maven::external_pom_dirs(root, linked);
|
||||
let owned: Vec<String> = dirs.iter().map(|dir| dir.to_string_lossy().into_owned()).collect();
|
||||
gradle::external_roots(root, owned.iter().map(String::as_str))
|
||||
}
|
||||
|
||||
/// Source, resource and build folders of the last Gradle sync, absolute.
|
||||
pub fn gradle_source_roots(
|
||||
root: &Path,
|
||||
@@ -321,7 +334,7 @@ mod tests {
|
||||
..Default::default()
|
||||
};
|
||||
let gradle = gradle::load(root).into_iter().collect();
|
||||
let model = BuildModel { maven: Some(maven), gradle };
|
||||
let model = BuildModel { maven: Some(maven), gradle, sbt: Vec::new() };
|
||||
let names: Vec<String> = dependency_coordinates(root, &model)
|
||||
.into_iter()
|
||||
.map(|c| format!("{}{}:{}:{}", if c.gradle { "G " } else { "M " }, c.group_id, c.artifact_id, c.version))
|
||||
@@ -340,7 +353,7 @@ mod tests {
|
||||
"ext { gson = '2.8.9' }\ndependencies { implementation \"com.google.code.gson:gson:${gson}\" }\n",
|
||||
)
|
||||
.unwrap();
|
||||
let model = BuildModel { maven: None, gradle: gradle::load(root).into_iter().collect() };
|
||||
let model = BuildModel { maven: None, gradle: gradle::load(root).into_iter().collect(), sbt: Vec::new() };
|
||||
let names: Vec<String> = dependency_coordinates(root, &model)
|
||||
.into_iter()
|
||||
.map(|c| format!("{}:{}:{}", c.group_id, c.artifact_id, c.version))
|
||||
@@ -359,7 +372,7 @@ mod tests {
|
||||
)
|
||||
.unwrap();
|
||||
std::fs::write(root.join("gradle.properties"), "# versions\nv = 1.2\nonlyInProperties: 2.3\n").unwrap();
|
||||
let model = BuildModel { maven: None, gradle: gradle::load(root).into_iter().collect() };
|
||||
let model = BuildModel { maven: None, gradle: gradle::load(root).into_iter().collect(), sbt: Vec::new() };
|
||||
let versions: Vec<_> = dependency_coordinates(root, &model).into_iter().map(|c| c.version).collect();
|
||||
assert_eq!(versions, ["1.2", "2.3"]);
|
||||
}
|
||||
|
||||
+547
-55
@@ -16,6 +16,7 @@ use std::io::Read;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::{Mutex, OnceLock};
|
||||
|
||||
pub mod completion;
|
||||
pub mod sources;
|
||||
|
||||
/// Lets the IDE's own Maven runs (Reload, Build) use what the local repository already
|
||||
@@ -106,7 +107,9 @@ pub struct MavenProject {
|
||||
pub profiles: Vec<String>,
|
||||
/// Java feature version the project compiles for (`maven.compiler.release`, `java.version`…).
|
||||
pub java_version: Option<u32>,
|
||||
/// Maven launcher: the nearest `mvnw` up to the project root (absolute), else `mvn`.
|
||||
/// Maven launcher: the nearest `mvnw` (absolute), else `mvn`. Inside the opened
|
||||
/// project the search stops at its root; outside it, the search stops before
|
||||
/// that root so a linked project keeps its own wrapper.
|
||||
pub command: String,
|
||||
/// Options before the goals (`-s`, `-Dmaven.repo.local=`); filled in by the shell from settings.
|
||||
pub options: Vec<String>,
|
||||
@@ -297,7 +300,7 @@ fn read_plugin(e: &Element) -> Option<RawPlugin> {
|
||||
})
|
||||
}
|
||||
|
||||
fn read_properties(e: Option<&Element>) -> Vec<(String, String)> {
|
||||
pub(crate) fn read_properties(e: Option<&Element>) -> Vec<(String, String)> {
|
||||
e.map(|p| p.children.iter().map(|c| (c.name.clone(), c.text.trim().to_owned())).collect()).unwrap_or_default()
|
||||
}
|
||||
|
||||
@@ -709,18 +712,27 @@ pub(crate) fn launch_inputs(
|
||||
// Profile activation may depend on the presence of an otherwise unrelated file.
|
||||
let activation = parent == "file" && matches!(el.name.as_str(), "exists" | "missing");
|
||||
if input || activation {
|
||||
// Other properties can be overridden by -D, profiles or settings.
|
||||
// Without Maven's effective roots, conservatively disable reuse for them.
|
||||
let value = el
|
||||
.text
|
||||
.trim()
|
||||
.replace("${project.basedir}", &dir.to_string_lossy())
|
||||
.replace("${basedir}", &dir.to_string_lossy());
|
||||
if value.contains("${") {
|
||||
return None;
|
||||
}
|
||||
if !value.is_empty() {
|
||||
out.push(dir.join(value));
|
||||
// A source root under Maven's build directory is generated by the build itself.
|
||||
// Skip it without resolving the property: `<build><directory>` may be custom, and
|
||||
// hashing its rewritten files would make every launch look changed.
|
||||
if !(input && el.text.contains("${project.build.directory}")) {
|
||||
// Other properties can be overridden by -D, profiles or settings. Without
|
||||
// Maven's effective model, leave them unresolved to conservatively disable reuse.
|
||||
let value = el
|
||||
.text
|
||||
.trim()
|
||||
.replace("${project.basedir}", &dir.to_string_lossy())
|
||||
.replace("${basedir}", &dir.to_string_lossy());
|
||||
if value.contains("${") {
|
||||
return None;
|
||||
}
|
||||
if !value.is_empty() {
|
||||
let path = dir.join(value);
|
||||
// Conventional generated sources under target are also build outputs.
|
||||
if !(input && path.starts_with(dir.join("target"))) {
|
||||
out.push(path);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
for child in &el.children {
|
||||
@@ -1032,10 +1044,6 @@ pub(crate) fn plugin(
|
||||
|
||||
const MAX_PROJECTS: usize = 1000;
|
||||
|
||||
fn rel_path(root: &Path, p: &Path) -> String {
|
||||
p.strip_prefix(root).unwrap_or(p).to_string_lossy().replace('\\', "/")
|
||||
}
|
||||
|
||||
/// The POMs that are not modules of another one: the root `pom.xml`, else the
|
||||
/// shallowest `pom.xml` of each branch (a folder holding several builds). An
|
||||
/// aggregate POM directly inside `src/` counts too (issue #1001: `src/pom.xml`
|
||||
@@ -1049,18 +1057,29 @@ pub fn is_maven_project(root: &Path) -> bool {
|
||||
!top_level_poms(root).is_empty()
|
||||
}
|
||||
|
||||
/// The `mvnw` nearest to `dir`, looking up to `root`.
|
||||
/// The `mvnw` nearest to `dir`. Inside `root` the walk stops at `root`. Outside it,
|
||||
/// the walk stops before entering `root` or any ancestor of `root`: a sibling project
|
||||
/// keeps its own wrapper, and the search does not continue to the filesystem root.
|
||||
fn wrapper(root: &Path, dir: &Path) -> String {
|
||||
let name = if cfg!(windows) { "mvnw.cmd" } else { "mvnw" };
|
||||
let mut cur = Some(dir);
|
||||
let root = dunce::canonicalize(root).unwrap_or_else(|_| root.to_path_buf());
|
||||
let dir = dunce::canonicalize(dir).unwrap_or_else(|_| dir.to_path_buf());
|
||||
let external = !dir.starts_with(&root);
|
||||
let mut cur = Some(dir.as_path());
|
||||
while let Some(d) = cur {
|
||||
if d.join(name).is_file() {
|
||||
return d.join(name).to_string_lossy().into_owned();
|
||||
}
|
||||
if d == root {
|
||||
if !external && d == root {
|
||||
break;
|
||||
}
|
||||
cur = d.parent();
|
||||
let Some(parent) = d.parent() else { break };
|
||||
// `parent` inside the opened project, or an ancestor of it (the common parent,
|
||||
// then the filesystem root). Stop before inspecting those.
|
||||
if external && (parent.starts_with(&root) || root.starts_with(parent)) {
|
||||
break;
|
||||
}
|
||||
cur = Some(parent);
|
||||
}
|
||||
if cfg!(windows) { "mvn.cmd".into() } else { "mvn".into() }
|
||||
}
|
||||
@@ -1104,24 +1123,118 @@ pub(crate) fn load_with(root: &Path, local_repo: Option<PathBuf>, profiles: &[St
|
||||
load_with_linked(root, local_repo, profiles, &[])
|
||||
}
|
||||
|
||||
/// The POMs of `linked` (project-relative, from Settings) that exist inside `root`.
|
||||
/// The `pom.xml` files `linked` names. Each entry is used as written (project-relative,
|
||||
/// `../sibling/pom.xml`, or absolute) and canonicalized; a path that is not an existing
|
||||
/// `pom.xml` is skipped. Nothing here lists a directory or walks parents looking for builds.
|
||||
fn linked_poms(root: &Path, linked: &[String]) -> Vec<PathBuf> {
|
||||
let base = dunce::canonicalize(root).unwrap_or_else(|_| root.to_path_buf());
|
||||
let mut out: Vec<PathBuf> = Vec::new();
|
||||
for pom in linked {
|
||||
let pom = normalized_pom(pom);
|
||||
if pom.is_empty() || pom.starts_with("../") || pom == ".." {
|
||||
continue;
|
||||
}
|
||||
let path = root.join(&pom);
|
||||
let inside = dunce::canonicalize(&path).is_ok_and(|p| p.starts_with(&base));
|
||||
if path.is_file() && inside && !out.contains(&path) {
|
||||
let Some(path) = dunce::canonicalize(base.join(pom.trim())).ok() else { continue };
|
||||
if is_pom_file(&path) && !out.contains(&path) {
|
||||
out.push(path);
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
fn is_pom_file(path: &Path) -> bool {
|
||||
path.is_file()
|
||||
&& path
|
||||
.file_name()
|
||||
.and_then(|name| name.to_str())
|
||||
.is_some_and(|name| if cfg!(windows) { name.eq_ignore_ascii_case("pom.xml") } else { name == "pom.xml" })
|
||||
}
|
||||
|
||||
/// Identity of `path` (both canonical) within `root`: project-relative inside it,
|
||||
/// otherwise the absolute path with forward slashes.
|
||||
fn project_rel(root: &Path, path: &Path) -> String {
|
||||
path.strip_prefix(root).unwrap_or(path).to_string_lossy().replace('\\', "/")
|
||||
}
|
||||
|
||||
/// Canonical `pom.xml` paths named by `linked`, keyed by the spelling to store and to
|
||||
/// show so unlink removes the same entry settings hold. The first spelling wins.
|
||||
fn linked_identities(root: &Path, linked: &[String]) -> HashMap<PathBuf, String> {
|
||||
let mut out = HashMap::new();
|
||||
for entry in linked {
|
||||
let entry = entry.trim();
|
||||
if entry.is_empty() {
|
||||
continue;
|
||||
}
|
||||
let Some(path) = dunce::canonicalize(root.join(entry)).ok() else { continue };
|
||||
if is_pom_file(&path) {
|
||||
out.entry(path).or_insert_with(|| entry.replace('\\', "/"));
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// Stable, deduplicated form of `poms` for `languages.java.maven.linkedProjects`.
|
||||
/// An existing `pom.xml` inside `root` is project-relative; one outside it is the
|
||||
/// canonical absolute path. Spellings of the same file (`../B/pom.xml`, an absolute
|
||||
/// path, a symlink) collapse to one entry. A path that exists and is not a `pom.xml`
|
||||
/// is dropped. A `pom.xml` that cannot be resolved yet is kept, so a link is not
|
||||
/// discarded while its volume is offline. No directory is scanned.
|
||||
pub fn normalize_linked_poms(root: &Path, poms: &[String]) -> Vec<String> {
|
||||
let Some(root) = dunce::canonicalize(root).ok() else {
|
||||
let mut out = Vec::new();
|
||||
for pom in poms {
|
||||
let pom = normalized_pom(pom);
|
||||
if !pom.is_empty() && pom_name_ok(&pom) && !out.contains(&pom) {
|
||||
out.push(pom);
|
||||
}
|
||||
}
|
||||
return out;
|
||||
};
|
||||
let mut out = Vec::new();
|
||||
let mut seen = HashSet::new();
|
||||
for pom in poms {
|
||||
let pom = pom.trim();
|
||||
if pom.is_empty() {
|
||||
continue;
|
||||
}
|
||||
let key = match dunce::canonicalize(root.join(pom)) {
|
||||
Ok(path) if is_pom_file(&path) => project_rel(&root, &path),
|
||||
Ok(_) => continue,
|
||||
Err(_) => {
|
||||
let fallback = normalized_pom(pom);
|
||||
let joined = root.join(pom);
|
||||
if !pom_name_ok(&fallback) || (joined.exists() && !is_pom_file(&joined)) {
|
||||
continue;
|
||||
}
|
||||
fallback
|
||||
}
|
||||
};
|
||||
if key.is_empty() || !seen.insert(key.clone()) {
|
||||
continue;
|
||||
}
|
||||
out.push(key);
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
fn pom_name_ok(path: &str) -> bool {
|
||||
let name = path.rsplit('/').next().unwrap_or(path);
|
||||
if cfg!(windows) { name.eq_ignore_ascii_case("pom.xml") } else { name == "pom.xml" }
|
||||
}
|
||||
|
||||
/// Directories of linked `pom.xml` files that sit outside `root` (the opened project
|
||||
/// stays the root). Callers keep the outermost directory only. Modules a POM declares
|
||||
/// outside its own directory are not included: a linked file cannot widen the indexed
|
||||
/// tree to an arbitrary path.
|
||||
pub fn external_pom_dirs(root: &Path, linked: &[String]) -> Vec<PathBuf> {
|
||||
let base = dunce::canonicalize(root).unwrap_or_else(|_| root.to_path_buf());
|
||||
let mut out = Vec::new();
|
||||
for pom in linked_poms(root, linked) {
|
||||
let Some(dir) = pom.parent() else { continue };
|
||||
if dir.starts_with(&base) || out.iter().any(|have| have == dir) {
|
||||
continue;
|
||||
}
|
||||
out.push(dir.to_path_buf());
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// Whether `root` holds a Maven build or the settings link one inside it.
|
||||
pub fn has_projects(root: &Path, settings: &ide_core::settings::MavenSettings) -> bool {
|
||||
is_maven_project(root) || !linked_poms(root, &settings.linked_projects).is_empty()
|
||||
@@ -1130,7 +1243,9 @@ pub fn has_projects(root: &Path, settings: &ide_core::settings::MavenSettings) -
|
||||
/// The model with the chosen `profiles` (`-P` entries; `!id` deactivates: their modules,
|
||||
/// dependencies and properties are part of the model, those turned off are not) and the
|
||||
/// `linked` POMs (Add Maven Projects): builds that no reactor lists, shown as independent
|
||||
/// top-level projects. A linked POM that turns out to be a module of the root reactor stays that module.
|
||||
/// top-level projects, including a `pom.xml` outside the opened root. A linked POM that
|
||||
/// turns out to be a module of the root reactor stays that module. The same canonical file
|
||||
/// is loaded once, so a cycle or a second spelling does not duplicate it or edit either POM.
|
||||
pub fn load_with_linked(
|
||||
root: &Path,
|
||||
local_repo: Option<PathBuf>,
|
||||
@@ -1139,7 +1254,10 @@ pub fn load_with_linked(
|
||||
) -> MavenModel {
|
||||
let mut resolver = Resolver::new(local_repo, profiles);
|
||||
let mut model = MavenModel::default();
|
||||
let mut seen: HashSet<PathBuf> = HashSet::new();
|
||||
// Canonical POM → its project, for a module listed again by a later aggregator.
|
||||
let mut seen: HashMap<PathBuf, Option<usize>> = HashMap::new();
|
||||
let canonical_root = dunce::canonicalize(root).unwrap_or_else(|_| root.to_path_buf());
|
||||
let linked_ids = linked_identities(&canonical_root, linked);
|
||||
let mut tops = top_level_poms(root);
|
||||
for pom in linked_poms(root, linked) {
|
||||
if !tops.contains(&pom) {
|
||||
@@ -1147,17 +1265,31 @@ pub fn load_with_linked(
|
||||
}
|
||||
}
|
||||
// (pom, aggregated); the root reactor and its modules come before linked POMs.
|
||||
let mut stack: Vec<(PathBuf, bool)> = tops.into_iter().rev().map(|p| (p, false)).collect();
|
||||
while let Some((path, aggregated)) = stack.pop() {
|
||||
// (pom, listed by): a module carries the top-level project its chain started from and
|
||||
// whether the aggregator listing it lies inside the opened root.
|
||||
let mut stack: Vec<(PathBuf, Option<(usize, bool)>)> = tops.into_iter().rev().map(|p| (p, None)).collect();
|
||||
while let Some((path, listed_by)) = stack.pop() {
|
||||
let aggregated = listed_by.is_some();
|
||||
if model.projects.len() >= MAX_PROJECTS {
|
||||
break;
|
||||
}
|
||||
let canonical = dunce::canonicalize(&path).unwrap_or_else(|_| path.clone());
|
||||
if !seen.insert(canonical) {
|
||||
if let Some(known) = seen.get(&canonical) {
|
||||
// Found as a top-level POM first, then listed by an aggregator elsewhere
|
||||
// (`<module>../</module>` of `parent/pom.xml`): it is that aggregator's module.
|
||||
// Not when the chain leads back to where it started (a cycle), and not for an
|
||||
// aggregator outside the opened root: a linked sibling does not take it over.
|
||||
if let (Some(known), Some((origin, true))) = (*known, listed_by)
|
||||
&& known != origin
|
||||
{
|
||||
model.projects[known].aggregated = true;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
seen.insert(canonical.clone(), None);
|
||||
let Some(pom) = resolver.pom(&path) else { continue };
|
||||
let dir = path.parent().unwrap_or(root).to_path_buf();
|
||||
let dir_canon = dunce::canonicalize(&dir).unwrap_or_else(|_| dir.clone());
|
||||
let ctx = resolver.context(&path, 0);
|
||||
let props = resolver.properties_for(&pom, &ctx, &dir);
|
||||
let i = |s: &str| interpolate(s, &props);
|
||||
@@ -1168,7 +1300,7 @@ pub fn load_with_linked(
|
||||
}
|
||||
let mut modules: Vec<PathBuf> = Vec::new();
|
||||
for m in module_names {
|
||||
let mut mp = dir.join(i(&m));
|
||||
let mut mp = crate::gradle::normalize(&dir.join(i(&m)));
|
||||
if mp.is_dir() || mp.extension().is_none_or(|e| e != "xml") {
|
||||
mp.push("pom.xml");
|
||||
}
|
||||
@@ -1232,28 +1364,39 @@ pub fn load_with_linked(
|
||||
}
|
||||
}
|
||||
let artifact_id = i(&pom.artifact_id);
|
||||
let module_ids: Vec<String> = modules
|
||||
.iter()
|
||||
.map(|m| {
|
||||
let canonical = dunce::canonicalize(m).unwrap_or_else(|_| m.clone());
|
||||
linked_ids.get(&canonical).cloned().unwrap_or_else(|| project_rel(&canonical_root, &canonical))
|
||||
})
|
||||
.collect();
|
||||
model.projects.push(MavenProject {
|
||||
project_root: None,
|
||||
pom: rel_path(root, &path),
|
||||
dir: rel_path(root, &dir),
|
||||
pom: linked_ids.get(&canonical).cloned().unwrap_or_else(|| project_rel(&canonical_root, &canonical)),
|
||||
dir: project_rel(&canonical_root, &dir_canon),
|
||||
group_id: i(props.get("project.groupId").map(String::as_str).unwrap_or_default()),
|
||||
version: i(props.get("project.version").map(String::as_str).unwrap_or_default()),
|
||||
display_name: pom.name.as_deref().map(i).filter(|n| !n.is_empty()).unwrap_or_else(|| artifact_id.clone()),
|
||||
artifact_id,
|
||||
packaging,
|
||||
parent: pom.parent.as_ref().map(|p| format!("{}:{}:{}", p.group_id, p.artifact_id, i(&p.version))),
|
||||
modules: modules.iter().map(|m| rel_path(root, m)).collect(),
|
||||
modules: module_ids,
|
||||
aggregated,
|
||||
plugins,
|
||||
dependencies,
|
||||
profiles: ctx.profiles.iter().map(|(id, _)| id.clone()).collect(),
|
||||
java_version: jv,
|
||||
command: wrapper(root, &dir),
|
||||
command: wrapper(&canonical_root, &dir_canon),
|
||||
options: Vec::new(),
|
||||
env: BTreeMap::new(),
|
||||
});
|
||||
let index = model.projects.len() - 1;
|
||||
seen.insert(canonical, Some(index));
|
||||
let origin = listed_by.map_or(index, |(origin, _)| origin);
|
||||
let inside = dir_canon.starts_with(&canonical_root);
|
||||
for m in modules.into_iter().rev() {
|
||||
stack.push((m, true));
|
||||
stack.push((m, Some((origin, inside))));
|
||||
}
|
||||
}
|
||||
model
|
||||
@@ -1341,17 +1484,27 @@ impl UnlinkedProjects {
|
||||
|
||||
fn normalized_pom(pom: &str) -> String {
|
||||
let pom = pom.replace('\\', "/");
|
||||
let mut parts = Vec::new();
|
||||
let unc = pom.starts_with("//");
|
||||
let absolute = pom.starts_with('/');
|
||||
let mut parts: Vec<&str> = Vec::new();
|
||||
for part in pom.split('/') {
|
||||
match part {
|
||||
"" | "." => {}
|
||||
".." if parts.last().is_some_and(|p| *p != "..") => {
|
||||
// Keep a drive letter (`C:`) and a leading `..` that has nothing to climb.
|
||||
".." if parts.last().is_some_and(|p| *p != ".." && !p.ends_with(':')) => {
|
||||
parts.pop();
|
||||
}
|
||||
part => parts.push(part),
|
||||
}
|
||||
}
|
||||
parts.join("/")
|
||||
let joined = parts.join("/");
|
||||
if unc {
|
||||
format!("//{joined}")
|
||||
} else if absolute {
|
||||
format!("/{joined}")
|
||||
} else {
|
||||
joined
|
||||
}
|
||||
}
|
||||
|
||||
/// Every POM of the model (for change detection).
|
||||
@@ -1536,6 +1689,15 @@ pub enum PomGotoKind {
|
||||
ManagedDependency,
|
||||
/// A `<pluginManagement>` entry providing the plugin's version.
|
||||
ManagedPlugin,
|
||||
/// The POM of the artifact itself: a project module's, else the local repository's.
|
||||
ArtifactPom,
|
||||
}
|
||||
|
||||
/// A Maven project of the workspace, for going to a dependency on it.
|
||||
pub struct ModulePom {
|
||||
pub group_id: String,
|
||||
pub artifact_id: String,
|
||||
pub pom: PathBuf,
|
||||
}
|
||||
|
||||
/// Byte offset of `pos` (0-based line / UTF-16 column), clamped to `text`.
|
||||
@@ -1586,7 +1748,7 @@ fn property_ref_at(text: &str, at: usize) -> Option<(&str, usize, usize)> {
|
||||
}
|
||||
|
||||
/// The elements containing `at`, from the root down to the innermost one.
|
||||
fn element_path(root: &Element, at: usize) -> Vec<&Element> {
|
||||
pub(crate) fn element_path(root: &Element, at: usize) -> Vec<&Element> {
|
||||
let mut path = vec![root];
|
||||
loop {
|
||||
let Some(next) = path.last().unwrap().children.iter().find(|c| c.start <= at && at < c.end) else {
|
||||
@@ -1615,6 +1777,20 @@ fn coordinates(e: &Element, props: &HashMap<String, String>) -> Option<(String,
|
||||
/// the local repository when it is not in the project). `None` when there is no
|
||||
/// resolvable declaration here.
|
||||
pub fn goto_declaration(text: &str, path: &Path, pos: Pos, local_repo: Option<PathBuf>) -> Option<PomGoto> {
|
||||
goto_declaration_in(text, path, pos, local_repo, Vec::new)
|
||||
}
|
||||
|
||||
/// [`goto_declaration`], where a `<parent>` and a dependency or plugin whose version no
|
||||
/// management entry provides (or a management entry itself) go to the artifact's own
|
||||
/// POM: the project module among `modules` with its coordinates, else the local
|
||||
/// repository's POM of its version.
|
||||
pub fn goto_declaration_in(
|
||||
text: &str,
|
||||
path: &Path,
|
||||
pos: Pos,
|
||||
local_repo: Option<PathBuf>,
|
||||
modules: impl FnOnce() -> Vec<ModulePom>,
|
||||
) -> Option<PomGoto> {
|
||||
let root = xml::parse(text).filter(|e| e.name == "project")?;
|
||||
let at = offset_at(text, pos);
|
||||
if let Some((name, _, _)) = property_ref_at(text, at)
|
||||
@@ -1625,15 +1801,56 @@ pub fn goto_declaration(text: &str, path: &Path, pos: Pos, local_repo: Option<Pa
|
||||
// A `<dependency>` / `<plugin>` in a declaration position: its version may come
|
||||
// from a management entry of this POM or an ancestor's.
|
||||
let elements = element_path(&root, at);
|
||||
let e = elements.iter().rev().find(|e| e.name == "dependency" || e.name == "plugin")?;
|
||||
if elements.iter().any(|a| a.name == "dependencyManagement" || a.name == "pluginManagement") {
|
||||
return None; // Management entries are declarations; there is nothing to go to.
|
||||
if elements.get(1).is_some_and(|e| e.name == "parent") {
|
||||
let chain = parent_chain(path, text, local_repo);
|
||||
let (pom, parent_text) = chain.get(1)?;
|
||||
let label = elements[1].text_of("artifactId").unwrap_or_default();
|
||||
return Some(pom_goto(pom.clone(), parent_text, label));
|
||||
}
|
||||
let kind = match e.name.as_str() {
|
||||
"dependency" => PomGotoKind::ManagedDependency,
|
||||
_ => PomGotoKind::ManagedPlugin,
|
||||
};
|
||||
managed_target(text, path, e, kind, local_repo)
|
||||
let e = elements.iter().rev().find(|e| e.name == "dependency" || e.name == "plugin")?;
|
||||
// A management entry declares the version: it goes to the artifact (a BOM import, say).
|
||||
if !elements.iter().any(|a| a.name == "dependencyManagement" || a.name == "pluginManagement") {
|
||||
let kind = match e.name.as_str() {
|
||||
"dependency" => PomGotoKind::ManagedDependency,
|
||||
_ => PomGotoKind::ManagedPlugin,
|
||||
};
|
||||
if let Some(found) = managed_target(text, path, e, kind, local_repo.clone()) {
|
||||
return Some(found);
|
||||
}
|
||||
}
|
||||
artifact_target(text, path, e, local_repo, modules)
|
||||
}
|
||||
|
||||
/// The artifact's own POM for a dependency or plugin element `e`.
|
||||
fn artifact_target(
|
||||
text: &str,
|
||||
path: &Path,
|
||||
e: &Element,
|
||||
local_repo: Option<PathBuf>,
|
||||
modules: impl FnOnce() -> Vec<ModulePom>,
|
||||
) -> Option<PomGoto> {
|
||||
let mut resolver = Resolver::new(local_repo.clone(), &[]);
|
||||
let chain = parent_chain(path, text, local_repo);
|
||||
let props = properties_at(&mut resolver, &chain, 0);
|
||||
let (group, artifact) = coordinates(e, &props)?;
|
||||
let label = format!("{group}:{artifact}");
|
||||
if let Some(module) = modules().into_iter().find(|m| m.group_id == group && m.artifact_id == artifact) {
|
||||
let module_text = xml::read_text(&module.pom)?;
|
||||
return Some(pom_goto(module.pom, &module_text, label));
|
||||
}
|
||||
let version = interpolate(&e.text_of("version")?, &props);
|
||||
let pom = resolver.repo_pom(&group, &artifact, &version)?;
|
||||
let pom_text = xml::read_text(&pom)?;
|
||||
Some(pom_goto(pom, &pom_text, label))
|
||||
}
|
||||
|
||||
/// The POM at `path` as a target, at its `<artifactId>`.
|
||||
fn pom_goto(path: PathBuf, text: &str, label: String) -> PomGoto {
|
||||
let range = xml::parse(text)
|
||||
.filter(|e| e.name == "project")
|
||||
.and_then(|root| root.child("artifactId").map(|a| range_of(text, a)))
|
||||
.unwrap_or_default();
|
||||
PomGoto { path, range, kind: PomGotoKind::ArtifactPom, label }
|
||||
}
|
||||
|
||||
/// The parent POMs of the POM in `text` at `path`, child-first (its `<parent>` refs,
|
||||
@@ -1932,6 +2149,52 @@ mod tests {
|
||||
assert_eq!(known.goals, ["compile", "help", "testCompile"], "the common plugins keep their table");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn launch_inputs_skip_generated_sources_under_the_project_build_directory() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let root = tmp.path();
|
||||
write(
|
||||
root,
|
||||
"pom.xml",
|
||||
"<project><groupId>demo</groupId><artifactId>parent</artifactId><version>1</version>\
|
||||
<packaging>pom</packaging><modules><module>app</module></modules></project>",
|
||||
);
|
||||
write(
|
||||
root,
|
||||
"app/pom.xml",
|
||||
r#"<project><parent><groupId>demo</groupId><artifactId>parent</artifactId><version>1</version></parent>
|
||||
<artifactId>app</artifactId><build><plugins><plugin>
|
||||
<groupId>org.codehaus.mojo</groupId><artifactId>build-helper-maven-plugin</artifactId>
|
||||
<executions><execution><configuration><sources>
|
||||
<source>${project.build.directory}/generated-sources/java</source>
|
||||
</sources></configuration></execution></executions>
|
||||
</plugin></plugins></build></project>"#,
|
||||
);
|
||||
let model = load_with_repo(root, None);
|
||||
let projects: Vec<&MavenProject> = model.projects.iter().collect();
|
||||
let inputs = launch_inputs(root, &projects, None).expect("portable launch inputs");
|
||||
assert!(!inputs.contains(&root.join("app/target/generated-sources/java")), "{inputs:?}");
|
||||
assert!(inputs.contains(&root.join("app/src")), "conventional sources stay fingerprinted");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn launch_inputs_disable_reuse_for_file_activation_on_the_build_directory() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let root = tmp.path();
|
||||
write(
|
||||
root,
|
||||
"pom.xml",
|
||||
r#"<project><groupId>demo</groupId><artifactId>app</artifactId><version>1</version>
|
||||
<build><directory>${project.basedir}/custom-build</directory></build>
|
||||
<profiles><profile><id>generated</id><activation><file>
|
||||
<exists>${project.build.directory}/enable-profile</exists>
|
||||
</file></activation></profile></profiles></project>"#,
|
||||
);
|
||||
let model = load_with_repo(root, None);
|
||||
let projects: Vec<&MavenProject> = model.projects.iter().collect();
|
||||
assert!(launch_inputs(root, &projects, None).is_none());
|
||||
}
|
||||
|
||||
fn write(dir: &Path, rel: &str, text: &str) {
|
||||
let p = dir.join(rel);
|
||||
std::fs::create_dir_all(p.parent().unwrap()).unwrap();
|
||||
@@ -2147,6 +2410,169 @@ mod tests {
|
||||
assert_eq!(top(&load_linked(deep.path(), &settings)), [("a/b/c/tool".into(), false)]);
|
||||
}
|
||||
|
||||
/// A sibling Maven project is linked beside the opened root. Its reactor, including a
|
||||
/// module that points back at the opened project, is read once. Neither POM is rewritten,
|
||||
/// and only the linked project's directory is an extra content root — not the common
|
||||
/// parent and not a module that lives outside that directory.
|
||||
#[test]
|
||||
fn external_sibling_keeps_its_directory_and_does_not_edit_poms() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let base = dunce::canonicalize(tmp.path()).unwrap();
|
||||
let a = base.join("A");
|
||||
let b = base.join("B");
|
||||
let c = base.join("C");
|
||||
std::fs::create_dir_all(a.join("core")).unwrap();
|
||||
std::fs::create_dir_all(b.join("mod")).unwrap();
|
||||
std::fs::create_dir_all(&c).unwrap();
|
||||
let a_pom = "<project><groupId>demo</groupId><artifactId>a</artifactId><version>1</version><packaging>pom</packaging><modules><module>core</module></modules></project>";
|
||||
let b_pom = "<project><groupId>demo</groupId><artifactId>b</artifactId><version>1</version><packaging>pom</packaging><modules><module>mod</module><module>../A</module><module>../C</module><module>.</module></modules></project>";
|
||||
let c_pom = "<project><groupId>demo</groupId><artifactId>c</artifactId><version>1</version><modules><module>../B</module></modules></project>";
|
||||
std::fs::write(a.join("pom.xml"), a_pom).unwrap();
|
||||
std::fs::write(a.join("core/pom.xml"), "<project><artifactId>core</artifactId></project>").unwrap();
|
||||
std::fs::write(b.join("pom.xml"), b_pom).unwrap();
|
||||
std::fs::write(b.join("mod/pom.xml"), "<project><artifactId>bmod</artifactId></project>").unwrap();
|
||||
std::fs::write(c.join("pom.xml"), c_pom).unwrap();
|
||||
let wrapper = if cfg!(windows) { "mvnw.cmd" } else { "mvnw" };
|
||||
std::fs::write(a.join(wrapper), "#!/bin/sh\n").unwrap();
|
||||
std::fs::write(b.join(wrapper), "#!/bin/sh\n").unwrap();
|
||||
std::fs::write(base.join(wrapper), "#!/bin/sh\n").unwrap();
|
||||
|
||||
let discovered = load_with_linked(&a, None, &[], &[]);
|
||||
assert_eq!(discovered.projects.iter().map(|p| p.artifact_id.as_str()).collect::<Vec<_>>(), ["a", "core"]);
|
||||
|
||||
let stored = vec!["../B/pom.xml".into()];
|
||||
let linked = load_with_linked(&a, None, &[], &stored);
|
||||
let b_project = linked.projects.iter().find(|p| p.artifact_id == "b").unwrap();
|
||||
assert!(!b_project.aggregated);
|
||||
assert_eq!(b_project.pom, "../B/pom.xml");
|
||||
assert_eq!(dunce::canonicalize(a.join(&b_project.dir)).unwrap(), b);
|
||||
assert_eq!(dunce::canonicalize(&b_project.command).unwrap(), dunce::canonicalize(b.join(wrapper)).unwrap());
|
||||
let a_project = linked.projects.iter().find(|p| p.artifact_id == "a").unwrap();
|
||||
assert!(!a_project.aggregated);
|
||||
assert_eq!(a_project.modules, ["core/pom.xml"]);
|
||||
assert_eq!(dunce::canonicalize(&a_project.command).unwrap(), dunce::canonicalize(a.join(wrapper)).unwrap());
|
||||
assert_eq!(linked.projects.iter().filter(|p| p.artifact_id == "a").count(), 1);
|
||||
assert_eq!(linked.projects.iter().filter(|p| p.artifact_id == "b").count(), 1);
|
||||
assert!(linked.projects.iter().any(|p| p.artifact_id == "c" && p.aggregated));
|
||||
assert!(linked.projects.iter().any(|p| p.artifact_id == "bmod" && p.aggregated));
|
||||
let again = load_with_linked(&a, None, &[], &stored);
|
||||
assert_eq!(
|
||||
again.projects.iter().map(|p| &p.pom).collect::<Vec<_>>(),
|
||||
linked.projects.iter().map(|p| &p.pom).collect::<Vec<_>>()
|
||||
);
|
||||
|
||||
let absolute = b.join("pom.xml").to_string_lossy().replace('\\', "/");
|
||||
let via_a = a.join("../B/pom.xml").to_string_lossy().replace('\\', "/");
|
||||
let normalized = normalize_linked_poms(
|
||||
&a,
|
||||
&[
|
||||
"../B/pom.xml".into(),
|
||||
absolute,
|
||||
via_a,
|
||||
a.join("pom.xml").to_string_lossy().into_owned(),
|
||||
b.to_string_lossy().into_owned(),
|
||||
"stub/pom.xml".into(),
|
||||
"notes.xml".into(),
|
||||
],
|
||||
);
|
||||
assert_eq!(normalized.len(), 3, "{normalized:?}");
|
||||
assert_eq!(normalized[0], dunce::canonicalize(b.join("pom.xml")).unwrap().to_string_lossy().replace('\\', "/"));
|
||||
assert_eq!(normalized[1], "pom.xml");
|
||||
assert_eq!(normalized[2], "stub/pom.xml");
|
||||
assert_eq!(crate::maven_external_roots(&a, &normalized), vec![b.clone()]);
|
||||
assert_eq!(crate::maven_external_roots(&a, &stored), vec![b.clone()]);
|
||||
|
||||
let removed = load_with_linked(&a, None, &[], &[]);
|
||||
assert!(removed.projects.iter().all(|p| p.artifact_id != "b"));
|
||||
assert_eq!(std::fs::read_to_string(a.join("pom.xml")).unwrap(), a_pom);
|
||||
assert_eq!(std::fs::read_to_string(b.join("pom.xml")).unwrap(), b_pom);
|
||||
assert_eq!(std::fs::read_to_string(c.join("pom.xml")).unwrap(), c_pom);
|
||||
assert!(b.join("mod/pom.xml").is_file());
|
||||
}
|
||||
|
||||
/// The launcher recorded on an external project is that project's wrapper, and
|
||||
/// running it uses the external directory as the working directory.
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn external_project_wrapper_runs_in_its_own_directory() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let base = dunce::canonicalize(tmp.path()).unwrap();
|
||||
let a = base.join("A");
|
||||
let b = base.join("B");
|
||||
std::fs::create_dir_all(b.join("src/main/java")).unwrap();
|
||||
std::fs::create_dir_all(&a).unwrap();
|
||||
std::fs::write(a.join("pom.xml"), "<project><artifactId>a</artifactId></project>").unwrap();
|
||||
std::fs::write(
|
||||
b.join("pom.xml"),
|
||||
"<project><artifactId>b</artifactId><build><sourceDirectory>src/main/java</sourceDirectory></build></project>",
|
||||
)
|
||||
.unwrap();
|
||||
std::fs::write(b.join("src/main/java/B.java"), "class B {}").unwrap();
|
||||
let script = b.join("mvnw");
|
||||
std::fs::write(&script, "#!/bin/sh\npwd > \"$1\"\nprintf 'BUILD SUCCESS\\n'\n").unwrap();
|
||||
#[cfg(unix)]
|
||||
{
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
std::fs::set_permissions(&script, std::fs::Permissions::from_mode(0o755)).unwrap();
|
||||
}
|
||||
let model = load_with_linked(&a, None, &[], &["../B/pom.xml".into()]);
|
||||
let project = model.projects.iter().find(|p| p.artifact_id == "b").unwrap();
|
||||
let cwd = a.join(&project.dir);
|
||||
assert_eq!(dunce::canonicalize(&cwd).unwrap(), b);
|
||||
let marker = tmp.path().join("cwd");
|
||||
let out = crate::process::run(
|
||||
&project.command,
|
||||
&[marker.to_string_lossy().into_owned()],
|
||||
&cwd,
|
||||
&std::collections::BTreeMap::new(),
|
||||
&ide_core::CancellationToken::new(),
|
||||
|_, _| {},
|
||||
)
|
||||
.unwrap();
|
||||
assert!(out.success, "{}", out.text);
|
||||
let recorded = dunce::canonicalize(std::fs::read_to_string(&marker).unwrap().trim()).unwrap();
|
||||
assert_eq!(recorded, b);
|
||||
}
|
||||
|
||||
/// #2877: an aggregator in a subfolder lists the root POM as `../`; linked, it holds every
|
||||
/// module, the root one included, under the same POM paths the projects have.
|
||||
#[test]
|
||||
fn an_aggregator_in_a_subfolder_holds_the_root_pom() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let root = tmp.path();
|
||||
let child = |id: &str, rel: &str| {
|
||||
format!(
|
||||
"<project><parent><groupId>demo</groupId><artifactId>parent</artifactId><version>1.0</version><relativePath>{rel}</relativePath></parent><artifactId>{id}</artifactId></project>"
|
||||
)
|
||||
};
|
||||
write(root, "pom.xml", &child("app", "parent/pom.xml"));
|
||||
write(
|
||||
root,
|
||||
"parent/pom.xml",
|
||||
"<project><groupId>demo</groupId><artifactId>parent</artifactId><version>1.0</version><packaging>pom</packaging><modules><module>../</module><module>../infra</module></modules></project>",
|
||||
);
|
||||
write(root, "infra/pom.xml", &child("infra", "../parent/pom.xml"));
|
||||
|
||||
let model = load_with_linked(root, None, &[], &["parent/pom.xml".into()]);
|
||||
let tops: Vec<&str> = model.projects.iter().filter(|p| !p.aggregated).map(|p| p.artifact_id.as_str()).collect();
|
||||
assert_eq!(tops, ["parent"]);
|
||||
let parent = model.projects.iter().find(|p| p.artifact_id == "parent").unwrap();
|
||||
assert_eq!(parent.modules, ["pom.xml", "infra/pom.xml"]);
|
||||
for pom in &parent.modules {
|
||||
assert!(model.projects.iter().any(|p| &p.pom == pom), "{pom}");
|
||||
}
|
||||
|
||||
// A cycle (the root lists the aggregator, which lists the root) keeps the root on top.
|
||||
write(
|
||||
root,
|
||||
"pom.xml",
|
||||
"<project><groupId>demo</groupId><artifactId>app</artifactId><version>1.0</version><packaging>pom</packaging><modules><module>parent</module></modules></project>",
|
||||
);
|
||||
let model = load_with_linked(root, None, &[], &[]);
|
||||
let tops: Vec<&str> = model.projects.iter().filter(|p| !p.aggregated).map(|p| p.artifact_id.as_str()).collect();
|
||||
assert_eq!(tops, ["app"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn chosen_profiles_bring_their_modules_and_dependencies() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
@@ -2780,9 +3206,9 @@ mod tests {
|
||||
let text = std::fs::read_to_string(&path).unwrap();
|
||||
// The property is declared nowhere along the chain.
|
||||
assert_eq!(goto_declaration(&text, &path, at(&text, "${missing.key}"), None), None);
|
||||
// The dependency names its own version: it is the declaration.
|
||||
// The dependency names its own version and no local repository has its POM.
|
||||
assert_eq!(goto_declaration(&text, &path, at(&text, "<artifactId>other-lib"), None), None);
|
||||
// A management entry is a declaration itself, not a reference.
|
||||
// A management entry declares the version; its artifact's POM is not at hand either.
|
||||
let parent_path = tmp.path().join("pom.xml");
|
||||
let parent_text = std::fs::read_to_string(&parent_path).unwrap();
|
||||
assert_eq!(
|
||||
@@ -2794,6 +3220,72 @@ mod tests {
|
||||
assert_eq!(goto_declaration("<project></project>", &path, Pos::new(0, 3), None), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn artifacts_go_to_their_own_pom_in_the_project_or_the_local_repository() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let repo = tmp.path().join("repo");
|
||||
let bom_dir = repo.join("com/alibaba/cloud/spring-cloud-alibaba-dependencies/2.2.6.RELEASE");
|
||||
std::fs::create_dir_all(&bom_dir).unwrap();
|
||||
let bom = "<project>\n <groupId>com.alibaba.cloud</groupId>\n <artifactId>spring-cloud-alibaba-dependencies</artifactId>\n</project>";
|
||||
std::fs::write(bom_dir.join("spring-cloud-alibaba-dependencies-2.2.6.RELEASE.pom"), bom).unwrap();
|
||||
let parent = format!(
|
||||
r#"{PARENT_HEADER}<properties><alibaba.version>2.2.6.RELEASE</alibaba.version></properties>
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
|
||||
<version>${{alibaba.version}}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
</project>"#
|
||||
);
|
||||
let child = format!(
|
||||
r#"{CHILD_HEADER}<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.example</groupId>
|
||||
<artifactId>core</artifactId>
|
||||
<version>1.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>"#
|
||||
);
|
||||
write_parent_child(tmp.path(), &parent, &child);
|
||||
let core = tmp.path().join("core/pom.xml");
|
||||
std::fs::create_dir_all(core.parent().unwrap()).unwrap();
|
||||
std::fs::write(
|
||||
&core,
|
||||
"<project>\n <groupId>com.example</groupId>\n <artifactId>core</artifactId>\n</project>",
|
||||
)
|
||||
.unwrap();
|
||||
let modules =
|
||||
|| vec![ModulePom { group_id: "com.example".into(), artifact_id: "core".into(), pom: core.clone() }];
|
||||
|
||||
// A BOM import in <dependencyManagement> opens the BOM from the local repository.
|
||||
let parent_path = tmp.path().join("pom.xml");
|
||||
let parent_text = std::fs::read_to_string(&parent_path).unwrap();
|
||||
let at_bom = at(&parent_text, "spring-cloud-alibaba-dependencies<");
|
||||
let goto = goto_declaration_in(&parent_text, &parent_path, at_bom, Some(repo.clone()), modules).unwrap();
|
||||
assert_eq!(goto.path, bom_dir.join("spring-cloud-alibaba-dependencies-2.2.6.RELEASE.pom"));
|
||||
assert_eq!(goto.kind, PomGotoKind::ArtifactPom);
|
||||
assert_eq!(goto.range.start, pos_at(bom, bom.find("<artifactId>").unwrap()));
|
||||
|
||||
// A dependency on a project module opens the module's POM.
|
||||
let path = tmp.path().join("app/pom.xml");
|
||||
let text = std::fs::read_to_string(&path).unwrap();
|
||||
let goto =
|
||||
goto_declaration_in(&text, &path, at(&text, "core</artifactId>"), Some(repo.clone()), modules).unwrap();
|
||||
assert_eq!((goto.path.as_path(), goto.label.as_str()), (core.as_path(), "com.example:core"));
|
||||
|
||||
// The <parent> section opens the parent POM.
|
||||
let goto = goto_declaration_in(&text, &path, at(&text, "parent</artifactId>"), Some(repo), modules).unwrap();
|
||||
assert_eq!(goto.path, canon(&parent_path));
|
||||
assert_eq!(goto.kind, PomGotoKind::ArtifactPom);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_property_reference_after_non_ascii_text_is_still_found() {
|
||||
let text = format!(
|
||||
|
||||
@@ -0,0 +1,694 @@
|
||||
//! Completion of Maven coordinates in a POM: inside a `<dependency>`, the
|
||||
//! `<groupId>`, `<artifactId>` and `<version>` values are completed from the
|
||||
//! local repository layout (`<repo>/<group path>/<artifactId>/<version>/…`).
|
||||
//! The Maven Central search endpoint tops the lists up remotely; the host op
|
||||
//! fetches those (cached) and merges them behind the local ones, so this side
|
||||
//! stays pure text and directory logic and tests build their own repositories.
|
||||
|
||||
use super::xml::{self, Element};
|
||||
use super::{element_path, interpolate, read_properties};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use specta::Type;
|
||||
use std::cmp::Ordering;
|
||||
use std::collections::HashMap;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
/// The coordinate element a POM completion fills.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize, Type)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub enum PomCompletionKind {
|
||||
/// The dependency's `<groupId>`.
|
||||
GroupId,
|
||||
/// The dependency's `<artifactId>`.
|
||||
ArtifactId,
|
||||
/// The dependency's `<version>`.
|
||||
Version,
|
||||
}
|
||||
|
||||
/// One suggestion: the coordinate as inserted, and an optional note shown in
|
||||
/// the lookup (the owning group of an artifactId, its newest installed version).
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Type)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct PomCompletionOption {
|
||||
/// The text the completion inserts (`org.example.lib`, `example-lib`, `1.2.0`).
|
||||
pub label: String,
|
||||
pub detail: Option<String>,
|
||||
}
|
||||
|
||||
/// The completion offered at a caret of a POM: the replaced value span (UTF-16
|
||||
/// offsets) and the suggestions, local ones first.
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Type)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct PomCompletion {
|
||||
/// UTF-16 offset where the completed text starts: the value's first character.
|
||||
pub from: u32,
|
||||
/// UTF-16 offset where the replaced value ends: past the caret when the caret
|
||||
/// sits inside an existing value, which is then replaced whole.
|
||||
pub to: u32,
|
||||
/// Which coordinate element the caret is in.
|
||||
pub kind: PomCompletionKind,
|
||||
/// The suggestions, local repository first.
|
||||
pub options: Vec<PomCompletionOption>,
|
||||
}
|
||||
|
||||
/// Where the caret sits in a `<dependency>` coordinate element: what is being
|
||||
/// completed, the typed prefix, and the sibling coordinates the rest need.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct PomCompletionTarget {
|
||||
/// UTF-16 offset of the value's first character (the caret in an empty value).
|
||||
pub from: u32,
|
||||
/// UTF-16 offset past the value's last character (the caret in an empty value).
|
||||
pub to: u32,
|
||||
/// The value typed so far: the text between `from` and the caret, trimmed.
|
||||
pub typed: String,
|
||||
pub kind: PomCompletionKind,
|
||||
/// The interpolated `<groupId>` sibling, when completing the artifactId and
|
||||
/// the group is written out plain (no unresolved `${property}`).
|
||||
pub group: Option<String>,
|
||||
/// The interpolated `<artifactId>` sibling, when completing the version.
|
||||
pub artifact: Option<String>,
|
||||
}
|
||||
|
||||
/// A coordinate element's text, with the POM's own `${properties}` replaced;
|
||||
/// `None` when the element is missing or a property stays unresolved (the
|
||||
/// coordinates behind it are not known without the parent chain, which the
|
||||
/// completion does not follow).
|
||||
fn coordinate(e: Option<&Element>, props: &HashMap<String, String>) -> Option<String> {
|
||||
let text = interpolate(e?.text.trim(), props);
|
||||
(!text.is_empty() && !text.contains("${")).then_some(text)
|
||||
}
|
||||
|
||||
fn own_properties(root: &Element) -> HashMap<String, String> {
|
||||
read_properties(root.child("properties")).into_iter().collect()
|
||||
}
|
||||
|
||||
/// UTF-16 offset -> byte offset, clamped to `text`.
|
||||
fn utf16_to_byte(text: &str, offset: u32) -> usize {
|
||||
let mut seen = 0u32;
|
||||
for (byte, c) in text.char_indices() {
|
||||
if seen >= offset {
|
||||
return byte;
|
||||
}
|
||||
seen += c.len_utf16() as u32;
|
||||
}
|
||||
text.len()
|
||||
}
|
||||
|
||||
/// Byte offset -> UTF-16 offset.
|
||||
fn utf16_at(text: &str, byte: usize) -> u32 {
|
||||
text[..byte].encode_utf16().count() as u32
|
||||
}
|
||||
|
||||
/// Byte offset just past the start tag's `>` of the element at `start`.
|
||||
fn start_tag_end(text: &str, start: usize) -> Option<usize> {
|
||||
let bytes = text.as_bytes();
|
||||
let mut i = start.checked_add(1)?;
|
||||
let mut quote = None;
|
||||
while i < bytes.len() {
|
||||
match (quote, bytes[i]) {
|
||||
(Some(q), b) if b == q => quote = None,
|
||||
(Some(_), _) => {}
|
||||
(None, b'"' | b'\'') => quote = Some(bytes[i]),
|
||||
(None, b'>') => return Some(i + 1),
|
||||
_ => {}
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// What to complete at the caret (`offset`, UTF-16) of the POM `text`: the
|
||||
/// `<groupId>` / `<artifactId>` / `<version>` element of a `<dependency>` the
|
||||
/// caret sits in the value of. `None` everywhere else — other elements, carets
|
||||
/// in an end tag or a `${property}` reference, dependencies without the
|
||||
/// `<groupId>` (and, for versions, the `<artifactId>`) their completions need.
|
||||
pub fn completion_target(text: &str, offset: u32) -> Option<PomCompletionTarget> {
|
||||
let root = xml::parse(text).filter(|e| e.name == "project")?;
|
||||
let at = utf16_to_byte(text, offset);
|
||||
// A caret at the very end of the input sits in the element still being
|
||||
// typed, whose range reaches to the input's end; step back one character
|
||||
// so `element_path` sees it (the caret's own offset stays `at`).
|
||||
let probe = if at == text.len() && at > 0 {
|
||||
let mut p = at - 1;
|
||||
while p > 0 && !text.is_char_boundary(p) {
|
||||
p -= 1;
|
||||
}
|
||||
p
|
||||
} else {
|
||||
at
|
||||
};
|
||||
let elements = element_path(&root, probe);
|
||||
let e = *elements.last()?;
|
||||
let parent = elements.get(elements.len().checked_sub(2)?)?;
|
||||
let kind = match e.name.as_str() {
|
||||
"groupId" => PomCompletionKind::GroupId,
|
||||
"artifactId" => PomCompletionKind::ArtifactId,
|
||||
"version" => PomCompletionKind::Version,
|
||||
_ => return None,
|
||||
};
|
||||
if parent.name != "dependency" || at > e.inner_end {
|
||||
return None;
|
||||
}
|
||||
let inner_start = start_tag_end(text, e.start)?;
|
||||
let value = &text[inner_start..e.inner_end.max(inner_start)];
|
||||
if value.contains("${") {
|
||||
return None; // Property references are goto-declaration territory, not completion.
|
||||
}
|
||||
let lead = value.len() - value.trim_start().len();
|
||||
let trail = value.len() - value.trim_end().len();
|
||||
let (mut from, mut to) = (inner_start + lead, e.inner_end.max(inner_start) - trail);
|
||||
if from > to {
|
||||
// A value of whitespace only: complete at the caret.
|
||||
from = at.min(to);
|
||||
to = to.max(from);
|
||||
}
|
||||
from = from.min(at);
|
||||
to = to.max(from);
|
||||
let props = own_properties(&root);
|
||||
let group = (kind != PomCompletionKind::GroupId).then(|| coordinate(parent.child("groupId"), &props)).flatten();
|
||||
let artifact =
|
||||
(kind == PomCompletionKind::Version).then(|| coordinate(parent.child("artifactId"), &props)).flatten();
|
||||
let typed = text[from..at].trim().to_owned();
|
||||
Some(PomCompletionTarget { from: utf16_at(text, from), to: utf16_at(text, to), typed, kind, group, artifact })
|
||||
}
|
||||
|
||||
/// The suggestions the local repository at `repo` offers for `target`: group
|
||||
/// ids installed under it, the artifacts of the target's group, or the versions
|
||||
/// of the target's artifact. Every scan is bounded (see `SCAN_BUDGET`) and
|
||||
/// reads only directory listings of `repo` — nothing outside it, no jar
|
||||
/// contents.
|
||||
pub fn local_options(repo: &Path, target: &PomCompletionTarget) -> Vec<PomCompletionOption> {
|
||||
let mut budget = SCAN_BUDGET;
|
||||
match target.kind {
|
||||
// `org.spr` completes the `spr` part under `org/`, `org.` the next
|
||||
// segment under `org/`; every typed segment is validated so POM text
|
||||
// cannot walk out of the repository.
|
||||
PomCompletionKind::GroupId => {
|
||||
let segments: Vec<&str> = target.typed.split('.').collect();
|
||||
let (partial, base) = segments.split_last().unwrap_or((&"", &[]));
|
||||
if base.iter().all(|s| segment(s)) {
|
||||
let dir = base.iter().fold(repo.to_path_buf(), |p, s| p.join(s));
|
||||
scan_groups(&dir, &base.join("."), partial, &mut budget)
|
||||
} else {
|
||||
Vec::new()
|
||||
}
|
||||
}
|
||||
PomCompletionKind::ArtifactId => target
|
||||
.group
|
||||
.as_deref()
|
||||
.and_then(|g| repo_dir(repo, &[g]))
|
||||
.map_or_else(Vec::new, |dir| scan_artifacts(&dir, &target.typed, &mut budget)),
|
||||
PomCompletionKind::Version => target
|
||||
.group
|
||||
.as_deref()
|
||||
.zip(target.artifact.as_deref())
|
||||
.and_then(|(g, a)| repo_dir(repo, &[g, a]))
|
||||
.map_or_else(Vec::new, |dir| scan_versions(&dir, &target.typed, &mut budget)),
|
||||
}
|
||||
}
|
||||
|
||||
/// Directory listings one completion may read: the scans are interactive, so
|
||||
/// they stop before they could be felt, and a huge repository simply yields
|
||||
/// fewer suggestions (its shallowest first — the walk is breadth-preferring).
|
||||
const SCAN_BUDGET: u32 = 2048;
|
||||
/// Suggestion caps per kind: group ids and artifact ids fill a lookup page,
|
||||
/// versions list an artifact's releases.
|
||||
const MAX_GROUP_OPTIONS: usize = 60;
|
||||
const MAX_ARTIFACT_OPTIONS: usize = 60;
|
||||
const MAX_VERSION_OPTIONS: usize = 100;
|
||||
/// The cap the merged list (local plus Maven Central's) is truncated to.
|
||||
pub const MAX_OPTIONS: usize = 100;
|
||||
|
||||
/// A directory entry whose name passes the repository layout's alphabet
|
||||
/// (mirrors `sources::artifact`'s): letters, digits, `._-`. Path separators
|
||||
/// and `..` never pass, so a POM's `${…}`-free text cannot traverse out of
|
||||
/// the repository.
|
||||
fn segment(name: &str) -> bool {
|
||||
!name.is_empty()
|
||||
&& name != "."
|
||||
&& name != ".."
|
||||
&& name.chars().all(|c| c.is_ascii_alphanumeric() || "._-".contains(c))
|
||||
}
|
||||
|
||||
/// The subdirectories of `dir` with repository-layout names, spending `budget`
|
||||
/// on the listing; `None` when the budget ran out before `dir` was listed.
|
||||
fn subdirs(dir: &Path, budget: &mut u32) -> Option<Vec<String>> {
|
||||
if *budget == 0 {
|
||||
return None;
|
||||
}
|
||||
*budget -= 1;
|
||||
let Ok(entries) = std::fs::read_dir(dir) else {
|
||||
return Some(Vec::new());
|
||||
};
|
||||
let mut names = Vec::new();
|
||||
for entry in entries.flatten() {
|
||||
// The budget bounds file-type checks too: a plain listing answers.
|
||||
if entry.file_type().map(|t| t.is_dir()).unwrap_or(false)
|
||||
&& let Ok(name) = entry.file_name().into_string()
|
||||
&& segment(&name)
|
||||
{
|
||||
names.push(name);
|
||||
}
|
||||
}
|
||||
Some(names)
|
||||
}
|
||||
|
||||
/// Whether `dir` holds files — a version directory does (jar, pom, the
|
||||
/// tracking files), a group path segment never does.
|
||||
fn holds_files(dir: &Path, budget: &mut u32) -> bool {
|
||||
if *budget == 0 {
|
||||
return false;
|
||||
}
|
||||
*budget -= 1;
|
||||
let Ok(entries) = std::fs::read_dir(dir) else {
|
||||
return false;
|
||||
};
|
||||
entries.flatten().any(|e| e.file_type().map(|t| t.is_file()).unwrap_or(false))
|
||||
}
|
||||
|
||||
/// Whether the version directory `dir` holds the version: its `.pom` or a `.jar`. A failed
|
||||
/// download leaves only `*.lastUpdated` tracking files behind, for a version that may not
|
||||
/// even exist (one typed while the POM was being edited).
|
||||
fn installed(dir: &Path, budget: &mut u32) -> bool {
|
||||
if *budget == 0 {
|
||||
return false;
|
||||
}
|
||||
*budget -= 1;
|
||||
let Ok(entries) = std::fs::read_dir(dir) else {
|
||||
return false;
|
||||
};
|
||||
entries.flatten().any(|e| {
|
||||
e.file_type().map(|t| t.is_file()).unwrap_or(false)
|
||||
&& e.file_name().to_str().is_some_and(|n| n.ends_with(".pom") || n.ends_with(".jar"))
|
||||
})
|
||||
}
|
||||
|
||||
/// Whether `dir` is an artifact directory: it holds versions of one artifact,
|
||||
/// recognizable by a `maven-metadata*.xml` beside them or a child directory
|
||||
/// holding files. A group segment's children are directories without files.
|
||||
fn artifact_dir(dir: &Path, budget: &mut u32) -> bool {
|
||||
if *budget == 0 {
|
||||
return false;
|
||||
}
|
||||
*budget -= 1;
|
||||
let Ok(entries) = std::fs::read_dir(dir) else {
|
||||
return false;
|
||||
};
|
||||
for entry in entries.flatten() {
|
||||
let Ok(name) = entry.file_name().into_string() else { continue };
|
||||
if name.starts_with("maven-metadata") {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
let Some(children) = subdirs(dir, budget) else { return false };
|
||||
children.iter().any(|c| holds_files(&dir.join(c), budget))
|
||||
}
|
||||
|
||||
/// Whether `dir` is a group directory (holds artifact directories) and whether
|
||||
/// it also holds deeper group segments worth descending into.
|
||||
fn group_dir(dir: &Path, budget: &mut u32) -> Option<(bool, bool)> {
|
||||
let children = subdirs(dir, budget)?;
|
||||
let (mut group, mut descend) = (false, false);
|
||||
for child in &children {
|
||||
if artifact_dir(&dir.join(child), budget) {
|
||||
group = true;
|
||||
} else {
|
||||
descend = true;
|
||||
}
|
||||
}
|
||||
Some((group, descend))
|
||||
}
|
||||
|
||||
/// The group ids installed under `dir` (which the typed prefix `prefix` —
|
||||
/// "" for the repository root — already names), completing the partial last
|
||||
/// segment `partial`, shallower groups first.
|
||||
fn scan_groups(dir: &Path, prefix: &str, partial: &str, budget: &mut u32) -> Vec<PomCompletionOption> {
|
||||
let mut out = Vec::new();
|
||||
scan_groups_into(dir, prefix, partial, &mut out, budget);
|
||||
out.sort_by(|a, b| a.label.cmp(&b.label));
|
||||
out
|
||||
}
|
||||
|
||||
fn scan_groups_into(dir: &Path, prefix: &str, partial: &str, out: &mut Vec<PomCompletionOption>, budget: &mut u32) {
|
||||
if out.len() >= MAX_GROUP_OPTIONS {
|
||||
return;
|
||||
}
|
||||
let Some(children) = subdirs(dir, budget) else { return };
|
||||
for name in children {
|
||||
if !name.starts_with(partial) {
|
||||
continue;
|
||||
}
|
||||
let path = dir.join(&name);
|
||||
let label = if prefix.is_empty() { name.clone() } else { format!("{prefix}.{name}") };
|
||||
let Some((group, descend)) = group_dir(&path, budget) else { return };
|
||||
if group {
|
||||
out.push(PomCompletionOption { label: label.clone(), detail: None });
|
||||
if out.len() >= MAX_GROUP_OPTIONS {
|
||||
return;
|
||||
}
|
||||
}
|
||||
// Deeper groups (org.example under org.example's own parent) follow the
|
||||
// shallower ones, so a truncated scan of a huge repository still offers
|
||||
// every first-level segment.
|
||||
if descend {
|
||||
scan_groups_into(&path, &label, "", out, budget);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The artifact ids installed under the group directory `dir`, completing the
|
||||
/// typed prefix, each with its newest installed version as the detail.
|
||||
fn scan_artifacts(dir: &Path, typed: &str, budget: &mut u32) -> Vec<PomCompletionOption> {
|
||||
let Some(children) = subdirs(dir, budget) else { return Vec::new() };
|
||||
let mut out = Vec::new();
|
||||
for name in children {
|
||||
if !name.starts_with(typed) || !artifact_dir(&dir.join(&name), budget) {
|
||||
continue;
|
||||
}
|
||||
let detail = newest_version(&dir.join(&name), budget);
|
||||
out.push(PomCompletionOption { label: name, detail });
|
||||
}
|
||||
out.sort_by(|a, b| a.label.cmp(&b.label));
|
||||
out.truncate(MAX_ARTIFACT_OPTIONS);
|
||||
out
|
||||
}
|
||||
|
||||
/// The newest version directory of the artifact at `dir`, by Maven's ordering.
|
||||
fn newest_version(dir: &Path, budget: &mut u32) -> Option<String> {
|
||||
let children = subdirs(dir, budget)?;
|
||||
children.into_iter().filter(|name| installed(&dir.join(name), budget)).max_by(|a, b| compare_versions(a, b))
|
||||
}
|
||||
|
||||
/// The versions installed of the artifact at `dir`, completing the typed
|
||||
/// prefix, newest first.
|
||||
fn scan_versions(dir: &Path, typed: &str, budget: &mut u32) -> Vec<PomCompletionOption> {
|
||||
let Some(children) = subdirs(dir, budget) else { return Vec::new() };
|
||||
let mut out: Vec<_> = children
|
||||
.into_iter()
|
||||
.filter(|name| name.starts_with(typed) && installed(&dir.join(name), budget))
|
||||
.map(|name| PomCompletionOption { label: name, detail: None })
|
||||
.collect();
|
||||
out.sort_by(|a, b| compare_versions(&b.label, &a.label));
|
||||
out.truncate(MAX_VERSION_OPTIONS);
|
||||
out
|
||||
}
|
||||
|
||||
/// The repository directory of `coordinates` (a group id, or a group and an artifact
|
||||
/// id): the group's dots are directories, an artifact id is one directory whatever dots
|
||||
/// it has (`org/eclipse/platform/org.eclipse.core.runtime`). Each segment is validated
|
||||
/// against the layout's alphabet so POM text cannot walk outside `repo`.
|
||||
fn repo_dir(repo: &Path, coordinates: &[&str]) -> Option<PathBuf> {
|
||||
let (group, artifact) = coordinates.split_first()?;
|
||||
let segments: Vec<&str> = group.split('.').chain(artifact.iter().copied()).collect();
|
||||
segments.iter().all(|s| segment(s)).then(|| segments.iter().fold(repo.to_path_buf(), |p, s| p.join(s)))
|
||||
}
|
||||
|
||||
/// Maven-ish version order, best-effort (a full `ComparableVersion` is out of
|
||||
/// proportion for ordering a lookup): numeric runs compare numerically, letter
|
||||
/// runs by their qualifier rank (`alpha < beta < m < rc < snapshot < release <
|
||||
/// sp`, unknown ones with releases lexically); missing parts rank as zero.
|
||||
fn compare_versions(a: &str, b: &str) -> Ordering {
|
||||
fn tokens(v: &str) -> Vec<String> {
|
||||
v.split(|c: char| !c.is_ascii_alphanumeric()).filter(|t| !t.is_empty()).map(str::to_ascii_lowercase).collect()
|
||||
}
|
||||
fn rank(t: &str) -> Option<u8> {
|
||||
match t {
|
||||
"a" | "alpha" => Some(0),
|
||||
"b" | "beta" => Some(1),
|
||||
"m" | "milestone" => Some(2),
|
||||
"rc" | "cr" => Some(3),
|
||||
"snapshot" => Some(4),
|
||||
"" | "ga" | "final" => Some(5),
|
||||
"sp" => Some(6),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
let (a, b) = (tokens(a), tokens(b));
|
||||
for i in 0..a.len().max(b.len()) {
|
||||
let (x, y) = (a.get(i).map(String::as_str), b.get(i).map(String::as_str));
|
||||
let (xn, yn) = (x.and_then(|s| s.parse::<u64>().ok()), y.and_then(|s| s.parse::<u64>().ok()));
|
||||
let order = match (xn, yn) {
|
||||
// Numbers compare numerically; a number outranks a qualifier.
|
||||
(Some(xn), Some(yn)) => xn.cmp(&yn),
|
||||
(Some(_), None) if y.is_some() => return Ordering::Greater,
|
||||
(None, Some(_)) if x.is_some() => return Ordering::Less,
|
||||
_ => {
|
||||
let xr = x.and_then(rank).unwrap_or(5);
|
||||
let yr = y.and_then(rank).unwrap_or(5);
|
||||
if xr != yr { xr.cmp(&yr) } else { x.unwrap_or("").cmp(y.unwrap_or("")) }
|
||||
}
|
||||
};
|
||||
if order != Ordering::Equal {
|
||||
return order;
|
||||
}
|
||||
}
|
||||
Ordering::Equal
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
/// A small local repository: every file is laid out as Maven installs it,
|
||||
/// so the scans recognize group, artifact and version directories.
|
||||
fn repo(dir: &Path) -> PathBuf {
|
||||
let repo = dir.join("repository");
|
||||
for file in [
|
||||
"org/apache/maven/maven-core/4.0.0-rc-5/maven-core-4.0.0-rc-5.jar",
|
||||
"org/apache/maven/plugins/maven-compiler-plugin/3.8.1/maven-compiler-plugin-3.8.1.jar",
|
||||
"org/example/lib/1.0/lib-1.0.jar",
|
||||
"org/example/lib/1.9.1/lib-1.9.1.jar",
|
||||
"org/example/lib/1.10/lib-1.10.jar",
|
||||
"org/example/lib/2.0-beta-1/lib-2.0-beta-1.jar",
|
||||
"org/example/lib/2.0/lib-2.0.pom",
|
||||
"net/nu/netty/4.1.100/netty-4.1.100.jar",
|
||||
"org/eclipse/platform/org.eclipse.core.runtime/3.31.0/org.eclipse.core.runtime-3.31.0.jar",
|
||||
// Failed downloads (a version typed while editing): tracking files only, not installed.
|
||||
"org/example/lib/2/lib-2.jar.lastUpdated",
|
||||
"org/example/lib/3.0/lib-3.0.pom.lastUpdated",
|
||||
] {
|
||||
let path = repo.join(file);
|
||||
std::fs::create_dir_all(path.parent().unwrap()).unwrap();
|
||||
std::fs::write(&path, b"").unwrap();
|
||||
}
|
||||
repo
|
||||
}
|
||||
|
||||
fn target(text: &str, needle: &str) -> Option<PomCompletionTarget> {
|
||||
completion_target(text, (text.find(needle).unwrap() + needle.len()) as u32)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn completes_each_coordinate_element_of_a_dependency() {
|
||||
let text = "<project><dependencies><dependency>\
|
||||
<groupId>org.example</groupId><artifactId>lib</artifactId><version>1.0</version>\
|
||||
</dependency></dependencies></project>";
|
||||
let t = target(text, "<groupId>org.example").unwrap();
|
||||
assert_eq!(t.kind, PomCompletionKind::GroupId);
|
||||
assert_eq!(t.typed, "org.example");
|
||||
assert_eq!((t.from, t.to), (44, 55));
|
||||
assert!(t.group.is_none() && t.artifact.is_none());
|
||||
|
||||
let t = target(text, "<artifactId>lib").unwrap();
|
||||
assert_eq!(t.kind, PomCompletionKind::ArtifactId);
|
||||
assert_eq!(t.typed, "lib");
|
||||
assert_eq!(t.group.as_deref(), Some("org.example"));
|
||||
assert!(t.artifact.is_none());
|
||||
|
||||
let t = target(text, "<version>1.0").unwrap();
|
||||
assert_eq!(t.kind, PomCompletionKind::Version);
|
||||
assert_eq!(t.typed, "1.0");
|
||||
assert_eq!(t.group.as_deref(), Some("org.example"));
|
||||
assert_eq!(t.artifact.as_deref(), Some("lib"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn completes_while_the_element_is_being_typed_and_the_value_is_empty() {
|
||||
let empty = "<project><dependencies><dependency><groupId></groupId></dependency></dependencies></project>";
|
||||
let t = target(empty, "<groupId>").unwrap();
|
||||
assert_eq!(t.kind, PomCompletionKind::GroupId);
|
||||
assert_eq!(t.typed, "");
|
||||
assert_eq!(t.from, t.to);
|
||||
|
||||
let open = "<project><dependencies><dependency><artifactId>li";
|
||||
let t = target(open, "li").unwrap();
|
||||
assert_eq!(t.kind, PomCompletionKind::ArtifactId);
|
||||
assert_eq!(t.typed, "li");
|
||||
// The value under construction ends with the input and is replaced whole.
|
||||
assert_eq!(t.to, open.len() as u32);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn completes_only_dependency_coordinates() {
|
||||
// The same elements outside a <dependency> get nothing: a <plugin> is
|
||||
// completed by the repository, not coordinates.
|
||||
let plugin =
|
||||
"<project><build><plugins><plugin><groupId>org.apache.maven</groupId></plugin></plugins></build></project>";
|
||||
assert_eq!(target(plugin, "<groupId>org.apache.maven"), None);
|
||||
// A caret in the end tag, or on a `${property}` value, is not a completion.
|
||||
let text =
|
||||
"<project><dependencies><dependency><groupId>org.example</groupId></dependency></dependencies></project>";
|
||||
assert_eq!(target(text, "</groupId>"), None);
|
||||
let property = "<project><dependencies><dependency><groupId>${example.group}</groupId></dependency></dependencies></project>";
|
||||
assert_eq!(target(property, "${example.group}"), None);
|
||||
// A dependency's groupId is required before its artifacts can be listed.
|
||||
let no_group =
|
||||
"<project><dependencies><dependency><artifactId>lib</artifactId></dependency></dependencies></project>";
|
||||
let t = target(no_group, "<artifactId>lib").unwrap();
|
||||
assert!(t.group.is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn interpolates_the_poms_own_properties_for_siblings() {
|
||||
let text = "<project>\
|
||||
<properties><example.group>org.example</example.group><example.version>2.0</example.version></properties>\
|
||||
<dependencies><dependency><groupId>${example.group}</groupId><artifactId>lib</artifactId><version>${example.version}</version></dependency></dependencies>\
|
||||
</project>";
|
||||
// The groupId the caret is typing stays a completion; the siblings resolve.
|
||||
let t = target(text, "<artifactId>lib").unwrap();
|
||||
assert_eq!(t.group.as_deref(), Some("org.example"));
|
||||
// A `${property}` under the caret itself gets nothing to complete.
|
||||
assert_eq!(target(text, "<version>${example.version}"), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn local_scan_lists_installed_groups_shallowest_first() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let repo = repo(tmp.path());
|
||||
let options = |typed: &str| {
|
||||
local_options(
|
||||
&repo,
|
||||
&PomCompletionTarget {
|
||||
from: 0,
|
||||
to: 0,
|
||||
typed: typed.into(),
|
||||
kind: PomCompletionKind::GroupId,
|
||||
group: None,
|
||||
artifact: None,
|
||||
},
|
||||
)
|
||||
.into_iter()
|
||||
.map(|o| o.label)
|
||||
.collect::<Vec<_>>()
|
||||
};
|
||||
// Only directories holding artifacts are group ids: `net` and `org`
|
||||
// alone never receive an install, their deeper levels do.
|
||||
let org = ["org.apache.maven", "org.apache.maven.plugins", "org.eclipse.platform", "org.example"];
|
||||
assert_eq!(options(""), [&["net.nu"][..], &org].concat());
|
||||
assert_eq!(options("org"), org);
|
||||
assert_eq!(options("org."), org);
|
||||
assert_eq!(options("org.spr"), Vec::<String>::new());
|
||||
assert_eq!(options("net."), ["net.nu"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn local_scan_lists_artifacts_of_the_group_with_their_newest_version() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let repo = repo(tmp.path());
|
||||
let options = |typed: &str| {
|
||||
local_options(
|
||||
&repo,
|
||||
&PomCompletionTarget {
|
||||
from: 0,
|
||||
to: 0,
|
||||
typed: typed.into(),
|
||||
kind: PomCompletionKind::ArtifactId,
|
||||
group: Some("org.example".into()),
|
||||
artifact: None,
|
||||
},
|
||||
)
|
||||
.into_iter()
|
||||
.map(|o| (o.label, o.detail))
|
||||
.collect::<Vec<_>>()
|
||||
};
|
||||
assert_eq!(options(""), [("lib".to_owned(), Some("2.0".to_owned()))]);
|
||||
assert_eq!(options("net"), Vec::<(String, Option<String>)>::new());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn local_scan_lists_versions_of_the_artifact_newest_first() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let repo = repo(tmp.path());
|
||||
let options = |typed: &str| {
|
||||
local_options(
|
||||
&repo,
|
||||
&PomCompletionTarget {
|
||||
from: 0,
|
||||
to: 0,
|
||||
typed: typed.into(),
|
||||
kind: PomCompletionKind::Version,
|
||||
group: Some("org.example".into()),
|
||||
artifact: Some("lib".into()),
|
||||
},
|
||||
)
|
||||
.into_iter()
|
||||
.map(|o| o.label)
|
||||
.collect::<Vec<_>>()
|
||||
};
|
||||
assert_eq!(options(""), ["2.0", "2.0-beta-1", "1.10", "1.9.1", "1.0"]);
|
||||
assert_eq!(options("1."), ["1.10", "1.9.1", "1.0"]);
|
||||
// An artifact id with dots is one directory.
|
||||
let dotted = local_options(
|
||||
&repo,
|
||||
&PomCompletionTarget {
|
||||
from: 0,
|
||||
to: 0,
|
||||
typed: "".into(),
|
||||
kind: PomCompletionKind::Version,
|
||||
group: Some("org.eclipse.platform".into()),
|
||||
artifact: Some("org.eclipse.core.runtime".into()),
|
||||
},
|
||||
);
|
||||
assert_eq!(dotted.into_iter().map(|o| o.label).collect::<Vec<_>>(), ["3.31.0"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_poms_text_cannot_walk_out_of_the_repository() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let repo = repo(tmp.path());
|
||||
for group in ["../../etc", "..", "org/../.."] {
|
||||
let options = local_options(
|
||||
&repo,
|
||||
&PomCompletionTarget {
|
||||
from: 0,
|
||||
to: 0,
|
||||
typed: "".into(),
|
||||
kind: PomCompletionKind::ArtifactId,
|
||||
group: Some(group.into()),
|
||||
artifact: None,
|
||||
},
|
||||
);
|
||||
assert!(options.is_empty(), "{group}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_scan_stops_at_the_option_cap() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let repo = tmp.path().join("repository");
|
||||
for i in 0..MAX_GROUP_OPTIONS + 10 {
|
||||
let group = repo.join(format!("com/example/g{i}"));
|
||||
std::fs::create_dir_all(group.join("art/1.0")).unwrap();
|
||||
std::fs::write(group.join("art/1.0/art-1.0.jar"), b"").unwrap();
|
||||
}
|
||||
let options = local_options(
|
||||
&repo,
|
||||
&PomCompletionTarget {
|
||||
from: 0,
|
||||
to: 0,
|
||||
typed: "".into(),
|
||||
kind: PomCompletionKind::GroupId,
|
||||
group: None,
|
||||
artifact: None,
|
||||
},
|
||||
);
|
||||
assert_eq!(options.len(), MAX_GROUP_OPTIONS);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn maven_version_order() {
|
||||
let mut versions = ["2.0", "1.0", "2.0-beta-1", "2.0-alpha-2", "1.10", "1.9.1", "4.0.0-rc-5", "4.0.0-rc-13"];
|
||||
versions.sort_by(|a, b| compare_versions(a, b));
|
||||
assert_eq!(versions, ["1.0", "1.9.1", "1.10", "2.0-alpha-2", "2.0-beta-1", "2.0", "4.0.0-rc-5", "4.0.0-rc-13"]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,301 @@
|
||||
//! sbt builds for the sbt tool window: each `build.sbt` build under the project root
|
||||
//! with the projects its definition declares (`lazy val core = project`,
|
||||
//! `(project in file("core"))`, `Project("core", file("core"))`), read statically —
|
||||
//! sbt itself is not started.
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use specta::Type;
|
||||
use std::path::Path;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Type)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct SbtBuild {
|
||||
/// Folder of `build.sbt`, relative to the project root (`""` for the root itself).
|
||||
pub dir: String,
|
||||
/// The launcher: a `sbt` / `sbtx` script in the build folder, else `sbt` on `PATH`.
|
||||
pub command: String,
|
||||
/// `sbt.version` of `project/build.properties`.
|
||||
pub version: Option<String>,
|
||||
/// The root project first, then the declared projects in source order.
|
||||
pub projects: Vec<SbtProject>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Type)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct SbtProject {
|
||||
/// The project id tasks are scoped with (`core/compile`).
|
||||
pub id: String,
|
||||
/// Base folder relative to the build folder (`""` for the build's own).
|
||||
pub dir: String,
|
||||
/// The build's root project: its tasks run unscoped (and aggregate when sbt does).
|
||||
pub root: bool,
|
||||
}
|
||||
|
||||
/// The sbt builds under `root` (outermost `build.sbt` folders).
|
||||
pub fn load(root: &Path) -> Vec<SbtBuild> {
|
||||
ide_core::project_roots::discover(root, &["build.sbt"])
|
||||
.into_iter()
|
||||
.filter_map(|dir| {
|
||||
let text = std::fs::read_to_string(dir.join("build.sbt")).ok()?;
|
||||
let rel = dir.strip_prefix(root).unwrap_or(&dir).to_string_lossy().replace('\\', "/");
|
||||
let name = dir.file_name().map(|n| n.to_string_lossy().into_owned()).unwrap_or_else(|| "root".to_owned());
|
||||
Some(SbtBuild {
|
||||
dir: rel,
|
||||
command: launcher(&dir),
|
||||
version: build_version(&dir),
|
||||
projects: projects(&text, &name),
|
||||
})
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn launcher(dir: &Path) -> String {
|
||||
for script in ["sbt", "sbtx"] {
|
||||
if !cfg!(windows) && dir.join(script).is_file() {
|
||||
return format!("./{script}");
|
||||
}
|
||||
}
|
||||
if cfg!(windows) && dir.join("sbt.bat").is_file() {
|
||||
return "sbt.bat".to_owned();
|
||||
}
|
||||
"sbt".to_owned()
|
||||
}
|
||||
|
||||
fn build_version(dir: &Path) -> Option<String> {
|
||||
let text = std::fs::read_to_string(dir.join("project/build.properties")).ok()?;
|
||||
text.lines().find_map(|line| {
|
||||
let (key, value) = line.split_once('=')?;
|
||||
(key.trim() == "sbt.version").then(|| value.trim().to_owned())
|
||||
})
|
||||
}
|
||||
|
||||
/// `text` with comments and the contents of string literals blanked (quotes kept),
|
||||
/// byte for byte, so offsets into it are offsets into `text`.
|
||||
fn mask(text: &str) -> String {
|
||||
let mut out = String::with_capacity(text.len());
|
||||
let blank = |out: &mut String, c: char| {
|
||||
if c == '\n' {
|
||||
out.push('\n');
|
||||
} else {
|
||||
out.extend(std::iter::repeat_n(' ', c.len_utf8()));
|
||||
}
|
||||
};
|
||||
let mut chars = text.chars().peekable();
|
||||
while let Some(c) = chars.next() {
|
||||
match c {
|
||||
'"' => {
|
||||
out.push('"');
|
||||
let mut ahead = chars.clone();
|
||||
if ahead.next() == Some('"') && ahead.next() == Some('"') {
|
||||
// `"""…"""`: no escapes inside.
|
||||
chars.next();
|
||||
chars.next();
|
||||
out.push_str("\"\"");
|
||||
let mut quotes = 0;
|
||||
for c in chars.by_ref() {
|
||||
quotes = if c == '"' { quotes + 1 } else { 0 };
|
||||
if quotes > 0 {
|
||||
out.push('"')
|
||||
} else {
|
||||
blank(&mut out, c)
|
||||
}
|
||||
if quotes == 3 {
|
||||
break;
|
||||
}
|
||||
}
|
||||
continue;
|
||||
}
|
||||
while let Some(c) = chars.next() {
|
||||
if c == '"' || c == '\n' {
|
||||
out.push(c);
|
||||
break;
|
||||
}
|
||||
blank(&mut out, c);
|
||||
if c == '\\'
|
||||
&& let Some(n) = chars.next()
|
||||
{
|
||||
blank(&mut out, n);
|
||||
}
|
||||
}
|
||||
}
|
||||
'/' if chars.peek() == Some(&'/') => {
|
||||
blank(&mut out, c);
|
||||
for c in chars.by_ref() {
|
||||
blank(&mut out, c);
|
||||
if c == '\n' {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
'/' if chars.peek() == Some(&'*') => {
|
||||
blank(&mut out, c);
|
||||
let mut prev = ' ';
|
||||
for c in chars.by_ref() {
|
||||
blank(&mut out, c);
|
||||
if prev == '*' && c == '/' {
|
||||
break;
|
||||
}
|
||||
prev = c;
|
||||
}
|
||||
}
|
||||
_ => out.push(c),
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
fn identifier(s: &str) -> Option<(&str, &str)> {
|
||||
let s = s.trim_start();
|
||||
if let Some(rest) = s.strip_prefix('`') {
|
||||
let end = rest.find('`')?;
|
||||
return Some((&rest[..end], &rest[end + 1..]));
|
||||
}
|
||||
let end = s.find(|c: char| !(c.is_alphanumeric() || c == '_')).unwrap_or(s.len());
|
||||
(end > 0).then(|| (&s[..end], &s[end..]))
|
||||
}
|
||||
|
||||
/// The string literal at the start of `s` (after spaces), and the rest.
|
||||
fn string_literal(s: &str) -> Option<(&str, &str)> {
|
||||
let rest = s.trim_start().strip_prefix('"')?;
|
||||
let end = rest.find('"')?;
|
||||
Some((&rest[..end], &rest[end + 1..]))
|
||||
}
|
||||
|
||||
/// The folder of a `file("x")` / `new File("x")` at the start of `s`.
|
||||
fn file_arg(s: &str) -> Option<&str> {
|
||||
let s = s.trim_start();
|
||||
let s = s.strip_prefix("file").or_else(|| s.strip_prefix("new File"))?;
|
||||
let s = s.trim_start().strip_prefix('(')?;
|
||||
string_literal(s).map(|(dir, _)| dir)
|
||||
}
|
||||
|
||||
fn normalize_dir(dir: &str) -> String {
|
||||
let dir = dir.trim_start_matches("./").trim_end_matches('/');
|
||||
if dir == "." { String::new() } else { dir.to_owned() }
|
||||
}
|
||||
|
||||
/// What a `lazy val <id> = <rhs>` declares: a project and its base folder. `rhs` is
|
||||
/// masked (see [`mask`]); `original` is the same text unmasked, for the folder names.
|
||||
fn project_of(id: &str, rhs: &str, original: &str) -> Option<String> {
|
||||
let at = |s: &str| original.get(rhs.len() - s.len()..).unwrap_or_default();
|
||||
let rhs = rhs.trim_start().trim_start_matches('(').trim_start();
|
||||
if let Some(rest) = rhs.strip_prefix("Project") {
|
||||
// `Project("id", file("dir"))` / `Project(id = "x", base = file("dir"))`.
|
||||
let args = rest.trim_start().strip_prefix('(')?;
|
||||
let base = args.find("file(").or_else(|| args.find("new File("))?;
|
||||
return file_arg(at(&args[base..])).map(normalize_dir);
|
||||
}
|
||||
let rest = rhs.strip_prefix("project")?;
|
||||
if rest.starts_with(|c: char| c.is_alphanumeric() || c == '_') {
|
||||
return None;
|
||||
}
|
||||
let rest = rest.trim_start();
|
||||
let located = rest
|
||||
.strip_prefix(".in")
|
||||
.and_then(|r| r.trim_start().strip_prefix('('))
|
||||
.or_else(|| rest.strip_prefix("in ").or_else(|| rest.strip_prefix("in\t")));
|
||||
Some(match located.and_then(|l| file_arg(at(l))) {
|
||||
Some(dir) => normalize_dir(dir),
|
||||
None => id.to_owned(),
|
||||
})
|
||||
}
|
||||
|
||||
/// The projects a build definition declares; `name` is the build folder's name, the id
|
||||
/// sbt gives an implicit root project.
|
||||
pub fn projects(text: &str, name: &str) -> Vec<SbtProject> {
|
||||
let masked = mask(text);
|
||||
let mut out: Vec<SbtProject> = Vec::new();
|
||||
let mut from = 0;
|
||||
while let Some(found) = masked[from..].find("val ") {
|
||||
let at = from + found;
|
||||
from = at + 4;
|
||||
if masked[..at].chars().last().is_some_and(|c| c.is_alphanumeric() || c == '_') {
|
||||
continue;
|
||||
}
|
||||
let Some((id, tail)) = identifier(&masked[from..]) else { continue };
|
||||
let Some(rhs) = tail.trim_start().strip_prefix('=') else { continue };
|
||||
if rhs.starts_with('=') || rhs.starts_with('>') {
|
||||
continue;
|
||||
}
|
||||
let Some(dir) = project_of(id, rhs, &text[masked.len() - rhs.len()..]) else { continue };
|
||||
if !out.iter().any(|p| p.id == id) {
|
||||
out.push(SbtProject { id: id.to_owned(), root: dir.is_empty(), dir });
|
||||
}
|
||||
}
|
||||
match out.iter().position(|p| p.root) {
|
||||
Some(i) => {
|
||||
let root = out.remove(i);
|
||||
out.insert(0, root);
|
||||
}
|
||||
None => out.insert(0, SbtProject { id: name.to_owned(), dir: String::new(), root: true }),
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn ids(text: &str) -> Vec<(String, String, bool)> {
|
||||
projects(text, "shop").into_iter().map(|p| (p.id, p.dir, p.root)).collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn single_project_builds_have_an_implicit_root() {
|
||||
assert_eq!(
|
||||
ids("ThisBuild / scalaVersion := \"3.3.4\"\nname := \"shop\"\n"),
|
||||
[("shop".into(), "".into(), true)]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn declared_projects_and_their_folders() {
|
||||
let text = r#"
|
||||
// lazy val commented = project
|
||||
lazy val root = (project in file("."))
|
||||
.aggregate(core, `web-app`, util)
|
||||
.settings(name := "shop")
|
||||
|
||||
lazy val core = project
|
||||
lazy val `web-app` = (project in file("modules/web")).dependsOn(core)
|
||||
lazy val util = project.in(file("libs/util/"))
|
||||
lazy val legacy = Project("legacy", file("old"))
|
||||
/* lazy val hidden = project */
|
||||
val description = "lazy val fake = project"
|
||||
lazy val projectSettings = Seq(scalacOptions += "-deprecation")
|
||||
"#;
|
||||
assert_eq!(
|
||||
ids(text),
|
||||
[
|
||||
("root".into(), "".into(), true),
|
||||
("core".into(), "core".into(), false),
|
||||
("web-app".into(), "modules/web".into(), false),
|
||||
("util".into(), "libs/util".into(), false),
|
||||
("legacy".into(), "old".into(), false),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_root_declared_last_is_listed_first() {
|
||||
let text = "lazy val api = project\nlazy val all = (project in file(\".\")).aggregate(api)\n";
|
||||
assert_eq!(ids(text), [("all".into(), "".into(), true), ("api".into(), "api".into(), false)]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn builds_are_found_with_their_launcher_and_version() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let root = tmp.path();
|
||||
std::fs::create_dir_all(root.join("svc/project")).unwrap();
|
||||
std::fs::create_dir_all(root.join("svc/core")).unwrap();
|
||||
std::fs::write(root.join("svc/build.sbt"), "lazy val core = project\n").unwrap();
|
||||
// A project's own build.sbt is part of the outer build.
|
||||
std::fs::write(root.join("svc/core/build.sbt"), "libraryDependencies += \"a\" % \"b\" % \"1\"\n").unwrap();
|
||||
std::fs::write(root.join("svc/project/build.properties"), "sbt.version = 1.10.7\n").unwrap();
|
||||
let builds = load(root);
|
||||
assert_eq!(builds.len(), 1);
|
||||
let b = &builds[0];
|
||||
assert_eq!((b.dir.as_str(), b.command.as_str(), b.version.as_deref()), ("svc", "sbt", Some("1.10.7")));
|
||||
assert_eq!(b.projects.iter().map(|p| p.id.as_str()).collect::<Vec<_>>(), ["svc", "core"]);
|
||||
}
|
||||
}
|
||||
@@ -73,6 +73,9 @@ struct GradleCache {
|
||||
/// The projects as Gradle evaluated them; empty in caches from before they were read.
|
||||
#[serde(default)]
|
||||
projects: Vec<ReportedProject>,
|
||||
/// The projects report how their resources are processed; older caches are stale.
|
||||
#[serde(default)]
|
||||
resources: bool,
|
||||
}
|
||||
|
||||
/// Init script printing every project, its source sets and Java version once configured.
|
||||
@@ -244,11 +247,12 @@ pub fn apply_gradle_cache(root: &Path, cache_dir: &Path, model: &mut GradleModel
|
||||
}
|
||||
}
|
||||
}
|
||||
// A successful sync from before projects were cached has not seen computed includes.
|
||||
let without_projects = cache.error.is_none() && cache.projects.is_empty();
|
||||
// A successful sync from before projects were cached has not seen computed includes,
|
||||
// nor, before resources were read, which launches need Gradle to process them.
|
||||
let outdated = cache.error.is_none() && (cache.projects.is_empty() || !cache.resources);
|
||||
model.synced_at = Some(cache.at_ms);
|
||||
model.sync_error = cache.error;
|
||||
model.stale = without_projects || cache.hash != hash_gradle_files(&gradle::build_files(root, model));
|
||||
model.stale = outdated || cache.hash != hash_gradle_files(&gradle::build_files(root, model));
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- processes
|
||||
@@ -266,6 +270,13 @@ fn run(
|
||||
|
||||
/// The first error lines of a failed build, for the tool window.
|
||||
pub(crate) fn error_summary(text: &str) -> String {
|
||||
// The JVM's "could not find or load main class" (localized) when `gradlew` has no jar.
|
||||
if text.contains("org.gradle.wrapper.GradleWrapperMain") {
|
||||
return "gradle/wrapper/gradle-wrapper.jar is missing (often excluded by a `*.jar` rule in .gitignore). \
|
||||
Restore it, run `gradle wrapper` with an installed Gradle, or choose Distribution: Local installation \
|
||||
in Settings › Build, Execution, Deployment › Gradle."
|
||||
.to_owned();
|
||||
}
|
||||
let lines: Vec<&str> = text
|
||||
.lines()
|
||||
.filter(|l| {
|
||||
@@ -462,6 +473,7 @@ pub fn sync_gradle_model(
|
||||
error: (!out.success).then(|| error_summary(&out.text)),
|
||||
tasks: if out.success { gradle::parse_tasks(&out.text) } else { Vec::new() },
|
||||
projects,
|
||||
resources: true,
|
||||
};
|
||||
write_cache(&cache_dir, GRADLE_CACHE, &cache)
|
||||
}
|
||||
@@ -713,6 +725,7 @@ mod tests {
|
||||
test,
|
||||
src: src.iter().map(|p| core_src(p)).collect(),
|
||||
res: res.iter().map(|p| core_src(p)).collect(),
|
||||
processed: None,
|
||||
};
|
||||
// A settings script's own println comes first; task listing lines follow.
|
||||
let out = format!(
|
||||
@@ -737,6 +750,15 @@ mod tests {
|
||||
);
|
||||
let projects = parse_reported_projects(&out);
|
||||
assert_eq!(projects.len(), 3, "the malformed line is skipped");
|
||||
// As the init script prints a set whose resources Gradle filters or renames.
|
||||
let processed = parse_reported_projects(&format!(
|
||||
"@openidea-model\t{}",
|
||||
r#"{"path":":","dir":"/p","sourceSets":[{"name":"main","test":false,"src":[],"res":[],"processed":{"task":"processResources","dir":"/p/build/resources/main"}}]}"#
|
||||
));
|
||||
assert_eq!(
|
||||
processed[0].source_sets[0].processed,
|
||||
Some(gradle::ProcessedResources { task: "processResources".into(), dir: "/p/build/resources/main".into() })
|
||||
);
|
||||
let src = |p: &str| PathBuf::from(core_src(p));
|
||||
assert_eq!(
|
||||
gradle::reported_source_roots(&projects),
|
||||
@@ -802,12 +824,18 @@ mod tests {
|
||||
..Default::default()
|
||||
})
|
||||
.collect(),
|
||||
resources: true,
|
||||
};
|
||||
write_cache(&cache_dir, GRADLE_CACHE, &cache).unwrap();
|
||||
apply_gradle_cache(&root, &cache_dir, &mut model);
|
||||
assert_eq!(model.projects[1].tasks.len(), 1);
|
||||
assert!(model.projects[0].tasks.iter().any(|t| t.name == "wrapper"), "root keeps its defaults");
|
||||
assert!(!model.stale);
|
||||
write_cache(&cache_dir, GRADLE_CACHE, &GradleCache { resources: false, ..cache.clone() }).unwrap();
|
||||
let mut m = gradle::load(&root).unwrap();
|
||||
apply_gradle_cache(&root, &cache_dir, &mut m);
|
||||
assert!(m.stale, "synced before resource processing was read");
|
||||
write_cache(&cache_dir, GRADLE_CACHE, &cache).unwrap();
|
||||
|
||||
let stale_after = |script: &str| {
|
||||
std::fs::write(root.join("app/build.gradle"), script).unwrap();
|
||||
@@ -841,6 +869,7 @@ mod tests {
|
||||
dir: root.to_string_lossy().into_owned(),
|
||||
..Default::default()
|
||||
}],
|
||||
resources: true,
|
||||
..Default::default()
|
||||
};
|
||||
write_cache(&cache_dir, GRADLE_CACHE, &cache).unwrap();
|
||||
@@ -887,6 +916,8 @@ mod tests {
|
||||
let text = "[INFO] x\n[ERROR] Failed to execute goal\n[ERROR] \n[ERROR] Re-run Maven using -X\n";
|
||||
assert_eq!(error_summary(text), "Failed to execute goal");
|
||||
assert_eq!(error_summary("one\nlast line\n\n"), "last line");
|
||||
let missing_jar = "错误: 找不到或无法加载主类 org.gradle.wrapper.GradleWrapperMain\n";
|
||||
assert!(error_summary(missing_jar).starts_with("gradle/wrapper/gradle-wrapper.jar is missing"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "ide-clipboard"
|
||||
description = "The OS clipboard's file list (Explorer <-> project view): Windows CF_HDROP or macOS NSPasteboard file URLs, no window or message loop"
|
||||
description = "The OS clipboard's file list (Explorer <-> project view) and pasted images: Windows CF_HDROP / PNG / CF_DIB or macOS NSPasteboard, no window or message loop"
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
license.workspace = true
|
||||
@@ -9,8 +9,14 @@ license.workspace = true
|
||||
|
||||
[target.'cfg(target_os = "macos")'.dependencies]
|
||||
objc2 = { version = "0.6", default-features = false, features = ["std"] }
|
||||
objc2-foundation = { version = "0.3", default-features = false, features = ["std", "NSArray", "NSDictionary", "NSEnumerator", "NSObject", "NSString", "NSURL", "NSValue"] }
|
||||
objc2-app-kit = { version = "0.3", default-features = false, features = ["std", "NSPasteboard"] }
|
||||
objc2-foundation = { version = "0.3", default-features = false, features = ["std", "NSArray", "NSData", "NSDictionary", "NSEnumerator", "NSObject", "NSString", "NSURL", "NSValue"] }
|
||||
objc2-app-kit = { version = "0.3", default-features = false, features = ["std", "NSBitmapImageRep", "NSImageRep", "NSPasteboard"] }
|
||||
|
||||
[target.'cfg(windows)'.dependencies]
|
||||
png = "0.18"
|
||||
|
||||
[target.'cfg(target_os = "linux")'.dependencies]
|
||||
gtk = "0.18"
|
||||
|
||||
[dev-dependencies]
|
||||
png = "0.18"
|
||||
@@ -18,6 +18,10 @@
|
||||
//!
|
||||
//! Other platforms (`text/uri-list` on X11) are not wired up: [`files`] is
|
||||
//! empty and [`set_files`] fails there.
|
||||
//!
|
||||
//! [`png`] reads an image (a screenshot) for the project view's Paste, which
|
||||
//! saves it as a file: `public.png` or `public.tiff` on macOS, the registered
|
||||
//! `PNG` format or `CF_DIB` on Windows. Linux reads it through GTK in the app.
|
||||
|
||||
use std::path::PathBuf;
|
||||
|
||||
@@ -27,6 +31,26 @@ pub fn set_text(text: &str, owner: usize) -> std::io::Result<()> {
|
||||
imp::set_text(text, owner)
|
||||
}
|
||||
|
||||
/// The clipboard's plain text, None when it holds none. macOS only (main thread);
|
||||
/// Linux reads it asynchronously through GTK in the app, Windows through the WebView.
|
||||
#[cfg(target_os = "macos")]
|
||||
pub fn text() -> Option<String> {
|
||||
imp::text()
|
||||
}
|
||||
|
||||
/// The clipboard's image as PNG bytes, None when it holds none. Call on the main
|
||||
/// thread on macOS.
|
||||
#[cfg(any(windows, target_os = "macos"))]
|
||||
pub fn png() -> Option<Vec<u8>> {
|
||||
imp::png()
|
||||
}
|
||||
|
||||
/// Whether the clipboard holds an image, without reading it. Call on the main
|
||||
/// thread on macOS and Linux.
|
||||
pub fn has_image() -> bool {
|
||||
imp::has_image()
|
||||
}
|
||||
|
||||
/// Size of `DROPFILES`: `pFiles`, `pt` (two `LONG`s), `fNC`, `fWide`.
|
||||
const HEADER: usize = 20;
|
||||
|
||||
@@ -127,9 +151,62 @@ pub fn decode(bytes: &[u8]) -> Vec<PathBuf> {
|
||||
out
|
||||
}
|
||||
|
||||
/// `BI_RGB` and `BI_BITFIELDS`: uncompressed pixels, the forms screenshots take.
|
||||
#[cfg(any(windows, test))]
|
||||
const BI_RGB: u32 = 0;
|
||||
#[cfg(any(windows, test))]
|
||||
const BI_BITFIELDS: u32 = 3;
|
||||
|
||||
/// A packed DIB (`BITMAPINFOHEADER` or a later header, then the pixels) as PNG.
|
||||
/// Only 24- and 32-bit uncompressed bitmaps, which is what screenshots and
|
||||
/// copied images are; None for anything else. A 32-bit bitmap whose alpha is
|
||||
/// all zero (most writers leave it unset) is opaque.
|
||||
#[cfg(any(windows, test))]
|
||||
fn dib_to_png(dib: &[u8]) -> Option<Vec<u8>> {
|
||||
let u32_at = |at: usize| dib.get(at..at + 4).map(|b| u32::from_le_bytes([b[0], b[1], b[2], b[3]]));
|
||||
let header = u32_at(0)? as usize;
|
||||
let width = u32_at(4)? as i32;
|
||||
let height = u32_at(8)? as i32;
|
||||
let bits = u16::from_le_bytes([*dib.get(14)?, *dib.get(15)?]);
|
||||
let compression = u32_at(16)?;
|
||||
if header < 40 || width <= 0 || height == 0 || !matches!(bits, 24 | 32) {
|
||||
return None;
|
||||
}
|
||||
if compression != BI_RGB && !(compression == BI_BITFIELDS && bits == 32) {
|
||||
return None;
|
||||
}
|
||||
// A `BITMAPINFOHEADER` keeps the three bitfield masks after it; later
|
||||
// headers hold them inside. Masks other than BGRA's are not supported.
|
||||
let masks = if compression == BI_BITFIELDS && header == 40 { 12 } else { 0 };
|
||||
let colors = u32_at(32)? as usize;
|
||||
let (width, rows) = (width as usize, height.unsigned_abs() as usize);
|
||||
let stride = (width * bits as usize).div_ceil(32) * 4;
|
||||
let start = header + masks + colors * 4;
|
||||
let pixels = dib.get(start..start.checked_add(stride.checked_mul(rows)?)?)?;
|
||||
let step = bits as usize / 8;
|
||||
let mut rgba = Vec::with_capacity(width * rows * 4);
|
||||
for row in 0..rows {
|
||||
// A positive height stores the bottom row first.
|
||||
let source = if height > 0 { rows - 1 - row } else { row };
|
||||
let line = &pixels[source * stride..source * stride + width * step];
|
||||
for px in line.chunks_exact(step) {
|
||||
rgba.extend_from_slice(&[px[2], px[1], px[0], if step == 4 { px[3] } else { 255 }]);
|
||||
}
|
||||
}
|
||||
if step == 4 && rgba.as_chunks::<4>().0.iter().all(|px| px[3] == 0) {
|
||||
rgba.as_chunks_mut::<4>().0.iter_mut().for_each(|px| px[3] = 255);
|
||||
}
|
||||
let mut out = Vec::new();
|
||||
let mut encoder = png::Encoder::new(&mut out, width as u32, rows as u32);
|
||||
encoder.set_color(png::ColorType::Rgba);
|
||||
encoder.set_depth(png::BitDepth::Eight);
|
||||
encoder.write_header().ok()?.write_image_data(&rgba).ok()?;
|
||||
Some(out)
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
mod imp {
|
||||
use super::{HEADER, decode, encode, names_text};
|
||||
use super::{HEADER, decode, dib_to_png, encode, names_text};
|
||||
use std::ffi::c_void;
|
||||
use std::path::PathBuf;
|
||||
use std::ptr;
|
||||
@@ -157,6 +234,59 @@ mod imp {
|
||||
fn GetClipboardData(format: u32) -> Handle;
|
||||
fn SetClipboardData(format: u32, memory: Handle) -> Handle;
|
||||
fn IsClipboardFormatAvailable(format: u32) -> i32;
|
||||
fn RegisterClipboardFormatW(name: *const u16) -> u32;
|
||||
}
|
||||
|
||||
/// `CF_DIB`: Windows synthesizes it from `CF_BITMAP` and `CF_DIBV5`, so every
|
||||
/// bitmap on the clipboard reads as one.
|
||||
const CF_DIB: u32 = 8;
|
||||
|
||||
/// The `PNG` format browsers and the Snipping Tool add beside the bitmap; it
|
||||
/// keeps transparency, so it is read first.
|
||||
fn png_format() -> u32 {
|
||||
let name: Vec<u16> = "PNG\0".encode_utf16().collect();
|
||||
unsafe { RegisterClipboardFormatW(name.as_ptr()) }
|
||||
}
|
||||
|
||||
pub fn has_image() -> bool {
|
||||
let png = png_format();
|
||||
(png != 0 && unsafe { IsClipboardFormatAvailable(png) } != 0)
|
||||
|| unsafe { IsClipboardFormatAvailable(CF_DIB) } != 0
|
||||
}
|
||||
|
||||
/// A copy of the clipboard's `format` block. Call with the clipboard open.
|
||||
fn read_block(format: u32) -> Option<Vec<u8>> {
|
||||
// SAFETY: the clipboard keeps the block alive while it is open, and
|
||||
// `GlobalSize` bounds the bytes read from it.
|
||||
unsafe {
|
||||
let memory = GetClipboardData(format);
|
||||
if memory.is_null() {
|
||||
return None;
|
||||
}
|
||||
let data = GlobalLock(memory);
|
||||
if data.is_null() {
|
||||
return None;
|
||||
}
|
||||
let bytes = std::slice::from_raw_parts(data as *const u8, GlobalSize(memory)).to_vec();
|
||||
GlobalUnlock(memory);
|
||||
Some(bytes)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn png() -> Option<Vec<u8>> {
|
||||
if !open() {
|
||||
return None;
|
||||
}
|
||||
let png = png_format();
|
||||
let image = if png != 0 && unsafe { IsClipboardFormatAvailable(png) } != 0 {
|
||||
read_block(png)
|
||||
} else if unsafe { IsClipboardFormatAvailable(CF_DIB) } != 0 {
|
||||
read_block(CF_DIB).and_then(|dib| dib_to_png(&dib))
|
||||
} else {
|
||||
None
|
||||
};
|
||||
unsafe { CloseClipboard() };
|
||||
image
|
||||
}
|
||||
|
||||
#[link(name = "kernel32")]
|
||||
@@ -320,8 +450,8 @@ mod imp {
|
||||
use objc2::runtime::{AnyClass, AnyObject, ProtocolObject};
|
||||
use objc2::{ClassType, MainThreadMarker};
|
||||
use objc2_app_kit::{
|
||||
NSPasteboard, NSPasteboardReadingOptionKey, NSPasteboardTypeString, NSPasteboardURLReadingFileURLsOnlyKey,
|
||||
NSPasteboardWriting,
|
||||
NSBitmapImageFileType, NSBitmapImageRep, NSPasteboard, NSPasteboardReadingOptionKey, NSPasteboardTypePNG,
|
||||
NSPasteboardTypeString, NSPasteboardTypeTIFF, NSPasteboardURLReadingFileURLsOnlyKey, NSPasteboardWriting,
|
||||
};
|
||||
use objc2_foundation::{NSArray, NSDictionary, NSNumber, NSString, NSURL};
|
||||
use std::path::PathBuf;
|
||||
@@ -373,6 +503,41 @@ mod imp {
|
||||
})
|
||||
}
|
||||
|
||||
pub fn has_image() -> bool {
|
||||
debug_assert!(MainThreadMarker::new().is_some(), "NSPasteboard access requires the main thread");
|
||||
autoreleasepool(|_| {
|
||||
// SAFETY: the types are AppKit constants, valid for the process' lifetime.
|
||||
let types = NSArray::from_slice(&[unsafe { NSPasteboardTypePNG }, unsafe { NSPasteboardTypeTIFF }]);
|
||||
NSPasteboard::generalPasteboard().availableTypeFromArray(&types).is_some()
|
||||
})
|
||||
}
|
||||
|
||||
/// PNG as copied, else TIFF (what macOS screenshots and most apps write) converted.
|
||||
pub fn png() -> Option<Vec<u8>> {
|
||||
debug_assert!(MainThreadMarker::new().is_some(), "NSPasteboard access requires the main thread");
|
||||
autoreleasepool(|_| {
|
||||
let board = NSPasteboard::generalPasteboard();
|
||||
// SAFETY: the types are AppKit constants, valid for the process' lifetime.
|
||||
if let Some(png) = board.dataForType(unsafe { NSPasteboardTypePNG }) {
|
||||
return Some(png.to_vec());
|
||||
}
|
||||
let tiff = board.dataForType(unsafe { NSPasteboardTypeTIFF })?;
|
||||
let image = NSBitmapImageRep::imageRepWithData(&tiff)?;
|
||||
// SAFETY: an empty property dictionary is valid for every file type.
|
||||
let png =
|
||||
unsafe { image.representationUsingType_properties(NSBitmapImageFileType::PNG, &NSDictionary::new()) }?;
|
||||
Some(png.to_vec())
|
||||
})
|
||||
}
|
||||
|
||||
pub fn text() -> Option<String> {
|
||||
debug_assert!(MainThreadMarker::new().is_some(), "NSPasteboard access requires the main thread");
|
||||
autoreleasepool(|_| {
|
||||
// SAFETY: the type is an AppKit constant, valid for the process' lifetime.
|
||||
NSPasteboard::generalPasteboard().stringForType(unsafe { NSPasteboardTypeString }).map(|s| s.to_string())
|
||||
})
|
||||
}
|
||||
|
||||
pub fn set_text(text: &str, _owner: usize) -> std::io::Result<()> {
|
||||
debug_assert!(MainThreadMarker::new().is_some(), "NSPasteboard access requires the main thread");
|
||||
autoreleasepool(|_| {
|
||||
@@ -433,6 +598,17 @@ mod imp {
|
||||
false
|
||||
}
|
||||
|
||||
pub fn has_image() -> bool {
|
||||
#[cfg(target_os = "linux")]
|
||||
{
|
||||
gtk::Clipboard::get(>k::gdk::SELECTION_CLIPBOARD).wait_is_image_available()
|
||||
}
|
||||
#[cfg(not(target_os = "linux"))]
|
||||
{
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
pub fn files() -> std::io::Result<Vec<PathBuf>> {
|
||||
Ok(Vec::new())
|
||||
}
|
||||
@@ -506,4 +682,62 @@ mod tests {
|
||||
bytes.extend_from_slice(b"C:\\a\\b.txt\0C:\\c\0\0");
|
||||
assert_eq!(decode(&bytes), paths(&["C:\\a\\b.txt", "C:\\c"]));
|
||||
}
|
||||
|
||||
/// A `BITMAPINFOHEADER` DIB of `width` × `height` (positive: bottom-up).
|
||||
fn dib(width: i32, height: i32, bits: u16, compression: u32, rows: &[&[u8]]) -> Vec<u8> {
|
||||
let mut out = Vec::new();
|
||||
out.extend_from_slice(&40u32.to_le_bytes());
|
||||
out.extend_from_slice(&width.to_le_bytes());
|
||||
out.extend_from_slice(&height.to_le_bytes());
|
||||
out.extend_from_slice(&1u16.to_le_bytes());
|
||||
out.extend_from_slice(&bits.to_le_bytes());
|
||||
out.extend_from_slice(&compression.to_le_bytes());
|
||||
out.extend_from_slice(&[0u8; 20]);
|
||||
if compression == super::BI_BITFIELDS {
|
||||
for mask in [0x00ff_0000u32, 0x0000_ff00, 0x0000_00ff] {
|
||||
out.extend_from_slice(&mask.to_le_bytes());
|
||||
}
|
||||
}
|
||||
for row in rows {
|
||||
out.extend_from_slice(row);
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
fn decode_png(png: &[u8]) -> (u32, u32, Vec<u8>) {
|
||||
let mut reader = png::Decoder::new(std::io::Cursor::new(png)).read_info().unwrap();
|
||||
let mut buf = vec![0; reader.output_buffer_size().unwrap()];
|
||||
let info = reader.next_frame(&mut buf).unwrap();
|
||||
buf.truncate(info.buffer_size());
|
||||
(info.width, info.height, buf)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_bottom_up_24_bit_bitmap_becomes_an_upright_opaque_png() {
|
||||
// Rows are padded to 4 bytes; the first stored row is the bottom one.
|
||||
let bottom: &[u8] = &[255, 0, 0, 0, 255, 0, 0, 0];
|
||||
let top: &[u8] = &[0, 0, 255, 255, 255, 255, 0, 0];
|
||||
let png = super::dib_to_png(&dib(2, 2, 24, super::BI_RGB, &[bottom, top])).unwrap();
|
||||
let (w, h, rgba) = decode_png(&png);
|
||||
assert_eq!((w, h), (2, 2));
|
||||
assert_eq!(rgba, [255, 0, 0, 255, 255, 255, 255, 255, 0, 0, 255, 255, 0, 255, 0, 255]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_32_bit_bitmap_keeps_its_alpha_unless_none_was_written() {
|
||||
let row: &[u8] = &[10, 20, 30, 0, 40, 50, 60, 0];
|
||||
let (_, _, rgba) = decode_png(&super::dib_to_png(&dib(2, -1, 32, super::BI_BITFIELDS, &[row])).unwrap());
|
||||
assert_eq!(rgba, [30, 20, 10, 255, 60, 50, 40, 255]);
|
||||
let row: &[u8] = &[10, 20, 30, 128, 40, 50, 60, 0];
|
||||
let (_, _, rgba) = decode_png(&super::dib_to_png(&dib(2, -1, 32, super::BI_RGB, &[row])).unwrap());
|
||||
assert_eq!(rgba, [30, 20, 10, 128, 60, 50, 40, 0]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unsupported_or_truncated_bitmaps_are_not_images() {
|
||||
let row: &[u8] = &[1, 2, 3, 4];
|
||||
assert!(super::dib_to_png(&dib(4, 1, 8, super::BI_RGB, &[row])).is_none());
|
||||
assert!(super::dib_to_png(&dib(2, 2, 24, super::BI_RGB, &[row])).is_none());
|
||||
assert!(super::dib_to_png(&[0; 10]).is_none());
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,8 @@ use std::collections::BTreeMap;
|
||||
pub struct EditorGeneralSettings {
|
||||
/// Change font size with ⌘ + mouse wheel.
|
||||
pub wheel_zoom: bool,
|
||||
/// Scope of ⌘ + mouse wheel font zoom: `active` editor | `all` editors.
|
||||
pub wheel_zoom_scope: String,
|
||||
pub markdown_image_folder: String,
|
||||
pub markdown_image_name_pattern: String,
|
||||
/// Drag-and-drop of selected text inside the editor.
|
||||
@@ -37,6 +39,7 @@ impl Default for EditorGeneralSettings {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
wheel_zoom: false,
|
||||
wheel_zoom_scope: "active".into(),
|
||||
markdown_image_folder: "assets".into(),
|
||||
markdown_image_name_pattern: "image-{timestamp}".into(),
|
||||
drag_and_drop: true,
|
||||
@@ -67,6 +70,8 @@ pub struct EditorAppearanceSettings {
|
||||
/// Smooth caret movement curve: `snappy` | `gliding`.
|
||||
pub caret_movement_style: String,
|
||||
pub line_numbers: bool,
|
||||
/// Gutter numbering: `absolute` | `relative` | `hybrid` (absolute on the caret line, distances elsewhere).
|
||||
pub line_number_mode: String,
|
||||
pub method_separators: bool,
|
||||
/// Sub-options of `editor.showWhitespace`.
|
||||
pub whitespace_leading: bool,
|
||||
@@ -78,6 +83,10 @@ pub struct EditorAppearanceSettings {
|
||||
/// Right margin and visual guides from Code Style.
|
||||
pub hard_wrap_guides: bool,
|
||||
pub intention_bulb: bool,
|
||||
/// Draw doc comments (Javadoc, KDoc, JSDoc, rustdoc) as rendered documentation
|
||||
/// instead of their source, as IDEA's option of the same name. View › Active
|
||||
/// Editor › Render Documentation Comments overrides this for one editor.
|
||||
pub render_doc_comments: bool,
|
||||
/// Distraction Free Mode: width of the centred text column, in characters.
|
||||
pub distraction_free_width: u32,
|
||||
}
|
||||
@@ -92,6 +101,7 @@ impl Default for EditorAppearanceSettings {
|
||||
smooth_caret_movement: false,
|
||||
caret_movement_style: "snappy".into(),
|
||||
line_numbers: true,
|
||||
line_number_mode: "absolute".into(),
|
||||
method_separators: false,
|
||||
whitespace_leading: true,
|
||||
whitespace_inner: true,
|
||||
@@ -100,6 +110,7 @@ impl Default for EditorAppearanceSettings {
|
||||
rainbow_indent_guides: false,
|
||||
hard_wrap_guides: true,
|
||||
intention_bulb: true,
|
||||
render_doc_comments: false,
|
||||
distraction_free_width: 80,
|
||||
}
|
||||
}
|
||||
@@ -262,6 +273,8 @@ pub struct SmartKeysSettings {
|
||||
pub pair_quotes: bool,
|
||||
pub surround_selection: bool,
|
||||
pub tab_out_of_pair: bool,
|
||||
/// Simultaneous `<tag></tag>` editing: renaming a tag renames its matching tag.
|
||||
pub simultaneous_tag_editing: bool,
|
||||
/// Reformat on paste: `none` | `indentBlock` | `indentEachLine`.
|
||||
pub reformat_on_paste: String,
|
||||
pub escape_on_paste: bool,
|
||||
@@ -281,6 +294,7 @@ impl Default for SmartKeysSettings {
|
||||
pair_quotes: true,
|
||||
surround_selection: true,
|
||||
tab_out_of_pair: true,
|
||||
simultaneous_tag_editing: true,
|
||||
reformat_on_paste: "indentEachLine".into(),
|
||||
escape_on_paste: true,
|
||||
smart_indent: true,
|
||||
@@ -333,6 +347,23 @@ pub struct InlayHintsSettings {
|
||||
pub code_vision_usages: bool,
|
||||
/// Code vision: last author of a declaration (VCS blame) beside its usages.
|
||||
pub code_vision_author: bool,
|
||||
/// Code vision: "no usages" on declarations nothing uses (IDEA shows it; clicking searches everywhere).
|
||||
pub code_vision_no_usages: bool,
|
||||
/// Code vision: where the usages go, above the declaration or at the end of its line.
|
||||
pub code_vision_usages_position: CodeVisionPosition,
|
||||
/// Code vision: where the author goes, above the declaration or at the end of its line.
|
||||
pub code_vision_author_position: CodeVisionPosition,
|
||||
}
|
||||
|
||||
/// Editor › Inlay Hints › Code Vision position (IDEA's "Default position for metrics").
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Serialize, Deserialize, Type)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub enum CodeVisionPosition {
|
||||
/// A row of its own above the declaration.
|
||||
#[default]
|
||||
Top,
|
||||
/// After the end of the declaration's first line.
|
||||
Right,
|
||||
}
|
||||
|
||||
impl Default for InlayHintsSettings {
|
||||
@@ -344,6 +375,9 @@ impl Default for InlayHintsSettings {
|
||||
disabled_languages: Vec::new(),
|
||||
code_vision_usages: true,
|
||||
code_vision_author: true,
|
||||
code_vision_no_usages: true,
|
||||
code_vision_usages_position: CodeVisionPosition::Top,
|
||||
code_vision_author_position: CodeVisionPosition::Top,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,13 +8,18 @@
|
||||
use crate::{DebugSessionId, Diagnostic, DocId, RunId, ServerId, TaskId, TerminalId, TextChange};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use specta::Type;
|
||||
use std::collections::BTreeMap;
|
||||
use std::sync::Arc;
|
||||
|
||||
/// What a built-in MCP tool asks the project window for. `AgentRun` runs `command` (a shell
|
||||
/// line) in the Run tool window's agent tab `name`, on pipes, answered once the process
|
||||
/// started or failed to.
|
||||
#[derive(Clone, Debug, Serialize, Deserialize, Type)]
|
||||
#[serde(tag = "type", rename_all = "camelCase", rename_all_fields = "camelCase")]
|
||||
pub enum McpUiRequest {
|
||||
Confirm { client: String, tool: String, subject: String, project: String, detail: Option<String> },
|
||||
OpenFile { path: crate::IpcPath, line: Option<u32>, column: Option<u32> },
|
||||
AgentRun { name: String, command: String, cwd: crate::IpcPath, env: BTreeMap<String, String> },
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize, Type)]
|
||||
@@ -72,6 +77,9 @@ pub enum IdeEvent {
|
||||
WorkspaceEditApplied {
|
||||
id: u32,
|
||||
paths: Vec<crate::IpcPath>,
|
||||
/// Made by an agent through the MCP server's tools: undoable like a refactoring, but
|
||||
/// not announced as one.
|
||||
by_agent: bool,
|
||||
},
|
||||
/// An open document's language changed (Settings › File Types, Override File
|
||||
/// Type), or its UI mode (a framework's root marker appeared or went).
|
||||
@@ -110,6 +118,10 @@ pub enum IdeEvent {
|
||||
LanguageServer {
|
||||
server: ServerId,
|
||||
name: String,
|
||||
/// What the user sees as the server when another program serves in its place.
|
||||
#[serde(default)]
|
||||
#[specta(optional)]
|
||||
label: Option<String>,
|
||||
status: ServerStatus,
|
||||
message: Option<String>,
|
||||
},
|
||||
@@ -273,6 +285,82 @@ pub enum DebugEvent {
|
||||
change_type: String,
|
||||
message: String,
|
||||
},
|
||||
/// The session's capabilities changed after it started: fetch them again.
|
||||
Capabilities,
|
||||
/// The adapter's `invalidated` event: what it shows for `areas` (`all`, `stacks`, `threads`,
|
||||
/// `variables`; empty means all) is stale and has to be fetched again.
|
||||
Invalidated {
|
||||
areas: Vec<String>,
|
||||
},
|
||||
JvmBackend {
|
||||
state: JvmBackendState,
|
||||
},
|
||||
/// A step in the life of one request of a debugged page.
|
||||
JsNetwork {
|
||||
event: JsNetworkEvent,
|
||||
},
|
||||
/// The adapter does not report the requests of this page: there will be no `JsNetwork`.
|
||||
JsNetworkUnavailable,
|
||||
}
|
||||
|
||||
/// Which step of a request a [`JsNetworkEvent`] reports.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize, Type)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub enum JsNetworkKind {
|
||||
Request,
|
||||
Response,
|
||||
Finished,
|
||||
Failed,
|
||||
}
|
||||
|
||||
/// The script frame that made a request, where the browser ran it (0-based line and column).
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Type)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct JsInitiator {
|
||||
pub function: String,
|
||||
pub url: String,
|
||||
pub line: u32,
|
||||
pub column: u32,
|
||||
}
|
||||
|
||||
/// What a debugged page's browser reports about a request, reduced to what the Network view
|
||||
/// shows. Events of one request share `request_id`; each kind fills its own fields.
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Type)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct JsNetworkEvent {
|
||||
pub kind: JsNetworkKind,
|
||||
pub request_id: String,
|
||||
/// The browser's monotonic clock, comparable between the events of a page.
|
||||
pub timestamp_ms: u64,
|
||||
/// Request and response.
|
||||
pub url: Option<String>,
|
||||
pub method: Option<String>,
|
||||
/// `Fetch`, `XHR`, `Script`, `Document`, …
|
||||
pub resource_type: Option<String>,
|
||||
pub status: Option<u16>,
|
||||
pub mime_type: Option<String>,
|
||||
/// The request's headers, or the response's.
|
||||
pub headers: BTreeMap<String, String>,
|
||||
/// Bytes received over the network, once finished.
|
||||
pub encoded_length: Option<u64>,
|
||||
pub error_text: Option<String>,
|
||||
pub initiator: Option<JsInitiator>,
|
||||
}
|
||||
|
||||
/// The backend that reads a debugged JVM's objects (instances, fields, referrers).
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Type)]
|
||||
#[serde(tag = "kind", rename_all = "camelCase", rename_all_fields = "camelCase")]
|
||||
pub enum JvmBackendState {
|
||||
Ready,
|
||||
/// Turned off for the rest of the session: the debug adapter lacks what it needs.
|
||||
Unsupported {
|
||||
detail: String,
|
||||
},
|
||||
/// The Kotlin JDWP link only forwards now; inspecting stops.
|
||||
Degraded {
|
||||
detail: String,
|
||||
},
|
||||
Closed,
|
||||
}
|
||||
|
||||
/// Where backend crates publish [`IdeEvent`]s. Cheap to clone via `Arc`.
|
||||
@@ -294,3 +382,27 @@ pub struct NullSink;
|
||||
impl EventSink for NullSink {
|
||||
fn emit(&self, _: IdeEvent) {}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use serde_json::json;
|
||||
|
||||
#[test]
|
||||
fn jvm_backend_events_json_shape() {
|
||||
let degraded = DebugEvent::JvmBackend { state: JvmBackendState::Degraded { detail: "x".into() } };
|
||||
assert_eq!(
|
||||
serde_json::to_value(°raded).unwrap(),
|
||||
json!({"kind": "jvmBackend", "state": {"kind": "degraded", "detail": "x"}})
|
||||
);
|
||||
assert_eq!(serde_json::to_value(DebugEvent::Capabilities).unwrap(), json!({"kind": "capabilities"}));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn invalidated_event_json_shape() {
|
||||
assert_eq!(
|
||||
serde_json::to_value(DebugEvent::Invalidated { areas: vec!["variables".into()] }).unwrap(),
|
||||
json!({"kind": "invalidated", "areas": ["variables"]})
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -14,7 +14,8 @@ pub fn inside_git_repo(dir: &Path) -> bool {
|
||||
dir.ancestors().any(|d| d.join(".git").exists() || d.join(".jj").exists())
|
||||
}
|
||||
|
||||
/// IDEA's default list, plus `.jj` and the Windows shell files.
|
||||
/// IDEA's default list, plus `.jj`, rope's `.ropeproject` (pylsp-rope creates it in the
|
||||
/// project root) and the Windows shell files.
|
||||
pub const DEFAULT_IGNORED_NAMES: &[&str] = &[
|
||||
"*.pyc",
|
||||
"*.pyo",
|
||||
@@ -25,6 +26,7 @@ pub const DEFAULT_IGNORED_NAMES: &[&str] = &[
|
||||
".git",
|
||||
".hg",
|
||||
".jj",
|
||||
".ropeproject",
|
||||
".svn",
|
||||
"CVS",
|
||||
"__pycache__",
|
||||
|
||||
@@ -12,11 +12,16 @@ use std::path::{Path, PathBuf};
|
||||
|
||||
use serde::{Deserialize, Deserializer, Serialize, Serializer};
|
||||
|
||||
/// `p` as the UI sees it: `C:\a\b` and `\\?\C:\a\b` become `C:/a/b`.
|
||||
/// `p` as the UI sees it: `C:\a\b`, `c:\a\b` and `\\?\C:\a\b` become `C:/a/b`.
|
||||
pub fn to_ipc(p: &Path) -> String {
|
||||
#[cfg(windows)]
|
||||
{
|
||||
dunce::simplified(p).to_string_lossy().replace('\\', "/")
|
||||
let mut s = dunce::simplified(p).to_string_lossy().replace('\\', "/");
|
||||
// The UI compares path strings; a drive letter is case-insensitive.
|
||||
if s.as_bytes().get(1) == Some(&b':') {
|
||||
s[..1].make_ascii_uppercase();
|
||||
}
|
||||
s
|
||||
}
|
||||
#[cfg(not(windows))]
|
||||
{
|
||||
@@ -144,5 +149,8 @@ mod tests {
|
||||
assert_eq!(to_ipc(Path::new(r"\\?\C:\a\b")), "C:/a/b");
|
||||
assert_eq!(to_ipc(&Path::new("C:/a").join("b")), "C:/a/b");
|
||||
assert_eq!(serde_json::to_string(&IpcPath::from(r"C:\a\b")).unwrap(), r#""C:/a/b""#);
|
||||
assert_eq!(to_ipc(Path::new(r"c:\a\b")), "C:/a/b");
|
||||
assert_eq!(to_ipc(Path::new(r"\\?\d:\a")), "D:/a");
|
||||
assert_eq!(to_ipc(Path::new(r"\\server\share\a")), "//server/share/a");
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,10 @@
|
||||
//! JSON with comments (`devcontainer.json`, `deno.jsonc`, `tsconfig.json`): turned
|
||||
//! into plain JSON for `serde_json`.
|
||||
|
||||
/// Strips `//` and `/* */` comments and trailing commas so JSONC parses as JSON.
|
||||
/// Strips `//` and `/* */` comments, trailing commas and a leading byte order mark (Visual
|
||||
/// Studio writes one into `launchSettings.json`) so JSONC parses as JSON.
|
||||
pub fn strip(src: &str) -> String {
|
||||
let src = src.strip_prefix('\u{feff}').unwrap_or(src);
|
||||
let mut out = String::with_capacity(src.len());
|
||||
let mut chars = src.chars().peekable();
|
||||
let mut in_str = false;
|
||||
@@ -87,4 +89,10 @@ mod tests {
|
||||
assert_eq!(v["url"], "https://x.dev//y /* no */");
|
||||
assert_eq!(v["q"], "say \"hi\",");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn drops_a_byte_order_mark() {
|
||||
let v: serde_json::Value = serde_json::from_str(&strip("\u{feff}{ \"a\": 1 }")).unwrap();
|
||||
assert_eq!(v["a"], 1);
|
||||
}
|
||||
}
|
||||
@@ -21,6 +21,7 @@ pub mod process;
|
||||
pub mod project_dir;
|
||||
pub mod project_roots;
|
||||
pub mod proxy_settings;
|
||||
pub mod real_path;
|
||||
pub mod remote_settings;
|
||||
pub mod settings;
|
||||
pub mod task;
|
||||
|
||||
@@ -21,6 +21,32 @@
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
/// Removes ANSI escape sequences (colour, cursor movement).
|
||||
pub fn strip_ansi(s: &str) -> String {
|
||||
if !s.contains('\u{1b}') {
|
||||
return s.to_owned();
|
||||
}
|
||||
let mut out = String::with_capacity(s.len());
|
||||
let mut chars = s.chars().peekable();
|
||||
while let Some(c) = chars.next() {
|
||||
if c != '\u{1b}' {
|
||||
out.push(c);
|
||||
continue;
|
||||
}
|
||||
if chars.peek() == Some(&'[') {
|
||||
chars.next();
|
||||
for c in chars.by_ref() {
|
||||
if c.is_ascii_alphabetic() || c == '~' {
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
chars.next();
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// Decodes complete lines or collected output from IDE-managed tools, preferring UTF-8.
|
||||
/// Windows launcher/native diagnostics fall back to the system ANSI code page.
|
||||
/// Callers must buffer incomplete lines so multibyte characters are never split.
|
||||
@@ -50,7 +76,8 @@ fn decode_tool_output_with_code_page(bytes: &[u8], code_page: u32) -> String {
|
||||
text
|
||||
}
|
||||
|
||||
fn encoding_for_code_page(cp: u32) -> Option<&'static encoding_rs::Encoding> {
|
||||
/// The `encoding_rs` encoding of a Windows code page, when it has one.
|
||||
pub fn encoding_for_code_page(cp: u32) -> Option<&'static encoding_rs::Encoding> {
|
||||
Some(match cp {
|
||||
65001 => encoding_rs::UTF_8,
|
||||
936 => encoding_rs::GBK,
|
||||
@@ -75,6 +102,56 @@ fn encoding_for_code_page(cp: u32) -> Option<&'static encoding_rs::Encoding> {
|
||||
})
|
||||
}
|
||||
|
||||
/// The code page Windows console programs write to a pipe in: the OEM code page (GBK on a
|
||||
/// Chinese system, Shift_JIS on a Japanese one, EUC-KR on a Korean one, 437 / 850 / 866…
|
||||
/// elsewhere). `None` when it is UTF-8 (Windows' "Use Unicode UTF-8" option) or off Windows.
|
||||
pub fn console_code_page() -> Option<u32> {
|
||||
#[cfg(windows)]
|
||||
{
|
||||
// SAFETY: GetOEMCP is an argument-less query with no preconditions.
|
||||
let cp = unsafe { windows_sys::Win32::Globalization::GetOEMCP() };
|
||||
(cp != 65001).then_some(cp)
|
||||
}
|
||||
#[cfg(not(windows))]
|
||||
{
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether every character of code page `cp` is one byte, so text in it can be decoded
|
||||
/// piece by piece.
|
||||
#[cfg(windows)]
|
||||
pub fn is_single_byte_code_page(cp: u32) -> bool {
|
||||
use windows_sys::Win32::Globalization::{CPINFO, GetCPInfo};
|
||||
// SAFETY: CPINFO is plain data, which GetCPInfo fills in.
|
||||
unsafe {
|
||||
let mut info: CPINFO = std::mem::zeroed();
|
||||
GetCPInfo(cp, &mut info) != 0 && info.MaxCharSize == 1
|
||||
}
|
||||
}
|
||||
|
||||
/// `bytes` in code page `cp`, decoded by Windows (for the code pages `encoding_rs` lacks,
|
||||
/// like the OEM pages 437, 850 or 852).
|
||||
#[cfg(windows)]
|
||||
pub fn decode_code_page(bytes: &[u8], cp: u32) -> String {
|
||||
use windows_sys::Win32::Globalization::MultiByteToWideChar;
|
||||
let Ok(len) = i32::try_from(bytes.len()) else {
|
||||
return String::from_utf8_lossy(bytes).into_owned();
|
||||
};
|
||||
if len == 0 {
|
||||
return String::new();
|
||||
}
|
||||
// SAFETY: `bytes` holds `len` bytes; no output buffer asks for the length needed.
|
||||
let needed = unsafe { MultiByteToWideChar(cp, 0, bytes.as_ptr(), len, std::ptr::null_mut(), 0) };
|
||||
if needed <= 0 {
|
||||
return String::from_utf8_lossy(bytes).into_owned();
|
||||
}
|
||||
let mut wide = vec![0u16; needed as usize];
|
||||
// SAFETY: `wide` holds `needed` units.
|
||||
let written = unsafe { MultiByteToWideChar(cp, 0, bytes.as_ptr(), len, wide.as_mut_ptr(), needed) };
|
||||
String::from_utf16_lossy(&wide[..written.max(0) as usize])
|
||||
}
|
||||
|
||||
const STD_PROPERTIES: &[&str] = &["stdout.encoding", "stderr.encoding", "sun.stdout.encoding", "sun.stderr.encoding"];
|
||||
const FILE_ENCODING: &str = "file.encoding";
|
||||
const TOOL_OPTIONS: &str = "JAVA_TOOL_OPTIONS";
|
||||
|
||||
@@ -72,6 +72,99 @@ pub fn kill_tree_of(pid: u32) {
|
||||
let _ = pid;
|
||||
}
|
||||
|
||||
/// Whether a process with this id exists. PID reuse makes this conservative: a reused id keeps
|
||||
/// whatever was tied to the old process alive, a live process is never reported dead.
|
||||
pub fn process_alive(pid: u32) -> bool {
|
||||
// `kill(0, 0)` would signal the caller's own process group.
|
||||
if pid == 0 {
|
||||
return false;
|
||||
}
|
||||
#[cfg(unix)]
|
||||
{
|
||||
// Above `i32::MAX` the id would wrap to a negative value, which `kill` reads as a group.
|
||||
let Ok(pid) = libc::pid_t::try_from(pid) else { return false };
|
||||
// SAFETY: signal 0 delivers nothing; it only checks that the process exists and is reachable.
|
||||
unsafe { libc::kill(pid, 0) == 0 || std::io::Error::last_os_error().raw_os_error() == Some(libc::EPERM) }
|
||||
}
|
||||
#[cfg(windows)]
|
||||
{
|
||||
use std::os::windows::io::{AsRawHandle, FromRawHandle, OwnedHandle};
|
||||
use windows_sys::Win32::Foundation::{ERROR_ACCESS_DENIED, GetLastError, STILL_ACTIVE};
|
||||
use windows_sys::Win32::System::Threading::{
|
||||
GetExitCodeProcess, OpenProcess, PROCESS_QUERY_LIMITED_INFORMATION,
|
||||
};
|
||||
|
||||
// SAFETY: plain call; a non-null result is a process handle owned by this function.
|
||||
let handle = unsafe { OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, 0, pid) };
|
||||
if handle.is_null() {
|
||||
// A process we may not open still exists.
|
||||
// SAFETY: reads the thread's last error, set by the call above.
|
||||
return unsafe { GetLastError() } == ERROR_ACCESS_DENIED;
|
||||
}
|
||||
// SAFETY: `handle` is valid and nobody else owns it.
|
||||
let handle = unsafe { OwnedHandle::from_raw_handle(handle) };
|
||||
let mut code = 0u32;
|
||||
// SAFETY: a live process handle opened with query access; one out-parameter.
|
||||
let ok = unsafe { GetExitCodeProcess(handle.as_raw_handle(), &mut code) };
|
||||
// A handle that cannot be queried is not evidence that the process is gone.
|
||||
ok == 0 || code == STILL_ACTIVE as u32
|
||||
}
|
||||
#[cfg(not(any(unix, windows)))]
|
||||
{
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
/// The program and everything it started: an adapter may report the launcher shell, whose
|
||||
/// child is the debugged program, so stopping only the reported pid leaves the program alive.
|
||||
#[cfg(unix)]
|
||||
pub fn process_tree(pid: i32) -> Vec<i32> {
|
||||
use std::collections::HashMap;
|
||||
let Ok(out) = std::process::Command::new("ps").args(["-A", "-o", "pid=,ppid="]).output() else {
|
||||
return vec![pid];
|
||||
};
|
||||
let mut children: HashMap<i32, Vec<i32>> = HashMap::new();
|
||||
for line in String::from_utf8_lossy(&out.stdout).lines() {
|
||||
let mut it = line.split_whitespace();
|
||||
let (Some(p), Some(parent)) = (it.next(), it.next()) else { continue };
|
||||
let (Ok(p), Ok(parent)) = (p.parse::<i32>(), parent.parse::<i32>()) else { continue };
|
||||
children.entry(parent).or_default().push(p);
|
||||
}
|
||||
let mut tree = vec![pid];
|
||||
let mut at = 0;
|
||||
while at < tree.len() {
|
||||
if let Some(kids) = children.get(&tree[at]) {
|
||||
tree.extend(kids.iter().copied());
|
||||
}
|
||||
at += 1;
|
||||
}
|
||||
tree
|
||||
}
|
||||
|
||||
/// Every process `pid` started, directly or not, parents before their children.
|
||||
pub fn descendants(pid: u32) -> Vec<u32> {
|
||||
#[cfg(unix)]
|
||||
{
|
||||
let Ok(root) = i32::try_from(pid) else { return Vec::new() };
|
||||
process_tree(root).into_iter().skip(1).filter_map(|p| u32::try_from(p).ok()).collect()
|
||||
}
|
||||
#[cfg(windows)]
|
||||
{
|
||||
use std::os::windows::io::AsRawHandle;
|
||||
windows_tree::descendants(pid)
|
||||
.iter()
|
||||
// SAFETY: a live process handle opened with query access.
|
||||
.map(|h| unsafe { windows_sys::Win32::System::Threading::GetProcessId(h.as_raw_handle()) })
|
||||
.filter(|&p| p != 0)
|
||||
.collect()
|
||||
}
|
||||
#[cfg(not(any(unix, windows)))]
|
||||
{
|
||||
let _ = pid;
|
||||
Vec::new()
|
||||
}
|
||||
}
|
||||
|
||||
/// [`kill_tree`] that first asks the group to exit: `SIGTERM`, then `grace`, then `SIGKILL`.
|
||||
/// Git removes its `index.lock` on `SIGTERM`, where `SIGKILL` leaves it behind and the next
|
||||
/// git command fails. Windows has no such signal to send from outside the child's console,
|
||||
@@ -730,6 +823,30 @@ fn append_path_entries(path: &OsStr, extra: &OsStr) -> OsString {
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn process_alive_tells_live_from_dead_pids() {
|
||||
assert!(process_alive(std::process::id()));
|
||||
assert!(!process_alive(0));
|
||||
let mut cmd = if cfg!(windows) {
|
||||
let mut cmd = Command::new("cmd");
|
||||
cmd.args(["/C", "exit 0"]);
|
||||
cmd
|
||||
} else {
|
||||
Command::new("true")
|
||||
};
|
||||
let mut child = cmd.spawn().unwrap();
|
||||
let pid = child.id();
|
||||
child.wait().unwrap();
|
||||
drop(child);
|
||||
assert!(!process_alive(pid));
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn process_alive_rejects_ids_that_are_not_a_single_process() {
|
||||
assert!(!process_alive(u32::MAX));
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn kill_tree_of_reaches_what_the_child_started() {
|
||||
@@ -1050,6 +1167,25 @@ mod tests {
|
||||
}
|
||||
assert_eq!(find_executable("nope", &search), None);
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn process_tree_lists_the_child_of_a_shell() {
|
||||
let mut cmd = Command::new("sh");
|
||||
own_group(&mut cmd).args(["-c", "sleep 5 & wait"]);
|
||||
let mut child = cmd.spawn().unwrap();
|
||||
let sh_pid = child.id() as i32;
|
||||
// The shell forks `sleep` after it starts, so the tree grows a moment later.
|
||||
let deadline = std::time::Instant::now() + Duration::from_secs(2);
|
||||
let mut tree = process_tree(sh_pid);
|
||||
while tree.len() < 2 && std::time::Instant::now() < deadline {
|
||||
std::thread::sleep(Duration::from_millis(20));
|
||||
tree = process_tree(sh_pid);
|
||||
}
|
||||
kill_tree(&mut child);
|
||||
assert_eq!(tree[0], sh_pid, "the pid itself comes first");
|
||||
assert!(tree.len() >= 2 && tree[1..].iter().all(|p| *p != sh_pid), "the shell's child follows: {tree:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(all(test, unix))]
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
//! Where a path really lands, and whether it reaches git's own metadata, for writes that must
|
||||
//! stay inside a project and out of its `.git`.
|
||||
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
/// `joined` after `..`/symlink resolution as far as the file system allows:
|
||||
/// the canonical path when it exists, else the canonicalized deepest existing
|
||||
/// ancestor with the still-missing tail resolved textually — so the location
|
||||
/// of a missing path is still judged where it would really land.
|
||||
pub fn resolve_real(joined: &Path) -> PathBuf {
|
||||
let mut tail = Vec::new();
|
||||
let mut candidate = joined.to_path_buf();
|
||||
let mut real = loop {
|
||||
match dunce::canonicalize(&candidate) {
|
||||
Ok(real) => break real,
|
||||
Err(_) => {
|
||||
let Some(last) = candidate.components().next_back() else {
|
||||
return joined.to_path_buf();
|
||||
};
|
||||
tail.push(last.as_os_str().to_owned());
|
||||
if !candidate.pop() {
|
||||
return joined.to_path_buf();
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
for part in tail.iter().rev() {
|
||||
if part == ".." {
|
||||
real.pop();
|
||||
} else if part != "." {
|
||||
real.push(part);
|
||||
}
|
||||
}
|
||||
real
|
||||
}
|
||||
|
||||
/// Whether a path (relative to the project root) goes through git's own `.git` folder or file.
|
||||
/// git runs what it finds in `.git` (hooks, `gpg.program`, filters), so a content write must
|
||||
/// never reach it. Names are normalized for trailing dots and spaces and NTFS stream suffixes;
|
||||
/// the caller also checks the short-name-resolved path via [`resolve_real`].
|
||||
pub fn names_git_dir(rel: &Path) -> bool {
|
||||
rel.components().any(|part| {
|
||||
let name = part.as_os_str().to_string_lossy();
|
||||
let name = name.split(':').next().unwrap_or_default().trim_end_matches(['.', ' ']);
|
||||
name.eq_ignore_ascii_case(".git")
|
||||
})
|
||||
}
|
||||
|
||||
/// A resolved write destination under `root` that reaches git's own `.git` metadata, judged by
|
||||
/// the name given and by the name the file system resolves it to.
|
||||
pub fn reaches_git_dir(root: &Path, real: &Path) -> bool {
|
||||
real.strip_prefix(root).is_ok_and(names_git_dir) || resolve_real(real).strip_prefix(root).is_ok_and(names_git_dir)
|
||||
}
|
||||
@@ -77,6 +77,8 @@ pub enum AutoUpload {
|
||||
#[serde(default, rename_all = "camelCase")]
|
||||
pub struct DeploymentSettings {
|
||||
pub servers: Vec<DeploymentServer>,
|
||||
/// Server groups: Upload to… on a group uploads to each of its servers.
|
||||
pub groups: Vec<DeploymentGroup>,
|
||||
/// Server used by Upload / Download / Sync and automatic upload; empty = none.
|
||||
pub default_server: String,
|
||||
pub auto_upload: AutoUpload,
|
||||
@@ -92,6 +94,7 @@ impl Default for DeploymentSettings {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
servers: Vec::new(),
|
||||
groups: Vec::new(),
|
||||
default_server: String::new(),
|
||||
auto_upload: AutoUpload::Never,
|
||||
delete_remote: false,
|
||||
@@ -103,14 +106,47 @@ impl Default for DeploymentSettings {
|
||||
}
|
||||
}
|
||||
|
||||
/// Named set of deployment servers that files are uploaded to together.
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, Type)]
|
||||
#[serde(default, rename_all = "camelCase")]
|
||||
pub struct DeploymentGroup {
|
||||
pub id: String,
|
||||
pub name: String,
|
||||
/// [`DeploymentServer::id`]s, in upload order.
|
||||
pub servers: Vec<String>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize, Type)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub enum DeploymentType {
|
||||
#[default]
|
||||
Sftp,
|
||||
Ftp,
|
||||
/// FTP secured with `AUTH TLS` (explicit FTPS).
|
||||
Ftps,
|
||||
Local,
|
||||
}
|
||||
|
||||
/// Host and login of an FTP / FTPS deployment server. The password is kept with
|
||||
/// the saved passwords, under the server's id.
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Type)]
|
||||
#[serde(default, rename_all = "camelCase")]
|
||||
pub struct FtpConnection {
|
||||
pub host: String,
|
||||
/// `None`: 21.
|
||||
pub port: Option<u16>,
|
||||
/// Empty: anonymous login.
|
||||
pub user: String,
|
||||
/// Passive mode (the client opens data connections); off: active mode.
|
||||
pub passive: bool,
|
||||
}
|
||||
|
||||
impl Default for FtpConnection {
|
||||
fn default() -> Self {
|
||||
Self { host: String::new(), port: None, user: String::new(), passive: true }
|
||||
}
|
||||
}
|
||||
|
||||
/// A deployment filesystem and how project folders map onto it.
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, Type)]
|
||||
#[serde(default, rename_all = "camelCase")]
|
||||
@@ -120,6 +156,8 @@ pub struct DeploymentServer {
|
||||
pub name: String,
|
||||
/// [`SshConfiguration::id`].
|
||||
pub ssh_config: String,
|
||||
/// FTP / FTPS servers.
|
||||
pub ftp: FtpConnection,
|
||||
/// Absolute remote folder the mappings are relative to; empty = the login directory.
|
||||
pub root_path: String,
|
||||
/// Web server URL of the root (Open in Browser on the deployed file).
|
||||
@@ -225,8 +263,11 @@ mod deployment_tests {
|
||||
serde_json::from_str(r#"{"id":"existing","sshConfig":"ssh","rootPath":"/srv"}"#).unwrap();
|
||||
assert_eq!(legacy.kind, DeploymentType::Sftp);
|
||||
assert_eq!(legacy.ssh_config, "ssh");
|
||||
for kind in [DeploymentType::Sftp, DeploymentType::Local] {
|
||||
let server = DeploymentServer { kind, ..legacy.clone() };
|
||||
assert!(legacy.ftp.passive);
|
||||
let ftp =
|
||||
FtpConnection { host: "ftp.example.com".into(), port: Some(2121), user: "web".into(), passive: false };
|
||||
for kind in [DeploymentType::Sftp, DeploymentType::Ftp, DeploymentType::Ftps, DeploymentType::Local] {
|
||||
let server = DeploymentServer { kind, ftp: ftp.clone(), ..legacy.clone() };
|
||||
assert_eq!(
|
||||
serde_json::from_str::<DeploymentServer>(&serde_json::to_string(&server).unwrap()).unwrap(),
|
||||
server
|
||||
|
||||
@@ -42,6 +42,8 @@ pub struct Settings {
|
||||
pub terminal: TerminalSettings,
|
||||
pub actions_on_save: ActionsOnSaveSettings,
|
||||
pub external_tools: Vec<ExternalTool>,
|
||||
/// Tools › Diff & Merge › External Diff Tools.
|
||||
pub external_diff: ExternalDiffSettings,
|
||||
/// Language servers / debug adapters at a user-chosen location, by the
|
||||
/// command the language definition launches (`elixir-ls`, `codelldb`).
|
||||
pub tools: BTreeMap<String, ToolSettings>,
|
||||
@@ -63,6 +65,14 @@ pub struct Settings {
|
||||
pub agents: AgentSettings,
|
||||
/// Agents › MCP Server (global only).
|
||||
pub mcp_server: McpServerSettings,
|
||||
/// Tools › Agents: Command Execution Policy.
|
||||
pub agent_security: AgentSecuritySettings,
|
||||
/// Appearance & Behavior › Scopes: named file sets other settings refer to.
|
||||
pub scopes: Vec<NamedScope>,
|
||||
/// Appearance & Behavior › File Colors.
|
||||
pub file_colors: FileColorSettings,
|
||||
/// Database › Query Execution, Languages & Frameworks › SQL Resolution Scopes.
|
||||
pub database: DatabaseSettings,
|
||||
/// Plugin settings (`[contributes.settings]`): plugin id → its values by key.
|
||||
#[specta(type = BTreeMap<String, BTreeMap<String, specta_typescript::Unknown>>)]
|
||||
pub plugins: BTreeMap<String, serde_json::Map<String, serde_json::Value>>,
|
||||
@@ -84,6 +94,18 @@ pub struct AgentSettings {
|
||||
pub fold_turns: bool,
|
||||
/// Streaming text is revealed at a steady pace rather than chunk by chunk.
|
||||
pub smooth_text: bool,
|
||||
/// Conversations and the composer expand to the full width of the pane instead of staying in a centred column.
|
||||
pub fit_window_width: bool,
|
||||
/// While the agent works, playful words fill the pauses in what it reports.
|
||||
pub playful_status: bool,
|
||||
/// Seconds the agent reports nothing new before the playful words take over; 0 shows them
|
||||
/// from the start.
|
||||
pub playful_after_seconds: u32,
|
||||
/// The user's playful words by activity (`working`, `thinking`, `reading`, …), replacing
|
||||
/// the built-in ones of that activity; an empty list keeps its status factual.
|
||||
#[serde(deserialize_with = "or_default")]
|
||||
#[specta(type = BTreeMap<String, Vec<String>>)]
|
||||
pub playful_words: BTreeMap<String, Vec<String>>,
|
||||
#[serde(deserialize_with = "or_default")]
|
||||
#[specta(type = ThinkingDisplay)]
|
||||
pub thinking: ThinkingDisplay,
|
||||
@@ -100,6 +122,11 @@ pub struct AgentSettings {
|
||||
#[serde(deserialize_with = "or_default")]
|
||||
#[specta(type = WhileRunning)]
|
||||
pub while_running: WhileRunning,
|
||||
/// Whether new tasks start in the agent's mode that approves everything (Claude Code's
|
||||
/// bypass permissions, Codex's full access) or in one that asks. User settings only.
|
||||
#[serde(deserialize_with = "or_default")]
|
||||
#[specta(type = NewTaskApprovals)]
|
||||
pub new_task_approvals: NewTaskApprovals,
|
||||
/// Settings new tasks of an agent start with: agent id → option id → value.
|
||||
#[serde(deserialize_with = "agent_defaults")]
|
||||
#[specta(type = BTreeMap<String, BTreeMap<String, String>>)]
|
||||
@@ -120,11 +147,16 @@ impl Default for AgentSettings {
|
||||
show_cost: false,
|
||||
fold_turns: true,
|
||||
smooth_text: true,
|
||||
fit_window_width: false,
|
||||
playful_status: false,
|
||||
playful_after_seconds: 0,
|
||||
playful_words: BTreeMap::new(),
|
||||
thinking: ThinkingDisplay::Auto,
|
||||
tool_calls: ToolCallDisplay::Grouped,
|
||||
expand_tool_output: false,
|
||||
send_with: SendKey::Enter,
|
||||
while_running: WhileRunning::Queue,
|
||||
new_task_approvals: NewTaskApprovals::Bypass,
|
||||
defaults: BTreeMap::new(),
|
||||
mcp_servers: BTreeMap::new(),
|
||||
}
|
||||
@@ -192,6 +224,23 @@ where
|
||||
Ok(serde_json::from_value(serde_json::Value::deserialize(d)?).unwrap_or_default())
|
||||
}
|
||||
|
||||
/// The built-in themes that were removed (#2981; the One Dark Pro plugin
|
||||
/// maintains their look) and the built-in that stands in for each: read by
|
||||
/// [`theme_id`], and the removal notice looks for them in settings files.
|
||||
pub const REMOVED_THEMES: [(&str, &str); 2] = [("one-light", "light"), ("one-dark", "dark")];
|
||||
|
||||
/// A theme id, with the built-in themes that were removed read as the Islands theme of their kind.
|
||||
fn theme_id<'de, D>(d: D) -> Result<String, D::Error>
|
||||
where
|
||||
D: serde::Deserializer<'de>,
|
||||
{
|
||||
let id = String::deserialize(d)?;
|
||||
Ok(match REMOVED_THEMES.iter().find(|(removed, _)| *removed == id.as_str()) {
|
||||
Some((_, stand_in)) => (*stand_in).to_owned(),
|
||||
None => id,
|
||||
})
|
||||
}
|
||||
|
||||
/// [`AgentSettings::defaults`], keeping only the text values of each agent's table.
|
||||
fn agent_defaults<'de, D>(d: D) -> Result<BTreeMap<String, BTreeMap<String, String>>, D::Error>
|
||||
where
|
||||
@@ -255,12 +304,61 @@ pub enum WhileRunning {
|
||||
Steer,
|
||||
}
|
||||
|
||||
/// The approvals new agent tasks start with.
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize, Type)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub enum NewTaskApprovals {
|
||||
/// The agent's mode that approves everything without asking, when it has one.
|
||||
#[default]
|
||||
Bypass,
|
||||
/// A mode in which the agent asks before edits and commands.
|
||||
Ask,
|
||||
}
|
||||
|
||||
/// Agents › MCP Server: agents use the IDE's built-in tools.
|
||||
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, Type)]
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Type)]
|
||||
#[serde(default, rename_all = "camelCase")]
|
||||
pub struct McpServerSettings {
|
||||
/// Serve the tools on 127.0.0.1 while the IDE runs.
|
||||
/// Serve the tools on [`Self::address`] while the IDE runs.
|
||||
pub enabled: bool,
|
||||
/// Run the tools that change the project or start processes without asking first
|
||||
/// (IDEA's brave mode). Read from the IDE's own settings only, never a project's.
|
||||
pub run_without_confirmation: bool,
|
||||
/// Finished agent tabs (`run_command`) the Run tool window shows; older ones are archived.
|
||||
pub agent_tab_limit: u32,
|
||||
/// Archived agent tabs kept; older ones are closed.
|
||||
pub agent_archive_limit: u32,
|
||||
/// The IP address to listen on: `127.0.0.1` (this computer only, the default), another
|
||||
/// address of this computer, or `0.0.0.0` / `::` for every address.
|
||||
pub address: String,
|
||||
/// Listen on [`Self::port`] in every run, rather than a free port each time, so HTTP clients and
|
||||
/// port forwards keep working after a restart.
|
||||
pub fixed_port: bool,
|
||||
/// The port of [`Self::fixed_port`].
|
||||
pub port: u16,
|
||||
/// Keep the bearer token across runs (in the app data, readable by this user only) rather than
|
||||
/// make a new one each run.
|
||||
pub fixed_token: bool,
|
||||
}
|
||||
|
||||
/// The default of [`McpServerSettings::port`].
|
||||
pub const DEFAULT_MCP_PORT: u16 = 26371;
|
||||
/// The default of [`McpServerSettings::address`]: this computer only.
|
||||
pub const DEFAULT_MCP_ADDRESS: &str = "127.0.0.1";
|
||||
|
||||
impl Default for McpServerSettings {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
enabled: false,
|
||||
run_without_confirmation: false,
|
||||
agent_tab_limit: 1,
|
||||
agent_archive_limit: 999,
|
||||
address: DEFAULT_MCP_ADDRESS.to_owned(),
|
||||
fixed_port: true,
|
||||
port: DEFAULT_MCP_PORT,
|
||||
fixed_token: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Absent profiles retain automatic discovery for existing projects.
|
||||
@@ -396,6 +494,7 @@ impl Default for Settings {
|
||||
debugger: DebuggerSettings::default(),
|
||||
run: RunSettings::default(),
|
||||
terminal: TerminalSettings::default(),
|
||||
external_diff: ExternalDiffSettings::default(),
|
||||
actions_on_save: ActionsOnSaveSettings::default(),
|
||||
external_tools: Vec::new(),
|
||||
tools: BTreeMap::new(),
|
||||
@@ -408,6 +507,10 @@ impl Default for Settings {
|
||||
cmake: CMakeSettings::default(),
|
||||
agents: AgentSettings::default(),
|
||||
mcp_server: McpServerSettings::default(),
|
||||
agent_security: AgentSecuritySettings::default(),
|
||||
scopes: Vec::new(),
|
||||
file_colors: FileColorSettings::default(),
|
||||
database: DatabaseSettings::default(),
|
||||
plugins: BTreeMap::new(),
|
||||
}
|
||||
}
|
||||
@@ -418,6 +521,47 @@ fn default_keymap() -> &'static str {
|
||||
if cfg!(target_os = "macos") { "intellij-macos" } else { "intellij-windows" }
|
||||
}
|
||||
|
||||
/// A named set of files: `file:src//*.ts && !file:*.test.ts` (project-relative path patterns
|
||||
/// joined with `||`, `&&`, `!` and parentheses).
|
||||
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize, Type)]
|
||||
#[serde(default, rename_all = "camelCase")]
|
||||
pub struct NamedScope {
|
||||
pub name: String,
|
||||
pub pattern: String,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Type)]
|
||||
#[serde(default, rename_all = "camelCase")]
|
||||
pub struct FileColorSettings {
|
||||
pub enabled: bool,
|
||||
pub editor_tabs: bool,
|
||||
pub project_view: bool,
|
||||
/// Scope → colour; the first scope that holds a file gives its colour.
|
||||
pub colors: Vec<FileColor>,
|
||||
}
|
||||
|
||||
impl Default for FileColorSettings {
|
||||
fn default() -> Self {
|
||||
let color = |scope: &str, color: &str| FileColor { scope: scope.into(), color: color.into() };
|
||||
Self {
|
||||
enabled: true,
|
||||
editor_tabs: true,
|
||||
project_view: true,
|
||||
colors: vec![color("Non-Project Files", "yellow"), color("Tests", "green")],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize, Type)]
|
||||
#[serde(default, rename_all = "camelCase")]
|
||||
pub struct FileColor {
|
||||
/// A built-in scope (`Tests`, `Production`, `Project Files`, `Non-Project Files`) or a
|
||||
/// `scopes` entry, by name.
|
||||
pub scope: String,
|
||||
/// A palette name (`blue`, `green`, `orange`, `rose`, `violet`, `yellow`, `gray`) or `#rrggbb`.
|
||||
pub color: String,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize, Type)]
|
||||
#[serde(default, rename_all = "camelCase")]
|
||||
pub struct CustomKeymap {
|
||||
@@ -453,6 +597,10 @@ pub struct GeneralSettings {
|
||||
/// Where updates come from: `auto` (the CNB mirror for the Chinese UI, else GitHub) | `github` | `cnb`.
|
||||
pub update_source: String,
|
||||
pub auto_check_updates: bool,
|
||||
/// System Settings › Updates: open the What's New page after the app starts a new version.
|
||||
pub show_whats_new_after_update: bool,
|
||||
/// The version the app last started with, so the first start after an update is recognisable.
|
||||
pub last_run_version: String,
|
||||
}
|
||||
|
||||
impl Default for GeneralSettings {
|
||||
@@ -471,6 +619,8 @@ impl Default for GeneralSettings {
|
||||
project_name: String::new(),
|
||||
update_source: "auto".into(),
|
||||
auto_check_updates: true,
|
||||
show_whats_new_after_update: true,
|
||||
last_run_version: String::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -500,13 +650,20 @@ impl Default for DateFormatSettings {
|
||||
pub struct NotificationSettings {
|
||||
pub balloons: bool,
|
||||
pub sound: bool,
|
||||
/// Mirror balloon notifications to the OS when no OpenIDEA window is focused.
|
||||
#[serde(default = "default_true")]
|
||||
pub system_notifications: bool,
|
||||
/// Group id → display; groups not listed use their default.
|
||||
pub groups: BTreeMap<String, NotificationGroupSettings>,
|
||||
}
|
||||
|
||||
fn default_true() -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
impl Default for NotificationSettings {
|
||||
fn default() -> Self {
|
||||
Self { balloons: true, sound: false, groups: BTreeMap::new() }
|
||||
Self { balloons: true, sound: false, system_notifications: true, groups: BTreeMap::new() }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -545,13 +702,19 @@ pub enum NavigationBarPosition {
|
||||
pub struct AppearanceSettings {
|
||||
/// A built-in theme id (`light`, `dark`, `dark-hc`, …), `system` (legacy
|
||||
/// "Sync with OS"), or a plugin's theme: `plugin:<plugin-id>/<theme-id>`.
|
||||
#[serde(deserialize_with = "theme_id")]
|
||||
#[specta(type = String)]
|
||||
pub theme: String,
|
||||
pub accent: String,
|
||||
pub ui_font_size: f32,
|
||||
pub locale: String,
|
||||
/// Follow the OS light/dark mode with the preferred themes below.
|
||||
pub sync_with_os: bool,
|
||||
#[serde(deserialize_with = "theme_id")]
|
||||
#[specta(type = String)]
|
||||
pub preferred_light_theme: String,
|
||||
#[serde(deserialize_with = "theme_id")]
|
||||
#[specta(type = String)]
|
||||
pub preferred_dark_theme: String,
|
||||
/// A plugin's file icon theme (`<plugin-id>/<theme-id>`); empty = the built-in icons.
|
||||
pub file_icon_theme: String,
|
||||
@@ -574,7 +737,7 @@ pub struct AppearanceSettings {
|
||||
pub project_colors_in_toolbar: bool,
|
||||
pub dnd_with_alt_only: bool,
|
||||
pub menu_icons: bool,
|
||||
/// `hamburger` | `merged`: main menu presentation in the frameless title bar.
|
||||
/// `hamburger` | `merged` | `toolbar` (a separate row): main menu presentation in the frameless title bar.
|
||||
pub main_menu_style: String,
|
||||
/// A lone Alt press focuses or opens the main menu (IDEA `ide.windows.main.menu.focus.on.alt`).
|
||||
pub main_menu_alt: bool,
|
||||
@@ -596,6 +759,28 @@ pub struct AppearanceSettings {
|
||||
pub status_bar_widgets: BTreeMap<String, bool>,
|
||||
/// Customize Main Toolbar; `None` = the default toolbar.
|
||||
pub main_toolbar: Option<MainToolbarSettings>,
|
||||
/// Appearance › Background Image: an image shown through the IDE window.
|
||||
pub background_image: BackgroundImageSettings,
|
||||
}
|
||||
|
||||
/// Appearance › Background Image. An empty `path` shows no image.
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Type)]
|
||||
#[serde(default, rename_all = "camelCase")]
|
||||
pub struct BackgroundImageSettings {
|
||||
pub path: String,
|
||||
/// Percent, 0–100.
|
||||
pub opacity: u8,
|
||||
/// `scale` (cover the window) | `plain` (original size) | `tile`.
|
||||
pub fill: String,
|
||||
/// Where the image is anchored: `center`, `top-left`, `top`, `top-right`, `left`, `right`,
|
||||
/// `bottom-left`, `bottom`, `bottom-right`.
|
||||
pub anchor: String,
|
||||
}
|
||||
|
||||
impl Default for BackgroundImageSettings {
|
||||
fn default() -> Self {
|
||||
Self { path: String::new(), opacity: 15, fill: "scale".into(), anchor: "center".into() }
|
||||
}
|
||||
}
|
||||
|
||||
/// Main toolbar entries per side, in order: title widget ids (`project`,
|
||||
@@ -666,6 +851,7 @@ impl Default for AppearanceSettings {
|
||||
navigation_bar: NavigationBarPosition::StatusBar,
|
||||
status_bar_widgets: BTreeMap::new(),
|
||||
main_toolbar: None,
|
||||
background_image: BackgroundImageSettings::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -793,6 +979,9 @@ pub struct FileSettings {
|
||||
pub file_types: BTreeMap<String, FileTypeAssociation>,
|
||||
/// Override File Type: file (project-relative and `/`-separated, or absolute) → language id.
|
||||
pub file_type_overrides: BTreeMap<String, String>,
|
||||
/// File Types › Files opened in associated applications: file-name patterns
|
||||
/// (`*.xlsx`) opened with the system application instead of an editor.
|
||||
pub associated_applications: Vec<String>,
|
||||
/// Compose detection choices, keyed by project-relative file path.
|
||||
pub compose_file_overrides: BTreeMap<String, ComposeFileOverride>,
|
||||
}
|
||||
@@ -829,6 +1018,15 @@ impl Default for FileSettings {
|
||||
local_history_max_mb: 512,
|
||||
file_types: BTreeMap::new(),
|
||||
file_type_overrides: BTreeMap::new(),
|
||||
associated_applications: vec![
|
||||
"*.xlsx".into(),
|
||||
"*.xls".into(),
|
||||
"*.docx".into(),
|
||||
"*.doc".into(),
|
||||
"*.pptx".into(),
|
||||
"*.ppt".into(),
|
||||
"*.pdf".into(),
|
||||
],
|
||||
compose_file_overrides: BTreeMap::new(),
|
||||
}
|
||||
}
|
||||
@@ -852,6 +1050,7 @@ pub struct LanguagesSettings {
|
||||
pub ruby: RubySettings,
|
||||
pub markdown: MarkdownSettings,
|
||||
pub arduino: ArduinoSettings,
|
||||
pub zig: ZigSettings,
|
||||
}
|
||||
|
||||
impl Default for LanguagesSettings {
|
||||
@@ -874,6 +1073,7 @@ impl Default for LanguagesSettings {
|
||||
ruby: RubySettings::default(),
|
||||
markdown: MarkdownSettings::default(),
|
||||
arduino: ArduinoSettings::default(),
|
||||
zig: ZigSettings::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -974,15 +1174,44 @@ impl Default for ExternalFormatterSettings {
|
||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Type)]
|
||||
#[serde(default, rename_all = "camelCase")]
|
||||
pub struct PythonSettings {
|
||||
/// Project interpreter executable; empty = auto-detect (project venv, else `python3`).
|
||||
/// Project interpreter executable; empty = auto-detect (project venv, else `python3`);
|
||||
/// `target:<run-target-id>` = the Python of a Docker or Compose run target.
|
||||
pub interpreter: String,
|
||||
/// Activate the interpreter's virtualenv in new terminal sessions.
|
||||
pub activate_virtualenv: bool,
|
||||
/// Interpreters the user added to Settings › Python › All Interpreters (executable paths).
|
||||
/// Detection contributes the rest; `hidden_interpreters` removes some of them.
|
||||
pub interpreters: Vec<String>,
|
||||
/// Interpreters the user removed from that list, as executable paths: detection does
|
||||
/// not list them again.
|
||||
pub hidden_interpreters: Vec<String>,
|
||||
/// Hide interpreters that belong to another project (a virtualenv inside its folder).
|
||||
pub hide_other_project_interpreters: bool,
|
||||
/// Show each interpreter's path under its name on the interpreters page.
|
||||
pub show_interpreter_paths: bool,
|
||||
}
|
||||
|
||||
impl Default for PythonSettings {
|
||||
fn default() -> Self {
|
||||
Self { interpreter: String::new(), activate_virtualenv: true }
|
||||
Self {
|
||||
interpreter: String::new(),
|
||||
activate_virtualenv: true,
|
||||
interpreters: Vec::new(),
|
||||
hidden_interpreters: Vec::new(),
|
||||
hide_other_project_interpreters: false,
|
||||
show_interpreter_paths: true,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl PythonSettings {
|
||||
/// What `interpreter` starts with when the project interpreter is a run target's.
|
||||
pub const TARGET_PREFIX: &str = "target:";
|
||||
|
||||
/// The run target whose Python is the project interpreter (`target:<id>`), if one is.
|
||||
pub fn run_target(&self) -> Option<&str> {
|
||||
let id = self.interpreter.trim().strip_prefix(Self::TARGET_PREFIX)?.trim();
|
||||
(!id.is_empty()).then_some(id)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1024,6 +1253,9 @@ pub struct NodeSettings {
|
||||
pub run_target: String,
|
||||
/// `npm` | `pnpm` | `yarn`; empty = detect from the lock file / `packageManager`.
|
||||
pub package_manager: String,
|
||||
/// Runtime for running JavaScript / TypeScript files: `node` | `bun` | `deno`; empty = from the
|
||||
/// project (`deno.json` → Deno, `bun.lock` → Bun, else Node.js).
|
||||
pub runtime: String,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Type)]
|
||||
@@ -1053,6 +1285,13 @@ impl Default for RustSettings {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize, Type)]
|
||||
#[serde(default, rename_all = "camelCase")]
|
||||
pub struct ZigSettings {
|
||||
/// Directory holding the `zig` executable; empty = the one on `PATH`.
|
||||
pub zig: String,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize, Type)]
|
||||
#[serde(default, rename_all = "camelCase")]
|
||||
pub struct GoSettings {
|
||||
@@ -1108,6 +1347,9 @@ pub struct JavaSettings {
|
||||
pub jdtls_vm_args: String,
|
||||
/// Share one JDT LS across builds in an ordinary project window; other windows remain separate.
|
||||
pub jdtls_shared: bool,
|
||||
/// JDK homes added by hand (Platform Settings › SDKs › Add JDK from Disk…), listed with the
|
||||
/// detected ones.
|
||||
pub added_jdks: Vec<String>,
|
||||
/// Build Tools › Maven.
|
||||
pub maven: MavenSettings,
|
||||
/// Build Tools › Gradle.
|
||||
@@ -1158,7 +1400,9 @@ pub struct MavenSettings {
|
||||
pub runner_jre: String,
|
||||
/// Project-relative root POMs removed from the Maven project model.
|
||||
pub unlinked_projects: Vec<String>,
|
||||
/// Project-relative POMs outside the root reactor added as independent top-level projects.
|
||||
/// POMs added as independent top-level projects. Inside the opened project the path is
|
||||
/// project-relative (`tools/pom.xml`); outside it, the canonical absolute path. Linking
|
||||
/// does not edit either POM.
|
||||
pub linked_projects: Vec<String>,
|
||||
/// Maven home (or its `mvn` executable); empty = the project's `mvnw`, else `mvn` on `PATH`.
|
||||
pub home: String,
|
||||
@@ -1179,6 +1423,8 @@ pub struct MavenSettings {
|
||||
pub threads: String,
|
||||
/// Active Maven profiles (`-P`), comma-separated; empty leaves profiles to the POM/settings.
|
||||
pub active_profiles: String,
|
||||
/// Delegate IDE build/run actions to Maven: launches build with Maven (`javac`) instead of jdtls (ECJ).
|
||||
pub delegate_build: bool,
|
||||
}
|
||||
|
||||
impl MavenSettings {
|
||||
@@ -1216,11 +1462,15 @@ pub struct TypeScriptSettings {
|
||||
/// `bundled` (the server's own TypeScript), `workspace` (`node_modules/typescript`),
|
||||
/// or a path to a TypeScript `lib` directory.
|
||||
pub version: String,
|
||||
/// tsserver heap limit in MB. `0` = automatic: 8192 when vtsls hosts the Vue
|
||||
/// plugin (hybrid mode; a Vue project's program alone needs ~3 GiB, and the
|
||||
/// vtsls default 3072 dies in a GC death spiral), else unset (vtsls default).
|
||||
pub tsserver_max_memory: u32,
|
||||
}
|
||||
|
||||
impl Default for TypeScriptSettings {
|
||||
fn default() -> Self {
|
||||
Self { version: "bundled".into() }
|
||||
Self { version: "bundled".into(), tsserver_max_memory: 0 }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1396,6 +1646,29 @@ impl Settings {
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn python_interpreter_may_name_a_run_target() {
|
||||
let python = |interpreter: &str| PythonSettings { interpreter: interpreter.into(), ..Default::default() };
|
||||
assert_eq!(python("target:web").run_target(), Some("web"));
|
||||
assert_eq!(python(" target: web ").run_target(), Some("web"));
|
||||
for local in ["", "target:", "/usr/bin/python3", ".venv/bin/python", "targets/python"] {
|
||||
assert_eq!(python(local).run_target(), None, "{local}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn removed_one_themes_read_as_islands_themes() {
|
||||
let settings: Settings = serde_json::from_str(
|
||||
r#"{"appearance":{"theme":"one-dark","preferredLightTheme":"one-light","preferredDarkTheme":"one-dark"}}"#,
|
||||
)
|
||||
.unwrap();
|
||||
let a = settings.appearance;
|
||||
assert_eq!((a.theme.as_str(), a.preferred_light_theme.as_str()), ("dark", "light"));
|
||||
assert_eq!(a.preferred_dark_theme, "dark");
|
||||
let kept: Settings = serde_json::from_str(r#"{"appearance":{"theme":"solarized-dark"}}"#).unwrap();
|
||||
assert_eq!(kept.appearance.theme, "solarized-dark");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn window_close_settings_default_off_and_round_trip() {
|
||||
for old in [r#"{}"#, r#"{"general":{"confirmExit":false}}"#] {
|
||||
@@ -1534,11 +1807,15 @@ mod tests {
|
||||
let old: Settings = serde_json::from_str(r#"{"agents":{"showCost":true}}"#).unwrap();
|
||||
assert!(old.agents.show_cost);
|
||||
assert!(old.agents.show_context_usage && old.agents.show_work_time && old.agents.show_usage_limits);
|
||||
assert!(!old.agents.fit_window_width);
|
||||
assert!(!Settings::default().agents.show_cost);
|
||||
assert!(!Settings::default().agents.fit_window_width);
|
||||
let mut settings = Settings::default();
|
||||
settings.agents.show_usage_limits = false;
|
||||
settings.agents.fit_window_width = true;
|
||||
let json = serde_json::to_value(&settings).unwrap();
|
||||
assert_eq!(json["agents"]["showUsageLimits"], false);
|
||||
assert_eq!(json["agents"]["fitWindowWidth"], true);
|
||||
let restored: Settings = serde_json::from_value(json).unwrap();
|
||||
assert_eq!(restored.agents, settings.agents);
|
||||
}
|
||||
@@ -1683,10 +1960,12 @@ mod maven_linkage_tests {
|
||||
fn maven_runner_jre_defaults_and_round_trips() {
|
||||
let old: MavenSettings = serde_json::from_str(r#"{"home":"/opt/maven"}"#).unwrap();
|
||||
assert!(old.runner_jre.is_empty());
|
||||
assert!(!old.delegate_build);
|
||||
assert!(MavenSettings::default().runner_jre.is_empty());
|
||||
let settings = MavenSettings { runner_jre: "/opt/jdk-21".into(), ..old };
|
||||
let settings = MavenSettings { runner_jre: "/opt/jdk-21".into(), delegate_build: true, ..old };
|
||||
let encoded = serde_json::to_string(&settings).unwrap();
|
||||
assert!(encoded.contains(r#""runnerJre":"/opt/jdk-21""#));
|
||||
assert!(encoded.contains(r#""delegateBuild":true"#));
|
||||
assert_eq!(serde_json::from_str::<MavenSettings>(&encoded).unwrap(), settings);
|
||||
}
|
||||
|
||||
|
||||
@@ -138,8 +138,11 @@ impl WorkspaceEdit {
|
||||
continue;
|
||||
}
|
||||
*expected = expected.or(version);
|
||||
// Repeats within one entry are intended (LSP inserts at one position
|
||||
// apply in order); only an alias entry repeats what is already merged.
|
||||
let merged = own.len();
|
||||
for edit in edits {
|
||||
if !own.contains(&edit) {
|
||||
if !own[..merged].contains(&edit) {
|
||||
own.push(edit);
|
||||
}
|
||||
}
|
||||
@@ -277,10 +280,40 @@ pub struct OutlineItem {
|
||||
/// Fuzzy-match highlight: indices (in chars of the displayed string) that matched.
|
||||
pub type MatchIndices = Vec<u32>;
|
||||
|
||||
#[cfg(all(test, unix))]
|
||||
#[cfg(test)]
|
||||
mod workspace_edit_tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn repeated_edits_within_one_entry_survive_but_repeats_across_entries_do_not() {
|
||||
let path = PathBuf::from("missing-dir/main.ts");
|
||||
let at = |line, text: &str| TextEdit {
|
||||
range: Range::new(Pos::new(line, 0), Pos::new(line, 0)),
|
||||
new_text: text.into(),
|
||||
};
|
||||
let entry = |edits: Vec<TextEdit>| FileEdit::Edit { path: path.clone().into(), edits, version: None };
|
||||
let (nl, brace, other) = (at(0, "\n"), at(1, "}"), at(2, "x"));
|
||||
for (entries, expected) in [
|
||||
(vec![vec![nl.clone(), nl.clone()]], vec![nl.clone(), nl.clone()]),
|
||||
(vec![vec![brace.clone(), nl.clone(), brace.clone()]], vec![brace.clone(), nl.clone(), brace.clone()]),
|
||||
(vec![vec![nl.clone(), nl.clone()], vec![nl.clone(), nl.clone()]], vec![nl.clone(), nl.clone()]),
|
||||
(vec![vec![nl.clone()], vec![nl.clone(), other.clone()]], vec![nl.clone(), other.clone()]),
|
||||
(
|
||||
vec![vec![nl.clone()], vec![other.clone(), other.clone()]],
|
||||
vec![nl.clone(), other.clone(), other.clone()],
|
||||
),
|
||||
] {
|
||||
let mut workspace =
|
||||
WorkspaceEdit { files: entries.clone().into_iter().map(entry).collect(), ..Default::default() };
|
||||
workspace.deduplicate();
|
||||
assert_eq!(workspace.files, [entry(expected.clone())], "{entries:?}");
|
||||
let once = workspace.clone();
|
||||
workspace.deduplicate();
|
||||
assert_eq!(workspace, once, "{entries:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn aliases_are_one_edit_but_resource_operations_preserve_order() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
|
||||
@@ -61,16 +61,13 @@ pub fn find_command(command: &str, search: &OsStr, root: &Path) -> Option<PathBu
|
||||
|
||||
fn is_executable(path: &Path) -> bool {
|
||||
let Ok(metadata) = path.metadata() else { return false };
|
||||
if !metadata.is_file() {
|
||||
return false;
|
||||
}
|
||||
#[cfg(unix)]
|
||||
{
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
metadata.permissions().mode() & 0o111 != 0
|
||||
metadata.is_file() && metadata.permissions().mode() & 0o111 != 0
|
||||
}
|
||||
#[cfg(not(unix))]
|
||||
true
|
||||
metadata.is_file()
|
||||
}
|
||||
|
||||
/// [`search_path`]'s directories, in order and once each. Only absolute ones: a
|
||||
|
||||
@@ -19,6 +19,7 @@ pub enum Tool {
|
||||
Maven,
|
||||
Gradle,
|
||||
Ruby,
|
||||
Zig,
|
||||
}
|
||||
|
||||
impl Tool {
|
||||
@@ -32,6 +33,7 @@ impl Tool {
|
||||
Tool::Maven => &["maven"],
|
||||
Tool::Gradle => &["gradle"],
|
||||
Tool::Ruby => &["ruby"],
|
||||
Tool::Zig => &["zig"],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -69,6 +69,9 @@ pub struct GitSettings {
|
||||
pub push_dialog_only_protected: bool,
|
||||
/// Stashes and shelves share one tab.
|
||||
pub combine_stash_and_shelf: bool,
|
||||
/// The non-modal Commit tool window works on the index: Staged / Unstaged groups,
|
||||
/// stage and unstage instead of inclusion checkboxes, and no change lists.
|
||||
pub staging_area: bool,
|
||||
}
|
||||
|
||||
impl Default for GitSettings {
|
||||
@@ -90,6 +93,7 @@ impl Default for GitSettings {
|
||||
show_push_dialog: true,
|
||||
push_dialog_only_protected: false,
|
||||
combine_stash_and_shelf: false,
|
||||
staging_area: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -175,6 +179,24 @@ impl Default for VcsConfirmationSettings {
|
||||
}
|
||||
}
|
||||
|
||||
/// Tools › Diff & Merge › External Diff Tools: a program that shows two files' differences.
|
||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Type)]
|
||||
#[serde(default, rename_all = "camelCase")]
|
||||
pub struct ExternalDiffSettings {
|
||||
/// Offer Show Diff in External Tool in diff viewers.
|
||||
pub enabled: bool,
|
||||
/// The program to start.
|
||||
pub path: String,
|
||||
/// Its arguments: `%1` the left file, `%2` the right file, `%3` the diff's title.
|
||||
pub parameters: String,
|
||||
}
|
||||
|
||||
impl Default for ExternalDiffSettings {
|
||||
fn default() -> Self {
|
||||
Self { enabled: false, path: String::new(), parameters: "%1 %2 %3".into() }
|
||||
}
|
||||
}
|
||||
|
||||
/// Version Control › Shelf.
|
||||
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize, Type)]
|
||||
#[serde(default, rename_all = "camelCase")]
|
||||
@@ -192,6 +214,57 @@ pub struct IssueLink {
|
||||
pub link: String,
|
||||
}
|
||||
|
||||
/// Per-level colours for console log lines: a line whose level is detected
|
||||
/// (`ERROR`, `WARN`, …) takes the level's colour; ANSI colours the program
|
||||
/// prints itself win over it. Values are CSS colours, usually scheme tokens.
|
||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Type)]
|
||||
#[serde(default, rename_all = "camelCase")]
|
||||
pub struct LogLevelColors {
|
||||
pub enabled: bool,
|
||||
pub error: String,
|
||||
pub warn: String,
|
||||
pub info: String,
|
||||
pub debug: String,
|
||||
pub trace: String,
|
||||
}
|
||||
|
||||
impl Default for LogLevelColors {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
enabled: false,
|
||||
error: "var(--ansi-red)".into(),
|
||||
warn: "var(--ansi-yellow)".into(),
|
||||
info: "var(--ansi-green)".into(),
|
||||
debug: "var(--ansi-cyan)".into(),
|
||||
trace: "var(--ansi-magenta)".into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Database settings: Query Execution and SQL Resolution Scopes.
|
||||
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize, Type)]
|
||||
#[serde(default, rename_all = "camelCase")]
|
||||
pub struct DatabaseSettings {
|
||||
/// A console's result sets show under the statements that produced them, in the editor,
|
||||
/// instead of in the Services view's result tabs.
|
||||
pub in_editor_results: bool,
|
||||
/// Languages & Frameworks › SQL Resolution Scopes: the data source SQL in a file or folder
|
||||
/// resolves against (completion, inspections, navigation), most specific path first.
|
||||
pub resolution_scopes: Vec<SqlResolutionScope>,
|
||||
}
|
||||
|
||||
/// One row of SQL Resolution Scopes.
|
||||
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize, Type)]
|
||||
#[serde(default, rename_all = "camelCase")]
|
||||
pub struct SqlResolutionScope {
|
||||
/// Project-relative file or folder, `/`-separated; empty for the whole project.
|
||||
pub path: String,
|
||||
/// Data source id.
|
||||
pub data_source: String,
|
||||
/// Schema unqualified names resolve in; empty for the data source's default.
|
||||
pub schema: String,
|
||||
}
|
||||
|
||||
/// Build, Execution, Deployment › Console.
|
||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Type)]
|
||||
#[serde(default, rename_all = "camelCase")]
|
||||
@@ -203,6 +276,8 @@ pub struct ConsoleSettings {
|
||||
pub encoding: String,
|
||||
/// Consecutive lines containing any of these are folded.
|
||||
pub fold_patterns: Vec<String>,
|
||||
/// Log lines take the colour of their level (Run/Debug consoles).
|
||||
pub log_colors: LogLevelColors,
|
||||
}
|
||||
|
||||
impl Default for ConsoleSettings {
|
||||
@@ -219,6 +294,7 @@ impl Default for ConsoleSettings {
|
||||
"at node:internal/".into(),
|
||||
"<frozen importlib._bootstrap".into(),
|
||||
],
|
||||
log_colors: LogLevelColors::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -230,18 +306,24 @@ pub struct DebuggerSettings {
|
||||
pub show_on_breakpoint: bool,
|
||||
pub focus_app_on_breakpoint: bool,
|
||||
pub hide_on_termination: bool,
|
||||
/// A breakpoint or exception hit brings the Debug tool window's Threads & Variables tab to the front.
|
||||
pub variables_on_breakpoint: bool,
|
||||
pub breakpoints_any_file: bool,
|
||||
/// While suspended, hovering the start of a line shows a Run to Cursor button.
|
||||
pub run_to_cursor_inlay: bool,
|
||||
pub inline_values: bool,
|
||||
/// Data Views: list variables by name instead of the adapter's order.
|
||||
pub sort_values: bool,
|
||||
/// Data Views: evaluate watches while the program runs, where the debugger can (JavaScript).
|
||||
pub live_watches: bool,
|
||||
pub value_tooltip: bool,
|
||||
/// While suspended, selecting an expression shows its value.
|
||||
pub value_tooltip_on_selection: bool,
|
||||
pub value_tooltip_delay: u32,
|
||||
/// Stepping: do not step into library code (debugpy `justMyCode`, js-debug `skipFiles`).
|
||||
pub skip_library_code: bool,
|
||||
/// JavaScript: debugging a dev server (`vite`, `next dev`) also opens its page in a browser session.
|
||||
pub debug_server_page: bool,
|
||||
pub python: PythonDebuggerSettings,
|
||||
}
|
||||
|
||||
@@ -251,14 +333,17 @@ impl Default for DebuggerSettings {
|
||||
show_on_breakpoint: true,
|
||||
focus_app_on_breakpoint: false,
|
||||
hide_on_termination: false,
|
||||
variables_on_breakpoint: true,
|
||||
breakpoints_any_file: false,
|
||||
run_to_cursor_inlay: true,
|
||||
inline_values: true,
|
||||
sort_values: false,
|
||||
live_watches: false,
|
||||
value_tooltip: true,
|
||||
value_tooltip_on_selection: true,
|
||||
value_tooltip_delay: 700,
|
||||
skip_library_code: true,
|
||||
debug_server_page: false,
|
||||
python: PythonDebuggerSettings::default(),
|
||||
}
|
||||
}
|
||||
@@ -288,6 +373,8 @@ pub struct RunSettings {
|
||||
/// Ask before closing a Run tab whose process is still running.
|
||||
pub confirm_terminate: bool,
|
||||
pub show_on_start: bool,
|
||||
/// New Run/Debug tabs start pinned: a later run doesn't take their place once finished.
|
||||
pub pin_tabs: bool,
|
||||
/// Grace period between interrupt and kill when stopping a process.
|
||||
pub termination_timeout_ms: u32,
|
||||
}
|
||||
@@ -299,6 +386,7 @@ impl Default for RunSettings {
|
||||
confirm_rerun: true,
|
||||
confirm_terminate: true,
|
||||
show_on_start: true,
|
||||
pin_tabs: false,
|
||||
termination_timeout_ms: 3000,
|
||||
}
|
||||
}
|
||||
@@ -412,3 +500,30 @@ impl Default for ExternalTool {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Tools › Agents: Command Execution Policy (whitelist and blacklist patterns).
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Type)]
|
||||
#[serde(default, rename_all = "camelCase")]
|
||||
pub struct AgentSecuritySettings {
|
||||
/// Commands or patterns (`git status*`, `pwd`) automatically approved without prompting.
|
||||
pub allowed_commands: Vec<String>,
|
||||
/// Commands or patterns (`rm -rf *`, `mkfs*`) automatically rejected.
|
||||
pub blocked_commands: Vec<String>,
|
||||
}
|
||||
|
||||
impl Default for AgentSecuritySettings {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
allowed_commands: Vec::new(),
|
||||
blocked_commands: vec![
|
||||
"rm -rf *".into(),
|
||||
"rm -rf /".into(),
|
||||
"mkfs*".into(),
|
||||
"dd *".into(),
|
||||
":(){ :|:& };:".into(),
|
||||
"shutdown*".into(),
|
||||
"reboot*".into(),
|
||||
],
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,22 +7,32 @@ license.workspace = true
|
||||
|
||||
[dependencies]
|
||||
futures.workspace = true
|
||||
tokio-tungstenite.workspace = true
|
||||
base64.workspace = true
|
||||
tempfile.workspace = true
|
||||
ide-core.workspace = true
|
||||
ide-language.workspace = true
|
||||
ide-jsonrpc.workspace = true
|
||||
ide-jvm-attach.workspace = true
|
||||
ide-jvm-inspect.workspace = true
|
||||
ide-jdwp.workspace = true
|
||||
ide-heap.workspace = true
|
||||
serde.workspace = true
|
||||
serde_json.workspace = true
|
||||
specta.workspace = true
|
||||
tokio = { workspace = true, features = ["net"] }
|
||||
parking_lot.workspace = true
|
||||
memchr = "2"
|
||||
percent-encoding = "2"
|
||||
portable-pty.workspace = true
|
||||
tracing.workspace = true
|
||||
|
||||
[target.'cfg(unix)'.dependencies]
|
||||
libc = "0.2"
|
||||
|
||||
[target.'cfg(windows)'.dependencies]
|
||||
windows-sys = { version = "0.61", features = ["Wdk_System_Threading", "Win32_Foundation", "Win32_System_Diagnostics_ToolHelp", "Win32_System_Threading"] }
|
||||
|
||||
[dev-dependencies]
|
||||
tracing-subscriber.workspace = true
|
||||
tokio = { workspace = true, features = ["net", "rt-multi-thread", "macros", "test-util"] }
|
||||
@@ -9,6 +9,7 @@
|
||||
use ide_core::process::executable_names;
|
||||
use ide_core::{IdeError, IdeResult};
|
||||
use ide_language::{DebuggerConfig, LanguageRegistry, ProgramPin};
|
||||
use serde_json::Value;
|
||||
use std::future::Future;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::pin::Pin;
|
||||
@@ -72,6 +73,13 @@ fn builtin(adapter: &str) -> Option<DebuggerConfig> {
|
||||
"stdio",
|
||||
"Install Kotlin Debug Adapter in Settings › Languages & Frameworks › Language Servers",
|
||||
),
|
||||
// Inside the language server: reached through `debugServer`, never started.
|
||||
"kotlin-lsp" => cfg(
|
||||
"kotlin-lsp",
|
||||
&[],
|
||||
"tcp",
|
||||
"Install JetBrains Kotlin LSP in Settings › Languages & Frameworks › Language Servers",
|
||||
),
|
||||
// Windows installs `python.exe`; `python3.exe` there is at most a Store alias.
|
||||
"debugpy" => cfg(
|
||||
if cfg!(windows) { "python" } else { "python3" },
|
||||
@@ -98,9 +106,12 @@ fn builtin(adapter: &str) -> Option<DebuggerConfig> {
|
||||
"stdio",
|
||||
"Install netcoredbg (.NET debugger) in Settings › Languages & Frameworks › Language Servers",
|
||||
),
|
||||
"lldb-dap" => {
|
||||
cfg("lldb-dap", &[], "stdio", "Install LLVM's lldb-dap (brew install llvm; on macOS `xcrun -f lldb-dap`)")
|
||||
}
|
||||
"lldb-dap" => cfg(
|
||||
"lldb-dap",
|
||||
&[],
|
||||
"stdio",
|
||||
"Install LLVM's lldb-dap (brew install llvm), or on macOS use the one found by xcrun -f lldb-dap",
|
||||
),
|
||||
// GDB's own DAP interpreter, stdio transport. The DAP support it needs shipped in GDB 14
|
||||
// (`prepare_gdb` refuses older ones with the required version in the message).
|
||||
"gdb" => cfg(
|
||||
@@ -109,6 +120,12 @@ fn builtin(adapter: &str) -> Option<DebuggerConfig> {
|
||||
"stdio",
|
||||
"Install GDB 14 or newer (apt install gdb, dnf install gdb, brew install gdb)",
|
||||
),
|
||||
"unity" => cfg(
|
||||
"unity-debug",
|
||||
&[],
|
||||
"stdio",
|
||||
"Install Unity Debugger in Settings > Language Servers, or configure the unity-debug tool path",
|
||||
),
|
||||
"rdbg" => cfg("rdbg", &["-O", "--port", PORT_PLACEHOLDER, "-c"], "tcp", "gem install debug"),
|
||||
// A jdtls bundle: the session is started through jdtls and reached via `debugServer`.
|
||||
"java-debug" => cfg(
|
||||
@@ -117,6 +134,12 @@ fn builtin(adapter: &str) -> Option<DebuggerConfig> {
|
||||
"tcp",
|
||||
"Install Microsoft java-debug (Settings › Languages & Frameworks › Language Servers)",
|
||||
),
|
||||
"bun-dap" => cfg(
|
||||
"bun-dap",
|
||||
&["${port}", "127.0.0.1"],
|
||||
"tcp",
|
||||
"To install bun-dap: git clone https://github.com/morf-bls/bun-dap, run bun install && bun run build, then place dist/bun-dap in your PATH",
|
||||
),
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
@@ -196,6 +219,50 @@ pub fn tools_root() -> Option<PathBuf> {
|
||||
TOOLS_ROOT.read().clone()
|
||||
}
|
||||
|
||||
/// The browser js-debug launches for `ty`, or `None` when the launch starts no browser.
|
||||
fn launched_browser(ty: &str) -> Option<&'static str> {
|
||||
match ty {
|
||||
"pwa-chrome" | "chrome" => Some("chrome"),
|
||||
"pwa-msedge" | "msedge" => Some("msedge"),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Gives a browser launch that names no `userDataDir` a profile of `project`'s own in the
|
||||
/// IDE's local data (`cache/browser-profiles`), so sign-ins and site data are still there in
|
||||
/// the next session. js-debug's own default is one temporary profile for every project.
|
||||
pub(crate) fn set_browser_profile(arguments: &mut Value, project: &Path) {
|
||||
if let Some(local_data) = tools_root().and_then(|tools| tools.parent().map(Path::to_owned)) {
|
||||
set_browser_profile_in(arguments, project, &local_data);
|
||||
}
|
||||
}
|
||||
|
||||
fn set_browser_profile_in(arguments: &mut Value, project: &Path, local_data: &Path) {
|
||||
let Some(obj) = arguments.as_object_mut() else { return };
|
||||
let browser = obj.get("type").and_then(Value::as_str).and_then(launched_browser);
|
||||
let launch = obj.get("request").and_then(Value::as_str) == Some("launch");
|
||||
let (Some(browser), true, false) = (browser, launch, obj.contains_key("userDataDir")) else { return };
|
||||
let dir = local_data.join("cache").join("browser-profiles").join(profile_name(project)).join(browser);
|
||||
obj.insert("userDataDir".into(), dir.to_string_lossy().into_owned().into());
|
||||
}
|
||||
|
||||
/// `<folder name>-<hash of the path>`: readable, and distinct for projects whose folders share a name.
|
||||
fn profile_name(project: &Path) -> String {
|
||||
// FNV-1a: the name has to stay the same across toolchains, which `DefaultHasher` doesn't promise.
|
||||
let hash = project
|
||||
.to_string_lossy()
|
||||
.bytes()
|
||||
.fold(0xcbf2_9ce4_8422_2325_u64, |h, b| (h ^ u64::from(b)).wrapping_mul(0x0000_0100_0000_01b3));
|
||||
let name: String = project
|
||||
.file_name()
|
||||
.map(|n| n.to_string_lossy().into_owned())
|
||||
.unwrap_or_default()
|
||||
.chars()
|
||||
.map(|c| if c.is_ascii_alphanumeric() || c == '-' || c == '_' || c == '.' { c } else { '_' })
|
||||
.collect();
|
||||
format!("{name}-{hash:016x}")
|
||||
}
|
||||
|
||||
/// Executable directories of managed installs, read at lookup time so tools installed
|
||||
/// after startup are found without a restart: `<tool>/bin`, `<tool>/venv/bin`, npm's `.bin`.
|
||||
fn managed_dirs(root: &Path) -> Vec<PathBuf> {
|
||||
@@ -1024,6 +1091,33 @@ mod tests {
|
||||
assert!(err.message.contains("runs inside the Java language server"), "{}", err.message);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_browser_launch_gets_a_profile_of_its_project() {
|
||||
let local = Path::new("/data/local");
|
||||
let profile = |args: serde_json::Value, project: &str| {
|
||||
let mut args = args;
|
||||
set_browser_profile_in(&mut args, Path::new(project), local);
|
||||
args.get("userDataDir").cloned()
|
||||
};
|
||||
let chrome = serde_json::json!({ "type": "pwa-chrome", "request": "launch" });
|
||||
let dir = profile(chrome.clone(), "/work/shop").unwrap();
|
||||
let dir = Path::new(dir.as_str().unwrap());
|
||||
assert!(dir.starts_with("/data/local/cache/browser-profiles"), "{dir:?}");
|
||||
assert!(dir.ends_with("chrome"), "{dir:?}");
|
||||
let name = dir.parent().unwrap().file_name().unwrap().to_string_lossy().into_owned();
|
||||
assert!(name.starts_with("shop-") && name.len() == "shop-".len() + 16, "{name}");
|
||||
// The same project again: the same profile. Another folder of that name, or Edge: another one.
|
||||
assert_eq!(profile(chrome.clone(), "/work/shop").unwrap(), dir.to_string_lossy().as_ref());
|
||||
assert_ne!(profile(chrome.clone(), "/other/shop").unwrap(), dir.to_string_lossy().as_ref());
|
||||
let edge = profile(serde_json::json!({ "type": "pwa-msedge", "request": "launch" }), "/work/shop").unwrap();
|
||||
assert!(Path::new(edge.as_str().unwrap()).ends_with("msedge"), "{edge}");
|
||||
// A chosen profile (or the default one, `false`) is kept; Node and attach requests get none.
|
||||
let own = serde_json::json!({ "type": "pwa-chrome", "request": "launch", "userDataDir": false });
|
||||
assert_eq!(profile(own, "/work/shop"), Some(false.into()));
|
||||
assert_eq!(profile(serde_json::json!({ "type": "pwa-node", "request": "launch" }), "/work/shop"), None);
|
||||
assert_eq!(profile(serde_json::json!({ "type": "pwa-chrome", "request": "attach" }), "/work/shop"), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn port_conflicts_are_recognised_for_retry() {
|
||||
let in_use = IdeError::unavailable(
|
||||
|
||||
+236
-12
@@ -1,6 +1,7 @@
|
||||
//! Attach to Process: the current user's processes, grouped by the debugger that can
|
||||
//! attach to them (IDEA's `XAttachDebuggerProvider`s): JVMs with a JDWP agent, Node,
|
||||
//! Python (debugpy injection), Go binaries (Delve) and anything else natively (LLDB).
|
||||
//! Python (debugpy injection), .NET (netcoredbg), Go binaries (Delve) and anything else
|
||||
//! natively (LLDB).
|
||||
|
||||
use crate::adapter;
|
||||
use ide_core::{IdeError, IdeResult};
|
||||
@@ -11,9 +12,11 @@ use std::path::Path;
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, Type)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub enum AttachKind {
|
||||
Unity,
|
||||
Java,
|
||||
Node,
|
||||
Python,
|
||||
Dotnet,
|
||||
Go,
|
||||
Native,
|
||||
}
|
||||
@@ -95,13 +98,29 @@ fn file_name(path: &str) -> &str {
|
||||
path.rsplit(['/', '\\']).next().unwrap_or(path)
|
||||
}
|
||||
|
||||
/// A .NET app: the `dotnet` host, or an apphost next to its `<name>.runtimeconfig.json`.
|
||||
pub fn is_dotnet(exe: &str) -> bool {
|
||||
let path = Path::new(exe);
|
||||
let Some(stem) = path.file_stem().and_then(|s| s.to_str()) else { return false };
|
||||
if stem == "dotnet" {
|
||||
return true;
|
||||
}
|
||||
path.is_absolute() && path.with_file_name(format!("{stem}.runtimeconfig.json")).is_file()
|
||||
}
|
||||
|
||||
fn is_python(name: &str) -> bool {
|
||||
let n = name.strip_suffix(".exe").unwrap_or(name);
|
||||
n == "Python" || n.strip_prefix("python").is_some_and(|v| v.chars().all(|c| c.is_ascii_digit() || c == '.'))
|
||||
}
|
||||
|
||||
/// Classifies a process; `is_go` is asked only for processes no interpreter claims.
|
||||
pub fn classify(p: &RawProcess, is_go: impl Fn(&str) -> bool, native_adapter: &str) -> Option<AttachableProcess> {
|
||||
/// Classifies a process; `is_dotnet` and `is_go` are asked only for processes no
|
||||
/// interpreter claims.
|
||||
pub fn classify(
|
||||
p: &RawProcess,
|
||||
is_dotnet: impl Fn(&str) -> bool,
|
||||
is_go: impl Fn(&str) -> bool,
|
||||
native_adapter: &str,
|
||||
) -> Option<AttachableProcess> {
|
||||
let name = file_name(&p.exe).to_owned();
|
||||
let make = |kind, adapter: &str, addr: Option<(Option<String>, u16)>| {
|
||||
let (host, port) = match addr {
|
||||
@@ -129,6 +148,9 @@ pub fn classify(p: &RawProcess, is_go: impl Fn(&str) -> bool, native_adapter: &s
|
||||
if is_python(&name) {
|
||||
return make(AttachKind::Python, "debugpy", debugpy_listen(&p.args));
|
||||
}
|
||||
if is_dotnet(&p.exe) {
|
||||
return make(AttachKind::Dotnet, "netcoredbg", None);
|
||||
}
|
||||
if is_go(&p.exe) {
|
||||
return make(AttachKind::Go, "delve", None);
|
||||
}
|
||||
@@ -143,7 +165,7 @@ const GO_SCAN_LIMIT: u64 = 96 << 20;
|
||||
pub fn is_go_binary(exe: &str) -> bool {
|
||||
use std::io::Read;
|
||||
let skip = ["/System/", "/usr/", "/bin/", "/sbin/", "/Library/Apple/", "/Applications/"];
|
||||
if skip.iter().any(|p| exe.starts_with(p)) || !Path::new(exe).is_absolute() {
|
||||
if skip.iter().any(|p| exe.starts_with(p)) || is_windows_system(exe) || !Path::new(exe).is_absolute() {
|
||||
return false;
|
||||
}
|
||||
let Ok(file) = std::fs::File::open(exe) else { return false };
|
||||
@@ -165,6 +187,17 @@ pub fn is_go_binary(exe: &str) -> bool {
|
||||
}
|
||||
}
|
||||
|
||||
/// Windows' own and installed programs (`C:\Windows\…`, `C:\Program Files…`), like the
|
||||
/// system locations skipped on Unix.
|
||||
fn is_windows_system(exe: &str) -> bool {
|
||||
let lower = exe.to_ascii_lowercase();
|
||||
lower.get(1..).is_some_and(|rest| {
|
||||
rest.starts_with(":\\windows\\")
|
||||
|| rest.starts_with(":\\program files\\")
|
||||
|| rest.starts_with(":\\program files (x86)\\")
|
||||
})
|
||||
}
|
||||
|
||||
/// The current user's processes (without this one).
|
||||
#[cfg(unix)]
|
||||
pub fn list_processes() -> IdeResult<Vec<RawProcess>> {
|
||||
@@ -181,7 +214,7 @@ pub fn list_processes() -> IdeResult<Vec<RawProcess>> {
|
||||
let (pid, rest) = line.split_once(char::is_whitespace)?;
|
||||
Some((pid.parse().ok()?, rest.trim().to_owned()))
|
||||
};
|
||||
// `comm` is the executable path on macOS; on Linux it is truncated, so read /proc.
|
||||
// `comm` is the executable path as started on macOS; on Linux it is truncated, so read /proc.
|
||||
let exes: HashMap<u32, String> =
|
||||
if cfg!(target_os = "linux") { HashMap::new() } else { ps("pid=,comm=")?.lines().filter_map(split).collect() };
|
||||
let me = std::process::id();
|
||||
@@ -190,9 +223,8 @@ pub fn list_processes() -> IdeResult<Vec<RawProcess>> {
|
||||
.filter_map(split)
|
||||
.filter(|(pid, _)| *pid != me)
|
||||
.map(|(pid, args)| {
|
||||
let exe = exes
|
||||
.get(&pid)
|
||||
.cloned()
|
||||
let exe = exe_path(pid)
|
||||
.or_else(|| exes.get(&pid).cloned())
|
||||
.or_else(|| std::fs::read_link(format!("/proc/{pid}/exe")).ok().map(|p| p.display().to_string()))
|
||||
.unwrap_or_else(|| args.split_whitespace().next().unwrap_or_default().to_owned());
|
||||
RawProcess { pid, exe, args }
|
||||
@@ -200,9 +232,134 @@ pub fn list_processes() -> IdeResult<Vec<RawProcess>> {
|
||||
.collect())
|
||||
}
|
||||
|
||||
#[cfg(not(unix))]
|
||||
/// The processes this user may open (without this one): system and other users'
|
||||
/// processes refuse `PROCESS_QUERY_LIMITED_INFORMATION` and are left out.
|
||||
#[cfg(windows)]
|
||||
pub fn list_processes() -> IdeResult<Vec<RawProcess>> {
|
||||
Err(IdeError::unavailable("Attach to Process is not supported on this platform yet"))
|
||||
windows_processes::list()
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
mod windows_processes {
|
||||
use super::RawProcess;
|
||||
use ide_core::{IdeError, IdeResult};
|
||||
use std::os::windows::io::{AsRawHandle, FromRawHandle, OwnedHandle};
|
||||
use windows_sys::Wdk::System::Threading::{NtQueryInformationProcess, ProcessCommandLineInformation};
|
||||
use windows_sys::Win32::Foundation::{INVALID_HANDLE_VALUE, UNICODE_STRING};
|
||||
use windows_sys::Win32::System::Diagnostics::ToolHelp::{
|
||||
CreateToolhelp32Snapshot, PROCESSENTRY32W, Process32FirstW, Process32NextW, TH32CS_SNAPPROCESS,
|
||||
};
|
||||
use windows_sys::Win32::System::Threading::{
|
||||
OpenProcess, PROCESS_NAME_WIN32, PROCESS_QUERY_LIMITED_INFORMATION, QueryFullProcessImageNameW,
|
||||
};
|
||||
|
||||
pub fn list() -> IdeResult<Vec<RawProcess>> {
|
||||
let me = std::process::id();
|
||||
Ok(pids()?
|
||||
.into_iter()
|
||||
.filter(|&pid| pid != 0 && pid != me)
|
||||
.filter_map(|pid| {
|
||||
// SAFETY: plain call; a non-null result is a process handle owned here.
|
||||
let handle = unsafe { OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, 0, pid) };
|
||||
if handle.is_null() {
|
||||
return None;
|
||||
}
|
||||
// SAFETY: as above.
|
||||
let process = unsafe { OwnedHandle::from_raw_handle(handle) };
|
||||
let exe = image_path(&process)?;
|
||||
let args = command_line(&process).unwrap_or_else(|| exe.clone());
|
||||
Some(RawProcess { pid, exe, args })
|
||||
})
|
||||
.collect())
|
||||
}
|
||||
|
||||
fn pids() -> IdeResult<Vec<u32>> {
|
||||
let mut out = Vec::new();
|
||||
// SAFETY: a snapshot handle owned here and an entry sized as the API requires.
|
||||
unsafe {
|
||||
let snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
|
||||
if snapshot.is_null() || snapshot == INVALID_HANDLE_VALUE {
|
||||
return Err(IdeError::unavailable(format!(
|
||||
"cannot list processes: {}",
|
||||
std::io::Error::last_os_error()
|
||||
)));
|
||||
}
|
||||
let snapshot = OwnedHandle::from_raw_handle(snapshot);
|
||||
let mut entry = PROCESSENTRY32W { dwSize: size_of::<PROCESSENTRY32W>() as u32, ..Default::default() };
|
||||
let mut more = Process32FirstW(snapshot.as_raw_handle(), &mut entry);
|
||||
while more != 0 {
|
||||
out.push(entry.th32ProcessID);
|
||||
more = Process32NextW(snapshot.as_raw_handle(), &mut entry);
|
||||
}
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
fn image_path(process: &OwnedHandle) -> Option<String> {
|
||||
let mut buf = vec![0u16; 32 * 1024];
|
||||
let mut len = buf.len() as u32;
|
||||
// SAFETY: a live process handle with query access; `len` is the buffer's capacity.
|
||||
let ok = unsafe {
|
||||
QueryFullProcessImageNameW(process.as_raw_handle(), PROCESS_NAME_WIN32, buf.as_mut_ptr(), &mut len)
|
||||
};
|
||||
(ok != 0).then(|| String::from_utf16_lossy(&buf[..len as usize]))
|
||||
}
|
||||
|
||||
/// The command line (Windows 8.1+), read with the same limited query access.
|
||||
fn command_line(process: &OwnedHandle) -> Option<String> {
|
||||
let mut size = 0u32;
|
||||
// SAFETY: a size query with no buffer; it fails and reports the size it needs.
|
||||
unsafe {
|
||||
NtQueryInformationProcess(
|
||||
process.as_raw_handle(),
|
||||
ProcessCommandLineInformation,
|
||||
std::ptr::null_mut(),
|
||||
0,
|
||||
&mut size,
|
||||
)
|
||||
};
|
||||
if (size as usize) < size_of::<UNICODE_STRING>() {
|
||||
return None;
|
||||
}
|
||||
// u64 elements keep the UNICODE_STRING header aligned.
|
||||
let mut buf = vec![0u64; (size as usize).div_ceil(8)];
|
||||
// SAFETY: a buffer of at least `size` bytes.
|
||||
let status = unsafe {
|
||||
NtQueryInformationProcess(
|
||||
process.as_raw_handle(),
|
||||
ProcessCommandLineInformation,
|
||||
buf.as_mut_ptr().cast(),
|
||||
size,
|
||||
&mut size,
|
||||
)
|
||||
};
|
||||
if status < 0 {
|
||||
return None;
|
||||
}
|
||||
// SAFETY: on success the buffer starts with a UNICODE_STRING whose text lies inside it.
|
||||
let text = unsafe {
|
||||
let s = &*buf.as_ptr().cast::<UNICODE_STRING>();
|
||||
if s.Buffer.is_null() {
|
||||
return None;
|
||||
}
|
||||
std::slice::from_raw_parts(s.Buffer, usize::from(s.Length) / 2)
|
||||
};
|
||||
Some(String::from_utf16_lossy(text))
|
||||
}
|
||||
}
|
||||
|
||||
/// The full executable path: `comm` keeps a relative path a process was started with.
|
||||
#[cfg(target_os = "macos")]
|
||||
fn exe_path(pid: u32) -> Option<String> {
|
||||
let mut buf = vec![0u8; libc::PROC_PIDPATHINFO_MAXSIZE as usize];
|
||||
// SAFETY: the buffer holds `PROC_PIDPATHINFO_MAXSIZE` bytes, as the call requires.
|
||||
let n = unsafe { libc::proc_pidpath(i32::try_from(pid).ok()?, buf.as_mut_ptr().cast(), buf.len() as u32) };
|
||||
(n > 0).then(|| String::from_utf8_lossy(&buf[..n as usize]).into_owned())
|
||||
}
|
||||
|
||||
#[cfg(all(unix, not(target_os = "macos")))]
|
||||
fn exe_path(_pid: u32) -> Option<String> {
|
||||
None
|
||||
}
|
||||
|
||||
/// Node's default inspector port, which `SIGUSR1` opens.
|
||||
@@ -244,11 +401,29 @@ pub fn attachable_processes() -> IdeResult<Vec<AttachableProcess>> {
|
||||
let cwd = std::env::temp_dir();
|
||||
let native = if adapter::resolve_binary("codelldb", &cwd).is_some() { "codelldb" } else { "lldb-dap" };
|
||||
let mut out: Vec<AttachableProcess> =
|
||||
list_processes()?.iter().filter_map(|p| classify(p, is_go_binary, native)).collect();
|
||||
list_processes()?.iter().filter_map(|p| classify(p, is_dotnet, is_go_binary, native)).collect();
|
||||
out.sort_by(|a, b| a.kind.cmp(&b.kind).then_with(|| a.name.to_lowercase().cmp(&b.name.to_lowercase())));
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
/// The Attach to Process candidates among the processes `root` started, parents first.
|
||||
/// Blocking, like [`attachable_processes`].
|
||||
pub fn attachable_descendants(root: u32) -> IdeResult<Vec<AttachableProcess>> {
|
||||
let order = ide_core::process::descendants(root);
|
||||
if order.is_empty() {
|
||||
return Ok(Vec::new());
|
||||
}
|
||||
let cwd = std::env::temp_dir();
|
||||
let native = if adapter::resolve_binary("codelldb", &cwd).is_some() { "codelldb" } else { "lldb-dap" };
|
||||
let mut out: Vec<AttachableProcess> = list_processes()?
|
||||
.iter()
|
||||
.filter(|p| order.contains(&p.pid))
|
||||
.filter_map(|p| classify(p, is_dotnet, is_go_binary, native))
|
||||
.collect();
|
||||
out.sort_by_key(|p| order.iter().position(|&pid| pid == p.pid));
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@@ -258,7 +433,30 @@ mod tests {
|
||||
}
|
||||
|
||||
fn kind(exe: &str, args: &str) -> Option<(AttachKind, Option<u16>)> {
|
||||
classify(&raw(exe, args), |e| e.ends_with("/gosrv"), "codelldb").map(|p| (p.kind, p.port))
|
||||
classify(&raw(exe, args), |e| e.ends_with("/Api"), |e| e.ends_with("/gosrv"), "codelldb")
|
||||
.map(|p| (p.kind, p.port))
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn a_runs_candidates_are_the_processes_it_started() {
|
||||
let mut child = std::process::Command::new("sh").args(["-c", "sleep 30; true"]).spawn().unwrap();
|
||||
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(10);
|
||||
let found = loop {
|
||||
let found = attachable_descendants(child.id()).unwrap();
|
||||
if !found.is_empty() || std::time::Instant::now() > deadline {
|
||||
break found;
|
||||
}
|
||||
std::thread::sleep(std::time::Duration::from_millis(50));
|
||||
};
|
||||
for p in &found {
|
||||
let _ = std::process::Command::new("kill").arg(p.pid.to_string()).status();
|
||||
}
|
||||
child.kill().unwrap();
|
||||
child.wait().unwrap();
|
||||
assert!(found.iter().all(|p| p.pid != child.id()), "{found:?}");
|
||||
assert!(found.iter().any(|p| p.name == "sleep" && p.kind == AttachKind::Native), "{found:?}");
|
||||
assert!(attachable_descendants(u32::MAX - 1).unwrap().is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -292,11 +490,37 @@ mod tests {
|
||||
kind("/Library/Frameworks/Python.framework/Versions/3.12/Resources/Python.app/Contents/MacOS/Python", "x"),
|
||||
Some((AttachKind::Python, None))
|
||||
);
|
||||
assert_eq!(kind("/home/me/Api", "./Api"), Some((AttachKind::Dotnet, None)));
|
||||
assert_eq!(kind("/home/me/gosrv", "./gosrv"), Some((AttachKind::Go, None)));
|
||||
assert_eq!(kind("/home/me/a.out", "./a.out"), Some((AttachKind::Native, None)));
|
||||
assert!(!is_python("pythonista"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn detects_dotnet_hosts_and_apphosts() {
|
||||
let dir = std::env::temp_dir().join(format!("openidea-attach-dotnet-{}", std::process::id()));
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let app = dir.join("Api");
|
||||
std::fs::write(&app, b"").unwrap();
|
||||
std::fs::write(dir.join("Api.runtimeconfig.json"), b"{}").unwrap();
|
||||
let tool = dir.join("tool");
|
||||
std::fs::write(&tool, b"").unwrap();
|
||||
assert!(is_dotnet(app.to_str().unwrap()));
|
||||
assert!(!is_dotnet(tool.to_str().unwrap()));
|
||||
assert!(is_dotnet("/usr/local/share/dotnet/dotnet"));
|
||||
assert!(is_dotnet("dotnet"));
|
||||
assert!(!is_dotnet("Api"));
|
||||
std::fs::remove_dir_all(&dir).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn skips_windows_system_programs() {
|
||||
assert!(is_windows_system(r"C:\Windows\System32\svchost.exe"));
|
||||
assert!(is_windows_system(r"c:\program files (x86)\App\app.exe"));
|
||||
assert!(!is_windows_system(r"D:\src\srv\srv.exe"));
|
||||
assert!(!is_windows_system("/usr/bin/true"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn detects_go_binaries_by_buildinfo() {
|
||||
let dir = std::env::temp_dir().join(format!("openidea-attach-{}", std::process::id()));
|
||||
|
||||
@@ -342,4 +342,16 @@ mod tests {
|
||||
assert_eq!(error_message(&msg), "cannot find a.py");
|
||||
assert_eq!(error_message(&json!({"message": "nope"})), "nope");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_failed_launch_response_keeps_delves_too_old_error_verbatim() {
|
||||
// The UI's repair offer keys off this exact wording (`missingAdapter`), so the
|
||||
// conversion to the IPC error must not fold it into a generic message.
|
||||
let message = "Failed to launch /p/api: Version of Delve is too old for Go version go1.27.0 \
|
||||
(maximum supported version 1.26, suppress this error with --check-go-version=false)";
|
||||
let e = ide_core::IdeError::from(DapError::Failed { command: "launch".into(), message: message.into() });
|
||||
assert_eq!(e.kind, ide_core::error::ErrorKind::InvalidInput);
|
||||
assert_eq!(e.message, format!("launch failed: {message}"));
|
||||
assert!(e.message.contains("Version of Delve is too old for Go version go1.27.0"));
|
||||
}
|
||||
}
|
||||
+100
-13
@@ -12,6 +12,35 @@ use tokio::net::{TcpListener, TcpStream};
|
||||
/// How long the relay waits for java-debug to attach once the JVM is connected.
|
||||
const ATTACH_TIMEOUT: Duration = Duration::from_secs(60);
|
||||
|
||||
/// Loopback ports relaying a connected JVM, while their relay runs.
|
||||
static RELAY_PORTS: std::sync::Mutex<Vec<u16>> = std::sync::Mutex::new(Vec::new());
|
||||
|
||||
fn relay_ports() -> std::sync::MutexGuard<'static, Vec<u16>> {
|
||||
RELAY_PORTS.lock().unwrap_or_else(std::sync::PoisonError::into_inner)
|
||||
}
|
||||
|
||||
/// Whether `port` is a relay of [`listen_for_jvm`]: a session attaching to it debugs a JVM that
|
||||
/// connected to the IDE, so the JVM has no JDWP listener the memory view could find it by.
|
||||
pub(crate) fn is_listen_relay(port: u16) -> bool {
|
||||
relay_ports().contains(&port)
|
||||
}
|
||||
|
||||
/// Registers a relay port for as long as it lives.
|
||||
struct RelayPort(u16);
|
||||
|
||||
impl RelayPort {
|
||||
fn new(port: u16) -> Self {
|
||||
relay_ports().push(port);
|
||||
Self(port)
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for RelayPort {
|
||||
fn drop(&mut self) {
|
||||
relay_ports().retain(|&p| p != self.0);
|
||||
}
|
||||
}
|
||||
|
||||
/// The address to listen on for `host`: the interface it names, or every interface when
|
||||
/// it is not one of this machine's (a DNS name or a NAT address the JVM connects to).
|
||||
async fn bind(host: &str, port: u16) -> IdeResult<TcpListener> {
|
||||
@@ -40,11 +69,11 @@ pub async fn listen_for_jvm(host: &str, port: u16, cancel: CancellationToken) ->
|
||||
let _ = jvm.set_nodelay(true);
|
||||
let relay = TcpListener::bind(("127.0.0.1", 0)).await?;
|
||||
let local = relay.local_addr()?.port();
|
||||
tokio::spawn(relay_one(relay, jvm));
|
||||
tokio::spawn(relay_one(relay, jvm, RelayPort::new(local)));
|
||||
Ok(local)
|
||||
}
|
||||
|
||||
async fn relay_one(relay: TcpListener, mut jvm: TcpStream) {
|
||||
async fn relay_one(relay: TcpListener, mut jvm: TcpStream, _registered: RelayPort) {
|
||||
let debugger = match tokio::time::timeout(ATTACH_TIMEOUT, relay.accept()).await {
|
||||
Ok(Ok((s, _))) => s,
|
||||
_ => return,
|
||||
@@ -58,22 +87,64 @@ async fn relay_one(relay: TcpListener, mut jvm: TcpStream) {
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
pub(crate) mod tests {
|
||||
use super::*;
|
||||
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
||||
|
||||
#[tokio::test]
|
||||
async fn relays_the_jdwp_handshake_both_ways() {
|
||||
let port = TcpListener::bind(("127.0.0.1", 0)).await.unwrap().local_addr().unwrap().port();
|
||||
let waiting = tokio::spawn(listen_for_jvm("127.0.0.1", port, CancellationToken::new()));
|
||||
// The remote JVM (`server=n`) connects to the IDE.
|
||||
let mut jvm = loop {
|
||||
if let Ok(s) = TcpStream::connect(("127.0.0.1", port)).await {
|
||||
break s;
|
||||
/// A remote JVM (`server=n`) connected to [`listen_for_jvm`], and the relay port. A port
|
||||
/// picked by binding to 0 can be taken by a parallel test before `listen_for_jvm` binds it,
|
||||
/// so a failed listen is retried on a fresh one.
|
||||
pub(crate) async fn connected_jvm() -> (TcpStream, u16) {
|
||||
for _ in 0..20 {
|
||||
let port = TcpListener::bind(("127.0.0.1", 0)).await.unwrap().local_addr().unwrap().port();
|
||||
if let Some(connected) = jvm_on(port).await {
|
||||
return connected;
|
||||
}
|
||||
}
|
||||
panic!("listen_for_jvm never accepted a JVM");
|
||||
}
|
||||
|
||||
/// One attempt of [`connected_jvm`]; `None` when the port turned out to be taken.
|
||||
async fn jvm_on(port: u16) -> Option<(TcpStream, u16)> {
|
||||
let mut waiting = tokio::spawn(listen_for_jvm("127.0.0.1", port, CancellationToken::new()));
|
||||
let mut jvm = None;
|
||||
for _ in 0..100 {
|
||||
// A listen that failed to bind leaves nothing to connect to.
|
||||
if waiting.is_finished() {
|
||||
break;
|
||||
}
|
||||
// macOS does not refuse a connect to a port a socket holds without listening: the
|
||||
// SYN goes unanswered until the connect times out.
|
||||
let connect = TcpStream::connect(("127.0.0.1", port));
|
||||
if let Ok(Ok(s)) = tokio::time::timeout(Duration::from_secs(1), connect).await {
|
||||
jvm = Some(s);
|
||||
break;
|
||||
}
|
||||
tokio::time::sleep(Duration::from_millis(20)).await;
|
||||
};
|
||||
let local = waiting.await.unwrap().unwrap();
|
||||
}
|
||||
// A connection to another test's listener never reaches `listen_for_jvm`.
|
||||
let relay = tokio::time::timeout(Duration::from_secs(2), &mut waiting).await;
|
||||
if let (Some(jvm), Ok(Ok(Ok(relay)))) = (jvm, relay) {
|
||||
return Some((jvm, relay));
|
||||
}
|
||||
waiting.abort();
|
||||
None
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_port_another_socket_holds_is_given_up() {
|
||||
// Bound but not listening, as a parallel test's outgoing connection holds its local port.
|
||||
let held = tokio::net::TcpSocket::new_v4().unwrap();
|
||||
held.bind(([127, 0, 0, 1], 0).into()).unwrap();
|
||||
let port = held.local_addr().unwrap().port();
|
||||
let attempt = tokio::time::timeout(Duration::from_secs(5), jvm_on(port)).await;
|
||||
assert!(matches!(attempt, Ok(None)), "an attempt on a taken port gives up instead of waiting forever");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn relays_the_jdwp_handshake_both_ways() {
|
||||
// The remote JVM (`server=n`) connects to the IDE.
|
||||
let (mut jvm, local) = connected_jvm().await;
|
||||
// java-debug attaches to the relay and starts the handshake.
|
||||
let mut debugger = TcpStream::connect(("127.0.0.1", local)).await.unwrap();
|
||||
debugger.write_all(b"JDWP-Handshake").await.unwrap();
|
||||
@@ -85,6 +156,22 @@ mod tests {
|
||||
assert_eq!(&buf, b"JDWP-Handshake");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn relay_port_is_known_while_the_relay_runs() {
|
||||
let (jvm, local) = connected_jvm().await;
|
||||
assert!(is_listen_relay(local));
|
||||
|
||||
let debugger = TcpStream::connect(("127.0.0.1", local)).await.unwrap();
|
||||
drop((jvm, debugger));
|
||||
for _ in 0..200 {
|
||||
if !is_listen_relay(local) {
|
||||
return;
|
||||
}
|
||||
tokio::time::sleep(Duration::from_millis(10)).await;
|
||||
}
|
||||
panic!("relay port {local} still registered after the relay ended");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn waiting_is_cancellable() {
|
||||
let cancel = CancellationToken::new();
|
||||
|
||||
@@ -0,0 +1,528 @@
|
||||
//! Replacing the responses a debugged page receives. js-debug hands out a WebSocket that
|
||||
//! speaks the browser's own protocol (`requestCDPProxy`); over it the browser is told to hold
|
||||
//! the requests whose URL matches a rule (`Fetch.enable`) and each held request is answered
|
||||
//! here, with the rule's response or by letting it through.
|
||||
//!
|
||||
//! A held request stays held until it is answered, so interception is always switched off
|
||||
//! (`Fetch.disable`) before the connection is given up: a page whose requests nobody answers hangs.
|
||||
|
||||
use crate::{DebugManager, DebugSessionId, REQUEST_TIMEOUT, js_debug};
|
||||
use base64::Engine;
|
||||
use futures::{SinkExt, StreamExt};
|
||||
use ide_core::{IdeError, IdeResult};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::{Value, json};
|
||||
use specta::Type;
|
||||
use std::collections::HashMap;
|
||||
use tokio::sync::{oneshot, watch};
|
||||
use tokio_tungstenite::tungstenite::{Message, http::StatusCode};
|
||||
|
||||
/// A response given in place of the server's to every request whose URL contains `url_contains`.
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Type)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct JsResponseRule {
|
||||
pub url_contains: String,
|
||||
pub status: u16,
|
||||
pub content_type: String,
|
||||
pub body: String,
|
||||
}
|
||||
|
||||
/// The browser's URL pattern (`*` any text, `?` one character, `\` escapes) for "contains `part`".
|
||||
pub(crate) fn url_pattern(part: &str) -> String {
|
||||
let mut pattern = String::with_capacity(part.len() + 2);
|
||||
pattern.push('*');
|
||||
for c in part.chars() {
|
||||
if matches!(c, '*' | '?' | '\\') {
|
||||
pattern.push('\\');
|
||||
}
|
||||
pattern.push(c);
|
||||
}
|
||||
pattern.push('*');
|
||||
pattern
|
||||
}
|
||||
|
||||
pub(crate) fn rule_for<'a>(rules: &'a [JsResponseRule], url: &str) -> Option<&'a JsResponseRule> {
|
||||
rules.iter().find(|rule| url.contains(&rule.url_contains))
|
||||
}
|
||||
|
||||
/// What to tell the browser about a request it holds: the first matching rule's response, or
|
||||
/// to send the request on. Every held request gets one of the two.
|
||||
pub(crate) fn answer(held: &Value, rules: &[JsResponseRule]) -> (&'static str, Value) {
|
||||
let request_id = held.get("requestId").and_then(Value::as_str).unwrap_or_default();
|
||||
let url = held.pointer("/request/url").and_then(Value::as_str).unwrap_or_default();
|
||||
let Some(rule) = rule_for(rules, url) else {
|
||||
return ("Fetch.continueRequest", json!({ "requestId": request_id }));
|
||||
};
|
||||
let header = |name: &str, value: &str| json!({ "name": name, "value": value });
|
||||
// A page on another origin only reads a response that names its origin. The server would
|
||||
// say so; the response given in its place has to.
|
||||
let mut cross_origin = Vec::new();
|
||||
if let Some(origin) = request_header(held, "Origin") {
|
||||
cross_origin.push(header("Access-Control-Allow-Origin", origin));
|
||||
cross_origin.push(header("Access-Control-Allow-Credentials", "true"));
|
||||
}
|
||||
let method = held.pointer("/request/method").and_then(Value::as_str).unwrap_or_default();
|
||||
// The browser's question before such a request: allowed here, the server may not exist.
|
||||
if method == "OPTIONS"
|
||||
&& let Some(asked) = request_header(held, "Access-Control-Request-Method")
|
||||
{
|
||||
cross_origin.push(header("Access-Control-Allow-Methods", asked));
|
||||
if let Some(asked) = request_header(held, "Access-Control-Request-Headers") {
|
||||
cross_origin.push(header("Access-Control-Allow-Headers", asked));
|
||||
}
|
||||
let allowed = json!({
|
||||
"requestId": request_id,
|
||||
"responseCode": 204,
|
||||
"responsePhrase": phrase(204),
|
||||
"responseHeaders": cross_origin,
|
||||
});
|
||||
return ("Fetch.fulfillRequest", allowed);
|
||||
}
|
||||
let mut headers = vec![header("Content-Type", &rule.content_type)];
|
||||
headers.extend(cross_origin);
|
||||
let replaced = json!({
|
||||
"requestId": request_id,
|
||||
"responseCode": rule.status,
|
||||
"responsePhrase": phrase(rule.status),
|
||||
"responseHeaders": headers,
|
||||
"body": base64::engine::general_purpose::STANDARD.encode(rule.body.as_bytes()),
|
||||
});
|
||||
("Fetch.fulfillRequest", replaced)
|
||||
}
|
||||
|
||||
/// A header of the held request; the browser reports names as the page wrote them.
|
||||
fn request_header<'a>(held: &'a Value, name: &str) -> Option<&'a str> {
|
||||
held.pointer("/request/headers")?.as_object()?.iter().find(|(key, _)| key.eq_ignore_ascii_case(name))?.1.as_str()
|
||||
}
|
||||
|
||||
/// The browser refuses a response without a phrase when it has none of its own for the status.
|
||||
fn phrase(status: u16) -> &'static str {
|
||||
StatusCode::from_u16(status).ok().and_then(|status| status.canonical_reason()).unwrap_or("Unknown")
|
||||
}
|
||||
|
||||
fn interception(rules: &[JsResponseRule]) -> Value {
|
||||
let patterns: Vec<Value> = rules
|
||||
.iter()
|
||||
.map(|rule| json!({ "urlPattern": url_pattern(&rule.url_contains), "requestStage": "Request" }))
|
||||
.collect();
|
||||
json!({ "patterns": patterns })
|
||||
}
|
||||
|
||||
/// Applies `rules` to the page behind js-debug's CDP proxy at `ws_url` until their sender is
|
||||
/// dropped (the session is gone) or the proxy closes the connection. The browser holds
|
||||
/// requests only while there are rules. `ready` hears whether the proxy reports held
|
||||
/// requests at all; nothing is held before it has agreed to.
|
||||
pub(crate) async fn serve(
|
||||
ws_url: String,
|
||||
mut rules: watch::Receiver<Vec<JsResponseRule>>,
|
||||
ready: oneshot::Sender<Result<(), String>>,
|
||||
) {
|
||||
let socket = match tokio_tungstenite::connect_async(ws_url.as_str()).await {
|
||||
Ok((socket, _)) => socket,
|
||||
Err(e) => {
|
||||
let _ = ready.send(Err(e.to_string()));
|
||||
return;
|
||||
}
|
||||
};
|
||||
let (mut write, mut read) = socket.split();
|
||||
let mut last_id = 0u64;
|
||||
let mut call = |method: &str, params: Value| {
|
||||
last_id += 1;
|
||||
(last_id, Message::text(json!({ "id": last_id, "method": method, "params": params }).to_string()))
|
||||
};
|
||||
|
||||
let (subscription, subscribe) = call("JsDebug.subscribe", json!({ "events": ["Fetch.requestPaused"] }));
|
||||
let subscribed = tokio::time::timeout(REQUEST_TIMEOUT, async {
|
||||
write.send(subscribe).await.map_err(|e| e.to_string())?;
|
||||
loop {
|
||||
let reply = read.next().await.ok_or("the connection closed")?.map_err(|e| e.to_string())?;
|
||||
let Some(reply) = reply.to_text().ok().and_then(|text| serde_json::from_str::<Value>(text).ok()) else {
|
||||
continue;
|
||||
};
|
||||
if reply.get("id").and_then(Value::as_u64) != Some(subscription) {
|
||||
continue;
|
||||
}
|
||||
break match reply.get("error") {
|
||||
Some(error) => Err(error.get("message").and_then(Value::as_str).unwrap_or("refused").to_owned()),
|
||||
None => Ok(()),
|
||||
};
|
||||
}
|
||||
})
|
||||
.await
|
||||
.unwrap_or_else(|_| Err("no answer".to_owned()));
|
||||
if let Err(why) = subscribed {
|
||||
let _ = ready.send(Err(why));
|
||||
let _ = write.close().await;
|
||||
return;
|
||||
}
|
||||
let _ = ready.send(Ok(()));
|
||||
|
||||
let mut current = rules.borrow_and_update().clone();
|
||||
let mut holding = !current.is_empty();
|
||||
if holding && write.send(call("Fetch.enable", interception(¤t)).1).await.is_err() {
|
||||
return;
|
||||
}
|
||||
// The request each sent replacement is for, by command id, until the browser has taken it.
|
||||
let mut replacing: HashMap<u64, String> = HashMap::new();
|
||||
loop {
|
||||
tokio::select! {
|
||||
changed = rules.changed() => {
|
||||
if changed.is_err() {
|
||||
break;
|
||||
}
|
||||
current = rules.borrow_and_update().clone();
|
||||
let (_, command) = match (current.is_empty(), holding) {
|
||||
(true, false) => continue,
|
||||
(true, true) => call("Fetch.disable", json!({})),
|
||||
(false, _) => call("Fetch.enable", interception(¤t)),
|
||||
};
|
||||
holding = !current.is_empty();
|
||||
if write.send(command).await.is_err() {
|
||||
return;
|
||||
}
|
||||
}
|
||||
message = read.next() => {
|
||||
// The proxy closed (the page or the session ended): nothing is held any more.
|
||||
let Some(Ok(message)) = message else { return };
|
||||
let Ok(text) = message.to_text() else { continue };
|
||||
let Ok(message) = serde_json::from_str::<Value>(text) else { continue };
|
||||
if let Some(id) = message.get("id").and_then(Value::as_u64) {
|
||||
// The browser did not take the replacement: let the request through, or
|
||||
// it stays held and the page waits for it.
|
||||
if let Some(request_id) = replacing.remove(&id)
|
||||
&& message.get("error").is_some()
|
||||
{
|
||||
let (_, go_on) = call("Fetch.continueRequest", json!({ "requestId": request_id }));
|
||||
if write.send(go_on).await.is_err() {
|
||||
return;
|
||||
}
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if message.get("method").and_then(Value::as_str) != Some("Fetch.requestPaused") {
|
||||
continue;
|
||||
}
|
||||
let Some(held) = message.get("params") else { continue };
|
||||
let Some(request_id) = held.get("requestId").and_then(Value::as_str) else { continue };
|
||||
let (method, params) = answer(held, ¤t);
|
||||
let (id, command) = call(method, params);
|
||||
if method == "Fetch.fulfillRequest" {
|
||||
replacing.insert(id, request_id.to_owned());
|
||||
}
|
||||
if write.send(command).await.is_err() {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if holding {
|
||||
let _ = write.send(call("Fetch.disable", json!({})).1).await;
|
||||
}
|
||||
let _ = write.close().await;
|
||||
}
|
||||
|
||||
impl DebugManager {
|
||||
/// Makes `rules` the page's response rules; none switches the replacing off. The first
|
||||
/// rules of a session open the connection to js-debug's CDP proxy, later ones go over it.
|
||||
/// An error means the rules are not applied.
|
||||
pub async fn js_set_response_overrides(&self, s: DebugSessionId, rules: Vec<JsResponseRule>) -> IdeResult<()> {
|
||||
let session = self.session(s)?;
|
||||
if !js_debug::is_page(&session) {
|
||||
return Err(IdeError::unavailable("this debug session is not a page in a browser"));
|
||||
}
|
||||
// Held to the end: two callers at once would each open a connection.
|
||||
let mut applied = session.js_overrides.lock().await;
|
||||
if applied.as_ref().is_some_and(|sender| sender.send(rules.clone()).is_ok()) {
|
||||
return Ok(());
|
||||
}
|
||||
// No connection yet, or the proxy closed it.
|
||||
applied.take();
|
||||
if rules.is_empty() {
|
||||
return Ok(());
|
||||
}
|
||||
let refused =
|
||||
|why: String| IdeError::unavailable(format!("the JavaScript debugger has no browser connection: {why}"));
|
||||
if !js_debug::launched(&session).await {
|
||||
return Err(refused("the page's debug session did not finish starting".into()));
|
||||
}
|
||||
let proxy = session
|
||||
.conn
|
||||
.request_timeout("requestCDPProxy", json!({}), REQUEST_TIMEOUT)
|
||||
.await
|
||||
.map_err(|e| refused(e.to_string()))?;
|
||||
let (Some(host), Some(port)) =
|
||||
(proxy.get("host").and_then(Value::as_str), proxy.get("port").and_then(Value::as_u64))
|
||||
else {
|
||||
return Err(refused("no address".into()));
|
||||
};
|
||||
let path = proxy.get("path").and_then(Value::as_str).unwrap_or_default();
|
||||
let (sender, receiver) = watch::channel(rules);
|
||||
let (ready, is_ready) = oneshot::channel();
|
||||
tokio::spawn(serve(format!("ws://{host}:{port}{path}"), receiver, ready));
|
||||
match is_ready.await {
|
||||
Ok(Ok(())) => {
|
||||
*applied = Some(sender);
|
||||
Ok(())
|
||||
}
|
||||
Ok(Err(why)) => Err(refused(why)),
|
||||
Err(_) => Err(refused("no answer".into())),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub(crate) mod tests {
|
||||
use super::*;
|
||||
use futures::{SinkExt, StreamExt};
|
||||
use serde_json::json;
|
||||
use tokio::sync::mpsc;
|
||||
use tokio_tungstenite::tungstenite::Message;
|
||||
|
||||
pub(crate) fn rule(url_contains: &str, status: u16, body: &str) -> JsResponseRule {
|
||||
JsResponseRule {
|
||||
url_contains: url_contains.into(),
|
||||
status,
|
||||
content_type: "application/json".into(),
|
||||
body: body.into(),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_part_of_a_url_becomes_a_pattern_that_matches_it_anywhere() {
|
||||
assert_eq!(url_pattern("api/items"), "*api/items*");
|
||||
// The pattern's own wildcards and escape character are taken literally.
|
||||
assert_eq!(url_pattern(r"a*b?c\d"), r"*a\*b\?c\\d*");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_first_rule_whose_text_is_in_the_url_answers() {
|
||||
let rules = [rule("api/items", 201, "{\"a\":\"é\"}"), rule("api", 500, "later")];
|
||||
assert_eq!(rule_for(&rules, "http://h/api/items?x=1").map(|r| r.status), Some(201));
|
||||
assert_eq!(rule_for(&rules, "http://h/api/users").map(|r| r.status), Some(500));
|
||||
assert!(rule_for(&rules, "http://h/logo.png").is_none());
|
||||
|
||||
let (command, params) = answer(&held("7", "http://h/api/items")["params"], &rules);
|
||||
assert_eq!(command, "Fetch.fulfillRequest");
|
||||
assert_eq!(
|
||||
params,
|
||||
json!({
|
||||
"requestId": "7",
|
||||
"responseCode": 201,
|
||||
"responsePhrase": "Created",
|
||||
"responseHeaders": [{ "name": "Content-Type", "value": "application/json" }],
|
||||
// base64 of the UTF-8 text `{"a":"é"}`
|
||||
"body": "eyJhIjoiw6kifQ==",
|
||||
})
|
||||
);
|
||||
// A request the browser held that no rule is for goes on to the network.
|
||||
assert_eq!(
|
||||
answer(&held("8", "http://h/logo.png")["params"], &rules),
|
||||
("Fetch.continueRequest", json!({ "requestId": "8" }))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_status_the_browser_has_no_phrase_for_is_given_one() {
|
||||
// The browser refuses a response whose status it cannot name.
|
||||
let (_, params) = answer(&held("1", "http://h/api")["params"], &[rule("api", 419, "")]);
|
||||
assert_eq!((¶ms["responseCode"], ¶ms["responsePhrase"]), (&json!(419), &json!("Unknown")));
|
||||
}
|
||||
|
||||
fn header<'a>(params: &'a Value, name: &str) -> Option<&'a str> {
|
||||
params["responseHeaders"].as_array()?.iter().find(|h| h["name"] == name)?["value"].as_str()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_replaced_response_passes_the_pages_cross_origin_check() {
|
||||
let rules = [rule("api/items", 200, "[]")];
|
||||
let from_page = |method: &str, headers: Value| json!({ "requestId": "3", "request": { "url": "http://localhost:8080/api/items", "method": method, "headers": headers } });
|
||||
// A page on another origin: the response names that origin, as the real server would.
|
||||
let (_, params) = answer(&from_page("GET", json!({ "origin": "http://localhost:5173" })), &rules);
|
||||
assert_eq!(header(¶ms, "Access-Control-Allow-Origin"), Some("http://localhost:5173"));
|
||||
assert_eq!(header(¶ms, "Access-Control-Allow-Credentials"), Some("true"));
|
||||
assert_eq!(params["body"], "W10=");
|
||||
|
||||
// The browser's question before such a request is answered too: the server may not exist.
|
||||
let preflight = from_page(
|
||||
"OPTIONS",
|
||||
json!({
|
||||
"Origin": "http://localhost:5173",
|
||||
"Access-Control-Request-Method": "PUT",
|
||||
"Access-Control-Request-Headers": "content-type,x-token",
|
||||
}),
|
||||
);
|
||||
let (command, params) = answer(&preflight, &rules);
|
||||
assert_eq!((command, ¶ms["responseCode"]), ("Fetch.fulfillRequest", &json!(204)));
|
||||
assert_eq!(header(¶ms, "Access-Control-Allow-Origin"), Some("http://localhost:5173"));
|
||||
assert_eq!(header(¶ms, "Access-Control-Allow-Methods"), Some("PUT"));
|
||||
assert_eq!(header(¶ms, "Access-Control-Allow-Headers"), Some("content-type,x-token"));
|
||||
assert_eq!(params.get("body"), None);
|
||||
|
||||
// Same origin: nothing but the rule's own header.
|
||||
let (_, params) = answer(&from_page("GET", json!({})), &rules);
|
||||
assert_eq!(params["responseHeaders"], json!([{ "name": "Content-Type", "value": "application/json" }]));
|
||||
}
|
||||
|
||||
/// A stand-in for js-debug's CDP proxy: what it receives, and a way to send it events.
|
||||
pub(crate) async fn proxy() -> (String, mpsc::UnboundedReceiver<Value>, mpsc::UnboundedSender<Value>) {
|
||||
proxy_refusing(|_| false).await
|
||||
}
|
||||
|
||||
/// A proxy that answers the commands `refused` picks with an error, like the real one does
|
||||
/// for what it or the browser does not accept.
|
||||
pub(crate) async fn proxy_refusing(
|
||||
refused: fn(&Value) -> bool,
|
||||
) -> (String, mpsc::UnboundedReceiver<Value>, mpsc::UnboundedSender<Value>) {
|
||||
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||
let url = format!("ws://127.0.0.1:{}/session", listener.local_addr().unwrap().port());
|
||||
let (received_tx, received) = mpsc::unbounded_channel();
|
||||
let (events, mut events_rx) = mpsc::unbounded_channel::<Value>();
|
||||
tokio::spawn(async move {
|
||||
let (stream, _) = listener.accept().await.unwrap();
|
||||
let (mut write, mut read) = tokio_tungstenite::accept_async(stream).await.unwrap().split();
|
||||
loop {
|
||||
tokio::select! {
|
||||
message = read.next() => {
|
||||
let Some(Ok(message)) = message else { break };
|
||||
let Ok(text) = message.to_text() else { continue };
|
||||
let Ok(request) = serde_json::from_str::<Value>(text) else { continue };
|
||||
let reply = if refused(&request) {
|
||||
json!({ "id": request["id"], "error": { "code": -32602, "message": "refused" } })
|
||||
} else {
|
||||
json!({ "id": request["id"], "result": {} })
|
||||
};
|
||||
let _ = write.send(Message::text(reply.to_string())).await;
|
||||
let _ = received_tx.send(request);
|
||||
}
|
||||
Some(event) = events_rx.recv() => {
|
||||
let _ = write.send(Message::text(event.to_string())).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
(url, received, events)
|
||||
}
|
||||
|
||||
pub(crate) async fn next(received: &mut mpsc::UnboundedReceiver<Value>) -> (String, Value) {
|
||||
let request = tokio::time::timeout(std::time::Duration::from_secs(5), received.recv())
|
||||
.await
|
||||
.expect("the proxy got a request in time")
|
||||
.expect("the connection is open");
|
||||
(request["method"].as_str().unwrap().to_owned(), request["params"].clone())
|
||||
}
|
||||
|
||||
/// The browser's event for a request it holds.
|
||||
fn held(request_id: &str, url: &str) -> Value {
|
||||
json!({ "method": "Fetch.requestPaused", "params": { "requestId": request_id, "request": { "url": url } } })
|
||||
}
|
||||
|
||||
type Serving = (tokio::task::JoinHandle<()>, oneshot::Receiver<Result<(), String>>);
|
||||
|
||||
fn serving(url: String, rules: watch::Receiver<Vec<JsResponseRule>>) -> Serving {
|
||||
let (ready, is_ready) = oneshot::channel();
|
||||
(tokio::spawn(serve(url, rules, ready)), is_ready)
|
||||
}
|
||||
|
||||
async fn ended(task: tokio::task::JoinHandle<()>) {
|
||||
tokio::time::timeout(std::time::Duration::from_secs(5), task).await.unwrap().unwrap();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn held_requests_are_answered_and_interception_ends_before_the_connection() {
|
||||
let (url, mut received, events) = proxy().await;
|
||||
let (rules, watched) = tokio::sync::watch::channel(vec![rule("api/items", 200, "[]")]);
|
||||
let (serving, ready) = serving(url, watched);
|
||||
assert_eq!(ready.await.unwrap(), Ok(()));
|
||||
|
||||
assert_eq!(
|
||||
next(&mut received).await,
|
||||
("JsDebug.subscribe".into(), json!({ "events": ["Fetch.requestPaused"] }))
|
||||
);
|
||||
assert_eq!(
|
||||
next(&mut received).await,
|
||||
(
|
||||
"Fetch.enable".into(),
|
||||
json!({ "patterns": [{ "urlPattern": "*api/items*", "requestStage": "Request" }] })
|
||||
)
|
||||
);
|
||||
|
||||
events.send(held("1", "http://h/api/items")).unwrap();
|
||||
let (command, params) = next(&mut received).await;
|
||||
assert_eq!(
|
||||
(command.as_str(), ¶ms["requestId"], ¶ms["responseCode"]),
|
||||
("Fetch.fulfillRequest", &json!("1"), &json!(200))
|
||||
);
|
||||
events.send(held("2", "http://h/other")).unwrap();
|
||||
assert_eq!(next(&mut received).await, ("Fetch.continueRequest".into(), json!({ "requestId": "2" })));
|
||||
|
||||
// New rules replace the browser's patterns.
|
||||
rules.send(vec![rule("api/users", 404, "{}")]).unwrap();
|
||||
assert_eq!(
|
||||
next(&mut received).await,
|
||||
(
|
||||
"Fetch.enable".into(),
|
||||
json!({ "patterns": [{ "urlPattern": "*api/users*", "requestStage": "Request" }] })
|
||||
)
|
||||
);
|
||||
|
||||
// No rules left: the browser stops holding requests. The connection stays for the
|
||||
// rules that come next, and goes with the session.
|
||||
rules.send(Vec::new()).unwrap();
|
||||
assert_eq!(next(&mut received).await.0, "Fetch.disable");
|
||||
rules.send(vec![rule("api/items", 200, "[]")]).unwrap();
|
||||
assert_eq!(next(&mut received).await.0, "Fetch.enable");
|
||||
rules.send(Vec::new()).unwrap();
|
||||
assert_eq!(next(&mut received).await.0, "Fetch.disable");
|
||||
drop(rules);
|
||||
ended(serving).await;
|
||||
// Already off: not switched off a second time.
|
||||
assert!(received.try_recv().is_err());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_session_that_goes_away_switches_interception_off() {
|
||||
let (url, mut received, _events) = proxy().await;
|
||||
let (rules, watched) = tokio::sync::watch::channel(vec![rule("api", 200, "[]")]);
|
||||
let (serving, _ready) = serving(url, watched);
|
||||
assert_eq!(next(&mut received).await.0, "JsDebug.subscribe");
|
||||
assert_eq!(next(&mut received).await.0, "Fetch.enable");
|
||||
drop(rules);
|
||||
assert_eq!(next(&mut received).await.0, "Fetch.disable");
|
||||
ended(serving).await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_response_the_browser_refuses_lets_the_request_through() {
|
||||
let (url, mut received, events) = proxy_refusing(|request| request["method"] == "Fetch.fulfillRequest").await;
|
||||
let (_rules, watched) = tokio::sync::watch::channel(vec![rule("api", 200, "[]")]);
|
||||
let (_serving, _ready) = serving(url, watched);
|
||||
assert_eq!(next(&mut received).await.0, "JsDebug.subscribe");
|
||||
assert_eq!(next(&mut received).await.0, "Fetch.enable");
|
||||
events.send(held("9", "http://h/api")).unwrap();
|
||||
assert_eq!(next(&mut received).await.0, "Fetch.fulfillRequest");
|
||||
// Unanswered, the request would hang the page.
|
||||
assert_eq!(next(&mut received).await, ("Fetch.continueRequest".into(), json!({ "requestId": "9" })));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_proxy_that_does_not_report_held_requests_is_not_asked_to_hold_any() {
|
||||
let (url, mut received, _events) = proxy_refusing(|request| request["method"] == "JsDebug.subscribe").await;
|
||||
let (_rules, watched) = tokio::sync::watch::channel(vec![rule("api", 200, "[]")]);
|
||||
let (serving, ready) = serving(url, watched);
|
||||
assert_eq!(next(&mut received).await.0, "JsDebug.subscribe");
|
||||
assert_eq!(ready.await.unwrap(), Err("refused".to_owned()));
|
||||
ended(serving).await;
|
||||
assert!(received.try_recv().is_err());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn no_connection_to_the_proxy_is_reported() {
|
||||
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||
let url = format!("ws://127.0.0.1:{}/session", listener.local_addr().unwrap().port());
|
||||
drop(listener);
|
||||
let (_rules, watched) = tokio::sync::watch::channel(vec![rule("api", 200, "[]")]);
|
||||
let (serving, ready) = serving(url, watched);
|
||||
assert!(ready.await.unwrap().is_err());
|
||||
ended(serving).await;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,323 @@
|
||||
//! Requests of vscode-js-debug's own, beyond the Debug Adapter Protocol: what a page in a
|
||||
//! browser offers a debugger besides stepping (breakpoints on DOM events and requests).
|
||||
//! Each is a typed method; a js-debug build that lacks one answers with an error, which is
|
||||
//! reported as the feature being unavailable.
|
||||
|
||||
use crate::session::Session;
|
||||
use crate::{DebugManager, DebugSessionId, REQUEST_TIMEOUT};
|
||||
use ide_core::event::{JsInitiator, JsNetworkEvent, JsNetworkKind};
|
||||
use ide_core::{IdeError, IdeResult};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::{Value, json};
|
||||
use specta::Type;
|
||||
use std::collections::BTreeMap;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::atomic::{AtomicU32, Ordering};
|
||||
|
||||
pub(crate) const ADAPTER: &str = "js-debug";
|
||||
|
||||
/// Whether `session` debugs a page: the child js-debug starts for a browser launch or attach.
|
||||
/// The launch's own session only owns the browser.
|
||||
pub(crate) fn is_page(session: &Session) -> bool {
|
||||
session.adapter == ADAPTER
|
||||
&& session.parent.is_some()
|
||||
&& matches!(session.launch_type.get().map(String::as_str), Some("pwa-chrome" | "pwa-msedge"))
|
||||
}
|
||||
|
||||
/// Waits until the adapter answered the `launch` of `session`. js-debug sets a page's session
|
||||
/// up between `configurationDone` and that answer; a request of its own that arrives then is
|
||||
/// answered and has no effect.
|
||||
pub(crate) async fn launched(session: &Session) -> bool {
|
||||
let mut launched = session.launched.subscribe();
|
||||
let answered = tokio::time::timeout(REQUEST_TIMEOUT, launched.wait_for(|answered| *answered)).await;
|
||||
matches!(answered, Ok(Ok(_)))
|
||||
}
|
||||
|
||||
impl DebugManager {
|
||||
/// One of js-debug's requests for pages, sent to the page `s` once its session has started.
|
||||
async fn js_page_request(&self, s: DebugSessionId, command: &str, arguments: Value) -> IdeResult<Value> {
|
||||
let session = self.session(s)?;
|
||||
if !is_page(&session) {
|
||||
return Err(IdeError::unavailable("this debug session is not a page in a browser"));
|
||||
}
|
||||
if !launched(&session).await {
|
||||
return Err(IdeError::unavailable("the page's debug session did not finish starting"));
|
||||
}
|
||||
session
|
||||
.conn
|
||||
.request_timeout(command, arguments, REQUEST_TIMEOUT)
|
||||
.await
|
||||
.map_err(|e| IdeError::unavailable(format!("the JavaScript debugger refused `{command}`: {e}")))
|
||||
}
|
||||
|
||||
/// Breakpoints on events (`listener:click`, `instrumentation:setTimeout`) and on requests
|
||||
/// whose URL contains one of `xhr`. The lists replace the page's previous ones.
|
||||
pub async fn js_set_custom_breakpoints(
|
||||
&self,
|
||||
s: DebugSessionId,
|
||||
ids: Vec<String>,
|
||||
xhr: Vec<String>,
|
||||
) -> IdeResult<()> {
|
||||
self.js_page_request(s, "setCustomBreakpoints", json!({ "ids": ids, "xhr": xhr })).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Starts recording the page's CPU profile; [`Self::js_stop_profile`] ends it.
|
||||
pub async fn js_start_profile(&self, s: DebugSessionId) -> IdeResult<()> {
|
||||
self.js_page_request(s, "startProfile", json!({ "type": "cpu" })).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Ends the recording and returns a new directory holding it as `page.cpuprofile`
|
||||
/// (what `ide-profiler` reads a directory of).
|
||||
pub async fn js_stop_profile(&self, s: DebugSessionId) -> IdeResult<PathBuf> {
|
||||
let session = self.session(s)?;
|
||||
// js-debug names the file it records into when the recording starts.
|
||||
let Some(recorded) = session.js_profile.lock().take() else {
|
||||
return Err(IdeError::invalid("no CPU profile is being recorded"));
|
||||
};
|
||||
self.js_page_request(s, "stopProfile", json!({})).await?;
|
||||
static HANDED_OVER: AtomicU32 = AtomicU32::new(0);
|
||||
let dir = std::env::temp_dir().join(format!(
|
||||
"openidea-js-profile-{}-{}-{}",
|
||||
std::process::id(),
|
||||
s.0,
|
||||
HANDED_OVER.fetch_add(1, Ordering::Relaxed)
|
||||
));
|
||||
let copied = tokio::task::spawn_blocking({
|
||||
let dir = dir.clone();
|
||||
move || {
|
||||
std::fs::create_dir_all(&dir)?;
|
||||
std::fs::copy(&recorded, dir.join("page.cpuprofile"))?;
|
||||
// The adapter's own copy is in the system's temporary directory: no use keeping two.
|
||||
let _ = std::fs::remove_file(&recorded);
|
||||
Ok::<_, std::io::Error>(())
|
||||
}
|
||||
})
|
||||
.await
|
||||
.map_err(|e| IdeError::internal(e.to_string()))?;
|
||||
copied.map_err(|e| IdeError::unavailable(format!("the recorded CPU profile cannot be read: {e}")))?;
|
||||
Ok(dir)
|
||||
}
|
||||
}
|
||||
|
||||
/// The browser events js-debug is asked to pass on (`enableNetworking`'s `mirrorEvents`).
|
||||
pub(crate) const NETWORK_EVENTS: [&str; 4] =
|
||||
["requestWillBeSent", "responseReceived", "loadingFinished", "loadingFailed"];
|
||||
|
||||
/// Response bodies shown are cut at this many bytes.
|
||||
pub const RESPONSE_BODY_LIMIT: usize = 1_048_576;
|
||||
|
||||
/// The body of a response, as text or as the browser's base64 for binary content.
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Type)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct JsResponseBody {
|
||||
pub body: String,
|
||||
pub base64_encoded: bool,
|
||||
/// The body was longer than [`RESPONSE_BODY_LIMIT`] and is cut there.
|
||||
pub truncated: bool,
|
||||
}
|
||||
|
||||
fn text(v: &Value, key: &str) -> Option<String> {
|
||||
v.get(key).and_then(Value::as_str).map(str::to_owned)
|
||||
}
|
||||
|
||||
fn headers(v: &Value) -> BTreeMap<String, String> {
|
||||
v.get("headers")
|
||||
.and_then(Value::as_object)
|
||||
.map(|h| h.iter().filter_map(|(k, v)| Some((k.clone(), v.as_str()?.to_owned()))).collect())
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
/// The frame a request is attributed to: the innermost one outside `node_modules`, looked
|
||||
/// for through the stacks that scheduled this one (an `await`, a timer), since a request sent
|
||||
/// through a library has the library innermost. A stack of library frames only gives its first.
|
||||
fn initiator(data: &Value) -> Option<JsInitiator> {
|
||||
let stack = data.pointer("/initiator/stack")?;
|
||||
let frames = |stack: &Value| stack.get("callFrames").and_then(Value::as_array).cloned().unwrap_or_default();
|
||||
let stacks = std::iter::successors(Some(stack), |stack| stack.get("parent")).take(32);
|
||||
let all: Vec<Value> = stacks.flat_map(frames).collect();
|
||||
let own = |frame: &&Value| {
|
||||
frame.get("url").and_then(Value::as_str).is_some_and(|url| !url.is_empty() && !url.contains("/node_modules/"))
|
||||
};
|
||||
let frame = all.iter().find(own).or(all.first())?;
|
||||
let number = |key: &str| frame.get(key).and_then(Value::as_u64).and_then(|n| u32::try_from(n).ok()).unwrap_or(0);
|
||||
Some(JsInitiator {
|
||||
function: text(frame, "functionName").unwrap_or_default(),
|
||||
url: text(frame, "url")?,
|
||||
line: number("lineNumber"),
|
||||
column: number("columnNumber"),
|
||||
})
|
||||
}
|
||||
|
||||
/// js-debug's `networkEvent` (`{event, data}`, `data` being the browser's own parameters of
|
||||
/// that event) as a [`JsNetworkEvent`]; `None` for events the Network view has no use for.
|
||||
pub(crate) fn network_event(body: &Value) -> Option<JsNetworkEvent> {
|
||||
let data = body.get("data")?;
|
||||
let kind = match body.get("event").and_then(Value::as_str)? {
|
||||
"requestWillBeSent" => JsNetworkKind::Request,
|
||||
"responseReceived" => JsNetworkKind::Response,
|
||||
"loadingFinished" => JsNetworkKind::Finished,
|
||||
"loadingFailed" => JsNetworkKind::Failed,
|
||||
_ => return None,
|
||||
};
|
||||
let part = match kind {
|
||||
JsNetworkKind::Request => data.get("request"),
|
||||
JsNetworkKind::Response => data.get("response"),
|
||||
_ => None,
|
||||
};
|
||||
let part = part.unwrap_or(&Value::Null);
|
||||
Some(JsNetworkEvent {
|
||||
kind,
|
||||
request_id: text(data, "requestId")?,
|
||||
// Seconds with a fraction.
|
||||
timestamp_ms: (data.get("timestamp").and_then(Value::as_f64).unwrap_or(0.0).max(0.0) * 1000.0).round() as u64,
|
||||
url: text(part, "url"),
|
||||
method: text(part, "method"),
|
||||
resource_type: text(data, "type"),
|
||||
status: part.get("status").and_then(Value::as_u64).and_then(|s| u16::try_from(s).ok()),
|
||||
mime_type: text(part, "mimeType"),
|
||||
headers: headers(part),
|
||||
encoded_length: data.get("encodedDataLength").and_then(Value::as_f64).map(|n| n.max(0.0) as u64),
|
||||
error_text: text(data, "errorText"),
|
||||
initiator: if kind == JsNetworkKind::Request { initiator(data) } else { None },
|
||||
})
|
||||
}
|
||||
|
||||
/// `networkCall getResponseBody`'s answer, cut at `limit` bytes on a character boundary.
|
||||
fn response_body(answer: &Value, limit: usize) -> JsResponseBody {
|
||||
let mut body = text(answer, "body").unwrap_or_default();
|
||||
let truncated = body.len() > limit;
|
||||
if truncated {
|
||||
let mut end = limit;
|
||||
while !body.is_char_boundary(end) {
|
||||
end -= 1;
|
||||
}
|
||||
body.truncate(end);
|
||||
}
|
||||
JsResponseBody {
|
||||
body,
|
||||
base64_encoded: answer.get("base64Encoded").and_then(Value::as_bool).unwrap_or(false),
|
||||
truncated,
|
||||
}
|
||||
}
|
||||
|
||||
/// The arguments of `enableNetworking`, which has a page report its requests
|
||||
/// ([`ide_core::event::DebugEvent::JsNetwork`]). A page session sends it as it starts.
|
||||
pub(crate) fn networking_arguments() -> Value {
|
||||
json!({ "mirrorEvents": NETWORK_EVENTS })
|
||||
}
|
||||
|
||||
impl DebugManager {
|
||||
/// The body of the response to `request_id`, while the browser still has it.
|
||||
pub async fn js_response_body(&self, s: DebugSessionId, request_id: String) -> IdeResult<JsResponseBody> {
|
||||
let call = json!({ "method": "getResponseBody", "params": { "requestId": request_id } });
|
||||
let answer = self.js_page_request(s, "networkCall", call).await?;
|
||||
Ok(response_body(&answer, RESPONSE_BODY_LIMIT))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn event(kind: &str, data: Value) -> Option<ide_core::event::JsNetworkEvent> {
|
||||
network_event(&json!({ "event": kind, "data": data }))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_request_carries_what_was_asked_and_who_asked() {
|
||||
let e = event(
|
||||
"requestWillBeSent",
|
||||
json!({
|
||||
"requestId": "76695.31", "timestamp": 611807.5, "type": "Fetch",
|
||||
"request": { "method": "GET", "url": "http://127.0.0.1:5195/src/main.ts", "headers": { "Accept": "*/*", "X-N": 1 } },
|
||||
"initiator": { "type": "script", "stack": { "callFrames": [
|
||||
{ "functionName": "load", "url": "http://127.0.0.1:5195/src/App.vue", "lineNumber": 9, "columnNumber": 2 }
|
||||
] } }
|
||||
}),
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(e.kind, JsNetworkKind::Request);
|
||||
assert_eq!((e.request_id.as_str(), e.timestamp_ms), ("76695.31", 611_807_500));
|
||||
assert_eq!(e.url.as_deref(), Some("http://127.0.0.1:5195/src/main.ts"));
|
||||
assert_eq!((e.method.as_deref(), e.resource_type.as_deref()), (Some("GET"), Some("Fetch")));
|
||||
// Only text headers are kept.
|
||||
assert_eq!(e.headers.len(), 1);
|
||||
assert_eq!(e.headers["Accept"], "*/*");
|
||||
let from = e.initiator.unwrap();
|
||||
assert_eq!((from.function.as_str(), from.line, from.column), ("load", 9, 2));
|
||||
assert_eq!(from.url, "http://127.0.0.1:5195/src/App.vue");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_request_made_through_a_library_is_asked_by_the_code_that_called_the_library() {
|
||||
let frame = |function: &str, url: &str| json!({ "functionName": function, "url": url, "lineNumber": 1, "columnNumber": 0 });
|
||||
let lib = "http://localhost:5173/node_modules/.vite/deps/http-client.js";
|
||||
let asked_by = |stack: Value| {
|
||||
let data = json!({ "requestId": "1", "timestamp": 1.0, "request": { "method": "GET", "url": "http://localhost/api" },
|
||||
"initiator": { "type": "script", "stack": stack } });
|
||||
event("requestWillBeSent", data).unwrap().initiator.unwrap().function
|
||||
};
|
||||
// The innermost frame is the library's; the project's caller is further out.
|
||||
let direct = json!({ "callFrames": [frame("dispatch", lib), frame("request", lib), frame("loadItems", "http://localhost:5173/src/api.ts")] });
|
||||
assert_eq!(asked_by(direct), "loadItems");
|
||||
// Across an `await`, the caller is in the stack that scheduled this one.
|
||||
let scheduled = json!({ "callFrames": [frame("dispatch", lib)],
|
||||
"parent": { "callFrames": [frame("then", lib)], "parent": { "callFrames": [frame("onMounted", "http://localhost:5173/src/App.vue")] } } });
|
||||
assert_eq!(asked_by(scheduled), "onMounted");
|
||||
// A library on its own: the frame that sent the request.
|
||||
assert_eq!(asked_by(json!({ "callFrames": [frame("dispatch", lib), frame("poll", lib)] })), "dispatch");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_response_a_finish_and_a_failure_carry_their_outcome() {
|
||||
let response = event(
|
||||
"responseReceived",
|
||||
json!({ "requestId": "7", "timestamp": 2.0, "type": "Script", "response": {
|
||||
"url": "http://localhost/a.js", "status": 200, "mimeType": "text/javascript",
|
||||
"headers": { "Content-Type": "text/javascript" }
|
||||
} }),
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(response.kind, JsNetworkKind::Response);
|
||||
assert_eq!((response.status, response.mime_type.as_deref()), (Some(200), Some("text/javascript")));
|
||||
assert_eq!(response.headers["Content-Type"], "text/javascript");
|
||||
|
||||
let finished =
|
||||
event("loadingFinished", json!({ "requestId": "7", "timestamp": 2.5, "encodedDataLength": 1234 })).unwrap();
|
||||
assert_eq!((finished.kind, finished.encoded_length), (JsNetworkKind::Finished, Some(1234)));
|
||||
|
||||
let failed =
|
||||
event("loadingFailed", json!({ "requestId": "8", "timestamp": 3.0, "errorText": "net::ERR_FAILED" }))
|
||||
.unwrap();
|
||||
assert_eq!((failed.kind, failed.error_text.as_deref()), (JsNetworkKind::Failed, Some("net::ERR_FAILED")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn events_that_name_no_request_or_are_not_mirrored_are_dropped() {
|
||||
assert!(event("requestWillBeSent", json!({ "timestamp": 1.0 })).is_none());
|
||||
assert!(event("dataReceived", json!({ "requestId": "7", "timestamp": 1.0 })).is_none());
|
||||
assert!(network_event(&json!({ "data": { "requestId": "7" } })).is_none());
|
||||
// A request made by the page's parser has no script behind it.
|
||||
let parser = event(
|
||||
"requestWillBeSent",
|
||||
json!({ "requestId": "9", "timestamp": 1.0, "request": { "method": "GET", "url": "http://localhost/" },
|
||||
"initiator": { "type": "parser" } }),
|
||||
)
|
||||
.unwrap();
|
||||
assert!(parser.initiator.is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_response_body_is_cut_at_the_limit_on_a_character() {
|
||||
let whole = response_body(&json!({ "body": "héllo", "base64Encoded": false }), 100);
|
||||
assert_eq!((whole.body.as_str(), whole.base64_encoded, whole.truncated), ("héllo", false, false));
|
||||
// `é` is two bytes: a cut inside it backs off to the character before.
|
||||
let cut = response_body(&json!({ "body": "héllo" }), 2);
|
||||
assert_eq!((cut.body.as_str(), cut.truncated), ("h", true));
|
||||
let binary = response_body(&json!({ "body": "AAEC", "base64Encoded": true }), 100);
|
||||
assert!(binary.base64_encoded && !binary.truncated);
|
||||
assert_eq!(response_body(&json!({}), 100).body, "");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
//! The class table counted by the debugger (JDWP `InstanceCounts`) where no histogram can be
|
||||
//! taken: JDK 8-16 while suspended, Windows, a remote JVM. Only while suspended, and slow on a
|
||||
//! large heap (seconds on JDK 17+).
|
||||
|
||||
use super::inspect::Call;
|
||||
use super::snapshot::diff_rows;
|
||||
use super::{ClassCounts, JvmMemoryResult, JvmMemorySnapshot, JvmMemorySource, JvmMemoryState, JvmMemoryUnavailable};
|
||||
use crate::session::Session;
|
||||
use ide_core::IdeResult;
|
||||
use ide_jvm_attach::HistogramRow;
|
||||
use ide_jvm_inspect::ClassCount;
|
||||
|
||||
/// Reasons the debugger may be able to work around; the others (the program exited, the JVM
|
||||
/// did not answer) would fail for it too.
|
||||
pub(super) fn falls_back(reason: &JvmMemoryUnavailable) -> bool {
|
||||
use JvmMemoryUnavailable as U;
|
||||
matches!(
|
||||
reason,
|
||||
U::UnsafeWhileSuspended { .. }
|
||||
| U::UnsupportedPlatform
|
||||
| U::AttachedJvmNotFound
|
||||
| U::ListenerNotRunning
|
||||
| U::ListenModeNotSupported
|
||||
| U::NoJvmProcess
|
||||
)
|
||||
}
|
||||
|
||||
/// `histogram` unless it is unavailable for a reason `session`'s inspector can work around.
|
||||
pub(super) async fn or_debugger_counts(
|
||||
state: &mut JvmMemoryState,
|
||||
histogram: Result<JvmMemorySnapshot, JvmMemoryUnavailable>,
|
||||
session: Option<&Session>,
|
||||
) -> IdeResult<JvmMemoryResult> {
|
||||
let reason = match histogram {
|
||||
Ok(snapshot) => return Ok(JvmMemoryResult::Ready { snapshot }),
|
||||
Err(reason) => reason,
|
||||
};
|
||||
let unavailable = |can_count_when_paused| Ok(JvmMemoryResult::Unavailable { reason, can_count_when_paused });
|
||||
let Some(session) = session.filter(|_| falls_back(&reason)) else {
|
||||
return unavailable(false);
|
||||
};
|
||||
let call = match Call::begin(session, None).await {
|
||||
Ok(call) => call,
|
||||
Err(super::JvmInspectUnavailable::NotSuspended) => return unavailable(true),
|
||||
Err(_) => return unavailable(false),
|
||||
};
|
||||
let started = std::time::Instant::now();
|
||||
let result = call.run(call.inspector.class_counts(&call.target)).await;
|
||||
match call.finish(result) {
|
||||
Ok(counts) => {
|
||||
let elapsed_ms = u32::try_from(started.elapsed().as_millis()).unwrap_or(u32::MAX);
|
||||
let previous = state.baseline(JvmMemorySource::Debugger);
|
||||
let (snapshot, counts) = debugger_snapshot(counts, previous.as_deref(), elapsed_ms);
|
||||
state.remember(JvmMemorySource::Debugger, counts);
|
||||
Ok(JvmMemoryResult::Ready { snapshot })
|
||||
}
|
||||
Err(e) => {
|
||||
tracing::debug!("debugger class counts: {e}");
|
||||
let running = !session.suspended.get();
|
||||
unavailable(running && session.jvm_inspector.lock().is_some())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Classes without instances are left out; most instances first.
|
||||
pub(super) fn debugger_snapshot(
|
||||
counts: Vec<ClassCount>,
|
||||
previous: Option<&ClassCounts>,
|
||||
elapsed_ms: u32,
|
||||
) -> (JvmMemorySnapshot, ClassCounts) {
|
||||
let rows = counts
|
||||
.into_iter()
|
||||
.filter(|c| c.instances > 0)
|
||||
.map(|c| HistogramRow { name: c.name, module: None, instances: c.instances, bytes: 0 })
|
||||
.collect();
|
||||
let (mut rows, counts) = diff_rows(rows, previous);
|
||||
for row in &mut rows {
|
||||
row.delta_bytes = None;
|
||||
}
|
||||
rows.sort_by(|a, b| b.instances.cmp(&a.instances).then_with(|| a.name.cmp(&b.name)));
|
||||
let snapshot = JvmMemorySnapshot {
|
||||
total_instances: rows.iter().map(|r| r.instances).sum(),
|
||||
total_bytes: 0,
|
||||
rows,
|
||||
java_version: String::new(),
|
||||
taken_while_suspended: true,
|
||||
has_baseline: previous.is_some(),
|
||||
elapsed_ms,
|
||||
source: JvmMemorySource::Debugger,
|
||||
};
|
||||
(snapshot, counts)
|
||||
}
|
||||
@@ -0,0 +1,293 @@
|
||||
//! A heap dump of a debugged JVM, written over its attach listener into a snapshot directory.
|
||||
|
||||
use super::snapshot::{AttachProbe, JdkProbe, attach_failure, gone, pick_jvm_pid};
|
||||
use super::{
|
||||
JvmMemoryState, JvmMemoryUnavailable, JvmTarget, histogram_pid, keep_found, lookup, resolve_jvm,
|
||||
safe_while_suspended, session_target,
|
||||
};
|
||||
use crate::session::Session;
|
||||
use ide_core::{CancellationToken, IdeError, IdeResult};
|
||||
use ide_jvm_attach::{AttachError, HeapDumpReply};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use specta::Type;
|
||||
use std::future::Future;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::Arc;
|
||||
|
||||
const DUMP_FILE: &str = "dump.hprof";
|
||||
/// For a refusal whose reply holds no reason: empty, or only the line announcing the dump.
|
||||
const NO_REASON: &str = "the JVM wrote no heap dump and gave no reason";
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Type)]
|
||||
#[serde(tag = "kind", rename_all = "camelCase", rename_all_fields = "camelCase")]
|
||||
pub enum JvmHeapUnavailable {
|
||||
/// The reason a class table could not be read applies to the dump too.
|
||||
Memory {
|
||||
reason: JvmMemoryUnavailable,
|
||||
},
|
||||
/// Bytes.
|
||||
NotEnoughDiskSpace {
|
||||
needed: u64,
|
||||
available: u64,
|
||||
},
|
||||
DumpFailed {
|
||||
message: String,
|
||||
},
|
||||
DumpTimedOut {
|
||||
seconds: u32,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct JvmHeapDump {
|
||||
pub path: PathBuf,
|
||||
/// The class histogram taken just before the dump.
|
||||
pub histogram: Vec<ide_heap::HistogramEntry>,
|
||||
/// Raw `VM.flags`; empty when the JVM did not answer.
|
||||
pub vm_flags: String,
|
||||
/// 0 when unknown.
|
||||
pub java_major: u32,
|
||||
/// Empty when unknown.
|
||||
pub java_version: String,
|
||||
pub pid: u32,
|
||||
pub taken_while_suspended: bool,
|
||||
pub dump_ms: u32,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub enum JvmHeapOutcome {
|
||||
Ready(JvmHeapDump),
|
||||
/// The snapshot directory has been deleted.
|
||||
Unavailable(JvmHeapUnavailable),
|
||||
/// The snapshot directory has been deleted.
|
||||
Cancelled,
|
||||
}
|
||||
|
||||
pub(crate) async fn session_heap_dump(
|
||||
session: Arc<Session>,
|
||||
dir: PathBuf,
|
||||
cancel: CancellationToken,
|
||||
) -> IdeResult<JvmHeapOutcome> {
|
||||
let launched = histogram_pid(&session);
|
||||
let capture = {
|
||||
let (dir, cancel) = (dir.clone(), cancel.clone());
|
||||
async move {
|
||||
let target = session_target(&session, launched);
|
||||
let probe = |suspended| -> Arc<dyn AttachProbe> { Arc::new(JdkProbe { suspended }) };
|
||||
let stopped = session.clone();
|
||||
let suspended = move || capture_suspended(&stopped);
|
||||
heap_dump_with(&session.jvm_memory, suspended, probe, target, dir, cancel).await
|
||||
}
|
||||
};
|
||||
detached(capture, dir, cancel).await
|
||||
}
|
||||
|
||||
/// The JVM can be suspended before the adapter reports the stop, so a session with a JDWP link
|
||||
/// also counts the link's view.
|
||||
pub(super) fn capture_suspended(session: &Session) -> bool {
|
||||
session.suspended.get()
|
||||
|| session
|
||||
.jdwp_link
|
||||
.lock()
|
||||
.as_ref()
|
||||
.is_some_and(|link| matches!(link.state(), ide_jdwp::LinkState::Suspended { .. }))
|
||||
}
|
||||
|
||||
/// Runs `capture` in a task of its own, so a caller that stops waiting cannot release the
|
||||
/// session's lock while the JVM is still writing the dump and let a class-table request run
|
||||
/// next to it. On `cancel` returns at once and deletes `dir`.
|
||||
pub(super) async fn detached(
|
||||
capture: impl Future<Output = IdeResult<JvmHeapOutcome>> + Send + 'static,
|
||||
dir: PathBuf,
|
||||
cancel: CancellationToken,
|
||||
) -> IdeResult<JvmHeapOutcome> {
|
||||
let task = tokio::spawn(capture);
|
||||
tokio::select! {
|
||||
joined = task => joined.map_err(|e| IdeError::internal(format!("JVM heap dump failed: {e}")))?,
|
||||
() = cancel.cancelled() => {
|
||||
discard_later(dir).await;
|
||||
Ok(JvmHeapOutcome::Cancelled)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Holds `state`'s lock throughout, as a class-table request does; `suspended` and then `probe`
|
||||
/// are read once it is held, and `suspended` again by [`take_heap_dump`]. Anything but `Ready`
|
||||
/// deletes `dir`.
|
||||
pub(super) async fn heap_dump_with(
|
||||
state: &tokio::sync::Mutex<JvmMemoryState>,
|
||||
suspended: impl Fn() -> bool + Send + Sync + 'static,
|
||||
probe: impl FnOnce(bool) -> Arc<dyn AttachProbe>,
|
||||
target: JvmTarget<'_>,
|
||||
dir: PathBuf,
|
||||
cancel: CancellationToken,
|
||||
) -> IdeResult<JvmHeapOutcome> {
|
||||
// The capture's histogram never becomes the Memory view's baseline, so the state is unused.
|
||||
let _turn = state.lock().await;
|
||||
let outcome = capture(suspended, probe, target, dir.clone(), cancel).await;
|
||||
if !matches!(outcome, Ok(JvmHeapOutcome::Ready(_))) {
|
||||
discard_later(dir).await;
|
||||
}
|
||||
outcome
|
||||
}
|
||||
|
||||
async fn capture(
|
||||
suspended_now: impl Fn() -> bool + Send + Sync + 'static,
|
||||
probe: impl FnOnce(bool) -> Arc<dyn AttachProbe>,
|
||||
target: JvmTarget<'_>,
|
||||
dir: PathBuf,
|
||||
cancel: CancellationToken,
|
||||
) -> IdeResult<JvmHeapOutcome> {
|
||||
if cancel.is_cancelled() {
|
||||
return Ok(JvmHeapOutcome::Cancelled);
|
||||
}
|
||||
let suspended = suspended_now();
|
||||
let lookup = match lookup(target) {
|
||||
Ok(lookup) => lookup,
|
||||
Err(reason) => return Ok(memory(reason)),
|
||||
};
|
||||
let probe = probe(suspended);
|
||||
let (found, outcome) = tokio::task::spawn_blocking(move || match resolve_jvm(lookup, &*probe, suspended) {
|
||||
Ok((tree, found)) => (found, take_heap_dump(&*probe, &tree, suspended, &suspended_now, &dir, &cancel)),
|
||||
Err(reason) => (None, Ok(memory(reason))),
|
||||
})
|
||||
.await
|
||||
.map_err(|e| IdeError::internal(format!("JVM heap dump failed: {e}")))?;
|
||||
keep_found(target, found);
|
||||
outcome
|
||||
}
|
||||
|
||||
/// `tree` starts with the session's reported pid, followed by its descendants. `suspended` is
|
||||
/// the stop read when the capture began; `suspended_now` reads it again. Anything but `Ready`
|
||||
/// deletes `dir`.
|
||||
pub(crate) fn take_heap_dump(
|
||||
probe: &dyn AttachProbe,
|
||||
tree: &[i32],
|
||||
suspended: bool,
|
||||
suspended_now: &dyn Fn() -> bool,
|
||||
dir: &Path,
|
||||
cancel: &CancellationToken,
|
||||
) -> IdeResult<JvmHeapOutcome> {
|
||||
let outcome = dump(probe, tree, suspended, suspended_now, dir, cancel);
|
||||
if !matches!(outcome, Ok(JvmHeapOutcome::Ready(_))) {
|
||||
discard(dir);
|
||||
}
|
||||
outcome
|
||||
}
|
||||
|
||||
fn dump(
|
||||
probe: &dyn AttachProbe,
|
||||
tree: &[i32],
|
||||
suspended: bool,
|
||||
suspended_now: &dyn Fn() -> bool,
|
||||
dir: &Path,
|
||||
cancel: &CancellationToken,
|
||||
) -> IdeResult<JvmHeapOutcome> {
|
||||
use JvmHeapUnavailable as H;
|
||||
use JvmMemoryUnavailable as U;
|
||||
let Some(root) = tree.first().and_then(|&p| u32::try_from(p).ok()) else {
|
||||
return Ok(memory(U::NoJvmProcess));
|
||||
};
|
||||
let Some(jvm) = pick_jvm_pid(tree, |p| probe.has_listener(p)) else {
|
||||
return Ok(memory(gone(probe, root)));
|
||||
};
|
||||
|
||||
let version = probe.version(jvm);
|
||||
let java_major = version.as_ref().map_or(0, |v| v.major);
|
||||
let safe = safe_while_suspended(version.as_ref());
|
||||
let unsafe_stop = || Ok(memory(U::UnsafeWhileSuspended { java_major }));
|
||||
if suspended && !safe {
|
||||
return unsafe_stop();
|
||||
}
|
||||
// The program may stop during the histogram, and a stopped JDK below 17 hangs on the next
|
||||
// request until it resumes.
|
||||
let stopped_since = || !safe && suspended_now();
|
||||
|
||||
let text = match probe.histogram(jvm) {
|
||||
Ok(text) => text,
|
||||
Err(e) => return attach_failure(probe, jvm, e, "class histogram").map(memory),
|
||||
};
|
||||
let histogram: Vec<ide_heap::HistogramEntry> = ide_jvm_attach::parse_class_histogram(&text)
|
||||
.rows
|
||||
.into_iter()
|
||||
.map(|r| ide_heap::HistogramEntry { name: r.name, instances: r.instances, bytes: r.bytes })
|
||||
.collect();
|
||||
let (instances, bytes) =
|
||||
histogram.iter().fold((0u64, 0u64), |(i, b), e| (i.saturating_add(e.instances), b.saturating_add(e.bytes)));
|
||||
let needed = ide_heap::capture_need(bytes, instances);
|
||||
if let Some(available) = probe.free_bytes(dir)
|
||||
&& available < needed
|
||||
{
|
||||
return Ok(JvmHeapOutcome::Unavailable(H::NotEnoughDiskSpace { needed, available }));
|
||||
}
|
||||
|
||||
if stopped_since() {
|
||||
return unsafe_stop();
|
||||
}
|
||||
// Without flags the index assumes the default object layout.
|
||||
let vm_flags = probe.vm_flags(jvm).unwrap_or_else(|e| {
|
||||
tracing::debug!("VM.flags of JVM {jvm}: {e}");
|
||||
String::new()
|
||||
});
|
||||
if cancel.is_cancelled() {
|
||||
return Ok(JvmHeapOutcome::Cancelled);
|
||||
}
|
||||
if stopped_since() {
|
||||
return unsafe_stop();
|
||||
}
|
||||
|
||||
let path = dir.join(DUMP_FILE);
|
||||
let timeout = ide_jvm_attach::dump_timeout(bytes);
|
||||
let started = std::time::Instant::now();
|
||||
let reply = probe.heap_dump(jvm, &path, timeout);
|
||||
let dump_ms = u32::try_from(started.elapsed().as_millis()).unwrap_or(u32::MAX);
|
||||
if cancel.is_cancelled() {
|
||||
return Ok(JvmHeapOutcome::Cancelled);
|
||||
}
|
||||
let failed = |message: String| Ok(JvmHeapOutcome::Unavailable(H::DumpFailed { message }));
|
||||
match reply {
|
||||
Ok(HeapDumpReply::Created { .. }) => {
|
||||
if !std::fs::metadata(&path).is_ok_and(|m| m.is_file() && m.len() > 0) {
|
||||
return failed(format!("the JVM reported a heap dump, but {} is missing or empty", path.display()));
|
||||
}
|
||||
Ok(JvmHeapOutcome::Ready(JvmHeapDump {
|
||||
path,
|
||||
histogram,
|
||||
vm_flags,
|
||||
java_major,
|
||||
java_version: version.map(|v| v.text).unwrap_or_default(),
|
||||
pid: jvm,
|
||||
taken_while_suspended: suspended,
|
||||
dump_ms,
|
||||
}))
|
||||
}
|
||||
Ok(HeapDumpReply::Refused { message }) if message.is_empty() || message.starts_with("Dumping heap to") => {
|
||||
failed(NO_REASON.into())
|
||||
}
|
||||
Ok(HeapDumpReply::Refused { message }) => failed(message),
|
||||
Err(AttachError::Timeout) => {
|
||||
let seconds = u32::try_from(timeout.as_secs()).unwrap_or(u32::MAX);
|
||||
Ok(JvmHeapOutcome::Unavailable(H::DumpTimedOut { seconds }))
|
||||
}
|
||||
// A running JDK rejects an argument with a non-zero result code.
|
||||
Err(AttachError::Command { message, .. }) => failed(message),
|
||||
Err(e) => attach_failure(probe, jvm, e, "heap dump").map(memory),
|
||||
}
|
||||
}
|
||||
|
||||
fn memory(reason: JvmMemoryUnavailable) -> JvmHeapOutcome {
|
||||
JvmHeapOutcome::Unavailable(JvmHeapUnavailable::Memory { reason })
|
||||
}
|
||||
|
||||
fn discard(dir: &Path) {
|
||||
if let Err(e) = std::fs::remove_dir_all(dir)
|
||||
&& e.kind() != std::io::ErrorKind::NotFound
|
||||
{
|
||||
tracing::warn!("could not delete heap snapshot directory {}: {e}", dir.display());
|
||||
}
|
||||
}
|
||||
|
||||
/// A dump may be large, so it is deleted off the async threads.
|
||||
async fn discard_later(dir: PathBuf) {
|
||||
let _ = tokio::task::spawn_blocking(move || discard(&dir)).await;
|
||||
}
|
||||
@@ -0,0 +1,251 @@
|
||||
//! A suspended JVM's instances, fields and referrers, read through the session's
|
||||
//! [`JvmInspector`]. An object is only valid within the stop it was read in.
|
||||
|
||||
use super::launch_pid;
|
||||
use crate::session::Session;
|
||||
use ide_core::event::{DebugEvent, JvmBackendState};
|
||||
use ide_core::{IdeError, IdeResult};
|
||||
use ide_jvm_inspect::{
|
||||
DEFAULT_REFERRERS, InspectError, InspectFuture, JvmField, JvmInspector, JvmReferrer, JvmValue, MAX_INSTANCES,
|
||||
MAX_REFERRERS, ObjectId, VmTarget,
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use specta::Type;
|
||||
use std::sync::Arc;
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize, Type)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct JvmObjectRef {
|
||||
/// The `generation` of the [`JvmInstancesResult`] the object came from.
|
||||
pub generation: u64,
|
||||
pub id: ObjectId,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Type)]
|
||||
#[serde(tag = "kind", rename_all = "camelCase", rename_all_fields = "camelCase")]
|
||||
pub enum JvmInspectUnavailable {
|
||||
/// The session has no inspector: not a JVM session, not ready yet, or turned off.
|
||||
NoBackend,
|
||||
/// The debug adapter lacks what the inspector needs; turned off for the session.
|
||||
Unsupported {
|
||||
detail: String,
|
||||
},
|
||||
NotSuspended,
|
||||
/// The object is from an earlier stop, or the program stopped or resumed meanwhile.
|
||||
Stale,
|
||||
Collected,
|
||||
ClassNotLoaded,
|
||||
VmNotFound,
|
||||
/// Transient: ask again.
|
||||
TimedOut,
|
||||
BackendClosed,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Type)]
|
||||
#[serde(tag = "status", rename_all = "camelCase", rename_all_fields = "camelCase")]
|
||||
pub enum JvmInstancesResult {
|
||||
Ready {
|
||||
/// Goes into the [`JvmObjectRef`] of each instance.
|
||||
generation: u64,
|
||||
instances: Vec<JvmValue>,
|
||||
/// The limit was reached; there may be more.
|
||||
truncated: bool,
|
||||
},
|
||||
Unavailable {
|
||||
reason: JvmInspectUnavailable,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Type)]
|
||||
#[serde(tag = "status", rename_all = "camelCase", rename_all_fields = "camelCase")]
|
||||
pub enum JvmFieldsResult {
|
||||
Ready { fields: Vec<JvmField> },
|
||||
Unavailable { reason: JvmInspectUnavailable },
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Type)]
|
||||
#[serde(tag = "status", rename_all = "camelCase", rename_all_fields = "camelCase")]
|
||||
pub enum JvmReferrersResult {
|
||||
Ready { referrers: Vec<JvmReferrer>, truncated: bool },
|
||||
Unavailable { reason: JvmInspectUnavailable },
|
||||
}
|
||||
|
||||
/// The session's JVM for the inspector: java-debug's VM is found by the launched program's pid
|
||||
/// or by the port of an attach (the thread lists of two JVMs can be identical).
|
||||
pub(crate) fn vm_target(s: &Session) -> VmTarget {
|
||||
let (pid, attach) = if s.request == "launch" {
|
||||
(u32::try_from(launch_pid(s)).ok().filter(|&p| p > 0), None)
|
||||
} else {
|
||||
(None, s.attach_target.get())
|
||||
};
|
||||
VmTarget {
|
||||
pid,
|
||||
attach_host: attach.map(|(host, _)| host.clone()).filter(|h| !h.is_empty()),
|
||||
attach_port: attach.map(|&(_, port)| port),
|
||||
generation: s.suspended.ticket(),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn set_inspector(s: &Session, inspector: Arc<dyn JvmInspector>) {
|
||||
*s.jvm_inspector.lock() = Some(inspector);
|
||||
s.emit(DebugEvent::Capabilities);
|
||||
}
|
||||
|
||||
pub(crate) async fn instances(s: &Session, class_name: &str, limit: u32) -> IdeResult<JvmInstancesResult> {
|
||||
let limit = limit.clamp(1, MAX_INSTANCES);
|
||||
let call = match Call::begin(s, None).await {
|
||||
Ok(call) => call,
|
||||
Err(reason) => return Ok(JvmInstancesResult::Unavailable { reason }),
|
||||
};
|
||||
let generation = call.target.generation;
|
||||
let result = call.run(call.inspector.instances(&call.target, class_name, limit)).await;
|
||||
Ok(match settle(call.finish(result))? {
|
||||
Ok(instances) => {
|
||||
JvmInstancesResult::Ready { generation, truncated: instances.len() >= limit as usize, instances }
|
||||
}
|
||||
Err(reason) => JvmInstancesResult::Unavailable { reason },
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) async fn fields(s: &Session, object: JvmObjectRef) -> IdeResult<JvmFieldsResult> {
|
||||
let call = match Call::begin(s, Some(object.generation)).await {
|
||||
Ok(call) => call,
|
||||
Err(reason) => return Ok(JvmFieldsResult::Unavailable { reason }),
|
||||
};
|
||||
let result = call.run(call.inspector.fields(&call.target, object.id)).await;
|
||||
Ok(match settle(call.finish(result))? {
|
||||
Ok(fields) => JvmFieldsResult::Ready { fields },
|
||||
Err(reason) => JvmFieldsResult::Unavailable { reason },
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) async fn referrers(s: &Session, object: JvmObjectRef, limit: u32) -> IdeResult<JvmReferrersResult> {
|
||||
let limit = if limit == 0 { DEFAULT_REFERRERS } else { limit.min(MAX_REFERRERS) };
|
||||
let call = match Call::begin(s, Some(object.generation)).await {
|
||||
Ok(call) => call,
|
||||
Err(reason) => return Ok(JvmReferrersResult::Unavailable { reason }),
|
||||
};
|
||||
let result = call.run(call.inspector.referrers(&call.target, object.id, limit)).await;
|
||||
Ok(match settle(call.finish(result))? {
|
||||
Ok(referrers) => JvmReferrersResult::Ready { truncated: referrers.len() >= limit as usize, referrers },
|
||||
Err(reason) => JvmReferrersResult::Unavailable { reason },
|
||||
})
|
||||
}
|
||||
|
||||
/// One inspector request. Holds the session's inspect lock and a clone of its inspector, which
|
||||
/// [`Call::run`] lets go of when the session ends.
|
||||
pub(super) struct Call<'s> {
|
||||
session: &'s Session,
|
||||
pub(super) inspector: Arc<dyn JvmInspector>,
|
||||
pub(super) target: VmTarget,
|
||||
_turn: tokio::sync::MutexGuard<'s, ()>,
|
||||
}
|
||||
|
||||
impl<'s> Call<'s> {
|
||||
/// `object_generation`: the stop the request's object was read in.
|
||||
pub(super) async fn begin(
|
||||
s: &'s Session,
|
||||
object_generation: Option<u64>,
|
||||
) -> Result<Call<'s>, JvmInspectUnavailable> {
|
||||
// Checked under the lock: a request queued behind another must see the state it runs in.
|
||||
let turn = s.inspect_lock.lock().await;
|
||||
let Some(inspector) = s.jvm_inspector.lock().clone() else {
|
||||
return Err(JvmInspectUnavailable::NoBackend);
|
||||
};
|
||||
if !s.suspended.get() {
|
||||
return Err(JvmInspectUnavailable::NotSuspended);
|
||||
}
|
||||
let target = vm_target(s);
|
||||
if object_generation.is_some_and(|g| g != target.generation) {
|
||||
return Err(JvmInspectUnavailable::Stale);
|
||||
}
|
||||
Ok(Call { session: s, inspector, target, _turn: turn })
|
||||
}
|
||||
|
||||
pub(super) async fn run<T>(&self, request: InspectFuture<'_, T>) -> Result<T, InspectError> {
|
||||
tokio::select! {
|
||||
result = request => result,
|
||||
() = self.session.ended.cancelled() => Err(InspectError::Closed),
|
||||
}
|
||||
}
|
||||
|
||||
/// The result unless the program stopped or resumed while it was read: a request of several
|
||||
/// commands can straddle a stop. `Unsupported` turns the backend off.
|
||||
pub(super) fn finish<T>(self, result: Result<T, InspectError>) -> Result<T, InspectError> {
|
||||
let s = self.session;
|
||||
match result {
|
||||
Err(InspectError::Unsupported(detail)) => {
|
||||
turn_off(s, &self.inspector, &detail);
|
||||
Err(InspectError::Unsupported(detail))
|
||||
}
|
||||
_ if !s.suspended.get() || s.suspended.ticket() != self.target.generation => Err(InspectError::Stale),
|
||||
result => result,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// An adapter that lacks what the inspector needs will not gain it during the session.
|
||||
fn turn_off(s: &Session, inspector: &Arc<dyn JvmInspector>, detail: &str) {
|
||||
let removed = {
|
||||
let mut slot = s.jvm_inspector.lock();
|
||||
// Not one installed since this request began.
|
||||
let current = slot.as_ref().is_some_and(|i| std::ptr::addr_eq(Arc::as_ptr(i), Arc::as_ptr(inspector)));
|
||||
current && slot.take().is_some()
|
||||
};
|
||||
if removed {
|
||||
s.emit(DebugEvent::JvmBackend { state: JvmBackendState::Unsupported { detail: detail.to_owned() } });
|
||||
s.emit(DebugEvent::Capabilities);
|
||||
}
|
||||
}
|
||||
|
||||
/// A failure the inspector could not classify is an error, not a reason shown to the user.
|
||||
fn settle<T>(result: Result<T, InspectError>) -> IdeResult<Result<T, JvmInspectUnavailable>> {
|
||||
use JvmInspectUnavailable as U;
|
||||
let error = match result {
|
||||
Ok(value) => return Ok(Ok(value)),
|
||||
Err(error) => error,
|
||||
};
|
||||
Ok(Err(match error {
|
||||
InspectError::NotSuspended => U::NotSuspended,
|
||||
InspectError::Stale => U::Stale,
|
||||
InspectError::Collected => U::Collected,
|
||||
InspectError::ClassNotLoaded => U::ClassNotLoaded,
|
||||
InspectError::VmNotFound => U::VmNotFound,
|
||||
InspectError::Unsupported(detail) => U::Unsupported { detail },
|
||||
// Also the Kotlin link's `Busy`: the backend stays.
|
||||
InspectError::TimedOut => U::TimedOut,
|
||||
InspectError::Closed => U::BackendClosed,
|
||||
InspectError::Failed(message) => return Err(IdeError::internal(format!("JVM inspector: {message}"))),
|
||||
}))
|
||||
}
|
||||
|
||||
/// Reports a Kotlin link that stops inspecting or closes. Holds neither the link nor the
|
||||
/// session: it ends once the session drops the link, and never drops it itself (after
|
||||
/// `VirtualMachine.Dispose` the link is closed but still forwarding the reply).
|
||||
pub(crate) fn watch_link(s: &Arc<Session>, link: &ide_jdwp::JdwpLink) {
|
||||
let mut states = link.subscribe();
|
||||
let session = Arc::downgrade(s);
|
||||
tokio::spawn(async move {
|
||||
let mut reported = None;
|
||||
loop {
|
||||
let state = match &*states.borrow_and_update() {
|
||||
ide_jdwp::LinkState::Degraded(detail) => Some(JvmBackendState::Degraded { detail: detail.clone() }),
|
||||
ide_jdwp::LinkState::Closed => Some(JvmBackendState::Closed),
|
||||
_ => None,
|
||||
};
|
||||
if let Some(state) = state
|
||||
&& reported.as_ref() != Some(&state)
|
||||
{
|
||||
let Some(s) = session.upgrade() else { return };
|
||||
if s.ended.is_cancelled() || s.terminated.load(std::sync::atomic::Ordering::Acquire) {
|
||||
return;
|
||||
}
|
||||
s.emit(DebugEvent::JvmBackend { state: state.clone() });
|
||||
reported = Some(state);
|
||||
}
|
||||
if states.changed().await.is_err() {
|
||||
return;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,338 @@
|
||||
//! Class-table snapshots of a debugged JVM, read over the HotSpot attach listener or, where
|
||||
//! that is unavailable, counted by the debugger; and the JVM's objects through the debugger.
|
||||
|
||||
mod fallback;
|
||||
mod heap;
|
||||
mod inspect;
|
||||
mod scan;
|
||||
mod snapshot;
|
||||
#[cfg(test)]
|
||||
pub(crate) mod test_support;
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
use crate::session::Session;
|
||||
use crate::session::Suspension;
|
||||
use ide_core::IdeResult;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use specta::Type;
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
use std::sync::atomic::{AtomicI64, Ordering};
|
||||
use std::time::Duration;
|
||||
|
||||
pub use heap::*;
|
||||
pub use inspect::{JvmFieldsResult, JvmInspectUnavailable, JvmInstancesResult, JvmObjectRef, JvmReferrersResult};
|
||||
pub(crate) use inspect::{fields, instances, referrers, set_inspector, watch_link};
|
||||
#[cfg(all(test, unix))]
|
||||
pub(crate) use scan::watch_for_attached_jvm;
|
||||
pub(crate) use scan::{JvmFinder, attach_target, find_attached_jvm, find_local_jvm, local_jdwp_port};
|
||||
use snapshot::{AttachProbe, JdkProbe, take_snapshot};
|
||||
|
||||
pub const JVM_MEMORY_ADAPTERS: &[&str] = &["java-debug", crate::kotlin::ADAPTER, crate::kotlin_lsp::ADAPTER];
|
||||
pub const HISTOGRAM_TIMEOUT: Duration = Duration::from_secs(10);
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Type)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct JvmClassRow {
|
||||
pub name: String,
|
||||
pub module: Option<String>,
|
||||
pub instances: u64,
|
||||
pub bytes: u64,
|
||||
/// Change since the previous snapshot; `None` on the first one and for ambiguous rows.
|
||||
pub delta_instances: Option<i64>,
|
||||
pub delta_bytes: Option<i64>,
|
||||
/// Several classes share this name (different class loaders), so it has no delta.
|
||||
pub ambiguous: bool,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Type)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct JvmMemorySnapshot {
|
||||
/// Largest `bytes` first.
|
||||
pub rows: Vec<JvmClassRow>,
|
||||
pub total_instances: u64,
|
||||
pub total_bytes: u64,
|
||||
/// Empty when unknown.
|
||||
pub java_version: String,
|
||||
pub taken_while_suspended: bool,
|
||||
/// A previous snapshot from the same source exists; deltas are against it.
|
||||
pub has_baseline: bool,
|
||||
/// Wall time of the histogram or count request.
|
||||
pub elapsed_ms: u32,
|
||||
pub source: JvmMemorySource,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize, Type)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub enum JvmMemorySource {
|
||||
/// `GC.class_histogram -all`: sizes, and objects not yet collected.
|
||||
Histogram,
|
||||
/// JDWP `InstanceCounts`: reachable objects only, no sizes (`bytes` is 0).
|
||||
Debugger,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize, Type)]
|
||||
#[serde(tag = "kind", rename_all = "camelCase", rename_all_fields = "camelCase")]
|
||||
pub enum JvmMemoryUnavailable {
|
||||
/// An attach session whose JVM is remote, or local but not found by its JDWP port.
|
||||
AttachedJvmNotFound,
|
||||
/// An attach through the IDE's listen-mode relay: the JVM connected to the IDE, so it has
|
||||
/// no JDWP listener to be found by.
|
||||
ListenModeNotSupported,
|
||||
UnsupportedPlatform,
|
||||
NoJvmProcess,
|
||||
ListenerNotRunning,
|
||||
/// `java_major` is 0 when the version is unknown.
|
||||
UnsafeWhileSuspended {
|
||||
java_major: u32,
|
||||
},
|
||||
ProgramExited,
|
||||
TimedOut,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, Type)]
|
||||
#[serde(tag = "status", rename_all = "camelCase", rename_all_fields = "camelCase")]
|
||||
pub enum JvmMemoryResult {
|
||||
Ready {
|
||||
snapshot: JvmMemorySnapshot,
|
||||
},
|
||||
Unavailable {
|
||||
reason: JvmMemoryUnavailable,
|
||||
/// The debugger could count the classes if the program were paused.
|
||||
can_count_when_paused: bool,
|
||||
},
|
||||
}
|
||||
|
||||
/// Instances and bytes per class name, for the next snapshot's deltas; `None` for a name
|
||||
/// several classes shared, so it gets no delta next time either.
|
||||
type ClassCounts = HashMap<String, Option<(u64, u64)>>;
|
||||
|
||||
/// Per-session baseline; its mutex also keeps one class-table request per session at a time.
|
||||
#[derive(Default)]
|
||||
pub struct JvmMemoryState {
|
||||
/// Shared with the blocking task, so a task that panics leaves it in place.
|
||||
previous: Option<(JvmMemorySource, Arc<ClassCounts>)>,
|
||||
}
|
||||
|
||||
impl JvmMemoryState {
|
||||
/// The previous snapshot's counts if it came from `source`: counts from the other source
|
||||
/// are not comparable (only reachable objects versus all of them).
|
||||
pub(crate) fn baseline(&self, source: JvmMemorySource) -> Option<Arc<ClassCounts>> {
|
||||
self.previous.as_ref().filter(|(s, _)| *s == source).map(|(_, counts)| counts.clone())
|
||||
}
|
||||
|
||||
pub(crate) fn remember(&mut self, source: JvmMemorySource, counts: ClassCounts) {
|
||||
self.previous = Some((source, Arc::new(counts)));
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) async fn session_classes(session: Arc<Session>) -> IdeResult<JvmMemoryResult> {
|
||||
until_located(&session).await;
|
||||
let launched = histogram_pid(&session);
|
||||
// Detached, so a caller that gives up cannot release the lock while the JVM is still
|
||||
// answering and let a second histogram request run next to it.
|
||||
tokio::spawn(async move {
|
||||
let target = session_target(&session, launched);
|
||||
let probe = |suspended| -> Arc<dyn AttachProbe> { Arc::new(JdkProbe { suspended }) };
|
||||
snapshot(&session.jvm_memory, &session.suspended, probe, target, Some(&session)).await
|
||||
})
|
||||
.await
|
||||
.map_err(|e| ide_core::IdeError::internal(format!("JVM memory snapshot failed: {e}")))?
|
||||
}
|
||||
|
||||
/// Bounds the wait of [`until_located`]; `configure` has its own timeouts, this only guards them.
|
||||
const LOCATE_WAIT: Duration = Duration::from_secs(30);
|
||||
|
||||
/// Until `configure` has recorded where `session`'s JVM is. The Memory view asks as soon as the
|
||||
/// session appears; looked up earlier, an attached JVM is reported as missing.
|
||||
async fn until_located(session: &Session) {
|
||||
let mut located = session.jvm_located.subscribe();
|
||||
tokio::select! {
|
||||
_ = located.wait_for(|v| *v) => {}
|
||||
() = session.ended.cancelled() => {}
|
||||
() = tokio::time::sleep(LOCATE_WAIT) => {}
|
||||
}
|
||||
}
|
||||
|
||||
/// `launched`: [`histogram_pid`] of `session`.
|
||||
fn session_target(session: &Session, launched: Result<Option<i32>, JvmMemoryUnavailable>) -> JvmTarget<'_> {
|
||||
match launched {
|
||||
Ok(Some(pid)) => JvmTarget::Launched(pid),
|
||||
Ok(None) => JvmTarget::Attached { pid: &session.jvm_pid, jdwp_port: session.jdwp_port.get().copied() },
|
||||
Err(reason) => JvmTarget::Unavailable(reason),
|
||||
}
|
||||
}
|
||||
|
||||
/// The pid of a launch session's program (`None` for an attach session), or why no histogram
|
||||
/// can be taken.
|
||||
fn histogram_pid(session: &Session) -> Result<Option<i32>, JvmMemoryUnavailable> {
|
||||
if !JVM_MEMORY_ADAPTERS.contains(&session.adapter.as_str()) {
|
||||
return Err(JvmMemoryUnavailable::NoJvmProcess);
|
||||
}
|
||||
if !cfg!(unix) {
|
||||
return Err(JvmMemoryUnavailable::UnsupportedPlatform);
|
||||
}
|
||||
if session.request == "launch" {
|
||||
return match i32::try_from(launch_pid(session)).ok().filter(|&p| p > 0) {
|
||||
Some(pid) => Ok(Some(pid)),
|
||||
None => Err(JvmMemoryUnavailable::NoJvmProcess),
|
||||
};
|
||||
}
|
||||
if session.jvm_pid.load(Ordering::Acquire) <= 0 {
|
||||
if session.terminated.load(Ordering::Acquire) {
|
||||
return Err(JvmMemoryUnavailable::ProgramExited);
|
||||
}
|
||||
if session.listen_relay.load(Ordering::Acquire) {
|
||||
return Err(JvmMemoryUnavailable::ListenModeNotSupported);
|
||||
}
|
||||
}
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
/// The JVM a launch session started: the one the IDE started for an app-launched (Kotlin)
|
||||
/// session, else the program the adapter reported; 0 when neither is known.
|
||||
pub(crate) fn launch_pid(session: &Session) -> i64 {
|
||||
match session.jvm_pid.load(Ordering::Acquire) {
|
||||
p if p > 0 => p,
|
||||
_ => session.debuggee_pid.load(Ordering::Acquire),
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
pub(crate) enum JvmTarget<'a> {
|
||||
/// The program a launch session started: its pid and everything it started.
|
||||
Launched(i32),
|
||||
/// The JVM of an attach session. While `pid` is 0 it is looked up by `jdwp_port`, which is
|
||||
/// `None` for a remote JVM.
|
||||
Attached { pid: &'a AtomicI64, jdwp_port: Option<u16> },
|
||||
/// Known without asking any JVM: no histogram can be taken, for this reason.
|
||||
Unavailable(JvmMemoryUnavailable),
|
||||
}
|
||||
|
||||
enum Lookup {
|
||||
Tree(i32),
|
||||
Known(i32),
|
||||
Scan(u16),
|
||||
}
|
||||
|
||||
/// `probe` is built from the suspension read once the lock is held: a request queued behind
|
||||
/// another must not decide on a state from before the program hit a breakpoint. Where no
|
||||
/// histogram can be taken, `debugger` (the session) may count the classes instead.
|
||||
pub(crate) async fn snapshot(
|
||||
state: &tokio::sync::Mutex<JvmMemoryState>,
|
||||
suspended: &Suspension,
|
||||
probe: impl FnOnce(bool) -> Arc<dyn AttachProbe>,
|
||||
target: JvmTarget<'_>,
|
||||
debugger: Option<&Session>,
|
||||
) -> IdeResult<JvmMemoryResult> {
|
||||
let mut state = state.lock().await;
|
||||
let outcome = histogram(&mut state, suspended, probe, target).await?;
|
||||
fallback::or_debugger_counts(&mut state, outcome, debugger).await
|
||||
}
|
||||
|
||||
async fn histogram(
|
||||
state: &mut JvmMemoryState,
|
||||
suspended: &Suspension,
|
||||
probe: impl FnOnce(bool) -> Arc<dyn AttachProbe>,
|
||||
target: JvmTarget<'_>,
|
||||
) -> IdeResult<Result<JvmMemorySnapshot, JvmMemoryUnavailable>> {
|
||||
let suspended = suspended.get();
|
||||
let lookup = match lookup(target) {
|
||||
Ok(lookup) => lookup,
|
||||
Err(reason) => return Ok(Err(reason)),
|
||||
};
|
||||
let probe = probe(suspended);
|
||||
let previous = state.baseline(JvmMemorySource::Histogram);
|
||||
let (found, outcome) = tokio::task::spawn_blocking(move || match resolve_jvm(lookup, &*probe, suspended) {
|
||||
Ok((tree, found)) => (found, take_snapshot(&*probe, &tree, suspended, previous.as_deref())),
|
||||
Err(reason) => (None, Ok(Err(reason))),
|
||||
})
|
||||
.await
|
||||
.map_err(|e| ide_core::IdeError::internal(format!("JVM memory snapshot failed: {e}")))?;
|
||||
keep_found(target, found);
|
||||
Ok(outcome?.map(|(snapshot, counts)| {
|
||||
state.remember(JvmMemorySource::Histogram, counts);
|
||||
snapshot
|
||||
}))
|
||||
}
|
||||
|
||||
/// Where to look for `target`'s JVM, or why there is none to ask.
|
||||
fn lookup(target: JvmTarget<'_>) -> Result<Lookup, JvmMemoryUnavailable> {
|
||||
match target {
|
||||
JvmTarget::Launched(pid) => Ok(Lookup::Tree(pid)),
|
||||
JvmTarget::Attached { pid, jdwp_port } => match i32::try_from(pid.load(Ordering::Acquire)) {
|
||||
Ok(p) if p > 0 => Ok(Lookup::Known(p)),
|
||||
_ => jdwp_port.map(Lookup::Scan).ok_or(JvmMemoryUnavailable::AttachedJvmNotFound),
|
||||
},
|
||||
JvmTarget::Unavailable(reason) => Err(reason),
|
||||
}
|
||||
}
|
||||
|
||||
/// Blocking. The pids to pick the JVM from, and the pid a scan found (for [`keep_found`]).
|
||||
fn resolve_jvm(
|
||||
lookup: Lookup,
|
||||
probe: &dyn AttachProbe,
|
||||
suspended: bool,
|
||||
) -> Result<(Vec<i32>, Option<i32>), JvmMemoryUnavailable> {
|
||||
match lookup {
|
||||
Lookup::Tree(pid) => Ok((pid_tree(pid), None)),
|
||||
Lookup::Known(pid) => Ok((vec![pid], None)),
|
||||
Lookup::Scan(port) => scan_for_jvm(probe, port, suspended).map(|pid| (vec![pid], Some(pid))),
|
||||
}
|
||||
}
|
||||
|
||||
fn keep_found(target: JvmTarget<'_>, found: Option<i32>) {
|
||||
if let (Some(found), JvmTarget::Attached { pid, .. }) = (found, target) {
|
||||
// The attach-time scan may have found it meanwhile; either answer is the same JVM.
|
||||
let _ = pid.compare_exchange(0, i64::from(found), Ordering::AcqRel, Ordering::Acquire);
|
||||
}
|
||||
}
|
||||
|
||||
/// A suspended JDK below 17 hangs on an attach request, so while suspended only a JVM known to
|
||||
/// be 17 or later is asked anything. Pass a version read without attaching (the `release` file).
|
||||
fn safe_while_suspended(version: Option<&ide_jvm_attach::JavaVersion>) -> bool {
|
||||
version.is_some_and(|v| ide_jvm_attach::histogram_safe_while_suspended(v.major))
|
||||
}
|
||||
|
||||
/// The JVM listening on JDWP `port`. While suspended the scan reaches the session's own JVM
|
||||
/// too, so JVMs unsafe to ask are skipped, and when one of them may be the session's the
|
||||
/// answer is a refusal rather than "not found".
|
||||
fn scan_for_jvm(probe: &dyn AttachProbe, port: u16, suspended: bool) -> Result<i32, JvmMemoryUnavailable> {
|
||||
// Majors of the JVMs left out; 0 for an unknown version.
|
||||
let skipped = std::cell::RefCell::new(std::collections::BTreeSet::new());
|
||||
let may_query = |pid: u32| {
|
||||
if !suspended {
|
||||
return true;
|
||||
}
|
||||
let version = probe.version(pid);
|
||||
let safe = safe_while_suspended(version.as_ref());
|
||||
if !safe {
|
||||
skipped.borrow_mut().insert(version.map_or(0, |v| v.major));
|
||||
}
|
||||
safe
|
||||
};
|
||||
match probe.find_by_jdwp_port(port, &may_query).and_then(|p| i32::try_from(p).ok()) {
|
||||
Some(pid) if pid > 0 => Ok(pid),
|
||||
_ => {
|
||||
let skipped = skipped.into_inner();
|
||||
Err(match skipped.first() {
|
||||
None => JvmMemoryUnavailable::AttachedJvmNotFound,
|
||||
Some(&major) => JvmMemoryUnavailable::UnsafeWhileSuspended {
|
||||
java_major: if skipped.len() == 1 { major } else { 0 },
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn pid_tree(pid: i32) -> Vec<i32> {
|
||||
#[cfg(unix)]
|
||||
{
|
||||
ide_core::process::process_tree(pid)
|
||||
}
|
||||
#[cfg(not(unix))]
|
||||
{
|
||||
vec![pid]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,146 @@
|
||||
//! Finding the JVM behind an attach session by its JDWP port.
|
||||
|
||||
use super::safe_while_suspended;
|
||||
use crate::session::Session;
|
||||
use serde_json::Value;
|
||||
use std::net::IpAddr;
|
||||
use std::sync::Arc;
|
||||
use std::sync::atomic::Ordering;
|
||||
use std::time::Duration;
|
||||
|
||||
/// One try of the scan at attach time, and the limit per socket within it: a silent socket's
|
||||
/// query thread lives that long, so a longer per-socket limit piles threads up across tries.
|
||||
const ATTACH_SCAN_TRY: Duration = Duration::from_millis(500);
|
||||
const ATTACH_SCAN_INTERVAL: Duration = Duration::from_millis(150);
|
||||
/// The JVM's attach socket can appear a little after the JDWP "Listening" banner, which is
|
||||
/// when the IDE attaches.
|
||||
const ATTACH_SCAN_WINDOW: Duration = Duration::from_secs(3);
|
||||
/// The single scan a refresh makes while the attach-time one found nothing.
|
||||
pub(super) const RESCAN_PER_SOCKET: Duration = Duration::from_secs(1);
|
||||
pub(super) const RESCAN_BUDGET: Duration = Duration::from_secs(2);
|
||||
|
||||
/// JDWP port of an attach to this machine (`hostName` loopback or absent): only such a JVM has
|
||||
/// an attach socket the IDE can find.
|
||||
pub(crate) fn local_jdwp_port(arguments: &Value) -> Option<u16> {
|
||||
let local = match arguments.get("hostName") {
|
||||
None | Some(Value::Null) => true,
|
||||
Some(Value::String(host)) => {
|
||||
let host = host.trim();
|
||||
let ip = host.strip_prefix('[').and_then(|h| h.strip_suffix(']')).unwrap_or(host);
|
||||
host.eq_ignore_ascii_case("localhost") || ip.parse::<IpAddr>().is_ok_and(|ip| ip.is_loopback())
|
||||
}
|
||||
Some(_) => false,
|
||||
};
|
||||
jdwp_port(arguments).filter(|_| local)
|
||||
}
|
||||
|
||||
/// `port` of an attach request: a number or a numeric string, never 0.
|
||||
fn jdwp_port(arguments: &Value) -> Option<u16> {
|
||||
let port = match arguments.get("port")? {
|
||||
Value::Number(n) => n.as_u64().and_then(|p| u16::try_from(p).ok()),
|
||||
Value::String(s) => s.trim().parse().ok(),
|
||||
_ => None,
|
||||
};
|
||||
port.filter(|&p| p != 0)
|
||||
}
|
||||
|
||||
/// `hostName` (empty when absent) and `port` of an attach request, local or not.
|
||||
pub(crate) fn attach_target(arguments: &Value) -> Option<(String, u16)> {
|
||||
let host = arguments.get("hostName").and_then(Value::as_str).unwrap_or_default();
|
||||
Some((host.to_owned(), jdwp_port(arguments)?))
|
||||
}
|
||||
|
||||
/// Pid of the local JVM listening on a JDWP port (first argument), given whether the session is
|
||||
/// suspended (second). Replaceable so tests never send attach requests to real JVMs.
|
||||
pub(crate) type JvmFinder = Arc<dyn Fn(u16, bool) -> Option<u32> + Send + Sync>;
|
||||
|
||||
/// The [`JvmFinder`] of the attach-time scan.
|
||||
pub(crate) fn find_local_jvm(port: u16, suspended: bool) -> Option<u32> {
|
||||
let may_query = |pid| !suspended || safe_while_suspended(ide_jvm_attach::java_version_of(pid).as_ref());
|
||||
ide_jvm_attach::find_jvm_by_jdwp_port(port, ATTACH_SCAN_TRY, ATTACH_SCAN_TRY, may_query)
|
||||
}
|
||||
|
||||
/// Looks for the JVM behind a local attach: one try now, the rest in the background, so a JVM
|
||||
/// that never opens an attach socket holds the attach up by one try at most. Call it before the
|
||||
/// debugger connects: the JVM clears its JDWP listener address then, and a scan can no longer
|
||||
/// tell it by its port.
|
||||
pub(crate) async fn find_attached_jvm(session: &Arc<Session>, port: u16, finder: JvmFinder) {
|
||||
let _ = session.jdwp_port.set(port);
|
||||
let find = move |suspended: bool| finder(port, suspended);
|
||||
let first = find.clone();
|
||||
if let Ok(Some(pid)) = tokio::task::spawn_blocking(move || first(false)).await {
|
||||
let _ = session.jvm_pid.compare_exchange(0, i64::from(pid), Ordering::AcqRel, Ordering::Acquire);
|
||||
return;
|
||||
}
|
||||
drop(watch_for_attached_jvm(session, port, find, ATTACH_SCAN_INTERVAL, ATTACH_SCAN_WINDOW));
|
||||
}
|
||||
|
||||
/// Retries `find` for the session's `jvm_pid`, holding the session weakly so an ended session
|
||||
/// is not kept alive. `find` is told whether the session is suspended.
|
||||
pub(crate) fn watch_for_attached_jvm<F>(
|
||||
session: &Arc<Session>,
|
||||
port: u16,
|
||||
find: F,
|
||||
interval: Duration,
|
||||
window: Duration,
|
||||
) -> tokio::task::JoinHandle<()>
|
||||
where
|
||||
F: Fn(bool) -> Option<u32> + Clone + Send + 'static,
|
||||
{
|
||||
let weak = Arc::downgrade(session);
|
||||
tokio::spawn(async move {
|
||||
let gate = || match weak.upgrade() {
|
||||
Some(s) if !s.terminated.load(Ordering::Acquire) && !s.conn.is_closed() => {
|
||||
if s.jvm_pid.load(Ordering::Acquire) > 0 {
|
||||
ScanGate::Ended
|
||||
} else {
|
||||
ScanGate::Scan { suspended: s.suspended.get() }
|
||||
}
|
||||
}
|
||||
_ => ScanGate::Ended,
|
||||
};
|
||||
let Some(pid) = retry_find(find, gate, interval, window).await else {
|
||||
tracing::debug!("no local JVM found for JDWP port {port} yet");
|
||||
return;
|
||||
};
|
||||
if let Some(s) = weak.upgrade() {
|
||||
let _ = s.jvm_pid.compare_exchange(0, i64::from(pid), Ordering::AcqRel, Ordering::Acquire);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
pub(crate) enum ScanGate {
|
||||
Scan { suspended: bool },
|
||||
Ended,
|
||||
}
|
||||
|
||||
/// Calls `find` on a blocking thread, on each try `gate` allows, until it returns a pid, `gate`
|
||||
/// reports the end or `window` has passed.
|
||||
pub(crate) async fn retry_find<F>(
|
||||
find: F,
|
||||
gate: impl Fn() -> ScanGate,
|
||||
interval: Duration,
|
||||
window: Duration,
|
||||
) -> Option<u32>
|
||||
where
|
||||
F: Fn(bool) -> Option<u32> + Clone + Send + 'static,
|
||||
{
|
||||
let deadline = tokio::time::Instant::now() + window;
|
||||
loop {
|
||||
match gate() {
|
||||
ScanGate::Ended => break,
|
||||
ScanGate::Scan { suspended } => {
|
||||
let find = find.clone();
|
||||
if let Ok(Some(pid)) = tokio::task::spawn_blocking(move || find(suspended)).await {
|
||||
return Some(pid);
|
||||
}
|
||||
}
|
||||
}
|
||||
if tokio::time::Instant::now() + interval >= deadline {
|
||||
break;
|
||||
}
|
||||
tokio::time::sleep(interval).await;
|
||||
}
|
||||
None
|
||||
}
|
||||
@@ -0,0 +1,214 @@
|
||||
//! One class histogram of a JVM and its deltas against the previous one.
|
||||
|
||||
use super::scan::{RESCAN_BUDGET, RESCAN_PER_SOCKET};
|
||||
use super::{
|
||||
ClassCounts, HISTOGRAM_TIMEOUT, JvmClassRow, JvmMemorySnapshot, JvmMemorySource, JvmMemoryUnavailable,
|
||||
safe_while_suspended,
|
||||
};
|
||||
use ide_core::IdeResult;
|
||||
use ide_jvm_attach::{AttachError, HeapDumpReply, HistogramRow, JavaVersion};
|
||||
use std::collections::HashMap;
|
||||
use std::path::Path;
|
||||
use std::time::Duration;
|
||||
|
||||
/// Version probe for a JVM whose `release` file could not be read.
|
||||
const VERSION_TIMEOUT: Duration = Duration::from_secs(2);
|
||||
const FLAGS_TIMEOUT: Duration = Duration::from_secs(2);
|
||||
|
||||
/// The attach calls a snapshot or a heap dump makes, so tests can stand in for a JVM.
|
||||
pub(crate) trait AttachProbe: Send + Sync {
|
||||
fn has_listener(&self, pid: u32) -> bool;
|
||||
/// Asks only the JVMs whose pid `may_query` accepts.
|
||||
fn find_by_jdwp_port(&self, port: u16, may_query: &dyn Fn(u32) -> bool) -> Option<u32>;
|
||||
fn version(&self, pid: u32) -> Option<JavaVersion>;
|
||||
fn histogram(&self, pid: u32) -> Result<String, AttachError>;
|
||||
fn alive(&self, pid: u32) -> bool;
|
||||
fn vm_flags(&self, pid: u32) -> Result<String, AttachError>;
|
||||
fn heap_dump(&self, pid: u32, path: &Path, timeout: Duration) -> Result<HeapDumpReply, AttachError>;
|
||||
/// `None` where the free space is not known.
|
||||
fn free_bytes(&self, dir: &Path) -> Option<u64>;
|
||||
}
|
||||
|
||||
pub(super) struct JdkProbe {
|
||||
pub(super) suspended: bool,
|
||||
}
|
||||
|
||||
impl AttachProbe for JdkProbe {
|
||||
fn has_listener(&self, pid: u32) -> bool {
|
||||
ide_jvm_attach::has_listener(pid)
|
||||
}
|
||||
|
||||
fn find_by_jdwp_port(&self, port: u16, may_query: &dyn Fn(u32) -> bool) -> Option<u32> {
|
||||
ide_jvm_attach::find_jvm_by_jdwp_port(port, RESCAN_PER_SOCKET, RESCAN_BUDGET, may_query)
|
||||
}
|
||||
|
||||
fn version(&self, pid: u32) -> Option<JavaVersion> {
|
||||
ide_jvm_attach::java_version_of(pid).or_else(|| {
|
||||
// The version decides whether attaching to a suspended VM hangs it, so never
|
||||
// attach to learn it while suspended.
|
||||
if self.suspended {
|
||||
return None;
|
||||
}
|
||||
let out = ide_jvm_attach::jcmd(pid, "VM.version", VERSION_TIMEOUT).ok()?;
|
||||
ide_jvm_attach::parse_vm_version(&out)
|
||||
})
|
||||
}
|
||||
|
||||
fn histogram(&self, pid: u32) -> Result<String, AttachError> {
|
||||
ide_jvm_attach::jcmd(pid, "GC.class_histogram -all", HISTOGRAM_TIMEOUT)
|
||||
}
|
||||
|
||||
fn alive(&self, pid: u32) -> bool {
|
||||
alive(pid)
|
||||
}
|
||||
|
||||
fn vm_flags(&self, pid: u32) -> Result<String, AttachError> {
|
||||
ide_jvm_attach::vm_flags(pid, FLAGS_TIMEOUT)
|
||||
}
|
||||
|
||||
fn heap_dump(&self, pid: u32, path: &Path, timeout: Duration) -> Result<HeapDumpReply, AttachError> {
|
||||
ide_jvm_attach::heap_dump(pid, path, timeout)
|
||||
}
|
||||
|
||||
fn free_bytes(&self, dir: &Path) -> Option<u64> {
|
||||
ide_heap::free_bytes(dir)
|
||||
}
|
||||
}
|
||||
|
||||
fn alive(pid: u32) -> bool {
|
||||
#[cfg(unix)]
|
||||
{
|
||||
// 0 and negative pids address process groups.
|
||||
let Ok(pid) = i32::try_from(pid) else { return false };
|
||||
// SAFETY: signal 0 only checks that the process exists and may be signalled.
|
||||
pid > 0
|
||||
&& (unsafe { libc::kill(pid, 0) } == 0
|
||||
|| std::io::Error::last_os_error().raw_os_error() == Some(libc::EPERM))
|
||||
}
|
||||
#[cfg(not(unix))]
|
||||
{
|
||||
let _ = pid;
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
/// Why `pid`'s attach listener cannot answer: it stopped listening or the program ended.
|
||||
pub(super) fn gone(probe: &dyn AttachProbe, pid: u32) -> JvmMemoryUnavailable {
|
||||
if probe.alive(pid) { JvmMemoryUnavailable::ListenerNotRunning } else { JvmMemoryUnavailable::ProgramExited }
|
||||
}
|
||||
|
||||
/// The reason for an attach request (`what`) to `jvm` that failed, or an error where no reason
|
||||
/// fits.
|
||||
pub(super) fn attach_failure(
|
||||
probe: &dyn AttachProbe,
|
||||
jvm: u32,
|
||||
error: AttachError,
|
||||
what: &str,
|
||||
) -> IdeResult<JvmMemoryUnavailable> {
|
||||
use JvmMemoryUnavailable as U;
|
||||
match error {
|
||||
AttachError::Timeout => Ok(U::TimedOut),
|
||||
AttachError::Refused | AttachError::NoListener => Ok(gone(probe, jvm)),
|
||||
AttachError::ForeignListener => {
|
||||
tracing::warn!("attach socket of JVM {jvm} is no longer private to this user; not using it");
|
||||
Ok(U::ListenerNotRunning)
|
||||
}
|
||||
AttachError::Unsupported => Ok(U::UnsupportedPlatform),
|
||||
e => Err(ide_core::IdeError::internal(format!("{what} of JVM {jvm}: {e}"))),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn pick_jvm_pid(tree: &[i32], has_listener: impl Fn(u32) -> bool) -> Option<u32> {
|
||||
tree.iter().filter_map(|&p| u32::try_from(p).ok()).filter(|&p| p > 0).find(|&p| has_listener(p))
|
||||
}
|
||||
|
||||
pub(crate) fn diff_rows(rows: Vec<HistogramRow>, previous: Option<&ClassCounts>) -> (Vec<JvmClassRow>, ClassCounts) {
|
||||
let mut seen: HashMap<&str, u32> = HashMap::with_capacity(rows.len());
|
||||
for r in &rows {
|
||||
*seen.entry(r.name.as_str()).or_default() += 1;
|
||||
}
|
||||
let ambiguous: Vec<bool> = rows.iter().map(|r| seen[r.name.as_str()] > 1).collect();
|
||||
drop(seen);
|
||||
|
||||
let mut counts = ClassCounts::with_capacity(rows.len());
|
||||
let mut out: Vec<JvmClassRow> = rows
|
||||
.into_iter()
|
||||
.zip(ambiguous)
|
||||
.map(|(r, ambiguous)| {
|
||||
counts.insert(r.name.clone(), (!ambiguous).then_some((r.instances, r.bytes)));
|
||||
let delta = match previous {
|
||||
_ if ambiguous => None,
|
||||
None => None,
|
||||
Some(p) => match p.get(&r.name) {
|
||||
Some(None) => None,
|
||||
// Absent: new since the previous snapshot, so it grew from nothing.
|
||||
before => {
|
||||
let (instances, bytes) = before.copied().flatten().unwrap_or_default();
|
||||
Some((delta(r.instances, instances), delta(r.bytes, bytes)))
|
||||
}
|
||||
},
|
||||
};
|
||||
JvmClassRow {
|
||||
name: r.name,
|
||||
module: r.module,
|
||||
instances: r.instances,
|
||||
bytes: r.bytes,
|
||||
delta_instances: delta.map(|d| d.0),
|
||||
delta_bytes: delta.map(|d| d.1),
|
||||
ambiguous,
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
out.sort_by(|a, b| b.bytes.cmp(&a.bytes).then_with(|| a.name.cmp(&b.name)));
|
||||
(out, counts)
|
||||
}
|
||||
|
||||
/// Exact while the difference fits in an i64 (two's complement).
|
||||
fn delta(now: u64, before: u64) -> i64 {
|
||||
now.wrapping_sub(before) as i64
|
||||
}
|
||||
|
||||
/// `pid_tree` starts with the session's reported pid, followed by its descendants.
|
||||
pub(crate) fn take_snapshot(
|
||||
probe: &dyn AttachProbe,
|
||||
pid_tree: &[i32],
|
||||
suspended: bool,
|
||||
previous: Option<&ClassCounts>,
|
||||
) -> IdeResult<Result<(JvmMemorySnapshot, ClassCounts), JvmMemoryUnavailable>> {
|
||||
use JvmMemoryUnavailable as U;
|
||||
let Some(root) = pid_tree.first().and_then(|&p| u32::try_from(p).ok()) else {
|
||||
return Ok(Err(U::NoJvmProcess));
|
||||
};
|
||||
let Some(jvm) = pick_jvm_pid(pid_tree, |p| probe.has_listener(p)) else {
|
||||
return Ok(Err(gone(probe, root)));
|
||||
};
|
||||
|
||||
let version = probe.version(jvm);
|
||||
if suspended && !safe_while_suspended(version.as_ref()) {
|
||||
return Ok(Err(U::UnsafeWhileSuspended { java_major: version.map_or(0, |v| v.major) }));
|
||||
}
|
||||
|
||||
let started = std::time::Instant::now();
|
||||
let text = match probe.histogram(jvm) {
|
||||
Ok(text) => text,
|
||||
Err(e) => return attach_failure(probe, jvm, e, "class histogram").map(Err),
|
||||
};
|
||||
let elapsed_ms = u32::try_from(started.elapsed().as_millis()).unwrap_or(u32::MAX);
|
||||
|
||||
let histogram = ide_jvm_attach::parse_class_histogram(&text);
|
||||
if histogram.skipped_lines > 0 {
|
||||
tracing::debug!("class histogram of JVM {jvm}: {} unreadable lines", histogram.skipped_lines);
|
||||
}
|
||||
let (rows, counts) = diff_rows(histogram.rows, previous);
|
||||
let snapshot = JvmMemorySnapshot {
|
||||
total_instances: rows.iter().map(|r| r.instances).sum(),
|
||||
total_bytes: rows.iter().map(|r| r.bytes).sum(),
|
||||
rows,
|
||||
java_version: version.map(|v| v.text).unwrap_or_default(),
|
||||
taken_while_suspended: suspended,
|
||||
has_baseline: previous.is_some(),
|
||||
elapsed_ms,
|
||||
source: JvmMemorySource::Histogram,
|
||||
};
|
||||
Ok(Ok((snapshot, counts)))
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
//! A scripted [`JvmInspector`] and a session without an adapter behind it.
|
||||
|
||||
use crate::SessionOwner;
|
||||
use crate::adapter::{AdapterSpec, Transport};
|
||||
use crate::connection::Connection;
|
||||
use crate::session::Session;
|
||||
use ide_core::{DebugSessionId, IdeEvent, SharedSink};
|
||||
use ide_jvm_inspect::{
|
||||
ClassCount, InspectError, InspectFuture, JvmField, JvmInspector, JvmReferrer, JvmValue, ObjectId, VmTarget,
|
||||
};
|
||||
use parking_lot::Mutex;
|
||||
use std::sync::Arc;
|
||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||
use tokio::sync::Notify;
|
||||
|
||||
#[derive(Default)]
|
||||
pub(crate) struct FakeInspector {
|
||||
pub targets: Mutex<Vec<VmTarget>>,
|
||||
/// The `limit` of each `instances` / `referrers` call.
|
||||
pub limits: Mutex<Vec<u32>>,
|
||||
pub calls: AtomicUsize,
|
||||
/// Returned by every call when set.
|
||||
pub error: Mutex<Option<InspectError>>,
|
||||
pub counts: Mutex<Vec<ClassCount>>,
|
||||
pub instances: Mutex<Vec<JvmValue>>,
|
||||
/// Every call signals `entered`, then waits for `release`.
|
||||
pub hold: bool,
|
||||
pub entered: Notify,
|
||||
pub release: Notify,
|
||||
}
|
||||
|
||||
impl FakeInspector {
|
||||
pub fn failing(error: InspectError) -> Self {
|
||||
Self { error: Mutex::new(Some(error)), ..Self::default() }
|
||||
}
|
||||
|
||||
pub fn calls(&self) -> usize {
|
||||
self.calls.load(Ordering::SeqCst)
|
||||
}
|
||||
|
||||
async fn answer<T: Send>(&self, target: &VmTarget, limit: Option<u32>, ok: T) -> Result<T, InspectError> {
|
||||
self.calls.fetch_add(1, Ordering::SeqCst);
|
||||
self.targets.lock().push(target.clone());
|
||||
self.limits.lock().extend(limit);
|
||||
if self.hold {
|
||||
self.entered.notify_one();
|
||||
self.release.notified().await;
|
||||
}
|
||||
match self.error.lock().clone() {
|
||||
Some(e) => Err(e),
|
||||
None => Ok(ok),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl JvmInspector for FakeInspector {
|
||||
fn class_counts<'a>(&'a self, target: &'a VmTarget) -> InspectFuture<'a, Vec<ClassCount>> {
|
||||
Box::pin(self.answer(target, None, self.counts.lock().clone()))
|
||||
}
|
||||
|
||||
fn instances<'a>(&'a self, target: &'a VmTarget, _: &'a str, limit: u32) -> InspectFuture<'a, Vec<JvmValue>> {
|
||||
Box::pin(self.answer(target, Some(limit), self.instances.lock().clone()))
|
||||
}
|
||||
|
||||
fn fields<'a>(&'a self, target: &'a VmTarget, _: ObjectId) -> InspectFuture<'a, Vec<JvmField>> {
|
||||
Box::pin(self.answer(target, None, Vec::new()))
|
||||
}
|
||||
|
||||
fn referrers<'a>(&'a self, target: &'a VmTarget, _: ObjectId, limit: u32) -> InspectFuture<'a, Vec<JvmReferrer>> {
|
||||
Box::pin(self.answer(target, Some(limit), Vec::new()))
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) struct BareSession {
|
||||
pub session: Arc<Session>,
|
||||
pub events: Arc<Mutex<Vec<IdeEvent>>>,
|
||||
/// The adapter's end of the connection, kept open.
|
||||
_adapter: tokio::io::DuplexStream,
|
||||
}
|
||||
|
||||
/// A java-debug session that no adapter answers; must be called inside a Tokio runtime.
|
||||
pub(crate) fn bare_session(request: &str) -> BareSession {
|
||||
bare_session_of("java-debug", request)
|
||||
}
|
||||
|
||||
/// A session of adapter `name` that no adapter answers; must be called inside a Tokio runtime.
|
||||
pub(crate) fn bare_session_of(name: &str, request: &str) -> BareSession {
|
||||
let events: Arc<Mutex<Vec<IdeEvent>>> = Arc::default();
|
||||
let sink_events = events.clone();
|
||||
let sink: SharedSink = Arc::new(move |e: IdeEvent| sink_events.lock().push(e));
|
||||
let (client, adapter) = tokio::io::duplex(1 << 10);
|
||||
let (reader, writer) = tokio::io::split(client);
|
||||
let (conn, _) = Connection::start(reader, writer);
|
||||
let spec = AdapterSpec {
|
||||
adapter: name.into(),
|
||||
command: String::new(),
|
||||
args: Vec::new(),
|
||||
transport: Transport::Stdio,
|
||||
install_hint: None,
|
||||
cwd: std::env::temp_dir(),
|
||||
env: Vec::new(),
|
||||
server_host: None,
|
||||
connect: None,
|
||||
pinned: None,
|
||||
};
|
||||
let session = Session::new(
|
||||
DebugSessionId(1),
|
||||
SessionOwner::new("test", sink),
|
||||
"test".into(),
|
||||
name.into(),
|
||||
None,
|
||||
request.into(),
|
||||
conn,
|
||||
spec,
|
||||
None,
|
||||
None,
|
||||
);
|
||||
BareSession { session: Arc::new(session), events, _adapter: adapter }
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
Loaded 100 of 2572 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user