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

feat(go): run "go build" on generated code #2485

Merged
merged 23 commits into from
Jan 28, 2021
Merged

Conversation

eladb
Copy link
Contributor

@eladb eladb commented Jan 26, 2021

To ensure the generated go code is formatted and compiles by executing go fmt
and go build on the output directory.

Similar to other languages, in order to be able to build the go module within mono-repos (and in the jsii repo itself), we need to be able to resolve local dependencies. This is done in the following way:

  1. Create a list of dist/go candidates (based on the jsii.outdir relative to dependency package dirs)
  2. Add to this list the current outDir, in case --outdir is used
  3. Iterate over the module's dependencies (recursively) and for each candidate dist directory, check if we can find a <goModule>/go.mod file that contains a module <moduleName> directive.
  4. If we find one, we append a replace directive with a relative path to our own go.mod which tells go fmt and go build to find the dependency locally.
  5. At the end, we restore the original go.mod file so that the published one will not include the replace directives.

TESTING: this is primarily tested using the test:build target of jsii-pacmak which generates & builds the go code for the "calc" fixture (in all pacmak execution variants).

NOTE: running go fmt on the generated sources turned out to be problematic because sources are generated in parallel by default, and therefore it was not possible to resolve local dependencies at that time (which is required since go fmt processes go.mod).

Resolves #2463


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

To ensure the generated go code is formatted and compiles by executing `go fmt`
and `go build` on the output directory.

In order to be able to do that within mono-repos (and in the jsii repo itself), we need to be able to resolve local dependencies (same as all other languages).

Resolves #2463
@mergify mergify bot added the contribution/core This is a PR that came from AWS. label Jan 26, 2021
@eladb eladb marked this pull request as draft January 26, 2021 19:55
Elad Ben-Israel added 3 commits January 26, 2021 22:36
- Restore original `go.mod` so the published one does not have the “replace” directives
- Only add `replace` if the dependency’s `go.mod` file includes the correct module name.
@eladb eladb marked this pull request as ready for review January 26, 2021 21:30
Copy link
Contributor

@MrArnoldPalmer MrArnoldPalmer left a comment

Choose a reason for hiding this comment

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

Code looks good. Wondering if we should exclude 'go build' if the code-only option is set? Formatting would still be wanted so resolving dependencies is still necessary.

@eladb
Copy link
Contributor Author

eladb commented Jan 27, 2021

Code looks good. Wondering if we should exclude 'go build' if the code-only option is set? Formatting would still be wanted so resolving dependencies is still necessary.

Currently both fmt and build are executed only when we are actually building. I can look into resolving deps in the codegen case but I am currently leaning towards not formatting at all for now.

Elad Ben-Israel added 4 commits January 27, 2021 10:29
The module @scope/jsii-calc-base-of-base is intended to be used as a _transitive_ dependency and not direct. Add a test to ensure this doesn't regress in the future.
@mergify
Copy link
Contributor

mergify bot commented Jan 27, 2021

The title of this Pull Request does not conform with [Conventional Commits] guidelines. It will need to be adjusted before the PR can be merged.
[Conventional Commits]: https://www.conventionalcommits.org

@mergify
Copy link
Contributor

mergify bot commented Jan 27, 2021

The title of this Pull Request does not conform with [Conventional Commits] guidelines. It will need to be adjusted before the PR can be merged.
[Conventional Commits]: https://www.conventionalcommits.org

@eladb eladb changed the title feat(go): run go fmt and go build after code generation feat(go): run "go build" on generated go output Jan 27, 2021
packages/jsii-pacmak/lib/targets/go.ts Outdated Show resolved Hide resolved
packages/jsii-pacmak/lib/targets/go.ts Outdated Show resolved Hide resolved
packages/jsii-pacmak/lib/targets/go.ts Outdated Show resolved Hide resolved
packages/jsii-pacmak/lib/targets/go.ts Outdated Show resolved Hide resolved
@eladb eladb changed the title feat(go): run "go build" on generated go output feat(go): run "go build" on generated code Jan 28, 2021
packages/jsii-calc/package.json Show resolved Hide resolved
packages/jsii-pacmak/lib/targets/go.ts Show resolved Hide resolved
packages/jsii-pacmak/lib/targets/go.ts Outdated Show resolved Hide resolved
packages/jsii-pacmak/package.json Show resolved Hide resolved
@eladb eladb requested a review from RomainMuller January 28, 2021 09:45
@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildProject6AEA49D1-5lHf64IXfvmr
  • Commit ID: e1d1a11
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@RomainMuller RomainMuller merged commit d3602ec into main Jan 28, 2021
@RomainMuller RomainMuller deleted the benisrae/go-build branch January 28, 2021 11:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contribution/core This is a PR that came from AWS.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Go: run go build after code generation
4 participants