Skip to content

Commit

Permalink
fix(plugins): correct relative path support (#4125)
Browse files Browse the repository at this point in the history
* fix(plugins): correct relative path support

* updates
  • Loading branch information
julienrbrt authored May 13, 2024
1 parent 1e06cc2 commit 75bf998
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 21 deletions.
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() {
// 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

0 comments on commit 75bf998

Please sign in to comment.