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

Support for go modules #19

Closed
wants to merge 0 commits into from
Closed

Support for go modules #19

wants to merge 0 commits into from

Conversation

paivagustavo
Copy link

@paivagustavo paivagustavo commented Mar 26, 2019

I took some inspiration based on the @jonjohnsonjr PoC google/go-containerregistry/pull/329

And take it futher using the new https://godoc.org/golang.org/x/tools/go/packages package to work with both modules and normal go projects.

I'm not yet satisfied how the codes look but from my first tests it seems to being working. Will try to implement tests to this, don't know how to actually test this.

Fixes #7

@paivagustavo
Copy link
Author

paivagustavo commented Mar 26, 2019

@jonjohnsonjr since you were the one who did the PoC and the first attempt, could you please do an initial review?

@paivagustavo
Copy link
Author

I may have broken the kodata test. Guess I still have some work to do. Having some trouble to discover the absolute path for a given package.

pkg/build/gobuild.go Outdated Show resolved Hide resolved
@paivagustavo
Copy link
Author

@jonjohnsonjr I've cleaned up the code and it's working for both modules and non modules projects. It still missing some test, I can't figure out how to do it.

Maybe create another "ko test" command but with modules and force the export GO111MODULE=on ? I still don't think that will actually test correctly this.

Maybe even create a go module project on the /tmp?

I would love some opinions on this.

@jonjohnsonjr
Copy link
Collaborator

I ended up using the go list command to find out the absolute path, that worked just fine.

I think by default packages.Load dispatches to go list under the hood. Is there a way to call packages.Load to do what you're doing by shelling out to go list? Not a huge deal, would just be a bit nicer.

It still missing some test, I can't figure out how to do it.

Which test and what's the issue?

@paivagustavo
Copy link
Author

I think by default packages.Load dispatches to go list under the hood. Is there a way to call packages.Load to do what you're doing by shelling out to go list? Not a huge deal, would just be a bit nicer.

I guess packages.Load could do it, but is it really necessary?

There is two points I can see for this:

  • packages.Load maybe an unnecessary abstract on top of go list for this use case.
  • on the other hand, it might be safer to use it since any breaking changes to go list is going to handled by packages.Load

I actually prefer to use go list direct because I feel that it's simpler but if you want to go on the other path I'm totally down with it too.

Which test and what's the issue?

I feel that current tests doesn't assure that ko actually works with modules.

@jonjohnsonjr
Copy link
Collaborator

I actually prefer to use go list direct because I feel that it's simpler

That seems fine since we already shell out to go build. Maybe just add a TODO to use packages instead if we find that using go list directly isn't reliable.

I feel that current tests doesn't assure that ko actually works with modules.

Ah, got it. Creating a module inside a ioutil.TempDir seems reasonable to me.

@paivagustavo
Copy link
Author

@jonjohnsonjr I've added some tests for it. I had to add an extra abstraction to set the working directory of the build process, so I can fake that I'm running ko from the go module project that is on the ioutil.Tempdir.

I have duplicated some test code just to validate the idea. If you feel like this is enough i'll refactor to be easier to add more test later.

Copy link
Collaborator

@jonjohnsonjr jonjohnsonjr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey sorry for taking so long to get back to this!

pkg/build/gobuild.go Outdated Show resolved Hide resolved
pkg/build/gobuild.go Outdated Show resolved Hide resolved
pkg/build/gobuild.go Outdated Show resolved Hide resolved
return "", errors.New("could not find specified import path: " + importPath)
}

output := outputs[0]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this ever return more than 0 or 1 results?

}
os.Mkdir(filepath.Join(tmpDir, "kodata"), 0777)

writeFile(t, filepath.Join(tmpDir, "main.go"), `
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to pull these out into some checked-in files, and just copy them to a tmpdir? It's kind of ugly with them inlined here. Is this just replicating these files? You could just copy that directory to tmpDir and then write the go.mod file (and maybe fix up symlinks? not sure).


// withWd is a functional option for overriding the working directory
// where ko is going run. This is exposed for testing.
func withWd(wd string) Option {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you avoid adding this by just doing os.Chdir in the test? (You probably would want to defer a Chdir back to the result of Getwd to avoid messing up other tests).

@jonjohnsonjr
Copy link
Collaborator

@paivagustavo any chance of picking this back up?

@paivagustavo
Copy link
Author

Hi @jonjohnsonjr I totally forgot about this. I'll try to finish finish this week :)

@paivagustavo
Copy link
Author

Just replying here to say that I'll finish this tomorrow :)

@paivagustavo
Copy link
Author

paivagustavo commented Jul 2, 2019

git is hard 😂

I have done a wrong push 😭 and so this PR was automatically closed.

Moving to #50

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ko does not work with Go modules
2 participants