-
Notifications
You must be signed in to change notification settings - Fork 246
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(go): unused imports emitted for type unions (#3664)
When imported types are solely referenced by type unions, a go import is emitted, but is never used (type unions end up represented as opaque `interface{}` type). This causes compilation failures. Added a test case for this scenario in particular, and adjusted go code generation to not emit dependency imports for type unions. These imports may be re-introduced soon, as we are working to add dynamic type checking around type unions in go (at which point those imports would no longer be unused). Fixes #3399
- Loading branch information
1 parent
3abe20f
commit 68a80d9
Showing
5 changed files
with
148 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
{ | ||
"author": { | ||
"email": "[email protected]", | ||
"name": "Amazon Web Services, Inc.", | ||
"organization": true, | ||
"roles": [ | ||
"owner" | ||
] | ||
}, | ||
"description": "A dummy test package", | ||
"fingerprint": "PHONY", | ||
"homepage": "https://aws.github.io/jsii", | ||
"jsiiVersion": "0.0.0-dev", | ||
"license": "Apache-2.0", | ||
"name": "base", | ||
"repository": { | ||
"directory": "packages/jsii-pacmak/test/targets/fixtures", | ||
"type": "git", | ||
"url": "https://github.com/aws/jsii" | ||
}, | ||
"schema": "jsii/0.10.0", | ||
"targets": { | ||
"go": { | ||
"moduleName": "github.com/aws/jsii/packages/jsii-pacmak/test/targets/fixtures" | ||
} | ||
}, | ||
"types": { | ||
"BaseA": { | ||
"assembly": "base", | ||
"fqn": "base.BaseA", | ||
"kind": "interface", | ||
"name": "BaseA" | ||
}, | ||
"BaseB": { | ||
"assembly": "base", | ||
"fqn": "base.BaseB", | ||
"kind": "interface", | ||
"name": "BaseB" | ||
} | ||
}, | ||
"version": "1.2.3" | ||
} |
58 changes: 58 additions & 0 deletions
58
packages/jsii-pacmak/test/targets/fixtures/dependent.jsii.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
{ | ||
"author": { | ||
"email": "[email protected]", | ||
"name": "Amazon Web Services, Inc.", | ||
"organization": true, | ||
"roles": [ | ||
"owner" | ||
] | ||
}, | ||
"dependencies": { | ||
"base": "1.2.3" | ||
}, | ||
"description": "A dummy test package", | ||
"fingerprint": "PHONY", | ||
"homepage": "https://aws.github.io/jsii", | ||
"jsiiVersion": "0.0.0-dev", | ||
"license": "Apache-2.0", | ||
"name": "dependent", | ||
"repository": { | ||
"directory": "packages/jsii-pacmak/test/targets/fixtures", | ||
"type": "git", | ||
"url": "https://github.com/aws/jsii" | ||
}, | ||
"schema": "jsii/0.10.0", | ||
"targets": { | ||
"go": { | ||
"moduleName": "github.com/aws/jsii/packages/jsii-pacmak/test/targets/fixtures" | ||
} | ||
}, | ||
"types": { | ||
"Dependent": { | ||
"assembly": "dependent", | ||
"fqn": "dependent.Dependent", | ||
"kind": "class", | ||
"name": "dependent", | ||
"methods": [ | ||
{ | ||
"name": "getBaseUnion", | ||
"returns": { | ||
"type": { | ||
"union": { | ||
"types": [ | ||
{ | ||
"fqn": "base.BaseA" | ||
}, | ||
{ | ||
"fqn": "base.BaseB" | ||
} | ||
] | ||
} | ||
} | ||
} | ||
} | ||
] | ||
} | ||
}, | ||
"version": "4.5.6" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { promises as fs } from 'fs'; | ||
import { TypeSystem } from 'jsii-reflect'; | ||
import { Rosetta } from 'jsii-rosetta'; | ||
import { tmpdir } from 'os'; | ||
import { join } from 'path'; | ||
|
||
import { Golang } from '../../lib/targets/go'; | ||
|
||
test('does not generate imports for unused types', async () => { | ||
const outDir = await fs.mkdtemp(join(tmpdir(), 'pacmak-golang-')); | ||
try { | ||
const tarball = join(outDir, 'mock-tarball.tgz'); | ||
await fs.writeFile(tarball, 'Mock Tarball'); | ||
|
||
const typeSystem = new TypeSystem(); | ||
await typeSystem.load(require.resolve('./fixtures/base.jsii.json')); | ||
const assembly = await typeSystem.load( | ||
require.resolve('./fixtures/dependent.jsii.json'), | ||
); | ||
|
||
const rosetta = new Rosetta(); | ||
const subject = new Golang({ | ||
arguments: {}, | ||
assembly, | ||
packageDir: '', | ||
rosetta, | ||
targetName: 'golang', | ||
}); | ||
|
||
await subject.generateCode(outDir, tarball); | ||
|
||
await expect( | ||
fs.readFile(join(outDir, assembly.name, `${assembly.name}.go`), 'utf-8'), | ||
).resolves.not.toContain( | ||
'github.com/aws/jsii/packages/jsii-pacmak/test/targets/fixtures/base', | ||
); | ||
} finally { | ||
await fs.rm(outDir, { recursive: true }); | ||
} | ||
}); |