Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions bundle/libraries/same_name_libraries.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package libraries

import (
"context"
"path/filepath"

"github.com/databricks/cli/bundle"
"github.com/databricks/cli/libs/diag"
"github.com/databricks/cli/libs/dyn"
)

type checkForSameNameLibraries struct{}

var patterns = []dyn.Pattern{
taskLibrariesPattern.Append(dyn.AnyIndex(), dyn.AnyKey()),
forEachTaskLibrariesPattern.Append(dyn.AnyIndex(), dyn.AnyKey()),
envDepsPattern.Append(dyn.AnyIndex()),
}

func (c checkForSameNameLibraries) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics {
var diags diag.Diagnostics
libBaseNames := make(map[string]bool)

err := b.Config.Mutate(func(v dyn.Value) (dyn.Value, error) {
var err error
for _, pattern := range patterns {
v, err = dyn.MapByPattern(v, pattern, func(p dyn.Path, lv dyn.Value) (dyn.Value, error) {
libPath := lv.MustString()
// If not local library, skip the check
if !IsLibraryLocal(libPath) {
return lv, nil
}

lib := filepath.Base(lv.MustString())
if libBaseNames[lib] {
Comment thread
andrewnester marked this conversation as resolved.
Outdated
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "Duplicate local library name",
Comment thread
andrewnester marked this conversation as resolved.
Outdated
Detail: "Local library names must be unique",
Locations: lv.Locations(),
Paths: []dyn.Path{p},
})
}

libBaseNames[lib] = true
return lv, nil
})
if err != nil {
return dyn.InvalidValue, err
}
}

if err != nil {
return dyn.InvalidValue, err
}

return v, nil
})
if err != nil {
diags = diags.Extend(diag.FromErr(err))
}

return diags
}

func (c checkForSameNameLibraries) Name() string {
return "CheckForSameNameLibraries"
}

func CheckForSameNameLibraries() bundle.Mutator {
return checkForSameNameLibraries{}
}
92 changes: 92 additions & 0 deletions bundle/libraries/same_name_libraries_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package libraries

import (
"context"
"testing"

"github.com/databricks/cli/bundle"
"github.com/databricks/cli/bundle/config"
"github.com/databricks/cli/bundle/config/resources"
"github.com/databricks/cli/libs/diag"
"github.com/databricks/databricks-sdk-go/service/compute"
"github.com/databricks/databricks-sdk-go/service/jobs"
"github.com/stretchr/testify/require"
)

func TestSameNameLibraries(t *testing.T) {
b := &bundle.Bundle{
Config: config.Root{
Resources: config.Resources{
Jobs: map[string]*resources.Job{
"test": {
JobSettings: &jobs.JobSettings{
Tasks: []jobs.Task{
{
Libraries: []compute.Library{
{
Whl: "full/path/test.whl",
},
},
},
{
Libraries: []compute.Library{
{
Whl: "other/path/test.whl",
},
},
},
},
},
},
},
},
},
}

diags := bundle.Apply(context.Background(), b, CheckForSameNameLibraries())
require.Len(t, diags, 1)
require.Equal(t, diag.Error, diags[0].Severity)
require.Equal(t, "Duplicate local library name", diags[0].Summary)
Comment thread
andrewnester marked this conversation as resolved.
Outdated
}

func TestSameNameLibrariesWithUniqueLibraries(t *testing.T) {
b := &bundle.Bundle{
Config: config.Root{
Resources: config.Resources{
Jobs: map[string]*resources.Job{
"test": {
JobSettings: &jobs.JobSettings{
Tasks: []jobs.Task{
{
Libraries: []compute.Library{
{
Whl: "full/path/test-0.1.1.whl",
},

{
Whl: "cowsay",
},
},
},
{
Libraries: []compute.Library{
{
Whl: "other/path/test-0.1.0.whl",
},

{
Whl: "cowsay",
},
},
},
},
},
},
},
},
},
}

diags := bundle.Apply(context.Background(), b, CheckForSameNameLibraries())
require.Empty(t, diags)
Comment thread
andrewnester marked this conversation as resolved.
}
5 changes: 5 additions & 0 deletions bundle/phases/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,11 @@ func Deploy(outputHandler sync.OutputHandler) bundle.Mutator {
mutator.ValidateGitDetails(),
artifacts.CleanUp(),
libraries.ExpandGlobReferences(),
// libraries.CheckForSameNameLibraries() needs to be run after we expand glob references so we
// know what are the actual library paths.
// libraries.ExpandGlobReferences() has to be run after the libraries are built and thus this
// mutator is part of the deploy step rather than validate.
libraries.CheckForSameNameLibraries(),
Comment thread
andrewnester marked this conversation as resolved.
libraries.Upload(),
trampoline.TransformWheelTask(),
files.Upload(outputHandler),
Expand Down