Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(plugins): correct relative path support #4125

Merged
merged 3 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
- [#4100](https://github.com/ignite/cli/pull/4100) Set the `proto-dir` flag only for the `scaffold chain` command and use the proto path from the config
- [#4110](https://github.com/ignite/cli/pull/4110) Scaffold a consumer chain with `interchain-security` v5.0.0-rc0.
- [#4111](https://github.com/ignite/cli/pull/4111) Remove vuex generation
- [#4117](https://github.com/ignite/cli/pull/4117) Support relative path when installing local plugins
- [#4117](https://github.com/ignite/cli/pull/4117), [#4125](https://github.com/ignite/cli/pull/4125) Support relative path when installing local plugins

### Changes

Expand Down
2 changes: 1 addition & 1 deletion ignite/config/plugins/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (p Plugin) IsGlobal() bool {

// IsLocalPath returns true if the plugin path is a local directory.
func (p Plugin) IsLocalPath() bool {
return strings.HasPrefix(p.Path, "/") || strings.HasPrefix(p.Path, ".")
return strings.HasPrefix(p.Path, "/") || strings.HasPrefix(p.Path, ".") || strings.HasPrefix(p.Path, "~")
}

// HasPath verifies if a plugin has the given path regardless of version.
Expand Down
33 changes: 14 additions & 19 deletions ignite/services/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,19 @@ func newPlugin(pluginsDir string, cp pluginsconfig.Plugin, options ...Option) *P
apply(p)
}

if strings.HasPrefix(pluginPath, "/") {
// This is a local plugin, check if the file exists
// This is a local plugin, check if the file exists
if tp := (&Plugin{Plugin: pluginsconfig.Plugin{Path: pluginPath}}); tp.IsLocalPath() {
julienrbrt marked this conversation as resolved.
Show resolved Hide resolved
// if directory is relative, make it absolute
if !filepath.IsAbs(pluginPath) {
pluginPathAbs, err := filepath.Abs(pluginPath)
if err != nil {
p.Error = errors.Errorf("failed to get absolute path of %s: %w", pluginPath, err)
return p
}

pluginPath = pluginPathAbs
}

st, err := os.Stat(pluginPath)
if err != nil {
p.Error = errors.Wrapf(err, "local app path %q not found", pluginPath)
Expand All @@ -157,6 +168,7 @@ func newPlugin(pluginsDir string, cp pluginsconfig.Plugin, options ...Option) *P
p.name = path.Base(pluginPath)
return p
}

// This is a remote plugin, parse the URL
if i := strings.LastIndex(pluginPath, "@"); i != -1 {
// path contains a reference
Expand Down Expand Up @@ -236,23 +248,6 @@ func (p *Plugin) load(ctx context.Context) {
}

if p.IsLocalPath() {
// if directory is relative, make it absolute
if !filepath.IsAbs(p.srcPath) {
wd, err := os.Getwd()
if err != nil {
p.Error = errors.Errorf("failed to get working directory: %w", err)
return
}

srcPathAbs, err := filepath.Abs(p.srcPath)
if err != nil {
p.Error = errors.Errorf("failed to get absolute path of %s: %w", p.srcPath, err)
return
}

p.srcPath = filepath.Join(wd, srcPathAbs)
}

// trigger rebuild for local plugin if binary is outdated
if p.outdatedBinary() {
p.build(ctx)
Expand Down
10 changes: 10 additions & 0 deletions ignite/services/plugin/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@ func TestNewPlugin(t *testing.T) {
stderr: os.Stderr,
},
},
{
name: "ok: relative local plugin",
pluginCfg: pluginsconfig.Plugin{Path: "./testdata"},
expectedPlugin: Plugin{
srcPath: path.Join(wd, "testdata"),
name: "testdata",
stdout: os.Stdout,
stderr: os.Stderr,
},
},
{
name: "fail: remote plugin with only domain",
pluginCfg: pluginsconfig.Plugin{Path: "github.com"},
Expand Down
Loading