Skip to content

Commit

Permalink
Return the underlying error if we can't locate the pulumi SDK (#16160)
Browse files Browse the repository at this point in the history
<!--- 
Thanks so much for your contribution! If this is your first time
contributing, please ensure that you have read the
[CONTRIBUTING](https://github.com/pulumi/pulumi/blob/master/CONTRIBUTING.md)
documentation.
-->

# Description

When locating the pulumi SDK fails with an error other than
MODULE_NOT_FOUND, return the underlying error instead of our default
error message asking to run `pulumi install`. This is helpful when
there's a typo in package.json and it can't be parsed.

Fixes #4973

## Checklist

- [x] I have run `make tidy` to update any new dependencies
- [x] I have run `make lint` to verify my code passes the lint check
  - [x] I have formatted my code using `gofumpt`

<!--- Please provide details if the checkbox below is to be left
unchecked. -->
- [ ] I have added tests that prove my fix is effective or that my
feature works
<!--- 
User-facing changes require a CHANGELOG entry.
-->
- [ ] I have run `make changelog` and committed the
`changelog/pending/<file>` documenting my change
<!--
If the change(s) in this PR is a modification of an existing call to the
Pulumi Cloud,
then the service should honor older versions of the CLI where this
change would not exist.
You must then bump the API version in
/pkg/backend/httpstate/client/api.go, as well as add
it to the service.
-->
- [ ] Yes, there are changes in this PR that warrants bumping the Pulumi
Cloud API version
<!-- @pulumi employees: If yes, you must submit corresponding changes in
the service repo. -->
  • Loading branch information
julienp committed May 10, 2024
1 parent 086a75d commit a51219f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
changes:
- type: fix
scope: sdk/nodejs
description: Return the underlying error if we can't locate the pulumi SDK
17 changes: 14 additions & 3 deletions sdk/nodejs/cmd/pulumi-language-nodejs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,16 @@ func main() {

// locateModule resolves a node module name to a file path that can be loaded
func locateModule(ctx context.Context, mod, programDir, nodeBin string) (string, error) {
args := []string{"-e", fmt.Sprintf("console.log(require.resolve('%s'));", mod)}
args := []string{"-e", fmt.Sprintf(`try {
console.log(require.resolve('%s'));
} catch (error) {
if (error.code === 'MODULE_NOT_FOUND') {
console.error("It looks like the Pulumi SDK has not been installed. Have you run pulumi install?")
} else {
console.error(error.message);
}
process.exit(1);
}`, mod)}

tracingSpan, _ := opentracing.StartSpanFromContext(ctx,
"locateModule",
Expand All @@ -166,6 +175,9 @@ func locateModule(ctx context.Context, mod, programDir, nodeBin string) (string,
cmd.Dir = programDir
out, err := cmd.Output()
if err != nil {
if ee, ok := err.(*exec.ExitError); ok {
return "", errors.New(string(ee.Stderr))
}
return "", err
}
return strings.TrimSpace(string(out)), nil
Expand Down Expand Up @@ -581,8 +593,7 @@ func (host *nodeLanguageHost) Run(ctx context.Context, req *pulumirpc.RunRequest

runPath, err = locateModule(ctx, runPath, programDirectory, nodeBin)
if err != nil {
cmdutil.ExitError(
"It looks like the Pulumi SDK has not been installed. Have you run pulumi install?")
cmdutil.ExitError(err.Error())
}

// Channel producing the final response we want to issue to our caller. Will get the result of
Expand Down

0 comments on commit a51219f

Please sign in to comment.