Commit 7f303d5e73 for golang

commit 7f303d5e73c6999f0fb37fbdfcc7d6a62b621dab
Author: nnnkkk7 <kurodanaoki0711pana@gmail.com>
Date:   Sat Nov 21 07:20:31 2026 +0900

    cmd/go: include header files in cgo action IDs

    Since CL 799960 the cgo run and cgo compile actions are cached
    separately, but their action IDs did not include the package's
    header files, and the compile action ID did not include the
    _cgo_export.h written by cgo. Changing a header reused stale cgo
    output and C objects.

    Fixes #83306

    Change-Id: Ibf0f7ba1d54888cb078059c947018d59b26c65a2
    Reviewed-on: https://go-review.googlesource.com/c/go/+/866841
    Reviewed-by: Sean Liao <sean@liao.dev>
    LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
    Auto-Submit: Sean Liao <sean@liao.dev>
    Reviewed-by: Michael Matloob <matloob@golang.org>
    Reviewed-by: David Chase <drchase@google.com>
    Reviewed-by: Michael Matloob <matloob@google.com>

diff --git a/src/cmd/go/internal/work/exec.go b/src/cmd/go/internal/work/exec.go
index a047c21423..97d1420a26 100644
--- a/src/cmd/go/internal/work/exec.go
+++ b/src/cmd/go/internal/work/exec.go
@@ -1272,6 +1272,13 @@ func (b *Builder) cgoCompileActionID(a *Action, f string, flags []string) cache.
 	// in the resulting .o file.
 	b.addPackageOrigin(h, p)

+	// The file may include the package's header files and the
+	// _cgo_export.h written by the cgo run action.
+	for _, file := range p.HFiles {
+		fmt.Fprintf(h, "hfile %s %s\n", file, b.fileHash(filepath.Join(p.Dir, file)))
+	}
+	fmt.Fprintf(h, "exportheader %s\n", b.fileHash(a.Objdir+"_cgo_export.h"))
+
 	// This has too much information.
 	// We only need to choose CC/CXX/FC based on which kind of compilation this
 	// action is doing. Perhaps we could add a getCCIDFunc to cgoCompileActor?
@@ -1344,6 +1351,10 @@ func (b *Builder) cgoRunActionID(a *Action, cover bool) cache.ActionID {
 	for _, file := range slices.Concat(p.SwigFiles, p.SwigCXXFiles) {
 		fmt.Fprintf(h, "file %s %s\n", file, b.fileHash(filepath.Join(p.Dir, file)))
 	}
+	// Header files may be included by the cgo preamble.
+	for _, file := range p.HFiles {
+		fmt.Fprintf(h, "hfile %s %s\n", file, b.fileHash(filepath.Join(p.Dir, file)))
+	}

 	return h.Sum()
 }
diff --git a/src/cmd/go/testdata/script/cgo_stale_header.txt b/src/cmd/go/testdata/script/cgo_stale_header.txt
new file mode 100644
index 0000000000..15080f148d
--- /dev/null
+++ b/src/cmd/go/testdata/script/cgo_stale_header.txt
@@ -0,0 +1,95 @@
+# Changing a header file must not reuse cached cgo output or C objects.
+
+[!cgo] skip
+[short] skip 'builds and runs cgo binaries'
+
+env GOCACHE=$WORK/cache
+
+# A header file in the package.
+go run ./h
+stdout '^1 10$'
+cp h/x2.h h/x.h
+go run ./h
+stdout '^2 20$'
+
+# _cgo_export.h, generated from the preamble of a file with //export.
+go run ./e
+stdout '^3$'
+cp e/e2.go.txt e/e.go
+go run ./e
+stdout '^6$'
+
+-- go.mod --
+module example.com/m
+
+go 1.26
+-- h/x.h --
+#define V 1
+int get(void);
+-- h/x2.h --
+#define V 2
+int get(void);
+-- h/f.c --
+#include "x.h"
+int get(void) { return V*10; }
+-- h/m.go --
+package main
+
+// #include "x.h"
+import "C"
+
+import "fmt"
+
+func main() { fmt.Println(C.V, C.get()) }
+-- e/e.go --
+package main
+
+/*
+#define N 2
+typedef struct { long a[N]; } S;
+*/
+import "C"
+
+//export F
+func F(s C.S) C.long {
+	var t C.long
+	for _, v := range s.a {
+		t += v
+	}
+	return t
+}
+-- e/e2.go.txt --
+package main
+
+/*
+#define N 3
+typedef struct { long a[N]; } S;
+*/
+import "C"
+
+//export F
+func F(s C.S) C.long {
+	var t C.long
+	for _, v := range s.a {
+		t += v
+	}
+	return t
+}
+-- e/c.go --
+package main
+
+// long callF(void);
+import "C"
+
+import "fmt"
+
+func main() { fmt.Println(C.callF()) }
+-- e/callf.c --
+#include "_cgo_export.h"
+
+long callF(void) {
+	S s;
+	for (int i = 0; i < (int)(sizeof(s.a)/sizeof(s.a[0])); i++)
+		s.a[i] = i+1;
+	return F(s);
+}