Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

error: unknown command with a -e flag (bun) #2205

Closed
kingmesal opened this issue Jun 3, 2024 · 8 comments
Closed

error: unknown command with a -e flag (bun) #2205

kingmesal opened this issue Jun 3, 2024 · 8 comments

Comments

@kingmesal
Copy link

I had the following code for version 9.4.1. When I upgraded to 12.1.0 this no longer works.

When I execute cli.ts debug -e dev

error: unknown command

When I execute cli.ts debug --env dev, it works fine.

If I change the code to use -a, --env <string> , then when I execute cli.ts debug -a dev it works fine.

cli.ts

const program = new Command();
program.addCommand(debug);

async function main() {
  await program.parseAsync();
}

debug.ts

const program = new Command("debug")
  .addOption(
    new Option("-e, --env <string>", "Target Environment")
      .default("dev")
      .env("ENV_NAME")
      .choices(["dev", "production"]),
  )
  .action(async (data) => {
    await debug(data as Args);
  });

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const debug = async (options: Args) => {
  console.log("hi");
}
@shadowspawn
Copy link
Collaborator

  1. I can't think of a change in Commander which would cause that.

  2. How are you running the TypesScript file? (What is after the #! if you have made your TypeScript runnable?) I am wondering is a newer version of that tool is being affected by the -e.

The unknown command error from Commander has single speech marks around the command, like:

% fab 'x'   
error: unknown command 'x'

Are you seeing those in the error message? (I am wondering if the error might be coming from something else, although looks pretty similar, so just checking!)

@kingmesal
Copy link
Author

i'm using bun to run it. I had someone else reproduce it with this on their machine and confirmed they also see this issue.

it is like the -e is causing it to blow up...

@shadowspawn
Copy link
Collaborator

shadowspawn commented Jun 4, 2024

I have reproduced what I think is the same issue. Try changing the parse call and see if it fixes your problem. From your example code:

// await program.parseAsync();
await program.parseAsync(process.argv);

What I suspect is happening is that bun is incorrectly including script arguments into process.execArgv and confusing Commander's auto-detection of command-line arguments.

// args.js
console.log({ argv: process.argv, execArgv: process.execArgv });
% node args.js debug -e dev   
{
  argv: [
    '/usr/local/bin/node',
    '/Users/john/Documents/Sandpits/commander/issues/2205/args.js',
    'debug',
    '-e',
    'dev'
  ],
  execArgv: []
}
% bun run args.js debug -e dev
{
  argv: [ "/opt/homebrew/Cellar/bun/1.1.9/bin/bun", "/Users/john/Documents/Sandpits/commander/issues/2205/args.js",
    "debug", "-e", "dev"
  ],
  execArgv: [ "-e" ],
}

Running a program based on your example code, the script name is being parsed by Commander as a command name:

% bun run cli.ts debug -e dev
error: unknown command '/Users/john/Documents/Sandpits/commander/issues/2205/cli.ts'

Reference: https://nodejs.org/docs/latest/api/process.html#processexecargv

The process.execArgv property returns the set of Node.js-specific command-line options passed when the Node.js process was launched. These options do not appear in the array returned by the process.argv property, and do not include the Node.js executable, the name of the script, or any options following the script name.

@shadowspawn shadowspawn changed the title error: unknown command with a -e flag error: unknown command with a -e flag (bun) Jun 4, 2024
@shadowspawn
Copy link
Collaborator

This has appeared in Commander v12.1.0 because of #2164 which added detection of -e in process.execArgv.

@kingmesal
Copy link
Author

Yes, that does allow it to run through the command chain.

@shadowspawn
Copy link
Collaborator

I have opened an issue against Bun. If it doesn't get positive activity, I will add a work-around in Commander.

oven-sh/bun#11673

@paperdave
Copy link

paperdave commented Jun 20, 2024

Will be fixed in oven-sh/bun#11987, likely landing in Bun 1.1.15 or 16 (probably within a week)

@shadowspawn
Copy link
Collaborator

This is fixed in Bun v1.1.16, thanks to @paperdave

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants