Repository: pulumi/esc (go)

Issue:
env run not disambiguating args correctly
### What happened?

I was trying to use `pulumi env run` (which just uses esc under the hood), but forgot to set the environment name in the command line args.  This resulted in a rather confusing error.

### Example

```
$ pulumi env run -- pulumi preview                                                                        
Warning: Referring to an environment name ('pulumi') without a project is deprecated.
Please use 'v-thomas-pulumi-corp/default/pulumi' or 'default/pulumi' instead.
error: resolving command: exec: "preview": executable file not found in $PATH
```

With the `--` disambiguation I was expecting it to warn me that there was no environment name specified on the command line.

### Output of `pulumi about`

```
$ pulumi about                    
CLI          
Version      3.153.2-dev.0
Go Version   go1.22.1
Go Compiler  gc

Plugins
KIND      NAME           VERSION
resource  lambda-plugin  0.0.1
language  python         3.153.2-dev.0

Host     
OS       debian
Version  trixie/sid
Arch     x86_64
```

### Additional context

_No response_

### Contributing

Vote on this issue by adding a 👍 reaction. 
To contribute a fix for this issue, leave a comment (and link to your pull request, if you've opened one already). 


Pull request:
Always expect environment name as first argument for run 
Fixes #478 

This PR checks ensures that the first argument for _run_ is always an environment. This is required for when `--` is used since in the current behavior will threat the first argument after `--` as the environment.

**Before:**
<img width="548" alt="image" src="https://github.com/user-attachments/assets/68204bd4-5199-4a0f-87d3-c70b2c7e3edb" />

**After:**
<img width="364" alt="image" src="https://github.com/user-attachments/assets/c537656e-fb52-4f56-83a2-341ed5f305f6" />



Patch:
diff --git a/CHANGELOG_PENDING.md b/CHANGELOG_PENDING.md
index 5831898..2692c62 100644
--- a/CHANGELOG_PENDING.md
+++ b/CHANGELOG_PENDING.md
@@ -1,5 +1,8 @@
 ### Improvements
 
+- `esc run` expects environment to be passed before `--`
+  [#545](https://github.com/pulumi/esc/pull/546)
+
 ### Bug Fixes
 
 - Fix `esc version`
diff --git a/cmd/esc/cli/env_run.go b/cmd/esc/cli/env_run.go
index 3c960d0..a8c2d8a 100644
--- a/cmd/esc/cli/env_run.go
+++ b/cmd/esc/cli/env_run.go
@@ -97,7 +97,7 @@ func newEnvRunCmd(envcmd *envCommand) *cobra.Command {
 	shell := valueOrDefault(filepath.Base(envcmd.esc.environ.Get("SHELL")), "sh")
 
 	cmd := &cobra.Command{
-		Use:   "run [<org-name>/][<project-name>/]<environment-name> [flags] -- [command]",
+		Use:   "run [<org-name>/][<project-name>/]<environment-name> [flags] [--] [command]",
 		Args:  cobra.ArbitraryArgs,
 		Short: "Open the environment with the given name and run a command.",
 		Long: fmt.Sprintf("Open the environment with the given name and run a command\n"+
@@ -109,7 +109,7 @@ func newEnvRunCmd(envcmd *envCommand) *cobra.Command {
 			"references in the command are not expanded by default. You should invoke the command\n"+
 			"inside a shell if you need environment variable expansion:\n"+
 			"\n"+
-			"    run -- %[1]s -c '\"echo $MY_ENV_VAR\"'\n"+
+			"    run <environment-name> -- %[1]s -c '\"echo $MY_ENV_VAR\"'\n"+
 			"\n"+
 			"The command to run is assumed to be non-interactive by default and its output\n"+
 			"streams are filtered to remove any secret values. Use the -i flag to run interactive\n"+
@@ -127,6 +127,10 @@ func newEnvRunCmd(envcmd *envCommand) *cobra.Command {
 				return err
 			}
 
+			// If -- was used but there's no arguments, it means no environment was specified before the command
+			if cmd.ArgsLenAtDash() == 0 {
+				return fmt.Errorf("no environment specified")
+			}
 			ref, args, err := envcmd.getExistingEnvRef(ctx, args)
 			if err != nil {
 				return err


Test patch:
diff --git a/cmd/esc/cli/testdata/run-no-env-error-with-separator.yaml b/cmd/esc/cli/testdata/run-no-env-error-with-separator.yaml
new file mode 100644
index 0000000..445bc8b
--- /dev/null
+++ b/cmd/esc/cli/testdata/run-no-env-error-with-separator.yaml
@@ -0,0 +1,10 @@
+run: |
+  esc run -- pulumi preview
+error: exit status 1
+
+---
+> esc run -- pulumi preview
+
+---
+> esc run -- pulumi preview
+Error: no environment specified
diff --git a/cmd/esc/cli/testdata/run-no-env-error.yaml b/cmd/esc/cli/testdata/run-no-env-error.yaml
new file mode 100644
index 0000000..445bc8b
--- /dev/null
+++ b/cmd/esc/cli/testdata/run-no-env-error.yaml
@@ -0,0 +1,10 @@
+run: |
+  esc run -- pulumi preview
+error: exit status 1
+
+---
+> esc run -- pulumi preview
+
+---
+> esc run -- pulumi preview
+Error: no environment specified