-trimpath records TinyGo C source paths as cache paths

オープン 初心者向け
#6,161 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
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時間
マージ済み PR(30日)
380

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

tinygo-org/tinygo のほかの issue

tinygo-org/tinygo の issue をすべて見る

似ている issue

Go の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。