Skip to content

Commit a687f8f

Browse files
authored
feat: set module plugin working dir (#1247)
1 parent 881d542 commit a687f8f

File tree

3 files changed

+12
-7
lines changed

3 files changed

+12
-7
lines changed

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ require (
228228
github.com/hashicorp/hc-install v0.6.2
229229
github.com/hashicorp/hcl v1.0.0 // indirect
230230
github.com/hashicorp/yamux v0.1.1 // indirect
231-
github.com/imdario/mergo v0.3.16
231+
github.com/imdario/mergo v0.3.16 // indirect
232232
github.com/inconshreveable/mousetrap v1.1.0 // indirect
233233
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
234234
github.com/jinzhu/inflection v1.0.0 // indirect

pkg/modules/generators/app_configurations_generator.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ func (g *appConfigurationGenerator) invokeModule(
474474
) (*proto.GeneratorResponse, error) {
475475
// init the plugin
476476
if pluginMap[key] == nil {
477-
plugin, err := modules.NewPlugin(key)
477+
plugin, err := modules.NewPlugin(key, g.stack.Path)
478478
if err != nil {
479479
return nil, err
480480
}

pkg/modules/plugin.go

+10-5
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,15 @@ type Plugin struct {
4242
client *plugin.Client
4343
// Module represents the real module impl
4444
Module Module
45+
// dir represents the working directory of the plugin binary, which will be typically set as the stack path.
46+
dir string
4547
}
4648

47-
func NewPlugin(key string) (*Plugin, error) {
49+
func NewPlugin(key, dir string) (*Plugin, error) {
4850
if key == "" {
4951
return nil, fmt.Errorf("module key can not be empty")
5052
}
51-
p := &Plugin{key: key}
53+
p := &Plugin{key: key, dir: dir}
5254
err := p.initModule()
5355
if err != nil {
5456
return nil, err
@@ -75,7 +77,7 @@ func (p *Plugin) initModule() error {
7577
return err
7678
}
7779
pluginName := prefix[0] + "-" + prefix[1]
78-
client, err := NewPluginClient(pluginPath, pluginName)
80+
client, err := NewPluginClient(pluginPath, pluginName, p.dir)
7981
if err != nil {
8082
return err
8183
}
@@ -122,7 +124,7 @@ func buildPluginPath(namespace, resourceType, version string) (string, error) {
122124
return p, nil
123125
}
124126

125-
func NewPluginClient(modulePluginPath, moduleName string) (*plugin.Client, error) {
127+
func NewPluginClient(modulePluginPath, moduleName, workingDir string) (*plugin.Client, error) {
126128
// create the plugin log file
127129
var logFilePath string
128130
dir, err := kfile.KusionDataFolder()
@@ -148,11 +150,14 @@ func NewPluginClient(modulePluginPath, moduleName string) (*plugin.Client, error
148150
Level: hclog.Debug,
149151
})
150152

153+
cmd := exec.Command(modulePluginPath)
154+
cmd.Dir = workingDir
155+
151156
// We're a host! Start by launching the plugin process.Need to defer kill
152157
client := plugin.NewClient(&plugin.ClientConfig{
153158
HandshakeConfig: HandshakeConfig,
154159
Plugins: PluginMap,
155-
Cmd: exec.Command(modulePluginPath),
160+
Cmd: cmd,
156161
AllowedProtocols: []plugin.Protocol{
157162
plugin.ProtocolGRPC,
158163
},

0 commit comments

Comments
 (0)