Skip to content

Commit 08fff21

Browse files
authored
add "do not edit" header to Go output (#1473)
1 parent d4f0885 commit 08fff21

File tree

1 file changed

+34
-10
lines changed

1 file changed

+34
-10
lines changed

crates/go/src/lib.rs

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -775,6 +775,9 @@ impl WorldGenerator for Go {
775775
fn finish(&mut self, resolve: &Resolve, id: WorldId, files: &mut Files) -> Result<()> {
776776
_ = (resolve, id);
777777

778+
let version = env!("CARGO_PKG_VERSION");
779+
let header = &format!("// Generated by `wit-bindgen` {version}. DO NOT EDIT!");
780+
778781
let src = mem::take(&mut self.src);
779782
let align = self.return_area_align.format(POINTER_SIZE_EXPRESSION);
780783
let size = self.return_area_size.format(POINTER_SIZE_EXPRESSION);
@@ -792,7 +795,8 @@ impl WorldGenerator for Go {
792795
&maybe_gofmt(
793796
self.opts.format,
794797
format!(
795-
r#"package main
798+
r#"{header}
799+
package main
796800
797801
import (
798802
"runtime"
@@ -815,7 +819,7 @@ func main() {{}}
815819
files.push("go.mod", b"module wit_component\n\ngo 1.25");
816820
files.push(
817821
"wit_runtime/wit_runtime.go",
818-
include_bytes!("wit_runtime.go"),
822+
format!("{header}\n{}", include_str!("wit_runtime.go")).as_bytes(),
819823
);
820824

821825
for (prefix, interfaces) in [("export_", &self.export_interfaces), ("", &self.interfaces)] {
@@ -828,7 +832,8 @@ func main() {{}}
828832
&maybe_gofmt(
829833
self.opts.format,
830834
format!(
831-
"package {prefix}{name}
835+
"{header}
836+
package {prefix}{name}
832837
833838
import (
834839
{imports}
@@ -869,7 +874,8 @@ import (
869874
&maybe_gofmt(
870875
self.opts.format,
871876
format!(
872-
r#"package wit_types
877+
r#"{header}
878+
package wit_types
873879
874880
{tuples}
875881
"#
@@ -880,27 +886,45 @@ import (
880886
}
881887

882888
if self.need_async {
883-
files.push("wit_async/wit_async.go", include_bytes!("wit_async.go"));
889+
files.push(
890+
"wit_async/wit_async.go",
891+
format!("{header}\n{}", include_str!("wit_async.go")).as_bytes(),
892+
);
884893
}
885894

886895
if self.need_option {
887-
files.push("wit_types/wit_option.go", include_bytes!("wit_option.go"));
896+
files.push(
897+
"wit_types/wit_option.go",
898+
format!("{header}\n{}", include_str!("wit_option.go")).as_bytes(),
899+
);
888900
}
889901

890902
if self.need_result {
891-
files.push("wit_types/wit_result.go", include_bytes!("wit_result.go"));
903+
files.push(
904+
"wit_types/wit_result.go",
905+
format!("{header}\n{}", include_str!("wit_result.go")).as_bytes(),
906+
);
892907
}
893908

894909
if self.need_unit {
895-
files.push("wit_types/wit_unit.go", include_bytes!("wit_unit.go"));
910+
files.push(
911+
"wit_types/wit_unit.go",
912+
format!("{header}\n{}", include_str!("wit_unit.go")).as_bytes(),
913+
);
896914
}
897915

898916
if self.need_future {
899-
files.push("wit_types/wit_future.go", include_bytes!("wit_future.go"));
917+
files.push(
918+
"wit_types/wit_future.go",
919+
format!("{header}\n{}", include_str!("wit_future.go")).as_bytes(),
920+
);
900921
}
901922

902923
if self.need_stream {
903-
files.push("wit_types/wit_stream.go", include_bytes!("wit_stream.go"));
924+
files.push(
925+
"wit_types/wit_stream.go",
926+
format!("{header}\n{}", include_str!("wit_stream.go")).as_bytes(),
927+
);
904928
}
905929

906930
Ok(())

0 commit comments

Comments
 (0)