Skip to content

Commit

Permalink
Fix bug with relative import path
Browse files Browse the repository at this point in the history
  • Loading branch information
RobbieMcKinstry committed Oct 6, 2022
1 parent b906f03 commit 6037812
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 13 deletions.
45 changes: 32 additions & 13 deletions pkg/codegen/nodejs/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -1497,7 +1497,7 @@ func (mod *modContext) genConfig(w io.Writer, variables []*schema.Property) erro
externalImports, imports := codegen.NewStringSet(), map[string]codegen.StringSet{}
referencesNestedTypes := mod.getImports(variables, externalImports, imports)

mod.genHeader(w, mod.sdkImports(referencesNestedTypes, true), externalImports, imports)
mod.genHeader(w, mod.sdkImports(referencesNestedTypes, true, ""), externalImports, imports)

fmt.Fprintf(w, "declare var exports: any;\n")

Expand Down Expand Up @@ -1537,17 +1537,34 @@ func (mod *modContext) genConfig(w io.Writer, variables []*schema.Property) erro
return nil
}

func (mod *modContext) getRelativePath() string {
rel, err := filepath.Rel(mod.mod, "")
func (mod *modContext) getRelativePath(dirRoot string) string {
var currPath string
if dirRoot == "" {
currPath = mod.mod
} else {
currPath = dirRoot
}
rel, err := filepath.Rel(currPath, "")
contract.Assert(err == nil)
return path.Dir(filepath.ToSlash(rel))
}

func (mod *modContext) sdkImports(nested, utilities bool) []string {
func (mod *modContext) sdkImports(nested, utilities bool, dirRoot string) []string {
imports := []string{"import * as pulumi from \"@pulumi/pulumi\";"}

relRoot := mod.getRelativePath()
// TODO: Skip writing sdkImports for types.inputs/types.outputs as special case.
relRoot := mod.getRelativePath(dirRoot)
// If this file is generating inputs in /types/, then it will be one level
// lower than otherwise.
// if typegen {
// relRoot = path.Join("..", relRoot)
// }
fmt.Printf("Mod: %s -- relRoot: %s\n", mod.mod, relRoot)
if nested {
// TODO: If we're nested, we need to know HOW FAR we're nested!
// this is only for nested TYPE outputs/inputs. Those are
// potentially nested but ALSO nested wthin the types file,
// which makes matters worse since they could be nested even deeper.
imports = append(imports, []string{
fmt.Sprintf(`import * as inputs from "%s/types/input";`, relRoot),
fmt.Sprintf(`import * as outputs from "%s/types/output";`, relRoot),
Expand All @@ -1570,7 +1587,7 @@ func (mod *modContext) sdkImports(nested, utilities bool) []string {
}

func (mod *modContext) utilitiesImport() string {
relRoot := mod.getRelativePath()
relRoot := mod.getRelativePath("")
return fmt.Sprintf("import * as utilities from \"%s/utilities\";", relRoot)
}

Expand All @@ -1592,7 +1609,7 @@ func (mod *modContext) buildImports() (codegen.StringSet, map[string]codegen.Str
// Instantiating the default might require an environmental variable. This
// uses utilities.
if hasDefaultObjects {
externalImports.Add(fmt.Sprintf("import * as utilities from \"%s/utilities\";", mod.getRelativePath()))
externalImports.Add(fmt.Sprintf("import * as utilities from \"%s/utilities\";", mod.getRelativePath("")))
}
return externalImports, imports
}
Expand Down Expand Up @@ -1686,7 +1703,7 @@ func (ns *namespace) intoIOFiles(ctx *ioContext, parent string) ([]*ioFile, erro
fmt.Printf("Generating namespace into IO file: %s\n", filename)
var file = newIOFile(filename)
// We start every file with the header information.
ctx.mod.genHeader(file.writer(), ctx.mod.sdkImports(true, false), ctx.externalImports, ctx.imports)
ctx.mod.genHeader(file.writer(), ctx.mod.sdkImports(true, false, dirRoot), ctx.externalImports, ctx.imports)
// We want to organize the items in the source file by alphabetical order.
sort.Slice(ns.types, func(i, j int) bool {
return tokenToName(ns.types[i].Token) < tokenToName(ns.types[j].Token)
Expand Down Expand Up @@ -1719,7 +1736,9 @@ func (ns *namespace) intoIOFiles(ctx *ioContext, parent string) ([]*ioFile, erro
// At this level, we export any nested definitions from
// the next level.
var fullPath = path.Join(dirRoot, child.name)
fmt.Fprintf(file.writer(), "export * as %s from \"%s\";\n", child.name, fullPath)
fmt.Printf("-----> Exporting file %s with parent %s\n", fullPath, dirRoot)
//fmt.Fprintf(file.writer(), "export * as %s from \"%s\";\n", child.name, fullPath)
fmt.Fprintf(file.writer(), "export * as %s from \"./%s\";\n", child.name, child.name)
// fmt.Fprintf(file.writer(), "export type { %s };", child.name)
nestedFiles, err := child.intoIOFiles(ctx, dirRoot)
if err != nil {
Expand Down Expand Up @@ -1951,7 +1970,7 @@ func (mod *modContext) gen(fs fs) error {
referencesNestedTypes := mod.getImportsForResource(r, externalImports, imports, r)

buffer := &bytes.Buffer{}
mod.genHeader(buffer, mod.sdkImports(referencesNestedTypes, true), externalImports, imports)
mod.genHeader(buffer, mod.sdkImports(referencesNestedTypes, true, ""), externalImports, imports)

rinfo, err := mod.genResource(buffer, r)
if err != nil {
Expand All @@ -1973,7 +1992,7 @@ func (mod *modContext) gen(fs fs) error {
referencesNestedTypes := mod.getImports(f, externalImports, imports)

buffer := &bytes.Buffer{}
mod.genHeader(buffer, mod.sdkImports(referencesNestedTypes, true), externalImports, imports)
mod.genHeader(buffer, mod.sdkImports(referencesNestedTypes, true, ""), externalImports, imports)

funInfo, err := mod.genFunction(buffer, f)
if err != nil {
Expand Down Expand Up @@ -2053,7 +2072,7 @@ func (mod *modContext) genIndex(exports []fileInfo) string {
var imports []string
// Include the SDK import if we'll be registering module resources.
if len(mod.resources) != 0 {
imports = mod.sdkImports(false /*nested*/, true /*utilities*/)
imports = mod.sdkImports(false, true, "")
} else if len(children) > 0 || len(mod.functions) > 0 {
// Even if there are no resources, exports ref utilities.
imports = append(imports, mod.utilitiesImport())
Expand Down Expand Up @@ -2092,7 +2111,7 @@ func (mod *modContext) genIndex(exports []fileInfo) string {
} else if len(mod.enums) > 0 {
fmt.Fprintf(w, "\n")
fmt.Fprintf(w, "// Export enums:\n")
rel := mod.getRelativePath()
rel := mod.getRelativePath("")
var filePath string
if mod.mod == "" {
filePath = ""
Expand Down
46 changes: 46 additions & 0 deletions pkg/codegen/nodejs/gen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,49 @@ func Test_isStringType(t *testing.T) {
})
}
}

// This test asserts that modContext.getRelativePath()
// returns the right relative path, regardless of whether
// the file is a resource definition or an Input/Output declaration
// from /types/
func TestGetRelativePath(t *testing.T) {
t.Parallel()
type TestCase struct {
mod string
dirRoot string
expected string
}
var cases = []TestCase{
{
mod: "foo",
dirRoot: "",
expected: "..",
}, {
mod: "foo/bar",
dirRoot: "",
expected: "../..",
}, {
mod: "types/accessanalyzer/input",
dirRoot: "",
expected: "../../..",
}, {
mod: "input",
dirRoot: "types/accessanalyzer",
expected: "../..",
},
}
for _, tc := range cases {
var ctx = &modContext{mod: tc.mod}
var observed = ctx.getRelativePath(tc.dirRoot)
require.Equal(
t,
tc.expected,
observed,
"Case (%s, %s): Expected %s, Observed %s",
tc.mod,
tc.dirRoot,
tc.expected,
observed,
)
}
}

0 comments on commit 6037812

Please sign in to comment.