Visitar URL original
Allow skipping up-front provider plugin installs for engine commands … · pulumi/pulumi@71ddfb5 · GitHub
Skip to content

Commit 71ddfb5

Browse files
authored
Allow skipping up-front provider plugin installs for engine commands (#22947)
This enables `pulumi {up,down,preview,import,refresh,watch}` to skip the up-front engine-based provider plugin installation. Fixes #18902
1 parent f3da388 commit 71ddfb5

11 files changed

Lines changed: 232 additions & 21 deletions

File tree

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
changes:
2+
- type: feat
3+
scope: cli/engine
4+
description: Add `--skip-plugin-pre-install` to skip up-front plugin installation

‎pkg/cmd/pulumi/operations/destroy.go‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ func NewDestroyCmd() *cobra.Command {
8888
var excludeDependents bool
8989
var excludeProtected bool
9090
var continueOnError bool
91+
var skipPluginPreInstall bool
9192

9293
// Flags for Neo.
9394
var neoEnabled bool
@@ -324,6 +325,7 @@ func NewDestroyCmd() *cobra.Command {
324325
Experimental: env.Experimental.Value(),
325326
ContinueOnError: continueOnError,
326327
DestroyProgram: runProgram,
328+
SkipPluginPreInstall: skipPluginPreInstall,
327329
}
328330

329331
_, destroyErr := backend.DestroyStack(ctx, s, backend.UpdateOperation{
@@ -475,6 +477,10 @@ func NewDestroyCmd() *cobra.Command {
475477
&yes, "yes", "y", false,
476478
"Automatically approve and perform the destroy after previewing it")
477479

480+
cmd.PersistentFlags().BoolVar(
481+
&skipPluginPreInstall, "skip-plugin-pre-install", false,
482+
"Skip the up-front provider plugin install step; missing plugins are installed lazily by the engine")
483+
478484
cmd.PersistentFlags().BoolVar(
479485
&neoEnabled, "neo", false,
480486
"Enable Pulumi Neo's assistance for improved CLI experience and insights "+

‎pkg/cmd/pulumi/operations/import.go‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,7 @@ func NewImportCmd() *cobra.Command {
631631
var yes bool
632632
var protectResources bool
633633
var properties []string
634+
var skipPluginPreInstall bool
634635

635636
var from string
636637
var generateResources string
@@ -986,6 +987,7 @@ func NewImportCmd() *cobra.Command {
986987
UseLegacyDiff: env.EnableLegacyDiff.Value(),
987988
UseLegacyRefreshDiff: env.EnableLegacyRefreshDiff.Value(),
988989
Experimental: env.Experimental.Value(),
990+
SkipPluginPreInstall: skipPluginPreInstall,
989991
}
990992

991993
_, err = backend.ImportStack(ctx, s, backend.UpdateOperation{
@@ -1138,6 +1140,9 @@ func NewImportCmd() *cobra.Command {
11381140
&generateResources, "generate-resources", "",
11391141
//nolint:lll
11401142
"When used with --from, always write a JSON-encoded file containing a list of importable resources discovered by conversion to the specified path")
1143+
cmd.PersistentFlags().BoolVar(
1144+
&skipPluginPreInstall, "skip-plugin-pre-install", false,
1145+
"Skip the up-front provider plugin install step; missing plugins are installed lazily by the engine")
11411146

11421147
if env.DebugCommands.Value() {
11431148
cmd.PersistentFlags().StringVar(

‎pkg/cmd/pulumi/operations/preview.go‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ func NewPreviewCmd() *cobra.Command {
320320
var targetDependents bool
321321
var excludeDependents bool
322322
var attachDebugger []string
323+
var skipPluginPreInstall bool
323324

324325
// Flags for Neo.
325326
var neoEnabled bool
@@ -522,10 +523,11 @@ func NewPreviewCmd() *cobra.Command {
522523
ExcludeDependents: excludeDependents,
523524
// If we're trying to save a plan then we _need_ to generate it. We also turn this on in
524525
// experimental mode to just get more testing of it.
525-
GeneratePlan: env.Experimental.Value() || planFilePath != "",
526-
Experimental: env.Experimental.Value(),
527-
AttachDebugger: attachDebugger,
528-
Autonamer: autonamer,
526+
GeneratePlan: env.Experimental.Value() || planFilePath != "",
527+
Experimental: env.Experimental.Value(),
528+
AttachDebugger: attachDebugger,
529+
Autonamer: autonamer,
530+
SkipPluginPreInstall: skipPluginPreInstall,
529531
},
530532
Display: displayOpts,
531533
}
@@ -745,6 +747,10 @@ func NewPreviewCmd() *cobra.Command {
745747
"Enable the ability to attach a debugger to the program and source based plugins being executed. Can limit debug type to 'program', 'plugins', 'plugin:<name>' or 'all'.")
746748
cmd.Flag("attach-debugger").NoOptDefVal = "program"
747749

750+
cmd.PersistentFlags().BoolVar(
751+
&skipPluginPreInstall, "skip-plugin-pre-install", false,
752+
"Skip the up-front provider plugin install step; missing plugins are installed lazily by the engine")
753+
748754
cmd.PersistentFlags().BoolVar(
749755
&neoEnabled, "neo", false,
750756
"Enable Pulumi Neo's assistance for improved CLI experience and insights "+

‎pkg/cmd/pulumi/operations/refresh.go‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ func NewRefreshCmd() *cobra.Command {
8484
var targetDependents bool
8585
var excludes *[]string
8686
var excludeDependents bool
87+
var skipPluginPreInstall bool
8788

8889
// Flags for handling pending creates
8990
var skipPendingCreates bool
@@ -313,6 +314,7 @@ func NewRefreshCmd() *cobra.Command {
313314
Experimental: env.Experimental.Value(),
314315
ExecKind: execKind,
315316
RefreshProgram: runProgram,
317+
SkipPluginPreInstall: skipPluginPreInstall,
316318
}
317319

318320
changes, err := backend.RefreshStack(ctx, s, backend.UpdateOperation{
@@ -440,6 +442,10 @@ func NewRefreshCmd() *cobra.Command {
440442
"import-pending-creates", nil,
441443
"A list of form [[URN ID]...] describing the provider IDs of pending creates")
442444

445+
cmd.PersistentFlags().BoolVar(
446+
&skipPluginPreInstall, "skip-plugin-pre-install", false,
447+
"Skip the up-front provider plugin install step; missing plugins are installed lazily by the engine")
448+
443449
cmd.PersistentFlags().BoolVar(
444450
&neoEnabled, "neo", false,
445451
"Enable Pulumi Neo's assistance for improved CLI experience and insights "+

‎pkg/cmd/pulumi/operations/up.go‎

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ func NewUpCmd() *cobra.Command {
130130
var planFilePath string
131131
var attachDebugger []string
132132
var strict bool
133+
var skipPluginPreInstall bool
133134

134135
// Flags for Neo.
135136
var neoEnabled bool
@@ -237,12 +238,13 @@ func NewUpCmd() *cobra.Command {
237238
ExcludeDependents: excludeDependents,
238239
// Trigger a plan to be generated during the preview phase which can be constrained to during the
239240
// update phase.
240-
GeneratePlan: env.Experimental.Value() || strict,
241-
Experimental: env.Experimental.Value(),
242-
Strict: strict,
243-
ContinueOnError: continueOnError,
244-
AttachDebugger: attachDebugger,
245-
Autonamer: autonamer,
241+
GeneratePlan: env.Experimental.Value() || strict,
242+
Experimental: env.Experimental.Value(),
243+
Strict: strict,
244+
ContinueOnError: continueOnError,
245+
AttachDebugger: attachDebugger,
246+
Autonamer: autonamer,
247+
SkipPluginPreInstall: skipPluginPreInstall,
246248
}
247249

248250
if planFilePath != "" {
@@ -438,9 +440,10 @@ func NewUpCmd() *cobra.Command {
438440
}
439441

440442
defer pctx.Close()
441-
442-
if err = newcmd.InstallDependencies(pctx, &proj.Runtime, main); err != nil {
443-
return err
443+
if !skipPluginPreInstall {
444+
if err = newcmd.InstallDependencies(pctx, &proj.Runtime, main); err != nil {
445+
return err
446+
}
444447
}
445448

446449
cfg, sm, err := cmdConfig.GetStackConfiguration(ctx, pctx.Diag, ssml, s, proj)
@@ -491,7 +494,8 @@ func NewUpCmd() *cobra.Command {
491494
UseLegacyRefreshDiff: env.EnableLegacyRefreshDiff.Value(),
492495
ContinueOnError: continueOnError,
493496

494-
AttachDebugger: attachDebugger,
497+
AttachDebugger: attachDebugger,
498+
SkipPluginPreInstall: skipPluginPreInstall,
495499
}
496500

497501
start := time.Now()
@@ -865,6 +869,11 @@ func NewUpCmd() *cobra.Command {
865869
"[EXPERIMENTAL] Enable strict plan behavior: generate a plan during preview and constrain the update "+
866870
"to that plan (opt-in). Cannot be used with --skip-preview.")
867871

872+
cmd.PersistentFlags().BoolVar(
873+
&skipPluginPreInstall, "skip-plugin-pre-install", false,
874+
"Skip the up-front provider plugin install step; missing plugins are installed lazily by the engine. "+
875+
"When deploying from a template, also skips installing the project's runtime dependencies.")
876+
868877
cmd.PersistentFlags().BoolVar(
869878
&neoEnabled, "neo", false,
870879
"Enable Pulumi Neo's assistance for improved CLI experience and insights "+

‎pkg/cmd/pulumi/operations/watch.go‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ func NewWatchCmd() *cobra.Command {
5555
var showSames bool
5656
var showURNs bool
5757
var secretsProvider string
58+
var skipPluginPreInstall bool
5859

5960
cmd := &cobra.Command{
6061
Use: "watch",
@@ -159,6 +160,7 @@ func NewWatchCmd() *cobra.Command {
159160
DisableResourceReferences: env.DisableResourceReferences.Value(),
160161
DisableOutputValues: env.DisableOutputValues.Value(),
161162
Experimental: env.Experimental.Value(),
163+
SkipPluginPreInstall: skipPluginPreInstall,
162164
}
163165

164166
err = backend.WatchStack(ctx, s, backend.UpdateOperation{
@@ -238,6 +240,9 @@ func NewWatchCmd() *cobra.Command {
238240
cmd.PersistentFlags().BoolVar(
239241
&showURNs, "urns", false,
240242
"Display full URNs instead of short resource names")
243+
cmd.PersistentFlags().BoolVar(
244+
&skipPluginPreInstall, "skip-plugin-pre-install", false,
245+
"Skip the up-front provider plugin install step; missing plugins are installed lazily by the engine")
241246

242247
cmd.PersistentFlags().StringVar(&execKind, "exec-kind", "", "")
243248
// ignore err, only happens if flag does not exist

‎pkg/engine/lifecycletest/framework/framework.go‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,12 +279,16 @@ func (op TestOp) runWithContext(
279279
}
280280
}
281281

282+
pluginManager := opts.PluginManager
283+
if pluginManager == nil {
284+
pluginManager = NopPluginManager{}
285+
}
282286
ctx := &engine.Context{
283287
Cancel: cancelCtx,
284288
Events: events,
285289
SnapshotManager: combined,
286290
BackendClient: backendClient,
287-
PluginManager: NopPluginManager{},
291+
PluginManager: pluginManager,
288292
}
289293

290294
updateOpts := opts.Options()
@@ -775,7 +779,9 @@ func (t *TestStep) ValidateAnd(f ValidateFunc) {
775779
type TestUpdateOptions struct {
776780
engine.UpdateOptions
777781
// a factory to produce a plugin host for an update operation.
778-
HostF deploytest.PluginHostFactory
782+
HostF deploytest.PluginHostFactory
783+
// PluginManager overrides the engine.Context.PluginManager used by the run. Defaults to NopPluginManager{}.
784+
PluginManager engine.PluginManager
779785
T TB
780786
SkipDisplayTests bool
781787
}
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
// Copyright 2026, Pulumi Corporation.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package lifecycletest
16+
17+
import (
18+
"context"
19+
"sync"
20+
"sync/atomic"
21+
"testing"
22+
23+
"github.com/blang/semver"
24+
"github.com/stretchr/testify/assert"
25+
"github.com/stretchr/testify/require"
26+
27+
. "github.com/pulumi/pulumi/pkg/v3/engine" //nolint:revive
28+
lt "github.com/pulumi/pulumi/pkg/v3/engine/lifecycletest/framework"
29+
"github.com/pulumi/pulumi/pkg/v3/resource/deploy/deploytest"
30+
"github.com/pulumi/pulumi/sdk/v3/go/common/apitype"
31+
"github.com/pulumi/pulumi/sdk/v3/go/common/diag"
32+
"github.com/pulumi/pulumi/sdk/v3/go/common/resource/plugin"
33+
"github.com/pulumi/pulumi/sdk/v3/go/common/workspace"
34+
)
35+
36+
// countingPluginManager records each plugin the engine considers for up-front installation. The
37+
// engine looks up every plugin in its install set via GetPluginPath; SkipPluginPreInstall
38+
// short-circuits that traversal, so the recorded set captures the gap between the two modes.
39+
type countingPluginManager struct {
40+
lt.NopPluginManager
41+
42+
mu sync.Mutex
43+
considered []string
44+
}
45+
46+
func (m *countingPluginManager) GetPluginPath(
47+
_ context.Context, _ diag.Sink, plug workspace.PluginDescriptor, _ []workspace.ProjectPlugin,
48+
) (string, error) {
49+
version := ""
50+
if plug.Version != nil {
51+
version = plug.Version.String()
52+
}
53+
m.mu.Lock()
54+
m.considered = append(m.considered, plug.Name+"@"+version)
55+
m.mu.Unlock()
56+
return "installed", nil
57+
}
58+
59+
func (m *countingPluginManager) Considered() []string {
60+
m.mu.Lock()
61+
defer m.mu.Unlock()
62+
out := make([]string, len(m.considered))
63+
copy(out, m.considered)
64+
return out
65+
}
66+
67+
// TestSkipPluginPreInstallSkipsUnusedLanguagePackages demonstrates the over-inference that
68+
// SkipPluginPreInstall avoids: the language host reports every package the program imports as
69+
// "required", but only the packages whose resources the program actually registers are needed at
70+
// runtime. Without SkipPluginPreInstall the engine installs every reported package up-front; with
71+
// SkipPluginPreInstall it installs none, leaving the provider registry to load packages lazily as
72+
// resources are registered.
73+
func TestSkipPluginPreInstallSkipsUnusedLanguagePackages(t *testing.T) {
74+
t.Parallel()
75+
76+
v1 := semver.Version{Major: 1}
77+
pkgA := workspace.PackageDescriptor{
78+
PluginDescriptor: workspace.PluginDescriptor{
79+
Name: "pkgA",
80+
Kind: apitype.ResourcePlugin,
81+
Version: &v1,
82+
},
83+
}
84+
pkgB := workspace.PackageDescriptor{
85+
PluginDescriptor: workspace.PluginDescriptor{
86+
Name: "pkgB",
87+
Kind: apitype.ResourcePlugin,
88+
Version: &v1,
89+
},
90+
}
91+
92+
var pkgALoads, pkgBLoads atomic.Int32
93+
loaders := []*deploytest.ProviderLoader{
94+
deploytest.NewProviderLoader("pkgA", v1, func() (plugin.Provider, error) {
95+
pkgALoads.Add(1)
96+
return &deploytest.Provider{}, nil
97+
}),
98+
deploytest.NewProviderLoader("pkgB", v1, func() (plugin.Provider, error) {
99+
pkgBLoads.Add(1)
100+
return &deploytest.Provider{}, nil
101+
}),
102+
}
103+
104+
program := func(_ plugin.RunInfo, monitor *deploytest.ResourceMonitor) error {
105+
_, err := monitor.RegisterResource("pkgA:m:typA", "resA", true)
106+
return err
107+
}
108+
109+
// The language host advertises both pkgA and pkgB as required, even though the program only
110+
// ever registers a resource of pkgA.
111+
programF := deploytest.NewLanguageRuntimeF(program, pkgA, pkgB)
112+
hostF := deploytest.NewPluginHostF(nil, nil, programF, loaders...)
113+
114+
runUpdate := func(skip bool) *countingPluginManager {
115+
// Reset counters
116+
pkgALoads.Store(0)
117+
pkgBLoads.Store(0)
118+
119+
pm := &countingPluginManager{}
120+
p := &lt.TestPlan{
121+
Options: lt.TestUpdateOptions{
122+
UpdateOptions: UpdateOptions{SkipPluginPreInstall: skip},
123+
T: t,
124+
HostF: hostF,
125+
PluginManager: pm,
126+
SkipDisplayTests: true,
127+
},
128+
}
129+
_, err := lt.TestOp(Update).RunStep(
130+
p.GetProject(), p.GetTarget(t, nil), p.Options, false, p.BackendClient, nil, "")
131+
require.NoError(t, err)
132+
return pm
133+
}
134+
135+
// Without SkipPluginPreInstall the engine considers both packages reported by the language host
136+
// for install, even though pkgB is never used at runtime.
137+
pm := runUpdate(false)
138+
assert.ElementsMatch(t, []string{"pkgA@1.0.0", "pkgB@1.0.0"}, pm.Considered())
139+
assert.EqualValues(t, 1, pkgALoads.Load(), "pkgA provider must be loaded to register resA")
140+
assert.EqualValues(t, 0, pkgBLoads.Load(), "pkgB provider is never used")
141+
142+
// With SkipPluginPreInstall the engine considers no plugins for up-front install. pkgA is still
143+
// loaded on demand by the provider registry when the program registers a resource of pkgA.
144+
pm = runUpdate(true)
145+
assert.Empty(t, pm.Considered())
146+
assert.EqualValues(t, 1, pkgALoads.Load(), "pkgA must still be loaded lazily")
147+
assert.EqualValues(t, 0, pkgBLoads.Load(), "pkgB still unused")
148+
}

0 commit comments

Comments
 (0)