From 357562a4340a40b3ffb869497b40d552fcfbbcdd Mon Sep 17 00:00:00 2001 From: David Bell Date: Thu, 16 Feb 2023 09:21:14 -0800 Subject: [PATCH] feat(go): add JSII_NODE to override node entrypoint (#3890) By default, jsii-go will lookup the node entrypoint from the PATH. This allows users to override with a specific node entrypoint via the JSII_NODE environment variable. issue: #3889 --- By submitting this pull request, I confirm that my contribution is made under the terms of the [Apache 2.0 license]. [Apache 2.0 license]: https://www.apache.org/licenses/LICENSE-2.0 --- .all-contributorsrc | 9 +++ README.md | 33 ++++----- .../internal/kernel/process/process.go | 7 +- .../internal/kernel/process/process_test.go | 69 +++++++++++++++++-- 4 files changed, 97 insertions(+), 21 deletions(-) diff --git a/.all-contributorsrc b/.all-contributorsrc index c5d0ae4760..7afd67ba33 100644 --- a/.all-contributorsrc +++ b/.all-contributorsrc @@ -1523,6 +1523,15 @@ "bug" ] }, + { + "login": "dastbe", + "name": "David Bell", + "avatar_url": "https://avatars.githubusercontent.com/u/634735?v=4", + "profile": "https://github.com/dastbe", + "contributions": [ + "code" + ] + }, { "login": "Ragnoroct", "name": "Will Bender", diff --git a/README.md b/README.md index 7f77646274..60f6464667 100644 --- a/README.md +++ b/README.md @@ -110,150 +110,151 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d Dave Slotnick
Dave Slotnick

🐛 + David Bell
David Bell

💻 Donald Stufft
Donald Stufft

🐛 💻 🤔 👀 Dongie Agnir
Dongie Agnir

💻 👀 Eduardo Rabelo
Eduardo Rabelo

📖 Eduardo Sena S. Rosa
Eduardo Sena S. Rosa

🐛 Elad Ben-Israel
Elad Ben-Israel

🐛 💻 🤔 🚧 👀 📢 Eli Polonsky
Eli Polonsky

🐛 💻 🤔 🚧 👀 - Eric Z. Beard
Eric Z. Beard

📆 + Eric Z. Beard
Eric Z. Beard

📆 Erik Karlsson
Erik Karlsson

🐛 Eugene Kozlov
Eugene Kozlov

💻 Fabio Gentile
Fabio Gentile

🐛 Florian Eitel
Florian Eitel

🤔 Glib Shpychka
Glib Shpychka

🐛 Graham Lea
Graham Lea

🤔 👀 - Hamza Assyad
Hamza Assyad

🐛 💻 🤔 👀 + Hamza Assyad
Hamza Assyad

🐛 💻 🤔 👀 Hari Pachuveetil
Hari Pachuveetil

📝 📖 Hsing-Hui Hsu
Hsing-Hui Hsu

💻 📖 🤔 👀 Ikko Ashimine
Ikko Ashimine

📖 James
James

🐛 💻 James Kelley
James Kelley

🐛 James Mead
James Mead

💻 - James Siri
James Siri

💻 🚧 + James Siri
James Siri

💻 🚧 Jason Del Ponte
Jason Del Ponte

🤔 👀 Jason Fulghum
Jason Fulghum

🤔 📆 👀 Jeff Malins
Jeff Malins

💻 Jerry Kindall
Jerry Kindall

📖 🤔 Jimmy Gaussen
Jimmy Gaussen

🤔 Johannes Weber
Johannes Weber

📖 - John Pantzlaff
John Pantzlaff

💻 + John Pantzlaff
John Pantzlaff

💻 Jon Steinich
Jon Steinich

🐛 🤔 💻 Joseph Lawson
Joseph Lawson

👀 Joseph Martin
Joseph Martin

🐛 Junix
Junix

🐛 Justin Frahm
Justin Frahm

🐛 Justin Taylor
Justin Taylor

🐛 - Kaizen Conroy
Kaizen Conroy

💻 🐛 + Kaizen Conroy
Kaizen Conroy

💻 🐛 Kaizen Conroy
Kaizen Conroy

💻 Kaushik Borra
Kaushik Borra

🐛 Knut O. Hellan
Knut O. Hellan

🐛 Kyle Thomson
Kyle Thomson

💻 👀 Leandro Padua
Leandro Padua

🐛 Liang Zhou
Liang Zhou

🐛 💻 - Madeline Kusters
Madeline Kusters

💻 🐛 + Madeline Kusters
Madeline Kusters

💻 🐛 Maja S Bratseth
Maja S Bratseth

🐛 Marcos Diez
Marcos Diez

🐛 Mark Nielsen
Mark Nielsen

💻 Matthew Bonig
Matthew Bonig

🐛 📝 Matthew Pirocchi
Matthew Pirocchi

💻 🤔 👀 Meng Xin Zhu
Meng Xin Zhu

🐛 - Michael Neil
Michael Neil

🚧 + Michael Neil
Michael Neil

🚧 Mike Lane
Mike Lane

🐛 Mitch Garnaat
Mitch Garnaat

🐛 💻 🤔 👀 Mitchell Valine
Mitchell Valine

🐛 💻 🤔 🚧 👀 Mohamad Soufan
Mohamad Soufan

📖 Mykola Mogylenko
Mykola Mogylenko

🐛 Naumel
Naumel

👀 - Neta Nir
Neta Nir

💻 🤔 🚧 👀 + Neta Nir
Neta Nir

💻 🤔 🚧 👀 Nick Lynch
Nick Lynch

🐛 💻 🚧 👀 Niranjan Jayakar
Niranjan Jayakar

🐛 💻 🤔 🚧 👀 Noah Litov
Noah Litov

💻 🚧 👀 Otavio Macedo
Otavio Macedo

💻 🐛 PIDZ - Bart
PIDZ - Bart

🤔 Peter Woodworth
Peter Woodworth

🚧 - Petr Kacer
Petr Kacer

🐛 + Petr Kacer
Petr Kacer

🐛 Petra Barus
Petra Barus

💻 Philip Cali
Philip Cali

🤔 Quentin Loos
Quentin Loos

🤔 Raphael
Raphael

🐛 Richard H Boyd
Richard H Boyd

🐛 Rico Huijbers
Rico Huijbers

🐛 💻 🤔 🚧 👀 - Romain Marcadier
Romain Marcadier

🐛 💻 🎨 🤔 🚧 👀 📝 + Romain Marcadier
Romain Marcadier

🐛 💻 🎨 🤔 🚧 👀 📝 SADIK KUZU
SADIK KUZU

👀 SK
SK

🤔 Sam Fink
Sam Fink

💻 👀 Sam Goodwin
Sam Goodwin

👀 Sebastian Korfmann
Sebastian Korfmann

🐛 💻 🤔 Sepehr Laal
Sepehr Laal

🐛 - Shane Witbeck
Shane Witbeck

🤔 + Shane Witbeck
Shane Witbeck

🤔 Shiv Lakshminarayan
Shiv Lakshminarayan

💻 🚧 👀 Somaya
Somaya

💻 🤔 🚧 👀 Stephen Kuenzli
Stephen Kuenzli

📖 Takahiro Sugiura
Takahiro Sugiura

📖 The Gitter Badger
The Gitter Badger

💻 🚧 Thomas Poignant
Thomas Poignant

🐛 - Thomas Steinbach
Thomas Steinbach

🐛 + Thomas Steinbach
Thomas Steinbach

🐛 Thorsten Hoeger
Thorsten Hoeger

💻 Tim Wagner
Tim Wagner

🐛 🤔 Tobias Lidskog
Tobias Lidskog

💻 Tom Bonner
Tom Bonner

🐛 Ty Coghlan
Ty Coghlan

🐛 Tyler van Hensbergen
Tyler van Hensbergen

🤔 - Vlad Hrybok
Vlad Hrybok

🐛 + Vlad Hrybok
Vlad Hrybok

🐛 Vladimir Shchur
Vladimir Shchur

🐛 Will Bender
Will Bender

🐛 Yan Zhulanow
Yan Zhulanow

💻 Yigong Liu
Yigong Liu

🐛 🤔 Zach Bienenfeld
Zach Bienenfeld

🐛 ajnarang
ajnarang

🤔 - aniljava
aniljava

💻 + aniljava
aniljava

💻 arnogeurts-sqills
arnogeurts-sqills

🐛 💻 cn-cit
cn-cit

🐛 deccy-mcc
deccy-mcc

🐛 dependabot-preview[bot]
dependabot-preview[bot]

🐛 🚧 dependabot[bot]
dependabot[bot]

🚧 dheffx
dheffx

🐛 - gregswdl
gregswdl

🐛 + gregswdl
gregswdl

🐛 guyroberts21
guyroberts21

📖 mattBrzezinski
mattBrzezinski

📖 mergify
mergify

🚧 mergify[bot]
mergify[bot]

🚧 nathannaveen
nathannaveen

🚧 seiyashima42
seiyashima42

🐛 💻 📖 - sullis
sullis

💻 + sullis
sullis

💻 vaneek
vaneek

🐛 wendysophie
wendysophie

🐛 diff --git a/packages/@jsii/go-runtime/jsii-runtime-go/internal/kernel/process/process.go b/packages/@jsii/go-runtime/jsii-runtime-go/internal/kernel/process/process.go index 935746192d..f437648f5c 100644 --- a/packages/@jsii/go-runtime/jsii-runtime-go/internal/kernel/process/process.go +++ b/packages/@jsii/go-runtime/jsii-runtime-go/internal/kernel/process/process.go @@ -16,6 +16,7 @@ import ( "github.com/aws/jsii-runtime-go/internal/embedded" ) +const JSII_NODE string = "JSII_NODE" const JSII_RUNTIME string = "JSII_RUNTIME" type ErrorResponse struct { @@ -104,7 +105,11 @@ func NewProcess(compatibleVersions string) (*Process, error) { p.Close() return nil, err } else { - p.cmd = exec.Command("node", entrypoint) + if node := os.Getenv(JSII_NODE); node != "" { + p.cmd = exec.Command(node, entrypoint) + } else { + p.cmd = exec.Command("node", entrypoint) + } } } diff --git a/packages/@jsii/go-runtime/jsii-runtime-go/internal/kernel/process/process_test.go b/packages/@jsii/go-runtime/jsii-runtime-go/internal/kernel/process/process_test.go index 0a38bed296..9bbcdfe155 100644 --- a/packages/@jsii/go-runtime/jsii-runtime-go/internal/kernel/process/process_test.go +++ b/packages/@jsii/go-runtime/jsii-runtime-go/internal/kernel/process/process_test.go @@ -101,6 +101,60 @@ func TestVersionCheck(t *testing.T) { } } +func TestJSIINode(t *testing.T) { + t.Run("successful node invocation", func(t *testing.T) { + node, err := getNodePath() + if err != nil { + t.Fatal(err) + return + } + + oldJsiiNode := os.Getenv(JSII_NODE) + os.Setenv(JSII_NODE, node) + defer os.Setenv(JSII_NODE, oldJsiiNode) + + process, err := NewProcess("0.0.0") + if err != nil { + t.Fatal(err) + return + } + defer process.Close() + + err = process.Request(TestRequest{"stats"}, &TestResponse{}) + if err != nil { + t.Fatal(err) + return + } + }) + + t.Run("unsuccessful node invocation", func(t *testing.T) { + oldJsiiNode := os.Getenv(JSII_NODE) + os.Setenv(JSII_NODE, "./absolute-gibberish") + defer os.Setenv(JSII_NODE, oldJsiiNode) + + process, err := NewProcess("0.0.0") + if err != nil { + t.Fatal(err) + return + } + defer process.Close() + + if err := process.Request(TestRequest{"stats"}, &TestResponse{}); err == nil { + t.Errorf("expected an error, but no error received") + } else if !strings.Contains(err.Error(), "no such file or directory") && !strings.Contains(err.Error(), "file does not exist") { + // We have 2 options here because Windows returns a different error message, of course... + t.Errorf("expected 'no such file or directory' or 'file does not exist', got %v", err.Error()) + } + }) +} + +type TestRequest struct { + Api string `json:"api"` +} + +type TestResponse struct { +} + type EchoRequest struct { Message string `json:"message"` } @@ -110,6 +164,15 @@ type EchoResponse struct { } func makeCustomRuntime(mockVersion string) (string, error) { + node, err := getNodePath() + if err != nil { + return "", err + } + + return fmt.Sprintf("%v %v %v", node, mockRuntime, mockVersion), nil +} + +func getNodePath() (string, error) { var ( node string err error @@ -119,8 +182,6 @@ func makeCustomRuntime(mockVersion string) (string, error) { } else { node, err = exec.LookPath("node") } - if err != nil { - return "", err - } - return fmt.Sprintf("%v %v %v", node, mockRuntime, mockVersion), nil + + return node, err }