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
17 changes: 13 additions & 4 deletions internal/tf/cache/services/provider_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"path"
"path/filepath"
"slices"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -318,12 +319,20 @@ func (cache *ProviderCache) warmUp(ctx context.Context) error {
return errors.Errorf("not found provider download url")
}

downloadURLExists, err := vfs.FileExists(fs, cache.DownloadURL)
if err != nil {
return errors.New(err)
// DownloadURL can be a local file path or a remote URL.
// On Windows, passing a URL to FileExists fails because the colon in "https:" is invalid path syntax.
var downloadURLIsLocalFile bool
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: I feel like this would be better served as a private function call.


if !strings.Contains(cache.DownloadURL, "://") {
var err error

downloadURLIsLocalFile, err = vfs.FileExists(fs, cache.DownloadURL)
if err != nil {
return errors.New(err)
}
}

if downloadURLExists {
if downloadURLIsLocalFile {
cache.archivePath = cache.DownloadURL
} else {
if err := util.DoWithRetry(ctx, fmt.Sprintf("Fetching provider %s", cache.Provider), maxRetriesFetchFile, retryDelayFetchFile, cache.logger, log.DebugLevel, func(ctx context.Context) error {
Expand Down
10 changes: 10 additions & 0 deletions test/fixtures/provider-cache/windows-remote-url/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
terraform {
required_providers {
null = {
source = "hashicorp/null"
version = "3.2.4"
}
}
}

resource "null_resource" "example" {}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Intentionally empty
22 changes: 22 additions & 0 deletions test/integration_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const (
testFixtureLocalRelativeArgsWindowsDownloadPath = "fixtures/download/local-windows"
testFixtureManifestRemoval = "fixtures/manifest-removal"
testFixtureTflintNoIssuesFound = "fixtures/tflint/no-issues-found"
testFixtureProviderCacheWindowsRemoteURL = "fixtures/provider-cache/windows-remote-url"

tempDir = `C:\tmp`
)
Expand Down Expand Up @@ -228,6 +229,27 @@ func TestWindowsScaffoldRef(t *testing.T) {
assert.NoError(t, err)
}

// TestWindowsProviderCacheWithRemoteURL verifies that provider caching works on Windows
// when providers are fetched from remote URLs. Previously, passing a URL like
// "https://github.com/..." to os.Stat caused a "syntax is incorrect" error on Windows
// because the colon in "https:" is invalid Windows path syntax.
func TestWindowsProviderCacheWithRemoteURL(t *testing.T) {
t.Parallel()

helpers.CleanupTerraformFolder(t, testFixtureProviderCacheWindowsRemoteURL)
tmpEnvPath := helpers.CopyEnvironment(t, testFixtureProviderCacheWindowsRemoteURL)
rootPath := filepath.Join(tmpEnvPath, testFixtureProviderCacheWindowsRemoteURL)

providerCacheDir := filepath.Join(t.TempDir(), "provider-cache-windows-test")

helpers.RunTerragrunt(t, fmt.Sprintf("terragrunt run init --provider-cache --provider-cache-dir %s --non-interactive --working-dir %s", providerCacheDir, rootPath))

// Verify the provider was cached
entries, err := os.ReadDir(providerCacheDir)
require.NoError(t, err)
assert.NotEmpty(t, entries, "provider cache dir should not be empty after init")
}

func CopyEnvironmentToPath(t *testing.T, environmentPath, targetPath string) {
if err := os.MkdirAll(targetPath, 0777); err != nil {
t.Fatalf("Failed to create temp dir %s due to error %v", targetPath, err)
Expand Down
Loading