workers-sdk icon indicating copy to clipboard operation
workers-sdk copied to clipboard

🐛 BUG: `wrangler pages dev -- npm run serve` gives an "Unknown argument" error

Open MAXOUXAX opened this issue 2 years ago • 23 comments

What version of Wrangler are you using?

2.0.2

What operating system are you using?

Windows

Describe the Bug

I am currently giving WebStorm a try and I am encountering a problem when running the command below: wrangler pages dev --proxy 8080 -- npm run serve

Indeed, when this command is executed via a classic Command Prompt, everything works fine, but when it is executed from PowerShell, the following error is displayed: error

The command npm run serve works correctly.

MAXOUXAX avatar May 11 '22 16:05 MAXOUXAX

I just tried this and could not reproduce the error. Steps I took:

(Windows 10 - PowerShell %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe)

  • mkdir tmp
  • cd tmp
  • npm i -D wrangler
  • Update package.json with "scripts": { "serve": "echo FOO" }
  • npx wrangler pages dev --proxy 8080 -- npm run serve
🚧 'wrangler pages <command>' is a beta command. Please report any issues to https://github.com/cloudflare/wrangler2/issues/new/choose
Running npm run serve...
No functions. Shimming...
Serving at http://localhost:8788/
[proxy]:
> serve
> echo HERE


[proxy]: HERE

X [ERROR] Proxy exited with status 0.

petebacondarwin avatar May 11 '22 17:05 petebacondarwin

Can you provide a runnable example of the problem?

petebacondarwin avatar May 11 '22 17:05 petebacondarwin

Can confirm this is a bug since I am also having the same issue.

Using [email protected] and:

  • getting the exact same error when running the same command on [email protected]
  • getting no error when using the Command Prompt or Git Bash

I have this problem when I try to run the command just after cloning this project (so you can try by yourself): https://github.com/Flotss/maxouxax.me

Flotss avatar May 13 '22 11:05 Flotss

  • npx wrangler pages dev --proxy 8080 -- npm run serve

I just tried starting wrangler with npx, and the command works just fine when doing so. The command only fails when running wrangler using the wrangler command.

Flotss avatar May 13 '22 11:05 Flotss

  • npx wrangler pages dev --proxy 8080 -- npm run serve

I just tried starting wrangler with npx, and the command works just fine when doing so. The command only fails when running wrangler using the wrangler command.

The behavior is very strange since I just tried to run the command with and without npx, and both commands failed.

MAXOUXAX avatar May 13 '22 15:05 MAXOUXAX

Can you provide a runnable example of the problem?

👉 Here is a complete reproduction video from scratch, including PowerShell, npm and NodeJS versions

https://user-images.githubusercontent.com/24844231/169083212-6f83b3ce-96f6-4efb-967e-3e6f22077ec9.mp4

MAXOUXAX avatar May 18 '22 15:05 MAXOUXAX

Hi @MAXOUXAX - thanks for these reproduction steps. I am finally able to get the same problem and I think I have a workaround. The problem is that yargs is only looking for a single positional argument after the --. But it sees npm run serve as three separate positional arguments; and so it is complaining that serve is not a valid positional argument. You can get this to work by putting the command in double quotes: npx wrangler pages dev -- "npm run serve".

petebacondarwin avatar May 18 '22 20:05 petebacondarwin

I think I also have a fix which is very simple:

https://github.com/cloudflare/wrangler2/blob/7a191a2fd0cb08f2a80c29703a307286264ef74f/packages/wrangler/src/pages.tsx#L1247

needs to change to:

"dev [directory] [-- command..]",

telling Yargs that command is variadic.

petebacondarwin avatar May 18 '22 20:05 petebacondarwin

Glad you successfully reproduced the bug!

Although I never built a CLI and therefore never used yargs, the bug only happens in PowerShell, the command works just fine in other prompts, which would mean that yargs behaves differently between prompts, which is pretty odd, and looks like a bug to me.

Just to be sure I got that right, it's not an issue coming from wrangler, but an non-intended behavior coming from yargs, which needs to be fixed, right?

Thanks for the workaround though, will use this for now!

MAXOUXAX avatar May 18 '22 20:05 MAXOUXAX

I am not sure why the behaviour is different between command prompts. I guess different prompts pass the args in in different ways at the OS level?? Anyway, my PR (#1057) should be a reliable fix across all, and is arguably the right way to do this anyway.

petebacondarwin avatar May 18 '22 20:05 petebacondarwin

I think the issue needs to be reopened, because the command wrangler pages dev -- npm run serve is completely broken on PowerShell.

What happens is that it doesn't execute the command passed as a parameter, so it opens a web server that tries to proxy requests on port 8080, but fails completely because there's no web server on that port, since the given command hasn't been executed.

image

EDIT: I thought the command was broken for all prompts, but it actually works in all prompts except PowerShell. So the problem is simply not solved.

MAXOUXAX avatar May 19 '22 14:05 MAXOUXAX

Can you try with wrangler 2.0.6, does the problem persist?

threepointone avatar May 19 '22 14:05 threepointone

Can you try with wrangler 2.0.6, does the problem persist?

Oops, didn't mention I, of course, upgraded to [email protected]! As you can see in the screenshot, the command works fine, it's just not throwing an error anymore, but rather starting Wrangler and nothing else.

MAXOUXAX avatar May 19 '22 15:05 MAXOUXAX

Let me try this again...

petebacondarwin avatar May 19 '22 18:05 petebacondarwin

OK so the problem is that the yargs definition is dev [directory] [-- command..], where both directory and command are optional. In PowerShell it appears that yargs will place the first positional string (even after the --) into the directory option. So if you do: dev -- npm run serve you end up with directory === "npm" and command === "run serve" 😢 And even if you do dev -- "npm run serve" then you get directory === "npm run command" and command === undefined 😢

petebacondarwin avatar May 19 '22 19:05 petebacondarwin

So here is the problem...

In Windows Powershell when I use npx I am seeing:

process.argv = [ "node.exe", "path/to/wrangler/cli.js", "pages", "dev", "--proxy", "8080", "npm run serve"]

In Windows Command Prompt when I use npx I am seeing:

process.argv = [ "node.exe", "path/to/wrangler/cli.js", "pages", "dev", "--proxy", "8080", "--", "npm run serve"]

Spot the difference?

This seems to be something related to npx since if I run a simple node.js script:

node index.js foo -- bar

where index.js looks like:

console.log(process.argv);

Then I do see the -- in the process.argv...

petebacondarwin avatar May 19 '22 19:05 petebacondarwin

I think we might need to record this as a known issue in Pages + Powershell?

petebacondarwin avatar May 19 '22 19:05 petebacondarwin

You can get this to work by putting the command in double quotes: npx wrangler pages dev -- "npm run serve".

I just want to mention that even this solution doesn't work anymore since [email protected], so my current solution is to run the npm run serve command separately after running the wrangler command.

MAXOUXAX avatar May 20 '22 18:05 MAXOUXAX

I think this is the best workaround for now. @MAXOUXAX - did the original workaround (to use quotes) actually work with 2.0.5? I believe it would not have, since even with out the fix in 2.0.6 it would still have the same problem.

petebacondarwin avatar May 20 '22 19:05 petebacondarwin

Haven't tested the workaround in previous versions so cannot really tell

MAXOUXAX avatar May 20 '22 19:05 MAXOUXAX

Unassigning me for the time being so the Pages team can make a call on what to do next. If Powershell support for this is mandatory, then it might be that we move to a named option (e.g. --command="...") rather than relying upon the post -- positional argument support.

petebacondarwin avatar May 23 '22 09:05 petebacondarwin

Just tried this behaviour on wrangler 2.0.24, and I am unable to reproduce. Are you still experiencing it? This may also be due to my more up to date powershell version.

Get-Host | Select-Object Version

Version
-------
7.1.5

Skye-31 avatar Aug 05 '22 12:08 Skye-31

Just tried this behaviour on wrangler 2.0.24, and I am unable to reproduce. Are you still experiencing it? This may also be due to my more up to date powershell version.

Get-Host | Select-Object Version

Version
-------
7.1.5

Can confirm this is still not working, wrangler spins up the web server, but the npm command never gets executed. Please see the screenshot below which includes the PowerShell version, the wrangler version, and the NodeJS version image

MAXOUXAX avatar Aug 11 '22 22:08 MAXOUXAX

@MAXOUXAX The Pages team does not plan to support this sort of command-chaining in Powershell in the near future. As such, we are closing this issue.

jrf0110 avatar Oct 12 '22 14:10 jrf0110

@MAXOUXAX The Pages team does not plan to support this sort of command-chaining in Powershell in the near future. As such, we are closing this issue.

Alright, just a few things then:

  • Tried again with the newest version of wrangler (2.1.11) AND a new computer and the problem persists
  • Running the exact same command but prefixing 'npx' fixes it, the command works just fine that way
  • Maybe add a warning somewhere that running wrangler in PowerShell may not work

MAXOUXAX avatar Oct 12 '22 15:10 MAXOUXAX

This doesn't seem to be too much to ask for. One command below works and the other doesn't. It seems like a fix would be possible. Otherwise this need to be clearly spelt out in the documentation.

Works

npx wrangler pages dev --proxy 8080 -- npm run dev

Fails

wrangler pages dev --proxy 8080 -- npm run dev

nneil avatar Apr 01 '24 21:04 nneil

@nneil - what version of wrangler do you have installed globally?

Try running:

npx wrangler --version
wrangler --version

The difference between the two commands you used is that the first will use the locally installed version and the second will use the global one. We do not recommend using a globally installed one; instead you should install wrangler local to your project and then use npx (or equivalent command for other package managers) to run it.

petebacondarwin avatar Apr 01 '24 21:04 petebacondarwin

@petebacondarwin That's a good point, but I should have been clearer. I install nothing as a global: I would much rather waste some disk space and have precise control over my versions. I just put ./node_modules/.bin in my path so I don't need npx

PS> Get-Command wrangler

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
ExternalScript  wrangler.ps1                                                  C:\Users\nneil\Projects\cloudflare\node_modules\.bin\wrangler.ps1

nneil avatar Apr 02 '24 07:04 nneil

Ah, right. This is an issue with how Powershell works, not the version of Wrangler that you are running. Sorry I should have actually checked the thread of the issue before responding.

This is a known issue on a Pages command that we have deprecated. I think it would be nice to try to tell if you are trying to do this in PowerShell and add a warning. I'll open an issue to add that.

Otherwise, there is no plan to make this work.

petebacondarwin avatar Apr 02 '24 13:04 petebacondarwin