Skip to content

Commit

Permalink
Merge #11440 #11441
Browse files Browse the repository at this point in the history
11440: Cleanup import generation r=iwahbe a=iwahbe

Cleanup the `getPulumiImport` function.

In doing so, remove an edge causing extraneous `/index` in #11427. 


For reviewers, looking at the diff for this PR is painful. It's much easier to review the old and new code separately.

Part of #11427.

11441: Changelog and go.mod updates for v3.47.2 r=pulumi-bot a=pulumi-bot

bors merge

Co-authored-by: Ian Wahbe <ian@wahbe.com>
Co-authored-by: github-actions <github-actions@github.com>
  • Loading branch information
3 people committed Nov 23, 2022
3 parents c19597a + bff689c + 1c521a4 commit 28be04e
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 47 deletions.
14 changes: 14 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,19 @@
# Changelog

## 3.47.2 (2022-11-22)


### Features

- [cli] Add prompt to `up` to use experimental update plans.
[#11353](https://github.com/pulumi/pulumi/pull/11353)


### Bug Fixes

- [sdk/python] Don't error on type mismatches when using input values for outputs
[#11422](https://github.com/pulumi/pulumi/pull/11422)

## 3.47.1 (2022-11-18)


Expand Down

This file was deleted.

This file was deleted.

62 changes: 25 additions & 37 deletions pkg/codegen/go/gen_program.go
Expand Up @@ -363,7 +363,7 @@ func (g *generator) collectImports(
program *pcl.Program,
stdImports,
pulumiImports,
preambleHelperMethods codegen.StringSet) (codegen.StringSet, codegen.StringSet, codegen.StringSet) {
preambleHelperMethods codegen.StringSet) {
// Accumulate import statements for the various providers
for _, n := range program.Nodes {
if r, isResource := n.(*pcl.Resource); isResource {
Expand Down Expand Up @@ -432,8 +432,6 @@ func (g *generator) collectImports(
})
contract.Assert(len(diags) == 0)
}

return stdImports, pulumiImports, preambleHelperMethods
}

func (g *generator) collectConvertImports(
Expand Down Expand Up @@ -492,48 +490,38 @@ func (g *generator) getGoPackageInfo(pkg string) (GoPackageInfo, bool) {
return info, ok
}

func (g *generator) getPulumiImport(pkg, vPath, mod, name string) string {
info, _ := g.getGoPackageInfo(pkg)
if m, ok := info.ModuleToPackage[mod]; ok {
mod = m
}
func (g *generator) getPulumiImport(pkg, versionPath, mod, name string) string {
// We do this before we let the user set overrides. That way the user can still have a
// module named IndexToken.
info, _ := g.getGoPackageInfo(pkg) // We're allowing `info` to be zero-initialized

imp := fmt.Sprintf("github.com/pulumi/pulumi-%s/sdk%s/go/%s/%s", pkg, vPath, pkg, mod)
// namespaceless invokes "aws:index:..."
if mod == "" {
imp = fmt.Sprintf("github.com/pulumi/pulumi-%s/sdk%s/go/%s", pkg, vPath, pkg)
}
importPath := func(mod string) string {
importBasePath := info.ImportBasePath
if importBasePath == "" {
importBasePath = fmt.Sprintf("github.com/pulumi/pulumi-%s/sdk%s/go/%s", pkg, versionPath, pkg)
}

// All providers don't follow the sdk/go/<package> scheme. Allow ImportBasePath as
// a means to override this assumption.
if info.ImportBasePath != "" {
if mod != "" {
imp = fmt.Sprintf("%s/%s", info.ImportBasePath, mod)
} else {
imp = info.ImportBasePath
if mod != "" && mod != IndexToken {
return fmt.Sprintf("%s/%s", importBasePath, mod)
}
return importBasePath
}

if alias, ok := info.PackageImportAliases[imp]; ok {
return fmt.Sprintf("%s %q", alias, imp)
if m, ok := info.ModuleToPackage[mod]; ok {
mod = m
}

modSplit := strings.Split(mod, "/")
// account for mods like "eks/ClusterVpcConfig" index...
if len(modSplit) > 1 {
if modSplit[0] == "" || modSplit[0] == IndexToken {
imp = fmt.Sprintf("github.com/pulumi/pulumi-%s/sdk%s/go/%s", pkg, vPath, pkg)
if info.ImportBasePath != "" {
imp = info.ImportBasePath
}
} else {
imp = fmt.Sprintf("github.com/pulumi/pulumi-%s/sdk%s/go/%s/%s", pkg, vPath, pkg, modSplit[0])
if info.ImportBasePath != "" {
imp = fmt.Sprintf("%s/%s", info.ImportBasePath, modSplit[0])
}
}
path := importPath(mod)
if alias, ok := info.PackageImportAliases[path]; ok {
return fmt.Sprintf("%s %q", alias, path)
}
return fmt.Sprintf("%q", imp)

// Trim off anything after the first '/'.
// This handles transforming modules like s3/bucket to s3 (as found in
// aws:s3/bucket:Bucket).
mod = strings.SplitN(mod, "/", 2)[0]

return fmt.Sprintf("%#v", importPath(mod))
}

// genPostamble closes the method
Expand Down
2 changes: 1 addition & 1 deletion pkg/go.mod
Expand Up @@ -36,7 +36,7 @@ require (
github.com/nbutton23/zxcvbn-go v0.0.0-20180912185939-ae427f1e4c1d
github.com/opentracing/opentracing-go v1.2.0
github.com/pgavlin/goldmark v1.1.33-0.20200616210433-b5eb04559386
github.com/pulumi/pulumi/sdk/v3 v3.47.1
github.com/pulumi/pulumi/sdk/v3 v3.47.2
github.com/santhosh-tekuri/jsonschema/v5 v5.0.0
github.com/sergi/go-diff v1.2.0
github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966
Expand Down
2 changes: 1 addition & 1 deletion tests/go.mod
Expand Up @@ -12,7 +12,7 @@ require (
github.com/blang/semver v3.5.1+incompatible
github.com/golang/protobuf v1.5.2
github.com/pulumi/pulumi/pkg/v3 v3.34.1
github.com/pulumi/pulumi/sdk/v3 v3.47.1
github.com/pulumi/pulumi/sdk/v3 v3.47.2
github.com/stretchr/testify v1.8.0
google.golang.org/grpc v1.49.0
sourcegraph.com/sourcegraph/appdash v0.0.0-20211028080628-e2786a622600
Expand Down

0 comments on commit 28be04e

Please sign in to comment.