vite icon indicating copy to clipboard operation
vite copied to clipboard

--open false as a cli arg does not prevent browser from opening

Open drobannx opened this issue 5 years ago • 6 comments

⚠️ IMPORTANT ⚠️ Please do not ignore this template. If you do, your issue will be closed immediately.

  • [x] Read the docs.
  • [x] Make sure this is a Vite issue and not a framework-specific issue. For example, if it's a Vue SFC related bug, it should likely be reported to https://github.com/vuejs/vue-next instead.
  • [x] This is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.

Describe the bug

We are trying to use the cli argument --open false to prevent the browser from opening when starting up a server but it does not work. If we do add the config option in vite.config.ts it does work.

I tracked down the reason and this is due to a type mismatch when deciding whether to open the browser. Because the cli arg will always come through as a string 'false' will evaluate to truth in the block below.

if (options.open && !isRestart) {
  const path = typeof options.open === 'string' ? options.open : base;
  openBrowser(`${protocol}://${hostname}:${port}${path}`, true, server.config.logger);
}

My teammate and I did notice that depending on OS and browser default, the call in openBrowser will fail silently as well.

Reproduction

Any project that tries to use --open false as a cli arg.

System Info

  • vite version: 2.0.5
  • Operating System: OSx 10.15.7
  • Node version: v14.14.0
  • Package manager (npm/yarn/pnpm) and version: yarn v1.22.10

I am happy to open an MR if there is a specific direction to get this to work correctly. My first thought was just to check against 'false' if typeof options.open is a string and not even call openBrowser if that condition is met.

drobannx avatar Mar 16 '21 22:03 drobannx

Hello @drobannx. We totally like your proposal/feedback, welcome to send us a Pull Request for it. Please provide changelog/TypeScript/documentation/test cases if needed and make sure CI passed, we will review it soon. We appreciate your effort in advance and looking forward to your contribution!

github-actions[bot] avatar Mar 17 '21 19:03 github-actions[bot]

Hi, use --no-open to set open to false.

Since Vite doesn't open the browser by default, I'm not sure why you use --open false in the first place.

egoist avatar Mar 20 '21 11:03 egoist

Yeah as @egoist already said, I think we should not add the possibility to allow --open false but just use --no-open if really needed. And also by default it does not open :shrug:

@matias-capeletto I will give you the last word on this an feel free to close both, the issue and PR if you share the same opinion with us

Shinigami92 avatar Mar 21 '21 10:03 Shinigami92

I agree that the extra complexity doesn't look good here. Now "true" and "false" are no longer valid paths after this change (no idea if that would ever be needed but I would prefer to avoid that branching)

@drobannx could you explain a bit more about your use case and why --no-open is not enough?

patak-cat avatar Mar 21 '21 10:03 patak-cat

@matias-capeletto - --no-open is perfect for our use case, we weren't aware it was an option.

When we ran the vite --help command, we didn't see --no-open but this works perfectly.

What are your thoughts on an MR to update the docs and/or change the --help output to show --no-open as an option and remove boolean as a type to --open

drobannx avatar Mar 22 '21 14:03 drobannx

@drobannx Would love to see (and review) a docu update Could you create a PR for that? I think you don't need to create an extra issue for that, we will jump directly into the PR :slightly_smiling_face:

I will close this issue for now :+1:

Shinigami92 avatar Mar 22 '21 14:03 Shinigami92