chainguard-dev / apko

Build OCI images from APK packages directly without Dockerfile

Home Page:https://apko.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`apko build` ignores `--workdir` flag

sfc-gh-ptabor opened this issue · comments

I spotted that 'apko build' is working outside of directory given using --workdir flag.

I suspect this piece of code: https://github.com/chainguard-dev/apko/blame/1e5f9dbfd40406c0219c30ffa9ee34007814a438/internal/cli/build.go#L146

func BuildCmd(ctx context.Context, imageRef, outputTarGZ string, archs []types.Architecture, tags []string, wantSBOM bool, sbomPath string, logger log.Logger, opts ...build.Option) error {
	wd, err := os.MkdirTemp("", "apko-*")

is Ignoring https://github.com/chainguard-dev/apko/blame/1e5f9dbfd40406c0219c30ffa9ee34007814a438/internal/cli/commands.go#L55C2-L55C2:

cmd.PersistentFlags().StringVarP(&workDir, "workdir", "C", cwd, "working dir (default is current dir where executed)")

@sfc-gh-ptabor can you define "working outside of directory"? What use case did you do that triggered a problem?

I would guess that this is a bug that's been around forever...

We try to Getwd here:

var workDir string
cwd, err := os.Getwd()
if err != nil {
cwd = ""
}

And then change to that dir here:

if cwd != "" {
if err := os.Chdir(cwd); err != nil {
fmt.Printf("failed to change dir to %s: %v\n", cwd, err)
}
}

But that just totally ignores the --workdir flag.

At least for my usage, I tend to invoke apko in a directory with a bunch of apko configs rather than in a directory where I want to populate a filesystem... I don't think we can "fix" this to use the current directory as the workdir because it would break so many existing things, but we could have the default value be a temporary directory (as it currently is now) and have the --workdir flag let you set a specific directory.

Is that the behavior you'd expect?

Or do you want to os.Chdir to the --workdir before invoking apko, but still build to a temporary directory?

I usually do as well, but sometimes you need to reference other dirs, especially in debug mode. Either way, it should work if it is there. Fixed in #830.