2024-02-08 03:48:59 -06:00
|
|
|
// Copyright (c) The OpenTofu Authors
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
// Copyright (c) 2023 HashiCorp, Inc.
|
2023-05-02 10:33:06 -05:00
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
Refactoring of module source addresses and module installation
It's been a long while since we gave close attention to the codepaths for
module source address parsing and external module package installation.
Due to their age, these codepaths often diverged from our modern practices
such as representing address types in the addrs package, and encapsulating
package installation details only in a particular location.
In particular, this refactor makes source address parsing a separate step
from module installation, which therefore makes the result of that parsing
available to other Terraform subsystems which work with the configuration
representation objects.
This also presented the opportunity to better encapsulate our use of
go-getter into a new package "getmodules" (echoing "getproviders"), which
is intended to be the only part of Terraform that directly interacts with
go-getter.
This is largely just a refactor of the existing functionality into a new
code organization, but there is one notable change in behavior here: the
source address parsing now happens during configuration loading rather
than module installation, which may cause errors about invalid addresses
to be returned in different situations than before. That counts as
backward compatible because we only promise to remain compatible with
configurations that are _valid_, which means that they can be initialized,
planned, and applied without any errors. This doesn't introduce any new
error cases, and instead just makes a pre-existing error case be detected
earlier.
Our module registry client is still using its own special module address
type from registry/regsrc for now, with a small shim from the new
addrs.ModuleSourceRegistry type. Hopefully in a later commit we'll also
rework the registry client to work with the new address type, but this
commit is already big enough as it is.
2021-05-27 21:24:59 -05:00
|
|
|
package getmodules
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
|
|
|
)
|
|
|
|
|
|
|
|
// fileDetector is a replacement for go-getter's own file detector which
|
2023-09-20 09:05:24 -05:00
|
|
|
// better meets OpenTofu's needs: specifically, it rejects relative filesystem
|
Refactoring of module source addresses and module installation
It's been a long while since we gave close attention to the codepaths for
module source address parsing and external module package installation.
Due to their age, these codepaths often diverged from our modern practices
such as representing address types in the addrs package, and encapsulating
package installation details only in a particular location.
In particular, this refactor makes source address parsing a separate step
from module installation, which therefore makes the result of that parsing
available to other Terraform subsystems which work with the configuration
representation objects.
This also presented the opportunity to better encapsulate our use of
go-getter into a new package "getmodules" (echoing "getproviders"), which
is intended to be the only part of Terraform that directly interacts with
go-getter.
This is largely just a refactor of the existing functionality into a new
code organization, but there is one notable change in behavior here: the
source address parsing now happens during configuration loading rather
than module installation, which may cause errors about invalid addresses
to be returned in different situations than before. That counts as
backward compatible because we only promise to remain compatible with
configurations that are _valid_, which means that they can be initialized,
planned, and applied without any errors. This doesn't introduce any new
error cases, and instead just makes a pre-existing error case be detected
earlier.
Our module registry client is still using its own special module address
type from registry/regsrc for now, with a small shim from the new
addrs.ModuleSourceRegistry type. Hopefully in a later commit we'll also
rework the registry client to work with the new address type, but this
commit is already big enough as it is.
2021-05-27 21:24:59 -05:00
|
|
|
// paths with a somewhat-decent error message.
|
|
|
|
//
|
|
|
|
// This is a replacement for some historical hackery we did where we tried to
|
|
|
|
// avoid calling into go-getter altogether in this situation. This is,
|
|
|
|
// therefore, a copy of getter.FileDetector but with the "not absolute path"
|
|
|
|
// case replaced with a similar result as Terraform's old heuristic would've
|
|
|
|
// returned: a custom error type that the caller can react to in order to
|
|
|
|
// produce a hint error message if desired.
|
|
|
|
type fileDetector struct{}
|
|
|
|
|
|
|
|
func (d *fileDetector) Detect(src, pwd string) (string, bool, error) {
|
|
|
|
if len(src) == 0 {
|
|
|
|
return "", false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if !filepath.IsAbs(src) {
|
|
|
|
return "", true, &MaybeRelativePathErr{src}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmtFileURL(src), true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func fmtFileURL(path string) string {
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
// Make sure we're using "/" on Windows. URLs are "/"-based.
|
|
|
|
path = filepath.ToSlash(path)
|
|
|
|
return fmt.Sprintf("file://%s", path)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure that we don't start with "/" since we add that below.
|
|
|
|
if path[0] == '/' {
|
|
|
|
path = path[1:]
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("file:///%s", path)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MaybeRelativePathErr is the error type returned by NormalizePackageAddress
|
|
|
|
// if the source address looks like it might be intended to be a relative
|
|
|
|
// filesystem path but without the required "./" or "../" prefix.
|
|
|
|
//
|
|
|
|
// Specifically, NormalizePackageAddress will return a pointer to this type,
|
|
|
|
// so the error type will be *MaybeRelativePathErr.
|
|
|
|
//
|
|
|
|
// It has a name starting with "Maybe" because in practice we can get here
|
|
|
|
// with any string that isn't recognized as one of the supported schemes:
|
|
|
|
// treating the address as a local filesystem path is our fallback for when
|
|
|
|
// everything else fails, but it could just as easily be a typo in an attempt
|
|
|
|
// to use one of the other schemes and thus not a filesystem path at all.
|
|
|
|
type MaybeRelativePathErr struct {
|
|
|
|
Addr string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *MaybeRelativePathErr) Error() string {
|
2023-09-20 09:05:24 -05:00
|
|
|
return fmt.Sprintf("OpenTofu cannot detect a supported external module source type for %s", e.Addr)
|
Refactoring of module source addresses and module installation
It's been a long while since we gave close attention to the codepaths for
module source address parsing and external module package installation.
Due to their age, these codepaths often diverged from our modern practices
such as representing address types in the addrs package, and encapsulating
package installation details only in a particular location.
In particular, this refactor makes source address parsing a separate step
from module installation, which therefore makes the result of that parsing
available to other Terraform subsystems which work with the configuration
representation objects.
This also presented the opportunity to better encapsulate our use of
go-getter into a new package "getmodules" (echoing "getproviders"), which
is intended to be the only part of Terraform that directly interacts with
go-getter.
This is largely just a refactor of the existing functionality into a new
code organization, but there is one notable change in behavior here: the
source address parsing now happens during configuration loading rather
than module installation, which may cause errors about invalid addresses
to be returned in different situations than before. That counts as
backward compatible because we only promise to remain compatible with
configurations that are _valid_, which means that they can be initialized,
planned, and applied without any errors. This doesn't introduce any new
error cases, and instead just makes a pre-existing error case be detected
earlier.
Our module registry client is still using its own special module address
type from registry/regsrc for now, with a small shim from the new
addrs.ModuleSourceRegistry type. Hopefully in a later commit we'll also
rework the registry client to work with the new address type, but this
commit is already big enough as it is.
2021-05-27 21:24:59 -05:00
|
|
|
}
|