workers-sdk
workers-sdk copied to clipboard
🐛 BUG: `wrangler pages dev -- npm run serve` gives an "Unknown argument" error
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:
The command npm run serve
works correctly.
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.
Can you provide a runnable example of the problem?
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
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.
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.
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
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"
.
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.
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!
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.
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.
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.
Can you try with wrangler 2.0.6, does the problem persist?
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.
Let me try this again...
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
😢
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
...
I think we might need to record this as a known issue in Pages + Powershell?
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.
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.
Haven't tested the workaround in previous versions so cannot really tell
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.
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
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
@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.
@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
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 - 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 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
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.