pub icon indicating copy to clipboard operation
pub copied to clipboard

Support git bash for global packages on windows

Open crtl opened this issue 3 years ago • 37 comments

When using dart pub global activate to install packages on windows systems, the installed packages / commands are only available in windows CMD or PowerShell but not in Git Bash.

Packages installed via pub global should also be runnable in git bash for windows.

Reproduction:

  1. on Windows 10
  2. dart pub global activate fvm
  3. running fvm flutter --version in powershell/cmd/windows terminal works
  4. does not work in git bash

Ive also tested it with global installed devtools package wich has the same effect. Ive also installed make on my system and when running make in git bash it does not produce any errors.

Makefile:

test:
  fvm flutter --version  # works
git-bash > make test # works
git-bash > fvm flutter --version # does not work

Environment:

[✓] Flutter (Channel stable, 2.10.3, on Microsoft Windows [Version 10.0.19044.1586], locale en-DE)
    • Flutter version 2.10.3 at C:\Users\User\fvm\versions\2.10.3
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 7e9793dee1 (4 weeks ago), 2022-03-02 11:23:12 -0600
    • Engine revision bd539267b4
    • Dart version 2.16.1
    • DevTools version 2.9.2

crtl avatar Apr 01 '22 14:04 crtl

I think you need to set your PATH up in the git bash.

export PATH=<path-to-pub-cache>/bin:$PATH

The <path-to-pub-cache> will usually be /.pub-cache .

(If you are not using flutter but pure dart, it will be %LOCALAPPDATA%/Pub/Cache)

You can run it as a single command for every bash session or stick it in your bash config file (https://stackoverflow.com/questions/6883760/git-for-windows-bashrc-or-equivalent-configuration-files-for-git-bash-shell).

sigurdm avatar Apr 07 '22 08:04 sigurdm

Reopen if this doesn't solve the problem

sigurdm avatar Apr 07 '22 08:04 sigurdm

Git bash uses windows system PATH so it is not required to manually reset it every time it is started.

@sigurdm I am not able to reopen the issue.

crtl avatar Apr 07 '22 09:04 crtl

Can you type:

> $echo $PATH

in git bash?

sigurdm avatar Apr 07 '22 13:04 sigurdm

Yes and it prints my path as I configured it in windows system variables UI. Also in general when I have to manually install a CLI app I install it, then use the windows systemvariables UI to add the bin to path and then it is available in git bash.

$ echo $PATH /c/Users/User/bin:/mingw64/bin:/usr/local/bin:/usr/bin:/bin:/mingw64/bin:/usr/bin:/c/Users/User/bin:/c/Program Files (x86)/Common Files/Oracle/Java/javapath:/c/Windows/system32:/c/Windows:/c/Windows/System32/Wbem:/c/Windows/System32/WindowsPowerShell/v1.0:/c/Windows/System32/OpenSSH:/c/Program Files (x86)/NVIDIA Corporation/PhysX/Common:/e/dev/xampp/php_74:/c/ProgramData/ComposerSetup/bin:/c/Program Files (x86)/Google/Cloud SDK/google-cloud-sdk/bin:/c/ProgramData/chocolatey/bin:/c/Program Files (x86)/Yarn/bin:/c/WINDOWS/system32:/c/WINDOWS:/c/WINDOWS/System32/Wbem:/c/WINDOWS/System32/WindowsPowerShell/v1.0:/c/WINDOWS/System32/OpenSSH:/c/Program Files/Symfony:/c/HashiCorp/Vagrant/bin:/cmd:/c/Program Files/Docker/Docker/resources/bin:/c/ProgramData/DockerDesktop/version-bin:/c/Program Files/nodejs:/c/Users/User/AppData/Local/Programs/Python/Python38-32/Scripts:/c/Users/User/AppData/Local/Programs/Python/Python38-32:/c/Program Files/JetBrains/PhpStorm 2019.3.3/bin:/c/cygwin64/bin:/c/Program Files/nodejs:/c/Users/User/AppData/Roaming/Composer/vendor/bin:/c/Program Files (x86)/cwRsync/bin:/d/dev/jdk-17.0.1/bin:/c/Users/User/AppData/Local/Programs/Microsoft VS Code/bin:/d/dev/xampp/php_74:/d/dev/xampp/mysql/bin:/c/Program Files/JetBrains/PyCharm Community Edition 2020.2.1/bin:/c/Users/User/AppData/Local/Yarn/bin:/c/Users/User/AppData/Local/Microsoft/WindowsApps:/d/dev/jetbrains-toolbox-scripts:/c/Program Files/PostgreSQL/13/bin:/e/dev/xampp/php_8:/d/dev/bin:/c/Users/User/AppData/Local/Pub/Cache/bin:/d/dev/flutter/sdk/bin/cache/dart-sdk/bin:/d/dev/flutter/sdk/bin:/c/Users/User/AppData/Local/Android/Sdk/platform-tools:/c/Users/User/AppData/Roaming/npm:/usr/bin/vendor_perl:/usr/bin/core_perl

crtl avatar Apr 08 '22 11:04 crtl

Can you run: /c/Users/User/AppData/Local/Pub/Cache/bin/fvm.bat from inside git bash ?

sigurdm avatar Apr 08 '22 11:04 sigurdm

Yes:

$ /c/Users/User/AppData/Local/Pub/Cache/bin/fvm.bat
Flutter Version Management: A cli to manage Flutter SDK versions.

Usage: fvm <command> [arguments]

Global options:
-h, --help       Print this usage information.
    --verbose    Print verbose output.
    --version    current version

Available commands:
  config     Set configuration for FVM
  dart       Proxies Dart Commands
  doctor     Shows information about environment, and project configuration.
  flavor     Switches between different project flavors
  flutter    Proxies Flutter Commands
  global     Sets Flutter SDK Version as a global
  install    Installs Flutter SDK Version
  list       Lists installed Flutter SDK Versions
  releases   View all Flutter SDK releases available for install.
  remove     Removes Flutter SDK Version
  spawn      Spawns a command on a Flutter version
  use        Sets Flutter SDK Version you would like to use in a project

Run "fvm help <command>" for more information about a command.

crtl avatar Apr 08 '22 11:04 crtl

Hmmm, ok - what about running

$ fvm.bat

Does that work?

sigurdm avatar Apr 08 '22 11:04 sigurdm

Yes that works.

crtl avatar Apr 08 '22 11:04 crtl

Ok, I don't think we can do much more.

I guess that is the pain of working with bash on windows.

sigurdm avatar Apr 08 '22 12:04 sigurdm

Couldn't the .sh also be installed ?

cedvdb avatar Jul 25 '22 21:07 cedvdb

Couldn't the .sh also be installed ?

I guess that would work. I don't have much experience with WSL.

Can a file be marked executable on WSL? Should we always create the bash-script on windows or only if we detect that we are running under WSL?

sigurdm avatar Aug 04 '22 14:08 sigurdm

@sigurdm As far as I know WSL(2) is just a normal installation of a Linux Distro (Ubuntu in my case) with all its features the same it would run in a VM or on normal system. The Windows drives are mounted in /mnt and from Windows its accessed over \wsl$ Network drive. You also have the same permission system with users and groups and windows user has userid 1000.

user@DESKTOP-ID ~ $ ls -la
total 152
drwxr-xr-x 15 user user  4096 Aug  4 14:55 .
drwxr-xr-x  3 root   root    4096 Aug 17  2021 ..
lrwxrwxrwx  1 user user    24 Aug 17  2021 .aws -> /mnt/c/Users/user/.aws
lrwxrwxrwx  1 user user    26 Aug 17  2021 .azure -> /mnt/c/Users/user/.azure
-rw-------  1 user user 43331 Aug  4 14:55 .bash_history
-rw-r--r--  1 user user   220 Aug 17  2021 .bash_logout
-rw-r--r--  1 user user  4111 Jul 29 10:52 .bashrc
drwxr-xr-x  6 user user  4096 Aug  1 18:22 .cache
drwxr-xr-x  5 user user  4096 Sep  8  2021 .config
drwxr-xr-x  5 user user  4096 Jul 29 14:25 .docker
-rw-r--r--  1 user user   193 Aug 31  2021 .gitconfig
drwx------  3 user user  4096 Jul 29 17:47 .gnupg
drwx------  2 user user  4096 Aug  4 17:51 .keychain
drwxr-xr-x  2 user user  4096 Aug 17  2021 .landscape
drwxr-xr-x  3 user user  4096 Aug 17  2021 .local
-rw-r--r--  1 user user     0 Aug  4 10:16 .motd_shown
drwxr-xr-x  7 user user  4096 Sep 28  2021 .npm
-rw-r--r--  1 user user   807 Aug 17  2021 .profile
drwxr-xr-x  3 user user  4096 Jul 29 10:45 .ssh
-rw-r--r--  1 user user     0 Aug 17  2021 .sudo_as_admin_successful
-rw-------  1 user user 14506 Aug  1 09:50 .viminfo
drwxr-xr-x  5 user user  4096 Oct 18  2021 .vscode-server
-rw-r--r--  1 user user   183 Oct 18  2021 .wget-hsts
drwxr-xr-x  4 user user  4096 Jul 29 17:47 .yarn
-rw-r--r--  1 user user   116 Aug  4 13:04 .yarnrc
drwxr-xr-x  2 user user  4096 Nov 19  2021 node_modules
drwxr-xr-x  7 user user  4096 Aug  4 11:30 projects
-rw-r--r--  1 user user    86 Nov 19  2021 yarn.lock

crtl avatar Aug 04 '22 15:08 crtl

in my case it is via git bash and not WSL

cedvdb avatar Aug 04 '22 20:08 cedvdb

Does anyone have an idea about this on git bash yet?

MarvinKweyu avatar Jun 27 '24 19:06 MarvinKweyu

We still need to answer the question:

Should we always create the bash-script on windows or only if we detect that we are running under WSL2?

And also how do we detect we are running under wsl2?

sigurdm avatar Jun 28 '24 07:06 sigurdm

Should we always create the bash-script on windows or only if we detect that we are running under WSL2?

Always, it's not only about WSL. Is there any drawback on always doing it ? That behavior is not uncommon

cedvdb avatar Jun 28 '24 07:06 cedvdb

Can we always make it executable - also outside a WSL2 environment? And how (currently we shell out to chmod on linux/mac)

sigurdm avatar Jun 28 '24 08:06 sigurdm

outside a WSL2 environment?

Git bash (I don't think it uses WSL under the hood)

cedvdb avatar Jun 28 '24 09:06 cedvdb

I don't think we can assume git bash is installed on any system

sigurdm avatar Jun 28 '24 10:06 sigurdm

@sigurdm you asked :

Should we always create the bash-script on windows or only if we detect that we are running under WSL2?

It's not only for WSL2, git bash would use it, so definitely not only when WSL2 is used. Is it just a matter of adding that file ? If someone does not have git bash nor WSL2 there is no side effect, is there ? So I don't see why you'd have to assume anything. I'm probably misunderstanding something here.

cedvdb avatar Jun 28 '24 10:06 cedvdb

there is no side effect, is there ? So I don't see why not

That was basically also my question. I'm not very familiar with neither windows nor WSL, so i want to be careful in assumptions about how they work.

But even if we create the shell script along the .bat file - we still need to mark it executable for WSL2 to execute it - no? Or is everything considered executable?

sigurdm avatar Jun 28 '24 11:06 sigurdm

Maybe this https://github.com/microsoft/WSL/issues/2003#issuecomment-297173637 can be used as a work-around?

sigurdm avatar Jun 28 '24 11:06 sigurdm

@sigurdm

thanks, it worked for me to use the fvm.bat

andreyna1808 avatar Sep 15 '24 22:09 andreyna1808

When installing Dart and Flutter on windows a bash script is always supplied. Since the install instructions recommend adding them to the PATH I would assume that this is safe in general.

Copying and modifying this file to match the installed global package enables the command both in git bash and in powershell.

The generated file for Dart looks like this:
#!/usr/bin/env bash
# Copyright 2014 The Flutter Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

# ---------------------------------- NOTE ---------------------------------- #
#
# Please keep the logic in this file consistent with the logic in the
# `dart.bat` script in the same directory to ensure that Flutter & Dart continue
# to work across all platforms!
#
# -------------------------------------------------------------------------- #

set -e

# Needed because if it is set, cd may print the path it changed to.
unset CDPATH

# On Mac OS, readlink -f doesn't work, so follow_links traverses the path one
# link at a time, and then cds into the link destination and find out where it
# ends up.
#
# The returned filesystem path must be a format usable by Dart's URI parser,
# since the Dart command line tool treats its argument as a file URI, not a
# filename. For instance, multiple consecutive slashes should be reduced to a
# single slash, since double-slashes indicate a URI "authority", and these are
# supposed to be filenames. There is an edge case where this will return
# multiple slashes: when the input resolves to the root directory. However, if
# that were the case, we wouldn't be running this shell, so we don't do anything
# about it.
#
# The function is enclosed in a subshell to avoid changing the working directory
# of the caller.
function follow_links() (
  cd -P "$(dirname -- "$1")"
  file="$PWD/$(basename -- "$1")"
  while [[ -h "$file" ]]; do
    cd -P "$(dirname -- "$file")"
    file="$(readlink -- "$file")"
    cd -P "$(dirname -- "$file")"
    file="$PWD/$(basename -- "$file")"
  done
  echo "$file"
)

PROG_NAME="$(follow_links "${BASH_SOURCE[0]}")"
BIN_DIR="$(cd "${PROG_NAME%/*}" ; pwd -P)"
SHARED_NAME="$BIN_DIR/internal/shared.sh"
OS="$(uname -s)"

# If we're on Windows, invoke the batch script instead to get proper locking.
if [[ $OS =~ MINGW.* || $OS =~ CYGWIN.* || $OS =~ MSYS.* ]]; then
  exec "${BIN_DIR}/dart.bat" "$@"
fi

# To define `shared::execute()` function
source "$SHARED_NAME"

shared::execute "$@"

SandPod avatar Oct 07 '24 06:10 SandPod

@SandPod do you know if these files are marked as executable in some way?

sigurdm avatar Oct 07 '24 08:10 sigurdm

Also do we know if git bash runs flutter.bat or the bash file if you run flutter? (Maybe it doesn't matter if they have same behavior.)

sigurdm avatar Oct 07 '24 08:10 sigurdm

do you know if these files are marked as executable in some way?

Yes, these files show up as executables with these permissions:

-rwxr-xr-x 1 alexa 197609 2204 Oct  5 11:10 dart*
-rw-r--r-- 1 alexa 197609 1488 Oct  5 11:10 dart.bat
-rwxr-xr-x 1 alexa 197609 2436 Oct  5 11:10 flutter*
-rw-r--r-- 1 alexa 197609 2544 Oct  5 11:10 flutter.bat

Also do we know if git bash runs flutter.bat or the bash file if you run flutter?

It runs the bash file, if I modify the file with a simple echo statement at the top, all my flutter commands print this statement.

SandPod avatar Oct 07 '24 11:10 SandPod

Hmm - can we expect to always be able to run chmod on windows?

sigurdm avatar Oct 07 '24 11:10 sigurdm

Running into these issues because my Mac is being repaired and I'm using Windows for the week so feel free to ask more questions if it is needed :)

SandPod avatar Oct 07 '24 11:10 SandPod