-trimpath records TinyGo C source paths as cache paths

Đang mở Phù hợp với người mới
#6,161 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
85/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
go
Lĩnh vực
build-system

Hướng nghiên cứu

Bắt đầu ở builder, tại Config.CFlags, và kiểm tra cách nó tạo các ánh xạ tiền tố cho TINYGOROOT và GOCACHE; bản tái hiện được cung cấp chỉ ra sự chồng lấp tiền tố bộ nhớ đệm. Thêm bài kiểm thử trong issue vào builder/trimpath_test.go, sau đó chạy TINYGOROOT="$PWD" go test -tags llvm23 ./builder -run '^TestTrimPathCFlagsMapTinyGoSource$' -count=1 -v. Hoàn tất khi cả hai trường hợp đều đạt và DWARF ghi lại đường dẫn mã nguồn TinyGo như mong đợi.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

Problem

When GOCACHE is /tmp/tinygo and TINYGOROOT is the sibling directory /tmp/tinygo-source, the cache prefix map also matches C files under TINYGOROOT. Clang uses the later cache map and records /tmp/tinygo-source/probe.c as /_/tinygo-cache-source/probe.c in DWARF. The expected path is /_/github.com/tinygo-org/tinygo/probe.c.

Reproduction

Add this test as builder/trimpath_test.go. It compiles a C file with the flags from Config.CFlags and reads the DWARF compile-unit path from the ELF object. The separate-directory case passes, but the cache-prefix case fails.

package builder

import (
	"debug/dwarf"
	"debug/elf"
	"os"
	"path/filepath"
	"testing"

	"github.com/tinygo-org/tinygo/compileopts"
	"github.com/tinygo-org/tinygo/goenv"
)

func TestTrimPathCFlagsMapTinyGoSource(t *testing.T) {
	for _, tc := range []struct {
		name    string
		overlap bool
	}{
		{name: "separate"},
		{name: "cache-prefix", overlap: true},
	} {
		t.Run(tc.name, func(t *testing.T) {
			home := t.TempDir()
			t.Setenv("HOME", home)
			t.Setenv("XDG_CACHE_HOME", home)
			t.Setenv("LocalAppData", home)
			cache := goenv.Get("GOCACHE")
			tinygoRoot := filepath.Join(filepath.Dir(cache), "source")
			if tc.overlap {
				tinygoRoot = cache + "-source"
			}
			for _, name := range []string{
				"src/runtime/internal/sys/zversion.go",
				"src/device/arm/arm.go",
			} {
				filename := filepath.Join(tinygoRoot, filepath.FromSlash(name))
				if err := os.MkdirAll(filepath.Dir(filename), 0o755); err != nil {
					t.Fatal(err)
				}
				if err := os.WriteFile(filename, nil, 0o644); err != nil {
					t.Fatal(err)
				}
			}
			t.Setenv("TINYGOROOT", tinygoRoot)

			source := filepath.Join(tinygoRoot, "probe.c")
			if err := os.WriteFile(source, []byte("int probe(void) { return 1; }\n"), 0o644); err != nil {
				t.Fatal(err)
			}
			config := &compileopts.Config{
				Options: &compileopts.Options{TrimPath: true, Opt: "0"},
				Target: &compileopts.TargetSpec{
					Triple: "x86_64-unknown-linux-gnu", GOOS: "linux", GOARCH: "amd64",
				},
			}
			objectPath := filepath.Join(home, "probe.o")
			flags := append([]string{"-c", "-o", objectPath, source}, config.CFlags(false)...)
			if err := runCCompiler(flags...); err != nil {
				t.Fatal(err)
			}
			object, err := elf.Open(objectPath)
			if err != nil {
				t.Fatal(err)
			}
			defer object.Close()
			debugInfo, err := object.DWARF()
			if err != nil {
				t.Fatal(err)
			}
			unit, err := debugInfo.Reader().Next()
			if err != nil {
				t.Fatal(err)
			}
			if unit == nil || unit.Tag != dwarf.TagCompileUnit {
				t.Fatalf("expected a DWARF compile unit, got %v", unit)
			}
			want := config.CSourcePath("github.com/tinygo-org/tinygo/probe.c")
			if got := unit.Val(dwarf.AttrName); got != want {
				t.Errorf("DWARF source path = %q, want %q", got, want)
			}
		})
	}
}

On macOS/arm64 with Homebrew Clang 23.1.4:

TINYGOROOT="$PWD" go test -tags llvm23 ./builder -run '^TestTrimPathCFlagsMapTinyGoSource$' -count=1 -v
--- PASS: TestTrimPathCFlagsMapTinyGoSource/separate
--- FAIL: TestTrimPathCFlagsMapTinyGoSource/cache-prefix
    DWARF source path = "/_/tinygo-cache-source/probe.c", want "/_/github.com/tinygo-org/tinygo/probe.c"

This tests the TINYGOROOT and GOCACHE maps in Config.CFlags. PR #6155 covers the package module map.

Ngôn ngữ chính
Go
Star
17.8k
Fork
1.1k
Merge trung bình
1 ngày 4 giờ
Pull request đã merge (30 ngày)
380

Chuẩn bị môi trường

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của tinygo-org/tinygo

Tất cả issue của tinygo-org/tinygo

Issue tương tự

Thêm issue về Go

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.