Skip to content

Commit cbebb7c

Browse files
sl1pm4tapparentlymart
authored andcommitted
command/init: Don't panic if go-getter-fetched module has version constraint
1 parent 742deca commit cbebb7c

4 files changed

Lines changed: 37 additions & 0 deletions

File tree

internal/initwd/module_install.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,15 @@ func (i *ModuleInstaller) installGoGetterModule(req *earlyconfig.ModuleRequest,
480480
packageAddr, _ := splitAddrSubdir(req.SourceAddr)
481481
hooks.Download(key, packageAddr, nil)
482482

483+
if len(req.VersionConstraints) != 0 {
484+
diags = diags.Append(tfdiags.Sourceless(
485+
tfdiags.Error,
486+
"Invalid version constraint",
487+
fmt.Sprintf("Cannot apply a version constraint to module %q (at %s:%d) because it has a non Registry URL.", req.Name, req.CallPos.Filename, req.CallPos.Line),
488+
))
489+
return nil, diags
490+
}
491+
483492
modDir, err := getter.getWithGoGetter(instPath, req.SourceAddr)
484493
if err != nil {
485494
if _, ok := err.(*MaybeRelativePathErr); ok {

internal/initwd/module_install_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,24 @@ func TestModuleInstaller_error(t *testing.T) {
111111
}
112112
}
113113

114+
func TestModuleInstaller_invalid_version_constraint_error(t *testing.T) {
115+
fixtureDir := filepath.Clean("test-fixtures/invalid-version-constraint")
116+
dir, done := tempChdir(t, fixtureDir)
117+
defer done()
118+
119+
hooks := &testInstallHooks{}
120+
121+
modulesDir := filepath.Join(dir, ".terraform/modules")
122+
inst := NewModuleInstaller(modulesDir, nil)
123+
_, diags := inst.InstallModules(".", false, hooks)
124+
125+
if !diags.HasErrors() {
126+
t.Fatal("expected error")
127+
} else {
128+
assertDiagnosticSummary(t, diags, "Invalid version constraint")
129+
}
130+
}
131+
114132
func TestModuleInstaller_symlink(t *testing.T) {
115133
fixtureDir := filepath.Clean("test-fixtures/local-module-symlink")
116134
dir, done := tempChdir(t, fixtureDir)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.terraform/*
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# This fixture references the github repo at:
2+
# https://github.com/hashicorp/terraform-aws-module-installer-acctest
3+
# However, due to the nature of this test (verifying early error), the URL will not be contacted,
4+
# and the test is safe to execute as part of the normal test suite.
5+
6+
module "acctest_root" {
7+
source = "github.com/hashicorp/terraform-aws-module-installer-acctest"
8+
version = "0.0.1"
9+
}

0 commit comments

Comments
 (0)