dart-lang / pub

The pub command line tool
https://dart.dev/tools/pub/cmd
BSD 3-Clause "New" or "Revised" License
1.04k stars 229 forks source link

Support git bash for global packages on windows #3375

Open crtl opened 2 years ago

crtl commented 2 years ago

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
sigurdm commented 2 years ago

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 commented 2 years ago

Reopen if this doesn't solve the problem

crtl commented 2 years ago

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.

sigurdm commented 2 years ago

Can you type:

> $echo $PATH

in git bash?

crtl commented 2 years ago

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

sigurdm commented 2 years ago

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

crtl commented 2 years ago

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.
sigurdm commented 2 years ago

Hmmm, ok - what about running

$ fvm.bat

Does that work?

crtl commented 2 years ago

Yes that works.

sigurdm commented 2 years ago

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

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

cedvdb commented 2 years ago

Couldn't the .sh also be installed ?

sigurdm commented 2 years ago

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?

crtl commented 2 years ago

@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
cedvdb commented 2 years ago

in my case it is via git bash and not WSL

MarvinKweyu commented 4 months ago

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

sigurdm commented 4 months ago

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?

cedvdb commented 4 months ago

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

sigurdm commented 4 months ago

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

cedvdb commented 4 months ago

outside a WSL2 environment?

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

sigurdm commented 4 months ago

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

cedvdb commented 4 months ago

@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.

sigurdm commented 4 months ago

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 commented 4 months ago

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

andreyna1808 commented 1 month ago

@sigurdm

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

SandPod commented 1 month ago

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 "$@" ```
sigurdm commented 1 month ago

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

sigurdm commented 1 month ago

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.)

SandPod commented 1 month ago

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.

sigurdm commented 1 month ago

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

SandPod commented 1 month ago

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 commented 1 month ago

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

That is a great question that I don't know the answer to. My guess is that it depends on if WSL is available?

sigurdm commented 1 month ago

I guess at this point most people are using WSL2 right?

sigurdm commented 1 month ago

Seems we should read https://learn.microsoft.com/en-us/windows/wsl/file-permissions - it might contain the answers we need

SandPod commented 1 month ago

Strange, if I create the file (through powershell or whatever) and add some bash script content, for example

#!/usr/bin/env bash

echo "hello world"

It automatically gets the same permissions as we saw on the other scripts files, does windows maybe handle this automagically?

SandPod commented 1 month ago

This "permission promotion" even seem to happen for .txt files that are saved, as long as the content is a bash script.

Permissions when the file is empty:

-rw-r--r-- 1 alexa 197609 0 Oct  7 13:45 myscript.txt

Permissions when my simple hello world script is added through notepad:

-rwxr-xr-x 1 alexa 197609 41 Oct  7 13:46 myscript.txt*

🙃

sigurdm commented 1 month ago

Ah - that makes some sense. The executable bit is relying on the presence of the #!/ shebang. (Though I can find no documentation of this behavior in a cursory google)

That would mean we could:

That would ...probably work.

I wonder if there are backwards/forwards compatibility concerns with having two binstubs?

If you

I don't see any way around this, maybe we can live with it.

SandPod commented 1 month ago

Seems like an OK tradeoff.

Just one last consideration, the Flutter and Dart scripts always delegates the call to the .bat file. This could help solve the backwards compatibility issue, if the file we are delegating to is missing that could be handled gracefully.

There might also be other benefits to this, like a single source of truth for what is running?