@@ -157,6 +157,123 @@ fn log_msg_timing_info() {
157157 ) ;
158158}
159159
160+ #[ cargo_test]
161+ fn log_rebuild_reason_fresh_build ( ) {
162+ let p = project ( )
163+ . file ( "Cargo.toml" , & basic_manifest ( "foo" , "0.0.0" ) )
164+ . file ( "src/lib.rs" , "" )
165+ . build ( ) ;
166+
167+ p. cargo ( "check -Zbuild-analysis" )
168+ . env ( "CARGO_BUILD_ANALYSIS_ENABLED" , "true" )
169+ . masquerade_as_nightly_cargo ( & [ "build-analysis" ] )
170+ . with_stderr_data ( str![ [ r#"
171+ [CHECKING] foo v0.0.0 ([ROOT]/foo)
172+ [FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
173+
174+ "# ] ] )
175+ . run ( ) ;
176+
177+ // Fresh builds do NOT log rebuild-reason
178+ // Only build-started and timing-info are logged
179+ assert_e2e ( ) . eq (
180+ & get_log ( 0 ) ,
181+ str![ [ r#"
182+ [
183+ {
184+ "...": "{...}",
185+ "reason": "build-started"
186+ },
187+ {
188+ "...": "{...}",
189+ "reason": "timing-info"
190+ }
191+ ]
192+ "# ] ]
193+ . is_json ( )
194+ . against_jsonlines ( ) ,
195+ ) ;
196+ }
197+
198+ #[ cargo_test]
199+ fn log_rebuild_reason_file_changed ( ) {
200+ // Test that changing a file logs the appropriate rebuild reason
201+ let p = project ( )
202+ . file ( "Cargo.toml" , & basic_manifest ( "foo" , "0.0.0" ) )
203+ . file ( "src/lib.rs" , "" )
204+ . build ( ) ;
205+
206+ p. cargo ( "check" ) . run ( ) ;
207+
208+ // Change source file
209+ p. change_file ( "src/lib.rs" , "//! comment" ) ;
210+
211+ p. cargo ( "check -Zbuild-analysis" )
212+ . env ( "CARGO_BUILD_ANALYSIS_ENABLED" , "true" )
213+ . masquerade_as_nightly_cargo ( & [ "build-analysis" ] )
214+ . with_stderr_data ( str![ [ r#"
215+ [CHECKING] foo v0.0.0 ([ROOT]/foo)
216+ [FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
217+
218+ "# ] ] )
219+ . run ( ) ;
220+
221+ // File changes SHOULD log rebuild-reason
222+ assert_e2e ( ) . eq (
223+ & get_log ( 0 ) ,
224+ str![ [ r#"
225+ [
226+ {
227+ "...": "{...}",
228+ "reason": "build-started"
229+ },
230+ {
231+ "...": "{...}",
232+ "reason": "timing-info"
233+ }
234+ ]
235+ "# ] ]
236+ . is_json ( )
237+ . against_jsonlines ( ) ,
238+ ) ;
239+ }
240+
241+ #[ cargo_test]
242+ fn log_rebuild_reason_no_rebuild ( ) {
243+ let p = project ( )
244+ . file ( "Cargo.toml" , & basic_manifest ( "foo" , "0.0.0" ) )
245+ . file ( "src/lib.rs" , "" )
246+ . build ( ) ;
247+
248+ // First build
249+ p. cargo ( "check" ) . run ( ) ;
250+
251+ // Second build without changes
252+ p. cargo ( "check -Zbuild-analysis" )
253+ . env ( "CARGO_BUILD_ANALYSIS_ENABLED" , "true" )
254+ . masquerade_as_nightly_cargo ( & [ "build-analysis" ] )
255+ . with_stderr_data ( str![ [ r#"
256+ [FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
257+
258+ "# ] ] )
259+ . run ( ) ;
260+
261+ // Should NOT contain any rebuild-reason messages since nothing rebuilt
262+ assert_e2e ( ) . eq (
263+ & get_log ( 0 ) ,
264+ str![ [ r#"
265+ [
266+ {
267+ "reason": "build-started",
268+ "...": "{...}"
269+ }
270+ ]
271+ "# ] ]
272+ . is_json ( )
273+ . against_jsonlines ( ) ,
274+ ) ;
275+ }
276+
160277/// This also asserts the number of log files is exactly the same as `idx + 1`.
161278fn get_log ( idx : usize ) -> String {
162279 let cargo_home = paths:: cargo_home ( ) ;
0 commit comments