-trimpath records TinyGo C source paths as cache paths

Abierto Apto para principiantes
#6,161 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
2/5
Tiempo estimado
1-3 horas
Aptitud para principiantes
85/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
go
Área
build-system

Línea de trabajo

Empieza en builder, en Config.CFlags, e inspecciona cómo crea los mapas de prefijos de TINYGOROOT y GOCACHE; la reproducción proporcionada identifica el solapamiento de prefijos de caché. Añade la prueba del issue como builder/trimpath_test.go y luego ejecuta TINYGOROOT="$PWD" go test -tags llvm23 ./builder -run '^TestTrimPathCFlagsMapTinyGoSource$' -count=1 -v. El trabajo estará listo cuando pasen ambos casos y DWARF registre la ruta esperada del código fuente de TinyGo.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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.

Lenguaje dominante
Go
Estrellas
17.8k
Forks
1.1k
Merge medio
1 d 4 h
PR fusionados (30 d)
380

Preparar el entorno

Este proyecto no incluye contenedor de desarrollo, Dockerfile ni guía de contribución, así que la configuración corre por tu cuenta: empieza por su README y consulta nuestra guía para la primera contribución para los pasos generales.

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de tinygo-org/tinygo

Todos los issues de tinygo-org/tinygo

Issues similares

Más issues de Go

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.