diff --git a/compiler/rustc_metadata/src/fs.rs b/compiler/rustc_metadata/src/fs.rs index ed42efcfd77..b08b1247c6d 100644 --- a/compiler/rustc_metadata/src/fs.rs +++ b/compiler/rustc_metadata/src/fs.rs @@ -68,13 +68,24 @@ pub fn encode_and_write_metadata( .unwrap_or_else(|err| tcx.sess.fatal(&format!("couldn't create a temp dir: {}", err))); let metadata_tmpdir = MaybeTempDir::new(metadata_tmpdir, tcx.sess.opts.cg.save_temps); let metadata_filename = metadata_tmpdir.as_ref().join(METADATA_FILENAME); + let need_metadata_file = tcx.sess.opts.output_types.contains_key(&OutputType::Metadata); let metadata = match metadata_kind { MetadataKind::None => { - if tcx.sess.opts.json_artifact_notifications { - tcx.sess - .parse_sess - .span_diagnostic - .emit_artifact_notification(&out_filename, "metadata"); + if need_metadata_file { + // Though creating the empty file here seems to be meaningless, cargo expects it. + if let Err(e) = std::fs::File::create(&out_filename) { + tcx.sess.fatal(&format!( + "failed to create the file {}: {}", + out_filename.display(), + e + )); + } + if tcx.sess.opts.json_artifact_notifications { + tcx.sess + .parse_sess + .span_diagnostic + .emit_artifact_notification(&out_filename, "metadata"); + } } EncodedMetadata::empty() } @@ -83,7 +94,6 @@ pub fn encode_and_write_metadata( let _prof_timer = tcx.sess.prof.generic_activity("write_crate_metadata"); - let need_metadata_file = tcx.sess.opts.output_types.contains_key(&OutputType::Metadata); let (metadata_filename, metadata_tmpdir) = if need_metadata_file { if let Err(e) = non_durable_rename(&metadata_filename, &out_filename) { tcx.sess.fatal(&format!("failed to write {}: {}", out_filename.display(), e));