Commit Graph

82 Commits

Author SHA1 Message Date
Thomas Gummerer ae8134f5ad
upgrade to latest version of golangci-lint ()
The version we currently have doesn't support Go 1.22 properly, so it
throws a bunch of warnings locally when trying to run it with the latest
Go version installed. Just running the latest version locally also
doesn't quite work, since it throws a bunch of errors from the
perfsprint linter, which seems to have gotten stricter.

Upgrade to the latest version of golangci-lint, and fix all the errors
we're getting from it. Mostly done via `perfsprint -fix`, with some
manual changes that `perfsprint -fix` wouldn't touch.
2024-04-19 06:20:33 +00:00
Fraser Waters 3c9424ccc0
Fix lookup module deps with go.work files ()
<!--- 
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

<!--- Please include a summary of the change and which issue is fixed.
Please also include relevant motivation and context. -->

Fixes https://github.com/pulumi/pulumi/issues/15741.

`go list -m` returns all modules in a workspace if inside a Go
workspace. We only need the one module found in the Pulumi program
directory (or above it). So we set `GOWORK=off` before calling `go list
-m`.

## 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. -->
- [x] I have added tests that prove my fix is effective or that my
feature works
<!--- 
User-facing changes require a CHANGELOG entry.
-->
- [x] 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. -->
2024-03-21 09:48:04 +00:00
Fraser Waters 8bee243efb
Move buildutil out of sdk/common to the go language host ()
Same as https://github.com/pulumi/pulumi/pull/15165, a small bit of code
we can move just to the language host.
2024-01-17 22:25:47 +00:00
Fraser Waters 6162d16eb2
Move goversion out of sdk/common to the go language host ()
Another small part we can pull out of sdk/go/common.
2024-01-17 14:56:18 +00:00
Fraser Waters 3be1b6289c
Remove deprecated Protobufs imports ()
<!--- 
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

<!--- Please include a summary of the change and which issue is fixed.
Please also include relevant motivation and context. -->

github.com/golang/protobuf is marked deprecated and I was getting
increasingly triggered by the inconsistency of importing the `Empty`
type from "github.com/golang/protobuf/ptypes/empty" or
"google.golang.org/protobuf/types/known/emptypb" as "pbempty" or "empty"
or "emptypb". Similar for the struct type.

So this replaces all the Protobufs imports with ones from
"google.golang.org/protobuf", normalises the import name to always just
be the module name (emptypb), and adds the depguard linter to ensure we
don't use the deprecated package anymore.

## 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. -->
2024-01-17 09:35:20 +00:00
Fraser Waters 3560333ae6
Clean up uses of .Error() ()
Combination of a few cleanups.

1. Don't call .Error() on errors that are being passed to "%s" format
functions. Format will call `Error()` itself.
2. Don't call assert.Error then assert.Equal/Contains, just use
assert.ErrorEqual/ErrorContains instead.
3. Use "%w" if appropriate, instead of "%v"/"%s".
2023-12-20 15:54:06 +00:00
Fraser Waters 16d9f4c167
Enable perfsprint linter ()
<!--- 
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

<!--- Please include a summary of the change and which issue is fixed.
Please also include relevant motivation and context. -->

Prompted by a comment in another review:
https://github.com/pulumi/pulumi/pull/14654#discussion_r1419995945

This lints that we don't use `fmt.Errorf` when `errors.New` will
suffice, it also covers a load of other cases where `Sprintf` is
sub-optimal.

Most of these edits were made by running `perfsprint --fix`.

## 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. -->
2023-12-12 12:19:42 +00:00
Fraser Waters 6e986f90af
Pass root and main info to language host methods ()
<!--- 
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

<!--- Please include a summary of the change and which issue is fixed.
Please also include relevant motivation and context. -->

This is two changes rolled together in a way.

Firstly passing some of the data that we pass on language runtime
startup to also pass it to Run/GetRequiredPlugins/etc. This is needed
for matrix testing, as we only get to start the language runtime up once
for that but want to execute multiple programs with it.
I feel it's also a little more consistent as we use the language
runtimes in other contexts (codegen) where there isn't really a root
directory, and aren't any options (and if we did do options the options
for codegen are not going to be the same as for execution). It also
means we can reuse a language host for shimless and substack programs,
as before they heavily relied on their current working directory to
calculate paths, and obviosly could only take one set of options at
startup. Imagine a shimless python package + a python root program, that
would have needed two startups of the python language host to deal with,
this unblocks it so we can make the engine smarter and only use one.

Secondly renaming some of the fields we pass to
Run/GetRequiredPlugins/etc today. `Pwd` and `Program` were not very
descriptive and had pretty non-obvious documentation:
```
string pwd = 3;     // the program's working directory.
string program = 4; // the path to the program to execute.
```
`pwd` will remain, although probably rename it to `working_directory` at
some point, because while today we always start programs up with the
working directory equal to the program directory that definitely is
going to change in the future (at least for MLCs and substack programs).
But the name `pwd` doesn't make it clear that this was intended to be
the working directory _and_ the directory which contains the program.

`program` was in fact nearly always ".", and if it wasn't that it was
just a filename. The engine never sent a path for `program` (although we
did have some unit tests to check how that worked for the nodejs and
python hosts).

These are now replaced by a new structure with (I think) more clearly
named and documented fields (see ProgramInfo in langauge.proto).

The engine still sends the old data for now, we need to update
dotnet/yaml/java before we break the old interface and give Virtus Labs
a chance to update [besom](https://github.com/VirtusLab/besom).

## 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
  - [ ] I have formatted my code using `gofumpt`

<!--- Please provide details if the checkbox below is to be left
unchecked. -->
- [x] 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. -->
2023-12-10 17:30:51 +00:00
Fraser Waters 4af97b4c39
Return diagnostics from GeneratePackage ()
<!--- 
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

<!--- Please include a summary of the change and which issue is fixed.
Please also include relevant motivation and context. -->

Fixes https://github.com/pulumi/pulumi/issues/14660.

Fairly simple change to bring the GeneratePackage RPC method into
alignment with the other codegen methods and use returned diagnostics
rather than just error values.
`gen-sdk` is updated to print those diagnostics, and the python/node/go
runtimes updated to return the diagnostics from schema binding as
diagnostics rather than just an error value.

Might be worth at some point seeing if the rest of package generation
could use diagnostics rather than error values, but that's a larger
lift.

## 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
  - [ ] 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.
-->
- [x] 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. -->
2023-12-05 17:47:52 +00:00
Fraser Waters a1b52d6613
Make language-python it's own module ()
Fixes https://github.com/pulumi/pulumi/issues/13826.
    
This brings python inline with node and go as being it's own module and
running codegen via gRPC.

Also includes some improvements to the node and go codegen interfaces
from review.
2023-08-31 16:35:21 +00:00
Abhinav Gupta 2d3a1ecc3e
plugin(go, node, py): Exit cleanly on interrupt ()
This changes the language plugins for Go, Node, and Python
to watch for the os.Interrupt signal (SIGINT on Unix, CTRL_BREAK on
Windows)
that the plugin lifetime manager sends (per )
and exit cleanly on receiving the signal.

This is a no-op for users.
An immediate effect it'll have for us is that
we'll begin seeing test coverage data come out of these binaries.

A similar change is necessary in other language plugin binaries.
I did not touch uses of rpcutil.ServeWithOptions outside `main` packages
because whether a signal handler should be installed there or not
requires more knowledge of individual cases.

Note that this uses [signal.NotifyContext][1].
Calling the returned `cancel()` function removes the signal handler.
This is desirable so that if a user sends the signal again
(e.g., presses Ctrl-C again), we don't capture it
and let the OS kill the process.

  [1]: https://pkg.go.dev/os/signal#NotifyContext

Refs 
2023-08-29 15:42:31 +00:00
Fraser Waters 182e776660 Add hidden sdk-pack command
We need to be able to "pack" SDKs to refer to them as local dependencies
in matrix testing.

This is for two reasons.
1) We want to test as close as possible to the things we ship.
2) Not every language supports linking to a source tree, some require a
build step to give a linkable artifact.

These commands are going to end up looking _very_ similar to the publish
workflows, but while Providers work on that and while we work on matrix
testing we'll let them evolve in parallel.

The sdk-pack command is hidden unless PULUMI_DEV is set. I've checked
this works with matrix testing for NodeJS. We'll fill in the rest as we
need them for matrix testing.
2023-08-08 17:53:04 +01:00
Fraser Waters 99b736b55b Add localDependencies option to GenerateProject
This isn't currently actually used anywhere. I've just threaded it
through to all the program gen functions where it will be needed.

Matrix testing will be using and testing this.
2023-08-08 12:28:19 +01:00
Fraser Waters fd5000b32c Add schema loader service
This moves schema loading out of the language runtimes and over to the
engine host.
Language runtimes no longer need to create a plugin host, or diagnostic
sink either because of this.

All schema loading is done over grpc. This first pass is very basic, and
not expected to be performant but it moves the control of schema loading
to the engine which is necessary for matrix testing.

Testing of this is covered by the convert and code generation smoke tests.
2023-07-27 15:03:52 +01:00
Zaid Ajaj 5841f220b1 Consistently use the same non-strict bind options when applicable 2023-07-13 15:16:06 +02:00
Abhinav Gupta a244de9092
fix(host/go): Download external plugins even if they're not imported
We previously changed how the Go language host retrieves information
about a Go module:

    before: go list -m -json -mod=mod all
    after:  go list -m -json $importPath1 $importPath1

This change made it possible for us to supprot running in vendored mode
which allowed for use of private plugins that are not go-get-able.

This uncovered a corner case in the `go` command's behavior
when running in module mode:

If a package is listed in the go.mod
but it's not imported by the current module,
`go list -m -json $importPath` will not list its Dir in the output
even if it's present in the module cache.
For example, given a go.mod file that declares a dependency
but code that doesn't import it, as of Go 1.20.5,

    export GOMODCACHE=$(mktemp -d)
    go mod download -json
    go list -m -json $importPath

The output of `go mod download` will include information
about the dependency and the `Dir` where it was downloaded,
but `go list -m` will not.

Unfortunately, we can't just use `go mod download`
because that breaks vendoring:
vendored dependencies cannot always be redownloaded.

To resolve this issue,
we switch to a two-pass variant to gather this information:

- Run `go list -m` to get whatever information we can locally.
  This will be sufficient for the majority of use cases.
- For the handful of cases where the dependency isn't imported,
  we'll use `go mod download -json` to download them on-demand
  and get their location from that instead.

The `go mod download` step will take place only if we're in module mode.
In vendor mode, we'll work with what we have.

Resolves 
2023-07-11 16:22:15 -07:00
Fraser Waters 571fadae3f Use slice.Prealloc instead of make([]T, 0, ...)
Fixes https://github.com/pulumi/pulumi/issues/12738

https://github.com/pulumi/pulumi/pull/11834 turned on the prealloc
linter and changed a load of slice uses from just `var x T[]` to `x :=
make([]T, 0, preallocSize)`. This was good for performance but it turns
out there are a number of places in the codebase that treat a `nil`
slice as semnatically different to an empty slice.

Trying to test that, or even reason that through for every callsite is
untractable, so this PR replaces all expressions of the form `make([]T,
0, size)` with a call to `slice.Prealloc[T](size)`. When size is 0 that
returns a nil array, rather than an empty array.
2023-06-29 11:27:50 +01:00
Zaid Ajaj 9b9c95b0d3 fix enabling non-strict/lenient bind options 2023-06-23 16:19:02 +02:00
Zaid Ajaj 7ab7ad5a58 Prefer output-versioned invokes in generated programs for nodejs and python 2023-06-23 02:42:18 +02:00
Fraser Waters 46332e7d17 Make convert more lenient
Fixes https://github.com/pulumi/pulumi/issues/13117

This adds a new "--strict" flag to `pulumi convert` which defaults to
false. When strict is NOT set we bind the PCL with the extra options of
`SkipResourceTypechecking`, `AllowMissingVariables`, and
`AllowMissingProperties`. This will change some errors to warnings in
code generation.

The `strict` flag is sent over the gRPC interface to the Go/Node plugins
for their `GenerateProject` methods as they have to do PCL binding
plugin side currently.
2023-06-08 11:14:31 +01:00
Fraser Waters 5c999e28ca Test components in convert 2023-06-01 20:54:44 +01:00
Abhinav Gupta cb03565fcd
ci: Lint Go language host
The Go language host is a separate Go module
and did not have linting enabled.

This adds it to the list of packages that should be linted in CI
and fixes issues that were found:

- not gofumpted
- using ioutil
2023-05-19 15:01:42 -07:00
Abhinav Gupta dfefb0a613
fix(host/go): Allow Pulumi program in a subdir of the module
Fixes a regression in the Go language host where we started misbehaving
if the Pulumi.yaml was in a subdirectory of the Go module.

    myproject/
      |- go.mod
      |- foo.go
      '- infra/
          |- Pulumi.yaml
          '- main.go

This regression was introduced in ,
where we started parsing go.mod files to extract version information
and incorrectly assumed that the go.mod file was in the request
directory.

To fix this, we'll use the following command to get the absolute path
to the go.mod file.

    go list -m -f '{{.GoMod}}'

This command works in the root or subdirectory of a module
in both vendor and module mode.

Testing:
Includes a regression test in both module and vendor mode.

Resolves 
2023-05-17 13:20:38 -07:00
Fraser Waters 1a38eadc69 gRPC for GenerateProject/Program/Package
This changes codegen to be invoked via gRPC from pkg, rather than
invoking pkg/codegen directly.

Consider it a proof-of-concept for moving codegen to a gRPC interface
without the worries of forwards-backwards compatability (because we ship
language plugins at a fixed version side-by-side to users).
2023-05-06 13:14:59 +01:00
Fraser Waters 0f41bf29f0 Add per-language smoke tests to the integration tests 2023-05-06 09:23:19 +01:00
bors[bot] 35c7f566c6
Merge
12755: fix(host/go): Use official modfile package r=abhinav a=abhinav

Use the official golang.org/x/mod/modfile package to parse go.mod files
instead of the external copy of it.

Refs https://github.com/pulumi/pulumi/pull/12727#discussion_r1178333617


Co-authored-by: Abhinav Gupta <abhinav@pulumi.com>
2023-04-26 20:34:59 +00:00
Abhinav Gupta 70a3d4e8a6
fix(host/go): Use official modfile package
Use the official golang.org/x/mod/modfile package to parse go.mod files
instead of the external copy of it.

Refs https://github.com/pulumi/pulumi/pull/12727#discussion_r1178333617
2023-04-26 13:11:15 -07:00
Abhinav Gupta 3b932281a4
fix(go/host): Use program directory
Follow up to https://github.com/pulumi/pulumi/pull/12727#discussion_r1175433156

The GetRequiredPlugins and GetProgramDependencies methods of the Go
language host always operate in the current directory of the plugin.
This is likely unintentional because the request message includes a Pwd
field specifying the program directory.

For comparison:

- The Python host also runs in the plugin directory
- The Node host correctly uses the program directory:
  via the Pwd field for GetProgramDependencies
  and the Program field for GetRequiredPlugins

The existing behavior likely happens to work because the plugin happens
to run inside the program directory, but that's not guaranteed.

This switches the Go language host to run queries inside the program
directory to guard against issues in the future.
2023-04-24 10:13:21 -07:00
Abhinav Gupta 33b5ad6527
feat(go/host): Support vendored dependencies
The Go language host cannot resolve dependencies or plugins if a Pulumi
program vendors its dependencies.

BACKGROUND

The GetRequiredPlugins and GetProgramDependencies methods of the Go
language host rely on the following two commands:

    go list -m -mod=mod all
    go list -m -mod=mod ...
    # '...' means current module and its descendants

GetRequiredPlugins additionally searches the source directories for each
returned module for pulumi-plugin.json files at a pre-determined paths.

    $module/pulumi-plugin.json
    $module/go/pulumi-plugin.json
    $module/go/*/pulumi-plugin.json

This works for most Pulumi programs, except those that vendor private
dependencies with 'go mod vendor'.
For those programs, the above commands fail because -mod=mod forces them
to run in module mode, and their private dependencies are not accessible
in module mode (because they are not exposed publicly).

We use the -mod=mod flag to force 'go list' to run in module mode
because otherwise, it will automatically use vendor mode if a vendor
directory is present. However, in vendor mode, the two 'go list'
commands above are not supported.
The following links add more context on why, but in short:
vendor does not have enough information for the general 'go list'.

- https://stackoverflow.com/a/60660593,
- https://github.com/golang/go/issues/35589#issuecomment-554488544

In short,

- list all with -mod=mod fails because the dependency is private
- list without -mod=mod will use vendor mode
- vendor mode doesn't support the listing all

SOLUTION

Drop the -mod=mod flag so that 'go list' can decide whether to run in
module mode or vendor mode.
However, instead of running it with 'all' or '...',
pass in a list of dependencies extracted from the go.mod.

    go list -m import/path1 import/path2 # ...

This operation is completely offline in vendor mode
so it can list information about private dependencies too.

This alone isn't enough though because in vendor mode,
the JSON output does not include the module root directory.
E.g.

    % go list -mod=vendor -json -m github.com/pulumi/pulumi/sdk/v3
    {
            "Path": "github.com/pulumi/pulumi/sdk/v3",
            "Version": "v3.55.0",
            "GoVersion": "1.18"
    }

    # Versus

    % go list -mod=mod -json -m github.com/pulumi/pulumi/sdk/v3
    {
            "Path": "github.com/pulumi/pulumi/sdk/v3",
            "Version": "v3.55.0",
            "Time": "2023-02-14T11:04:22Z",
            "Dir": "[...]/go/pkg/mod/github.com/pulumi/pulumi/sdk/v3@v3.55.0",
            "GoMod": "[...]/go/pkg/mod/cache/download/github.com/pulumi/pulumi/sdk/v3/@v/v3.55.0.mod",
            "GoVersion": "1.18"
    }

Therefore, we have to manually calculate the path for each module root.
That's easy enough: vendor/$importPath.

Lastly, since GetProgramDependencies only needs a dependency list,
it now extracts information from the go.mod without calling 'go list'.

TESTING

Adds a variant of the test added in  that verifies the
functionality with vendoring. It removes the sources for the
dependencies to simulate private dependencies. The new test fails
without the accompanying change.

The fix was further manually verified against the reproduction included
in .

    % cd go-output
    % pulumi plugin rm -a -y
    % pulumi preview
    Previewing update (abhinav):
    Downloading plugin: 15.19 MiB / 15.19 MiB [=========================] 100.00% 0s
                                                                                    [resource plugin random-4.8.2] installing
         Type                      Name               Plan
     +   pulumi:pulumi:Stack       go-output-abhinav  create
     +   └─ random:index:RandomId  rrr                create

    Resources:
        + 2 to create

    % pulumi plugin ls
    NAME    KIND      VERSION  SIZE   INSTALLED       LAST USED
    random  resource  4.8.2    33 MB  26 seconds ago  26 seconds ago

    TOTAL plugin cache size: 33 MB

Note that the version of random (4.8.2) is what's specified in the
go.mod, not the latest release (v4.12.1).

    % grep pulumi-random go.mod
            github.com/pulumi/pulumi-random/sdk/v4 v4.8.2

With the plugin downloaded, I ran this again without an internet
connection.

    % pulumi preview
    Previewing update (abhinav):
         Type                      Name               Plan
     +   pulumi:pulumi:Stack       go-output-abhinav  create
     +   └─ random:index:RandomId  rrr                create

    Resources:
        + 2 to create

This means that if the dependencies are vendored, and the plugin is
already available, we won't make additional network requests, which also
addresses .

Resolves 
Resolves 
2023-04-24 09:49:16 -07:00
Abhinav Gupta 723b01efd6
refactor(go/host): Inject current working directory
Refactors the Go language host to inject CWD into it
instead of always using `os.Getwd`.
This allows the test for plugin and dependency behavior
to run in parallel.

Note: Some of the RPCs methods get a Directory or Pwd in the request
but it wasn't being used before so we were running in the directory
where the host was started.
This retains that behvaior for those methods rather than changing it
because it's not clear whether that omission was intentional.
2023-04-21 10:28:25 -07:00
Abhinav Gupta 5467e5ff9f
refactor(go/host): Extract global state into a struct
Refactors the Go language host plugin
to extract global state (e.g. stdout, command line args)
into a mainCmd struct.
The command line parsing logic is placed into a separate,
independently tested function.

This change does not modify any behavior.
2023-04-20 17:53:05 -07:00
Abhinav Gupta acda1e3b13
all: Fix revive issues
Fixes the following issues found by revive
included in the latest release of golangci-lint.

Full list of issues:

**pkg**

```
backend/display/object_diff.go:47:10: superfluous-else: if block ends with a break statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary) (revive)
backend/display/object_diff.go:716:12: redefines-builtin-id: redefinition of the built-in function delete (revive)
backend/display/object_diff.go:742:14: redefines-builtin-id: redefinition of the built-in function delete (revive)
backend/display/object_diff.go:983:10: superfluous-else: if block ends with a continue statement, so drop this else and outdent its block (revive)
backend/httpstate/backend.go:1814:4: redefines-builtin-id: redefinition of the built-in function cap (revive)
backend/httpstate/backend.go:1824:5: redefines-builtin-id: redefinition of the built-in function cap (revive)
backend/httpstate/client/client.go:444:2: if-return: redundant if ...; err != nil check, just return error instead. (revive)
backend/httpstate/client/client.go:455:2: if-return: redundant if ...; err != nil check, just return error instead. (revive)
cmd/pulumi/org.go:113:4: if-return: redundant if ...; err != nil check, just return error instead. (revive)
cmd/pulumi/util.go:216:2: if-return: redundant if ...; err != nil check, just return error instead. (revive)
codegen/docs/gen.go:428:2: redefines-builtin-id: redefinition of the built-in function copy (revive)
codegen/hcl2/model/expression.go:2151:5: redefines-builtin-id: redefinition of the built-in function close (revive)
codegen/hcl2/syntax/comments.go:151:2: redefines-builtin-id: redefinition of the built-in function close (revive)
codegen/hcl2/syntax/comments.go:329:3: redefines-builtin-id: redefinition of the built-in function close (revive)
codegen/hcl2/syntax/comments.go:381:5: redefines-builtin-id: redefinition of the built-in function close (revive)
codegen/nodejs/gen.go:1367:5: redefines-builtin-id: redefinition of the built-in function copy (revive)
codegen/python/gen_program_expressions.go:136:2: redefines-builtin-id: redefinition of the built-in function close (revive)
codegen/python/gen_program_expressions.go:142:3: redefines-builtin-id: redefinition of the built-in function close (revive)
codegen/report/report.go:126:6: redefines-builtin-id: redefinition of the built-in function panic (revive)
codegen/schema/docs_test.go:210:10: superfluous-else: if block ends with a continue statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary) (revive)
codegen/schema/schema.go:790:2: redefines-builtin-id: redefinition of the built-in type any (revive)
codegen/schema/schema.go:793:4: redefines-builtin-id: redefinition of the built-in type any (revive)
resource/deploy/plan.go:506:2: if-return: redundant if ...; err != nil check, just return error instead. (revive)
resource/deploy/snapshot_test.go:59:3: redefines-builtin-id: redefinition of the built-in function copy (revive)
resource/deploy/state_builder.go:108:2: redefines-builtin-id: redefinition of the built-in function copy (revive)
```

**sdk**

```
go/common/resource/plugin/context.go:142:2: redefines-builtin-id: redefinition of the built-in function copy (revive)
go/common/resource/plugin/plugin.go:142:12: superfluous-else: if block ends with a break statement, so drop this else and outdent its block (revive)
go/common/resource/properties_diff.go:114:2: redefines-builtin-id: redefinition of the built-in function len (revive)
go/common/resource/properties_diff.go:117:4: redefines-builtin-id: redefinition of the built-in function len (revive)
go/common/resource/properties_diff.go:122:4: redefines-builtin-id: redefinition of the built-in function len (revive)
go/common/resource/properties_diff.go:127:4: redefines-builtin-id: redefinition of the built-in function len (revive)
go/common/resource/properties_diff.go:132:4: redefines-builtin-id: redefinition of the built-in function len (revive)
go/common/util/deepcopy/copy.go:30:1: redefines-builtin-id: redefinition of the built-in function copy (revive)
go/common/workspace/creds.go:242:2: if-return: redundant if ...; err != nil check, just return error instead. (revive)
go/pulumi-language-go/main.go:569:2: if-return: redundant if ...; err != nil check, just return error instead. (revive)
go/pulumi-language-go/main.go:706:2: if-return: redundant if ...; err != nil check, just return error instead. (revive)
go/pulumi/run_test.go:925:2: redefines-builtin-id: redefinition of the built-in type any (revive)
go/pulumi/run_test.go:933:3: redefines-builtin-id: redefinition of the built-in type any (revive)
nodejs/cmd/pulumi-language-nodejs/main.go:778:2: if-return: redundant if ...; err != nil check, just return error instead. (revive)
python/cmd/pulumi-language-python/main.go:1011:2: if-return: redundant if ...; err != nil check, just return error instead. (revive)
python/cmd/pulumi-language-python/main.go:863:2: if-return: redundant if ...; err != nil check, just return error instead. (revive)
python/python.go:230:2: redefines-builtin-id: redefinition of the built-in function print (revive)
```

**tests**

```
integration/integration_util_test.go:282:11: superfluous-else: if block ends with a continue statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary) (revive)
```
2023-03-21 08:55:11 -07:00
Abhinav Gupta 7aa5b77a0c
all: Reformat with gofumpt
Per team discussion, switching to gofumpt.

[gofumpt][1] is an alternative, stricter alternative to gofmt.
It addresses other stylistic concerns that gofmt doesn't yet cover.

  [1]: https://github.com/mvdan/gofumpt

See the full list of [Added rules][2], but it includes:

- Dropping empty lines around function bodies
- Dropping unnecessary variable grouping when there's only one variable
- Ensuring an empty line between multi-line functions
- simplification (`-s` in gofmt) is always enabled
- Ensuring multi-line function signatures end with
  `) {` on a separate line.

  [2]: https://github.com/mvdan/gofumpt#Added-rules

gofumpt is stricter, but there's no lock-in.
All gofumpt output is valid gofmt output,
so if we decide we don't like it, it's easy to switch back
without any code changes.

gofumpt support is built into the tooling we use for development
so this won't change development workflows.

- golangci-lint includes a gofumpt check (enabled in this PR)
- gopls, the LSP for Go, includes a gofumpt option
  (see [installation instrutions][3])

  [3]: https://github.com/mvdan/gofumpt#installation

This change was generated by running:

```bash
gofumpt -w $(rg --files -g '*.go' | rg -v testdata | rg -v compilation_error)
```

The following files were manually tweaked afterwards:

- pkg/cmd/pulumi/stack_change_secrets_provider.go:
  one of the lines overflowed and had comments in an inconvenient place
- pkg/cmd/pulumi/destroy.go:
  `var x T = y` where `T` wasn't necessary
- pkg/cmd/pulumi/policy_new.go:
  long line because of error message
- pkg/backend/snapshot_test.go:
  long line trying to assign three variables in the same assignment

I have included mention of gofumpt in the CONTRIBUTING.md.
2023-03-03 09:00:24 -08:00
Fraser Waters 89178183ee Clean up pkg/errors in go language host 2023-01-14 20:56:53 +00:00
Aaron Friel 67f4dda5c5 Embed default gRPC server implementations 2023-01-05 17:54:11 -08:00
Fraser Waters 9e5f1cc618 Engine and Golang support for shimless providers
This allows the pulumi-language-go plugin to start up providers directly
from .go source files.

The other language providers will be extended to support this as well in
time.
2022-11-14 11:25:41 +00:00
Anton Tayanovskyy 9276574c80 Introduce PULUMI_DEBUG_GRPC support 2022-11-08 14:01:15 -05:00
杨成锴 7b09ad142a chore: Update doc comments, coding style, fix lint
* about error string: error string should not be capitalized or end with punctuation mark
* apply suggestions from code review
* lint: fix format error and simplify the code

Co-authored-by: Fraser Waters <frassle@gmail.com>
Co-authored-by: Aaron Friel <mayreply@aaronfriel.com>
2022-10-13 13:50:49 -07:00
Aaron Friel ee1d5f0cef fix: Best effort support for Go versions below 1.18 2022-09-27 10:43:41 -07:00
Fraser Waters a700bd83a5 Add Organization to go sdk 2022-09-22 12:46:04 +01:00
bors[bot] 27960bf593
Merge
10731: add `buildTarget` option for pulumi go programs to compile/recompile to the specified path r=dixler a=dixler

<!--- 
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

<!--- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. -->

Part of  

This implements the `buildTarget` option(Pulumi.yaml) for Pulumi Go programs to write the pulumi program out to a file. It has some performance benefits as Go can build a program faster when an existing program is provided.

## Checklist

<!--- Please provide details if the checkbox below is to be left unchecked. -->
- [x] I have added tests that prove my fix is effective or that my feature works
<!--- 
User-facing changes require a CHANGELOG entry.
-->
- [x] I have updated the [CHANGELOG-PENDING](https://github.com/pulumi/pulumi/blob/master/CHANGELOG_PENDING.md) file with my change
<!--
If the change(s) in this PR is a modification of an existing call to the Pulumi Service,
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 Service API version
  <!-- `@Pulumi` employees: If yes, you must submit corresponding changes in the service repo. -->


Co-authored-by: Kyle Dixler <kyle@pulumi.com>
2022-09-22 00:40:38 +00:00
Aaron Friel 7e555cb6ab ci: Simplify test listing, update go dependencies to 1.18 compat 2022-09-21 09:51:59 -07:00
Kyle Dixler a867720c11
cleaned up internal function `compileProgramCwd` 2022-09-15 09:13:49 -07:00
Kyle Dixler a8a23d96cd
updated comments 2022-09-14 15:18:30 -07:00
Kyle Dixler 64fb821d25
first commit 2022-09-14 14:48:09 -07:00
Kyle Dixler f84c985b5f
[sdk/go] deduplicate go error logs ()
This PR deduplicates error messages for the Go SDK on program failures and uses exit status 32 for pulumi Go programs to indicate that an error has been logged and that the engine should Bail and not print any more output.
2022-09-06 16:44:29 -07:00
Kyle Dixler 22430a0ebb
[sdk/go] enable direct compilation via `go build` ()
This PR modifies `pulumi-language-go` to attempt to compile and run user programs first. If env var `PULUMI_GO_USE_RUN=true` is set, we use the old behavior of using `go run`.
2022-08-26 07:16:48 -07:00
Fraser Waters d70fdb09d1
Move about information to language plugins ()
* Move about information to language plugins

* Add to CHANGELOG
2022-08-15 14:55:04 +01:00
Fraser Waters 76760205c7
Add proto interfaces to move about logic to plugins () 2022-07-25 12:35:16 +01:00
Fraser Waters 55be89e36a
Poll gRPC healthchecks to auto-cancel sub-processes ()
* Poll gRPC healthchecks to auto-cancel sub-processes

This hooks the rpcutils cancellation to a polling process that asks for the health of the engine.

If the engine dies, the health check fails, rpcutil gets cancelled, sub-process quits.
This is hooked into both the langauge plugins, and the shared provider main method.

N.B. Providers started in "attach" mode do not poll the health check as it is expected that these processes we're explictly started by the user and the user will shut them down.

* Fix cancel usage

* Add to CHANGELOG

* if nil check not needed
2022-06-06 13:28:00 +01:00