-trimpath records TinyGo C source paths as cache paths

未关闭 适合新手
#6,161 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
85/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
go
领域
build-system

调研方向

从 builder 中的 Config.CFlags 开始,检查它如何创建 TINYGOROOT 和 GOCACHE 的前缀映射;提供的复现指出了缓存前缀重叠问题。将 issue 中的测试添加到 builder/trimpath_test.go,然后运行 TINYGOROOT="$PWD" go test -tags llvm23 ./builder -run '^TestTrimPathCFlagsMapTinyGoSource$' -count=1 -v。两个用例都通过且 DWARF 记录了预期的 TinyGo 源码路径时,即为完成。

由索引模型根据 Issue 内容生成。

描述

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.

主要语言
Go
星标
17.8k
派生
1.1k
平均合并
1 天 4 小时
30 天内合并 PR
380

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

tinygo-org/tinygo 的其他 Issue

查看 tinygo-org/tinygo 的全部 Issue

相似的 Issue

更多 Go Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。