1
- use std:: { env, fs:: OpenOptions , io:: Write , path:: Path , process:: Command , sync:: OnceLock } ;
1
+ use std:: {
2
+ env,
3
+ fs:: { self , read_to_string} ,
4
+ path:: Path ,
5
+ process:: Command ,
6
+ sync:: OnceLock ,
7
+ } ;
2
8
3
9
use eyre:: Result ;
10
+ use itertools:: Itertools ;
4
11
use tempfile:: tempdir;
5
12
6
13
fn install_cli ( ) {
@@ -134,6 +141,7 @@ fn test_cli_init_build() -> Result<()> {
134
141
let manifest_path = temp_path. join ( "Cargo.toml" ) ;
135
142
install_cli ( ) ;
136
143
144
+ // Cargo will not respect patches if run within a workspace
137
145
run_cmd (
138
146
"cargo" ,
139
147
& [
@@ -145,7 +153,7 @@ fn test_cli_init_build() -> Result<()> {
145
153
] ,
146
154
) ?;
147
155
if matches ! ( env:: var( "USE_LOCAL_OPENVM" ) , Ok ( x) if x == "1" ) {
148
- append_patch_to_cargo_toml ( & manifest_path) ?;
156
+ replace_with_local_openvm ( & manifest_path) ?;
149
157
}
150
158
151
159
run_cmd (
@@ -185,25 +193,30 @@ fn run_cmd(program: &str, args: &[&str]) -> Result<()> {
185
193
Ok ( ( ) )
186
194
}
187
195
188
- fn append_patch_to_cargo_toml ( file_path : impl AsRef < Path > ) -> Result < ( ) > {
196
+ fn replace_with_local_openvm ( file_path : impl AsRef < Path > ) -> Result < ( ) > {
189
197
const MANIFEST_DIR : & str = env ! ( "CARGO_MANIFEST_DIR" ) ;
190
198
let openvm_path = Path :: new ( MANIFEST_DIR )
191
199
. parent ( )
192
200
. unwrap ( )
193
201
. join ( "toolchain" )
194
202
. join ( "openvm" ) ;
195
- let mut file = OpenOptions :: new ( )
196
- . create ( false )
197
- . append ( true )
198
- . open ( file_path) ?;
199
-
200
- // Add a newline first to ensure proper formatting
201
- writeln ! ( file) ?;
202
- writeln ! (
203
- file,
204
- r#"[patch."https://github.com/openvm-org/openvm.git"]"#
205
- ) ?;
206
- writeln ! ( file, r#"openvm = {{ path = "{}" }}"# , openvm_path. display( ) ) ?;
203
+ let content = read_to_string ( & file_path) ?;
204
+ let lines = content. lines ( ) . collect :: < Vec < _ > > ( ) ;
205
+ let new_content = lines
206
+ . iter ( )
207
+ . map ( |line| {
208
+ if line. starts_with ( "openvm = { git = \" https://github.com/openvm-org/openvm.git\" " ) {
209
+ format ! (
210
+ r#"openvm = {{ path = "{}", features = ["std"] }}"# ,
211
+ openvm_path. display( )
212
+ )
213
+ } else {
214
+ line. to_string ( )
215
+ }
216
+ } )
217
+ . join ( "\n " ) ;
218
+
219
+ fs:: write ( file_path, new_content) ?;
207
220
208
221
Ok ( ( ) )
209
222
}
0 commit comments