From 73fc2b3d9523562f63118a3f6cbbe56dace3a1ee Mon Sep 17 00:00:00 2001
From: Jessica Yuen <im.jessicayuen@gmail.com>
Date: Fri, 23 Feb 2018 15:33:47 -0800
Subject: [PATCH] Fix bug with invalid base.libsonnet import path

There exists a bug in the reference path of base.libsonnet for nested
environment directories. We are referencing a hardcoded import
"../base.libsonnet". The works for top level environment directories but
obviously does not work for nested directories.

This commit will add the environments directory as a global lib path.

Signed-off-by: Jessica Yuen <im.jessicayuen@gmail.com>
---
 cmd/diff.go                                               | 4 ++--
 cmd/root.go                                               | 4 ++--
 .../fixtures/sampleapp/environments/default/main.jsonnet  | 2 +-
 metadata/environment.go                                   | 6 +-----
 metadata/environment_test.go                              | 2 +-
 metadata/interface.go                                     | 2 +-
 metadata/manager.go                                       | 4 ++--
 metadata/manager_test.go                                  | 8 ++++----
 8 files changed, 14 insertions(+), 18 deletions(-)

diff --git a/cmd/diff.go b/cmd/diff.go
index 7c2a6e14..bb5ed355 100644
--- a/cmd/diff.go
+++ b/cmd/diff.go
@@ -295,7 +295,7 @@ func expandEnvObjs(fs afero.Fs, cmd *cobra.Command, env string, manager metadata
 		return nil, err
 	}
 
-	_, vendorPath := manager.LibPaths()
+	envPath, vendorPath := manager.LibPaths()
 	libPath, mainPath, paramsPath, err := manager.EnvPaths(env)
 	if err != nil {
 		return nil, err
@@ -315,7 +315,7 @@ func expandEnvObjs(fs afero.Fs, cmd *cobra.Command, env string, manager metadata
 		return nil, err
 	}
 
-	expander.FlagJpath = append([]string{string(vendorPath), string(libPath)}, expander.FlagJpath...)
+	expander.FlagJpath = append([]string{string(vendorPath), string(libPath), string(envPath)}, expander.FlagJpath...)
 	expander.ExtCodes = append([]string{baseObj, params, envSpec}, expander.ExtCodes...)
 
 	envFiles := []string{string(mainPath)}
diff --git a/cmd/root.go b/cmd/root.go
index 97970a34..76c2d185 100644
--- a/cmd/root.go
+++ b/cmd/root.go
@@ -268,13 +268,13 @@ func (te *cmdObjExpander) Expand() ([]*unstructured.Unstructured, error) {
 		return nil, errors.Wrap(err, "find metadata")
 	}
 
-	_, vendorPath := manager.LibPaths()
+	envPath, vendorPath := manager.LibPaths()
 	libPath, mainPath, _, err := manager.EnvPaths(te.config.env)
 	if err != nil {
 		return nil, err
 	}
 
-	expander.FlagJpath = append([]string{string(vendorPath), string(libPath)}, expander.FlagJpath...)
+	expander.FlagJpath = append([]string{string(vendorPath), string(libPath), string(envPath)}, expander.FlagJpath...)
 
 	namespacedComponentPaths, err := component.MakePathsByNamespace(te.config.fs, manager, te.config.cwd, te.config.env)
 	if err != nil {
diff --git a/integration/fixtures/sampleapp/environments/default/main.jsonnet b/integration/fixtures/sampleapp/environments/default/main.jsonnet
index 23010d3d..a6363c74 100644
--- a/integration/fixtures/sampleapp/environments/default/main.jsonnet
+++ b/integration/fixtures/sampleapp/environments/default/main.jsonnet
@@ -1,4 +1,4 @@
-local base = import "../base.libsonnet";
+local base = import "base.libsonnet";
 local k = import "k.libsonnet";
 
 base + {
diff --git a/metadata/environment.go b/metadata/environment.go
index 14f86701..7b0c2efa 100644
--- a/metadata/environment.go
+++ b/metadata/environment.go
@@ -464,12 +464,8 @@ func (m *manager) cleanEmptyParentDirs(name string) error {
 }
 
 func (m *manager) generateOverrideData() []byte {
-	const (
-		relBaseLibsonnetPath = "../" + baseLibsonnetFile
-	)
-
 	var buf bytes.Buffer
-	buf.WriteString(fmt.Sprintf("local base = import \"%s\";\n", relBaseLibsonnetPath))
+	buf.WriteString(fmt.Sprintf("local base = import \"%s\";\n", baseLibsonnetFile))
 	buf.WriteString(fmt.Sprintf("local k = import \"%s\";\n\n", lib.ExtensionsLibFilename))
 	buf.WriteString("base + {\n")
 	buf.WriteString("  // Insert user-specified overrides here. For example if a component is named \"nginx-deployment\", you might have something like:\n")
diff --git a/metadata/environment_test.go b/metadata/environment_test.go
index 572c3439..ef74fc11 100644
--- a/metadata/environment_test.go
+++ b/metadata/environment_test.go
@@ -249,7 +249,7 @@ func TestSetEnvironment(t *testing.T) {
 func TestGenerateOverrideData(t *testing.T) {
 	m := mockEnvironments(t, "test-gen-override-data")
 
-	expected := `local base = import "../base.libsonnet";
+	expected := `local base = import "base.libsonnet";
 local k = import "k.libsonnet";
 
 base + {
diff --git a/metadata/interface.go b/metadata/interface.go
index d60dcd82..a2ef70c9 100644
--- a/metadata/interface.go
+++ b/metadata/interface.go
@@ -37,7 +37,7 @@ var defaultFilePermissions = os.FileMode(0644)
 // libraries; and other non-core-application tasks.
 type Manager interface {
 	Root() string
-	LibPaths() (libPath, vendorPath string)
+	LibPaths() (envPath, vendorPath string)
 	EnvPaths(env string) (libPath, mainPath, paramsPath string, err error)
 
 	// Components API.
diff --git a/metadata/manager.go b/metadata/manager.go
index a6c07d02..9bae3038 100644
--- a/metadata/manager.go
+++ b/metadata/manager.go
@@ -181,8 +181,8 @@ func (m *manager) Root() string {
 	return m.rootPath
 }
 
-func (m *manager) LibPaths() (libPath, vendorPath string) {
-	return m.libPath, m.vendorPath
+func (m *manager) LibPaths() (envPath, vendorPath string) {
+	return m.environmentsPath, m.vendorPath
 }
 
 func (m *manager) createUserDirTree() error {
diff --git a/metadata/manager_test.go b/metadata/manager_test.go
index a8f02018..9ecb855c 100644
--- a/metadata/manager_test.go
+++ b/metadata/manager_test.go
@@ -179,12 +179,12 @@ func TestFindSuccess(t *testing.T) {
 func TestLibPaths(t *testing.T) {
 	appName := "test-lib-paths"
 	expectedVendorPath := path.Join(appName, vendorDir)
-	expectedLibPath := path.Join(appName, libDir)
+	expectedEnvPath := path.Join(appName, environmentsDir)
 	m := mockEnvironments(t, appName)
 
-	libPath, vendorPath := m.LibPaths()
-	if libPath != expectedLibPath {
-		t.Fatalf("Expected lib path to be:\n  '%s'\n, got:\n  '%s'", expectedLibPath, libPath)
+	envPath, vendorPath := m.LibPaths()
+	if envPath != expectedEnvPath {
+		t.Fatalf("Expected env path to be:\n  '%s'\n, got:\n  '%s'", expectedEnvPath, envPath)
 	}
 	if vendorPath != expectedVendorPath {
 		t.Fatalf("Expected vendor lib path to be:\n  '%s'\n, got:\n  '%s'", expectedVendorPath, vendorPath)
-- 
GitLab