Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: CompileCommand may return extraneous files #281

Merged
merged 9 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/toolexec/aspect/oncompile.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (w Weaver) OnCompile(cmd *proxy.CompileCommand) error {
RootConfig: map[string]string{"httpmode": "wrap"},
Lookup: imports.Lookup,
ImportPath: w.ImportPath,
GoVersion: cmd.Flags.GoVersion,
GoVersion: cmd.Flags.Lang,
ModifiedFile: func(file string) string {
return filepath.Join(orchestrionDir, "src", cmd.Flags.Package, filepath.Base(file))
},
Expand Down
87 changes: 87 additions & 0 deletions internal/toolexec/proxy/compile.flags.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 20 additions & 8 deletions internal/toolexec/proxy/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,35 @@ import (
"strings"
)

//go:generate go run github.com/datadog/orchestrion/internal/toolexec/proxy/generator -command=compile

type compileFlagSet struct {
Package string `ddflag:"-p"`
ImportCfg string `ddflag:"-importcfg"`
Output string `ddflag:"-o"`
TrimPath string `ddflag:"-trimpath"`
GoVersion string `ddflag:"-goversion"`
Package string `ddflag:"-p"`
ImportCfg string `ddflag:"-importcfg"`
Output string `ddflag:"-o"`
Lang string `ddflag:"-lang"`
showVersion bool `ddflag:"-V"`
}

// CompileCommand represents a go tool `compile` invocation
type CompileCommand struct {
command
Flags compileFlagSet
Files []string
// WorkDir is the $WORK directory managed by the go toolchain.
WorkDir string
}

func (*CompileCommand) Type() CommandType { return CommandTypeCompile }

func (c *CompileCommand) ShowVersion() bool {
return c.Flags.showVersion
}

// GoFiles returns the list of Go files passed as arguments to cmd
func (cmd *CompileCommand) GoFiles() []string {
files := make([]string, 0, len(cmd.args))
for _, path := range cmd.args {
files := make([]string, 0, len(cmd.Files))
for _, path := range cmd.Files {
if !strings.HasSuffix(path, ".go") {
continue
}
Expand All @@ -47,6 +54,7 @@ func (cmd *CompileCommand) GoFiles() []string {
func (cmd *CompileCommand) AddFiles(files []string) {
paramIdx := len(cmd.args)
cmd.args = append(cmd.args, files...)
cmd.Files = append(cmd.Files, files...)
for i, f := range files {
cmd.paramPos[f] = paramIdx + i
}
Expand All @@ -57,7 +65,11 @@ func parseCompileCommand(args []string) (Command, error) {
return nil, errors.New("unexpected number of command arguments")
}
cmd := CompileCommand{command: NewCommand(args)}
parseFlags(&cmd.Flags, args[1:])
pos, err := cmd.Flags.parse(args[1:])
if err != nil {
return nil, err
}
cmd.Files = pos

if cmd.Flags.ImportCfg != "" {
// The WorkDir is the parent of the stage directory, which is where the importcfg file is located.
Expand Down
31 changes: 23 additions & 8 deletions internal/toolexec/proxy/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
package proxy

import (
"reflect"
"testing"

"github.com/stretchr/testify/require"
Expand All @@ -15,33 +14,49 @@ import (
func TestParseCompile(t *testing.T) {
for name, tc := range map[string]struct {
input []string
stage string
goFiles []string
flags compileFlagSet
}{
"version_print": {
input: []string{"/path/compile", "-V=full"},
stage: ".",
flags: compileFlagSet{
showVersion: true,
},
},
"compile": {
input: []string{"/path/compile", "-o", "/buildDir/b002/a.out", "-p", "mypackage", "-goversion", "go1.42.1337", "-importcfg", "/buildDir/b002/importcfg", "/source/dir/main.go", "/source/dir/file1.go"},
stage: "b002",
input: []string{"/path/compile", "-o", "/buildDir/b002/a.out", "-p", "mypackage", "-lang=go1.42", "-importcfg", "/buildDir/b002/importcfg", "/source/dir/main.go", "/source/dir/file1.go"},
goFiles: []string{"/source/dir/main.go", "/source/dir/file1.go"},
flags: compileFlagSet{
Package: "mypackage",
ImportCfg: "/buildDir/b002/importcfg",
Output: "/buildDir/b002/a.out",
GoVersion: "go1.42.1337",
Lang: "go1.42",
},
},
"nats.go": {
input: []string{"/path/compile", "-o", "/buildDir/b002/a.out", "-p", "github.com/nats-io/nats.go", "-complete", "/path/to/source/file.go"},
goFiles: []string{"/path/to/source/file.go"},
flags: compileFlagSet{
Package: "github.com/nats-io/nats.go",
Output: "/buildDir/b002/a.out",
},
},
} {
if tc.goFiles == nil {
// Simplify comparisons, as goFiles always returns non-nil
tc.goFiles = []string{}
}

if name != "nats.go" {
continue
}
t.Run(name, func(t *testing.T) {
cmd, err := parseCompileCommand(tc.input)
require.NoError(t, err)
require.Equal(t, CommandTypeCompile, cmd.Type())
require.Equal(t, tc.stage, cmd.Stage())
c := cmd.(*CompileCommand)
require.True(t, reflect.DeepEqual(tc.flags, c.Flags))
require.Equal(t, tc.flags, c.Flags)
require.EqualValues(t, tc.goFiles, c.GoFiles())
})
}
}
95 changes: 0 additions & 95 deletions internal/toolexec/proxy/flags.go

This file was deleted.

Loading
Loading