// InvokeGo returns the stdout of a go command invocation.
InvokeGo returns the stdout of a go command invocation.
// InvokeGo returns the stdout of a go command invocation .
func InvokeGo(ctx context.Context, env []string, dir string, usesExportData bool, args ...string) (*bytes.Buffer, error) {
	if debug {
		defer func(start time.Time) { log.Printf("%s for %v", time.Since(start), cmdDebugStr(env, args...)) }(time.Now())
	}
	stdout := new(bytes.Buffer)
	stderr := new(bytes.Buffer)
	cmd := exec.CommandContext(ctx, "go", args...)
	cmd.Env = append(append([]string{}, env...), "PWD="+dir)
	cmd.Dir = dir
	cmd.Stdout = stdout
	cmd.Stderr = stderr
	if err := cmd.Run(); err != nil {
		exitErr, ok := err.(*exec.ExitError)
		if !ok {
			return nil, fmt.Errorf("couldn't exec 'go %v': %s %T", args, err, err)
		}
		if !usesExportData {
			return nil, fmt.Errorf("go %v: %s: %s", args, exitErr, stderr)
		}
	}
	if len(stderr.Bytes()) != 0 && os.Getenv("GOPACKAGESPRINTGOLISTERRORS") != "" {
		fmt.Fprintf(os.Stderr, "%s stderr: <<%s>>\n", cmdDebugStr(env, args...), stderr)
	}
	if false {
		fmt.Fprintf(os.Stderr, "%s stdout: <<%s>>\n", cmdDebugStr(env, args...), stdout)
	}
	return stdout, nil
}
func InvokeGo ( ctx context . Context , env [ ] string , dir string , usesExportData bool , args ... string ) ( * bytes . Buffer , error ) { if debug { defer func ( start time . Time ) { log . Printf ( " " , time . Since ( start ) , cmdDebugStr ( env , args ... ) ) } ( time . Now ( ) ) } stdout := new ( bytes . Buffer ) stderr := new ( bytes . Buffer ) cmd := exec . CommandContext ( ctx , " " , args ... ) cmd . Env = append ( append ( [ ] string { } , env ... ) , " " + dir ) cmd . Dir = dir cmd . Stdout = stdout cmd . Stderr = stderr if err := cmd . Run ( ) ; err != nil { exitErr , ok := err . ( * exec . ExitError ) if ! ok { return nil , fmt . Errorf ( " " , args , err , err ) } if ! usesExportData { return nil , fmt . Errorf ( " " , args , exitErr , stderr ) } } if len ( stderr . Bytes ( ) ) != 0 && os . Getenv ( " " ) != " " { fmt . Fprintf ( os . Stderr , " \n " , cmdDebugStr ( env , args ... ) , stderr ) } if false { fmt . Fprintf ( os . Stderr , " \n " , cmdDebugStr ( env , args ... ) , stdout ) } return stdout , nil }
InvokeGo returns the stdout of a go command invocation.
InvokeGo returns the stdout of a go command invocation .
// On darwin the cwd gets resolved to the real path, which breaks anything that // expects the working directory to keep the original path, including the // go command when dealing with modules. // The Go stdlib has a special feature where if the cwd and the PWD are the // same node then it trusts the PWD, so by setting it in the env for the child // process we fix up all the paths returned by the go command. // Catastrophic error: // - executable not found // - context cancellation // Export mode entails a build. // If that build fails, errors appear on stderr // (despite the -e flag) and the Export field is blank. // Do not fail in that case. // As of writing, go list -export prints some non-fatal compilation // errors to stderr, even with -e set. We would prefer that it put // them in the Package.Error JSON (see https://golang.org/issue/26319). // In the meantime, there's nowhere good to put them, but they can // be useful for debugging. Print them if $GOPACKAGESPRINTGOLISTERRORS // is set. // debugging