From 942476401242f4242944c565667ef61923891c0b Mon Sep 17 00:00:00 2001 From: Louis Shawn Date: Fri, 6 Feb 2026 09:23:50 +0800 Subject: [PATCH 1/2] cli: warn on ambiguous 2-part environment refs --- cmd/esc/cli/cli_test.go | 6 ++- cmd/esc/cli/env.go | 44 +++++++++++++++++++ .../env-ambiguous-two-part-warning.yaml | 17 +++++++ 3 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 cmd/esc/cli/testdata/env-ambiguous-two-part-warning.yaml diff --git a/cmd/esc/cli/cli_test.go b/cmd/esc/cli/cli_test.go index bcb65279..cd6b5c18 100644 --- a/cmd/esc/cli/cli_test.go +++ b/cmd/esc/cli/cli_test.go @@ -291,6 +291,7 @@ func (env *testEnvironment) latest() *testEnvironmentRevision { type testPulumiClient struct { user string + orgs []string defaultOrg string environments map[string]*testEnvironment openEnvs map[string]*esc.Environment @@ -497,7 +498,7 @@ func (c *testPulumiClient) URL() string { // GetPulumiAccountDetails returns the user implied by the API token associated with this client. func (c *testPulumiClient) GetPulumiAccountDetails(ctx context.Context) (string, []string, *workspace.TokenInformation, error) { - return c.user, nil, nil, nil + return c.user, c.orgs, nil, nil } func (c *testPulumiClient) GetRevisionNumber(ctx context.Context, orgName, projectName, envName, version string) (int, error) { @@ -1440,6 +1441,8 @@ type cliTestcaseYAML struct { Process *cliTestcaseProcess `yaml:"process,omitempty"` + Orgs []string `yaml:"orgs,omitempty"` + Environments map[string]yaml.Node `yaml:"environments,omitempty"` } @@ -1565,6 +1568,7 @@ func loadTestcase(path string) (*cliTestcaseYAML, *cliTestcase, error) { exec.login = &testLoginManager{creds: creds} exec.client = &testPulumiClient{ user: "test-user", + orgs: testcase.Orgs, environments: environments, openEnvs: map[string]*esc.Environment{}, } diff --git a/cmd/esc/cli/env.go b/cmd/esc/cli/env.go index cf67cee6..330b5310 100644 --- a/cmd/esc/cli/env.go +++ b/cmd/esc/cli/env.go @@ -35,6 +35,8 @@ type envCommand struct { esc *escCommand envNameFlag string + + warnedAmbiguousRefs map[string]struct{} } func newEnvCmd(esc *escCommand) *cobra.Command { @@ -109,6 +111,44 @@ func (r *environmentRef) String() string { return s } +func (cmd *envCommand) warnIfAmbiguousTwoPartRef(ctx context.Context, refString string, ref environmentRef) { + if !ref.hasAmbiguousPath || strings.Count(refString, "/") != 1 { + return + } + + // Only warn when the first segment could be interpreted as an org name the user belongs to. + // In that case, `/` may be either `/` or `/default/`. + _, orgs, _, err := cmd.esc.client.GetPulumiAccountDetails(ctx) + if err != nil { + return + } + + inOrgs := false + for _, org := range orgs { + if strings.EqualFold(org, ref.projectName) { + inOrgs = true + break + } + } + if !inOrgs { + return + } + + if cmd.warnedAmbiguousRefs == nil { + cmd.warnedAmbiguousRefs = map[string]struct{}{} + } + if _, ok := cmd.warnedAmbiguousRefs[refString]; ok { + return + } + cmd.warnedAmbiguousRefs[refString] = struct{}{} + + fmt.Fprintf( + cmd.esc.stderr, + "warning: environment reference %q is ambiguous (could be / or /default/); prefer //.\n", + refString, + ) +} + func (cmd *envCommand) parseRef(refStr string) environmentRef { var orgName, projectName, envNameAndVersion string @@ -194,6 +234,8 @@ func (cmd *envCommand) getNewEnvRef( return ref, args, nil } + cmd.warnIfAmbiguousTwoPartRef(ctx, cmd.envNameFlag, ref) + // Check if project at / exists. Assume not if listing environments errors allEnvs, _ := cmd.listEnvironments(ctx, "", "") existsProject := false @@ -259,6 +301,8 @@ func (cmd *envCommand) getExistingEnvRefWithRelative( return ref, nil } + cmd.warnIfAmbiguousTwoPartRef(ctx, refString, ref) + // Check // exists, _ := cmd.esc.client.EnvironmentExists(ctx, ref.orgName, ref.projectName, ref.envName) diff --git a/cmd/esc/cli/testdata/env-ambiguous-two-part-warning.yaml b/cmd/esc/cli/testdata/env-ambiguous-two-part-warning.yaml new file mode 100644 index 00000000..b4c25248 --- /dev/null +++ b/cmd/esc/cli/testdata/env-ambiguous-two-part-warning.yaml @@ -0,0 +1,17 @@ +run: | + esc env get test-org/env --definition +orgs: + - test-org +environments: + test-user/test-org/env: + values: + string: esc + +--- +> esc env get test-org/env --definition +values: + string: esc + +--- +> esc env get test-org/env --definition +warning: environment reference "test-org/env" is ambiguous (could be / or /default/); prefer //. From 5927edc6ad14df1a3a038fdd17f0278fc968119d Mon Sep 17 00:00:00 2001 From: Louis Shawn Date: Tue, 10 Feb 2026 20:17:38 +0800 Subject: [PATCH 2/2] cli: scope ambiguous env warnings to legacy path checks --- cmd/esc/cli/cli_test.go | 9 ++-- cmd/esc/cli/env.go | 46 ++++--------------- .../env-ambiguous-two-part-warning.yaml | 4 +- 3 files changed, 13 insertions(+), 46 deletions(-) diff --git a/cmd/esc/cli/cli_test.go b/cmd/esc/cli/cli_test.go index cd6b5c18..859aa82e 100644 --- a/cmd/esc/cli/cli_test.go +++ b/cmd/esc/cli/cli_test.go @@ -291,7 +291,6 @@ func (env *testEnvironment) latest() *testEnvironmentRevision { type testPulumiClient struct { user string - orgs []string defaultOrg string environments map[string]*testEnvironment openEnvs map[string]*esc.Environment @@ -498,7 +497,7 @@ func (c *testPulumiClient) URL() string { // GetPulumiAccountDetails returns the user implied by the API token associated with this client. func (c *testPulumiClient) GetPulumiAccountDetails(ctx context.Context) (string, []string, *workspace.TokenInformation, error) { - return c.user, c.orgs, nil, nil + return c.user, nil, nil, nil } func (c *testPulumiClient) GetRevisionNumber(ctx context.Context, orgName, projectName, envName, version string) (int, error) { @@ -1204,7 +1203,8 @@ func (c *testPulumiClient) EnvironmentExists( projectName string, envName string, ) (bool, error) { - return false, nil + _, ok := c.environments[path.Join(orgName, projectName, envName)] + return ok, nil } func (c *testPulumiClient) CreateEnvironmentOpenRequest( @@ -1441,8 +1441,6 @@ type cliTestcaseYAML struct { Process *cliTestcaseProcess `yaml:"process,omitempty"` - Orgs []string `yaml:"orgs,omitempty"` - Environments map[string]yaml.Node `yaml:"environments,omitempty"` } @@ -1568,7 +1566,6 @@ func loadTestcase(path string) (*cliTestcaseYAML, *cliTestcase, error) { exec.login = &testLoginManager{creds: creds} exec.client = &testPulumiClient{ user: "test-user", - orgs: testcase.Orgs, environments: environments, openEnvs: map[string]*esc.Environment{}, } diff --git a/cmd/esc/cli/env.go b/cmd/esc/cli/env.go index 330b5310..065ee0b4 100644 --- a/cmd/esc/cli/env.go +++ b/cmd/esc/cli/env.go @@ -35,8 +35,6 @@ type envCommand struct { esc *escCommand envNameFlag string - - warnedAmbiguousRefs map[string]struct{} } func newEnvCmd(esc *escCommand) *cobra.Command { @@ -111,37 +109,7 @@ func (r *environmentRef) String() string { return s } -func (cmd *envCommand) warnIfAmbiguousTwoPartRef(ctx context.Context, refString string, ref environmentRef) { - if !ref.hasAmbiguousPath || strings.Count(refString, "/") != 1 { - return - } - - // Only warn when the first segment could be interpreted as an org name the user belongs to. - // In that case, `/` may be either `/` or `/default/`. - _, orgs, _, err := cmd.esc.client.GetPulumiAccountDetails(ctx) - if err != nil { - return - } - - inOrgs := false - for _, org := range orgs { - if strings.EqualFold(org, ref.projectName) { - inOrgs = true - break - } - } - if !inOrgs { - return - } - - if cmd.warnedAmbiguousRefs == nil { - cmd.warnedAmbiguousRefs = map[string]struct{}{} - } - if _, ok := cmd.warnedAmbiguousRefs[refString]; ok { - return - } - cmd.warnedAmbiguousRefs[refString] = struct{}{} - +func (cmd *envCommand) warnIfAmbiguousTwoPartRef(refString string) { fmt.Fprintf( cmd.esc.stderr, "warning: environment reference %q is ambiguous (could be / or /default/); prefer //.\n", @@ -234,8 +202,6 @@ func (cmd *envCommand) getNewEnvRef( return ref, args, nil } - cmd.warnIfAmbiguousTwoPartRef(ctx, cmd.envNameFlag, ref) - // Check if project at / exists. Assume not if listing environments errors allEnvs, _ := cmd.listEnvironments(ctx, "", "") existsProject := false @@ -265,6 +231,10 @@ func (cmd *envCommand) getNewEnvRef( } } + if existsLegacyPath { + cmd.warnIfAmbiguousTwoPartRef(cmd.envNameFlag) + } + if !existsProject && existsLegacyPath { return legacyRef, args, nil } @@ -301,8 +271,6 @@ func (cmd *envCommand) getExistingEnvRefWithRelative( return ref, nil } - cmd.warnIfAmbiguousTwoPartRef(ctx, refString, ref) - // Check // exists, _ := cmd.esc.client.EnvironmentExists(ctx, ref.orgName, ref.projectName, ref.envName) @@ -323,6 +291,10 @@ func (cmd *envCommand) getExistingEnvRefWithRelative( legacyRef.envName, ) + if existsLegacyPath { + cmd.warnIfAmbiguousTwoPartRef(refString) + } + // Require unambiguous path if both paths exist if exists && existsLegacyPath { return ref, ambiguousIdentifierError{ diff --git a/cmd/esc/cli/testdata/env-ambiguous-two-part-warning.yaml b/cmd/esc/cli/testdata/env-ambiguous-two-part-warning.yaml index b4c25248..6719a996 100644 --- a/cmd/esc/cli/testdata/env-ambiguous-two-part-warning.yaml +++ b/cmd/esc/cli/testdata/env-ambiguous-two-part-warning.yaml @@ -1,9 +1,7 @@ run: | esc env get test-org/env --definition -orgs: - - test-org environments: - test-user/test-org/env: + test-org/default/env: values: string: esc