Skip to content

Commit

Permalink
feat(go): run "go build" on generated code (#2485)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Elad Ben-Israel authored Jan 28, 2021
1 parent 4b03834 commit d3602ec
Show file tree
Hide file tree
Showing 22 changed files with 1,049 additions and 690 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
extends: ../../../../eslint-config.yaml
extends: ../../../eslint-config.yaml
rules:
'import/no-extraneous-dependencies':
- error
Expand Down
3 changes: 3 additions & 0 deletions packages/@jsii/go-runtime/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
/jsii-calc/
*.generated.go

*.js
*.d.ts
6 changes: 0 additions & 6 deletions packages/@jsii/go-runtime/build-tools/tsconfig.json

This file was deleted.

3 changes: 3 additions & 0 deletions packages/@jsii/go-runtime/lib/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import * as path from 'path';

export const runtimePath = path.resolve(__dirname, '..', 'jsii-runtime-go');
8 changes: 4 additions & 4 deletions packages/@jsii/go-runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
"version": "0.0.0",
"private": true,
"description": "",
"main": "index.js",
"main": "lib/index.js",
"scripts": {
"build": "npm run gen:rt && (cd ./jsii-runtime-go && go build)",
"build": "tsc --build && npm run gen:rt && (cd ./jsii-runtime-go && go build)",
"fmt": "go fmt ./... && (cd ./jsii-runtime-go && go fmt)",
"gen:calc": "ts-node build-tools/gen-calc.ts",
"gen:rt": "ts-node build-tools/gen.ts",
"gen:calc": "node build-tools/gen-calc.js",
"gen:rt": "node build-tools/gen.js",
"generate": "npm run gen:rt && npm run gen:calc",
"lint": "go vet ./... && (cd ./jsii-runtime-go && go vet)",
"lint:fix": "yarn lint --fix",
Expand Down
4 changes: 4 additions & 0 deletions packages/@jsii/go-runtime/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "../../../tsconfig-base",
"include": ["**/*.ts"]
}
33 changes: 23 additions & 10 deletions packages/@scope/jsii-calc-base/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,34 @@
import { IVeryBaseInterface, VeryBaseProps } from '@scope/jsii-calc-base-of-base';
import {
IVeryBaseInterface,
VeryBaseProps,
StaticConsumer as StaticConsumerBase,
} from '@scope/jsii-calc-base-of-base';

/**
* A base class.
*/
export abstract class Base {
/**
* @returns the name of the class (to verify native type names are created for derived classes).
*/
public typeName() {
return (this.constructor as any).name;
}
/**
* @returns the name of the class (to verify native type names are created for derived classes).
*/
public typeName() {
return (this.constructor as any).name;
}
}

export interface BaseProps extends VeryBaseProps {
readonly bar: string;
readonly bar: string;
}

export interface IBaseInterface extends IVeryBaseInterface {
bar(): void;
}
bar(): void;
}

/**
* Hides the transitive dependency of base-of-base
*/
export class StaticConsumer {
public static consume(...args: any[]) {
return StaticConsumerBase.consume(...args);
}
}
48 changes: 41 additions & 7 deletions packages/@scope/jsii-calc-base/test/assembly.jsii
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
"kind": "class",
"locationInModule": {
"filename": "lib/index.ts",
"line": 6
"line": 10
},
"methods": [
{
Expand All @@ -102,7 +102,7 @@
},
"locationInModule": {
"filename": "lib/index.ts",
"line": 10
"line": 14
},
"name": "typeName",
"returns": {
Expand All @@ -124,7 +124,7 @@
"kind": "interface",
"locationInModule": {
"filename": "lib/index.ts",
"line": 15
"line": 19
},
"name": "BaseProps",
"properties": [
Expand All @@ -133,7 +133,7 @@
"immutable": true,
"locationInModule": {
"filename": "lib/index.ts",
"line": 16
"line": 20
},
"name": "bar",
"type": {
Expand All @@ -151,21 +151,55 @@
"kind": "interface",
"locationInModule": {
"filename": "lib/index.ts",
"line": 19
"line": 23
},
"methods": [
{
"abstract": true,
"locationInModule": {
"filename": "lib/index.ts",
"line": 20
"line": 24
},
"name": "bar"
}
],
"name": "IBaseInterface"
},
"@scope/jsii-calc-base.StaticConsumer": {
"assembly": "@scope/jsii-calc-base",
"docs": {
"summary": "Hides the transitive dependency of base-of-base."
},
"fqn": "@scope/jsii-calc-base.StaticConsumer",
"initializer": {},
"kind": "class",
"locationInModule": {
"filename": "lib/index.ts",
"line": 30
},
"methods": [
{
"locationInModule": {
"filename": "lib/index.ts",
"line": 31
},
"name": "consume",
"parameters": [
{
"name": "args",
"type": {
"primitive": "any"
},
"variadic": true
}
],
"static": true,
"variadic": true
}
],
"name": "StaticConsumer"
}
},
"version": "0.0.0",
"fingerprint": "Ajk3MslkfbYCrArr+xpk4fCqUsDaJMcCWIRdaVGDWDo="
"fingerprint": "nk3lZyZihw8+X5KKk4nmX16b78gkSh2KmOCC72ZlkYk="
}
7 changes: 3 additions & 4 deletions packages/jsii-calc/lib/compliance.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as base from '@scope/jsii-calc-base';
import { StaticConsumer } from '@scope/jsii-calc-base-of-base';
import {
EnumFromScopedModule,
IDoublable,
Expand Down Expand Up @@ -291,19 +290,19 @@ export class RuntimeTypeChecking {
* Used to verify verification of number of method arguments.
*/
public methodWithOptionalArguments(arg1: number, arg2: string, arg3?: Date) {
StaticConsumer.consume(arg1, arg2, arg3);
base.StaticConsumer.consume(arg1, arg2, arg3);
}

public methodWithDefaultedArguments(
arg1 = 2,
arg2?: string,
arg3: Date = new Date(),
) {
StaticConsumer.consume(arg1, arg2, arg3);
base.StaticConsumer.consume(arg1, arg2, arg3);
}

public methodWithOptionalAnyArgument(arg?: any) {
StaticConsumer.consume(arg);
base.StaticConsumer.consume(arg);
}
}

Expand Down
4 changes: 1 addition & 3 deletions packages/jsii-calc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,16 @@
"watch": "jsii --project-references -w",
"lint": "eslint . --ext .js,.ts --ignore-path=.gitignore",
"lint:fix": "yarn lint --fix",
"test": "node test/test.calc.js && diff-test test/assembly.jsii .jsii",
"test": "(ls test/test.*.js | xargs -n1 node) && diff-test test/assembly.jsii .jsii",
"test:update": "npm run build && UPDATE_DIFF=1 npm run test"
},
"dependencies": {
"@fixtures/jsii-calc-bundled": "file:../@fixtures/jsii-calc-bundled",
"@scope/jsii-calc-base": "^0.0.0",
"@scope/jsii-calc-base-of-base": "^0.0.0",
"@scope/jsii-calc-lib": "^0.0.0"
},
"peerDependencies": {
"@scope/jsii-calc-base": "^0.0.0",
"@scope/jsii-calc-base-of-base": "^0.0.0",
"@scope/jsii-calc-lib": "^0.0.0"
},
"devDependencies": {
Expand Down
Loading

0 comments on commit d3602ec

Please sign in to comment.