chainguard-dev / apko

Build OCI images from APK packages directly without Dockerfile
https://apko.dev
Apache License 2.0
1.17k stars 115 forks source link

`apko build` ignores `--workdir` flag #787

Closed sfc-gh-ptabor closed 1 year ago

sfc-gh-ptabor commented 1 year ago

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)")
deitch commented 1 year ago

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

jonjohnsonjr commented 1 year ago

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

We try to Getwd here: https://github.com/chainguard-dev/apko/blob/c68c72f556a384fa805c0ced7eb9cb823b63edee/internal/cli/commands.go#L28-L32

And then change to that dir here:

https://github.com/chainguard-dev/apko/blob/c68c72f556a384fa805c0ced7eb9cb823b63edee/internal/cli/commands.go#L39-L43

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?

deitch commented 1 year ago

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.